Merge "Add permissions for using PlatformCompat methods" am: 418169d181 am: 3df9ed3852 am: f637859aef

Change-Id: I80598339c58679e348a66b7f967eb6dece1d7642
This commit is contained in:
Automerger Merge Worker
2020-01-28 12:35:19 +00:00
5 changed files with 62 additions and 1 deletions

View File

@@ -4856,6 +4856,19 @@
<permission android:name="android.permission.ACCESS_SHARED_LIBRARIES"
android:protectionLevel="signature|installer" />
<!-- Allows an app to log compat change usage.
@hide <p>Not for use by third-party applications.</p> -->
<permission android:name="android.permission.LOG_COMPAT_CHANGE"
android:protectionLevel="signature" />
<!-- Allows an app to read compat change config.
@hide <p>Not for use by third-party applications.</p> -->
<permission android:name="android.permission.READ_COMPAT_CHANGE_CONFIG"
android:protectionLevel="signature" />
<!-- Allows an app to override compat change config.
@hide <p>Not for use by third-party applications.</p> -->
<permission android:name="android.permission.OVERRIDE_COMPAT_CHANGE_CONFIG"
android:protectionLevel="signature" />
<!-- Allows input events to be monitored. Very dangerous! @hide -->
<permission android:name="android.permission.MONITOR_INPUT"
android:protectionLevel="signature" />

View File

@@ -16,6 +16,11 @@
package com.android.server.compat;
import static android.Manifest.permission.LOG_COMPAT_CHANGE;
import static android.Manifest.permission.OVERRIDE_COMPAT_CHANGE_CONFIG;
import static android.Manifest.permission.READ_COMPAT_CHANGE_CONFIG;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import android.app.ActivityManager;
import android.app.IActivityManager;
import android.content.Context;
@@ -68,12 +73,14 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public void reportChange(long changeId, ApplicationInfo appInfo) {
checkCompatChangeLogPermission();
reportChange(changeId, appInfo.uid,
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
}
@Override
public void reportChangeByPackageName(long changeId, String packageName, int userId) {
checkCompatChangeLogPermission();
ApplicationInfo appInfo = getApplicationInfo(packageName, userId);
if (appInfo == null) {
return;
@@ -83,11 +90,13 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public void reportChangeByUid(long changeId, int uid) {
checkCompatChangeLogPermission();
reportChange(changeId, uid, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
}
@Override
public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) {
checkCompatChangeReadPermission();
if (mCompatConfig.isChangeEnabled(changeId, appInfo)) {
reportChange(changeId, appInfo.uid,
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED);
@@ -100,6 +109,7 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public boolean isChangeEnabledByPackageName(long changeId, String packageName, int userId) {
checkCompatChangeReadPermission();
ApplicationInfo appInfo = getApplicationInfo(packageName, userId);
if (appInfo == null) {
return true;
@@ -109,6 +119,7 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public boolean isChangeEnabledByUid(long changeId, int uid) {
checkCompatChangeReadPermission();
String[] packages = mContext.getPackageManager().getPackagesForUid(uid);
if (packages == null || packages.length == 0) {
return true;
@@ -141,6 +152,7 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public void setOverrides(CompatibilityChangeConfig overrides, String packageName)
throws RemoteException, SecurityException {
checkCompatChangeOverridePermission();
mCompatConfig.addOverrides(overrides, packageName);
killPackage(packageName);
}
@@ -148,11 +160,13 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public void setOverridesForTest(CompatibilityChangeConfig overrides, String packageName)
throws RemoteException, SecurityException {
checkCompatChangeOverridePermission();
mCompatConfig.addOverrides(overrides, packageName);
}
@Override
public void clearOverrides(String packageName) throws RemoteException, SecurityException {
checkCompatChangeOverridePermission();
mCompatConfig.removePackageOverrides(packageName);
killPackage(packageName);
}
@@ -160,12 +174,14 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public void clearOverridesForTest(String packageName)
throws RemoteException, SecurityException {
checkCompatChangeOverridePermission();
mCompatConfig.removePackageOverrides(packageName);
}
@Override
public boolean clearOverride(long changeId, String packageName)
throws RemoteException, SecurityException {
checkCompatChangeOverridePermission();
boolean existed = mCompatConfig.removeOverride(changeId, packageName);
killPackage(packageName);
return existed;
@@ -173,11 +189,13 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
public CompatibilityChangeConfig getAppConfig(ApplicationInfo appInfo) {
checkCompatChangeReadPermission();
return mCompatConfig.getAppConfig(appInfo);
}
@Override
public CompatibilityChangeInfo[] listAllChanges() {
checkCompatChangeReadPermission();
return mCompatConfig.dumpChanges();
}
@@ -216,6 +234,7 @@ public class PlatformCompat extends IPlatformCompat.Stub {
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
checkCompatChangeReadPermission();
if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, "platform_compat", pw)) return;
mCompatConfig.dumpConfig(pw);
}
@@ -273,4 +292,25 @@ public class PlatformCompat extends IPlatformCompat.Stub {
Binder.restoreCallingIdentity(identity);
}
}
private void checkCompatChangeLogPermission() throws SecurityException {
if (mContext.checkCallingOrSelfPermission(LOG_COMPAT_CHANGE)
!= PERMISSION_GRANTED) {
throw new SecurityException("Cannot log compat change usage");
}
}
private void checkCompatChangeReadPermission() throws SecurityException {
if (mContext.checkCallingOrSelfPermission(READ_COMPAT_CHANGE_CONFIG)
!= PERMISSION_GRANTED) {
throw new SecurityException("Cannot read compat change");
}
}
private void checkCompatChangeOverridePermission() throws SecurityException {
if (mContext.checkCallingOrSelfPermission(OVERRIDE_COMPAT_CHANGE_CONFIG)
!= PERMISSION_GRANTED) {
throw new SecurityException("Cannot override compat change");
}
}
}

View File

@@ -66,6 +66,8 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.SUSPEND_APPS"/>
<uses-permission android:name="android.permission.CONTROL_KEYGUARD"/>
<uses-permission android:name="android.permission.READ_COMPAT_CHANGE_CONFIG"/>
<uses-permission android:name="android.permission.LOG_COMPAT_CHANGE"/>
<uses-permission android:name="android.permission.MANAGE_BIND_INSTANT_SERVICE"/>
<uses-permission android:name="android.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS" />
<uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />

View File

@@ -18,7 +18,6 @@ android_test {
name: "PlatformCompatGating",
// Only compile source java files in this apk.
srcs: ["src/**/*.java"],
certificate: "platform",
libs: [
"android.test.runner",
"android.test.base",

View File

@@ -16,7 +16,9 @@
package android.compat.testing;
import android.Manifest;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.compat.Compatibility;
import android.compat.Compatibility.ChangeConfig;
import android.content.Context;
@@ -83,12 +85,16 @@ public class PlatformCompatChangeRule extends CoreCompatChangeRule {
@Override
public void evaluate() throws Throwable {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
UiAutomation uiAutomation = instrumentation.getUiAutomation();
String packageName = instrumentation.getTargetContext().getPackageName();
IPlatformCompat platformCompat = IPlatformCompat.Stub
.asInterface(ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE));
if (platformCompat == null) {
throw new IllegalStateException("Could not get IPlatformCompat service!");
}
uiAutomation.adoptShellPermissionIdentity(
Manifest.permission.READ_COMPAT_CHANGE_CONFIG,
Manifest.permission.OVERRIDE_COMPAT_CHANGE_CONFIG);
Compatibility.setOverrides(mConfig);
try {
platformCompat.setOverridesForTest(new CompatibilityChangeConfig(mConfig),
@@ -101,6 +107,7 @@ public class PlatformCompatChangeRule extends CoreCompatChangeRule {
} catch (RemoteException e) {
throw new RuntimeException("Could not call IPlatformCompat binder method!", e);
} finally {
uiAutomation.dropShellPermissionIdentity();
Compatibility.clearOverrides();
}
}