Skip to contentSkip to conversation

binfmt_elf: fix calculations for bss padding

PATCH Applied Kees Cook 2 participants 9 messages Jul 8, 2016 – Jul 13, 2016

Subsystems (4)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

cover binfmt_elf: fix calculations for bss padding

This fixes a double-bug in ELF loading, as noticed by Hector Marco-Gisbert. To quote his original email:

The size of the bss section for some interpreters is not correctly calculated resulting in unnecessary calls to vm_brk() with enormous size values.

The bug appears when loading some interpreters with a small bss size. Once the last loadable segment has been loaded, the bss section is zeroed up to the page boundary and the elf_bss variable is updated to this new page boundary. Because of this update (alignment), the last_bss could be less than elf_bss and the subtraction "last_bss - elf_bss" value could overflow.
...
[e.g.] The size value requested to the vm_brk() call (last_bss - elf_bss) is 0xfffffffffffff938 and internally this size is page aligned in the do_brk() function resulting in a 0 length request.

This series takes a slightly different approach to fixing it and updates vm_brk to refuse bad allocation sizes.

-Kees

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#1 lore raw

PATCH 1/2 binfmt_elf: fix calculations for bss padding

A double-bug exists in the bss calculation code, where an overflow can happen in the "last_bss - elf_bss" calculation, but vm_brk internally aligns the argument, underflowing it, wrapping back around safe. We shouldn't depend on these bugs staying in sync, so this cleans up the bss padding handling to avoid the overflow.

This moves the bss padzero() before the last_bss > elf_bss case, since the zero-filling of the ELF_PAGE should have nothing to do with the relationship of last_bss and elf_bss: any trailing portion should be zeroed, and a zero size is already handled by padzero().

Then it handles the math on elf_bss vs last_bss correctly. These need to both be ELF_PAGE aligned to get the comparison correct, since that's the expected granularity of the mappings. Since elf_bss already had alignment-based padding happen in padzero(), the "start" of the new vm_brk() should be moved forward as done in the original code. However, since the "end" of the vm_brk() area will already become PAGE_ALIGNed in vm_brk() then last_bss should get aligned here to avoid hiding it as a side-effect.

Additionally makes a cosmetic change to the initial last_bss calculation so it's easier to read in comparison to the load_addr calculation above it (i.e. the only difference is p_filesz vs p_memsz).

---
 fs/binfmt_elf.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
fs/binfmt_elf.c +18 -16
index e158b22ef32f..fe948933bcc5 100644
@@ -605,28 +605,30 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
 			 * Do the same thing for the memory mapping - between
 			 * elf_bss and last_bss is the bss section.
 			 */
-			k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
+			k = load_addr + eppnt->p_vaddr + eppnt->p_memsz;
 			if (k > last_bss)
 				last_bss = k;
 		}
 	}
 
+	/*
+	 * Now fill out the bss section: first pad the last page from
+	 * the file up to the page boundary, and zero it from elf_bss
+	 * up to the end of the page.
+	 */
+	if (padzero(elf_bss)) {
+		error = -EFAULT;
+		goto out;
+	}
+	/*
+	 * Next, align both the file and mem bss up to the page size,
+	 * since this is where elf_bss was just zeroed up to, and where
+	 * last_bss will end after the vm_brk() below.
+	 */
+	elf_bss = ELF_PAGEALIGN(elf_bss);
+	last_bss = ELF_PAGEALIGN(last_bss);
+	/* Finally, if there is still more bss to allocate, do it. */
 	if (last_bss > elf_bss) {
-		/*
-		 * Now fill out the bss section.  First pad the last page up
-		 * to the page boundary, and then perform a mmap to make sure
-		 * that there are zero-mapped pages up to and including the
-		 * last bss page.
-		 */
-		if (padzero(elf_bss)) {
-			error = -EFAULT;
-			goto out;
-		}
-
-		/* What we have mapped so far */
-		elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
-
-		/* Map the last of the bss segment */
 		error = vm_brk(elf_bss, last_bss - elf_bss);
 		if (error)
 			goto out;
Signature · 7 lines
2.7.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#2 lore raw
Quoted 20 lines · Kees Cook

A double-bug exists in the bss calculation code, where an overflow can happen in the "last_bss - elf_bss" calculation, but vm_brk internally aligns the argument, underflowing it, wrapping back around safe. We shouldn't depend on these bugs staying in sync, so this cleans up the bss padding handling to avoid the overflow.

This moves the bss padzero() before the last_bss > elf_bss case, since the zero-filling of the ELF_PAGE should have nothing to do with the relationship of last_bss and elf_bss: any trailing portion should be zeroed, and a zero size is already handled by padzero().

Then it handles the math on elf_bss vs last_bss correctly. These need to both be ELF_PAGE aligned to get the comparison correct, since that's the expected granularity of the mappings. Since elf_bss already had alignment-based padding happen in padzero(), the "start" of the new vm_brk() should be moved forward as done in the original code. However, since the "end" of the vm_brk() area will already become PAGE_ALIGNed in vm_brk() then last_bss should get aligned here to avoid hiding it as a side-effect.

Quoted 6 lines

Additionally makes a cosmetic change to the initial last_bss calculation so it's easier to read in comparison to the load_addr calculation above it (i.e. the only difference is p_filesz vs p_memsz).

Andrew or Al, can you pick this up for -next? It doesn't depend on the do_brk() fix (patch 2/2)...

-Kees

Quoted 58 lines
---
 fs/binfmt_elf.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
fs/binfmt_elf.c +18 -17
index e158b22ef32f..fe948933bcc5 100644
@@ -605,28 +605,30 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
                         * Do the same thing for the memory mapping - between
                         * elf_bss and last_bss is the bss section.
                         */
-                       k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
+                       k = load_addr + eppnt->p_vaddr + eppnt->p_memsz;
                        if (k > last_bss)
                                last_bss = k;
                }
        }
 
+       /*
+        * Now fill out the bss section: first pad the last page from
+        * the file up to the page boundary, and zero it from elf_bss
+        * up to the end of the page.
+        */
+       if (padzero(elf_bss)) {
+               error = -EFAULT;
+               goto out;
+       }
+       /*
+        * Next, align both the file and mem bss up to the page size,
+        * since this is where elf_bss was just zeroed up to, and where
+        * last_bss will end after the vm_brk() below.
+        */
+       elf_bss = ELF_PAGEALIGN(elf_bss);
+       last_bss = ELF_PAGEALIGN(last_bss);
+       /* Finally, if there is still more bss to allocate, do it. */
        if (last_bss > elf_bss) {
-               /*
-                * Now fill out the bss section.  First pad the last page up
-                * to the page boundary, and then perform a mmap to make sure
-                * that there are zero-mapped pages up to and including the
-                * last bss page.
-                */
-               if (padzero(elf_bss)) {
-                       error = -EFAULT;
-                       goto out;
-               }
-
-               /* What we have mapped so far */
-               elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
-
-               /* Map the last of the bss segment */
                error = vm_brk(elf_bss, last_bss - elf_bss);
                if (error)
                        goto out;
--

2.7.4

Signature · 8 lines
Kees Cook
Chrome OS & Brillo Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#3 lore raw

PATCH 2/2 mm: refuse wrapped vm_brk requests

In reply to Kees Cook

The vm_brk() alignment calculations should refuse to overflow. The ELF loader depending on this, but it has been fixed now. No other unsafe callers have been found.

---
 mm/mmap.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/mmap.c +5 -3
index de2c1769cc68..1874ee0e1266 100644
@@ -2625,16 +2625,18 @@ static inline void verify_mm_writelocked(struct mm_struct *mm)
  *  anonymous maps.  eventually we may be able to do some
  *  brk-specific accounting here.
  */
-static int do_brk(unsigned long addr, unsigned long len)
+static int do_brk(unsigned long addr, unsigned long request)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma, *prev;
-	unsigned long flags;
+	unsigned long flags, len;
 	struct rb_node **rb_link, *rb_parent;
 	pgoff_t pgoff = addr >> PAGE_SHIFT;
 	int error;
 
-	len = PAGE_ALIGN(len);
+	len = PAGE_ALIGN(request);
+	if (len < request)
+		return -ENOMEM;
 	if (!len)
 		return 0;
 
Signature · 7 lines
2.7.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

Re: [PATCH 2/2] mm: refuse wrapped vm_brk requests

I think both patches are fine, just a question.

Quoted 6 lines · Kees Cook
mm/mmap.c +1 -1
-static int do_brk(unsigned long addr, unsigned long len)
+static int do_brk(unsigned long addr, unsigned long request)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma, *prev;

mm/mmap.c static inline void verify_mm_writelocked(struct mm_struct *mm) · line 2639

Quoted 10 lines
mm/mmap.c +4 -2
-	unsigned long flags;
+	unsigned long flags, len;
 	struct rb_node **rb_link, *rb_parent;
 	pgoff_t pgoff = addr >> PAGE_SHIFT;
 	int error;
 
-	len = PAGE_ALIGN(len);
+	len = PAGE_ALIGN(request);
+	if (len < request)
+		return -ENOMEM;

So iiuc "len < request" is only possible if len == 0, right?

mm/mmap.c static inline void verify_mm_writelocked(struct mm_struct *mm) · line 2641

Quoted 2 lines
mm/mmap.c +0 -0
 	if (!len)
 		return 0;

and thus this patch fixes the error code returned by do_brk() in case of overflow, now it returns -ENOMEM rather than zero. Perhaps

	if (!len)
		return 0;
	len = PAGE_ALIGN(len);
	if (!len)
		return -ENOMEM;

would be more clear but this is subjective.

I am wondering if we should shift this overflow check to the caller(s). Say, sys_brk() does find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE) before do_brk(), and in case of overflow find_vma_intersection() can wrongly return NULL.

Then do_brk() will be called with len = -oldbrk, this can overflow or not but in any case this doesn't look right too.

Or I am totally confused?

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#5 lore raw

Re: [PATCH 2/2] mm: refuse wrapped vm_brk requests

Quoted 15 lines · Oleg Nesterov

I think both patches are fine, just a question.

Quoted 12 lines · Kees Cook
mm/mmap.c +2 -2
-static int do_brk(unsigned long addr, unsigned long len)
+static int do_brk(unsigned long addr, unsigned long request)
 {
      struct mm_struct *mm = current->mm;
      struct vm_area_struct *vma, *prev;
-     unsigned long flags;
+     unsigned long flags, len;
      struct rb_node **rb_link, *rb_parent;
      pgoff_t pgoff = addr >> PAGE_SHIFT;
      int error;
Quoted 6 lines

mm/mmap.c static inline void verify_mm_writelocked(struct mm_struct *mm) · line 2639

Quoted 4 lines
mm/mmap.c +3 -1
-     len = PAGE_ALIGN(len);
+     len = PAGE_ALIGN(request);
+     if (len < request)
+             return -ENOMEM;

So iiuc "len < request" is only possible if len == 0, right?

Oh, hrm, good point.

Quoted 7 lines
Quoted 3 lines
      if (!len)
              return 0;

and thus this patch fixes the error code returned by do_brk() in case of overflow, now it returns -ENOMEM rather than zero. Perhaps

Quoted 7 lines
        if (!len)
                return 0;
        len = PAGE_ALIGN(len);
        if (!len)
                return -ENOMEM;

would be more clear but this is subjective.

I'm fine either way.

Quoted 9 lines

I am wondering if we should shift this overflow check to the caller(s). Say, sys_brk() does find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE) before do_brk(), and in case of overflow find_vma_intersection() can wrongly return NULL.

Then do_brk() will be called with len = -oldbrk, this can overflow or not but in any case this doesn't look right too.

Or I am totally confused?

I think the callers shouldn't request a negative value, sure, but vm_brk should notice and refuse it.

-Kees

Signature · 8 lines
Kees Cook
Chrome OS & Brillo Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

Re: [PATCH 2/2] mm: refuse wrapped vm_brk requests

Quoted 12 lines · Kees Cook
Quoted 10 lines · Oleg Nesterov

and thus this patch fixes the error code returned by do_brk() in case of overflow, now it returns -ENOMEM rather than zero. Perhaps

        if (!len)
                return 0;
        len = PAGE_ALIGN(len);
        if (!len)
                return -ENOMEM;
Quoted 3 lines
Quoted 1 line

would be more clear but this is subjective.

I'm fine either way.

Me too, so feel free to ignore,

Quoted 5 lines
Quoted 5 lines

I am wondering if we should shift this overflow check to the caller(s). Say, sys_brk() does find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE) before do_brk(), and in case of overflow find_vma_intersection() can wrongly return NULL.

Quoted 7 lines
Quoted 4 lines

Then do_brk() will be called with len = -oldbrk, this can overflow or not but in any case this doesn't look right too.

Or I am totally confused?

I think the callers shouldn't request a negative value, sure, but vm_brk should notice and refuse it.

Not sure I understand...

I tried to say that, with or without this change, sys_brk() should check for overflow too, otherwise it looks buggy.

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#7 lore raw

Re: [PATCH 2/2] mm: refuse wrapped vm_brk requests

Quoted 29 lines · Oleg Nesterov
Quoted 12 lines · Kees Cook
Quoted 10 lines · Oleg Nesterov

and thus this patch fixes the error code returned by do_brk() in case of overflow, now it returns -ENOMEM rather than zero. Perhaps

        if (!len)
                return 0;
        len = PAGE_ALIGN(len);
        if (!len)
                return -ENOMEM;
Quoted 3 lines
Quoted 1 line

would be more clear but this is subjective.

I'm fine either way.

Me too, so feel free to ignore,

Quoted 10 lines
Quoted 9 lines

I am wondering if we should shift this overflow check to the caller(s). Say, sys_brk() does find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE) before do_brk(), and in case of overflow find_vma_intersection() can wrongly return NULL.

Then do_brk() will be called with len = -oldbrk, this can overflow or not but in any case this doesn't look right too.

Or I am totally confused?

Quoted 7 lines
Quoted 2 lines

I think the callers shouldn't request a negative value, sure, but vm_brk should notice and refuse it.

Not sure I understand...

I tried to say that, with or without this change, sys_brk() should check for overflow too, otherwise it looks buggy.

Hmm, it's not clear to me the right way to fix sys_brk(), but it looks like my change to do_brk() would catch the problem?

-Kees

Signature · 8 lines
Kees Cook
Chrome OS & Brillo Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw

Re: [PATCH 2/2] mm: refuse wrapped vm_brk requests

Quoted 8 lines · Kees Cook
Quoted 3 lines · Oleg Nesterov

I tried to say that, with or without this change, sys_brk() should check for overflow too, otherwise it looks buggy.

Hmm, it's not clear to me the right way to fix sys_brk(), but it looks like my change to do_brk() would catch the problem?

How?

Once again, afaics nothing bad can happen, sys_brk() will silently fail, just the code looks wrong anyway.

Suppose that newbrk == 0 due to overflow, then both

	if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
		goto out;

and

	if (do_brk(oldbrk, newbrk-oldbrk) < 0)
		goto out;

look buggy.

find_vma_intersection(start_addr, end_addr) expects that start_addr < end_addr. Again, we do not really care if it returns NULL or not, and newbrk == 0 just means it will certainly return NULL if there is something above oldbrk. Just looks buggy/confusing.

do_brk(0 - oldbrk) will fail and this is what we want. But not because your change will catch the problem, PAGE_ALIGNE(-oldbrk) won't necessarily overflow. However, -oldbrk > TASK_SIZE so get_unmapped_area() should fail.

Nevermind, this is almost off-topic, so let me repeat just in case that both patches look good to me.

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>