diff options
author | Roshan Pius <rpius@google.com> | 2019-09-18 17:27:41 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-09-18 17:27:41 -0700 |
commit | 2c3e6f177642a3dcd378e0a404d60ba2af2841ed (patch) | |
tree | c9b189f0a7b00deceec18dbf05d2e83607d5e9e5 | |
parent | 4764d08e86004af17e4cfd8e24ad6685b9096c5d (diff) | |
parent | 4ffcc50359495af8d1f5ad94203752a00f32aaeb (diff) |
Merge "WifiPermissionsUtil: Use getApplicationInfoAsUser for target SDK checks" into qt-qpr1-dev am: c9d6806174
am: 4ffcc50359
Change-Id: I89423b1b3b5572d4e0299730fbd5551382585de7
6 files changed, 41 insertions, 34 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 082f56b43..899c934f1 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -782,7 +782,7 @@ public class WifiServiceImpl extends BaseWifiService { * Note: Invoke mAppOps.checkPackage(uid, packageName) before to ensure correct package name. */ private boolean isTargetSdkLessThanQOrPrivileged(String packageName, int pid, int uid) { - return mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q) + return mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q, uid) || isPrivileged(pid, uid) // DO/PO apps should be able to add/modify saved networks. || isDeviceOrProfileOwner(uid) @@ -804,7 +804,8 @@ public class WifiServiceImpl extends BaseWifiService { } boolean isPrivileged = isPrivileged(Binder.getCallingPid(), Binder.getCallingUid()); if (!isPrivileged && !isDeviceOrProfileOwner(Binder.getCallingUid()) - && !mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q) + && !mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q, + Binder.getCallingUid()) && !isSystem(packageName)) { mLog.info("setWifiEnabled not allowed for uid=%") .c(Binder.getCallingUid()).flush(); @@ -2296,7 +2297,7 @@ public class WifiServiceImpl extends BaseWifiService { final int uid = Binder.getCallingUid(); if (!mWifiPermissionsUtil.checkNetworkSettingsPermission(uid) && !mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(uid)) { - if (mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q)) { + if (mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q, uid)) { return false; } throw new SecurityException(TAG + ": Permission denied"); @@ -2318,7 +2319,7 @@ public class WifiServiceImpl extends BaseWifiService { mAppOps.checkPackage(uid, packageName); if (!mWifiPermissionsUtil.checkNetworkSettingsPermission(uid) && !mWifiPermissionsUtil.checkNetworkSetupWizardPermission(uid)) { - if (mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q)) { + if (mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q, uid)) { return new ArrayList<>(); } throw new SecurityException(TAG + ": Permission denied"); diff --git a/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java b/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java index 1ac73aef8..5832ee898 100644 --- a/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java +++ b/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java @@ -1286,7 +1286,7 @@ public class WifiAwareDataPathStateManager { // Note: checks are done on the manager. This is a backup for apps which bypass the // check. if (!allowNdpResponderFromAnyOverride && !wifiPermissionsUtil.isTargetSdkLessThan( - client.getCallingPackage(), Build.VERSION_CODES.P)) { + client.getCallingPackage(), Build.VERSION_CODES.P, uid)) { if (ns.type != WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB && ns.type != WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_OOB) { Log.e(TAG, "processNetworkSpecifier: networkSpecifier=" + ns diff --git a/service/java/com/android/server/wifi/util/WifiPermissionsUtil.java b/service/java/com/android/server/wifi/util/WifiPermissionsUtil.java index 86835a27f..4eadb4fbf 100644 --- a/service/java/com/android/server/wifi/util/WifiPermissionsUtil.java +++ b/service/java/com/android/server/wifi/util/WifiPermissionsUtil.java @@ -125,10 +125,11 @@ public class WifiPermissionsUtil { /** * Checks whether than the target SDK of the package is less than the specified version code. */ - public boolean isTargetSdkLessThan(String packageName, int versionCode) { + public boolean isTargetSdkLessThan(String packageName, int versionCode, int callingUid) { long ident = Binder.clearCallingIdentity(); try { - if (mContext.getPackageManager().getApplicationInfo(packageName, 0).targetSdkVersion + if (mContext.getPackageManager().getApplicationInfoAsUser( + packageName, 0, UserHandle.getUserId(callingUid)).targetSdkVersion < versionCode) { return true; } @@ -154,7 +155,7 @@ public class WifiPermissionsUtil { */ public boolean checkCallersLocationPermission(String pkgName, int uid, boolean coarseForTargetSdkLessThanQ) { - boolean isTargetSdkLessThanQ = isTargetSdkLessThan(pkgName, Build.VERSION_CODES.Q); + boolean isTargetSdkLessThanQ = isTargetSdkLessThan(pkgName, Build.VERSION_CODES.Q, uid); String permissionType = Manifest.permission.ACCESS_FINE_LOCATION; if (coarseForTargetSdkLessThanQ && isTargetSdkLessThanQ) { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 60f14b3c3..137f881d0 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -552,7 +552,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(anyBoolean())).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -571,7 +571,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -615,7 +615,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( Process.myUid(), DeviceAdminInfo.USES_POLICY_DEVICE_OWNER)) .thenReturn(true); @@ -635,7 +635,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); @@ -653,7 +653,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -670,7 +670,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -687,7 +687,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doThrow(new SecurityException()).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); try { mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true); @@ -707,7 +707,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_IGNORED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); mWifiServiceImpl.setWifiEnabled(TEST_PACKAGE_NAME, true); @@ -739,7 +739,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(true))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(true); when(mContext.checkPermission( @@ -797,7 +797,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.isWifiToggleEnabled()).thenReturn(false); mWifiServiceImpl.checkAndStartWifi(); @@ -875,7 +875,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mDevicePolicyManagerInternal.isActiveAdminWithPolicy( Process.myUid(), DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)) .thenReturn(true); @@ -895,7 +895,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); @@ -914,7 +914,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -931,7 +931,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(false); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(false); when(mSettingsStore.handleWifiToggled(eq(false))).thenReturn(true); when(mSettingsStore.isAirplaneModeOn()).thenReturn(false); @@ -2515,7 +2515,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mContext.getPackageManager()).thenReturn(pm); when(pm.getApplicationInfo(any(), anyInt())).thenReturn(mApplicationInfo); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); when(mClientModeImpl.syncAddOrUpdatePasspointConfig(any(), any(PasspointConfiguration.class), anyInt(), eq(TEST_PACKAGE_NAME))).thenReturn( @@ -2661,7 +2661,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(false); when(mWifiPermissionsUtil.checkNetworkSetupWizardPermission(anyInt())).thenReturn(false); when(mWifiPermissionsUtil.isTargetSdkLessThan(eq(TEST_PACKAGE_NAME), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); List<PasspointConfiguration> result = mWifiServiceImpl.getPasspointConfigurations( TEST_PACKAGE_NAME); @@ -2694,7 +2694,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())).thenReturn( false); when(mWifiPermissionsUtil.isTargetSdkLessThan(isNull(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); assertFalse(mWifiServiceImpl.removePasspointConfiguration(null, null)); } @@ -3545,7 +3545,7 @@ public class WifiServiceImplTest extends WifiBaseTest { .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mClientModeImpl.syncAddOrUpdateNetwork(any(), any())).thenReturn(0); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); assertEquals(0, mWifiServiceImpl.addOrUpdateNetwork(config, TEST_PACKAGE_NAME)); @@ -3688,7 +3688,7 @@ public class WifiServiceImplTest extends WifiBaseTest { doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) .noteOp(AppOpsManager.OPSTR_CHANGE_WIFI_STATE, Process.myUid(), TEST_PACKAGE_NAME); when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), - eq(Build.VERSION_CODES.Q))).thenReturn(true); + eq(Build.VERSION_CODES.Q), anyInt())).thenReturn(true); mWifiServiceImpl.enableNetwork(TEST_NETWORK_ID, true, TEST_PACKAGE_NAME); diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java index 717dfe6dd..b803dd515 100644 --- a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java @@ -162,7 +162,8 @@ public class WifiAwareDataPathStateManagerTest extends WifiBaseTest { // by default pretend to be an old API: i.e. allow Responders configured as *ANY*. This // allows older (more extrensive) tests to run. - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(true); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(true); when(mWifiPermissionsUtil.isLocationModeEnabled()).thenReturn(true); when(mMockNativeManager.isAwareNativeAvailable()).thenReturn(true); @@ -1019,7 +1020,8 @@ public class WifiAwareDataPathStateManagerTest extends WifiBaseTest { */ @Test public void testDataPathResonderMacPassphraseNoPeerIdSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(false, false, false, true, true); } @@ -1030,7 +1032,8 @@ public class WifiAwareDataPathStateManagerTest extends WifiBaseTest { @Test public void testDataPathResonderMacOpenNoPeerIdNoPmkPassphraseSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(false, false, false, false, true); } @@ -1076,7 +1079,8 @@ public class WifiAwareDataPathStateManagerTest extends WifiBaseTest { */ @Test public void testDataPathResonderDirectNoMacPassphraseSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(true, false, false, true, true); } @@ -1086,7 +1090,8 @@ public class WifiAwareDataPathStateManagerTest extends WifiBaseTest { */ @Test public void testDataPathResonderDirectNoMacNoPmkPassphraseSuccessNonLegacy() throws Exception { - when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt())).thenReturn(false); + when(mWifiPermissionsUtil.isTargetSdkLessThan(anyString(), anyInt(), anyInt())) + .thenReturn(false); testDataPathResponderUtility(true, false, false, false, true); } @@ -1475,7 +1480,7 @@ public class WifiAwareDataPathStateManagerTest extends WifiBaseTest { InOrder inOrderM = inOrder(mAwareMetricsMock); boolean isLegacy = mWifiPermissionsUtil.isTargetSdkLessThan("anything", - Build.VERSION_CODES.P); + Build.VERSION_CODES.P, 0); if (providePmk) { when(mPermissionsWrapperMock.getUidPermission( diff --git a/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java b/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java index 97d2b9fcb..6241d210e 100644 --- a/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/util/WifiPermissionsUtilTest.java @@ -1149,7 +1149,7 @@ public class WifiPermissionsUtilTest extends WifiBaseTest { } private void setupMocks() throws Exception { - when(mMockPkgMgr.getApplicationInfo(TEST_PACKAGE_NAME, 0)) + when(mMockPkgMgr.getApplicationInfoAsUser(eq(TEST_PACKAGE_NAME), eq(0), anyInt())) .thenReturn(mMockApplInfo); when(mMockContext.getPackageManager()).thenReturn(mMockPkgMgr); when(mMockAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, mUid, TEST_PACKAGE_NAME)) |