I want to check if another app has been granted a "dangerous" or "system" level permission.
I have tried loading another app's context and calling packageContext.checkCallingPermission(permission)
. However, the documentation says it returns
PERMISSION_GRANTED if the calling pid/uid is allowed that permission, or PERMISSION_DENIED if it is not.
Is it possible to check if another app has been granted a permission?
Here is my attempt (I wrote it before realizing it checks the calling pid/uid and doesn't seem to consider the context):
void checkAllGrantedPermissions(Context context) {
PackageManager pm = context.getPackageManager();
// get all installed apps with info about what permissions they requested.
List<PackageInfo> packageInfos = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
// Get the hidden method PermissionInfo#protectionToString(int) so we can log info about the requested permission
Method protectionToString;
try {
protectionToString = PermissionInfo.class.getDeclaredMethod("protectionToString", int.class);
if (!protectionToString.isAccessible()) protectionToString.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
// loop through all installed apps
for (PackageInfo packageInfo : packageInfos) {
if (packageInfo.requestedPermissions == null) {
// No permissions are requested in the AndroidManifest
continue;
}
String appName = packageInfo.applicationInfo.loadLabel(pm).toString();
String packageName = packageInfo.packageName;
// loop through all requested permissions in the AndroidManifest
for (String permission : packageInfo.requestedPermissions) {
PermissionInfo permissionInfo;
try {
permissionInfo = pm.getPermissionInfo(permission, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.i(TAG, String.format("unknown permission '%s' found in '%s'", permission, packageName));
continue;
}
// convert the protectionLevel to a string (not necessary, but useful info)
String protLevel;
try {
protLevel = (String) protectionToString.invoke(null, permissionInfo.protectionLevel);
} catch (Exception ignored) {
protLevel = "????";
}
// Create the package's context to check if the package has the requested permission
Context packageContext;
try {
packageContext = context.createPackageContext(packageName, 0);
} catch (PackageManager.NameNotFoundException wtf) {
continue;
}
int ret = packageContext.checkCallingPermission(permission);
if (ret == PackageManager.PERMISSION_DENIED) {
Log.i(TAG, String.format("%s [%s] is denied permission %s (%s)",
appName, packageName, permission, protLevel));
} else {
Log.i(TAG, String.format("%s [%s] has granted permission %s (%s)",
appName, packageName, permission, protLevel));
}
}
}
}
Is it possible to check if another app has been granted a permission?
Yep. You can retrieve the flags for each <uses-permission>
tag in the Manifest for a given package using PackageInfo.requestedPermissionsFlags
then compare those flags using a bitwise operation with PackageInfo.REQUESTED_PERMISSION_GRANTED
.
I want to check if another app has been granted a "dangerous" or "system" level permission.
You can perform this check using PackageManager.getPermissionInfo
then compare PermissionInfo.protectionLevel
to one of PermissionInfo.PROTECTION_DANGEROUS
or PermissionInfo.PROTECTION_SIGNATURE
.
For example:
final PackageManager pm = getPackageManager();
// Loop each package requesting <manifest> permissions
for (final PackageInfo pi : pm.getInstalledPackages(GET_PERMISSIONS)) {
final String[] requestedPermissions = pi.requestedPermissions;
if (requestedPermissions == null) {
// No permissions defined in <manifest>
continue;
}
// Loop each <uses-permission> tag to retrieve the permission flag
for (int i = 0, len = requestedPermissions.length; i < len; i++) {
final String requestedPerm = requestedPermissions[i];
// Retrieve the protection level for each requested permission
int protLevel;
try {
protLevel = pm.getPermissionInfo(requestedPerm, 0).protectionLevel;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Unknown permission: " + requestedPerm, e);
continue;
}
final boolean system = protLevel == PROTECTION_SIGNATURE;
final boolean dangerous = protLevel == PROTECTION_DANGEROUS;
final boolean granted = (pi.requestedPermissionsFlags[i]
& REQUESTED_PERMISSION_GRANTED) != 0;
}
}
For more information check out:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With