ELF: fix overflow in total mapping size calculation
authorAlexey Dobriyan <adobriyan@gmail.com>
Sun, 3 Oct 2021 12:11:24 +0000 (15:11 +0300)
committerKees Cook <keescook@chromium.org>
Tue, 1 Mar 2022 00:53:43 +0000 (16:53 -0800)
commitd49fd967f4cc5a27f63cd5f7d1c100c6359f9b4c
tree3d76f07f7bca75e2caab32abdbec99414b5c6765
parent397ea571211d3a62063ac40952d9f78d53d15ddf
ELF: fix overflow in total mapping size calculation

Kernel assumes that ELF program headers are ordered by mapping address,
but doesn't enforce it. It is possible to make mapping size extremely huge
by simply shuffling first and last PT_LOAD segments.

As long as PT_LOAD segments do not overlap, it is silly to require
sorting by v_addr anyway because mmap() doesn't care.

Don't assume PT_LOAD segments are sorted and calculate min and max
addresses correctly.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Tested-by: "Magnus Groß" <magnus.gross@rwth-aachen.de>
Link: https://lore.kernel.org/all/Yfqm7HbucDjPbES+@fractal.localdomain/
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/lkml/YVmd7D0M6G%2FDcP4O@localhost.localdomain
fs/binfmt_elf.c