[PATCH v2] elfedit: validate PT_NOTE bounds and alignment
mustafa
mustafa@medios.digiscrypt.com
Mon Nov 30 13:15:32 GMT 2026
update_gnu_property maps the whole file read/write and walks every
PT_NOTE segment, editing GNU property notes in place. The program
header table is checked against the file size, but the note segments
are not: p_offset and p_filesz are used directly to compute
"map + p_offset", so a note segment pointing 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 loop are also trusted. A zero p_align divides by
zero in "descsz % align" and makes ELF_ALIGN_UP return 0, so the walk
never advances. Treat p_align == 0 as 1, and reject other values that
are not powers of two, which ELF_ALIGN_UP cannot handle. Unlike
readelf, elfedit changes bytes in the file, so an alignment that does
not match the file must be rejected instead of normalised to 4. The
property bounds test adds a 32-bit value from the file to a pointer;
compare datasz with the space that is left instead. Also unmap the
file on the early "can't read program headers" return, which leaked
the mapping.
binutils/
* elfedit.c (update_gnu_property): Check PT_NOTE bounds.
Treat a zero p_align as 1 and reject other non-power-of-two
values. Bound datasz by the remaining descriptor size.
Unmap on the program header error path.
Signed-off-by: Mohammed Mustafa <mustafa@medios.digiscrypt.com>
---
Changes in v2:
- "Put these together in one decl?": use "char *buf, *p;".
- Drop the bumping to 4: "just 0 wants treating as if it was 1", and
reject other alignments that are not powers of two.
- Add the missing "binutils/" line ahead of the ChangeLog entry and
make the entry ChangeLog-like instead of repeating the description.
binutils/elfedit.c | 24 +++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/binutils/elfedit.c b/binutils/elfedit.c
index d9d269d88fd..ae5ba548a23 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,25 @@ 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, *p;
+
+ if (offset > (size_t) st_buf.st_size
+ || size > (size_t) st_buf.st_size - offset)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ if (align == 0)
+ align = 1;
+ else if ((align & (align - 1)) != 0)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ buf = map + offset;
+ p = buf;
while (p < buf + size)
{
@@ -218,7 +236,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.56.1
More information about the Binutils
mailing list