Merge "[SettingsProvider] move getCallingPackage() outside of lock" into sc-dev

This commit is contained in:
Songchun Fan
2021-06-30 17:41:38 +00:00
committed by Android (Google) Code Review

View File

@@ -1145,6 +1145,7 @@ public class SettingsProvider extends ContentProvider {
} }
enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG); enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
final String callingPackage = resolveCallingPackage();
synchronized (mLock) { synchronized (mLock) {
if (isSyncDisabledConfigLocked()) { if (isSyncDisabledConfigLocked()) {
@@ -1152,7 +1153,7 @@ public class SettingsProvider extends ContentProvider {
} }
final int key = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); final int key = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
boolean success = mSettingsRegistry.setConfigSettingsLocked(key, prefix, keyValues, boolean success = mSettingsRegistry.setConfigSettingsLocked(key, prefix, keyValues,
resolveCallingPackage()); callingPackage);
return success ? SET_ALL_RESULT_SUCCESS : SET_ALL_RESULT_FAILURE; return success ? SET_ALL_RESULT_SUCCESS : SET_ALL_RESULT_FAILURE;
} }
} }
@@ -1258,6 +1259,7 @@ public class SettingsProvider extends ContentProvider {
private boolean mutateConfigSetting(String name, String value, String prefix, private boolean mutateConfigSetting(String name, String value, String prefix,
boolean makeDefault, int operation, int mode) { boolean makeDefault, int operation, int mode) {
enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG); enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
final String callingPackage = resolveCallingPackage();
// Perform the mutation. // Perform the mutation.
synchronized (mLock) { synchronized (mLock) {
@@ -1265,7 +1267,7 @@ public class SettingsProvider extends ContentProvider {
case MUTATION_OPERATION_INSERT: { case MUTATION_OPERATION_INSERT: {
return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG, return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG,
UserHandle.USER_SYSTEM, name, value, null, makeDefault, true, UserHandle.USER_SYSTEM, name, value, null, makeDefault, true,
resolveCallingPackage(), false, null, callingPackage, false, null,
/* overrideableByRestore */ false); /* overrideableByRestore */ false);
} }
@@ -1276,7 +1278,7 @@ public class SettingsProvider extends ContentProvider {
case MUTATION_OPERATION_RESET: { case MUTATION_OPERATION_RESET: {
mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG, mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG,
UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix); UserHandle.USER_SYSTEM, callingPackage, mode, null, prefix);
} return true; } return true;
} }
} }
@@ -1434,13 +1436,15 @@ public class SettingsProvider extends ContentProvider {
return false; return false;
} }
final String callingPackage = getCallingPackage();
// Perform the mutation. // Perform the mutation.
synchronized (mLock) { synchronized (mLock) {
switch (operation) { switch (operation) {
case MUTATION_OPERATION_INSERT: { case MUTATION_OPERATION_INSERT: {
return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL, return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
UserHandle.USER_SYSTEM, name, value, tag, makeDefault, UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
getCallingPackage(), forceNotify, callingPackage, forceNotify,
CRITICAL_GLOBAL_SETTINGS, overrideableByRestore); CRITICAL_GLOBAL_SETTINGS, overrideableByRestore);
} }
@@ -1452,12 +1456,12 @@ public class SettingsProvider extends ContentProvider {
case MUTATION_OPERATION_UPDATE: { case MUTATION_OPERATION_UPDATE: {
return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL, return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
UserHandle.USER_SYSTEM, name, value, tag, makeDefault, UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS); callingPackage, forceNotify, CRITICAL_GLOBAL_SETTINGS);
} }
case MUTATION_OPERATION_RESET: { case MUTATION_OPERATION_RESET: {
mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL, mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); UserHandle.USER_SYSTEM, callingPackage, mode, tag);
} return true; } return true;
} }
} }
@@ -1466,11 +1470,12 @@ public class SettingsProvider extends ContentProvider {
} }
private PackageInfo getCallingPackageInfo(int userId) { private PackageInfo getCallingPackageInfo(int userId) {
final String callingPackage = getCallingPackage();
try { try {
return mPackageManager.getPackageInfo(getCallingPackage(), return mPackageManager.getPackageInfo(callingPackage,
PackageManager.GET_SIGNATURES, userId); PackageManager.GET_SIGNATURES, userId);
} catch (RemoteException e) { } catch (RemoteException e) {
throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist"); throw new IllegalStateException("Package " + callingPackage + " doesn't exist");
} }
} }
@@ -1720,13 +1725,15 @@ public class SettingsProvider extends ContentProvider {
return false; return false;
} }
final String callingPackage = getCallingPackage();
// Mutate the value. // Mutate the value.
synchronized (mLock) { synchronized (mLock) {
switch (operation) { switch (operation) {
case MUTATION_OPERATION_INSERT: { case MUTATION_OPERATION_INSERT: {
return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
owningUserId, name, value, tag, makeDefault, owningUserId, name, value, tag, makeDefault,
getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS, callingPackage, forceNotify, CRITICAL_SECURE_SETTINGS,
overrideableByRestore); overrideableByRestore);
} }
@@ -1738,12 +1745,12 @@ public class SettingsProvider extends ContentProvider {
case MUTATION_OPERATION_UPDATE: { case MUTATION_OPERATION_UPDATE: {
return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE, return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
owningUserId, name, value, tag, makeDefault, owningUserId, name, value, tag, makeDefault,
getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); callingPackage, forceNotify, CRITICAL_SECURE_SETTINGS);
} }
case MUTATION_OPERATION_RESET: { case MUTATION_OPERATION_RESET: {
mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE, mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); UserHandle.USER_SYSTEM, callingPackage, mode, tag);
} return true; } return true;
} }
} }
@@ -1840,11 +1847,12 @@ public class SettingsProvider extends ContentProvider {
private boolean mutateSystemSetting(String name, String value, int runAsUserId, int operation, private boolean mutateSystemSetting(String name, String value, int runAsUserId, int operation,
boolean overrideableByRestore) { boolean overrideableByRestore) {
final String callingPackage = getCallingPackage();
if (!hasWriteSecureSettingsPermission()) { if (!hasWriteSecureSettingsPermission()) {
// If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
// operation is allowed for the calling package through appops. // operation is allowed for the calling package through appops.
if (!Settings.checkAndNoteWriteSettingsOperation(getContext(), if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
Binder.getCallingUid(), getCallingPackage(), getCallingAttributionTag(), Binder.getCallingUid(), callingPackage, getCallingAttributionTag(),
true)) { true)) {
return false; return false;
} }
@@ -1889,7 +1897,7 @@ public class SettingsProvider extends ContentProvider {
case MUTATION_OPERATION_INSERT: { case MUTATION_OPERATION_INSERT: {
validateSystemSettingValue(name, value); validateSystemSettingValue(name, value);
return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM, return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
owningUserId, name, value, null, false, getCallingPackage(), owningUserId, name, value, null, false, callingPackage,
false, null, overrideableByRestore); false, null, overrideableByRestore);
} }
@@ -1901,7 +1909,7 @@ public class SettingsProvider extends ContentProvider {
case MUTATION_OPERATION_UPDATE: { case MUTATION_OPERATION_UPDATE: {
validateSystemSettingValue(name, value); validateSystemSettingValue(name, value);
return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM, return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
owningUserId, name, value, null, false, getCallingPackage(), owningUserId, name, value, null, false, callingPackage,
false, null); false, null);
} }
} }
@@ -2169,14 +2177,15 @@ public class SettingsProvider extends ContentProvider {
// user is a system permission and the app must be uninstalled in B and then installed as // user is a system permission and the app must be uninstalled in B and then installed as
// an Instant App that situation is not realistic or supported. // an Instant App that situation is not realistic or supported.
ApplicationInfo ai = null; ApplicationInfo ai = null;
final String callingPackage = getCallingPackage();
try { try {
ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0 ai = mPackageManager.getApplicationInfo(callingPackage, 0
, UserHandle.getCallingUserId()); , UserHandle.getCallingUserId());
} catch (RemoteException ignored) { } catch (RemoteException ignored) {
} }
if (ai == null) { if (ai == null) {
throw new IllegalStateException("Failed to lookup info for package " throw new IllegalStateException("Failed to lookup info for package "
+ getCallingPackage()); + callingPackage);
} }
return ai; return ai;
} }