Lab 01 — Read the task graph¶
Difficulty: Beginner Time: 45–70 minutes You will need: an Android device or emulator with
adb, the two apps from../examples/, and a terminal.
Why this lab exists¶
Almost everything written about launch modes is written from the documentation. The documentation is correct and routinely misread, and the misreadings are systematic — three of them appear in nearly every blog post on the subject:
- that
singleTopmeans "only one instance ever exists"; - that
singleTaskalways creates a new task; - that an Activity always ends up in its own app's task.
All three are wrong, and all three can be disproved in twenty minutes with a device and two commands. This lab does not ask you to believe README §3.4; it asks you to reproduce it.
By the end you will be able to look at any dumpsys activity activities output and say what launch
mode each Activity has without opening its manifest.
Objectives¶
- Read the four fields in
dumpsys activity activitiesthat matter, and ignore the other few hundred lines. - Derive
standard,singleTop,singleTaskandsingleInstancebehaviour from evidence. - Demonstrate that
taskAffinitysplits tasks within one app. - Observe a mixed-package task — two applications in one task — and understand what created it.
- Record the affinity-namespacing verdict for your device.
Setup¶
# host — build both apps out of tree and install them
cd 30-Activities/examples
./build.sh --install
If the install fails with INSTALL_FAILED_USER_RESTRICTED, that is an OEM policy, not a broken APK.
build.sh will automatically try the root path (su -c 'pm install -r ...'). If neither works, see
README §7.8 — you need a device where you can enable "Install via USB", or one you
can root.
Confirm both are present and note their uids; you will need them:
# host
adb shell "pm list packages | grep ch30"
adb shell "dumpsys package com.pentest.ch30.victim | grep userId=" | head -1
adb shell "dumpsys package com.pentest.ch30.attacker | grep userId=" | head -1
Set up two conveniences:
# host — a short alias for the only dumpsys view you need
tasks() {
adb shell "dumpsys activity activities" \
| grep -E "^\s+\* Task\{|Hist #|taskAffinity=|launchMode=" \
| grep -v "^ \* Task"
}
# host — the app's own view
logs() { adb shell "logcat -d -s CH30:I"; }
Start from a clean slate before every exercise:
# host
adb shell "am force-stop com.pentest.ch30.victim; am force-stop com.pentest.ch30.attacker"
adb shell "input keyevent KEYCODE_HOME"
adb shell logcat -c
Part 1 — Learning to read the dump¶
Step 1.1 — Look at one task¶
# host
adb shell "am start -n com.pentest.ch30.victim/.MainActivity"
adb shell "dumpsys activity activities" | grep -A25 "ch30.victim"
You will get a great deal of output. Find these four things and ignore everything else:
| Line | What it tells you |
|---|---|
* Task{<hash> #<id> ... A=<affinity> ... sz=<n>} |
a task, its id, its affinity, how many Activities it holds |
* Hist #<n>: ActivityRecord{... pkg/cls t<taskid>} |
one Activity; #0 is the root, higher numbers are higher in the stack |
taskAffinity= on a Hist block |
that Activity's effective affinity |
launchMode=<int> |
0 standard, 1 singleTop, 2 singleTask, 3 singleInstance, 4 singleInstancePerTask |
Question 1. What is the task id? What is the affinity string? Does the affinity string look like
com.pentest.ch30.victim, or like<some number>:com.pentest.ch30.victim?
Step 1.2 — Record the namespacing verdict¶
# host
cd 30-Activities
python3 -B scripts/task-affinity-map.py --check
Write the verdict down. It determines the answer to Lab 02 and it is the single most useful fact about your device for this chapter.
On the device this chapter was written against (Redmi M2101K6P, Android 11 / API 30) it reads:
verdict : NAMESPACED (17 of 17 affinity strings carry a uid prefix)
Question 2. What is your device's release, API level, and verdict? Write all three down together — a verdict without a device is worthless.
Step 1.3 — Understand why the dump looks duplicated¶
# host
adb shell "dumpsys activity activities" | grep -cE "\* Task\{"
adb shell "dumpsys activity activities" | grep -E "\* Task\{" | sort -u | wc -l
The first number is roughly twice the second.
Question 3. Why? (Hint: the dump walks the display hierarchy, then prints a summary.) What does this mean for any one-liner you write that counts tasks?
Part 2 — standard¶
Step 2.1¶
# host
adb shell "am force-stop com.pentest.ch30.victim"; adb shell "input keyevent KEYCODE_HOME"
adb shell logcat -c
adb shell "am start -n com.pentest.ch30.victim/.MainActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.AlphaActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.AlphaActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.AlphaActivity"; sleep 1
tasks
logs
AlphaActivity is declared android:launchMode="standard".
Question 4. How many
Hist #entries are in the task? What issz=?Question 5. In the logcat output, compare the
instance=values for the threeAlphaActivitylines. Are they the same or different? What doesSystem.identityHashCodeactually identify — the system's record or the app's Java object? Why does the distinction matter here?Question 6. Did
onNewIntentfire even once?
Expected shape (yours will differ in ids and hashes):
* Task{... #270 ... A=<uid>:com.pentest.ch30.victim ... sz=4}
* Hist #3: ... /.AlphaActivity t270
* Hist #2: ... /.AlphaActivity t270
* Hist #1: ... /.AlphaActivity t270
* Hist #0: ... /.MainActivity t270
Part 3 — singleTop, including the half everybody forgets¶
Step 3.1 — while it is on top¶
# host
adb shell "am force-stop com.pentest.ch30.victim"; adb shell "input keyevent KEYCODE_HOME"
adb shell logcat -c
adb shell "am start -n com.pentest.ch30.victim/.MainActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.BetaActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.BetaActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.BetaActivity"; sleep 1
logs
Question 7. How many distinct
instance=values do you see forBetaActivity? How manyonNewIntentlines?
Step 3.2 — now push something on top of it¶
# host — do NOT force-stop; continue from step 3.1
adb shell "am start -n com.pentest.ch30.victim/.AlphaActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.BetaActivity"; sleep 1
tasks
logs
Question 8. Is the last
BetaActivityline anonCreateor anonNewIntent? Is itsinstance=the same as the earlier one?Question 9. How many
BetaActivityentries are now in the task? Write, in one sentence, whatsingleTopactually guarantees.Question 10. A developer sets
singleTopon a login screen so that "there is only ever one login screen". What have they actually achieved, and what has been left reachable?
Part 4 — singleTask, and the claim that is wrong¶
# host
adb shell "am force-stop com.pentest.ch30.victim"; adb shell "input keyevent KEYCODE_HOME"
adb shell logcat -c
for A in MainActivity AlphaActivity GammaActivity AlphaActivity GammaActivity; do
adb shell "am start -n com.pentest.ch30.victim/.$A"
sleep 1
done
tasks
logs
GammaActivity is android:launchMode="singleTask".
Question 11. Look at the
taskId=in every log line. DidGammaActivityland in a new task or in the same one asMainActivity? Reconcile that with the claim "singleTaskcreates a new task".Question 12. Compare
sz=after the fourth start and after the fifth. It goes down. Which Activity disappeared, and which line in the log tells you it was destroyed rather than merely stopped? (Look foronDestroy ... isFinishing=true.)Question 13. State the actual rule for when
singleTaskcreates a new task.Question 14 (security). An attacker can trigger a
singleTaskstart on an exported Activity. What can they destroy? Name a screen where destroying whatever is above it would be useful to an attacker.
Part 5 — singleInstance¶
# host — continue from Part 4, do not reset
adb shell logcat -c
adb shell "am start -n com.pentest.ch30.victim/.DeltaActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.AlphaActivity"; sleep 1
tasks
logs
Question 15. Which task did
DeltaActivitygo into? What is that task'ssz=?Question 16.
AlphaActivitywas started while Delta was on screen. Which task did it land in? What does that tell you about asingleInstancetask's willingness to accept other Activities?Question 17. Delta's task and Main's task have the same affinity string. Explain why they are nevertheless two different tasks.
Part 6 — affinity splits tasks inside one app¶
# host
adb shell "am force-stop com.pentest.ch30.victim"; adb shell "input keyevent KEYCODE_HOME"
adb shell "am start -n com.pentest.ch30.victim/.MainActivity"; sleep 1
adb shell "am start -n com.pentest.ch30.victim/.EpsilonActivity"; sleep 1
tasks
EpsilonActivity is launchMode="standard" but declares
android:taskAffinity="com.pentest.ch30.other".
Question 18. Are Main and Epsilon in the same task? Both belong to the same package and the same uid.
Question 19.
am startsuppliesFLAG_ACTIVITY_NEW_TASKfor you (see README §3.3). Explain why that is necessary for this result — what would have happened without it?Question 20. Write the rule: what does the task boundary actually follow — the package, the uid, or the affinity string?
Part 7 — a mixed-package task¶
This is the part that matters most, and it is the one you cannot do with am alone.
# host
adb shell "am force-stop com.pentest.ch30.victim; am force-stop com.pentest.ch30.attacker"
adb shell "input keyevent KEYCODE_HOME"; sleep 1
# The ATTACKER's Activity starts the VICTIM's Activity, WITHOUT FLAG_ACTIVITY_NEW_TASK.
adb shell "am start -n com.pentest.ch30.attacker/.CrossStartActivity --es target alpha --ez newtask false"
sleep 2
tasks
Question 21. How many tasks contain a
ch30Activity? How many packages are inside the task that holdsAlphaActivity?Question 22. Read the
taskAffinity=line under the victim'sAlphaActivity. Whose uid is in it? Now read the task's ownA=affinity. Whose uid is in that? Explain the discrepancy.Question 23. The user is looking at the victim's screen and presses Back. What appears?
Now the control:
# host — the identical start, WITH the flag
adb shell "am force-stop com.pentest.ch30.victim; am force-stop com.pentest.ch30.attacker"
adb shell "input keyevent KEYCODE_HOME"; sleep 1
adb shell "am start -n com.pentest.ch30.attacker/.CrossStartActivity --es target alpha --ez newtask true"
sleep 2
tasks
Question 24. How many tasks now? What single flag accounts for the entire difference?
Question 25. Run
python3 -B scripts/task-affinity-map.pyagainst the first state. What severity does it assign toMIXED-PACKAGE-TASK, and why is that higher than the severity it assigns toAFFINITY-SQUATon a namespacing device? Do you agree?
Part 8 — see it from inside the app¶
# host — requires frida-server on the device; see ../../53-Frida/README.md
frida -U -f com.pentest.ch30.victim -l ../scripts/activity-trace.js
With it attached, in another terminal:
# host
adb shell "am start -n com.pentest.ch30.victim/.BetaActivity"
adb shell "am start -n com.pentest.ch30.victim/.BetaActivity"
Question 26. The script prints an identity block on every arrival. What does
Binder.getCallingUid()return, and what doesProcess.myUid()return? Explain the result in one sentence.Question 27.
getReferrer()shows something. What is it, and which of the three identity tiers in README §3.6 does it belong to? Could a malicious app set it to anything it liked?Question 28. Compare what
dumpsystold you in Part 3 with what the Frida script tells you. Which one can see the difference between "a new Java object" and "a new activity record"?
Hints¶
Hint for Question 5
`dumpsys` prints an `ActivityRecord{Hint for Question 12
Filter for destruction specifically:adb shell "logcat -d -s CH30:I" | grep onDestroy
Hint for Question 17
Affinity determines *which* task an Activity prefers. `singleInstance` overrides the "one task per affinity" arrangement with a stronger rule: this Activity gets a task of its own regardless. Two tasks may share an affinity string; what they may not do is both hold a `singleInstance` Activity.Hint for Question 19
Without `FLAG_ACTIVITY_NEW_TASK`, a `standard` Activity started from another Activity joins the caller's task and its affinity is not consulted at all. `am` runs in the shell process, which is not an Activity, so the flag is mandatory and `am` adds it. That is why this experiment works from `am` and Part 7's first experiment does not.Hint for Question 23
Look at the `Hist #` indices. `#0` is the root and it is revealed when everything above it finishes.Success criteria¶
You have completed this lab when you can produce, as evidence:
- Your device's release, API level, and affinity-namespacing verdict, together in one line.
- A logcat capture showing three
standardstarts producing three distinctinstance=values. - A logcat capture showing
singleTopproducingonNewIntenton the same instance, and a later capture showing it producing a new instance after something was stacked above it. - A
dumpsyscapture showing twoBetaActivityentries in one task. - A pair of
dumpsyscaptures showingsz=decreasing when asingleTaskActivity was re-entered, plus theonDestroy ... isFinishing=trueline that explains it. - A
dumpsyscapture showingsingleTaskin the same task as the Activity that started it. - A
dumpsyscapture showingsingleInstancealone in its own task, and a later start routed into a different task. - A
dumpsyscapture showing two Activities of the same package in different tasks because oftaskAffinity. - A
dumpsyscapture showing two different packages in one task, and the corresponding capture withFLAG_ACTIVITY_NEW_TASKshowing two tasks. - A one-sentence written statement of what
singleTopguarantees and what it does not. - A one-sentence written statement of when
singleTaskcreates a new task.
Clean up¶
# host
cd 30-Activities/examples
./build.sh --uninstall
Where to go next¶
- Lab 02 — Task hijacking, proved or disproved uses the affinity understanding from Parts 6 and 7 directly.
- README §3.4 has the measured captures to compare yours against.
../scripts/task-affinity-map.pyautomates everything in Parts 1, 6 and 7 — read its source after doing this by hand, not before.