Skip to content

apk: guard against short lines in installed database#1949

Open
arpitjain099 wants to merge 1 commit into
quay:mainfrom
arpitjain099:fix/apk-short-line-panic
Open

apk: guard against short lines in installed database#1949
arpitjain099 wants to merge 1 commit into
quay:mainfrom
arpitjain099:fix/apk-short-line-panic

Conversation

@arpitjain099

Copy link
Copy Markdown

I do supply-chain security research, mostly on scanners and indexers.

The apk record loop in apk/scanner.go slices every line as line[2:]. A bare newline is a 1 byte line, so that's slice bounds out of range [2:1] and the scan panics.

You get one from a stray blank line inside a record. Splitting on "\n\n" leaves the extra newline sitting at the front of the next record, and its first ReadBytes hands back just "\n". Layer contents come from whatever image is being indexed, so a malformed or crafted lib/apk/db/installed is enough to crash the indexer.

Fix is a length check before the slice. dpkg doesn't have this problem because textproto tolerates the junk for it, which the comment above the loop already explains apk can't use.

Test is TestBlankLine, a tarred-up installed database with an extra blank line, run through Scan the normal way. Panics on main, passes with the fix, and no network needed. go test ./apk/ and go test -race ./apk/ are clean.

The record parser slices every line as line[2:] to get the value, which
assumes each line is at least a one byte key plus a ":". A line that's
just a newline is 1 byte, so it panics with "slice bounds out of range
[2:1]" and takes down the scan.

An installed database with a stray blank line inside a record produces
exactly that: the "\n\n" split leaves the extra newline at the head of
the next record. Layer contents are attacker controlled, so this is
reachable from any indexed image.

Skip lines too short to hold a key and a value. The dpkg scanner gets
this for free from net/textproto, which apk can't use because its keys
are case sensitive.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@arpitjain099
arpitjain099 requested review from a team as code owners July 15, 2026 11:36
@arpitjain099
arpitjain099 requested a review from hdonnay July 15, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant