Skip to content

Strict Apk 2 hash compliance - #2153

Open
dgtlrift wants to merge 2 commits into
jordansissel:mainfrom
dgtlrift:apk-3
Open

Strict Apk 2 hash compliance#2153
dgtlrift wants to merge 2 commits into
jordansissel:mainfrom
dgtlrift:apk-3

Conversation

@dgtlrift

Copy link
Copy Markdown

Stale tar header checksums (cut_tar_record, and the main header in hash_datatar_sha1): ownership fields (uid/gid/uname/gname/magic) were rewritten but the tar header checksum was never recalculated afterward, corrupting the checksum apk validates on every header.
SHA1 per-file hash included tar padding: hash_record_sha1 was hashing the full 512-byte-aligned record buffer instead of just the real file content, so every APK-TOOLS.checksum.SHA1 pax extension was wrong.
Datahash computed on the wrong bytes entirely: the .PKGINFO datahash field is checked by apk against the SHA-256 of the compressed data.tar.gz bytes, not the uncompressed data.tar. fpm was hashing the raw tar before compression. I confirmed this by patching apk-tools' extract_v2.c to print the digest it calculates - it matched sha256(data.tar.gz) exactly, not sha256(data.tar). Fixed by restructuring output to gzip the data tar first, hash those compressed bytes, then build .PKGINFO/control tar/compress it, then concatenate the two already-compressed streams.
Found and fixed a related latent bug in hash_datatar_sha1: ownership-header rewriting was being applied even to the null (end-of-archive) terminator blocks, polluting bytes that must stay all-zero.

mcandre and others added 2 commits February 5, 2026 12:45
…sh_datatar_sha1): ownership fields (uid/gid/uname/gname/magic) were rewritten but the tar header checksum was never recalculated afterward, corrupting the checksum apk validates on every header.

SHA1 per-file hash included tar padding: hash_record_sha1 was hashing the full 512-byte-aligned record buffer instead of just the real file content, so every APK-TOOLS.checksum.SHA1 pax extension was wrong.
Datahash computed on the wrong bytes entirely: the .PKGINFO datahash field is checked by apk against the SHA-256 of the compressed data.tar.gz bytes, not the uncompressed data.tar. fpm was hashing the raw tar before compression. I confirmed this by patching apk-tools' extract_v2.c to print the digest it calculates - it matched sha256(data.tar.gz) exactly, not sha256(data.tar). Fixed by restructuring output to gzip the data tar first, hash those compressed bytes, then build .PKGINFO/control tar/compress it, then concatenate the two already-compressed streams.
Found and fixed a related latent bug in hash_datatar_sha1: ownership-header rewriting was being applied even to the null (end-of-archive) terminator blocks, polluting bytes that must stay all-zero.
@dgtlrift

Copy link
Copy Markdown
Author

Created new PR per @jordansissel recommendation

@dgtlrift dgtlrift changed the title Strict Apk 2 hash compiance Strict Apk 2 hash compliance Jul 17, 2026
@dgtlrift

dgtlrift commented Aug 5, 2026

Copy link
Copy Markdown
Author

Can someone review and commit?

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