Make sure settings writes are permission checked correctly

The last bit of undoing the earlier tangle around query results having
observers under the calling user's identity.  We do *not* want to drop
calling identity in the call() processing; we want the table-based
permission checks at the point of the underlying db operations to be
performed against that identity.

Bug 7265610

Change-Id: Ie0c9331ebd0918262a0a32b5b03b876fc2a92ca3
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index 76a5022..8086bbc 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -556,59 +556,52 @@
             }
         }
 
-        // Okay, permission checks have cleared.  Reset to our own identity so we can
-        // manipulate all users' data with impunity.
-        long oldId = Binder.clearCallingIdentity();
-        try {
-            // Note: we assume that get/put operations for moved-to-global names have already
-            // been directed to the new location on the caller side (otherwise we'd fix them
-            // up here).
-            DatabaseHelper dbHelper;
-            SettingsCache cache;
+        // Note: we assume that get/put operations for moved-to-global names have already
+        // been directed to the new location on the caller side (otherwise we'd fix them
+        // up here).
+        DatabaseHelper dbHelper;
+        SettingsCache cache;
 
-            // Get methods
-            if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
-                if (LOCAL_LOGV) Slog.v(TAG, "call(system:" + request + ") for " + callingUser);
-                dbHelper = getOrEstablishDatabase(callingUser);
-                cache = sSystemCaches.get(callingUser);
-                return lookupValue(dbHelper, TABLE_SYSTEM, cache, request);
-            }
-            if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
-                if (LOCAL_LOGV) Slog.v(TAG, "call(secure:" + request + ") for " + callingUser);
-                dbHelper = getOrEstablishDatabase(callingUser);
-                cache = sSecureCaches.get(callingUser);
-                return lookupValue(dbHelper, TABLE_SECURE, cache, request);
-            }
-            if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
-                if (LOCAL_LOGV) Slog.v(TAG, "call(global:" + request + ") for " + callingUser);
-                // fast path: owner db & cache are immutable after onCreate() so we need not
-                // guard on the attempt to look them up
-                return lookupValue(getOrEstablishDatabase(UserHandle.USER_OWNER), TABLE_GLOBAL,
-                        sGlobalCache, request);
-            }
+        // Get methods
+        if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
+            if (LOCAL_LOGV) Slog.v(TAG, "call(system:" + request + ") for " + callingUser);
+            dbHelper = getOrEstablishDatabase(callingUser);
+            cache = sSystemCaches.get(callingUser);
+            return lookupValue(dbHelper, TABLE_SYSTEM, cache, request);
+        }
+        if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
+            if (LOCAL_LOGV) Slog.v(TAG, "call(secure:" + request + ") for " + callingUser);
+            dbHelper = getOrEstablishDatabase(callingUser);
+            cache = sSecureCaches.get(callingUser);
+            return lookupValue(dbHelper, TABLE_SECURE, cache, request);
+        }
+        if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
+            if (LOCAL_LOGV) Slog.v(TAG, "call(global:" + request + ") for " + callingUser);
+            // fast path: owner db & cache are immutable after onCreate() so we need not
+            // guard on the attempt to look them up
+            return lookupValue(getOrEstablishDatabase(UserHandle.USER_OWNER), TABLE_GLOBAL,
+                    sGlobalCache, request);
+        }
 
-            // Put methods - new value is in the args bundle under the key named by
-            // the Settings.NameValueTable.VALUE static.
-            final String newValue = (args == null)
-                    ? null : args.getString(Settings.NameValueTable.VALUE);
+        // Put methods - new value is in the args bundle under the key named by
+        // the Settings.NameValueTable.VALUE static.
+        final String newValue = (args == null)
+        ? null : args.getString(Settings.NameValueTable.VALUE);
 
-            final ContentValues values = new ContentValues();
-            values.put(Settings.NameValueTable.NAME, request);
-            values.put(Settings.NameValueTable.VALUE, newValue);
-            if (Settings.CALL_METHOD_PUT_SYSTEM.equals(method)) {
-                if (LOCAL_LOGV) Slog.v(TAG, "call_put(system:" + request + "=" + newValue + ") for " + callingUser);
-                insertForUser(Settings.System.CONTENT_URI, values, callingUser);
-            } else if (Settings.CALL_METHOD_PUT_SECURE.equals(method)) {
-                if (LOCAL_LOGV) Slog.v(TAG, "call_put(secure:" + request + "=" + newValue + ") for " + callingUser);
-                insertForUser(Settings.Secure.CONTENT_URI, values, callingUser);
-            } else if (Settings.CALL_METHOD_PUT_GLOBAL.equals(method)) {
-                if (LOCAL_LOGV) Slog.v(TAG, "call_put(global:" + request + "=" + newValue + ") for " + callingUser);
-                insertForUser(Settings.Global.CONTENT_URI, values, callingUser);
-            } else {
-                Slog.w(TAG, "call() with invalid method: " + method);
-            }
-        } finally {
-            Binder.restoreCallingIdentity(oldId);
+        final ContentValues values = new ContentValues();
+        values.put(Settings.NameValueTable.NAME, request);
+        values.put(Settings.NameValueTable.VALUE, newValue);
+        if (Settings.CALL_METHOD_PUT_SYSTEM.equals(method)) {
+            if (LOCAL_LOGV) Slog.v(TAG, "call_put(system:" + request + "=" + newValue + ") for " + callingUser);
+            insertForUser(Settings.System.CONTENT_URI, values, callingUser);
+        } else if (Settings.CALL_METHOD_PUT_SECURE.equals(method)) {
+            if (LOCAL_LOGV) Slog.v(TAG, "call_put(secure:" + request + "=" + newValue + ") for " + callingUser);
+            insertForUser(Settings.Secure.CONTENT_URI, values, callingUser);
+        } else if (Settings.CALL_METHOD_PUT_GLOBAL.equals(method)) {
+            if (LOCAL_LOGV) Slog.v(TAG, "call_put(global:" + request + "=" + newValue + ") for " + callingUser);
+            insertForUser(Settings.Global.CONTENT_URI, values, callingUser);
+        } else {
+            Slog.w(TAG, "call() with invalid method: " + method);
         }
 
         return null;