binfmt_elf: Avoid total_mapping_size for ET_EXEC
Subsystems (4)
PATCH v2 1/1 binfmt_elf: Avoid total_mapping_size for ET_EXEC
Partially revert commit 5f501d555653 ("binfmt_elf: reintroduce using MAP_FIXED_NOREPLACE").
At least ia64 has ET_EXEC PT_LOAD segments that are not virtual-address contiguous (but _are_ file-offset contiguous). This would result in giant mapping attempts to cover the entire span, including the virtual address range hole. Disable total_mapping_size for ET_EXEC, which reduces the MAP_FIXED_NOREPLACE coverage to only the first PT_LOAD:
$ readelf -lW /usr/bin/gcc
...
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz ...
...
LOAD 0x000000 0x4000000000000000 0x4000000000000000 0x00b5a0 0x00b5a0 ...
LOAD 0x00b5a0 0x600000000000b5a0 0x600000000000b5a0 0x0005ac 0x000710 ...
...
^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^File offset range : 0x000000-0x00bb4c
0x00bb4c bytes
Virtual address range : 0x4000000000000000-0x600000000000bcb0
0x200000000000bcb0 bytes
Ironically, this is the reverse of the problem that originally caused problems with ET_EXEC and MAP_FIXED_NOREPLACE: overlaps. This problem is with holes. Future work could restore full coverage if load_elf_binary() were to perform mappings in a separate phase from the loading (where it could resolve both overlaps and holes).
---
Here's the v5.16 backport.
--- fs/binfmt_elf.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
fs/binfmt_elf.c +18 -7
@@ -1135,14 +1135,25 @@ static int load_elf_binary(struct linux_binprm *bprm)* is then page aligned. */ load_bias =ELF_PAGESTART (load_bias - vaddr); - } -/* -* Calculate the entire size of the ELF mapping (total_size). -* (Note that load_addr_set is set to true later once the -* initial mapping is performed.) -*/ -if (!load_addr_set) { +/* +* Calculate the entire size of the ELF mapping +* (total_size), used for the initial mapping, +* due to first_pt_load which is set to false later +* once the initial mapping is performed. +* +* Note that this is only sensible when the LOAD +* segments are contiguous (or overlapping). If +* used for LOADs that are far apart, this would +* cause the holes between LOADs to be mapped, +* running the risk of having the mapping fail, +* as it would be larger than the ELF file itself. +* +* As a result, only ET_DYN does this, since +* some ET_EXEC (e.g. ia64) may have virtual +* memory holes between LOADs. +* +*/ total_size =total_mapping_size (elf_phdata, elf_ex->e_phnum);if (!total_size) {
Signature · 1 line
2.32.0
Quoted 91 lines · Kees Cook
Partially revert commit
5f501d555653("binfmt_elf: reintroduce usingMAP_FIXED_NOREPLACE").At least ia64 has
ET_EXECPT_LOADsegments that are not virtual-address contiguous (but _are_ file-offset contiguous). This would result in giant mapping attempts to cover the entire span, including the virtual address range hole. Disabletotal_mapping_sizeforET_EXEC, which reduces theMAP_FIXED_NOREPLACEcoverage to only the firstPT_LOAD:$ readelf -lW /usr/bin/gcc ... Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz ... ... LOAD 0x000000 0x4000000000000000 0x4000000000000000 0x00b5a0 0x00b5a0 ... LOAD 0x00b5a0 0x600000000000b5a0 0x600000000000b5a0 0x0005ac 0x000710 ... ... ^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^File offset range : 0x000000-0x00bb4c
0x00bb4c bytesVirtual address range : 0x4000000000000000-0x600000000000bcb0
0x200000000000bcb0 bytesIronically, this is the reverse of the problem that originally caused problems with
ET_EXECandMAP_FIXED_NOREPLACE: overlaps. This problem is
with holes. Future work could restore full coverage ifload_elf_binary()
were to perform mappings in a separate phase from the loading (where it could resolve both overlaps and holes).Cc Alexander Viro <[email protected]>Cc Eric Biederman <[email protected]>Reported-by matoro <[email protected]>Reported-by John Paul Adrian Glaubitz <[email protected]>Fixes 5f501d555653 ("binfmt_elf: reintroduce using
MAP_FIXED_NOREPLACE")
Link:
https://lore.kernel.org/r/[email protected]Signed-off-by Kees Cook <[email protected]>---
Here's the v5.16 backport.--- fs/binfmt_elf.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)fs/binfmt_elf.c +18
-7@@ -1135,14 +1135,25 @@ static int load_elf_binary(struct linux_binprm*bprm) * is then page aligned. */ load_bias =ELF_PAGESTART (load_bias - vaddr); - } -/* -* Calculate the entire size of the ELF mapping (total_size). -* (Note that load_addr_set is set to true later once the -* initial mapping is performed.) -*/ -if (!load_addr_set) { +/* +* Calculate the entire size of the ELF mapping +* (total_size), used for the initial mapping, +* due to first_pt_load which is set to false later +* once the initial mapping is performed. +* +* Note that this is only sensible when the LOAD +* segments are contiguous (or overlapping). If +* used for LOADs that are far apart, this would +* cause the holes between LOADs to be mapped, +* running the risk of having the mapping fail, +* as it would be larger than the ELF file itself. +* +* As a result, only ET_DYN does this, since +* some ET_EXEC (e.g. ia64) may have virtual +* memory holes between LOADs. +* +*/ total_size =total_mapping_size (elf_phdata, elf_ex->e_phnum);
Quoted 1 line
if (!total_size) {
This does the trick! Thank you so much!!
Quoted 85 lines · matoro
Quoted 84 lines · Kees Cook
Partially revert commit
5f501d555653("binfmt_elf: reintroduce usingMAP_FIXED_NOREPLACE").At least ia64 has
ET_EXECPT_LOADsegments that are not virtual-address contiguous (but _are_ file-offset contiguous). This would result in giant mapping attempts to cover the entire span, including the virtual address range hole. Disabletotal_mapping_sizeforET_EXEC, which reduces theMAP_FIXED_NOREPLACEcoverage to only the firstPT_LOAD:$ readelf -lW /usr/bin/gcc ... Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz ... ... LOAD 0x000000 0x4000000000000000 0x4000000000000000 0x00b5a0 0x00b5a0 ... LOAD 0x00b5a0 0x600000000000b5a0 0x600000000000b5a0 0x0005ac 0x000710 ... ... ^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^File offset range : 0x000000-0x00bb4c
0x00bb4c bytesVirtual address range : 0x4000000000000000-0x600000000000bcb0
0x200000000000bcb0 bytesIronically, this is the reverse of the problem that originally caused problems with
ET_EXECandMAP_FIXED_NOREPLACE: overlaps. This problem is with holes. Future work could restore full coverage ifload_elf_binary()were to perform mappings in a separate phase from the loading (where it could resolve both overlaps and holes).Cc Alexander Viro <[email protected]>Cc Eric Biederman <[email protected]>Reported-by matoro <[email protected]>Reported-by John Paul Adrian Glaubitz <[email protected]>Fixes 5f501d555653 ("binfmt_elf: reintroduce using
MAP_FIXED_NOREPLACE")
Link:
https://lore.kernel.org/r/[email protected]Signed-off-by Kees Cook <[email protected]>---
Here's the v5.16 backport.--- fs/binfmt_elf.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)fs/binfmt_elf.c +15
-7@@ -1135,14 +1135,25 @@ static int load_elf_binary(struct linux_binprm*bprm) * is then page aligned. */ load_bias =ELF_PAGESTART (load_bias - vaddr); - } -/* -* Calculate the entire size of the ELF mapping (total_size). -* (Note that load_addr_set is set to true later once the -* initial mapping is performed.) -*/ -if (!load_addr_set) { +/* +* Calculate the entire size of the ELF mapping +* (total_size), used for the initial mapping, +* due to first_pt_load which is set to false later +* once the initial mapping is performed. +* +* Note that this is only sensible when the LOAD +* segments are contiguous (or overlapping). If +* used for LOADs that are far apart, this would +* cause the holes between LOADs to be mapped, +* running the risk of having the mapping fail, +* as it would be larger than the ELF file itself. +* +* As a result, only ET_DYN does this, since +* some ET_EXEC (e.g. ia64) may have virtual
Quoted 8 lines
Quoted 6 lines
fs/binfmt_elf.c +3
-0+* memory holes between LOADs. +* +*/ total_size =total_mapping_size (elf_phdata, elf_ex->e_phnum);if (!total_size) {
This does the trick! Thank you so much!!
Excellent; thank you for testing! I'll send this to Linus shortly.
Signature · 1 line
Kees Cook
Hello!
Quoted 74 lines · Kees Cook
Partially revert commit
5f501d555653("binfmt_elf: reintroduce usingMAP_FIXED_NOREPLACE").At least ia64 has
ET_EXECPT_LOADsegments that are not virtual-address contiguous (but _are_ file-offset contiguous). This would result in giant mapping attempts to cover the entire span, including the virtual address range hole. Disabletotal_mapping_sizeforET_EXEC, which reduces theMAP_FIXED_NOREPLACEcoverage to only the firstPT_LOAD:$ readelf -lW /usr/bin/gcc ... Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz ... ... LOAD 0x000000 0x4000000000000000 0x4000000000000000 0x00b5a0 0x00b5a0 ... LOAD 0x00b5a0 0x600000000000b5a0 0x600000000000b5a0 0x0005ac 0x000710 ... ... ^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^File offset range : 0x000000-0x00bb4c
0x00bb4c bytesVirtual address range : 0x4000000000000000-0x600000000000bcb0
0x200000000000bcb0 bytesIronically, this is the reverse of the problem that originally caused problems with
ET_EXECandMAP_FIXED_NOREPLACE: overlaps. This problem is with holes. Future work could restore full coverage ifload_elf_binary()were to perform mappings in a separate phase from the loading (where it could resolve both overlaps and holes).Cc Alexander Viro <[email protected]>Cc Eric Biederman <[email protected]>Reported-by matoro <[email protected]>Reported-by John Paul Adrian Glaubitz <[email protected]>Fixes 5f501d555653 ("binfmt_elf: reintroduce using MAP_FIXED_NOREPLACE")Signed-off-by Kees Cook <[email protected]>---
Here's the v5.16 backport.--- fs/binfmt_elf.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)fs/binfmt_elf.c +11
-7@@ -1135,14 +1135,25 @@ static int load_elf_binary(struct linux_binprm *bprm)* is then page aligned. */ load_bias =ELF_PAGESTART (load_bias - vaddr); - } -/* -* Calculate the entire size of the ELF mapping (total_size). -* (Note that load_addr_set is set to true later once the -* initial mapping is performed.) -*/ -if (!load_addr_set) { +/* +* Calculate the entire size of the ELF mapping +* (total_size), used for the initial mapping, +* due to first_pt_load which is set to false later +* once the initial mapping is performed. +* +* Note that this is only sensible when the LOAD +* segments are contiguous (or overlapping). If +* used for LOADs that are far apart, this would +* cause the holes between LOADs to be mapped, +* running the risk of having the mapping fail,
fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm) · line 1159
Quoted 10 lines
fs/binfmt_elf.c +7
-0+* as it would be larger than the ELF file itself. +* +* As a result, only ET_DYN does this, since +* some ET_EXEC (e.g. ia64) may have virtual +* memory holes between LOADs. +* +*/ total_size =total_mapping_size (elf_phdata, elf_ex->e_phnum);if (!total_size) {
I can confirm that this patch fixes the issue for me.
Thanks,
Adrian
Signature · 4 lines
.''`. John Paul Adrian Glaubitz : :' : Debian Developer - [email protected] `. `' Freie Universitaet Berlin - [email protected] `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913