[PATCH] elfedit: validate PT_NOTE bounds and alignment
mustafa
mustafa@medios.digiscrypt.com
Wed Nov 18 05:37:51 GMT 2026
update_gnu_property maps the whole file read/write and then walks every
PT_NOTE segment, editing the GNU property notes in place. The
program header table is checked against the file size, but the PT_NOTE
segments themselves are not: p_offset and p_filesz are used as they
are to compute "map + p_offset", so a note segment that points outside
the file makes elfedit read and write past the end of the mapping. A
crafted binary crashes it with SIGSEGV.
Other fields in the same loop are also trusted:
- p_align of 0 gives a division by zero in "descsz % align" and makes
ELF_ALIGN_UP return 0, so the walk never advances. Handle it the
way readelf does and treat an alignment below 4 as 4. An alignment
that is not a power of two cannot be used with ELF_ALIGN_UP and is
rejected.
- The property bounds test "ptr + datasz > ptr_end" adds a 32-bit
value taken from the file to a pointer. Compare datasz against the
space that is left instead.
Also unmap the file on the early "can't read program headers" return,
which leaked the mapping.
* elfedit.c (update_gnu_property): Check that each PT_NOTE
segment lies inside the file. Normalise p_align and reject
values that are not a power of two. Compare datasz against the
remaining size instead of adding it to the pointer. Unmap the
file on the program header error path.
Signed-off-by: Mohammed Mustafa <mustafa@medios.digiscrypt.com>
---
binutils/elfedit.c | 25 ++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/binutils/elfedit.c b/binutils/elfedit.c
index d9d269d88fd..2d7f9df85b7 100644
--- a/binutils/elfedit.c
+++ b/binutils/elfedit.c
@@ -116,6 +116,7 @@ update_gnu_property (const char *file_name, FILE *file)
> st_buf.st_size - elf_header.e_phoff))
{
error (_("%s: can't read program headers\n"), file_name);
+ munmap (map, st_buf.st_size);
return 1;
}
@@ -161,8 +162,26 @@ update_gnu_property (const char *file_name, FILE *file)
size_t offset = phdrs[i].p_offset;
size_t size = phdrs[i].p_filesz;
size_t align = phdrs[i].p_align;
- char *buf = map + offset;
- char *p = buf;
+ char *buf;
+ char *p;
+
+ if (offset > (size_t) st_buf.st_size
+ || size > (size_t) st_buf.st_size - offset)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ if (align < 4)
+ align = 4;
+ else if ((align & (align - 1)) != 0)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ buf = map + offset;
+ p = buf;
while (p < buf + size)
{
@@ -218,7 +237,7 @@ update_gnu_property (const char *file_name, FILE *file)
unsigned int bitmask, old_bitmask;
ptr += 8;
- if ((ptr + datasz) > ptr_end)
+ if (datasz > (size_t) (ptr_end - ptr))
{
ret = 1;
goto out;
--
2.53.0
More information about the Binutils
mailing list