Skip to content

Commit 3e0c240

Browse files
keestorvalds
authored andcommitted
ipc/shm: avoid ipc_rcu_alloc()
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/20170525185107.12869-11-manfred@colorfullife.com Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 101ede0 commit 3e0c240

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎ipc/shm.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ static const struct vm_operations_struct shm_vm_ops = {
518518
#endif
519519
};
520520

521+
static struct shmid_kernel *shm_alloc(void)
522+
{
523+
struct shmid_kernel *shp;
524+
525+
shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
526+
if (unlikely(!shp))
527+
return NULL;
528+
529+
atomic_set(&shp->shm_perm.refcount, 1);
530+
531+
return shp;
532+
}
533+
521534
/**
522535
* newseg - Create a new shared memory segment
523536
* @ns: namespace
@@ -548,10 +561,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
548561
ns->shm_tot + numpages > ns->shm_ctlall)
549562
return -ENOSPC;
550563

551-
BUILD_BUG_ON(offsetof(struct shmid_kernel, shm_perm) != 0);
552-
553-
shp = container_of(ipc_rcu_alloc(sizeof(*shp)), struct shmid_kernel,
554-
shm_perm);
564+
shp = shm_alloc();
555565
if (!shp)
556566
return -ENOMEM;
557567

0 commit comments

Comments
 (0)