Skip to contentSkip to conversation

Bug fix to ELF Loader which rejects valid ELFs

PATCH Superseded Burrow, Ryan - 0553 - MITLL 2 participants 3 messages Aug 11, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bug fix to ELF Loader which rejects valid ELFs

Check was incorrectly being applied to size of elf phdrs, instead of the number. The ELF standard allows for up to 65535 headers, but the check was being compared to the number of headers multiplied by the size of a program header.

---
 fs/binfmt_elf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/binfmt_elf.c +0 -0
index 9fe3b51c116a..5605b5205f84 100644
@@ -445,11 +445,11 @@ static struct elf_phdr *load_elf_phdrs(const struct

elfhdr *elf_ex,
goto out;

/* Sanity check the number of program headers... */
- /* ...and their total size. */
- size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
- if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN)
+ if (elf_ex->e_phnum == 0 || elf_ex->phnum > 65535)
goto out;

+ size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
+
elf_phdata = kmalloc(size, GFP_KERNEL);
if (!elf_phdata)
goto out;

Signature · 1 line
2.17.1
#1 lore raw

Review

Quoted 5 lines · On Tue, Aug 11, 2020 at 02:44:08PM +0000, Burrow, Ryan - 0553 - MITLL wrote:
(quoted diff) +1 -3
 	/* Sanity check the number of program headers... */
-	/* ...and their total size. */
-	size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
-	if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN)
+	if (elf_ex->e_phnum == 0 || elf_ex->phnum > 65535)

umm, did you compile-test this?

assuming you meant e_phnum, it's a 16-bit quantity, so it can't be bigger than 65535.

Review

Quoted 3 lines
(quoted diff) +1 -0
 		goto out;
 
+	size = sizeof(struct elf_phdr) * elf_ex->e_phnum;

use array_size() here?

#2 lore raw
Quoted 8 lines

-----Original Message-----
From: Matthew Wilcox <[email protected]>
Sent: Tuesday, August 11, 2020 11:05 AM
To: Burrow, Ryan - 0553 - MITLL <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] Bug fix to ELF Loader which rejects valid ELFs

On Tue, Aug 11, 2020 at 02:44:08PM +0000, Burrow, Ryan - 0553 - MITLL

Quoted 8 lines · wrote:

Review

Quoted 5 lines
(quoted diff) +1 -3
 	/* Sanity check the number of program headers... */
-	/* ...and their total size. */
-	size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
-	if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN)
+	if (elf_ex->e_phnum == 0 || elf_ex->phnum > 65535)

umm, did you compile-test this?

My apologies, I had made these edits in the context of other changes which I didn't want to include in this patch - I replicated these changes individually and (mistakenly) assumed I had done so correctly.

Quoted 9 lines

assuming you meant e_phnum, it's a 16-bit quantity, so it can't be bigger than 65535.

Review

Quoted 3 lines
(quoted diff) +1 -0
 		goto out;
 
+	size = sizeof(struct elf_phdr) * elf_ex->e_phnum;

use array_size() here?

That's a good catch - I had really just moved the previous allocation of size down to after the check, but I'll use array_size instead. Just to confirm the correct process, should I do an inline response with the updated commit, or submit a new patch email?