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

Commit 97fe08bc authored by Sumedh Sen's avatar Sumedh Sen Committed by Automerger Merge Worker
Browse files

Merge "Fix NullPointerException in PIA when installing from Files app" into...

Merge "Fix NullPointerException in PIA when installing from Files app" into udc-dev am: 94e474ca am: a74e5a38

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/22953227



Change-Id: Iabe78d3cb289e8c925b61e13fc6d18f5b41d2052
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents ac3cce48 a74e5a38
Loading
Loading
Loading
Loading
+31 −43
Original line number Diff line number Diff line
@@ -77,7 +77,21 @@ public class InstallStart extends Activity {
        }

        final ApplicationInfo sourceInfo = getSourceInfo(callingPackage);
        final int originatingUid = getOriginatingUid(sourceInfo);
        // Uid of the source package, coming from ActivityManager
        int callingUid = getLaunchedFromUid();
        if (callingUid == Process.INVALID_UID) {
            // Cannot reach ActivityManager. Aborting install.
            Log.e(LOG_TAG, "Could not determine the launching uid.");
        }
        // Uid of the source package, with a preference to uid from ApplicationInfo
        final int originatingUid = sourceInfo != null ? sourceInfo.uid : callingUid;

        if (callingUid == Process.INVALID_UID && sourceInfo == null) {
            mAbortInstall = true;
        }

        boolean isDocumentsManager = checkPermission(Manifest.permission.MANAGE_DOCUMENTS,
                -1, callingUid) == PackageManager.PERMISSION_GRANTED;
        boolean isTrustedSource = false;
        if (sourceInfo != null && sourceInfo.isPrivilegedApp()) {
            isTrustedSource = intent.getBooleanExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, false) || (
@@ -86,7 +100,8 @@ public class InstallStart extends Activity {
                            == PackageManager.PERMISSION_GRANTED);
        }

        if (!isTrustedSource && originatingUid != Process.INVALID_UID) {
        if (!isTrustedSource && !isSystemDownloadsProvider(callingUid) && !isDocumentsManager
                && originatingUid != Process.INVALID_UID) {
            final int targetSdkVersion = getMaxTargetSdkVersionForUid(this, originatingUid);
            if (targetSdkVersion < 0) {
                Log.w(LOG_TAG, "Cannot get target sdk version for uid " + originatingUid);
@@ -144,7 +159,7 @@ public class InstallStart extends Activity {

            if (packageUri != null
                    && packageUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
                    && canPackageQuery(originatingUid, packageUri)) {
                    && canPackageQuery(callingUid, packageUri)) {
                // [IMPORTANT] This path is deprecated, but should still work. Only necessary
                // features should be added.

@@ -213,41 +228,6 @@ public class InstallStart extends Activity {
        return null;
    }

    /**
     * Get the originating uid if possible, or {@link Process#INVALID_UID} if not available
     *
     * @param sourceInfo The source of this installation
     * @return The UID of the installation source or INVALID_UID
     */
    private int getOriginatingUid(@Nullable ApplicationInfo sourceInfo) {
        // The originating uid from the intent. We only trust/use this if it comes from either
        // the document manager app or the downloads provider
        final int uidFromIntent = getIntent().getIntExtra(Intent.EXTRA_ORIGINATING_UID,
                Process.INVALID_UID);

        final int callingUid;
        if (sourceInfo != null) {
            callingUid = sourceInfo.uid;
        } else {
            callingUid = getLaunchedFromUid();
            if (callingUid == Process.INVALID_UID) {
                // Cannot reach ActivityManager. Aborting install.
                Log.e(LOG_TAG, "Could not determine the launching uid.");
                mAbortInstall = true;
                return Process.INVALID_UID;
            }
        }
        if (checkPermission(Manifest.permission.MANAGE_DOCUMENTS, -1, callingUid)
                == PackageManager.PERMISSION_GRANTED) {
            return uidFromIntent;
        }
        if (isSystemDownloadsProvider(callingUid)) {
            return uidFromIntent;
        }
        // We don't trust uid from the intent. Use the calling uid instead.
        return callingUid;
    }

    private boolean isSystemDownloadsProvider(int uid) {
        final ProviderInfo downloadProviderPackage = getPackageManager().resolveContentProvider(
                DOWNLOADS_AUTHORITY, 0);
@@ -261,8 +241,7 @@ public class InstallStart extends Activity {
    }

    @NonNull
    private boolean canPackageQuery(int originatingUid, Uri packageUri) {
        String callingPackage = mPackageManager.getPackagesForUid(originatingUid)[0];
    private boolean canPackageQuery(int callingUid, Uri packageUri) {
        ProviderInfo info = mPackageManager.resolveContentProvider(packageUri.getAuthority(),
                PackageManager.ComponentInfoFlags.of(0));
        if (info == null) {
@@ -270,11 +249,20 @@ public class InstallStart extends Activity {
        }
        String targetPackage = info.packageName;

        String[] callingPackages = mPackageManager.getPackagesForUid(callingUid);
        if (callingPackages == null) {
            return false;
        }
        for (String callingPackage: callingPackages) {
            try {
            return mPackageManager.canPackageQuery(callingPackage, targetPackage);
                if (mPackageManager.canPackageQuery(callingPackage, targetPackage)) {
                    return true;
                }
            } catch (PackageManager.NameNotFoundException e) {
            return false;
                // no-op
            }
        }
        return false;
    }

    private boolean isCallerSessionOwner(int originatingUid, int sessionId) {