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

Commit 1cd3519b authored by Songchun Fan's avatar Songchun Fan
Browse files

[pm] remove package name from codePath

Initially, the package name is kept in the code path of the app, for the
purpose of easy debugging. However, ever since we introduced two-level
code path to hide the package name in the mount table, the existance of
the package name in the code path is no longer very useful. To debug the
code path of an app, the best way is to use `adb shell pm path
[package_name]` instead of `ls -lR /data/app`.

In this CL, we remove the package name from the code path. The goal is
to remove the possibility of leaking package name from the code path, if
we decide to mount the entire code path to somewhere else. Since we are
not changing the structure of the code path, this small change should be
a no-op to the rest of the system.

BUG: 291212866
Test: presubmit

Change-Id: Ia11c207e80615fdcf1213d6bc664c0123eb7724e
parent c46f6012
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -1867,8 +1867,7 @@ final class InstallPackageHelper {

        final File targetDir = resolveTargetDir(request.getInstallFlags(), request.getCodeFile());
        final File beforeCodeFile = request.getCodeFile();
        final File afterCodeFile = PackageManagerServiceUtils.getNextCodePath(targetDir,
                parsedPackage.getPackageName());
        final File afterCodeFile = PackageManagerServiceUtils.getNextCodePath(targetDir);

        if (DEBUG_INSTALL) Slog.d(TAG, "Renaming " + beforeCodeFile + " to " + afterCodeFile);
        final boolean onIncremental = mPm.mIncrementalManager != null
@@ -3099,8 +3098,7 @@ final class InstallPackageHelper {
            return null;
        }
        final File dstCodePath =
                PackageManagerServiceUtils.getNextCodePath(Environment.getDataAppDirectory(null),
                        packageName);
                PackageManagerServiceUtils.getNextCodePath(Environment.getDataAppDirectory(null));
        int ret = PackageManagerServiceUtils.decompressFiles(codePath, dstCodePath, packageName);
        if (ret == PackageManager.INSTALL_SUCCEEDED) {
            ret = PackageManagerServiceUtils.extractNativeBinaries(dstCodePath, packageName);
+6 −22
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import static com.android.server.pm.PackageManagerService.COMPRESSED_EXTENSION;
import static com.android.server.pm.PackageManagerService.DEBUG_COMPRESSION;
import static com.android.server.pm.PackageManagerService.DEBUG_INTENT_MATCHING;
import static com.android.server.pm.PackageManagerService.DEBUG_PREFERRED;
import static com.android.server.pm.PackageManagerService.RANDOM_CODEPATH_PREFIX;
import static com.android.server.pm.PackageManagerService.RANDOM_DIR_PREFIX;
import static com.android.server.pm.PackageManagerService.SHELL_PACKAGE_NAME;
import static com.android.server.pm.PackageManagerService.STUB_SUFFIX;
@@ -1299,16 +1298,15 @@ public class PackageManagerServiceUtils {
    }

    /**
     * Given {@code targetDir}, returns {@code targetDir/~~[randomStrA]/[packageName]-[randomStrB].}
     * Given {@code targetDir}, returns {@code targetDir/~~[randomStrA]/[randomStrB].}
     * Makes sure that {@code targetDir/~~[randomStrA]} directory doesn't exist.
     * Notice that this method doesn't actually create any directory.
     *
     * @param targetDir Directory that is two-levels up from the result directory.
     * @param packageName Name of the package whose code files are to be installed under the result
     *                    directory.
     * @return File object for the directory that should hold the code files of {@code packageName}.
     *
     * @return File object for the directory that should hold the code files.
     */
    public static File getNextCodePath(File targetDir, String packageName) {
    public static File getNextCodePath(File targetDir) {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[16];
        File firstLevelDir;
@@ -1320,22 +1318,8 @@ public class PackageManagerServiceUtils {
        } while (firstLevelDir.exists());

        random.nextBytes(bytes);
        String dirName = packageName + RANDOM_CODEPATH_PREFIX + Base64.encodeToString(bytes,
                Base64.URL_SAFE | Base64.NO_WRAP);
        final File result = new File(firstLevelDir, dirName);
        if (DEBUG && !Objects.equals(tryParsePackageName(result.getName()), packageName)) {
            throw new RuntimeException(
                    "codepath is off: " + result.getName() + " (" + packageName + ")");
        }
        return result;
    }

    static String tryParsePackageName(@NonNull String codePath) throws IllegalArgumentException {
        int packageNameEnds = codePath.indexOf(RANDOM_CODEPATH_PREFIX);
        if (packageNameEnds == -1) {
            throw new IllegalArgumentException("Not a valid package folder name");
        }
        return codePath.substring(0, packageNameEnds);
        String dirName = Base64.encodeToString(bytes, Base64.URL_SAFE | Base64.NO_WRAP);
        return new File(firstLevelDir, dirName);
    }

    /**