OSDN Git Service

Removed AMS.updatePersistableUriPermission()
authorFelipe Leme <felipeal@google.com>
Wed, 14 Feb 2018 20:00:29 +0000 (12:00 -0800)
committerFelipe Leme <felipeal@google.com>
Wed, 28 Feb 2018 23:15:37 +0000 (15:15 -0800)
Bug: 72055774

Test: manual verification
Test: atest CtsAppSecurityHostTestCases:ScopedDirectoryAccessTest#testResetDoNotAskAgain,testResetGranted
Test: atest CtsAppSecurityHostTestCases:ScopedDirectoryAccessTest

Change-Id: I90cd9a79a1caa19989e00dbc1a656469bc3221f9

core/java/android/app/ActivityManager.java
core/java/android/app/IActivityManager.aidl
core/java/android/content/ContentResolver.java
core/res/AndroidManifest.xml
services/core/java/com/android/server/am/ActivityManagerService.java
services/core/java/com/android/server/am/UriPermission.java

index 03faeee..ae47a68 100644 (file)
@@ -44,7 +44,6 @@ import android.graphics.GraphicBuffer;
 import android.graphics.Matrix;
 import android.graphics.Point;
 import android.graphics.Rect;
-import android.net.Uri;
 import android.os.BatteryStats;
 import android.os.Binder;
 import android.os.Build;
@@ -2751,30 +2750,6 @@ public class ActivityManager {
     }
 
     /**
-     * Updates (grants or revokes) a persitable URI permission.
-     *
-     * @param uri URI to be granted or revoked.
-     * @param prefix if {@code false}, permission apply to this specific URI; if {@code true}, it
-     * applies to all URIs that are prefixed by this URI.
-     * @param packageName target package.
-     * @param grant if {@code true} a new permission will be granted, otherwise an existing
-     * permission will be revoked.
-     *
-     * @return whether or not the requested succeeded.
-     *
-     * @hide
-     */
-    public boolean updatePersistableUriPermission(Uri uri, boolean prefix, String packageName,
-            boolean grant) {
-        try {
-            return getService().updatePersistableUriPermission(uri, prefix, packageName, grant,
-                    UserHandle.myUserId());
-        } catch (RemoteException e) {
-            throw e.rethrowFromSystemServer();
-        }
-    }
-
-    /**
      * Information you can retrieve about any processes that are in an error condition.
      */
     public static class ProcessErrorStateInfo implements Parcelable {
index ac301b3..1756f22 100644 (file)
@@ -423,10 +423,8 @@ interface IActivityManager {
     void reportActivityFullyDrawn(in IBinder token, boolean restoredFromBundle);
     void restart();
     void performIdleMaintenance();
-    void takePersistableUriPermission(in Uri uri, int modeFlags, int userId);
-    boolean updatePersistableUriPermission(in Uri uri, boolean prefix, String packageName,
-                                           boolean grant, int userId);
-    void releasePersistableUriPermission(in Uri uri, int modeFlags, int userId);
+    void takePersistableUriPermission(in Uri uri, int modeFlags, String toPackage, int userId);
+    void releasePersistableUriPermission(in Uri uri, int modeFlags, String toPackage, int userId);
     ParceledListSlice getPersistedUriPermissions(in String packageName, boolean incoming);
     void appNotRespondingViaProvider(in IBinder connection);
     Rect getTaskBounds(int taskId);
index 22496a4..10331d4 100644 (file)
@@ -2102,7 +2102,23 @@ public abstract class ContentResolver {
         Preconditions.checkNotNull(uri, "uri");
         try {
             ActivityManager.getService().takePersistableUriPermission(
-                    ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
+                    ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
+                    resolveUserId(uri));
+        } catch (RemoteException e) {
+        }
+    }
+
+    /**
+     * @hide
+     */
+    public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
+            @Intent.AccessUriMode int modeFlags) {
+        Preconditions.checkNotNull(toPackage, "toPackage");
+        Preconditions.checkNotNull(uri, "uri");
+        try {
+            ActivityManager.getService().takePersistableUriPermission(
+                    ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
+                    resolveUserId(uri));
         } catch (RemoteException e) {
         }
     }
@@ -2120,7 +2136,23 @@ public abstract class ContentResolver {
         Preconditions.checkNotNull(uri, "uri");
         try {
             ActivityManager.getService().releasePersistableUriPermission(
-                    ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
+                    ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
+                    resolveUserId(uri));
+        } catch (RemoteException e) {
+        }
+    }
+
+    /**
+     * @hide
+     */
+    public void releasePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
+            @Intent.AccessUriMode int modeFlags) {
+        Preconditions.checkNotNull(toPackage, "toPackage");
+        Preconditions.checkNotNull(uri, "uri");
+        try {
+            ActivityManager.getService().releasePersistableUriPermission(
+                    ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
+                    resolveUserId(uri));
         } catch (RemoteException e) {
         }
     }
index a7178a0..5e12e7e 100644 (file)
          settings app.  This permission cannot be granted to third-party apps.
          <p>Protection level: signature
     -->
-    <permission android:name="android.permission.MANAGE_SCOPED_ACCESS_DIRECTORY_PERMISSIONS"
+    <permission
+         android:name="android.permission.MANAGE_SCOPED_ACCESS_DIRECTORY_PERMISSIONS"
+         android:protectionLevel="signature" />
+
+    <!-- @hide
+         Allows an application to change the status of a persistable URI permission granted
+         to another application.
+         <p>This permission should <em>only</em> be requested by the platform
+         settings app.  This permission cannot be granted to third-party apps.
+         <p>Protection level: signature
+    -->
+    <permission android:name="android.permission.FORCE_PERSISTABLE_URI_PERMISSIONS"
         android:protectionLevel="signature" />
 
     <!-- @SystemApi Old permission for deleting an app's cache files, no longer used,
index f4b0dba..d0ce841 100644 (file)
@@ -10147,92 +10147,33 @@ public class ActivityManagerService extends IActivityManager.Stub
     }
 
     /**
-     * Updates (grants or revokes) a persitable URI permission.
-     *
-     * @param uri URI to be granted or revoked.
-     * @param prefix if {@code false}, permission apply to this specific URI; if {@code true}, it
-     * applies to all URIs that are prefixed by this URI.
-     * @param packageName target package.
-     * @param grant if {@code true} a new permission will be granted, otherwise an existing
-     * permission will be revoked.
-     * @param userId user handle
-     *
-     * @return whether or not the requested succeeded.
-     *
-     * @deprecated TODO(b/72055774): caller should use takePersistableUriPermission() or
-     * releasePersistableUriPermission() instead, but such change will be made in a separate CL
-     * so it can be easily reverted if it breaks existing functionality.
-     */
-    @Deprecated // STOPSHIP if not removed
-    @Override
-    public boolean updatePersistableUriPermission(Uri uri, boolean prefix, String packageName,
-            boolean grant, int userId) {
-        enforceCallingPermission(android.Manifest.permission.GET_APP_GRANTED_URI_PERMISSIONS,
-                "updatePersistableUriPermission");
-        final int uid = mPackageManagerInt.getPackageUid(packageName, 0, userId);
-
-        final GrantUri grantUri = new GrantUri(userId, uri, prefix);
-
-        boolean persistChanged = false;
-        synchronized (this) {
-            if (grant) { // Grant
-                final String authority = uri.getAuthority();
-                final ProviderInfo pi = getProviderInfoLocked(authority, userId, 0);
-                if (pi == null) {
-                    Slog.w(TAG, "No content provider found for authority " + authority);
-                    return false;
-                }
-                final UriPermission permission = findOrCreateUriPermissionLocked(pi.packageName,
-                        packageName, uid, grantUri);
-                if (permission.isNew()) {
-                    final int modeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION
-                            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
-                    permission.initPersistedModes(modeFlags, System.currentTimeMillis());
-                    persistChanged = true;
-                } else {
-                    // Caller should not try to grant permission that is already granted.
-                    Slog.w(TAG_URI_PERMISSION,
-                            "permission already granted for " + grantUri.toSafeString());
-                    return false;
-                }
-                persistChanged |= maybePrunePersistedUriGrantsLocked(uid);
-            } else { // Revoke
-                final UriPermission permission = findUriPermissionLocked(uid, grantUri);
-                if (permission == null) {
-                    // Caller should not try to revoke permission that is not granted.
-                    Slog.v(TAG_URI_PERMISSION, "no permission for " + grantUri.toSafeString());
-                    return false;
-                } else {
-                    permission.modeFlags = 0;
-                    removeUriPermissionIfNeededLocked(permission);
-                    persistChanged = true;
-                }
-            }
-            if (persistChanged) {
-                schedulePersistUriGrants();
-            }
-        }
-        return true;
-    }
-
-    /**
      * @param uri This uri must NOT contain an embedded userId.
+     * @param toPackage Name of package whose uri is being granted to (if {@code null}, uses
+     * calling uid)
      * @param userId The userId in which the uri is to be resolved.
      */
     @Override
-    public void takePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
-        enforceNotIsolatedCaller("takePersistableUriPermission");
+    public void takePersistableUriPermission(Uri uri, final int modeFlags,
+            @Nullable String toPackage, int userId) {
+        final int uid;
+        if (toPackage != null) {
+            enforceCallingPermission(android.Manifest.permission.FORCE_PERSISTABLE_URI_PERMISSIONS,
+                    "takePersistableUriPermission");
+            uid = mPackageManagerInt.getPackageUid(toPackage, 0, userId);
+        } else {
+            enforceNotIsolatedCaller("takePersistableUriPermission");
+            uid = Binder.getCallingUid();
+        }
 
         Preconditions.checkFlagsArgument(modeFlags,
                 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
 
         synchronized (this) {
-            final int callingUid = Binder.getCallingUid();
             boolean persistChanged = false;
             GrantUri grantUri = new GrantUri(userId, uri, false);
 
-            UriPermission exactPerm = findUriPermissionLocked(callingUid, grantUri);
-            UriPermission prefixPerm = findUriPermissionLocked(callingUid,
+            UriPermission exactPerm = findUriPermissionLocked(uid, grantUri);
+            UriPermission prefixPerm = findUriPermissionLocked(uid,
                     new GrantUri(userId, uri, true));
 
             final boolean exactValid = (exactPerm != null)
@@ -10242,7 +10183,7 @@ public class ActivityManagerService extends IActivityManager.Stub
 
             if (!(exactValid || prefixValid)) {
                 throw new SecurityException("No persistable permission grants found for UID "
-                        + callingUid + " and Uri " + grantUri.toSafeString());
+                        + uid + " and Uri " + grantUri.toSafeString());
             }
 
             if (exactValid) {
@@ -10252,7 +10193,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                 persistChanged |= prefixPerm.takePersistableModes(modeFlags);
             }
 
-            persistChanged |= maybePrunePersistedUriGrantsLocked(callingUid);
+            persistChanged |= maybePrunePersistedUriGrantsLocked(uid);
 
             if (persistChanged) {
                 schedulePersistUriGrants();
@@ -10262,25 +10203,36 @@ public class ActivityManagerService extends IActivityManager.Stub
 
     /**
      * @param uri This uri must NOT contain an embedded userId.
+     * @param toPackage Name of the target package whose uri is being released (if {@code null},
+     * uses calling uid)
      * @param userId The userId in which the uri is to be resolved.
      */
     @Override
-    public void releasePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
-        enforceNotIsolatedCaller("releasePersistableUriPermission");
+    public void releasePersistableUriPermission(Uri uri, final int modeFlags,
+            @Nullable String toPackage, int userId) {
+
+        final int uid;
+        if (toPackage != null) {
+            enforceCallingPermission(android.Manifest.permission.FORCE_PERSISTABLE_URI_PERMISSIONS,
+                    "releasePersistableUriPermission");
+            uid = mPackageManagerInt.getPackageUid(toPackage, 0, userId);
+        } else {
+            enforceNotIsolatedCaller("releasePersistableUriPermission");
+            uid = Binder.getCallingUid();
+        }
 
         Preconditions.checkFlagsArgument(modeFlags,
                 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
 
         synchronized (this) {
-            final int callingUid = Binder.getCallingUid();
             boolean persistChanged = false;
 
-            UriPermission exactPerm = findUriPermissionLocked(callingUid,
+            UriPermission exactPerm = findUriPermissionLocked(uid,
                     new GrantUri(userId, uri, false));
-            UriPermission prefixPerm = findUriPermissionLocked(callingUid,
+            UriPermission prefixPerm = findUriPermissionLocked(uid,
                     new GrantUri(userId, uri, true));
-            if (exactPerm == null && prefixPerm == null) {
-                throw new SecurityException("No permission grants found for UID " + callingUid
+            if (exactPerm == null && prefixPerm == null && toPackage == null) {
+                throw new SecurityException("No permission grants found for UID " + uid
                         + " and Uri " + uri.toSafeString());
             }
 
index 3bf1cf4..1e071aa 100644 (file)
@@ -124,10 +124,6 @@ final class UriPermission {
         updateModeFlags();
     }
 
-    boolean isNew() {
-        return persistedCreateTime == INVALID_TIME;
-    }
-
     void grantModes(int modeFlags, UriPermissionOwner owner) {
         final boolean persistable = (modeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0;
         modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION