Skip to content

Commit 7a04bee

Browse files
Jing JiAndroid Build Coastguard Worker
authored andcommitted
DO NOT MERGE: Context#startInstrumentation could be started from SHELL only now.
Or, if an instrumentation starts another instrumentation and so on, and the original instrumentation is started from SHELL, allow all Context#startInstrumentation calls in this chain. Otherwise, it'll throw a SecurityException. Bug: 237766679 Test: atest CtsAppTestCases:InstrumentationTest Merged-In: Ia08f225c21a3933067d066a578ea4af9c23e7d4c Merged-In: I1b76f61c5fd6c9f7e738978592260945a606f40c Merged-In: I3ea7aa27bd776fec546908a37f667f680da9c892 Change-Id: I7ca7345b064e8e74f7037b8fa3ed45bb6423e406 (cherry picked from commit a3e618a) Merged-In: I7ca7345b064e8e74f7037b8fa3ed45bb6423e406
1 parent eec689f commit 7a04bee

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

services/core/java/com/android/server/am/ActivityManagerService.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14655,6 +14655,17 @@ public boolean startInstrumentation(ComponentName className,
1465514655
throw new SecurityException(msg);
1465614656
}
1465714657
}
14658+
if (!Build.IS_DEBUGGABLE && callingUid != ROOT_UID && callingUid != SHELL_UID
14659+
&& callingUid != SYSTEM_UID && !hasActiveInstrumentationLocked(callingPid)) {
14660+
// If it's not debug build and not called from root/shell/system uid, reject it.
14661+
final String msg = "Permission Denial: instrumentation test "
14662+
+ className + " from pid=" + callingPid + ", uid=" + callingUid
14663+
+ ", pkgName=" + getPackageNameByPid(callingPid)
14664+
+ " not allowed because it's not started from SHELL";
14665+
Slog.wtfQuiet(TAG, msg);
14666+
reportStartInstrumentationFailureLocked(watcher, className, msg);
14667+
throw new SecurityException(msg);
14668+
}
1465814669

1465914670
boolean disableHiddenApiChecks = ai.usesNonSdkApi()
1466014671
|| (flags & INSTR_FLAG_DISABLE_HIDDEN_API_CHECKS) != 0;
@@ -14877,6 +14888,29 @@ private void instrumentWithoutRestart(ActiveInstrumentation activeInstr,
1487714888
}
1487814889
}
1487914890

14891+
@GuardedBy("this")
14892+
private boolean hasActiveInstrumentationLocked(int pid) {
14893+
if (pid == 0) {
14894+
return false;
14895+
}
14896+
synchronized (mPidsSelfLocked) {
14897+
ProcessRecord process = mPidsSelfLocked.get(pid);
14898+
return process != null && process.getActiveInstrumentation() != null;
14899+
}
14900+
}
14901+
14902+
private String getPackageNameByPid(int pid) {
14903+
synchronized (mPidsSelfLocked) {
14904+
final ProcessRecord app = mPidsSelfLocked.get(pid);
14905+
14906+
if (app != null && app.info != null) {
14907+
return app.info.packageName;
14908+
}
14909+
14910+
return null;
14911+
}
14912+
}
14913+
1488014914
private boolean isCallerShell() {
1488114915
final int callingUid = Binder.getCallingUid();
1488214916
return callingUid == SHELL_UID || callingUid == ROOT_UID;

0 commit comments

Comments
 (0)