+2021-12-08 Mark Wielaard <mark@klomp.org>
+
+ * link_map.c (dwfl_link_map_report): Limit malloc size to max
+ possible. When converting make sure we don't exceed the number
+ of bytes available in either in.d_buf nor out.d_buf.
+
2021-12-08 Mark Wielaard <mark@klomp.org>
* dwfl_segment_report_module.c (dwfl_segment_report_module): Don't
/* Note this in the !in_ok path. That means memory_callback
failed. But the callback might still have reset the d_size
value (to zero). So explicitly set it here again. */
+ if (unlikely (phnum > SIZE_MAX / phent))
+ {
+ __libdwfl_seterrno (DWFL_E_NOMEM);
+ return false;
+ }
in.d_size = phnum * phent;
in.d_buf = malloc (in.d_size);
if (unlikely (in.d_buf == NULL))
return false;
}
size_t nbytes = phnum * phent;
+ /* We can only process as many bytes/phnum as there are
+ in in.d_size. The data might have been truncated. */
+ if (nbytes > in.d_size)
+ {
+ nbytes = in.d_size;
+ phnum = nbytes / phent;
+ }
void *buf = malloc (nbytes);
Elf32_Phdr (*p32)[phnum] = buf;
Elf64_Phdr (*p64)[phnum] = buf;
{
.d_type = ELF_T_PHDR,
.d_version = EV_CURRENT,
- .d_size = phnum * phent,
+ .d_size = nbytes,
.d_buf = buf
};
- in.d_size = out.d_size;
+ if (in.d_size > out.d_size)
+ in.d_size = out.d_size;
if (likely ((elfclass == ELFCLASS32
? elf32_xlatetom : elf64_xlatetom)
(&out, &in, elfdata) != NULL))