Wi-Fi panel doesn't need to check permission

Prior to this cl, we use #getPackagesForUid()
to get a list of calling package names and
pick up 1st package name in the list as target
calling package. And then go to check the
Wi-Fi permission.

This implementation is ok for most apps without
sharing system uid. However, this may not work
if the package is set with sharing system ui.
In this case, we get a list of packages
and we don't know which one is caller. So, if we
decide to choose the 1st package as our
calling package, then it could fail to pass
permission check since that package could be not
a correct calling package.

In this cl, we skip permission check for those
packages running with system uid. So, it can resolve
Wi-Fi Panel problem since Wi-Fi panel runs
on settings process(with system uid).

Test: 1. adb shell am start -a android.settings.panel.action.WIFI
2. Verify on assistant app and system ui launcher and search app.
Bug: 240531998

Change-Id: Ia825853dde2e966e3d390cecfbe1a99f6439d31e
Merged-In: Ia825853dde2e966e3d390cecfbe1a99f6439d31e
This commit is contained in:
Tsung-Mao Fang
2022-08-11 13:18:23 +08:00
parent 9ba444a214
commit d2c372450c
4 changed files with 27 additions and 1 deletions

View File

@@ -621,4 +621,10 @@
<!-- Whether to put the apps with system UID into system component bucket or not --> <!-- Whether to put the apps with system UID into system component bucket or not -->
<bool name="config_battery_combine_system_components">false</bool> <bool name="config_battery_combine_system_components">false</bool>
<!-- An array of uid name for which packages exempt from Wi-Fi permission check. -->
<string-array name="config_exempt_wifi_permission_uid_name" translatable="false">
<item>@string/config_settingsintelligence_package_name</item>
<item>android.uid.system:1000</item>
</string-array>
</resources> </resources>

View File

@@ -98,7 +98,7 @@ public class WifiSlice implements CustomSliceable {
public Slice getSlice() { public Slice getSlice() {
// If external calling package doesn't have Wi-Fi permission. // If external calling package doesn't have Wi-Fi permission.
final boolean isPermissionGranted = final boolean isPermissionGranted =
Utils.isSettingsIntelligence(mContext) || isPermissionGranted(mContext); isCallerExemptUid(mContext) || isPermissionGranted(mContext);
final boolean isWifiEnabled = isWifiEnabled(); final boolean isWifiEnabled = isWifiEnabled();
ListBuilder listBuilder = getListBuilder(isWifiEnabled, null /* wifiSliceItem */, ListBuilder listBuilder = getListBuilder(isWifiEnabled, null /* wifiSliceItem */,
isPermissionGranted); isPermissionGranted);
@@ -139,6 +139,21 @@ public class WifiSlice implements CustomSliceable {
return listBuilder.build(); return listBuilder.build();
} }
private boolean isCallerExemptUid(Context context) {
final String[] allowedUidNames = context.getResources().getStringArray(
R.array.config_exempt_wifi_permission_uid_name);
final String uidName =
context.getPackageManager().getNameForUid(Binder.getCallingUid());
Log.d(TAG, "calling uid name : " + uidName);
for (String allowedUidName : allowedUidNames) {
if (TextUtils.equals(uidName, allowedUidName)) {
return true;
}
}
return false;
}
private static boolean isPermissionGranted(Context settingsContext) { private static boolean isPermissionGranted(Context settingsContext) {
final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid();
final String callingPackage = settingsContext.getPackageManager() final String callingPackage = settingsContext.getPackageManager()

View File

@@ -103,6 +103,7 @@ public class ContextualWifiSliceTest {
mContext.getString(R.string.config_settingsintelligence_package_name); mContext.getString(R.string.config_settingsintelligence_package_name);
ShadowBinder.setCallingUid(1); ShadowBinder.setCallingUid(1);
when(mPackageManager.getPackagesForUid(1)).thenReturn(new String[]{siPackageName}); when(mPackageManager.getPackagesForUid(1)).thenReturn(new String[]{siPackageName});
when(mPackageManager.getNameForUid(1)).thenReturn(siPackageName);
ShadowWifiSlice.setWifiPermissible(true); ShadowWifiSlice.setWifiPermissible(true);
mWifiSlice = new ContextualWifiSlice(mContext); mWifiSlice = new ContextualWifiSlice(mContext);
} }

View File

@@ -109,6 +109,7 @@ public class WifiSliceTest {
mSIPackageName = mContext.getString(R.string.config_settingsintelligence_package_name); mSIPackageName = mContext.getString(R.string.config_settingsintelligence_package_name);
ShadowBinder.setCallingUid(USER_ID); ShadowBinder.setCallingUid(USER_ID);
when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{mSIPackageName}); when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{mSIPackageName});
when(mPackageManager.getNameForUid(USER_ID)).thenReturn(mSIPackageName);
ShadowWifiSlice.setWifiPermissible(true); ShadowWifiSlice.setWifiPermissible(true);
mWifiSlice = new WifiSlice(mContext, mWifiRestriction); mWifiSlice = new WifiSlice(mContext, mWifiRestriction);
} }
@@ -116,6 +117,7 @@ public class WifiSliceTest {
@Test @Test
public void getWifiSlice_fromSIPackage_shouldHaveTitleAndToggle() { public void getWifiSlice_fromSIPackage_shouldHaveTitleAndToggle() {
when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{mSIPackageName}); when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{mSIPackageName});
when(mPackageManager.getNameForUid(USER_ID)).thenReturn(mSIPackageName);
ShadowWifiSlice.setWifiPermissible(false); ShadowWifiSlice.setWifiPermissible(false);
final Slice wifiSlice = mWifiSlice.getSlice(); final Slice wifiSlice = mWifiSlice.getSlice();
@@ -131,6 +133,7 @@ public class WifiSliceTest {
@Test @Test
public void getWifiSlice_notFromSIPackageAndWithWifiPermission_shouldHaveTitleAndToggle() { public void getWifiSlice_notFromSIPackageAndWithWifiPermission_shouldHaveTitleAndToggle() {
when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{"com.test"}); when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{"com.test"});
when(mPackageManager.getNameForUid(USER_ID)).thenReturn("com.test");
ShadowWifiSlice.setWifiPermissible(true); ShadowWifiSlice.setWifiPermissible(true);
final Slice wifiSlice = mWifiSlice.getSlice(); final Slice wifiSlice = mWifiSlice.getSlice();
@@ -145,6 +148,7 @@ public class WifiSliceTest {
@Test @Test
public void getWifiSlice_notFromSIPackageAndWithoutWifiPermission_shouldReturnNoToggle() { public void getWifiSlice_notFromSIPackageAndWithoutWifiPermission_shouldReturnNoToggle() {
when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{"com.test"}); when(mPackageManager.getPackagesForUid(USER_ID)).thenReturn(new String[]{"com.test"});
when(mPackageManager.getNameForUid(USER_ID)).thenReturn("com.test");
ShadowWifiSlice.setWifiPermissible(false); ShadowWifiSlice.setWifiPermissible(false);
final Slice wifiSlice = mWifiSlice.getSlice(); final Slice wifiSlice = mWifiSlice.getSlice();