From dd027b3ab2087f4ccaf4e9d72472327f27789a1d Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Fri, 19 Jan 2018 17:44:46 -0800 Subject: [PATCH] Fix issue #72116995: Add permission guarding Service#startForeground Now requires permission if targeting P. Note that this is a separate permission from the existing one that is required for instant apps to use foreground services. The reason for this is that their semantics are different (the instant apps permission is associated with an app op for control over what the app is allowed, while the regular app permission is just a normal permission that is always granted and only there for auditing of apps), and there are probably going to be cases where a developer will want to use a foreground service in the full version of their app but not as an instant app. Bug: 72116995 Test: atest CtsAppTestCases Change-Id: I883c9515c307ed8e39f0bf888c4045944c8183ac --- api/current.txt | 1 + core/java/android/app/Service.java | 12 ++++------- core/java/android/os/Build.java | 8 ++++++++ core/res/AndroidManifest.xml | 9 +++++++++ core/res/res/values/strings.xml | 5 +++++ core/tests/coretests/AndroidManifest.xml | 1 + .../MtpDocumentsProvider/AndroidManifest.xml | 1 + packages/Shell/AndroidManifest.xml | 1 + .../com/android/server/am/ActiveServices.java | 20 +++++++------------ .../server/am/ActivityManagerService.java | 14 +++++++++++++ .../tests/servicestests/AndroidManifest.xml | 1 + tests/FrameworkPerf/AndroidManifest.xml | 1 + tests/OneMedia/AndroidManifest.xml | 1 + 13 files changed, 54 insertions(+), 21 deletions(-) diff --git a/api/current.txt b/api/current.txt index 01117c9c27161..21ceeda92d98d 100644 --- a/api/current.txt +++ b/api/current.txt @@ -73,6 +73,7 @@ package android { field public static final java.lang.String DUMP = "android.permission.DUMP"; field public static final java.lang.String EXPAND_STATUS_BAR = "android.permission.EXPAND_STATUS_BAR"; field public static final java.lang.String FACTORY_TEST = "android.permission.FACTORY_TEST"; + field public static final java.lang.String FOREGROUND_SERVICE = "android.permission.FOREGROUND_SERVICE"; field public static final java.lang.String GET_ACCOUNTS = "android.permission.GET_ACCOUNTS"; field public static final java.lang.String GET_ACCOUNTS_PRIVILEGED = "android.permission.GET_ACCOUNTS_PRIVILEGED"; field public static final java.lang.String GET_PACKAGE_SIZE = "android.permission.GET_PACKAGE_SIZE"; diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java index 256c47934dc54..ea0fd75bec904 100644 --- a/core/java/android/app/Service.java +++ b/core/java/android/app/Service.java @@ -471,14 +471,6 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac * {@link #onStart} and returns either {@link #START_STICKY} * or {@link #START_STICKY_COMPATIBILITY}. * - *

If you need your application to run on platform versions prior to API - * level 5, you can use the following model to handle the older {@link #onStart} - * callback in that case. The handleCommand method is implemented by - * you as appropriate: - * - * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java - * start_compatibility} - * *

Note that the system calls this on your * service's main thread. A service's main thread is the same * thread where UI operations take place for Activities running in the @@ -687,6 +679,10 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac * {@link #startService(Intent)} first to tell the system it should keep the service running, * and then use this method to tell it to keep it running harder.

* + *

Apps targeting API {@link android.os.Build.VERSION_CODES#P} or later must request + * the permission {@link android.Manifest.permission#FOREGROUND_SERVICE} in order to use + * this API.

+ * * @param id The identifier for this notification as per * {@link NotificationManager#notify(int, Notification) * NotificationManager.notify(int, Notification)}; must not be 0. diff --git a/core/java/android/os/Build.java b/core/java/android/os/Build.java index 6d8831bccdbac..df6ce8e85601b 100644 --- a/core/java/android/os/Build.java +++ b/core/java/android/os/Build.java @@ -896,6 +896,14 @@ public class Build { /** * P. + * + *

Applications targeting this or a later release will get these + * new changes in behavior:

+ * */ public static final int P = CUR_DEVELOPMENT; // STOPSHIP Replace with the real version. } diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 5e12e7e108849..6c2db76478417 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -3827,6 +3827,15 @@ + + + Allows the app to make parts of itself persistent in memory. This can limit memory available to other apps slowing down the TV. Allows the app to make parts of itself persistent in memory. This can limit memory available to other apps slowing down the phone. + + run foreground service + + Allows the app to make use of foreground services. + measure app storage space diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml index 7d5c60aa292b2..53c22f624e8cb 100644 --- a/core/tests/coretests/AndroidManifest.xml +++ b/core/tests/coretests/AndroidManifest.xml @@ -51,6 +51,7 @@ + diff --git a/packages/MtpDocumentsProvider/AndroidManifest.xml b/packages/MtpDocumentsProvider/AndroidManifest.xml index 8d79f62f21d7e..c0a59b3badbfa 100644 --- a/packages/MtpDocumentsProvider/AndroidManifest.xml +++ b/packages/MtpDocumentsProvider/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.android.mtp" android:sharedUserId="android.media"> + + diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 0c6746e15988e..26f83f560c5c9 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -1043,20 +1043,14 @@ public final class ActiveServices { throw new SecurityException("Instant app " + r.appInfo.packageName + " does not have permission to create foreground services"); default: - try { - if (AppGlobals.getPackageManager().checkPermission( - android.Manifest.permission.INSTANT_APP_FOREGROUND_SERVICE, - r.appInfo.packageName, UserHandle.getUserId(r.appInfo.uid)) - != PackageManager.PERMISSION_GRANTED) { - throw new SecurityException("Instant app " + r.appInfo.packageName - + " does not have permission to create foreground" - + "services"); - } - } catch (RemoteException e) { - throw new SecurityException("Failed to check instant app permission." , - e); - } + mAm.enforcePermission( + android.Manifest.permission.INSTANT_APP_FOREGROUND_SERVICE, + r.app.pid, r.appInfo.uid, "startForeground"); } + } else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.P) { + mAm.enforcePermission( + android.Manifest.permission.FOREGROUND_SERVICE, + r.app.pid, r.appInfo.uid, "startForeground"); } if (r.fgRequired) { if (DEBUG_SERVICE || DEBUG_BACKGROUND_CHECK) { diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 76564537ca68f..3c058fc492fbe 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -8970,6 +8970,20 @@ public class ActivityManagerService extends IActivityManager.Stub throw new SecurityException(msg); } + /** + * This can be called with or without the global lock held. + */ + void enforcePermission(String permission, int pid, int uid, String func) { + if (checkPermission(permission, pid, uid) == PackageManager.PERMISSION_GRANTED) { + return; + } + + String msg = "Permission Denial: " + func + " from pid=" + pid + ", uid=" + uid + + " requires " + permission; + Slog.w(TAG, msg); + throw new SecurityException(msg); + } + /** * This can be called with or without the global lock held. */ diff --git a/services/tests/servicestests/AndroidManifest.xml b/services/tests/servicestests/AndroidManifest.xml index 568d7a733a190..372b8ccbdd7b8 100644 --- a/services/tests/servicestests/AndroidManifest.xml +++ b/services/tests/servicestests/AndroidManifest.xml @@ -61,6 +61,7 @@ + + diff --git a/tests/OneMedia/AndroidManifest.xml b/tests/OneMedia/AndroidManifest.xml index c6824ecea976e..8697f1b085bfa 100644 --- a/tests/OneMedia/AndroidManifest.xml +++ b/tests/OneMedia/AndroidManifest.xml @@ -5,6 +5,7 @@ android:versionName="1.0" > +