Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.slice; |
| 18 | |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 19 | import static android.app.usage.UsageEvents.Event.SLICE_PINNED; |
| 20 | import static android.app.usage.UsageEvents.Event.SLICE_PINNED_PRIV; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 21 | import static android.content.ContentProvider.getUriWithoutUserId; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 22 | import static android.content.ContentProvider.getUserIdFromUri; |
| 23 | import static android.content.ContentProvider.maybeAddUserId; |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.PERMISSION_DENIED; |
| 25 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 26 | import static android.os.Process.SYSTEM_UID; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 27 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 28 | import android.Manifest.permission; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 29 | import android.app.ActivityManager; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 30 | import android.app.AppOpsManager; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 31 | import android.app.ContentProviderHolder; |
| 32 | import android.app.IActivityManager; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 33 | import android.app.slice.ISliceManager; |
| 34 | import android.app.slice.SliceSpec; |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 35 | import android.app.usage.UsageStatsManagerInternal; |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 36 | import android.content.BroadcastReceiver; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 37 | import android.content.ComponentName; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 38 | import android.content.ContentProvider; |
Jason Monk | 7f01f3b | 2018-05-15 15:46:26 -0400 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 44 | import android.content.pm.PackageManagerInternal; |
Jason Monk | da90df7 | 2018-07-03 11:21:57 -0400 | [diff] [blame] | 45 | import android.content.pm.ProviderInfo; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 46 | import android.content.pm.ResolveInfo; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 47 | import android.net.Uri; |
| 48 | import android.os.Binder; |
| 49 | import android.os.Handler; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 50 | import android.os.IBinder; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 51 | import android.os.Looper; |
| 52 | import android.os.Process; |
| 53 | import android.os.RemoteException; |
Jason Monk | 7f01f3b | 2018-05-15 15:46:26 -0400 | [diff] [blame] | 54 | import android.os.ResultReceiver; |
| 55 | import android.os.ShellCallback; |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 56 | import android.os.UserHandle; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 57 | import android.util.ArrayMap; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 58 | import android.util.Slog; |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 59 | import android.util.SparseArray; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 60 | import android.util.Xml.Encoding; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 61 | |
| 62 | import com.android.internal.annotations.GuardedBy; |
| 63 | import com.android.internal.annotations.VisibleForTesting; |
| 64 | import com.android.internal.app.AssistUtils; |
| 65 | import com.android.internal.util.Preconditions; |
| 66 | import com.android.server.LocalServices; |
| 67 | import com.android.server.ServiceThread; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 68 | import com.android.server.SystemService; |
| 69 | |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 70 | import org.xmlpull.v1.XmlPullParser; |
| 71 | import org.xmlpull.v1.XmlPullParserException; |
| 72 | import org.xmlpull.v1.XmlPullParserFactory; |
| 73 | import org.xmlpull.v1.XmlSerializer; |
| 74 | |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 75 | import java.io.ByteArrayInputStream; |
| 76 | import java.io.ByteArrayOutputStream; |
Jason Monk | 7f01f3b | 2018-05-15 15:46:26 -0400 | [diff] [blame] | 77 | import java.io.FileDescriptor; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 78 | import java.io.IOException; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 79 | import java.util.ArrayList; |
| 80 | import java.util.List; |
| 81 | import java.util.Objects; |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 82 | import java.util.function.Supplier; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 83 | |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 84 | public class SliceManagerService extends ISliceManager.Stub { |
| 85 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 86 | private static final String TAG = "SliceManagerService"; |
| 87 | private final Object mLock = new Object(); |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 88 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 89 | private final Context mContext; |
| 90 | private final PackageManagerInternal mPackageManagerInternal; |
| 91 | private final AppOpsManager mAppOps; |
| 92 | private final AssistUtils mAssistUtils; |
| 93 | |
| 94 | @GuardedBy("mLock") |
| 95 | private final ArrayMap<Uri, PinnedSliceState> mPinnedSlicesByUri = new ArrayMap<>(); |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 96 | @GuardedBy("mLock") |
| 97 | private final SparseArray<PackageMatchingCache> mAssistantLookup = new SparseArray<>(); |
| 98 | @GuardedBy("mLock") |
| 99 | private final SparseArray<PackageMatchingCache> mHomeLookup = new SparseArray<>(); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 100 | private final Handler mHandler; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 101 | |
| 102 | private final SlicePermissionManager mPermissions; |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 103 | private final UsageStatsManagerInternal mAppUsageStats; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 104 | |
| 105 | public SliceManagerService(Context context) { |
| 106 | this(context, createHandler().getLooper()); |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 109 | @VisibleForTesting |
| 110 | SliceManagerService(Context context, Looper looper) { |
| 111 | mContext = context; |
| 112 | mPackageManagerInternal = Preconditions.checkNotNull( |
| 113 | LocalServices.getService(PackageManagerInternal.class)); |
| 114 | mAppOps = context.getSystemService(AppOpsManager.class); |
| 115 | mAssistUtils = new AssistUtils(context); |
| 116 | mHandler = new Handler(looper); |
| 117 | |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 118 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 119 | |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 120 | mPermissions = new SlicePermissionManager(mContext, looper); |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 121 | |
| 122 | IntentFilter filter = new IntentFilter(); |
| 123 | filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); |
| 124 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 125 | filter.addDataScheme("package"); |
| 126 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /// ----- Lifecycle stuff ----- |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 130 | private void systemReady() { |
| 131 | } |
| 132 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 133 | private void onUnlockUser(int userId) { |
| 134 | } |
| 135 | |
| 136 | private void onStopUser(int userId) { |
| 137 | synchronized (mLock) { |
| 138 | mPinnedSlicesByUri.values().removeIf(s -> getUserIdFromUri(s.getUri()) == userId); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// ----- ISliceManager stuff ----- |
| 143 | @Override |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 144 | public Uri[] getPinnedSlices(String pkg) { |
| 145 | verifyCaller(pkg); |
Jason Monk | 199286b | 2018-04-12 19:47:50 -0400 | [diff] [blame] | 146 | int callingUser = Binder.getCallingUserHandle().getIdentifier(); |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 147 | ArrayList<Uri> ret = new ArrayList<>(); |
| 148 | synchronized (mLock) { |
| 149 | for (PinnedSliceState state : mPinnedSlicesByUri.values()) { |
| 150 | if (Objects.equals(pkg, state.getPkg())) { |
Jason Monk | 199286b | 2018-04-12 19:47:50 -0400 | [diff] [blame] | 151 | Uri uri = state.getUri(); |
| 152 | int userId = ContentProvider.getUserIdFromUri(uri, callingUser); |
| 153 | if (userId == callingUser) { |
| 154 | ret.add(ContentProvider.getUriWithoutUserId(uri)); |
| 155 | } |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | return ret.toArray(new Uri[ret.size()]); |
| 160 | } |
| 161 | |
| 162 | @Override |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 163 | public void pinSlice(String pkg, Uri uri, SliceSpec[] specs, IBinder token) |
| 164 | throws RemoteException { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 165 | verifyCaller(pkg); |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 166 | enforceAccess(pkg, uri); |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 167 | int user = Binder.getCallingUserHandle().getIdentifier(); |
| 168 | uri = maybeAddUserId(uri, user); |
Jason Monk | 397f6d8 | 2018-04-24 17:01:11 -0400 | [diff] [blame] | 169 | String slicePkg = getProviderPkg(uri, user); |
| 170 | getOrCreatePinnedSlice(uri, slicePkg).pin(pkg, specs, token); |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 171 | |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 172 | mHandler.post(() -> { |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 173 | if (slicePkg != null && !Objects.equals(pkg, slicePkg)) { |
| 174 | mAppUsageStats.reportEvent(slicePkg, user, |
| 175 | isAssistant(pkg, user) || isDefaultHomeApp(pkg, user) |
| 176 | ? SLICE_PINNED_PRIV : SLICE_PINNED); |
| 177 | } |
| 178 | }); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 182 | public void unpinSlice(String pkg, Uri uri, IBinder token) throws RemoteException { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 183 | verifyCaller(pkg); |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 184 | enforceAccess(pkg, uri); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 185 | uri = maybeAddUserId(uri, Binder.getCallingUserHandle().getIdentifier()); |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 186 | if (getPinnedSlice(uri).unpin(pkg, token)) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 187 | removePinnedSlice(uri); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean hasSliceAccess(String pkg) throws RemoteException { |
| 193 | verifyCaller(pkg); |
| 194 | return hasFullSliceAccess(pkg, Binder.getCallingUserHandle().getIdentifier()); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public SliceSpec[] getPinnedSpecs(Uri uri, String pkg) throws RemoteException { |
| 199 | verifyCaller(pkg); |
| 200 | enforceAccess(pkg, uri); |
| 201 | return getPinnedSlice(uri).getSpecs(); |
| 202 | } |
| 203 | |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 204 | @Override |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 205 | public void grantSlicePermission(String pkg, String toPkg, Uri uri) throws RemoteException { |
| 206 | verifyCaller(pkg); |
| 207 | int user = Binder.getCallingUserHandle().getIdentifier(); |
| 208 | enforceOwner(pkg, uri, user); |
| 209 | mPermissions.grantSliceAccess(toPkg, user, pkg, user, uri); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void revokeSlicePermission(String pkg, String toPkg, Uri uri) throws RemoteException { |
| 214 | verifyCaller(pkg); |
| 215 | int user = Binder.getCallingUserHandle().getIdentifier(); |
| 216 | enforceOwner(pkg, uri, user); |
| 217 | mPermissions.revokeSliceAccess(toPkg, user, pkg, user, uri); |
| 218 | } |
| 219 | |
| 220 | @Override |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 221 | public int checkSlicePermission(Uri uri, String pkg, int pid, int uid, |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 222 | String[] autoGrantPermissions) { |
| 223 | int userId = UserHandle.getUserId(uid); |
| 224 | if (pkg == null) { |
| 225 | for (String p : mContext.getPackageManager().getPackagesForUid(uid)) { |
| 226 | if (checkSlicePermission(uri, p, pid, uid, autoGrantPermissions) |
| 227 | == PERMISSION_GRANTED) { |
| 228 | return PERMISSION_GRANTED; |
| 229 | } |
| 230 | } |
| 231 | return PERMISSION_DENIED; |
| 232 | } |
| 233 | if (hasFullSliceAccess(pkg, userId)) { |
| 234 | return PackageManager.PERMISSION_GRANTED; |
| 235 | } |
| 236 | if (mPermissions.hasPermission(pkg, userId, uri)) { |
| 237 | return PackageManager.PERMISSION_GRANTED; |
| 238 | } |
| 239 | if (autoGrantPermissions != null) { |
| 240 | // Need to own the Uri to call in with permissions to grant. |
| 241 | enforceOwner(pkg, uri, userId); |
| 242 | for (String perm : autoGrantPermissions) { |
| 243 | if (mContext.checkPermission(perm, pid, uid) == PERMISSION_GRANTED) { |
| 244 | int providerUser = ContentProvider.getUserIdFromUri(uri, userId); |
| 245 | String providerPkg = getProviderPkg(uri, providerUser); |
| 246 | mPermissions.grantSliceAccess(pkg, userId, providerPkg, providerUser, uri); |
| 247 | return PackageManager.PERMISSION_GRANTED; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | // Fallback to allowing uri permissions through. |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 252 | if (mContext.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 253 | == PERMISSION_GRANTED) { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 254 | return PackageManager.PERMISSION_GRANTED; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 255 | } |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 256 | return PackageManager.PERMISSION_DENIED; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void grantPermissionFromUser(Uri uri, String pkg, String callingPkg, boolean allSlices) { |
| 261 | verifyCaller(callingPkg); |
| 262 | getContext().enforceCallingOrSelfPermission(permission.MANAGE_SLICE_PERMISSIONS, |
| 263 | "Slice granting requires MANAGE_SLICE_PERMISSIONS"); |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 264 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 265 | if (allSlices) { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 266 | mPermissions.grantFullAccess(pkg, userId); |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 267 | } else { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 268 | // When granting, grant to all slices in the provider. |
| 269 | Uri grantUri = uri.buildUpon() |
| 270 | .path("") |
| 271 | .build(); |
| 272 | int providerUser = ContentProvider.getUserIdFromUri(grantUri, userId); |
| 273 | String providerPkg = getProviderPkg(grantUri, providerUser); |
| 274 | mPermissions.grantSliceAccess(pkg, userId, providerPkg, providerUser, grantUri); |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 275 | } |
| 276 | long ident = Binder.clearCallingIdentity(); |
| 277 | try { |
| 278 | mContext.getContentResolver().notifyChange(uri, null); |
| 279 | } finally { |
| 280 | Binder.restoreCallingIdentity(ident); |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 284 | // Backup/restore interface |
| 285 | @Override |
| 286 | public byte[] getBackupPayload(int user) { |
| 287 | if (Binder.getCallingUid() != SYSTEM_UID) { |
| 288 | throw new SecurityException("Caller must be system"); |
| 289 | } |
| 290 | //TODO: http://b/22388012 |
| 291 | if (user != UserHandle.USER_SYSTEM) { |
| 292 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 293 | return null; |
| 294 | } |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 295 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 296 | try { |
| 297 | XmlSerializer out = XmlPullParserFactory.newInstance().newSerializer(); |
| 298 | out.setOutput(baos, Encoding.UTF_8.name()); |
| 299 | |
| 300 | mPermissions.writeBackup(out); |
| 301 | |
| 302 | out.flush(); |
| 303 | return baos.toByteArray(); |
| 304 | } catch (IOException | XmlPullParserException e) { |
| 305 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 306 | } |
| 307 | return null; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void applyRestore(byte[] payload, int user) { |
| 312 | if (Binder.getCallingUid() != SYSTEM_UID) { |
| 313 | throw new SecurityException("Caller must be system"); |
| 314 | } |
| 315 | if (payload == null) { |
| 316 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 317 | return; |
| 318 | } |
| 319 | //TODO: http://b/22388012 |
| 320 | if (user != UserHandle.USER_SYSTEM) { |
| 321 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 322 | return; |
| 323 | } |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 324 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 325 | try { |
| 326 | XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); |
| 327 | parser.setInput(bais, Encoding.UTF_8.name()); |
| 328 | mPermissions.readRestore(parser); |
| 329 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 330 | Slog.w(TAG, "applyRestore: error reading payload", e); |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Jason Monk | 7f01f3b | 2018-05-15 15:46:26 -0400 | [diff] [blame] | 334 | @Override |
| 335 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 336 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
| 337 | new SliceShellCommand(this).exec(this, in, out, err, args, callback, resultReceiver); |
| 338 | } |
| 339 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 340 | /// ----- internal code ----- |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 341 | private void enforceOwner(String pkg, Uri uri, int user) { |
| 342 | if (!Objects.equals(getProviderPkg(uri, user), pkg) || pkg == null) { |
| 343 | throw new SecurityException("Caller must own " + uri); |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 344 | } |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Jason Monk | 9e3b864 | 2018-01-21 20:54:00 -0500 | [diff] [blame] | 347 | protected void removePinnedSlice(Uri uri) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 348 | synchronized (mLock) { |
| 349 | mPinnedSlicesByUri.remove(uri).destroy(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private PinnedSliceState getPinnedSlice(Uri uri) { |
| 354 | synchronized (mLock) { |
| 355 | PinnedSliceState manager = mPinnedSlicesByUri.get(uri); |
| 356 | if (manager == null) { |
| 357 | throw new IllegalStateException(String.format("Slice %s not pinned", |
| 358 | uri.toString())); |
| 359 | } |
| 360 | return manager; |
| 361 | } |
| 362 | } |
| 363 | |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 364 | private PinnedSliceState getOrCreatePinnedSlice(Uri uri, String pkg) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 365 | synchronized (mLock) { |
| 366 | PinnedSliceState manager = mPinnedSlicesByUri.get(uri); |
| 367 | if (manager == null) { |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 368 | manager = createPinnedSlice(uri, pkg); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 369 | mPinnedSlicesByUri.put(uri, manager); |
| 370 | } |
| 371 | return manager; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | @VisibleForTesting |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 376 | protected PinnedSliceState createPinnedSlice(Uri uri, String pkg) { |
| 377 | return new PinnedSliceState(this, uri, pkg); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | public Object getLock() { |
| 381 | return mLock; |
| 382 | } |
| 383 | |
| 384 | public Context getContext() { |
| 385 | return mContext; |
| 386 | } |
| 387 | |
| 388 | public Handler getHandler() { |
| 389 | return mHandler; |
| 390 | } |
| 391 | |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 392 | protected int checkAccess(String pkg, Uri uri, int uid, int pid) { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 393 | return checkSlicePermission(uri, pkg, uid, pid, null); |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 396 | private String getProviderPkg(Uri uri, int user) { |
| 397 | long ident = Binder.clearCallingIdentity(); |
| 398 | try { |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 399 | String providerName = getUriWithoutUserId(uri).getAuthority(); |
Jason Monk | da90df7 | 2018-07-03 11:21:57 -0400 | [diff] [blame] | 400 | ProviderInfo provider = mContext.getPackageManager().resolveContentProviderAsUser( |
| 401 | providerName, 0, getUserIdFromUri(uri, user)); |
| 402 | return provider.packageName; |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 403 | } finally { |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 404 | Binder.restoreCallingIdentity(ident); |
| 405 | } |
| 406 | } |
| 407 | |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 408 | private void enforceCrossUser(String pkg, Uri uri) { |
| 409 | int user = Binder.getCallingUserHandle().getIdentifier(); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 410 | if (getUserIdFromUri(uri, user) != user) { |
| 411 | getContext().enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS_FULL, |
| 412 | "Slice interaction across users requires INTERACT_ACROSS_USERS_FULL"); |
| 413 | } |
| 414 | } |
| 415 | |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 416 | private void enforceAccess(String pkg, Uri uri) throws RemoteException { |
| 417 | if (checkAccess(pkg, uri, Binder.getCallingUid(), Binder.getCallingPid()) |
| 418 | != PERMISSION_GRANTED) { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 419 | int userId = ContentProvider.getUserIdFromUri(uri, |
| 420 | Binder.getCallingUserHandle().getIdentifier()); |
| 421 | if (!Objects.equals(pkg, getProviderPkg(uri, userId))) { |
| 422 | throw new SecurityException("Access to slice " + uri + " is required"); |
| 423 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 424 | } |
Jason Monk | 3a1d2e97 | 2018-01-29 16:58:11 -0500 | [diff] [blame] | 425 | enforceCrossUser(pkg, uri); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private void verifyCaller(String pkg) { |
| 429 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 430 | } |
| 431 | |
| 432 | private boolean hasFullSliceAccess(String pkg, int userId) { |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 433 | long ident = Binder.clearCallingIdentity(); |
| 434 | try { |
| 435 | boolean ret = isDefaultHomeApp(pkg, userId) || isAssistant(pkg, userId) |
| 436 | || isGrantedFullAccess(pkg, userId); |
| 437 | return ret; |
| 438 | } finally { |
| 439 | Binder.restoreCallingIdentity(ident); |
| 440 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | private boolean isAssistant(String pkg, int userId) { |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 444 | return getAssistantMatcher(userId).matches(pkg); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 447 | private boolean isDefaultHomeApp(String pkg, int userId) { |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 448 | return getHomeMatcher(userId).matches(pkg); |
| 449 | } |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 450 | |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 451 | private PackageMatchingCache getAssistantMatcher(int userId) { |
| 452 | PackageMatchingCache matcher = mAssistantLookup.get(userId); |
| 453 | if (matcher == null) { |
| 454 | matcher = new PackageMatchingCache(() -> getAssistant(userId)); |
| 455 | mAssistantLookup.put(userId, matcher); |
| 456 | } |
| 457 | return matcher; |
| 458 | } |
| 459 | |
| 460 | private PackageMatchingCache getHomeMatcher(int userId) { |
| 461 | PackageMatchingCache matcher = mHomeLookup.get(userId); |
| 462 | if (matcher == null) { |
| 463 | matcher = new PackageMatchingCache(() -> getDefaultHome(userId)); |
| 464 | mHomeLookup.put(userId, matcher); |
| 465 | } |
| 466 | return matcher; |
| 467 | } |
| 468 | |
| 469 | private String getAssistant(int userId) { |
| 470 | final ComponentName cn = mAssistUtils.getAssistComponentForUser(userId); |
| 471 | if (cn == null) { |
| 472 | return null; |
| 473 | } |
| 474 | return cn.getPackageName(); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // Based on getDefaultHome in ShortcutService. |
| 478 | // TODO: Unify if possible |
| 479 | @VisibleForTesting |
Jason Monk | 74848ae | 2018-01-21 17:11:57 -0500 | [diff] [blame] | 480 | protected String getDefaultHome(int userId) { |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 481 | final long token = Binder.clearCallingIdentity(); |
| 482 | try { |
| 483 | final List<ResolveInfo> allHomeCandidates = new ArrayList<>(); |
| 484 | |
| 485 | // Default launcher from package manager. |
| 486 | final ComponentName defaultLauncher = mPackageManagerInternal |
| 487 | .getHomeActivitiesAsUser(allHomeCandidates, userId); |
| 488 | |
| 489 | ComponentName detected = null; |
| 490 | if (defaultLauncher != null) { |
| 491 | detected = defaultLauncher; |
| 492 | } |
| 493 | |
| 494 | if (detected == null) { |
| 495 | // If we reach here, that means it's the first check since the user was created, |
| 496 | // and there's already multiple launchers and there's no default set. |
| 497 | // Find the system one with the highest priority. |
| 498 | // (We need to check the priority too because of FallbackHome in Settings.) |
| 499 | // If there's no system launcher yet, then no one can access slices, until |
| 500 | // the user explicitly sets one. |
| 501 | final int size = allHomeCandidates.size(); |
| 502 | |
| 503 | int lastPriority = Integer.MIN_VALUE; |
| 504 | for (int i = 0; i < size; i++) { |
| 505 | final ResolveInfo ri = allHomeCandidates.get(i); |
| 506 | if (!ri.activityInfo.applicationInfo.isSystemApp()) { |
| 507 | continue; |
| 508 | } |
| 509 | if (ri.priority < lastPriority) { |
| 510 | continue; |
| 511 | } |
| 512 | detected = ri.activityInfo.getComponentName(); |
| 513 | lastPriority = ri.priority; |
| 514 | } |
| 515 | } |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 516 | return detected != null ? detected.getPackageName() : null; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 517 | } finally { |
| 518 | Binder.restoreCallingIdentity(token); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | private boolean isGrantedFullAccess(String pkg, int userId) { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 523 | return mPermissions.hasFullAccess(pkg, userId); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | private static ServiceThread createHandler() { |
| 527 | ServiceThread handlerThread = new ServiceThread(TAG, |
| 528 | Process.THREAD_PRIORITY_BACKGROUND, true /*allowIo*/); |
| 529 | handlerThread.start(); |
| 530 | return handlerThread; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 533 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 534 | @Override |
| 535 | public void onReceive(Context context, Intent intent) { |
| 536 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 537 | if (userId == UserHandle.USER_NULL) { |
| 538 | Slog.w(TAG, "Intent broadcast does not contain user handle: " + intent); |
| 539 | return; |
| 540 | } |
| 541 | Uri data = intent.getData(); |
| 542 | String pkg = data != null ? data.getSchemeSpecificPart() : null; |
| 543 | if (pkg == null) { |
| 544 | Slog.w(TAG, "Intent broadcast does not contain package name: " + intent); |
| 545 | return; |
| 546 | } |
| 547 | switch (intent.getAction()) { |
| 548 | case Intent.ACTION_PACKAGE_REMOVED: |
| 549 | final boolean replacing = |
| 550 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 551 | if (!replacing) { |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 552 | mPermissions.removePkg(pkg, userId); |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 553 | } |
| 554 | break; |
| 555 | case Intent.ACTION_PACKAGE_DATA_CLEARED: |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 556 | mPermissions.removePkg(pkg, userId); |
Jason Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 557 | break; |
| 558 | } |
| 559 | } |
| 560 | }; |
| 561 | |
Jason Monk | 7f01f3b | 2018-05-15 15:46:26 -0400 | [diff] [blame] | 562 | public String[] getAllPackagesGranted(String authority) { |
| 563 | String pkg = getProviderPkg(new Uri.Builder() |
| 564 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 565 | .authority(authority) |
| 566 | .build(), 0); |
| 567 | return mPermissions.getAllPackagesGranted(pkg); |
| 568 | } |
| 569 | |
Jason Monk | e009a8e | 2018-05-21 13:36:21 -0400 | [diff] [blame] | 570 | /** |
| 571 | * Holder that caches a package that has access to a slice. |
| 572 | */ |
| 573 | static class PackageMatchingCache { |
| 574 | |
| 575 | private final Supplier<String> mPkgSource; |
| 576 | private String mCurrentPkg; |
| 577 | |
| 578 | public PackageMatchingCache(Supplier<String> pkgSource) { |
| 579 | mPkgSource = pkgSource; |
| 580 | } |
| 581 | |
| 582 | public boolean matches(String pkgCandidate) { |
| 583 | if (pkgCandidate == null) return false; |
| 584 | |
| 585 | if (Objects.equals(pkgCandidate, mCurrentPkg)) { |
| 586 | return true; |
| 587 | } |
| 588 | // Failed on cached value, try updating. |
| 589 | mCurrentPkg = mPkgSource.get(); |
| 590 | return Objects.equals(pkgCandidate, mCurrentPkg); |
| 591 | } |
| 592 | } |
| 593 | |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 594 | public static class Lifecycle extends SystemService { |
| 595 | private SliceManagerService mService; |
| 596 | |
| 597 | public Lifecycle(Context context) { |
| 598 | super(context); |
| 599 | } |
| 600 | |
| 601 | @Override |
| 602 | public void onStart() { |
| 603 | mService = new SliceManagerService(getContext()); |
| 604 | publishBinderService(Context.SLICE_SERVICE, mService); |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void onBootPhase(int phase) { |
| 609 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 610 | mService.systemReady(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public void onUnlockUser(int userHandle) { |
| 616 | mService.onUnlockUser(userHandle); |
| 617 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 618 | |
| 619 | @Override |
| 620 | public void onStopUser(int userHandle) { |
| 621 | mService.onStopUser(userHandle); |
| 622 | } |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 623 | } |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 624 | |
| 625 | private class SliceGrant { |
| 626 | private final Uri mUri; |
| 627 | private final String mPkg; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 628 | private final int mUserId; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 629 | |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 630 | public SliceGrant(Uri uri, String pkg, int userId) { |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 631 | mUri = uri; |
| 632 | mPkg = pkg; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 633 | mUserId = userId; |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public int hashCode() { |
| 638 | return mUri.hashCode() + mPkg.hashCode(); |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public boolean equals(Object obj) { |
| 643 | if (!(obj instanceof SliceGrant)) return false; |
| 644 | SliceGrant other = (SliceGrant) obj; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 645 | return Objects.equals(other.mUri, mUri) && Objects.equals(other.mPkg, mPkg) |
| 646 | && (other.mUserId == mUserId); |
Jason Monk | e8f8be7 | 2018-01-21 10:10:35 -0500 | [diff] [blame] | 647 | } |
| 648 | } |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 649 | } |