From e878ba40e8c417b2c4a0e541d9aa751a81e89df5 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 10:06:14 -0500 Subject: [PATCH 1/6] Boot hook: write the boot scripts' lines back as they are 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 --- install/common/lib.sh | 67 ++++++++++++++++++++++++++++++++++++------ install/common/test.sh | 51 ++++++++++++++++++++++++++++---- 2 files changed, 103 insertions(+), 15 deletions(-) diff --git a/install/common/lib.sh b/install/common/lib.sh index 295394e6..f5c146f3 100644 --- a/install/common/lib.sh +++ b/install/common/lib.sh @@ -71,7 +71,37 @@ hook_block() { echo "$HOOK_END" } -# copy without the HMG-API block to stdout +# hook_line : the line and a newline, byte for byte. Never echo: the unit's mksh and dash +# (the off-car tests) both expand backslashes in echo's arguments, so "\\" became "\" and a +# "\c" ended the output there, cutting the line short and joining the next one to it (a stock +# line like V="x\cY" left an unclosed quote, and the boot script no longer ran). On the unit: +# mksh's `print -r`. A shell without it (dash in the off-car tests; the unit has no printf): +# echo with every backslash doubled, which an expanding echo turns back into one. Either is +# checked here on a line with backslashes; with neither exact, nothing is written (hook_add +# fails, the file stays as it is). A leading "-" goes as \0055, so echo never takes a line +# like "-n" for an option; hook_add's check catches anything else. +hook_echo() { + _r=$1; _o= + while :; do + case "$_r" in + *\\*) _o="$_o${_r%%\\*}\\\\"; _r=${_r#*\\} ;; + *) break ;; + esac + done + _o=$_o$_r + case "$_o" in -*) _o="\\0055${_o#-}" ;; esac + echo "$_o" +} +if [ "$(print -r -- 'a\\c\n' 2>/dev/null)" = 'a\\c\n' ]; then + hook_line() { print -r -- "$1"; } +elif [ "$(hook_echo 'a\\c\n\x41')" = 'a\\c\n\x41' ] && [ "$(hook_echo '-n')" = '-n' ] && [ "$(hook_echo '-e \\')" = '-e \\' ]; then + hook_line() { hook_echo "$1"; } +else + hook_line() { return 1; } +fi + +# copy without the HMG-API block to stdout: every other line as it is (a last line +# with no newline gets one) hook_strip() { _in=0 while IFS= read -r _l || [ -n "$_l" ]; do @@ -79,13 +109,22 @@ hook_strip() { "$HOOK_BEGIN"*) _in=1; continue ;; "$HOOK_END"*) _in=0; continue ;; esac - [ $_in = 1 ] || echo "$_l" + [ $_in = 1 ] || hook_line "$_l" || return 1 done < "$1" } +# hook_same : the same bytes (by md5; never two empty answers taken for the same) +hook_same() { + _sa=$(sum "$1"); _sb=$(sum "$2") + [ -n "$_sa" ] && [ "$_sa" = "$_sb" ] +} + # hook_add top|end: put the block after the first line (top: the file may # end with "exit 0", as NEMESIS's install-recovery.sh does) or at the end. A # missing file is created (and marked, so hook_remove deletes it again). +# The new file is checked before it goes in: without the block it is the file as it was (a file +# with no block yet: its own bytes, plus a newline when its last line had none). Otherwise 1, +# and the file is left as it is. hook_add() { _f=$ROOT$1 _t=$_f.hmgapi-new @@ -93,17 +132,26 @@ hook_add() { { echo "#!/system/bin/sh"; echo "# HMGAPI_CREATED"; hook_block; echo "exit 0"; } > "$_t" || return 1 chmod 755 "$_t" else - hook_strip "$_f" > "$_t.body" || return 1 + rm -f "$_t" "$_t.body" "$_t.nl" "$_t.check" + hook_strip "$_f" > "$_t.body" || { rm -f "$_t.body"; return 1; } + if ! grep -q "^$HOOK_BEGIN" "$_f"; then + { cat "$_f"; echo; } > "$_t.nl" + hook_same "$_t.body" "$_f" || hook_same "$_t.body" "$_t.nl" || { rm -f "$_t.body" "$_t.nl"; return 1; } + rm -f "$_t.nl" + fi if [ "$2" = top ]; then - _first=1 + _first=1; _bad= while IFS= read -r _l || [ -n "$_l" ]; do - echo "$_l" + hook_line "$_l" || { _bad=1; break; } if [ $_first = 1 ]; then hook_block; _first=0; fi done < "$_t.body" > "$_t" + [ -z "$_bad" ] || { rm -f "$_t" "$_t.body"; return 1; } else - { cat "$_t.body"; hook_block; } > "$_t" + { cat "$_t.body"; hook_block; } > "$_t" || { rm -f "$_t" "$_t.body"; return 1; } fi - rm -f "$_t.body" + hook_strip "$_t" > "$_t.check" && hook_same "$_t.check" "$_t.body" && grep -q "^$HOOK_BEGIN" "$_t" \ + || { rm -f "$_t" "$_t.body" "$_t.check"; return 1; } + rm -f "$_t.body" "$_t.check" chmod 755 "$_t" [ -x "$_f" ] || chmod 644 "$_t" fi @@ -112,8 +160,9 @@ hook_add() { # hook_restore : the backup's own bytes back, when the file is still that # backup plus HMG-API's block and nothing else. hook_strip writes each line again through -# the shell, which can change bytes (a last line with no newline gets one, and a shell's -# echo may expand a backslash), so a stripped file is only close to stock; the backup +# the shell, which can change bytes (a last line with no newline gets one, and releases +# before hook_line wrote lines with a plain echo, which expanded backslashes: a file they +# hooked still has that), so a stripped file is only close to stock; the backup # taken at install is stock exactly. To see that nothing else changed the file since, # both are stripped until another pass changes nothing, then compared: the file has been # through the shell more often than the backup (once per install or update, and again diff --git a/install/common/test.sh b/install/common/test.sh index 21bc1355..7484d078 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -7,8 +7,10 @@ SH=${UNIT_SH:-sh} T=$(mktemp -d); trap 'rm -rf "$T"' EXIT fail=0 check() { if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: wanted [$2], got [$3]"; fail=1; fi; } -L() { env ROOT="$T" $SH -c ". $PWD/lib.sh; $*"; } -mkdir -p "$T/system/etc" +mkdir -p "$T/system/etc" "$T/bin" "$T/bak" +printf '#!/bin/sh\n[ -f "$1" ] || exit 1\nset -- $(md5sum "$1"); echo "$1 $2"\n' > "$T/bin/md5"; chmod +x "$T/bin/md5" # the unit's md5 +L() { env PATH="$T/bin:$PATH" ROOT="$T" $SH -c ". $PWD/lib.sh; $*"; } +LM() { L "$@"; } printf '#!/system/bin/sh\n# NEMESIS\nnemesis watchdog\nexit 0\n' > "$T/system/etc/install-recovery.sh"; chmod 755 "$T/system/etc/install-recovery.sh" printf 'echo qb stock\n# NEMESIS_QB_HOOK\nnemesis watchdog\n' > "$T/system/etc/qb_runtime.sh" IR=$(md5sum < "$T/system/etc/install-recovery.sh"); QB=$(md5sum < "$T/system/etc/qb_runtime.sh") @@ -36,12 +38,49 @@ check "created when missing" "yes" "$([ -x "$T/system/etc/install-recovery.sh" ] L hook_remove /system/etc/install-recovery.sh check "created file removed again" "no" "$([ -e "$T/system/etc/install-recovery.sh" ] && echo yes || echo no)" +# every line written back as it is: echo expanded backslashes in both mksh (the unit) and dash, +# and "\c" ended the output (the line cut short, the next joined to it: an unclosed quote) +# (in $SH, and in mksh when it's installed: its print -r is what the unit uses) +bs_checks() { + B=$T/system/etc/bs.sh + printf '#!/system/bin/sh\nV="x\\cY"; echo ok\nP="a\\\\b"\nE="\\x1b[1;31m\\n\\t\\0101"\n indented\\\n-n\n-e\nexit 0\n' > "$B"; chmod 755 "$B" + BS=$(md5sum < "$B") + L hook_add /system/etc/bs.sh top + check "[$SH] backslashes: block in once" "1" "$(grep -c 'HMGAPI_HOOK begin' "$B")" + check "[$SH] ... the file minus the block is the file as it was" "$BS" "$(grep -v HMGAPI_HOOK "$B" | grep -v 'hmgapid boot' | md5sum)" + check "[$SH] ... the line with backslash-c whole" 'V="x\cY"; echo ok' "$(grep -F 'V="x' "$B")" + sh -n "$B" && echo "ok ... still parses" || { echo "FAIL ... backslash script syntax"; fail=1; } + L hook_add /system/etc/bs.sh top + check "[$SH] ... adding again: one block, nothing else changed" "1|$BS" "$(grep -c 'HMGAPI_HOOK begin' "$B")|$(grep -v HMGAPI_HOOK "$B" | grep -v 'hmgapid boot' | md5sum)" + L hook_remove /system/etc/bs.sh + check "[$SH] ... hook_remove: back byte for byte" "$BS" "$(md5sum < "$B")" + L hook_add /system/etc/bs.sh end + check "[$SH] ... at the end too" "$BS" "$(grep -v HMGAPI_HOOK "$B" | grep -v 'hmgapid boot' | md5sum)" + L hook_remove /system/etc/bs.sh + check "[$SH] ... and back" "$BS" "$(md5sum < "$B")" + printf 'A="\\\\"\nB="\\c"' > "$B" # no newline at the end: the block goes after one + L hook_add /system/etc/bs.sh end + check "[$SH] no last newline: one added, nothing else changed" "$(printf 'A="\\\\"\nB="\\c"\n' | md5sum)" "$(grep -v HMGAPI_HOOK "$B" | grep -v 'hmgapid boot' | md5sum)" + rm -f "$B" + # nothing to write lines with that keeps them as they are: the file is left as it is + printf 'echo qb stock\n' > "$B"; BS=$(md5sum < "$B") + env PATH="$T/bin:$PATH" ROOT="$T" $SH -c ". $PWD/lib.sh; hook_line() { return 1; }; hook_add /system/etc/bs.sh end" && r=0 || r=$? + check "[$SH] no faithful copy: hook_add fails, file untouched, no work files" "1|$BS|bs.sh" "$r|$(md5sum < "$B")|$(ls "$T/system/etc" | grep bs)" + # a copy that changes a line (a shell's echo): caught before the file is replaced + env PATH="$T/bin:$PATH" ROOT="$T" $SH -c ". $PWD/lib.sh; hook_line() { echo \"\$1x\"; }; hook_add /system/etc/bs.sh end" && r=0 || r=$? + check "[$SH] a copy that changes lines: hook_add fails, file untouched" "1|$BS|bs.sh" "$r|$(md5sum < "$B")|$(ls "$T/system/etc" | grep bs)" + rm -f "$B" +} +bs_checks +if [ "${SH##*/}" != mksh ] && command -v mksh >/dev/null 2>&1; then + SH0=$SH; SH=mksh; bs_checks; SH=$SH0 +else + echo "skip backslashes in mksh (no mksh here)" +fi + # hook_restore (the uninstall): the install's backup byte for byte, for a stock file the # shell can't write back exactly (backslashes, as the EV6's qb_runtime.sh has; no newline # at the end), as long as nothing else changed the file since -mkdir -p "$T/bin" "$T/bak" -printf '#!/bin/sh\n[ -f "$1" ] || exit 1\nset -- $(md5sum "$1"); echo "$1 $2"\n' > "$T/bin/md5"; chmod +x "$T/bin/md5" # the unit's md5 -LM() { env PATH="$T/bin:$PATH" ROOT="$T" $SH -c ". $PWD/lib.sh; $*"; } Q=$T/system/etc/qb_runtime.sh printf 'RED="\\x1b[1;31m"\necho "a\\nb\\\\c"\nnemesis watchdog' > "$Q" cp "$Q" "$T/bak/qb"; QS=$(md5sum < "$Q") @@ -73,7 +112,7 @@ printf 'echo qb stock\n# NEMESIS_QB_HOOK\nnemesis watchdog\n' > "$Q" # hook-check.sh at boot: a NEMESIS install or update rewrites install-recovery.sh as a whole D=$T/data/hmgapi mkdir -p "$D"; cp lib.sh "$D/"; echo "version 1.0.0" > "$D/installed.txt" -HC() { env ROOT="$T" $SH "$PWD/hook-check.sh" 2>&1; } +HC() { env PATH="$T/bin:$PATH" ROOT="$T" $SH "$PWD/hook-check.sh" 2>&1; } printf '#!/system/bin/sh\n# NEMESIS\nnemesis watchdog\nexit 0\n' > "$T/system/etc/install-recovery.sh"; chmod 755 "$T/system/etc/install-recovery.sh" L hook_add /system/etc/install-recovery.sh top L hook_add /system/etc/qb_runtime.sh end From ed257fb4a87bfbf43654adf76eff6351f6a673d1 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 10:06:14 -0500 Subject: [PATCH 2/6] Hook runtime: check the Dalvik layout before the first hook 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 --- core/loader/loader.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/core/loader/loader.c b/core/loader/loader.c index 8ac40f56..7fa29679 100644 --- a/core/loader/loader.c +++ b/core/loader/loader.c @@ -62,10 +62,12 @@ int cacheflush(long start, long end, long flags); /* ---- Dalvik 4.4 layouts (offsets checked on the EV6 libdvm) ---- */ #define ACC_STATIC 0x0008 #define ACC_NATIVE 0x0100 +#define M_CLASS(m) (*(void **)(m)) #define M_ACCESS(m) (*(u4 *)((char *)(m) + 0x04)) #define M_REGS(m) (*(u2 *)((char *)(m) + 0x0a)) #define M_OUTS(m) (*(u2 *)((char *)(m) + 0x0c)) #define M_INS(m) (*(u2 *)((char *)(m) + 0x0e)) +#define M_NAME(m) (*(const char **)((char *)(m) + 0x10)) #define M_SHORTY(m) (*(const char **)((char *)(m) + 0x1c)) #define M_INSNS(m) (*(void **)((char *)(m) + 0x20)) #define M_NATIVE(m) (*(void **)((char *)(m) + 0x28)) @@ -74,6 +76,8 @@ int cacheflush(long start, long end, long flags); #define CLASS_PRIM(c) (*(int *)((char *)(c) + 0x44)) #define PRIM_NOT 0 #define PRIM_VOID 1 +#define PRIM_INT 6 +#define ARRAY_LEN(a) (*(u4 *)((char *)(a) + 0x08)) #define ARRAY_DATA(a) ((void **)((char *)(a) + 0x10)) typedef union { int i; long long j; float f; double d; void *l; } JValue; @@ -313,6 +317,47 @@ static int failed(JNIEnv *env, const char *what) return 1; } +static int streq(const char *a, const char *b) +{ + if (!a || !b) return 0; + while (*a && *a == *b) a++, b++; + return *a == *b; +} + +/* + * The offsets above are the EV6 libdvm's (AOSP 4.4), with no check that this unit's libdvm is + * that build. A libdvm laid out otherwise would have the hooks write the wrong words of every + * hooked Method: in the zygote the boot guard catches the crash, but in an app it would crash + * as each one starts, which nothing catches. So before the first hook, the layout is read back + * from Bridge's own methods, which libdvm found by name, where nothing can be wrong but the + * offsets: the declaring class, name, shorty, flags and argument count of each, the 0x28 + * nativeFunc that dvmSetNativeFunc (libdvm's own code) just wrote, the primitive type of int, + * void and Bridge, and an array's length. 0, with the reason logged: no hooks. + */ +static const char *layout_bad(void *bc, void *orig, void *handle, void *self) +{ + void *a; + u4 len; + if (M_CLASS(handle) != bc || M_CLASS(orig) != bc) return "Method.clazz (0x00)"; + if (!streq(M_NAME(handle), "handleHookedMethod") || !streq(M_NAME(orig), "invokeOriginalMethodNative")) + return "Method.name (0x10)"; + /* (Member, int, Object, Object, Object[]) Object; (Member, int, Class[], Class, Object, Object[]) Object */ + if (!streq(M_SHORTY(handle), "LLILLL") || !streq(M_SHORTY(orig), "LLILLLL")) return "Method.shorty (0x1c)"; + if ((M_ACCESS(handle) & (ACC_STATIC | ACC_NATIVE)) != ACC_STATIC + || (M_ACCESS(orig) & (ACC_STATIC | ACC_NATIVE)) != (ACC_STATIC | ACC_NATIVE)) return "Method.accessFlags (0x04)"; + if (M_INS(handle) != 5 || M_INS(orig) != 6 || M_REGS(handle) < M_INS(handle)) return "Method.insSize/registersSize (0x0a, 0x0e)"; + if (!M_INSNS(handle)) return "Method.insns (0x20)"; + if (M_NATIVE(orig) != (void *)invokeOriginal) return "Method.nativeFunc (0x28)"; + if (CLASS_PRIM(dvm.findPrim('I')) != PRIM_INT || CLASS_PRIM(dvm.findPrim('V')) != PRIM_VOID || CLASS_PRIM(bc) != PRIM_NOT) + return "ClassObject.primitiveType (0x44)"; + a = dvm.allocArray(gObjectArrayClass, 3, 0); + if (!a) return "an Object[3] (no memory)"; + len = ARRAY_LEN(a); + dvm.release(a, self); + if (len != 3) return "ArrayObject.length (0x08)"; + return 0; +} + static int load_dvm(void) { void *h = dlopen("libdvm.so", 0); @@ -359,6 +404,14 @@ static void start_bridge(JNIEnv *env, const char *jar, const char *entry) gObjectArrayClass = dvm.findArrayClass("[Ljava/lang/Object;", 0); if (!m || !gHandleHooked || !gObjectArrayClass) { LOGE("init: bridge methods not found"); return; } dvm.setNativeFunc(m, invokeOriginal, 0); + { + const char *bad = layout_bad(bc, m, gHandleHooked, self); + if (bad) { + /* Bridge.init never runs, so nothing is hooked: the unit runs stock */ + LOGE("init: this libdvm's layout isn't the one HMG-API was built for (%s doesn't match), so hooks are off. Please send this line to the HMG-API maintainers with your car and its head unit software version", bad); + return; + } + } m = GetStaticMethodID(env, bridge, entry, "()V"); if (failed(env, entry) || !m) return; From 8eaf986d65e3c4acdc33170cf3a696c29105412c Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 10:06:14 -0500 Subject: [PATCH 3/6] Installer: an update checks the stock copy by md5 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 --- install/common/install-test.sh | 37 ++++++++++++++++++++++++++++++++++ install/common/install.sh | 19 +++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/install/common/install-test.sh b/install/common/install-test.sh index 0786ddd8..21aab98f 100644 --- a/install/common/install-test.sh +++ b/install/common/install-test.sh @@ -124,6 +124,43 @@ has "update over itself" "HMG-API's wrapper is in place" "$out" check "update keeps the stock copy" "$(md5sum < $T/real)" "$(md5sum < $R/system/bin/app_process.hmgapi-stock)" check "update keeps one boot hook" 1 "$(grep -c 'HMGAPI_HOOK begin' $R/system/etc/install-recovery.sh)" check "update keeps the owner's settings" '{"updates": true}' "$(cat $R/data/hmgapi/settings/hmgapi.json)" +has "... checks the stock copy by md5" "app_process.hmgapi-stock: stock (" "$out" + +# an update checks the stock copy by the md5 of the whole file, as a new install checks +# app_process: one that only starts like a binary (cut short, zero-filled, changed) comes back +# from the stock backup, and with no stock backup it's refused, nothing changed +K=$R/system/bin/app_process.hmgapi-stock; KB=$R/data/hmgapi/stock/system/bin/app_process +dd if=$T/real of=$T/bad.short bs=1024 count=2 2>/dev/null # cut short: still starts with ELF +dd if=/dev/zero of=$T/bad.zero bs=1 count=$(($(wc -c < $T/real))) 2>/dev/null # zero-filled, the same size +{ cat $T/real; echo x; } > $T/bad.elf # ELF, the wrong md5 +for bad in short zero elf; do + cp $T/bad.$bad $K + run $P/install.sh $P + check "update, stock copy $bad: works" 0 $rc + has "... said: put back from the stock backup" "app_process.hmgapi-stock md5 $(md5sum < $T/bad.$bad | cut -c1-32) isn't a known stock build (a copy cut short or damaged?): putting it back from the stock backup (" "$out" + check "... the stock copy is stock again" "$(md5sum < $T/real)" "$(md5sum < $K)" + check "... the backup kept" "$(md5sum < $T/real)" "$(md5sum < $KB)" + check "... the wrapper in place" "$(md5sum < $P/app_process)" "$(md5sum < $R/system/bin/app_process)" +done +mv $KB $T/bak.away +for bad in short zero elf; do + cp $T/bad.$bad $K; B4=$(tree) + run $P/install.sh $P + check "update, stock copy $bad, no stock backup: refused" 1 $rc + has "... said" "app_process.hmgapi-stock, the stock copy HMG-API's wrapper runs, has md5 $(md5sum < $T/bad.$bad | cut -c1-32), which isn't a known stock build (it may have been cut short or damaged), and there's no stock backup to put it back from, so nothing was changed." "$out" + check "... names no way to force it" "" "$(echo "$out" | grep -i force || true)" + check "... /system unchanged" "$B4" "$(tree)" +done +cp $T/bad.short $T/bak.bad; mv $T/bak.bad $KB; cp $T/bad.elf $K # a backup that isn't stock either +run $P/install.sh $P +check "update, stock copy and backup both unknown: refused" 1 $rc +cp $T/bad.elf $KB # a forced install of a build stock.txt doesn't know: both copies the same +export HMGAPI_FORCE=1; run $P/install.sh $P; unset HMGAPI_FORCE +check "... HMGAPI_FORCE=1 (maintainers, over adb): goes on" 0 $rc +has "... with a warning" "WARNING: app_process.hmgapi-stock md5 $(md5sum < $T/bad.elf | cut -c1-32) isn't a known stock build" "$out" +mv $T/bak.away $KB; cp $T/real $K +run $P/install.sh $P +check "stock copy and backup stock again: update works" "0|$(md5sum < $T/real)" "$rc|$(md5sum < $K)" # the app's install with Android up (stand-ins for pm and the rest) rm -f $R/data/hmgapi/public/HmgApi.apk; calls >/dev/null diff --git a/install/common/install.sh b/install/common/install.sh index 951ded0b..6e1a0eb8 100644 --- a/install/common/install.sh +++ b/install/common/install.sh @@ -157,6 +157,25 @@ if [ -n "$LPFIX" ]; then elif [ -f "$KEEP" ] && { [ "$HAVE" = "$WRAP" ] || [ -f "$BAK" ] || is_wrapper "$B"; }; then log "app_process: HMG-API's wrapper is in place (stock kept as app_process.hmgapi-stock)" UPDATE=1 + # the stock copy by the md5 of the whole file, as a new install checks app_process: not_stock + # below only sees whether it starts like a binary, and a copy cut short, zero-filled or + # otherwise damaged would pass that, then start no zygote. One that isn't a known stock build + # comes back from the backup when that is one (as RESCUE does for a missing copy); with + # neither known, refused as a new install is (HMGAPI_FORCE: maintainers only) + KS=$(sum "$KEEP"); kk= + while read m label; do [ "$m" = "$KS" ] && kk=$label; done < "$P/stock.txt" + RBAK=$(sum "$BAK"); rk= + [ -n "$RBAK" ] && while read m label; do [ "$m" = "$RBAK" ] && rk=$label; done < "$P/stock.txt" + if [ -n "$kk" ]; then + log "app_process.hmgapi-stock: stock ($kk)" + elif [ -n "$rk" ]; then + log "app_process.hmgapi-stock md5 $KS isn't a known stock build (a copy cut short or damaged?): putting it back from the stock backup ($rk)" + RESCUE=1 + elif [ "$HMGAPI_FORCE" = 1 ]; then + log "WARNING: app_process.hmgapi-stock md5 $KS isn't a known stock build, and there's no stock backup to put back; HMGAPI_FORCE=1, going on at your own risk" + else + fail "app_process.hmgapi-stock, the stock copy HMG-API's wrapper runs, has md5 $KS, which isn't a known stock build (it may have been cut short or damaged), and there's no stock backup to put it back from, so nothing was changed. $ASK_BUILD, and we'll add your build." + fi elif [ ! -f "$KEEP" ] && { [ "$HAVE" = "$WRAP" ] || is_wrapper "$B"; }; then # an HMG-API wrapper (this release's, or an older one: every version names # app_process.hmgapi-stock) with its stock copy gone: what an older uninstall leaves on NEMESIS From dbaad4891f7132283e9de6d957f2cf3157d4428d Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 10:06:14 -0500 Subject: [PATCH 4/6] Zygote wrapper: run the stock backup when the stock copy won't exec 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 --- core/zygote/test.sh | 28 ++++++++++++++++++++++++++-- core/zygote/zygote.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/core/zygote/test.sh b/core/zygote/test.sh index 1fe28632..eba0298d 100755 --- a/core/zygote/test.sh +++ b/core/zygote/test.sh @@ -17,7 +17,7 @@ chmod +x "$T/stock" mkdir -p "$T/hook" echo 5e1d0c34-aaaa-4bbb-8ccc-000000000001 > "$T/boot_id" # one kernel boot, until a test changes it . ../../tools/env.sh -clang $ARM_STATIC -DGUARD_DELAY=1 -DSTOCK="\"$T/stock\"" -DKMSG="\"$T/kmsg\"" -DHOOKDIR="\"$T/hook\"" -DHOOKLIB="\"$T/lib.so\"" -DHMGAPID="\"$T/hmgapid\"" -DBOOTID="\"$T/boot_id\"" \ +clang $ARM_STATIC -DGUARD_DELAY=1 -DSTOCK="\"$T/stock\"" -DSTOCKBAK="\"$T/stockbak\"" -DKMSG="\"$T/kmsg\"" -DHOOKDIR="\"$T/hook\"" -DHOOKLIB="\"$T/lib.so\"" -DHMGAPID="\"$T/hmgapid\"" -DBOOTID="\"$T/boot_id\"" \ -o "$T/wrap" zygote.c fail=0 check() { # name, expected-substring, actual @@ -106,8 +106,32 @@ echo 0 > "$T/hook/starts" sleep 2 check "watcher: no reset after zygote died" "1" "$(cat "$T/hook/starts")" +# the stock copy lost or damaged: the install's stock backup runs instead (mode 600, made 700) +# the backup: an ARM binary standing in for stock (the wrapper built again, running the script +# that prints its arguments) +cp "$T/stock" "$T/stock.sh" +clang $ARM_STATIC -DSTOCK="\"$T/stock.sh\"" -DSTOCKBAK="\"$T/none\"" -DKMSG="\"$T/kmsg.bak\"" -DHOOKDIR="\"$T/hook\"" -DHOOKLIB="\"$T/lib.so\"" -DHMGAPID="\"$T/hmgapid\"" -DBOOTID="\"$T/boot_id\"" \ + -o "$T/stockbak" zygote.c +chmod 600 "$T/stockbak" +mv "$T/stock" "$T/stock.away"; printf 'short' > "$T/stock"; chmod 755 "$T/stock" # cut short: no binary, not a script +: > "$T/kmsg"; out=$(LD_PRELOAD=old.so "$T/wrap" -Xzygote --zygote 2>/dev/null) +check "damaged stock copy: the stock backup runs, with the same arguments" "ARGS:-Xzygote --zygote" "$out" +check "... logged, with the errno" "failed, errno 8, exec $T/stockbak (the stock backup)" "$(cat "$T/kmsg")" +check "... the backup made executable (it's 600 after the install)" "-rwx------" "$(ls -l "$T/stockbak" | cut -c1-10)" rm "$T/stock" +: > "$T/kmsg"; out=$(LD_PRELOAD=old.so "$T/wrap" --zygote 2>/dev/null) +check "stock copy gone: the stock backup runs" "ARGS:--zygote" "$out" +printf 'not an ELF' > "$T/stockbak" +: > "$T/kmsg"; set +e; "$T/wrap" --zygote 2>/dev/null; rc=$?; set -e +check "backup isn't a binary: not run, exits 127" "127" "$rc" +check "... logged" "failed, errno 2, and no stock backup" "$(cat "$T/kmsg")" +rm "$T/stockbak" : > "$T/kmsg"; set +e; "$T/wrap" --zygote 2>/dev/null; rc=$?; set -e check "exec failure exits 127" "127" "$rc" -check "exec failure logged" "failed, errno 2" "$(cat "$T/kmsg")" +check "exec failure logged" "failed, errno 2, and no stock backup" "$(cat "$T/kmsg")" +{ printf '\177ELF'; printf 'garbage'; } > "$T/stockbak"; chmod 600 "$T/stockbak" +: > "$T/kmsg"; set +e; "$T/wrap" --zygote 2>/dev/null; rc=$?; set -e +check "a backup that won't exec either: exits 127" "127" "$rc" +check "... both failures logged" "2" "$(grep -c 'failed, errno' "$T/kmsg")" +check "... the backup's with its errno" "exec $T/stockbak failed, errno" "$(cat "$T/kmsg")" exit $fail diff --git a/core/zygote/zygote.c b/core/zygote/zygote.c index cd4f071d..faf085e5 100644 --- a/core/zygote/zygote.c +++ b/core/zygote/zygote.c @@ -30,8 +30,15 @@ * install-recovery.sh). hmgapid is single-instance (its socket bind): a second * start, here or from the boot hook, just exits. * Every start writes one line to the kernel log (tag "hmgapi-hook"). - * If the exec fails there is nothing left to fall back to, so this file stays - * as small as possible. + * If the exec fails (the stock copy lost or damaged on /system: a copy that never + * reached storage before a power loss), the install's backup of the stock binary, + * /data/hmgapi/stock/system/bin/app_process (its md5 checked against stock.txt when + * it was taken), is run instead, so Android still starts and an install or + * uninstall can put /system right. It's mode 600 (the install's umask 077), so it's + * made 700 first; it must start like an ELF binary. Where SELinux refuses an exec + * from /data (an Enforcing unit) that fails too, and this returns 127 as before. + * Beyond that there is nothing left to fall back to, so this file stays as small + * as possible. * * Builds freestanding for ARM (no libc, raw syscalls). AGPL-3.0-or-later. */ @@ -43,6 +50,9 @@ typedef unsigned long ul; #ifndef KMSG #define KMSG "/dev/kmsg" #endif +#ifndef STOCKBAK +#define STOCKBAK "/data/hmgapi/stock/system/bin/app_process" +#endif #ifndef HOOKDIR #define HOOKDIR "/data/hmgapi/hook" #endif @@ -79,6 +89,7 @@ static long xgetpid(void) { return sys3(20, 0, 0, 0); } static void xexit(int c) { sys3(1, c, 0, 0); for (;;) ; } static void xsleep(long s) { long ts[2] = { s, 0 }; sys3(162, (long)ts, 0, 0); } static void xunlink(const char *p) { sys3(10, (long)p, 0, 0); } +static long xchmod(const char *p, long mode) { return sys3(15, (long)p, mode, 0); } #define O_RDONLY 0 #define O_WRONLY 1 #define O_WR 0x241 /* O_WRONLY|O_CREAT|O_TRUNC */ @@ -87,6 +98,14 @@ static ul slen(const char *s) { ul n = 0; while (s[n]) n++; return n; } static int seq(const char *a, const char *b) { while (*a && *a == *b) a++, b++; return *a == *b; } static int spre(const char *s, const char *p) { while (*p && *s == *p) s++, p++; return !*p; } static int exists(const char *p) { long fd = xopen(p, O_RDONLY, 0); if (fd < 0) return 0; xclose(fd); return 1; } +static int is_elf(const char *p) +{ + char m[4]; long fd = xopen(p, O_RDONLY, 0), n; + if (fd < 0) return 0; + n = xread(fd, m, 4); + xclose(fd); + return n == 4 && m[0] == 0x7f && m[1] == 'E' && m[2] == 'L' && m[3] == 'F'; +} static char line[160]; static ul lpos; @@ -267,6 +286,13 @@ int main(int argc, char **argv, char **envp) rc = xexecve(STOCK, argv, env); put("<3>hmgapi-hook: exec " STOCK " failed, errno "); putn(-rc); + if (!is_elf(STOCKBAK)) { put(", and no stock backup"); flush(); return 127; } + put(", exec " STOCKBAK " (the stock backup)"); + flush(); + xchmod(STOCKBAK, 0700); + rc = xexecve(STOCKBAK, argv, env); + put("<3>hmgapi-hook: exec " STOCKBAK " failed, errno "); + putn(-rc); flush(); (void)slen; return 127; From db336f4cc7bda2be6320030b89b3ac21814f4b27 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 10:06:14 -0500 Subject: [PATCH 5/6] Changelog and notes: boot hook, Dalvik layout check, update md5 check, zygote fallback Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 28 ++++++++++++++++++++++++++++ CLAUDE.md | 4 ++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abe1b1c..6c625c9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,34 @@ copy is made. The uninstall was already safe (stock goes back over existing files, then `sync`). The same gap in NEMESIS 1.17's `app_process.real` was reported to NEMESIS. Tested off-car only. +- **The boot hook keeps the boot scripts' other lines as they are.** Putting HMG-API's block + into NEMESIS's `install-recovery.sh` and the stock `qb_runtime.sh` (every install, and + `hook-check.sh` at boot) wrote each line again with `echo`, which expands backslashes in the + unit's mksh as in dash: `\\` became `\`, and a `\c` ended the output there, so a line like + `V="x\cY"; echo ok` lost its closing quote and joined the next one, and the script stopped + running. Lines are now written with mksh's `print -r` (in a shell without it, as the + off-car tests' dash, with `echo` and every backslash doubled), checked on a test line when + the script starts, and the new file is checked before it goes in: without the block it must + be the file as it was, or the file is left alone. Tested off-car only (dash and macOS `sh`; + CI also runs the checks in mksh), not yet on a car. +- **An update checks the stock copy by md5.** A new install checks `app_process` against + `stock.txt`, but an update only checked that `app_process.hmgapi-stock` started like a + binary, so a copy cut short, zero-filled or otherwise damaged passed, and the next start had + no zygote. The catalogue's `engine update hmgapi` runs the same installer. Now a stock copy + that isn't a known build comes back from the stock backup when that is one, and with no + stock backup the update is refused with nothing changed, as a new install is + (`HMGAPI_FORCE=1` stays for maintainers). Tested off-car only. +- **The zygote wrapper falls back to the stock backup.** When the stock copy won't run (lost + or damaged on `/system`), the wrapper runs the install's backup of the stock binary, + `/data/hmgapi/stock/system/bin/app_process` (made executable first, and only if it starts + like an ELF binary), so Android still starts and an install or uninstall can put `/system` + right. Where SELinux refuses an exec from `/data` (an Enforcing unit) it exits as before. + Not yet tested on a car. +- **Hooks stay off on a Dalvik other than the EV6's.** The hook runtime's struct offsets come + from the EV6's libdvm. Before the first hook it now reads them back from Bridge's own + methods (class, name, shorty, flags, argument count, native function), the primitive types + and an array's length; on a mismatch it logs which one and leaves hooks off, so the unit runs + stock instead of crashing each app as it starts. Not yet tested on a car. ## 1.1.0-rc.10 (2026-11-25) diff --git a/CLAUDE.md b/CLAUDE.md index 9d4f13ff..6863cc34 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -33,8 +33,8 @@ Owners get to decide for their own units. Follow these by default. If the user a | Path | What | |---|---| | `core/hmgapid` | root daemon + client, static ARM (no libc). Ops per caller uid (root/shell: all incl. `sh`, `remount`; `hmgapi.app`: HMG-API ops; others: `ping`). Abstract socket `@hmgapid`. | -| `core/zygote` | `app_process` wrapper (installed as `/system/bin/app_process`, stock kept as `app_process.hmgapi-stock`): `LD_PRELOAD` for the zygote when `/data/hmgapi/hook/enable`, boot guard (`hook/starts`: starts in a row that didn't last 120 s, off at 6; `hook/boot`: the kernel's `boot_id` and its count, off at 3, so restarts within 120 s don't count as crashes; the watcher resets both after 120 s). | -| `core/loader` | `libhmgapi_hook.so`: interposes `JNI_CreateJavaVM`, loads `/system/framework/hmgapi-hook.jar` before `ZygoteInit.main`, Dalvik method hooks (Xposed-style, offsets checked on the EV6 libdvm). | +| `core/zygote` | `app_process` wrapper (installed as `/system/bin/app_process`, stock kept as `app_process.hmgapi-stock`): `LD_PRELOAD` for the zygote when `/data/hmgapi/hook/enable`, boot guard (`hook/starts`: starts in a row that didn't last 120 s, off at 6; `hook/boot`: the kernel's `boot_id` and its count, off at 3, so restarts within 120 s don't count as crashes; the watcher resets both after 120 s). When the stock copy won't exec, it runs the install's backup, `/data/hmgapi/stock/system/bin/app_process` (made 700 first, ELF only). | +| `core/loader` | `libhmgapi_hook.so`: interposes `JNI_CreateJavaVM`, loads `/system/framework/hmgapi-hook.jar` before `ZygoteInit.main`, Dalvik method hooks (Xposed-style, offsets checked on the EV6 libdvm, and read back from Bridge's own methods before the first hook: a different layout logs why and leaves hooks off). | | `core/runtime` | `hmgapi-hook.jar`: `Bridge`, `Hooks`, `MethodHook`, `ModEntry`/`ModContext`/`LoadedApp` (mod API), `Mods` (loader), launcher/editor hooks (`HMG-API`), ★ choices (`StarKey`). | | `core/app` | `hmgapi.app`: status + mod list (from `HmgApiStatus.json`, set by the hooks). | | `core/client` | `hmgapi-client.jar`: for mods' own apps and services (bundled in their APKs). | From 3d57727dda45074d27637019bffbad7972d04088 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 10:27:55 -0500 Subject: [PATCH 6/6] Zygote test: check the backup's failure line only 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 --- core/zygote/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/zygote/test.sh b/core/zygote/test.sh index eba0298d..93c1d55c 100755 --- a/core/zygote/test.sh +++ b/core/zygote/test.sh @@ -132,6 +132,6 @@ check "exec failure logged" "failed, errno 2, and no stock backup" "$(cat "$T/km { printf '\177ELF'; printf 'garbage'; } > "$T/stockbak"; chmod 600 "$T/stockbak" : > "$T/kmsg"; set +e; "$T/wrap" --zygote 2>/dev/null; rc=$?; set -e check "a backup that won't exec either: exits 127" "127" "$rc" -check "... both failures logged" "2" "$(grep -c 'failed, errno' "$T/kmsg")" -check "... the backup's with its errno" "exec $T/stockbak failed, errno" "$(cat "$T/kmsg")" +# (each log line goes to the start of $T/kmsg, a plain file here, so only the last is left) +check "... the backup's failure logged, with its errno" "exec $T/stockbak failed, errno" "$(cat "$T/kmsg")" exit $fail