Clear caller identity in doesPackageHaveCallingUid helper method so it can work across users.

When this helper method is called on a binder thread it checks the caller's permission instead of just using the system service privileges to make the call, resulting in erroneous SecurityExceptions.

Bug: 182832208
Bug: 198860541
Test: CTS/unit tests
Change-Id: I2c71109f2556a47b8e44150565800528496173f5
This commit is contained in:
Kurt Dresner
2021-09-14 22:19:44 +00:00
parent 9260cf2831
commit c8c2c0d005

View File

@@ -1167,11 +1167,16 @@ final class UiModeManagerService extends SystemService {
}
private boolean doesPackageHaveCallingUid(@NonNull String packageName) {
int callingUid = mInjector.getCallingUid();
int callingUserId = UserHandle.getUserId(callingUid);
final long ident = Binder.clearCallingIdentity();
try {
return getContext().getPackageManager().getPackageUidAsUser(packageName,
UserHandle.getCallingUserId()) == mInjector.getCallingUid();
callingUserId) == callingUid;
} catch (PackageManager.NameNotFoundException e) {
return false;
} finally {
Binder.restoreCallingIdentity(ident);
}
}