Repository navigation
tools: validate PBP offsets and section names before use - #413
Conversation
unpack-pbp: the eight section offsets are read from the file header and were never validated, so a 200-byte PBP makes the tool write out uninitialised buffer contents (16 MiB in the reproduction, up to ~2 GiB with a large last offset) and still exit 0. fread/fwrite were also tested with "< 0" on a size_t, which is never true, so a short read was treated as success. Reject offsets that are negative, past the end of the file or not monotonic, and treat anything but a full read/write as an error. psp-prxgen: the section name is copied with strcpy() from the section string table at a file-controlled offset, with no length limit and no guarantee of a terminator inside the table (szName is 128 bytes). Bound the copy to szName and to what the string table can still provide, and always terminate the result. iShstrndx was also accepted when equal to iShnum, indexing one past the section array; require iShstrndx < iShnum.
sharkwouter
left a comment
There was a problem hiding this comment.
Hey @xmgzxmgz, thanks for the PR. The changes to unpack-pbp look good and do not seem to break anything in my test, so I would merge them. I would still like you to undo the changes to psp-prxgen first, though, as that tool not validating the input as well as it could be is not a security concern. Thanks!
There was a problem hiding this comment.
Please don't change prxgen. This is only used during development directly after compiling the source file for it.
There was a problem hiding this comment.
I want to add that I feel it would be better to have separate PRs for the changes to different tools. I have not seen the edge case you found happen. Maybe it is worth fixing.
Per maintainer: prxgen is only used during development directly after compiling the source file, so the input validation there is unnecessary. Keeping this PR focused on unpack-pbp.
|
Done — reverted On the edge case you haven't seen: it needs a PBP whose section offsets are individually within the file but do not move forward — e.g. Two questions, so I don't guess:
Happy to add a test case under whatever harness you use, or drop this if you'd rather keep the tool as-is. |
sharkwouter
left a comment
There was a problem hiding this comment.
Thanks for contributing :)
Two input-validation gaps in
tools/, both reachable from a file the developeropens with the tool. Follow-up to #412 — thanks to @sharkwouter for the go-ahead
and to @rofl0r for the reminder that these tools run on the developer's machine,
so this is framed as input validation rather than as a security fix.
tools/unpack-pbp.c— section offsets are never checkedThe eight offsets come straight from the header.
if (!size)was the only lengthgate (it rejects exactly zero), and
fread/fwritewere compared with< 0on asize_t, which is never true, so a short read counted as success and the stale16 MiB buffer kept being written out.
Measured with a 200-byte PBP:
offset[7] = -4096DATA.PSPof uninitialised buffer content, exit 0ERROR: Section 7 offset (0xFFFFF000) is outside the file, no outputoffset[2] < offset[1]ERROR: Section 2 offset ... precedes section 1 offset, no outputoffset[7] = 0x7FFFFFF0The fix rejects offsets that are negative, past the end of the file, or not
monotonic, and treats
fread/fwritereturning anything but 1 as an error.tools/psp-prxgen.c—strcpyof a file-controlled section nameszNameischar[ELF_SECT_MAX_NAME]= 128 bytes; the source is the shstrtabentry at
sec->sh_name, which is neither length-checked nor guaranteed to beNUL-terminated inside the table.
A 394-byte ELF whose section name is 200 bytes, built with ASan:
iShstrndxwas also accepted when equal toiShnum, which indexes one past theend of the section array; that is rejected now as well.
Notes
-Wall -Wextra; the warnings that remain are pre-existingsign-compare ones.