Use correct API to get calling package name in CredentialStorage

Activity.getCallingPackage() does not always return the package
name of the actual calling app. getLaunchedFromPackage() should
be used instead.

Bug: 389681530
Test: manual
Flag: EXEMPT bugfix
Merged-In: Ibdbc45e53f4aa46fae79fa234705b3735bfda4cd
Change-Id: Ibdbc45e53f4aa46fae79fa234705b3735bfda4cd
(cherry picked from commit 70bd3efe0674bccb0d454845d86fb2402779a7bf)
This commit is contained in:
Rubin Xu
2025-05-21 15:34:51 +01:00
parent 4f4deeaf16
commit 48e4b5e1af

View File

@@ -17,6 +17,7 @@
package com.android.settings.security; package com.android.settings.security;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager; import android.app.admin.DevicePolicyManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -322,15 +323,25 @@ public final class CredentialStorage extends FragmentActivity {
} }
} }
private String getCallingPackageName() {
try {
return ActivityManager.getService().getLaunchedFromPackage(getActivityToken());
} catch (RemoteException re) {
// Error talking to ActivityManager, just give up
return null;
}
}
/** /**
* Check that the caller is either certinstaller or Settings running in a profile of this user. * Check that the caller is either certinstaller or Settings running in a profile of this user.
*/ */
private boolean checkCallerIsCertInstallerOrSelfInProfile() { private boolean checkCallerIsCertInstallerOrSelfInProfile() {
if (TextUtils.equals("com.android.certinstaller", getCallingPackage())) { String callingPackage = getCallingPackageName();
if (TextUtils.equals("com.android.certinstaller", callingPackage)) {
// CertInstaller is allowed to install credentials if it has the same signature as // CertInstaller is allowed to install credentials if it has the same signature as
// Settings package. // Settings package.
return getPackageManager().checkSignatures( return getPackageManager().checkSignatures(
getCallingPackage(), getPackageName()) == PackageManager.SIGNATURE_MATCH; callingPackage, getPackageName()) == PackageManager.SIGNATURE_MATCH;
} }
final int launchedFromUserId; final int launchedFromUserId;