* [PATCH 2/2] make shm_get_stat() more robust
@ 2009-01-27 22:48 Tony Battersby
2009-01-27 23:00 ` Andrew Morton
0 siblings, 1 reply; 2+ messages in thread
From: Tony Battersby @ 2009-01-27 22:48 UTC (permalink / raw)
To: linux-kernel; +Cc: Jiri Olsa, Jiri Kosina, Andrew Morton
shm_get_stat() assumes idr_find(&shm_ids(ns).ipcs_idr) returns
"struct shmid_kernel *"; all other callers assume that it returns
"struct kern_ipc_perm *". This works because "struct kern_ipc_perm"
is currently the first member of "struct shmid_kernel", but it would
be better to use container_of() to prevent future breakage.
Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
---
--- linux-2.6.29-rc2-git3/ipc/shm.c.orig 2009-01-27 16:23:10.000000000 -0500
+++ linux-2.6.29-rc2-git3/ipc/shm.c 2009-01-27 16:24:19.000000000 -0500
@@ -551,12 +551,14 @@ static void shm_get_stat(struct ipc_name
in_use = shm_ids(ns).in_use;
for (total = 0, next_id = 0; total < in_use; next_id++) {
+ struct kern_ipc_perm *ipc;
struct shmid_kernel *shp;
struct inode *inode;
- shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
- if (shp == NULL)
+ ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
+ if (ipc == NULL)
continue;
+ shp = container_of(ipc, struct shmid_kernel, shm_perm);
inode = shp->shm_file->f_path.dentry->d_inode;
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH 2/2] make shm_get_stat() more robust
2009-01-27 22:48 [PATCH 2/2] make shm_get_stat() more robust Tony Battersby
@ 2009-01-27 23:00 ` Andrew Morton
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2009-01-27 23:00 UTC (permalink / raw)
To: Tony Battersby; +Cc: linux-kernel, olsajiri, jkosina
On Tue, 27 Jan 2009 17:48:13 -0500
Tony Battersby <tonyb@cybernetics.com> wrote:
> shm_get_stat() assumes idr_find(&shm_ids(ns).ipcs_idr) returns
> "struct shmid_kernel *"; all other callers assume that it returns
> "struct kern_ipc_perm *". This works because "struct kern_ipc_perm"
> is currently the first member of "struct shmid_kernel", but it would
> be better to use container_of() to prevent future breakage.
>
> Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
> ---
> --- linux-2.6.29-rc2-git3/ipc/shm.c.orig 2009-01-27 16:23:10.000000000 -0500
> +++ linux-2.6.29-rc2-git3/ipc/shm.c 2009-01-27 16:24:19.000000000 -0500
> @@ -551,12 +551,14 @@ static void shm_get_stat(struct ipc_name
> in_use = shm_ids(ns).in_use;
>
> for (total = 0, next_id = 0; total < in_use; next_id++) {
> + struct kern_ipc_perm *ipc;
> struct shmid_kernel *shp;
> struct inode *inode;
>
> - shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
> - if (shp == NULL)
> + ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
> + if (ipc == NULL)
> continue;
> + shp = container_of(ipc, struct shmid_kernel, shm_perm);
>
> inode = shp->shm_file->f_path.dentry->d_inode;
>
yup, well spotted.
It would be good to add a little typesafe wrapper:
static inline struct kern_ipc_perm *shm_idr_find(struct ipc_ids *ipc_ids)
{
return idr_find(&ipc_ids->ipcs_idr);
}
(or similar)
so that this sort of mistake cannot happen again.
As you've found, open-coded use of a bare void*-returning function is a
bit dangerous.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-01-27 23:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-27 22:48 [PATCH 2/2] make shm_get_stat() more robust Tony Battersby
2009-01-27 23:00 ` Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox