Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). |
Expand Down
53 changes: 53 additions & 0 deletions core/loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 26 additions & 2 deletions core/zygote/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
# (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
30 changes: 28 additions & 2 deletions core/zygote/zygote.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions install/common/install-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading