Skip to content

Boot hardening: exact boot-hook lines, Dalvik layout check, update md5 check, zygote fallback - #436

Open
AwsomeFox wants to merge 6 commits into
fix/sync-stock-copyfrom
fix/boot-hardening
Open

AwsomeFox wants to merge 6 commits into
fix/sync-stock-copyfrom
fix/boot-hardening

Conversation

@AwsomeFox

Copy link
Copy Markdown
Contributor

What this changes

Boot hardening from the 2026-11-26 review. Stacked on #426 (fix/sync-stock-copy, the sync after the stock copy is made), so this PR's diff is only the commits after it; it moves to main once #426 lands.

  • Boot hook keeps the boot scripts' lines as they are (install/common/lib.sh). hook_strip and hook_add wrote each line of install-recovery.sh and qb_runtime.sh again with echo, which expands backslashes in mksh as in dash: \\ became \, and \c ended the output, so V="x\cY"; echo ok lost its closing quote and joined the next line. Lines now go through hook_line: mksh's print -r on the unit, or in a shell without print (dash in the off-car tests) echo with backslashes doubled and a leading - written as \0055. Whichever is used is checked on a test line when lib.sh loads. If neither is exact, hook_add fails. hook_add also checks the new file before it goes in: without the block it must be the file as it was (by md5), or the file is left alone. No printf (tools/check-scripts.sh: not on the unit).
  • Dalvik layout check before the first hook (core/loader/loader.c). The offsets are the EV6 libdvm's. Before Bridge.init they're read back from Bridge's own handleHookedMethod and invokeOriginalMethodNative: class (0x00), name (0x10), shorty (0x1c), flags, insSize/registersSize, insns, and the nativeFunc (0x28) that dvmSetNativeFunc just wrote. It also checks the primitive types of int, void and Bridge (0x44) and an Object[3]'s length (0x08). On a mismatch it logs which field and doesn't run Bridge.init: no hooks. If the name offset is wrong, reading it could still crash the zygote, which the boot guard catches; apps are no longer at risk.
  • An update checks the stock copy by md5 (install/common/install.sh). An update only ran not_stock on app_process.hmgapi-stock, so a copy cut short, zero-filled, or starting with ELF but changed passed. It's now checked against stock.txt. An unknown copy comes back from /data/hmgapi/stock/system/bin/app_process when that is a known build (the RESCUE path). With neither known, the update is refused with nothing changed, as a new install is. HMGAPI_FORCE=1 stays maintainers-only. The catalogue's engine update hmgapi (Catalogue, dependencies and updates in the engine #423) runs this installer.
  • Zygote wrapper falls back to the stock backup (core/zygote/zygote.c). When exec of the stock copy fails, it runs /data/hmgapi/stock/system/bin/app_process if it starts like an ELF binary, after chmod 700 (it's 600 under the install's umask 077). This brings back a unit whose /system stock copy was lost. SELinux: the EV6 boots with androidboot.selinux=disabled (permissive), and /data/local/tmp/nemesis-d already runs from /data. On an Enforcing unit the exec is probably refused and the wrapper exits 127 as before.
  • CHANGELOG.md and CLAUDE.md updated.

The related fix on catalog/app (the HTTPS library kept before /system changes) is pushed to #424 as its own commit.

Tested on

Off-car, on macOS:

  • install/common/test.sh: 48 checks pass in dash and macOS sh. Against the old lib.sh the new checks fail (10), with sh -n reporting the unclosed quote. The backslash checks also run in mksh when it's installed (CI); there was no mksh here.
  • install/common/install-test.sh: 295 checks pass, with a stand-in payload (placeholder binaries, this tree's scripts and maps). Against the old install.sh, 17 of the new update checks fail.
  • install/common/orders-test.sh: 8 pass.
  • tools/test-all.sh --fast: 30 ok, 1 skipped (mkicon: no ImageMagick).
  • loader.c and zygote.c compile for the ARM target. core/zygote/test.sh (11 new fallback checks) needs qemu-arm and runs only in CI.

Tested on cars

Off-car only. Still to try on a car:

  • the hook rewrite under the unit's mksh (a backslash-heavy qb_runtime.sh)
  • the loader with HMGAPI_SELFTEST
  • the zygote fallback (stock copy moved away)
  • an update over a damaged stock copy

Checklist

  • New or changed hook points: none
  • Mod scripts / installer: tools/check-scripts.sh passes
  • Catalogue entry: none
  • Anything written to /system keeps a stock check, a backup and an exact restore
  • No manufacturer apps or libraries, no per-car data
  • Vehicle commands: none
  • User-facing change: CHANGELOG.md updated; wiki/ not updated

🤖 Generated with Claude Code

AwsomeFox and others added 5 commits November 27, 2026 10:06
hook_strip and hook_add wrote each line of install-recovery.sh and
qb_runtime.sh again with echo, which expands backslashes in mksh as in
dash: \\ became \, and a \c ended the output, so V="x\cY"; echo ok lost
its closing quote and joined the next line. Lines now go through
hook_line: mksh's print -r on the unit, or (a shell without print, as
dash in the off-car tests) echo with backslashes doubled and a leading -
escaped, checked on a test line when lib.sh loads. hook_add checks the
new file before it goes in: without the block it must be the file as it
was, or the file is left alone. Tests with backslashes, \c, -n lines and
no last newline, in the test shell and in mksh when it's installed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Method, ClassObject and ArrayObject offsets are the EV6 libdvm's,
with no check that a unit's libdvm matches. Before Bridge.init they're
read back from Bridge's own handleHookedMethod and
invokeOriginalMethodNative (class, name, shorty, flags, argument count,
the nativeFunc dvmSetNativeFunc just wrote), the primitive types of int,
void and Bridge, and an Object[3]'s length. On a mismatch the loader logs
which one and doesn't run Bridge.init, so nothing is hooked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An update only checked that app_process.hmgapi-stock started like a
binary, so a copy cut short, zero-filled or otherwise damaged passed.
It's now checked against stock.txt; an unknown copy comes back from the
stock backup when that is a known build (the RESCUE path), and with
neither known the update is refused with nothing changed, as a new
install is. HMGAPI_FORCE=1 stays for maintainers. Tests: a short copy, a
zero-filled one, one that starts with ELF but has the wrong md5, each
with and without a stock backup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When exec of app_process.hmgapi-stock fails, the wrapper runs the
install's backup, /data/hmgapi/stock/system/bin/app_process, if it
starts like an ELF binary, after making it 700 (it's 600 under the
install's umask). On a unit where SELinux refuses an exec from /data it
fails too and the wrapper exits 127 as before. The EV6 boots with
SELinux disabled (permissive).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, zygote fallback

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AwsomeFox
AwsomeFox requested review from a team and VysseCodes as code owners November 27, 2026 15:27
Each log line goes to the start of the test's kmsg (a plain file, not
/dev/kmsg), so the second exec failure's line writes over the first and
counting both always found one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@VysseCodes VysseCodes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Careful work. The /data fallback only checks for an ELF header, which is fine as a last resort.

Merge order: after #426 (this is based on it).

DY-Jeong added a commit to DY-Jeong/HMG-API that referenced this pull request Dec 1, 2026
…, Dalvik layout check, update md5 check, zygote fallback

From AwsomeFox/fix/boot-hardening (Open-HMG#436).

This branch has not been deployed

No deployments
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