Lab 02 — The patch that changed nothing¶
Level: Intermediate · Time: 2–3 hours · Needs: the NDK on your host. A device is optional for Parts 1–3 and required for Part 4.
What this lab is¶
Everything you measure in this lab will be true, and you will still reach a wrong conclusion — twice. That is the design.
Lab 01 was about a lying source. This one is about honest sources answering a question adjacent to the one you meant. It is the harder failure, because there is nothing to catch: no contradiction surfaces, no second source disagrees, and the wrong conclusion is fully supported by correct evidence.
Objective. By the end you will be able to distinguish two mechanisms that produce the identical
symptom "my correct patch did nothing", state what a verified patch actually proves, and explain why
__attribute__((noinline)) does not do what most people think it does.
Prerequisites. 18-ARM §5 for AArch64 basics; a passing familiarity with
objdump. You do not need to read 59-radare2 first, and if you have
not, so much the better — you will meet its finding cold.
Part 0 — Toolchain¶
# host -- adjust the host tag: windows-x86_64, linux-x86_64 or darwin-x86_64
export NDK_BIN="$HOME/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/bin"
export OUT="$HOME/ch74-lab02" && mkdir -p "$OUT"
"$NDK_BIN/aarch64-linux-android21-clang" --version
"$NDK_BIN/llvm-objdump" --version
Build outside the repository.
.so,.oand executables are on this repository's artefact blocklist. Everything below writes to$OUT. If you are on Git Bash under Windows, remember that native tools cannot open an MSYS path — usecygpath -wif a tool reports "No such file" for a path that plainly exists.
Part 1 — Build a target and find the check¶
Save this as $OUT/gate.c. It is deliberately close to what real licence gating looks like.
/* $OUT/gate.c */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/* Returns 1 if the environment looks tampered with. */
__attribute__((noinline))
int probe_paths(void)
{
static const char *paths[] = { "/system/xbin/su", "/system/bin/su", "/sbin/su" };
for (unsigned i = 0; i < sizeof paths / sizeof paths[0]; i++) {
if (access(paths[i], F_OK) == 0) {
return 1;
}
}
return 0;
}
/* Returns 1 if the key is correct. */
__attribute__((noinline))
int verify_license(const char *key)
{
return key && strcmp(key, "lab02-valid") == 0;
}
/* The decision. */
__attribute__((noinline))
int gate(const char *key)
{
if (probe_paths()) {
return 0;
}
return verify_license(key);
}
int main(int argc, char **argv)
{
const char *key = (argc > 1) ? argv[1] : "";
printf("gate=%d\n", gate(key));
return 0;
}
# host
"$NDK_BIN/aarch64-linux-android21-clang" -Wall -Wextra -O2 -pie \
-Wl,-z,max-page-size=16384 -o "$OUT/gate" "$OUT/gate.c"
The
max-page-sizeflag is not optional if you intend to run this on a 16 KiB-page emulator. Without it a binary as trivial asint main(void){return 42;}exits 139 (SIGSEGV), and no error message anywhere mentions page size. This repository has reproduced that twice independently.
Now find the check:
# host
"$NDK_BIN/llvm-objdump" -d "$OUT/gate" | grep -A2 '<verify_license>:'
"$NDK_BIN/llvm-objdump" -d "$OUT/gate" | grep -A14 '<gate>:'
⏸ STOP 1¶
You want the program to accept a wrong key. You have found a function called verify_license.
- Write down the patch you would apply, and to which function.
- Write down what you expect to observe if the patch works. Be specific — an exact expected output.
- Write down what you would observe if it did not work.
Question 3 is the one everybody skips, and it is the one that makes the rest of the lab legible.
Part 2 — Apply the patch and verify it¶
On AArch64 the two instructions you need are:
| Instruction | Bytes (little-endian, file order) | Word |
|---|---|---|
mov w0, #1 |
20 00 80 52 |
0x52800020 |
mov w0, #0 |
00 00 80 52 |
0x52800000 |
ret |
c0 03 5f d6 |
0xd65f03c0 |
Find the virtual address of verify_license and convert it to a file offset. For a PIE built this
way the .text delta is constant across the section — derive it, do not assume it:
# host -- get .text's vaddr and file offset, then subtract
"$NDK_BIN/llvm-readelf" -S "$OUT/gate" | grep -w '.text'
Patch with Python so the arithmetic is visible:
#!/usr/bin/env python3
# $OUT/patch.py -- write "mov w0,#1; ret" at a file offset.
import sys
path, offset = sys.argv[1], int(sys.argv[2], 0)
patch = bytes([0x20, 0x00, 0x80, 0x52, # mov w0, #1
0xc0, 0x03, 0x5f, 0xd6]) # ret
with open(path, 'r+b') as fh:
fh.seek(offset)
before = fh.read(len(patch))
fh.seek(offset)
fh.write(patch)
print("offset : 0x%x" % offset)
print("before : %s" % before.hex(' '))
print("after : %s" % patch.hex(' '))
# host
cp "$OUT/gate" "$OUT/gate-patched"
python3 -B "$OUT/patch.py" "$OUT/gate-patched" <your-offset>
# VERIFY the patch is present
"$NDK_BIN/llvm-objdump" -d "$OUT/gate-patched" | grep -A3 '<verify_license>:'
You should see exactly:
<verify_license>:
....: 20 00 80 52 mov w0, #1
....: c0 03 5f d6 ret
⏸ STOP 2¶
The patch is present. The bytes are verified in the file. The disassembler agrees.
Write down: what have you just proved? One sentence. Be precise, and resist the obvious answer.
Answer — and it is the whole lab
You have proved **that the file changed**. You have not proved anything about the program. Those are different propositions, and the verification step everybody performs confirms the wrong one. `objdump` reads the file. Nobody cares about the file. The proposition you care about is *the program behaves differently*, and nothing you have done so far speaks to it.Part 3 — Run it¶
If you have a device:
# host
adb push "$OUT/gate" "$OUT/gate-patched" /data/local/tmp/
adb shell "chmod +x /data/local/tmp/gate /data/local/tmp/gate-patched"
for b in gate gate-patched; do
for k in wrongkey lab02-valid; do
printf '%-14s %-12s -> ' "$b" "$k"
adb shell "/data/local/tmp/$b $k"
done
done
If your device blocks execve under /data/local/tmp — some vendor builds do — run through the
linker instead:
adb shell "/system/bin/linker64 /data/local/tmp/gate wrongkey"
No device? Reason it out from the disassembly of gate and check yourself against the table below.
⏸ STOP 3¶
Fill in the four cells, then explain every result.
| Binary | key=wrongkey |
key=lab02-valid |
|---|---|---|
gate (unpatched) |
||
gate-patched |
What the reference measurement showed
On a device that ships `/system/xbin/su` — which every `userdebug` build does, and the stock Google emulator image is one — the result is: | Binary | `key=wrongkey` | `key=lab02-valid` | |---|---|---| | `gate` (unpatched) | `gate=0` | **`gate=0`** | | `gate-patched` | `gate=0` | **`gate=0`** | **Four zeroes.** Read the top-right cell first: **the correct key alone does not open the gate.** The licence check was never what stood in the way. Then the bottom row: the function named for the protection, patched to always succeed, verified present in the bytes, changed nothing at all. If your device has no `su` at any of those three paths, you will instead see `gate=0 / gate=1` on the top row and `gate=1 / gate=1` on the bottom — the patch works. **That is also a finding**, and it tells you your device does not trip `probe_paths`. Record which you saw and why; the mechanism is the same either way.Part 4 — Diagnose it¶
Do not guess. Read the caller.
# host
"$NDK_BIN/llvm-objdump" -d "$OUT/gate" | grep -A14 '<gate>:'
⏸ STOP 4¶
- How many
blinstructions are ingate, and what do they target? - Under what condition is
verify_licensereached? - Complete the truth table by patching the other function and re-running. What are the four patch-state combinations?
Answer
`gate` calls `probe_paths` **first** and branches on its result. On a non-zero result it returns 0 immediately, and `verify_license` is never reached. The shape looks like this:<gate>:
....: bl <probe_paths>
....: cbz w0, <continue> ; if probe == 0, carry on
....: mov w0, wzr ; else return 0 right now
....: b <exit>
<continue>:
....: bl <verify_license>
Part 5 — The second mechanism, same symptom¶
A different investigation, on a physical arm64 device (Android 13 / API 33), produced this:
# adb shell -- lc_verify_signature patched to return 1, WRONG token supplied
lc_verify_signature: 1 <-- the patch worked, observably
lc_decrypt_flag : refused (-1) <-- and the thing you wanted still refuses
Here the patched function is observably returning 1 — the program prints it. And the dependent operation still refuses. There is no upstream guard in this binary at all.
⏸ STOP 5¶
- What is the mechanism? It is not short-circuiting.
- What single command would distinguish this case from Part 4's?
Answer
**1.** At `-O2` clang **inlined** the body of `lc_verify_signature` into `lc_decrypt_flag`. The binary now contains two copies of the logic, and the patch landed on the standalone copy — the one nothing calls. There is no `bl lc_verify_signature` anywhere inside `lc_decrypt_flag`. **The check was copied, not called.** **2.** Count the call sites:llvm-objdump -d "$OUT/gate" | grep -oE 'bl[[:space:]]+0x[0-9a-f]+ <[^>]+>' | sort | uniq -c
Part 6 — The bonus refutation: noinline does not mean what you think¶
Every function in gate.c carries __attribute__((noinline)). That is why Part 4 worked at all —
without it, all three functions collapse into main and there is nothing to patch.
Now watch the attribute fail to do its job on a different transformation. Build the chapter's
fixture, which also carries noinline:
# host -- from the chapter's examples directory
"$NDK_BIN/aarch64-linux-android21-clang" -Wall -Wextra -O0 -fPIC -shared \
-o "$OUT/fold-O0.so" ../examples/fold.c
"$NDK_BIN/aarch64-linux-android21-clang" -Wall -Wextra -O2 -fPIC -shared \
-o "$OUT/fold-O2.so" ../examples/fold.c
"$NDK_BIN/llvm-strings" -n 4 "$OUT/fold-O0.so" | grep xbin
"$NDK_BIN/llvm-strings" -n 4 "$OUT/fold-O2.so" | grep xbin
The file XOR-encrypts /system/xbin/su at compile time and decodes it at runtime, in a decoder
explicitly marked noinline.
⏸ STOP 6¶
- Which build leaks the plaintext?
noinlinewas requested. Was it honoured? How would you check?
Answer
**1.** The `-O2` build leaks it. The `-O0` build, from **identical source**, does not. Higher optimisation made the secret more visible. **2.** It was honoured — and it bought nothing. Check by looking at both the section and the symbol:"$NDK_BIN/llvm-objdump" -s -j .rodata "$OUT/fold-O2.so"
# -> 2f737973 74656d2f 7862696e 2f737500 /system/xbin/su.
# the PLAINTEXT, and the ciphertext array is GONE
"$NDK_BIN/llvm-objdump" -d --disassemble-symbols=decode "$OUT/fold-O2.so"
# -> four instructions: adrp / ldr q0 / str q0 / ret
Part 7 — Write it up¶
Produce a short case study of your own session, in the notation used throughout this chapter:
Q <the question>
H <hypothesis>
P <what you predict if it holds>
R <what would refute it -- write this BEFORE you look>
E <the exact command>
O <the raw observation, not a conclusion>
V <SUPPORTED / REFUTED / INCONCLUSIVE>
Then audit it:
# host
python3 -B ../scripts/claim-ledger.py --strict my-lab02-writeup.md
It will fail you for: zero refutations, no device named, no vantage stated, or a count without a denominator. Fix them and re-run until it exits 0.
Success criteria¶
- You produced the four-row truth table on your own build, and can explain every cell — including why the correct key alone is not enough.
- You can state what
objdumpverification of a patch proves, in one sentence, correctly. - You can name both mechanisms for "correct patch, no effect" and give the command that separates them.
- You know why
grep 'bl.*<name>'has a false-negative mode and what to write instead. - You can explain what
noinlineconstrains and, more importantly, what it does not. - Your write-up passes
claim-ledger.py --strict.
What to carry into Lab 03¶
The technique here was check the proposition you actually care about. Everything you measured was true; the wrong conclusion came from measuring a proposition adjacent to the one you meant.
Lab 03 changes technique again: change who is asking. Same command, three different uids, three different answers — and one of them is the only one that matters.
Related¶
| Chapter | Why |
|---|---|
| 59-radare2 | Owns the short-circuit case and its truth table; §5.4 |
| 18-ARM | Owns the inlining case on physical hardware; §5.4, and the encodings |
| 58-IDA | Owns "the check that passed for the wrong reason" |
| 65-Hooking | The runtime analogue: a perfectly installed hook that changes nothing |
| 67-BypassingProtections | What to do once you know which decision to move |