Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.pm; |
| 18 | |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 19 | import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER; |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 20 | import static android.provider.DeviceConfig.NAMESPACE_PACKAGE_MANAGER_SERVICE; |
| 21 | |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 22 | import android.Manifest; |
Patrick Baumann | 051d75c | 2020-01-10 15:13:48 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 26 | import android.content.IntentFilter; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Patrick Baumann | 051d75c | 2020-01-10 15:13:48 -0800 | [diff] [blame] | 28 | import android.content.pm.PackageParser; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 29 | import android.content.pm.parsing.AndroidPackage; |
| 30 | import android.content.pm.parsing.ComponentParseUtils; |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 31 | import android.content.pm.parsing.ComponentParseUtils.ParsedActivity; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 32 | import android.content.pm.parsing.ComponentParseUtils.ParsedComponent; |
| 33 | import android.content.pm.parsing.ComponentParseUtils.ParsedIntentInfo; |
| 34 | import android.content.pm.parsing.ComponentParseUtils.ParsedProvider; |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 35 | import android.content.pm.parsing.ComponentParseUtils.ParsedService; |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 36 | import android.net.Uri; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 37 | import android.os.Process; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 38 | import android.os.Trace; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 39 | import android.os.UserHandle; |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 40 | import android.provider.DeviceConfig; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
| 42 | import android.util.ArrayMap; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 43 | import android.util.ArraySet; |
| 44 | import android.util.Slog; |
| 45 | import android.util.SparseArray; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 46 | import android.util.SparseSetArray; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 47 | |
| 48 | import com.android.internal.R; |
Patrick Baumann | a94cbc1 | 2019-09-17 08:08:10 -0700 | [diff] [blame] | 49 | import com.android.internal.annotations.VisibleForTesting; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 50 | import com.android.internal.util.ArrayUtils; |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 51 | import com.android.server.FgThread; |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 52 | import com.android.server.om.OverlayReferenceMapper; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 53 | |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 54 | import java.io.PrintWriter; |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 55 | import java.util.List; |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 56 | import java.util.Objects; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 57 | import java.util.Set; |
Patrick Baumann | e2d9d71 | 2020-01-10 13:32:09 -0800 | [diff] [blame] | 58 | import java.util.StringTokenizer; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * The entity responsible for filtering visibility between apps based on declarations in their |
| 62 | * manifests. |
| 63 | */ |
Patrick Baumann | a94cbc1 | 2019-09-17 08:08:10 -0700 | [diff] [blame] | 64 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 65 | public class AppsFilter { |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 66 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 67 | private static final String TAG = "AppsFilter"; |
Patrick Baumann | af9c3fe | 2019-08-12 08:13:32 -0700 | [diff] [blame] | 68 | |
Patrick Baumann | 40ed99a | 2019-08-12 09:43:10 -0700 | [diff] [blame] | 69 | // Logs all filtering instead of enforcing |
| 70 | private static final boolean DEBUG_ALLOW_ALL = false; |
Patrick Baumann | 57556b6 | 2020-01-29 07:23:39 -0800 | [diff] [blame] | 71 | private static final boolean DEBUG_LOGGING = false; |
| 72 | private static final boolean FEATURE_ENABLED_BY_DEFAULT = false; |
Patrick Baumann | 40ed99a | 2019-08-12 09:43:10 -0700 | [diff] [blame] | 73 | |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 74 | /** |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 75 | * This contains a list of app UIDs that are implicitly queryable because another app explicitly |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 76 | * interacted with it. For example, if application A starts a service in application B, |
| 77 | * application B is implicitly allowed to query for application A; regardless of any manifest |
| 78 | * entries. |
| 79 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 80 | private final SparseSetArray<Integer> mImplicitlyQueryable = new SparseSetArray<>(); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 83 | * A mapping from the set of App IDs that query other App IDs via package name to the |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 84 | * list of packages that they can see. |
| 85 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 86 | private final SparseSetArray<Integer> mQueriesViaPackage = new SparseSetArray<>(); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 89 | * A mapping from the set of App IDs that query others via intent to the list |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 90 | * of packages that the intents resolve to. |
| 91 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 92 | private final SparseSetArray<Integer> mQueriesViaIntent = new SparseSetArray<>(); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 95 | * A set of App IDs that are always queryable by any package, regardless of their manifest |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 96 | * content. |
| 97 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 98 | private final ArraySet<Integer> mForceQueryable = new ArraySet<>(); |
| 99 | |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 100 | /** |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 101 | * The set of package names provided by the device that should be force queryable regardless of |
| 102 | * their manifest contents. |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 103 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 104 | private final String[] mForceQueryableByDevicePackageNames; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 105 | |
| 106 | /** True if all system apps should be made queryable by default. */ |
| 107 | private final boolean mSystemAppsQueryable; |
| 108 | |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 109 | private final FeatureConfig mFeatureConfig; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 110 | |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 111 | private final OverlayReferenceMapper mOverlayReferenceMapper; |
Patrick Baumann | 051d75c | 2020-01-10 15:13:48 -0800 | [diff] [blame] | 112 | private PackageParser.SigningDetails mSystemSigningDetails; |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 113 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 114 | AppsFilter(FeatureConfig featureConfig, String[] forceQueryableWhitelist, |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 115 | boolean systemAppsQueryable, |
| 116 | @Nullable OverlayReferenceMapper.Provider overlayProvider) { |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 117 | mFeatureConfig = featureConfig; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 118 | mForceQueryableByDevicePackageNames = forceQueryableWhitelist; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 119 | mSystemAppsQueryable = systemAppsQueryable; |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 120 | mOverlayReferenceMapper = new OverlayReferenceMapper(true /*deferRebuild*/, |
| 121 | overlayProvider); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 124 | public interface FeatureConfig { |
| 125 | /** Called when the system is ready and components can be queried. */ |
| 126 | void onSystemReady(); |
Patrick Baumann | af9c3fe | 2019-08-12 08:13:32 -0700 | [diff] [blame] | 127 | |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 128 | /** @return true if we should filter apps at all. */ |
| 129 | boolean isGloballyEnabled(); |
| 130 | |
| 131 | /** @return true if the feature is enabled for the given package. */ |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 132 | boolean packageIsEnabled(AndroidPackage pkg); |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private static class FeatureConfigImpl implements FeatureConfig { |
| 136 | private static final String FILTERING_ENABLED_NAME = "package_query_filtering_enabled"; |
Anna Trostanetski | 66bb166 | 2019-10-29 16:44:25 +0000 | [diff] [blame] | 137 | private final PackageManagerService.Injector mInjector; |
Patrick Baumann | 57556b6 | 2020-01-29 07:23:39 -0800 | [diff] [blame] | 138 | private volatile boolean mFeatureEnabled = FEATURE_ENABLED_BY_DEFAULT; |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 139 | |
| 140 | private FeatureConfigImpl(PackageManagerService.Injector injector) { |
Anna Trostanetski | 66bb166 | 2019-10-29 16:44:25 +0000 | [diff] [blame] | 141 | mInjector = injector; |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onSystemReady() { |
| 146 | mFeatureEnabled = DeviceConfig.getBoolean( |
Patrick Baumann | 57556b6 | 2020-01-29 07:23:39 -0800 | [diff] [blame] | 147 | NAMESPACE_PACKAGE_MANAGER_SERVICE, FILTERING_ENABLED_NAME, |
| 148 | FEATURE_ENABLED_BY_DEFAULT); |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 149 | DeviceConfig.addOnPropertiesChangedListener( |
| 150 | NAMESPACE_PACKAGE_MANAGER_SERVICE, FgThread.getExecutor(), |
| 151 | properties -> { |
Linus Tufvesson | 87776d6 | 2019-12-05 16:15:10 +0000 | [diff] [blame] | 152 | if (properties.getKeyset().contains(FILTERING_ENABLED_NAME)) { |
| 153 | synchronized (FeatureConfigImpl.this) { |
| 154 | mFeatureEnabled = properties.getBoolean(FILTERING_ENABLED_NAME, |
Patrick Baumann | 57556b6 | 2020-01-29 07:23:39 -0800 | [diff] [blame] | 155 | FEATURE_ENABLED_BY_DEFAULT); |
Linus Tufvesson | 87776d6 | 2019-12-05 16:15:10 +0000 | [diff] [blame] | 156 | } |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 157 | } |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public boolean isGloballyEnabled() { |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 163 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "isGloballyEnabled"); |
| 164 | try { |
| 165 | return mFeatureEnabled; |
| 166 | } finally { |
| 167 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 168 | } |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 172 | public boolean packageIsEnabled(AndroidPackage pkg) { |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 173 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "packageIsEnabled"); |
| 174 | try { |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 175 | // TODO(b/135203078): Do not use toAppInfo |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 176 | return mInjector.getCompatibility().isChangeEnabled( |
Winson | 6571c8a | 2019-10-23 17:00:42 -0700 | [diff] [blame] | 177 | PackageManager.FILTER_APPLICATION_QUERY, pkg.toAppInfoWithoutState()); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 178 | } finally { |
| 179 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 180 | } |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 184 | public static AppsFilter create(PackageManagerService.Injector injector) { |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 185 | final boolean forceSystemAppsQueryable = |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 186 | injector.getContext().getResources() |
| 187 | .getBoolean(R.bool.config_forceSystemPackagesQueryable); |
| 188 | final FeatureConfig featureConfig = new FeatureConfigImpl(injector); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 189 | final String[] forcedQueryablePackageNames; |
| 190 | if (forceSystemAppsQueryable) { |
| 191 | // all system apps already queryable, no need to read and parse individual exceptions |
| 192 | forcedQueryablePackageNames = new String[]{}; |
| 193 | } else { |
| 194 | forcedQueryablePackageNames = |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 195 | injector.getContext().getResources() |
| 196 | .getStringArray(R.array.config_forceQueryablePackages); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 197 | for (int i = 0; i < forcedQueryablePackageNames.length; i++) { |
| 198 | forcedQueryablePackageNames[i] = forcedQueryablePackageNames[i].intern(); |
| 199 | } |
| 200 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 201 | return new AppsFilter(featureConfig, forcedQueryablePackageNames, |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 202 | forceSystemAppsQueryable, null); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** Returns true if the querying package may query for the potential target package */ |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 206 | private static boolean canQueryViaIntent(AndroidPackage querying, |
| 207 | AndroidPackage potentialTarget) { |
| 208 | if (querying.getQueriesIntents() == null) { |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 211 | for (Intent intent : querying.getQueriesIntents()) { |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 212 | if (matches(intent, potentialTarget)) { |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 213 | return true; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
Alan Stokes | a547c5c6 | 2020-01-20 11:36:08 +0000 | [diff] [blame] | 219 | private static boolean canQueryViaPackage(AndroidPackage querying, |
| 220 | AndroidPackage potentialTarget) { |
| 221 | return querying.getQueriesPackages() != null |
| 222 | && querying.getQueriesPackages().contains(potentialTarget.getPackageName()); |
| 223 | } |
| 224 | |
| 225 | private static boolean canQueryAsInstaller(PackageSetting querying, |
| 226 | AndroidPackage potentialTarget) { |
| 227 | final InstallSource installSource = querying.installSource; |
| 228 | if (potentialTarget.getPackageName().equals(installSource.installerPackageName)) { |
| 229 | return true; |
| 230 | } |
| 231 | if (!installSource.isInitiatingPackageUninstalled |
| 232 | && potentialTarget.getPackageName().equals(installSource.initiatingPackageName)) { |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 238 | private static boolean matches(Intent intent, AndroidPackage potentialTarget) { |
| 239 | for (int p = ArrayUtils.size(potentialTarget.getProviders()) - 1; p >= 0; p--) { |
| 240 | ParsedProvider provider = potentialTarget.getProviders().get(p); |
| 241 | if (!provider.isExported()) { |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 242 | continue; |
| 243 | } |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 244 | final Uri data = intent.getData(); |
Patrick Baumann | e2d9d71 | 2020-01-10 13:32:09 -0800 | [diff] [blame] | 245 | if (!"content".equalsIgnoreCase(intent.getScheme()) || data == null |
| 246 | || provider.getAuthority() == null) { |
| 247 | continue; |
| 248 | } |
| 249 | StringTokenizer authorities = new StringTokenizer(provider.getAuthority(), ";", false); |
| 250 | while (authorities.hasMoreElements()) { |
| 251 | if (Objects.equals(authorities.nextElement(), data.getAuthority())) { |
| 252 | return true; |
| 253 | } |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 256 | for (int s = ArrayUtils.size(potentialTarget.getServices()) - 1; s >= 0; s--) { |
| 257 | ParsedService service = potentialTarget.getServices().get(s); |
| 258 | if (!service.exported) { |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 259 | continue; |
| 260 | } |
| 261 | if (matchesAnyFilter(intent, service)) { |
| 262 | return true; |
| 263 | } |
| 264 | } |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 265 | for (int a = ArrayUtils.size(potentialTarget.getActivities()) - 1; a >= 0; a--) { |
| 266 | ParsedActivity activity = potentialTarget.getActivities().get(a); |
| 267 | if (!activity.exported) { |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 268 | continue; |
| 269 | } |
| 270 | if (matchesAnyFilter(intent, activity)) { |
| 271 | return true; |
| 272 | } |
| 273 | } |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 274 | for (int r = ArrayUtils.size(potentialTarget.getReceivers()) - 1; r >= 0; r--) { |
| 275 | ParsedActivity receiver = potentialTarget.getReceivers().get(r); |
| 276 | if (!receiver.exported) { |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 277 | continue; |
| 278 | } |
| 279 | if (matchesAnyFilter(intent, receiver)) { |
| 280 | return true; |
| 281 | } |
| 282 | } |
| 283 | return false; |
| 284 | } |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 285 | |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 286 | private static boolean matchesAnyFilter( |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 287 | Intent intent, ParsedComponent<? extends ParsedIntentInfo> component) { |
| 288 | List<? extends ParsedIntentInfo> intents = component.intents; |
| 289 | for (int i = ArrayUtils.size(intents) - 1; i >= 0; i--) { |
Patrick Baumann | 5863233 | 2019-10-09 10:12:10 -0700 | [diff] [blame] | 290 | IntentFilter intentFilter = intents.get(i); |
| 291 | if (intentFilter.match(intent.getAction(), intent.getType(), intent.getScheme(), |
| 292 | intent.getData(), intent.getCategories(), "AppsFilter") > 0) { |
| 293 | return true; |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | return false; |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Patrick Baumann | de37e43 | 2019-08-28 09:51:29 -0700 | [diff] [blame] | 300 | * Grants access based on an interaction between a calling and target package, granting |
| 301 | * visibility of the caller from the target. |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 302 | * |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 303 | * @param callingUid the uid initiating the interaction |
| 304 | * @param targetUid the uid being interacted with and thus gaining visibility of the |
| 305 | * initiating uid. |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 306 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 307 | public void grantImplicitAccess(int callingUid, int targetUid) { |
Patrick Baumann | dcf3d37 | 2020-01-16 15:22:22 -0800 | [diff] [blame] | 308 | if (targetUid != callingUid |
| 309 | && mImplicitlyQueryable.add(targetUid, callingUid) && DEBUG_LOGGING) { |
| 310 | Slog.wtf(TAG, "implicit access granted: " + targetUid + " -> " + callingUid); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 311 | } |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 314 | public void onSystemReady() { |
| 315 | mFeatureConfig.onSystemReady(); |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 316 | mOverlayReferenceMapper.rebuildIfDeferred(); |
Patrick Baumann | dfb121d | 2019-08-05 14:36:19 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Adds a package that should be considered when filtering visibility between apps. |
| 321 | * |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 322 | * @param newPkgSetting the new setting being added |
| 323 | * @param existingSettings all other settings currently on the device. |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 324 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 325 | public void addPackage(PackageSetting newPkgSetting, |
| 326 | ArrayMap<String, PackageSetting> existingSettings) { |
Patrick Baumann | 051d75c | 2020-01-10 15:13:48 -0800 | [diff] [blame] | 327 | if (Objects.equals("android", newPkgSetting.name)) { |
| 328 | // let's set aside the framework signatures |
| 329 | mSystemSigningDetails = newPkgSetting.signatures.mSigningDetails; |
| 330 | // and since we add overlays before we add the framework, let's revisit already added |
| 331 | // packages for signature matches |
| 332 | for (PackageSetting setting : existingSettings.values()) { |
| 333 | if (isSystemSigned(mSystemSigningDetails, setting)) { |
| 334 | mForceQueryable.add(setting.appId); |
| 335 | } |
| 336 | } |
| 337 | } |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 338 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "filter.addPackage"); |
| 339 | try { |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 340 | final AndroidPackage newPkg = newPkgSetting.pkg; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 341 | if (newPkg == null) { |
| 342 | // nothing to add |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | final boolean newIsForceQueryable = |
| 347 | mForceQueryable.contains(newPkgSetting.appId) |
| 348 | /* shared user that is already force queryable */ |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 349 | || newPkg.isForceQueryable() |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 350 | || (newPkgSetting.isSystem() && (mSystemAppsQueryable |
| 351 | || ArrayUtils.contains(mForceQueryableByDevicePackageNames, |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 352 | newPkg.getPackageName()))); |
Patrick Baumann | 051d75c | 2020-01-10 15:13:48 -0800 | [diff] [blame] | 353 | if (newIsForceQueryable |
| 354 | || (mSystemSigningDetails != null |
| 355 | && isSystemSigned(mSystemSigningDetails, newPkgSetting))) { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 356 | mForceQueryable.add(newPkgSetting.appId); |
| 357 | } |
| 358 | |
| 359 | for (int i = existingSettings.size() - 1; i >= 0; i--) { |
| 360 | final PackageSetting existingSetting = existingSettings.valueAt(i); |
| 361 | if (existingSetting.appId == newPkgSetting.appId || existingSetting.pkg == null) { |
| 362 | continue; |
| 363 | } |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 364 | final AndroidPackage existingPkg = existingSetting.pkg; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 365 | // let's evaluate the ability of already added packages to see this new package |
| 366 | if (!newIsForceQueryable) { |
| 367 | if (canQueryViaIntent(existingPkg, newPkg)) { |
| 368 | mQueriesViaIntent.add(existingSetting.appId, newPkgSetting.appId); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 369 | } |
Alan Stokes | a547c5c6 | 2020-01-20 11:36:08 +0000 | [diff] [blame] | 370 | if (canQueryViaPackage(existingPkg, newPkg) |
| 371 | || canQueryAsInstaller(existingSetting, newPkg)) { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 372 | mQueriesViaPackage.add(existingSetting.appId, newPkgSetting.appId); |
| 373 | } |
| 374 | } |
| 375 | // now we'll evaluate our new package's ability to see existing packages |
| 376 | if (!mForceQueryable.contains(existingSetting.appId)) { |
| 377 | if (canQueryViaIntent(newPkg, existingPkg)) { |
| 378 | mQueriesViaIntent.add(newPkgSetting.appId, existingSetting.appId); |
| 379 | } |
Alan Stokes | a547c5c6 | 2020-01-20 11:36:08 +0000 | [diff] [blame] | 380 | if (canQueryViaPackage(newPkg, existingPkg) |
| 381 | || canQueryAsInstaller(newPkgSetting, existingPkg)) { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 382 | mQueriesViaPackage.add(newPkgSetting.appId, existingSetting.appId); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 386 | |
| 387 | int existingSize = existingSettings.size(); |
| 388 | ArrayMap<String, AndroidPackage> existingPkgs = new ArrayMap<>(existingSize); |
| 389 | for (int index = 0; index < existingSize; index++) { |
| 390 | PackageSetting pkgSetting = existingSettings.valueAt(index); |
| 391 | if (pkgSetting.pkg != null) { |
| 392 | existingPkgs.put(pkgSetting.name, pkgSetting.pkg); |
| 393 | } |
| 394 | } |
| 395 | mOverlayReferenceMapper.addPkg(newPkgSetting.pkg, existingPkgs); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 396 | } finally { |
| 397 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 398 | } |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Patrick Baumann | 051d75c | 2020-01-10 15:13:48 -0800 | [diff] [blame] | 401 | private static boolean isSystemSigned(@NonNull PackageParser.SigningDetails sysSigningDetails, |
| 402 | PackageSetting pkgSetting) { |
| 403 | return pkgSetting.isSystem() |
| 404 | && pkgSetting.signatures.mSigningDetails.signaturesMatchExactly(sysSigningDetails); |
| 405 | } |
| 406 | |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 407 | /** |
| 408 | * Removes a package for consideration when filtering visibility between apps. |
| 409 | * |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 410 | * @param setting the setting of the package being removed. |
| 411 | * @param allUsers array of all current users on device. |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 412 | */ |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 413 | public void removePackage(PackageSetting setting, int[] allUsers, |
| 414 | ArrayMap<String, PackageSetting> existingSettings) { |
| 415 | mForceQueryable.remove(setting.appId); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 416 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 417 | for (int u = 0; u < allUsers.length; u++) { |
| 418 | final int userId = allUsers[u]; |
| 419 | final int removingUid = UserHandle.getUid(userId, setting.appId); |
| 420 | mImplicitlyQueryable.remove(removingUid); |
| 421 | for (int i = mImplicitlyQueryable.size() - 1; i >= 0; i--) { |
| 422 | mImplicitlyQueryable.remove(mImplicitlyQueryable.keyAt(i), removingUid); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 426 | mQueriesViaIntent.remove(setting.appId); |
| 427 | for (int i = mQueriesViaIntent.size() - 1; i >= 0; i--) { |
| 428 | mQueriesViaIntent.remove(mQueriesViaIntent.keyAt(i), setting.appId); |
| 429 | } |
| 430 | mQueriesViaPackage.remove(setting.appId); |
| 431 | for (int i = mQueriesViaPackage.size() - 1; i >= 0; i--) { |
| 432 | mQueriesViaPackage.remove(mQueriesViaPackage.keyAt(i), setting.appId); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 435 | // re-add other shared user members to re-establish visibility between them and other |
| 436 | // packages |
| 437 | if (setting.sharedUser != null) { |
| 438 | for (int i = setting.sharedUser.packages.size() - 1; i >= 0; i--) { |
| 439 | if (setting.sharedUser.packages.valueAt(i) == setting) { |
| 440 | continue; |
| 441 | } |
| 442 | addPackage(setting.sharedUser.packages.valueAt(i), existingSettings); |
| 443 | } |
| 444 | } |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 445 | |
| 446 | mOverlayReferenceMapper.removePkg(setting.name); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Returns true if the calling package should not be able to see the target package, false if no |
| 451 | * filtering should be done. |
| 452 | * |
| 453 | * @param callingUid the uid of the caller attempting to access a package |
| 454 | * @param callingSetting the setting attempting to access a package or null if it could not be |
| 455 | * found |
| 456 | * @param targetPkgSetting the package being accessed |
| 457 | * @param userId the user in which this access is being attempted |
| 458 | */ |
| 459 | public boolean shouldFilterApplication(int callingUid, @Nullable SettingBase callingSetting, |
| 460 | PackageSetting targetPkgSetting, int userId) { |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 461 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "shouldFilterApplication"); |
| 462 | try { |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 463 | if (!shouldFilterApplicationInternal(callingUid, callingSetting, targetPkgSetting, |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 464 | userId)) { |
| 465 | return false; |
| 466 | } |
| 467 | if (DEBUG_LOGGING) { |
| 468 | log(callingSetting, targetPkgSetting, |
| 469 | DEBUG_ALLOW_ALL ? "ALLOWED" : "BLOCKED", new RuntimeException()); |
| 470 | } |
| 471 | return !DEBUG_ALLOW_ALL; |
| 472 | } finally { |
| 473 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 474 | } |
| 475 | } |
| 476 | |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 477 | private boolean shouldFilterApplicationInternal(int callingUid, SettingBase callingSetting, |
| 478 | PackageSetting targetPkgSetting, int userId) { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 479 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "shouldFilterApplicationInternal"); |
| 480 | try { |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 481 | final boolean featureEnabled = mFeatureConfig.isGloballyEnabled(); |
| 482 | if (!featureEnabled) { |
| 483 | if (DEBUG_LOGGING) { |
| 484 | Slog.d(TAG, "filtering disabled; skipped"); |
| 485 | } |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 486 | return false; |
| 487 | } |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 488 | if (callingUid < Process.FIRST_APPLICATION_UID) { |
| 489 | if (DEBUG_LOGGING) { |
| 490 | Slog.d(TAG, "filtering skipped; " + callingUid + " is system"); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 491 | } |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 492 | return false; |
| 493 | } |
| 494 | if (callingSetting == null) { |
| 495 | Slog.wtf(TAG, "No setting found for non system uid " + callingUid); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 496 | return true; |
| 497 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 498 | final PackageSetting callingPkgSetting; |
| 499 | final ArraySet<PackageSetting> callingSharedPkgSettings; |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 500 | Trace.beginSection("callingSetting instanceof"); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 501 | if (callingSetting instanceof PackageSetting) { |
| 502 | callingPkgSetting = (PackageSetting) callingSetting; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 503 | callingSharedPkgSettings = null; |
| 504 | } else { |
| 505 | callingPkgSetting = null; |
| 506 | callingSharedPkgSettings = ((SharedUserSetting) callingSetting).packages; |
| 507 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 508 | Trace.endSection(); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 509 | |
| 510 | if (callingPkgSetting != null) { |
| 511 | if (!mFeatureConfig.packageIsEnabled(callingPkgSetting.pkg)) { |
| 512 | if (DEBUG_LOGGING) { |
| 513 | log(callingSetting, targetPkgSetting, "DISABLED"); |
| 514 | } |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 515 | return false; |
| 516 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 517 | } else { |
| 518 | for (int i = callingSharedPkgSettings.size() - 1; i >= 0; i--) { |
| 519 | if (!mFeatureConfig.packageIsEnabled(callingSharedPkgSettings.valueAt(i).pkg)) { |
| 520 | if (DEBUG_LOGGING) { |
| 521 | log(callingSetting, targetPkgSetting, "DISABLED"); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 522 | } |
Patrick Baumann | a72e674 | 2019-11-06 08:26:56 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | } |
Patrick Baumann | a72e674 | 2019-11-06 08:26:56 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 528 | // This package isn't technically installed and won't be written to settings, so we can |
| 529 | // treat it as filtered until it's available again. |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 530 | final AndroidPackage targetPkg = targetPkgSetting.pkg; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 531 | if (targetPkg == null) { |
| 532 | if (DEBUG_LOGGING) { |
| 533 | Slog.wtf(TAG, "shouldFilterApplication: " + "targetPkg is null"); |
| 534 | } |
| 535 | return true; |
| 536 | } |
Patrick Baumann | 71bb3f3 | 2020-01-15 15:36:57 -0800 | [diff] [blame] | 537 | if (targetPkg.isStaticSharedLibrary()) { |
| 538 | // not an app, this filtering takes place at a higher level |
| 539 | return false; |
| 540 | } |
Winson | 655a5b9 | 2019-10-23 10:49:32 -0700 | [diff] [blame] | 541 | final String targetName = targetPkg.getPackageName(); |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 542 | Trace.beginSection("getAppId"); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 543 | final int callingAppId; |
| 544 | if (callingPkgSetting != null) { |
| 545 | callingAppId = callingPkgSetting.appId; |
| 546 | } else { |
| 547 | callingAppId = callingSharedPkgSettings.valueAt(0).appId; // all should be the same |
| 548 | } |
| 549 | final int targetAppId = targetPkgSetting.appId; |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 550 | Trace.endSection(); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 551 | if (callingAppId == targetAppId) { |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 552 | if (DEBUG_LOGGING) { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 553 | log(callingSetting, targetPkgSetting, "same app id"); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 554 | } |
| 555 | return false; |
| 556 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 557 | |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 558 | try { |
| 559 | Trace.beginSection("hasPermission"); |
| 560 | if (callingSetting.getPermissionsState().hasPermission( |
| 561 | Manifest.permission.QUERY_ALL_PACKAGES, UserHandle.getUserId(callingUid))) { |
| 562 | if (DEBUG_LOGGING) { |
| 563 | log(callingSetting, targetPkgSetting, "has query-all permission"); |
| 564 | } |
| 565 | return false; |
Patrick Baumann | a72e674 | 2019-11-06 08:26:56 -0800 | [diff] [blame] | 566 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 567 | } finally { |
| 568 | Trace.endSection(); |
Patrick Baumann | a72e674 | 2019-11-06 08:26:56 -0800 | [diff] [blame] | 569 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 570 | try { |
| 571 | Trace.beginSection("mForceQueryable"); |
| 572 | if (mForceQueryable.contains(targetAppId)) { |
| 573 | if (DEBUG_LOGGING) { |
| 574 | log(callingSetting, targetPkgSetting, "force queryable"); |
| 575 | } |
| 576 | return false; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 577 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 578 | } finally { |
| 579 | Trace.endSection(); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 580 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 581 | try { |
| 582 | Trace.beginSection("mQueriesViaPackage"); |
| 583 | if (mQueriesViaPackage.contains(callingAppId, targetAppId)) { |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 584 | if (DEBUG_LOGGING) { |
| 585 | log(callingSetting, targetPkgSetting, "queries package"); |
| 586 | } |
| 587 | return false; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 588 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 589 | } finally { |
| 590 | Trace.endSection(); |
| 591 | } |
| 592 | try { |
| 593 | Trace.beginSection("mQueriesViaIntent"); |
| 594 | if (mQueriesViaIntent.contains(callingAppId, targetAppId)) { |
| 595 | if (DEBUG_LOGGING) { |
| 596 | log(callingSetting, targetPkgSetting, "queries intent"); |
| 597 | } |
| 598 | return false; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 599 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 600 | } finally { |
| 601 | Trace.endSection(); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 602 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 603 | |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 604 | try { |
| 605 | Trace.beginSection("mImplicitlyQueryable"); |
| 606 | final int targetUid = UserHandle.getUid(userId, targetAppId); |
| 607 | if (mImplicitlyQueryable.contains(callingUid, targetUid)) { |
| 608 | if (DEBUG_LOGGING) { |
| 609 | log(callingSetting, targetPkgSetting, "implicitly queryable for user"); |
| 610 | } |
| 611 | return false; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 612 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 613 | } finally { |
| 614 | Trace.endSection(); |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 615 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 616 | if (callingPkgSetting != null) { |
| 617 | if (callingPkgInstruments(callingPkgSetting, targetPkgSetting, targetName)) { |
| 618 | return false; |
| 619 | } |
| 620 | } else { |
| 621 | for (int i = callingSharedPkgSettings.size() - 1; i >= 0; i--) { |
| 622 | if (callingPkgInstruments(callingSharedPkgSettings.valueAt(i), |
| 623 | targetPkgSetting, targetName)) { |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | } |
Winson Chiu | 2fdaf81 | 2019-12-13 20:01:15 +0000 | [diff] [blame] | 628 | |
| 629 | if (callingSharedPkgSettings != null) { |
| 630 | int size = callingSharedPkgSettings.size(); |
| 631 | for (int index = 0; index < size; index++) { |
| 632 | PackageSetting pkgSetting = callingSharedPkgSettings.valueAt(index); |
| 633 | if (mOverlayReferenceMapper.isValidActor(targetName, pkgSetting.name)) { |
| 634 | if (DEBUG_LOGGING) { |
| 635 | log(callingPkgSetting, targetPkgSetting, |
| 636 | "matches shared user of package that acts on target of " |
| 637 | + "overlay"); |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | } else { |
| 643 | if (mOverlayReferenceMapper.isValidActor(targetName, callingPkgSetting.name)) { |
| 644 | if (DEBUG_LOGGING) { |
| 645 | log(callingPkgSetting, targetPkgSetting, "acts on target of overlay"); |
| 646 | } |
| 647 | return false; |
| 648 | } |
| 649 | } |
| 650 | |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 651 | return true; |
Patrick Baumann | 88318e0 | 2019-11-04 14:22:25 -0800 | [diff] [blame] | 652 | } finally { |
| 653 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 654 | } |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 657 | private static boolean callingPkgInstruments(PackageSetting callingPkgSetting, |
| 658 | PackageSetting targetPkgSetting, |
| 659 | String targetName) { |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 660 | try { |
| 661 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "callingPkgInstruments"); |
| 662 | final List<ComponentParseUtils.ParsedInstrumentation> inst = |
| 663 | callingPkgSetting.pkg.getInstrumentations(); |
| 664 | for (int i = ArrayUtils.size(inst) - 1; i >= 0; i--) { |
| 665 | if (Objects.equals(inst.get(i).getTargetPackage(), targetName)) { |
| 666 | if (DEBUG_LOGGING) { |
| 667 | log(callingPkgSetting, targetPkgSetting, "instrumentation"); |
| 668 | } |
| 669 | return true; |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 670 | } |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 671 | } |
Patrick Baumann | 2eebc9e | 2019-11-22 09:29:09 -0800 | [diff] [blame] | 672 | return false; |
| 673 | } finally { |
| 674 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 675 | } |
Patrick Baumann | 0c9257b | 2019-08-20 12:50:37 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 678 | private static void log(SettingBase callingPkgSetting, PackageSetting targetPkgSetting, |
| 679 | String description) { |
| 680 | log(callingPkgSetting, targetPkgSetting, description, null); |
| 681 | } |
| 682 | |
| 683 | private static void log(SettingBase callingPkgSetting, PackageSetting targetPkgSetting, |
| 684 | String description, Throwable throwable) { |
| 685 | Slog.wtf(TAG, |
Patrick Baumann | e2d9d71 | 2020-01-10 13:32:09 -0800 | [diff] [blame] | 686 | "interaction: " + callingPkgSetting |
Patrick Baumann | efb3736 | 2020-01-14 14:49:07 -0800 | [diff] [blame] | 687 | + " -> " + targetPkgSetting + " " |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 688 | + description, throwable); |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 691 | public void dumpQueries( |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 692 | PrintWriter pw, PackageManagerService pms, @Nullable Integer filteringAppId, |
| 693 | DumpState dumpState, |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 694 | int[] users) { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 695 | final SparseArray<String> cache = new SparseArray<>(); |
| 696 | ToString<Integer> expandPackages = input -> { |
| 697 | String cachedValue = cache.get(input); |
| 698 | if (cachedValue == null) { |
| 699 | final String[] packagesForUid = pms.getPackagesForUid(input); |
| 700 | if (packagesForUid == null) { |
| 701 | cachedValue = "[unknown app id " + input + "]"; |
| 702 | } else { |
| 703 | cachedValue = packagesForUid.length == 1 ? packagesForUid[0] |
| 704 | : "[" + TextUtils.join(",", packagesForUid) + "]"; |
| 705 | } |
| 706 | cache.put(input, cachedValue); |
| 707 | } |
| 708 | return cachedValue; |
| 709 | }; |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 710 | pw.println(); |
| 711 | pw.println("Queries:"); |
| 712 | dumpState.onTitlePrinted(); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 713 | if (!mFeatureConfig.isGloballyEnabled()) { |
| 714 | pw.println(" DISABLED"); |
| 715 | if (!DEBUG_LOGGING) { |
| 716 | return; |
| 717 | } |
| 718 | } |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 719 | pw.println(" system apps queryable: " + mSystemAppsQueryable); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 720 | dumpPackageSet(pw, filteringAppId, mForceQueryable, "forceQueryable", " ", expandPackages); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 721 | pw.println(" queries via package name:"); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 722 | dumpQueriesMap(pw, filteringAppId, mQueriesViaPackage, " ", expandPackages); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 723 | pw.println(" queries via intent:"); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 724 | dumpQueriesMap(pw, filteringAppId, mQueriesViaIntent, " ", expandPackages); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 725 | pw.println(" queryable via interaction:"); |
| 726 | for (int user : users) { |
| 727 | pw.append(" User ").append(Integer.toString(user)).println(":"); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 728 | dumpQueriesMap(pw, |
| 729 | filteringAppId == null ? null : UserHandle.getUid(user, filteringAppId), |
| 730 | mImplicitlyQueryable, " ", expandPackages); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 734 | private static void dumpQueriesMap(PrintWriter pw, @Nullable Integer filteringId, |
| 735 | SparseSetArray<Integer> queriesMap, String spacing, |
| 736 | @Nullable ToString<Integer> toString) { |
| 737 | for (int i = 0; i < queriesMap.size(); i++) { |
| 738 | Integer callingId = queriesMap.keyAt(i); |
| 739 | if (Objects.equals(callingId, filteringId)) { |
| 740 | // don't filter target package names if the calling is filteringId |
| 741 | dumpPackageSet( |
| 742 | pw, null /*filteringId*/, queriesMap.get(callingId), |
| 743 | toString == null |
| 744 | ? callingId.toString() |
| 745 | : toString.toString(callingId), |
| 746 | spacing, toString); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 747 | } else { |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 748 | dumpPackageSet( |
| 749 | pw, filteringId, queriesMap.get(callingId), |
| 750 | toString == null |
| 751 | ? callingId.toString() |
| 752 | : toString.toString(callingId), |
| 753 | spacing, toString); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 758 | private interface ToString<T> { |
| 759 | String toString(T input); |
| 760 | } |
| 761 | |
| 762 | private static <T> void dumpPackageSet(PrintWriter pw, @Nullable T filteringId, |
| 763 | Set<T> targetPkgSet, String subTitle, String spacing, |
| 764 | @Nullable ToString<T> toString) { |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 765 | if (targetPkgSet != null && targetPkgSet.size() > 0 |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 766 | && (filteringId == null || targetPkgSet.contains(filteringId))) { |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 767 | pw.append(spacing).append(subTitle).println(":"); |
Patrick Baumann | b9a49f4 | 2019-11-13 16:42:18 -0800 | [diff] [blame] | 768 | for (T item : targetPkgSet) { |
| 769 | if (filteringId == null || Objects.equals(filteringId, item)) { |
| 770 | pw.append(spacing).append(" ") |
| 771 | .println(toString == null ? item : toString.toString(item)); |
Patrick Baumann | a7ad66d | 2019-08-12 11:11:08 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
Patrick Baumann | 6c1c809 | 2019-06-27 14:55:44 -0700 | [diff] [blame] | 776 | } |