Skip to content

tools: validate PBP offsets and section names before use - #413

Merged
sharkwouter merged 2 commits into
pspdev:masterfrom
xmgzxmgz:tools/input-validation
Nov 16, 2026
Merged

sharkwouter merged 2 commits into
pspdev:masterfrom
xmgzxmgz:tools/input-validation

Conversation

@xmgzxmgz

Copy link
Copy Markdown
Contributor

Two input-validation gaps in tools/, both reachable from a file the developer
opens 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 checked

The eight offsets come straight from the header. if (!size) was the only length
gate (it rejects exactly zero), and fread/fwrite were compared with < 0 on a
size_t, which is never true, so a short read counted as success and the stale
16 MiB buffer kept being written out.

Measured with a 200-byte PBP:

input before after
valid PBP 8 files, exit 0 identical
offset[7] = -4096 writes a 16 MiB DATA.PSP of uninitialised buffer content, exit 0 ERROR: Section 7 offset (0xFFFFF000) is outside the file, no output
offset[2] < offset[1] negative-length section, exit 0 ERROR: Section 2 offset ... precedes section 1 offset, no output
offset[7] = 0x7FFFFFF0 keeps writing until it reaches ~2 GiB rejected

The fix rejects offsets that are negative, past the end of the file, or not
monotonic, and treats fread/fwrite returning anything but 1 as an error.

tools/psp-prxgen.c — strcpy of a file-controlled section name

szName is char[ELF_SECT_MAX_NAME] = 128 bytes; the source is the shstrtab
entry at sec->sh_name, which is neither length-checked nor guaranteed to be
NUL-terminated inside the table.

A 394-byte ELF whose section name is 200 bytes, built with ASan:

before: AddressSanitizer: SEGV on unknown address 0x424284848484
        #0 internal_strlen
        #1 load_sections psp-prxgen.c:303
        #2 load_elf psp-prxgen.c:550
after:  Error, no sceModuleInfo section found      (normal path, no crash)

iShstrndx was also accepted when equal to iShnum, which indexes one past the
end of the section array; that is rejected now as well.

Notes

  • Both changes stay inside the two tools; no library or ABI impact.
  • Built with -Wall -Wextra; the warnings that remain are pre-existing
    sign-compare ones.
  • Happy to split this into two PRs if you would rather review them separately.

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 sharkwouter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread tools/psp-prxgen.c

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change prxgen. This is only used during development directly after compiling the source file for it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@xmgzxmgz

Copy link
Copy Markdown
Contributor Author

Done — reverted tools/psp-prxgen.c to upstream in 82a7cab. This PR now only touches tools/unpack-pbp.c.

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. data.offset pointing back into the header, or a section name table whose entry has no NUL terminator before EOF. unpack_pbp() reads both with memcpy/strcpy sized from the header field, so a hand-crafted or truncated PBP lands outside the buffer instead of being rejected. It is not something a normal pack-pbp round-trip produces, which is probably why it never showed up.

Two questions, so I don't guess:

  1. Is a malformed-PBP input something you'd want rejected at all, or is unpack-pbp treated as a build-time tool that trusts its input? If the latter, I'll close this.
  2. If it is worth fixing, would you prefer a bounds check that errors out, or silent clamping to the real file size?

Happy to add a test case under whatever harness you use, or drop this if you'd rather keep the tool as-is.

@sharkwouter sharkwouter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing :)

@sharkwouter
sharkwouter merged commit f9f8790 into pspdev:master Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants