Skip to contentSkip to conversation

elf: fix NT_FILE integer overflow

PATCH Applied Alexey Dobriyan 2 participants 3 messages Jan 12, 2018 – Jan 13, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 elf: fix NT_FILE integer overflow

If vm.max_map_count bumped above 2^26 (67+ mil) and system has enough RAM to allocate all the VMAs (~12.8 GB on Fedora 27 with 200-byte VMAs), then it should be possible to overflow 32-bit "size", pass paranoia check, allocate very little vmalloc space and oops while writing into vmalloc guard page...

But I didn't test this, only coredump of regular process.

---

 fs/binfmt_elf.c |    2 ++
 1 file changed, 2 insertions(+)
fs/binfmt_elf.c +2 -0
@@ -1599,6 +1599,8 @@ static int fill_files_note(struct memelfnote *note)
 
 	/* *Estimated* file count and total data size needed */
 	count = current->mm->map_count;
+	if (count > UINT_MAX / 64)
+		return -EINVAL;
 	size = count * 64;
 
 	names_ofs = (2 + 3 * count) * sizeof(data[0]);
#1 lore raw
Quoted 13 lines · On Fri, 12 Jan 2018 23:34:27 +0300 Alexey Dobriyan <[email protected]> wrote:

If vm.max_map_count bumped above 2^26 (67+ mil) and system has enough RAM to allocate all the VMAs (~12.8 GB on Fedora 27 with 200-byte VMAs), then it should be possible to overflow 32-bit "size", pass paranoia check, allocate very little vmalloc space and oops while writing into vmalloc guard page...

But I didn't test this, only coredump of regular process.

...

fs/binfmt_elf.c static int fill_files_note(struct memelfnote *note) · line 1606

Quoted 8 lines
fs/binfmt_elf.c +2 -0
@@ -1599,6 +1599,8 @@ static int fill_files_note(struct memelfnote *note)
 
 	/* *Estimated* file count and total data size needed */
 	count = current->mm->map_count;
+	if (count > UINT_MAX / 64)
+		return -EINVAL;
 	size = count * 64;
 
 	names_ofs = (2 + 3 * count) * sizeof(data[0]);

Why not make `size' a ulong (or size_t)? That seems to be the appropriate type and the code will then immediately barf over the MAX_FILE_NOTE_SIZE comparison anyway.

#2 lore raw
Quoted 22 lines · Andrew Morton
Quoted 20 lines · On Fri, 12 Jan 2018 23:34:27 +0300 Alexey Dobriyan <[email protected]> wrote:

If vm.max_map_count bumped above 2^26 (67+ mil) and system has enough RAM to allocate all the VMAs (~12.8 GB on Fedora 27 with 200-byte VMAs), then it should be possible to overflow 32-bit "size", pass paranoia check, allocate very little vmalloc space and oops while writing into vmalloc guard page...

But I didn't test this, only coredump of regular process.

...

fs/binfmt_elf.c +2 -0
@@ -1599,6 +1599,8 @@ static int fill_files_note(struct memelfnote *note)
 
 	/* *Estimated* file count and total data size needed */
 	count = current->mm->map_count;
+	if (count > UINT_MAX / 64)
+		return -EINVAL;
 	size = count * 64;
 
Quoted 5 lines

fs/binfmt_elf.c static int fill_files_note(struct memelfnote *note) · line 1606

Quoted 1 line
fs/binfmt_elf.c +0 -0
 	names_ofs = (2 + 3 * count) * sizeof(data[0]);

Why not make `size' a ulong (or size_t)? That seems to be the appropriate type and the code will then immediately barf over the MAX_FILE_NOTE_SIZE comparison anyway.

You can do it, but MAX_FILE_NOTE_SIZE is only 4 MB, and 32-bit code is smaller than 64-bit code.