ipc/msg: avoid ipc_rcu_alloc()
authorKees Cook <[email protected]>
Wed, 12 Jul 2017 21:35:07 +0000 (14:35 -0700)
committerLinus Torvalds <[email protected]>
Wed, 12 Jul 2017 23:26:02 +0000 (16:26 -0700)
Instead of using ipc_rcu_alloc() which only performs the refcount bump,
open code it.  This also allows for msg_queue 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]>
ipc/msg.c

index 25d43e27ef12cd6d253bbbca733f0650929276a2..10094a731b8eed773cdbac20e94bd05f3b3cb072 100644 (file)
--- a/ipc/msg.c
+++ b/ipc/msg.c
@@ -109,6 +109,19 @@ static void msg_rcu_free(struct rcu_head *head)
        __msg_free(msq);
 }
 
+static struct msg_queue *msg_alloc(void)
+{
+       struct msg_queue *msq;
+
+       msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+       if (unlikely(!msq))
+               return NULL;
+
+       atomic_set(&msq->q_perm.refcount, 1);
+
+       return msq;
+}
+
 /**
  * newque - Create a new msg queue
  * @ns: namespace
@@ -123,10 +136,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
        key_t key = params->key;
        int msgflg = params->flg;
 
-       BUILD_BUG_ON(offsetof(struct msg_queue, q_perm) != 0);
-
-       msq = container_of(ipc_rcu_alloc(sizeof(*msq)), struct msg_queue,
-                               q_perm);
+       msq = msg_alloc();
        if (!msq)
                return -ENOMEM;