Skip to content

Commit e091f22

Browse files
committed
Use shared app gid for forward-locked processes
Use a shared app gid for each app across different users which allows forward-locked applications to share the same APK file. Change-Id: Ifecf51ee7865547117746f83e9733083d3dd5111
1 parent c574fd0 commit e091f22

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

core/java/android/os/Process.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,25 @@ public class Process {
151151
*/
152152
public static final int LAST_ISOLATED_UID = 99999;
153153

154+
/**
155+
* First gid for applications to share resources. Used when forward-locking
156+
* is enabled but all UserHandles need to be able to read the resources.
157+
* @hide
158+
*/
159+
public static final int FIRST_SHARED_APPLICATION_GID = 50000;
160+
161+
/**
162+
* Last gid for applications to share resources. Used when forward-locking
163+
* is enabled but all UserHandles need to be able to read the resources.
164+
* @hide
165+
*/
166+
public static final int LAST_SHARED_APPLICATION_GID = 59999;
167+
154168
/**
155169
* Defines a secondary group id for access to the bluetooth hardware.
156170
*/
157171
public static final int BLUETOOTH_GID = 2000;
158-
172+
159173
/**
160174
* Standard priority of application threads.
161175
* Use with {@link #setThreadPriority(int)} and

core/java/android/os/UserHandle.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ public static final int getAppId(int uid) {
138138
return uid % PER_USER_RANGE;
139139
}
140140

141+
/**
142+
* Returns the shared app gid for a given uid or appId.
143+
* @hide
144+
*/
145+
public static final int getSharedAppGid(int id) {
146+
return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE)
147+
- Process.FIRST_APPLICATION_UID;
148+
}
149+
141150
/**
142151
* Returns the user id of the current process
143152
* @return user id of the current process

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2008,9 +2008,10 @@ private final void startProcessLocked(ProcessRecord app,
20082008
int[] gids = null;
20092009
int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
20102010
if (!app.isolated) {
2011+
int[] permGids = null;
20112012
try {
20122013
final PackageManager pm = mContext.getPackageManager();
2013-
gids = pm.getPackageGids(app.info.packageName);
2014+
permGids = pm.getPackageGids(app.info.packageName);
20142015

20152016
if (Environment.isExternalStorageEmulated()) {
20162017
if (pm.checkPermission(
@@ -2024,6 +2025,18 @@ private final void startProcessLocked(ProcessRecord app,
20242025
} catch (PackageManager.NameNotFoundException e) {
20252026
Slog.w(TAG, "Unable to retrieve gids", e);
20262027
}
2028+
2029+
/*
2030+
* Add shared application GID so applications can share some
2031+
* resources like shared libraries
2032+
*/
2033+
if (permGids == null) {
2034+
gids = new int[1];
2035+
} else {
2036+
gids = new int[permGids.length + 1];
2037+
System.arraycopy(permGids, 0, gids, 1, permGids.length);
2038+
}
2039+
gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
20272040
}
20282041
if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
20292042
if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7285,7 +7285,7 @@ int doPostInstall(int status, int uid) {
72857285
final int groupOwner;
72867286
final String protectedFile;
72877287
if (isFwdLocked()) {
7288-
groupOwner = uid;
7288+
groupOwner = UserHandle.getSharedAppGid(uid);
72897289
protectedFile = RES_FILE_NAME;
72907290
} else {
72917291
groupOwner = -1;
@@ -7367,7 +7367,8 @@ int doPreCopy() {
73677367
int doPostCopy(int uid) {
73687368
if (isFwdLocked()) {
73697369
if (uid < Process.FIRST_APPLICATION_UID
7370-
|| !PackageHelper.fixSdPermissions(cid, uid, RES_FILE_NAME)) {
7370+
|| !PackageHelper.fixSdPermissions(cid, UserHandle.getSharedAppGid(uid),
7371+
RES_FILE_NAME)) {
73717372
Slog.e(TAG, "Failed to finalize " + cid);
73727373
PackageHelper.destroySdDir(cid);
73737374
return PackageManager.INSTALL_FAILED_CONTAINER_ERROR;

0 commit comments

Comments
 (0)