Skip to contentSkip to conversation

bug in generic strncpy_from_user

DISCUSSION Heiko Carstens 2 participants 7 messages Feb 26, 2013 – Feb 27, 2013 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#2 lore raw
In reply to a message not in the archive
Quoted 17 lines · Linus Torvalds
Quoted 15 lines · Heiko Carstens

FWIW, I think the generic strncpy_from_user() implemention has the same bug as the s390 variant:

Following example:

If there is a NUL terminated string "a\0" starting at e.g. 0xfffe this will cause an (invalid) page fault for the subsequent page at 0x10000 in case of CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS for e.g. this invocation:

strncpy_from_user(kname, 0xfffe, 4096);

If the user space page that contained the string was the last within a VMA this may cause a -EFAULT return value for the __get_user() invocation in strncpy_from_user() and byte-at-a-time processing will happen.

Quoted 6 lines
Quoted 4 lines

This is true and works fine unless the next VMA is the stack.

With "ulimit -s unlimited" it will just grow downwards to where the page fault happened instead that -EFAULT will be returned.

This should _never_ be the case.

I was wrong. -EFAULT will be returned, however the vma will grow nevertheless. Which in turn will cause trouble.

Quoted 6 lines

Our stack-down growing handling requires the whole guard page, and should never grow down to touch the previous vma.

If it does, that's a bug in our stack growing, not in strncpy_from_user(). Which could definitely be the case, of course. But we should fix it there, not change the strncpy.

After all expand_stack() simply expands the stack to the requested address, since the guard page is considered to be not included it will just grow exactly down to the previous mapping.
Only in handle_mm_fault() afterwards there is a check if the access happened within the guard page which then will make the access fault. However the stack vma has already grown and user space is broken afterwards.

Here an example: this is before the kernel accesses memory right behind the ld.so.cache mapping:

[root@p2345007 ~]# cat /proc/2285/maps

00400000-00401000 r-xp 00000000 5e:01 148858                             /root/a.out
00401000-00402000 rw-p 00000000 5e:01 148858                             /root/a.out
40000000-4001c000 r-xp 00000000 5e:01 260585                             /lib/ld-2.14.1.so
4001c000-4001e000 rw-p 0001b000 5e:01 260585                             /lib/ld-2.14.1.so
4001e000-40020000 r-xp 00000000 00:00 0                                  [vdso]

40020000-40023000 rw-p 00000000 00:00 0
40023000-4002c000 r--p 00000000 5e:01 39 /etc/ld.so.cache
7ffdf000-80000000 rw-p 00000000 00:00 0 [stack]

Then an "open" system call to the file "/lib/libc.so.6" happens. The string is null terminated and completely fits into the 0x4002b000 user space page. However when copying from user space with strncpy_from_user() the kernel also reads from the 0x4002c000 page.
This generates a fault and grows the stack vma downwards to 0x4002c000:

[root@p2345007 ~]# cat /proc/2285/maps

00400000-00401000 r-xp 00000000 5e:01 148858                             /root/a.out
00401000-00402000 rw-p 00000000 5e:01 148858                             /root/a.out
40000000-4001c000 r-xp 00000000 5e:01 260585                             /lib/ld-2.14.1.so
4001c000-4001e000 rw-p 0001b000 5e:01 260585                             /lib/ld-2.14.1.so
4001e000-40020000 r-xp 00000000 00:00 0                                  [vdso]

40020000-40023000 rw-p 00000000 00:00 0
40023000-4002c000 r--p 00000000 5e:01 39 /etc/ld.so.cache
4002d000-80000000 rw-p 00000000 00:00 0 [stack]

/proc/maps explicitly excludes the guard page, so the vma of the stack indeed starts at 0x4002c000. Only every access to the guard page will still fault.

#3 lore raw
Quoted 3 lines · Heiko Carstens

I was wrong. -EFAULT will be returned, however the vma will grow nevertheless. Which in turn will cause trouble.

Ok. We should fix that too.

There whole "access just past the end of the previous vma" should never cause the stack above to expand. The guard page at least gives people a SIGSEGV, but one of the main reasons for the guard page was actually to make sure that new "mmap()" calls do not create mappings just under the stack (in addition to the obvious SIGSEGV when you then access into that thing).

So while part of the meaning of the guard page is to get that SIGSEGV, that part is "for safetly". And apparently it works. But at the same time, there is absolutely no reason to ever expand the stack only to hit the guard page _anyway_, so if the stack expansion will cause the requested address to be in the guard page, then the stack expansion should just have failed.

I think the problem is that we add the guard page *after* we do the normal "let's try to expand" logic.

I'll take a look.

Linus

#4 lore raw
Quoted 5 lines · Linus Torvalds

I think the problem is that we add the guard page *after* we do the normal "let's try to expand" logic.

I'll take a look.

Ahh, no. The guard page logic happens later at the fault time. We do this in two phases - first "find_extend_vma()" does what the name claims, and then check_stack_guard_page() is done for the last-page case from within do_anonymous_page() when we actually touch the last page itself.

But that's actually fine. We can simply make "find_extend_vma()" do the obvious "refuse to extend the vma all the way", because we will later allow the guard page to extend downwards to "touch" the mapping, but that uses separate logic. So the attached trivial patch seems to make perfect sense:

It is totally untested, though. Does it work for you (and we should do the same thing for the grows-up case, obviously)?

Linus

 mm/mmap.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mmap.c +2 -0
index 318e121affda..ae47c1609d99 100644
@@ -2226,6 +2226,8 @@ find_extend_vma(struct mm_struct * mm, unsigned long addr)
 		return vma;
 	if (!(vma->vm_flags & VM_GROWSDOWN))
 		return NULL;
+	if (vma->vm_prev && vma->vm_prev->vm_end == addr)
+		return NULL;
 	start = vma->vm_start;
 	if (expand_stack(vma, addr))
 		return NULL;
#5 lore raw
Quoted 3 lines · Linus Torvalds

It is totally untested, though. Does it work for you (and we should do the same thing for the grows-up case, obviously)?

Ok, thinking about this more, this is not good enough.

There is one case where growing into the previous/next vma is perfectly ok: when the previous/next vma is the same stack vma, and the reason we expanded it was because we had somehow unmapped the stack in the middle.

Which isn't common or normal, but it has been known to happen. People doing things like unmapping sensitive data in the middle of the stack or whatever. We explicitly allow that case for the stack guard page for that reason.

So I'll need to extend on that logic a bit more.

Linus

#6 lore raw
Quoted 2 lines · Linus Torvalds

So I'll need to extend on that logic a bit more.

Ok, here's the fleshed-out patch. Also fixed to use "expand_stack()", which is what the page fault handling actually uses. And it has the GROWSUP case as well as a comment about this all.

So it all looks good. But it's still totally and entirely untested.

Because that's how I roll. Gangsta life!

Linus

 mm/mmap.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
mm/mmap.c +27 -0
index 318e121affda..2fd9f03eee6f 100644
@@ -2185,9 +2185,28 @@ int expand_downwards(struct vm_area_struct *vma,
 	return error;
 }
 
+/*
+ * Note how expand_stack() refuses to expand the stack all the way to
+ * abut the next virtual mapping, *unless* that mapping itself is also
+ * a stack mapping. We want to leave room for a guard page, after all
+ * (the guard page itself is not added here, that is done by the
+ * actual page faulting logic)
+ *
+ * This matches the behavior of the guard page logic (see mm/memory.c:
+ * check_stack_guard_page()), which only allows the guard page to be
+ * removed under these circumstances.
+ */
 #ifdef CONFIG_STACK_GROWSUP
 int expand_stack(struct vm_area_struct *vma, unsigned long address)
 {
+	struct vm_area_struct *next;
+
+	address &= PAGE_MASK;
+	next = vma->vm_next;
+	if (next && next->vm_start == address + PAGE_SIZE) {
+		if (!(next->vm_flags & VM_GROWSUP))
+			return -ENOMEM;
+	}
 	return expand_upwards(vma, address);
 }
 
@@ -2209,6 +2228,14 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr)
 #else
 int expand_stack(struct vm_area_struct *vma, unsigned long address)
 {
+	struct vm_area_struct *prev;
+
+	address &= PAGE_MASK;
+	prev = vma->vm_prev;
+	if (prev && prev->vm_end == address) {
+		if (!(prev->vm_flags & VM_GROWSDOWN))
+			return -ENOMEM;
+	}
 	return expand_downwards(vma, address);
 }
 
author Tested-by
#7 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 2 lines · Linus Torvalds

So I'll need to extend on that logic a bit more.

Ok, here's the fleshed-out patch. Also fixed to use "expand_stack()", which is what the page fault handling actually uses. And it has the GROWSUP case as well as a comment about this all.

So it all looks good. But it's still totally and entirely untested.

Tested with 64 bit kernel with 64 bit and 31 bit mode user space as well as with 31 bit kernel and 31 bit user space. Each with legacy and flexible mmap layout.
The bug is fixed and everything else still seems to work. Thanks!

#8 lore raw
Quoted 7 lines · Heiko Carstens

Tested with 64 bit kernel with 64 bit and 31 bit mode user space as well as with 31 bit kernel and 31 bit user space. Each with legacy and flexible mmap layout.
The bug is fixed and everything else still seems to work. Thanks!

Ok, committed.

I'm not marking it for stable, because it doesn't seem to matter all that much, but if there was some application that actually broke and made you notice it that way (as opposed to you just noticing the odd memory map), you might want to inform the stable team about commit 09884964335e.

Linus