feat(rpm): add the bounded RPM header reader (S38 plan: 13/59)
Summary
The RPM upload has to read a package's headers before it accepts the package. Every byte comes from the uploader, so the reader must not let a crafted package take memory or CPU it did not pay for.
This MR adds internal/format/rpm/rpmheader. Read reads the 96-byte lead and the two headers. It learns each header's size from its 16-byte intro, refuses one past rpm.max_header_size or with more than 65,535 entries, as rpm does, and charges the size to the caller's budget before it allocates it. It checks each entry's offset, length, string end and count against the header's data, and that the entries after the first follow one another without overlap. It hands the header to a callback, then releases the charge. It stops at the main header's end and returns that byte range, which primary.xml lists.
Nothing calls it yet, so users see no change. The upload wires it in at Step 40.
Downside: it refuses a header whose entries after the first are out of data order or overlap. rpm refuses those too, so no package dnf can install is lost. It always leaves the first entry out of the order, where rpm does so only for a region tag, so it can accept a header rpm refuses, never the reverse.
It also accepts a header that repeats a tag, as rpm does, and Entry returns the first copy. Nothing reads a tag yet. Step 14 (#1963 (closed)) refuses such a header before its visitor reads a field, and S38 records that as an open question until then.
Checklist
- Ran it: no service path calls it yet. I built four packages with real
rpmbuild(rpm 4.14, 4.16 signed, 4.19 source package, 6.0) and the reader's byte range matchescreaterepo_c'sheader-rangefor each.testdata/build-fixtures.shrebuilds them. I fuzzedFuzzReadlocally for 60 s. - Interfaces and data: none. A new internal package, with no API, configuration, schema or stored format.
- Failure: a bad or cut-short package is
ErrInvalid, which the upload will answer with422 invalid_package. A full budget and transport errors pass through for the caller to map, including a transport error that wraps an end of file. The charge is released bydefer, so a panicking callback releases it too. - Rollout: n/a, nothing calls it.
- Hot path: not on a request path yet. Once wired: eight allocations per package. Each header gets one of exactly its declared size, after the budget charge; the other six are small and fixed. The checks, and the wipe when each visit ends, take time in proportion to the header's size.
- Database: n/a.
- Security: this is the untrusted-input parser of S38 Security. The size cap and the budget charge both come before the allocation, so a header that declares 1 GiB is refused with no buffer allocated. Each guard has a test that fails when the guard is removed.
FuzzReadruns in CI as a second line of thefuzz:rpmjob.Readwipes each header when its visit ends, so a visitor that keepsDatainstead of copying it reads zeros and its own tests fail. That makes the mistake visible, not impossible: the kept slice still holds the memory, and a visitor whose tests never read a kept value would pass. The visitors come with Steps 14, 15 and 40. No route or permission. - ADRs: ADR-004: the header cap is checked before any byte past the intro is read, and the reader never reads past the declared size. ADR-023: the package sits under
internal/format/rpm/and imports only the standard library, which Step 1's format-isolation rule allows. - E2E: none. There is no route yet; upload rows come with the upload steps.
- Simplicity: I kept the index as raw bytes and decode an entry on lookup, rather than building an entry list, which would hold memory past the charge. I rejected sorting entries by offset to allow any order: it costs memory, and
rpmalready requires the order. The budget is a function parameter, not an interface.
Plan and spec amendments
- S38 Security: adds that the entries after the first follow one another in the data without overlap, as
rpmrequires. Without it, a crafted 1 MiB header took 66 s to check, against 10 ms with it. It also addsrpm's limit of 65,535 entries. This needs a Staff+ approval. - Plan Step 13: Delivers names the same check, reworded to keep the plan within its 8,000-word cap.
- S38 Open questions: adds whether the upload refuses a header that repeats a tag, and says a
422 invalid_packagerefusal would close the gap. The reader returns the first copy for now. - Plan Step 14: Delivers adds the repeated-tag refusal, so that step answers the question. The plan stays at 8,000 words.
Merge order
- !3863 (merged) (S38 key routes) deletes Open question 5, right above the question this MR adds, so git reports a conflict in
docs/specs/S38-rpm-hosted.md. Either can merge first. The second keeps the deletion and the new question.
Closes #1962 (closed)