|
| 1 | +# Classic `calc` Injection Case (Tight Headerpad) |
| 2 | + |
| 3 | +This folder is a reproducible failure/success comparison for Mach-O dylib injection: |
| 4 | + |
| 5 | +- The target binary (`calc`) has very limited header padding (about 32 bytes). |
| 6 | +- Injecting a long dylib path can corrupt the binary after re-signing. |
| 7 | +- Injecting a short dylib path with the right flags can persist the hook safely. |
| 8 | + |
| 9 | +## Directory Layout |
| 10 | + |
| 11 | +- `calc/calc.c`: target program source |
| 12 | +- `hook/`: Rust hook payload project (`sighook` based) |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- macOS aarch64 (Apple Silicon) |
| 17 | +- `codesign` available in your environment |
| 18 | + |
| 19 | +## Build and Run (Step-by-Step) |
| 20 | + |
| 21 | +Run from repository root: |
| 22 | + |
| 23 | +```bash |
| 24 | +# 1) Build insert-dylib |
| 25 | +cargo build --release |
| 26 | + |
| 27 | +# 2) Enter this example folder |
| 28 | +cd examples/classic-tight-headerpad-case |
| 29 | + |
| 30 | +# 3) Build the target binary |
| 31 | +cc -O2 -fno-inline calc/calc.c -o calc_bin |
| 32 | + |
| 33 | +# 4) Build the hook dylib |
| 34 | +cargo build --release --manifest-path hook/Cargo.toml |
| 35 | +``` |
| 36 | + |
| 37 | +### Baseline Check (`DYLD_INSERT_LIBRARIES`) |
| 38 | + |
| 39 | +```bash |
| 40 | +DYLD_INSERT_LIBRARIES="$PWD/hook/target/release/libclassic_hook.dylib" ./calc_bin |
| 41 | +``` |
| 42 | + |
| 43 | +Expected output: |
| 44 | + |
| 45 | +```text |
| 46 | +[+] hooked: now x0 is 99. |
| 47 | +Result: 99 |
| 48 | +``` |
| 49 | + |
| 50 | +## Failure Repro (Long Dylib Name) |
| 51 | + |
| 52 | +Use a longer filename such as `libh.dylib`: |
| 53 | + |
| 54 | +```bash |
| 55 | +cp hook/target/release/libclassic_hook.dylib libh.dylib |
| 56 | +../../target/release/insert-dylib libh.dylib calc_bin calc_bad |
| 57 | +codesign -f -s - ./calc_bad |
| 58 | +./calc_bad |
| 59 | +``` |
| 60 | + |
| 61 | +Common symptom: |
| 62 | + |
| 63 | +- Process exits unexpectedly and does not print the expected final result line. |
| 64 | + |
| 65 | +## Success Repro (Short Dylib Name) |
| 66 | + |
| 67 | +Use a short filename such as `h.dylib`: |
| 68 | + |
| 69 | +```bash |
| 70 | +cp hook/target/release/libclassic_hook.dylib h.dylib |
| 71 | +../../target/release/insert-dylib --no-strip-codesig h.dylib calc_bin calc_ok |
| 72 | +codesign -f -s - ./calc_ok |
| 73 | +./calc_ok |
| 74 | +``` |
| 75 | + |
| 76 | +Expected output: |
| 77 | + |
| 78 | +```text |
| 79 | +[+] hooked: now x0 is 99. |
| 80 | +Result: 99 |
| 81 | +``` |
| 82 | + |
| 83 | +## Why This Happens |
| 84 | + |
| 85 | +For `LC_LOAD_DYLIB`, command size is: |
| 86 | + |
| 87 | +- `cmdsize = 24 + align8(path_len + 1)` |
| 88 | + |
| 89 | +Where: |
| 90 | + |
| 91 | +- `24` is the fixed size of `struct dylib_command` |
| 92 | +- `path_len + 1` includes the trailing `\0` |
| 93 | + |
| 94 | +In this case: |
| 95 | + |
| 96 | +- available header space is about 32 bytes |
| 97 | +- `libh.dylib` needs 40 bytes (overflow) |
| 98 | +- `h.dylib` needs 32 bytes (fits exactly) |
| 99 | + |
| 100 | +That is why long path injection fails while short path injection succeeds. |
| 101 | + |
| 102 | +## Practical Notes |
| 103 | + |
| 104 | +- When header padding is tight, prefer short install names. |
| 105 | +- In this case, prefer `--no-strip-codesig`. |
| 106 | +- If you can rebuild the target, add `-Wl,-headerpad_max_install_names`. |
0 commit comments