Do not allow ephemeral apps to access ShortcutManager

Bug: 34178279
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest1 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest2 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest3 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest4 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest5 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest6 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest7 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest8 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest9 -w com.android.frameworks.servicestests
Test: adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest10 -w com.android.frameworks.servicestests

Change-Id: I5672f15705a1bf6568a8df58b66e48c802c11852
This commit is contained in:
Makoto Onuki
2017-01-23 11:37:45 -08:00
parent 22040a537a
commit 66e4a2b801
3 changed files with 46 additions and 3 deletions

View File

@@ -1529,10 +1529,11 @@ public class ShortcutService extends IShortcutService.Stub {
if (UserHandle.getUserId(callingUid) != userId) {
throw new SecurityException("Invalid user-ID");
}
if (injectGetPackageUid(packageName, userId) == callingUid) {
return; // Caller is valid.
if (injectGetPackageUid(packageName, userId) != callingUid) {
throw new SecurityException("Calling package name mismatch");
}
throw new SecurityException("Calling package name mismatch");
Preconditions.checkState(!isEphemeralApp(packageName, userId),
"Ephemeral apps can't use ShortcutManager");
}
// Overridden in unit tests to execute r synchronously.
@@ -3073,6 +3074,10 @@ public class ShortcutService extends IShortcutService.Stub {
return (ai != null) && (ai.flags & ApplicationInfo.FLAG_INSTALLED) != 0;
}
private static boolean isEphemeralApp(@Nullable ApplicationInfo ai) {
return (ai != null) && ai.isEphemeralApp();
}
private static boolean isInstalled(@Nullable PackageInfo pi) {
return (pi != null) && isInstalled(pi.applicationInfo);
}
@@ -3097,6 +3102,10 @@ public class ShortcutService extends IShortcutService.Stub {
return getApplicationInfo(packageName, userId) != null;
}
boolean isEphemeralApp(String packageName, int userId) {
return isEphemeralApp(getApplicationInfo(packageName, userId));
}
@Nullable
XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
return activityInfo.loadXmlMetaData(mContext.getPackageManager(), key);

View File

@@ -566,6 +566,7 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
protected Map<String, PackageInfo> mInjectedPackages;
protected Set<PackageWithUser> mUninstalledPackages;
protected Set<PackageWithUser> mEphemeralPackages;
protected Set<String> mSystemPackages;
protected PackageManager mMockPackageManager;
@@ -731,6 +732,7 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
mUninstalledPackages = new HashSet<>();
mSystemPackages = new HashSet<>();
mEphemeralPackages = new HashSet<>();
mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
@@ -1034,6 +1036,9 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
}
if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_EPHEMERAL;
}
if (mSystemPackages.contains(packageName)) {
ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
}

View File

@@ -46,6 +46,7 @@ import android.test.suitebuilder.annotation.SmallTest;
import com.android.frameworks.servicestests.R;
import com.android.server.pm.ShortcutService.ConfigConstants;
import com.android.server.pm.ShortcutUser.PackageWithUser;
import java.io.File;
import java.io.FileWriter;
@@ -2037,4 +2038,32 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest {
assertFalse(mService.isUserUnlockedL(USER_0));
assertFalse(mService.isUserUnlockedL(USER_10));
}
public void testEphemeralApp() {
mRunningUsers.put(USER_10, true); // this test needs user 10.
runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
assertWith(mManager.getDynamicShortcuts()).isEmpty();
});
runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
assertWith(mManager.getDynamicShortcuts()).isEmpty();
});
runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
assertWith(mManager.getDynamicShortcuts()).isEmpty();
});
// Make package 1 ephemeral.
mEphemeralPackages.add(PackageWithUser.of(USER_0, CALLING_PACKAGE_1));
runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
assertExpectException(IllegalStateException.class, "Ephemeral apps", () -> {
mManager.getDynamicShortcuts();
});
});
runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
assertWith(mManager.getDynamicShortcuts()).isEmpty();
});
runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
assertWith(mManager.getDynamicShortcuts()).isEmpty();
});
}
}