Skip to contentSkip to conversation

ipc/shm: shmat() fixes around nil-page

PATCH Applied Davidlohr Bueso 2 participants 5 messages May 3, 2018 – May 14, 2018 Memory management

lore
Share & tools
lore
#0 lore raw

cover ipc/shm: shmat() fixes around nil-page

Hi,

These patches fix two issues reported[1] a while back by Joe and Andrea around how shmat(2) behaves with nil-page.

The first reverts a commit that it was incorrectly thought that mapping nil-page (address=0) was a no no with MAP_FIXED. This is not the case, with the exception of SHM_REMAP; which is address in the second patch.

I chose two patches because it is easier to backport and it explicitly reverts bogus behaviour. Both patches ought to be in -stable and ltp testcases need updated (the added testcase around the cve can be modified to just test for SHM_RND|SHM_REMAP).

[1] lkml.kernel.org/r/20180430172152.nfa564pvgpk3ut7p@linux-n805

Thanks!

Davidlohr Bueso (2):
Revert "ipc/shm: Fix shmat mmap nil-page protection" ipc/shm: fix shmat() nil address after round-down when remapping

 ipc/shm.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
Signature · 1 line
2.13.6
#1 lore raw

PATCH 1/2 Revert "ipc/shm: Fix shmat mmap nil-page protection"

95e91b831f87 (ipc/shm: Fix shmat mmap nil-page protection) worked on the idea that we should not be mapping as root addr=0 and MAP_FIXED. However, it was reported that this scenario is in fact valid, thus making the patch both bogus and breaks userspace as well. For example X11's libint10.so relies on shmat(1, SHM_RND) for lowmem initialization[1].

[1] https://cgit.freedesktop.org/xorg/xserver/tree/hw/xfree86/os-support/linux/int10/linux.c#n347

---
 ipc/shm.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
ipc/shm.c +2 -7
index 0075990338f4..b81d53c8f459 100644
@@ -1371,13 +1371,8 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
 
 	if (addr) {
 		if (addr & (shmlba - 1)) {
-			/*
-			 * Round down to the nearest multiple of shmlba.
-			 * For sane do_mmap_pgoff() parameters, avoid
-			 * round downs that trigger nil-page and MAP_FIXED.
-			 */
-			if ((shmflg & SHM_RND) && addr >= shmlba)
-				addr &= ~(shmlba - 1);
+			if (shmflg & SHM_RND)
+				addr &= ~(shmlba - 1);  /* round down */
 			else
 #ifndef __ARCH_FORCE_SHMLBA
 				if (addr & ~PAGE_MASK)
Signature · 1 line
2.13.6
#2 lore raw

PATCH 2/2 ipc/shm: fix shmat() nil address after round-down when remapping

In reply to Davidlohr Bueso

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.

---
ipc/shm.c | 12 ++++++++++--

1 file changed, 10 insertions(+), 2 deletions(-)
ipc/shm.c +0 -0
index b81d53c8f459..29978ee76c2e 100644
@@ -1371,9 +1371,17 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
 
	if (addr) {
		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

Signature · 1 line
2.13.6
#3 lore raw
In reply to Davidlohr Bueso
Quoted 8 lines · Davidlohr Bueso

Hi,

These patches fix two issues reported[1] a while back by Joe and Andrea around how shmat(2) behaves with nil-page.

The first reverts a commit that it was incorrectly thought that mapping nil-page (address=0) was a no no with MAP_FIXED. This is not the case, with the exception of SHM_REMAP; which is address in the second patch.

Can you add appropriate Fixes: tags if possible? I guess patch 1 is clear, dunno about patch 2...

Quoted 4 lines

I chose two patches because it is easier to backport and it explicitly reverts bogus behaviour. Both patches ought to be in -stable and ltp testcases need updated (the added testcase around the cve can be modified to just test for SHM_RND|SHM_REMAP).

CC'd ltp so they know :)

Thanks,
Vlastimil

Quoted 12 lines

[1] lkml.kernel.org/r/20180430172152.nfa564pvgpk3ut7p@linux-n805

Thanks!

Davidlohr Bueso (2):
Revert "ipc/shm: Fix shmat mmap nil-page protection" ipc/shm: fix shmat() nil address after round-down when remapping

 ipc/shm.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
#4 lore raw
Quoted 2 lines · Vlastimil Babka

Can you add appropriate Fixes: tags if possible? I guess patch 1 is clear, dunno about patch 2...

Right, patch 1 would still benefit from Fixes tag. As to patch 2, the bug as been there since forever (pre-dates git history). But yeah, as both come together and are related to a certain extent, the same Fixes could serve both patches.

Thanks,
Davidlohr