Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3982e546 authored by Zim's avatar Zim
Browse files

Refactor how zygote forked processes acquire gids

Effectively no-op refactoring in preparation to associate apps with
pass_through mount mode with the media_rw gid. Minor changes:

1. Before, if either UserHandle#get[Shared|Cache]AppGid return ERR
we would replace with the value of the already existing getUserGid
Now, we don't duplicate the getUserGid in the final result
2. Before, if the externalstorageapp also has the android_writable
mount mode (which would not happen), we would only give it the
media_rw gid. Now it can have both media_rw and sdcard_rw.

Test: m
Bug: 144914977
Change-Id: I7a4ea5cda59ffc573e41982c4051ee00abf95802
parent 069dc427
Loading
Loading
Loading
Loading
+38 −32
Original line number Diff line number Diff line
@@ -1551,6 +1551,43 @@ public final class ProcessList {
        }
    }

    private int[] computeGidsForProcess(int mountExternal, int uid, int[] permGids,
            String packageName) {
        ArrayList<Integer> gidList = new ArrayList<>(permGids.length + 5);

        final int sharedAppGid = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
        final int cacheAppGid = UserHandle.getCacheAppGid(UserHandle.getAppId(uid));
        final int userGid = UserHandle.getUserGid(UserHandle.getUserId(uid));

        // Add shared application and profile GIDs so applications can share some
        // resources like shared libraries and access user-wide resources
        for (int permGid : permGids) {
            gidList.add(permGid);
        }
        if (sharedAppGid != UserHandle.ERR_GID) {
            gidList.add(sharedAppGid);
        }
        if (cacheAppGid != UserHandle.ERR_GID) {
            gidList.add(cacheAppGid);
        }
        if (userGid != UserHandle.ERR_GID) {
            gidList.add(userGid);
        }
        if (mountExternal == Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE) {
            gidList.add(Process.SDCARD_RW_GID);
        }
        if (packageName.equals("com.android.externalstorage")) {
            // Allows access to 'unreliable' (USB OTG) volumes via SAF
            gidList.add(Process.MEDIA_RW_GID);
        }

        int[] gidArray = new int[gidList.size()];
        for (int i = 0; i < gidArray.length; i++) {
            gidArray[i] = gidList.get(i);
        }
        return gidArray;
    }

    /**
     * @return {@code true} if process start is successful, false otherwise.
     */
@@ -1625,38 +1662,7 @@ public final class ProcessList {
                    }
                }

                int numGids = 3;
                if (mountExternal == Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE
                        || app.info.packageName.equals("com.android.externalstorage")) {
                    numGids++;
                }

                /*
                 * Add shared application and profile GIDs so applications can share some
                 * resources like shared libraries and access user-wide resources
                 */
                if (ArrayUtils.isEmpty(permGids)) {
                    gids = new int[numGids];
                } else {
                    gids = new int[permGids.length + numGids];
                    System.arraycopy(permGids, 0, gids, numGids, permGids.length);
                }
                gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
                gids[1] = UserHandle.getCacheAppGid(UserHandle.getAppId(uid));
                gids[2] = UserHandle.getUserGid(UserHandle.getUserId(uid));

                if (numGids > 3) {
                    if (app.info.packageName.equals("com.android.externalstorage")) {
                        // Allows access to 'unreliable' (USB OTG) volumes via SAF
                        gids[3] = Process.MEDIA_RW_GID;
                    } else if (mountExternal == Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE) {
                        gids[3] = Process.SDCARD_RW_GID;
                    }
                }

                // Replace any invalid GIDs
                if (gids[0] == UserHandle.ERR_GID) gids[0] = gids[2];
                if (gids[1] == UserHandle.ERR_GID) gids[1] = gids[2];
                gids = computeGidsForProcess(mountExternal, uid, permGids, app.info.packageName);
            }
            app.mountMode = mountExternal;
            checkSlow(startTime, "startProcess: building args");