]> git.hungrycats.org Git - linux/commitdiff
ipc/shm: fix shmat() nil address after round-down when remapping
authorDavidlohr Bueso <dave@stgolabs.net>
Fri, 25 May 2018 21:47:30 +0000 (14:47 -0700)
committerBen Hutchings <ben@decadent.org.uk>
Sun, 21 Oct 2018 07:46:20 +0000 (08:46 +0100)
commit 8f89c007b6dec16a1793cb88de88fcc02117bbbc upstream.

shmat()'s SHM_REMAP option forbids passing a nil address for; this is in
fact the very first thing we check for.  Andrea reported that for
SHM_RND|SHM_REMAP cases we can end up bypassing the initial addr check,
but we need to check again if the address was rounded down to nil.  As
of this patch, such cases will return -EINVAL.

Link: http://lkml.kernel.org/r/20180503204934.kk63josdu6u53fbd@linux-n805
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Reported-by: Andrea Arcangeli <aarcange@redhat.com>
Cc: Joe Lawrence <joe.lawrence@redhat.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
ipc/shm.c

index d8618f08bcf4816991537a0203c101764a21ff35..6d96289241a92d5f2da8fc049ff04e154e924b55 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -1112,9 +1112,17 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
                goto out;
        else if ((addr = (ulong)shmaddr)) {
                if (addr & (shmlba - 1)) {
-                       if (shmflg & SHM_RND)
+                       if (shmflg & SHM_RND) {
                                addr &= ~(shmlba - 1);  /* round down */
-                       else
+
+                               /*
+                                * Ensure that the round-down is non-nil
+                                * when remapping. This can happen for
+                                * cases when addr < shmlba.
+                                */
+                               if (!addr && (shmflg & SHM_REMAP))
+                                       goto out;
+                       } else
 #ifndef __ARCH_FORCE_SHMLBA
                                if (addr & ~PAGE_MASK)
 #endif