Skip to content

kernel: check kernel_read() results when verifying the APK signing block#3594

Open
palazik wants to merge 1 commit into
tiann:mainfrom
palazik:fix/apk-sign-read-checks
Open

kernel: check kernel_read() results when verifying the APK signing block#3594
palazik wants to merge 1 commit into
tiann:mainfrom
palazik:fix/apk-sign-read-checks

Conversation

@palazik

@palazik palazik commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

check_block() and check_v2_signature() in apk_sign.c read the APK v2 signing block field by field with kernel_read(), but none of those calls check how many bytes actually came back. On a truncated or otherwise malformed APK the code keeps doing offset arithmetic on whatever happened to be on the stack.

It's bounded by the CERT_MAX_LENGTH guard so this isn't a memory-safety hole, but relying on uninitialized/stale values on the manager-verification path isn't great.

This adds the length checks:

  • in check_block, every field read plus the certificate read now bail out (return false) on a short read;
  • in check_v2_signature, the reads after the EOCD scan goto clean, and the two reads inside the id-block loop invalidate the result first.

A properly signed manager APK reads every field back in full, so nothing changes for the normal case — only truncated/garbage input is now rejected explicitly instead of processed. I deliberately left the EOCD-search loop untouched since it already tolerates short reads via its n == i check.

check_block() and check_v2_signature() walk the APK signing block by
issuing a series of kernel_read() calls and never look at whether they
actually read what they asked for. On a truncated or malformed APK the
subsequent offset math runs on stale/garbage stack values.

A valid manager APK reads back every field in full, so behavior there is
unchanged; a short read now just bails out and treats the APK as not
matching. The EOCD scan loop is left as-is on purpose.
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.

1 participant