Skip to content

Commit 69f819b

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Include GIDs for unenforced permissions."
2 parents 7cf4640 + b9a0701 commit 69f819b

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

core/java/android/app/ActivityManagerNative.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,15 @@ public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
10571057
return true;
10581058
}
10591059

1060+
case KILL_PROCESSES_BELOW_FOREGROUND_TRANSACTION: {
1061+
data.enforceInterface(IActivityManager.descriptor);
1062+
String reason = data.readString();
1063+
boolean res = killProcessesBelowForeground(reason);
1064+
reply.writeNoException();
1065+
reply.writeInt(res ? 1 : 0);
1066+
return true;
1067+
}
1068+
10601069
case START_RUNNING_TRANSACTION: {
10611070
data.enforceInterface(IActivityManager.descriptor);
10621071
String pkg = data.readString();
@@ -2910,6 +2919,18 @@ public boolean killPids(int[] pids, String reason, boolean secure) throws Remote
29102919
reply.recycle();
29112920
return res;
29122921
}
2922+
@Override
2923+
public boolean killProcessesBelowForeground(String reason) throws RemoteException {
2924+
Parcel data = Parcel.obtain();
2925+
Parcel reply = Parcel.obtain();
2926+
data.writeInterfaceToken(IActivityManager.descriptor);
2927+
data.writeString(reason);
2928+
mRemote.transact(KILL_PROCESSES_BELOW_FOREGROUND_TRANSACTION, data, reply, 0);
2929+
boolean res = reply.readInt() != 0;
2930+
data.recycle();
2931+
reply.recycle();
2932+
return res;
2933+
}
29132934
public void startRunning(String pkg, String cls, String action,
29142935
String indata) throws RemoteException {
29152936
Parcel data = Parcel.obtain();

core/java/android/app/IActivityManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ public void setActivityController(IActivityController watcher)
216216
public void enterSafeMode() throws RemoteException;
217217

218218
public void noteWakeupAlarm(IIntentSender sender) throws RemoteException;
219-
219+
220220
public boolean killPids(int[] pids, String reason, boolean secure) throws RemoteException;
221-
221+
public boolean killProcessesBelowForeground(String reason) throws RemoteException;
222+
222223
// Special low-level communication with activity manager.
223224
public void startRunning(String pkg, String cls, String action,
224225
String data) throws RemoteException;
@@ -573,4 +574,5 @@ private WaitResult(Parcel source) {
573574
int GET_CONTENT_PROVIDER_EXTERNAL_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+140;
574575
int REMOVE_CONTENT_PROVIDER_EXTERNAL_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+141;
575576
int GET_MY_MEMORY_STATE_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+142;
577+
int KILL_PROCESSES_BELOW_FOREGROUND_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+143;
576578
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6997,7 +6997,43 @@ public boolean killPids(int[] pids, String pReason, boolean secure) {
69976997
}
69986998
return killed;
69996999
}
7000-
7000+
7001+
@Override
7002+
public boolean killProcessesBelowForeground(String reason) {
7003+
if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7004+
throw new SecurityException("killProcessesBelowForeground() only available to system");
7005+
}
7006+
7007+
return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason);
7008+
}
7009+
7010+
private boolean killProcessesBelowAdj(int belowAdj, String reason) {
7011+
if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7012+
throw new SecurityException("killProcessesBelowAdj() only available to system");
7013+
}
7014+
7015+
boolean killed = false;
7016+
synchronized (mPidsSelfLocked) {
7017+
final int size = mPidsSelfLocked.size();
7018+
for (int i = 0; i < size; i++) {
7019+
final int pid = mPidsSelfLocked.keyAt(i);
7020+
final ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7021+
if (proc == null) continue;
7022+
7023+
final int adj = proc.setAdj;
7024+
if (adj > belowAdj && !proc.killedBackground) {
7025+
Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
7026+
EventLog.writeEvent(
7027+
EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason);
7028+
killed = true;
7029+
proc.killedBackground = true;
7030+
Process.killProcessQuiet(pid);
7031+
}
7032+
}
7033+
}
7034+
return killed;
7035+
}
7036+
70017037
public final void startRunning(String pkg, String cls, String action,
70027038
String data) {
70037039
synchronized(this) {

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,16 @@ public int[] getPackageGids(String packageName) {
15871587
if (p != null) {
15881588
final PackageSetting ps = (PackageSetting)p.mExtras;
15891589
final SharedUserSetting suid = ps.sharedUser;
1590-
return suid != null ? suid.gids : ps.gids;
1590+
int[] gids = suid != null ? suid.gids : ps.gids;
1591+
1592+
// include GIDs for any unenforced permissions
1593+
if (!isPermissionEnforcedLocked(READ_EXTERNAL_STORAGE)) {
1594+
final BasePermission basePerm = mSettings.mPermissions.get(
1595+
READ_EXTERNAL_STORAGE);
1596+
gids = appendInts(gids, basePerm.gids);
1597+
}
1598+
1599+
return gids;
15911600
}
15921601
}
15931602
// stupid thing to indicate an error.
@@ -8890,6 +8899,19 @@ public void setPermissionEnforcement(String permission, int enforcement) {
88908899
if (mSettings.mReadExternalStorageEnforcement != enforcement) {
88918900
mSettings.mReadExternalStorageEnforcement = enforcement;
88928901
mSettings.writeLPr();
8902+
8903+
// kill any non-foreground processes so we restart them and
8904+
// grant/revoke the GID.
8905+
final IActivityManager am = ActivityManagerNative.getDefault();
8906+
if (am != null) {
8907+
final long token = Binder.clearCallingIdentity();
8908+
try {
8909+
am.killProcessesBelowForeground("setPermissionEnforcement");
8910+
} catch (RemoteException e) {
8911+
} finally {
8912+
Binder.restoreCallingIdentity(token);
8913+
}
8914+
}
88938915
}
88948916
}
88958917
} else {

0 commit comments

Comments
 (0)