Skip to content

Commit

Permalink
ipc/shm: avoid ipc_rcu_alloc()
Browse files Browse the repository at this point in the history
Instead of using ipc_rcu_alloc() which only performs the refcount bump,
open code it.  This also allows for shmid_kernel structure layout to be
randomized in the future.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Kees Cook <[email protected]>
Signed-off-by: Manfred Spraul <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
kees authored and torvalds committed Jul 12, 2017
1 parent 101ede0 commit 3e0c240
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions ipc/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,19 @@ static const struct vm_operations_struct shm_vm_ops = {
#endif
};

static struct shmid_kernel *shm_alloc(void)
{
struct shmid_kernel *shp;

shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
if (unlikely(!shp))
return NULL;

atomic_set(&shp->shm_perm.refcount, 1);

return shp;
}

/**
* newseg - Create a new shared memory segment
* @ns: namespace
Expand Down Expand Up @@ -548,10 +561,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
ns->shm_tot + numpages > ns->shm_ctlall)
return -ENOSPC;

BUILD_BUG_ON(offsetof(struct shmid_kernel, shm_perm) != 0);

shp = container_of(ipc_rcu_alloc(sizeof(*shp)), struct shmid_kernel,
shm_perm);
shp = shm_alloc();
if (!shp)
return -ENOMEM;

Expand Down

0 comments on commit 3e0c240

Please sign in to comment.