Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.firewall; |
| 18 | |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 19 | import android.app.AppGlobals; |
| 20 | import android.content.ComponentName; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
Ben Gruver | b622379 | 2013-07-29 16:35:40 -0700 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 24 | import android.content.pm.IPackageManager; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
| 26 | import android.os.Environment; |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 27 | import android.os.FileObserver; |
| 28 | import android.os.Handler; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 29 | import android.os.Looper; |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 30 | import android.os.Message; |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 32 | import android.util.ArrayMap; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 33 | import android.util.Slog; |
| 34 | import android.util.Xml; |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 35 | import com.android.internal.util.ArrayUtils; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 36 | import com.android.internal.util.XmlUtils; |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 37 | import com.android.server.EventLogTags; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 38 | import com.android.server.IntentResolver; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 39 | import org.xmlpull.v1.XmlPullParser; |
| 40 | import org.xmlpull.v1.XmlPullParserException; |
| 41 | |
| 42 | import java.io.File; |
| 43 | import java.io.FileInputStream; |
| 44 | import java.io.FileNotFoundException; |
| 45 | import java.io.IOException; |
| 46 | import java.util.ArrayList; |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 47 | import java.util.Arrays; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 48 | import java.util.HashMap; |
| 49 | import java.util.List; |
| 50 | |
| 51 | public class IntentFirewall { |
Ben Gruver | dd72c9e | 2013-08-06 12:34:17 -0700 | [diff] [blame] | 52 | static final String TAG = "IntentFirewall"; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 53 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 54 | // e.g. /data/system/ifw or /data/secure/system/ifw |
| 55 | private static final File RULES_DIR = new File(Environment.getSystemSecureDirectory(), "ifw"); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 56 | |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 57 | private static final int LOG_PACKAGES_MAX_LENGTH = 150; |
| 58 | private static final int LOG_PACKAGES_SUFFICIENT_LENGTH = 125; |
| 59 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 60 | private static final String TAG_RULES = "rules"; |
| 61 | private static final String TAG_ACTIVITY = "activity"; |
| 62 | private static final String TAG_SERVICE = "service"; |
| 63 | private static final String TAG_BROADCAST = "broadcast"; |
| 64 | |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 65 | private static final int TYPE_ACTIVITY = 0; |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 66 | private static final int TYPE_BROADCAST = 1; |
| 67 | private static final int TYPE_SERVICE = 2; |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 68 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 69 | private static final HashMap<String, FilterFactory> factoryMap; |
| 70 | |
| 71 | private final AMSInterface mAms; |
| 72 | |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 73 | private final RuleObserver mObserver; |
| 74 | |
| 75 | private FirewallIntentResolver mActivityResolver = new FirewallIntentResolver(); |
| 76 | private FirewallIntentResolver mBroadcastResolver = new FirewallIntentResolver(); |
| 77 | private FirewallIntentResolver mServiceResolver = new FirewallIntentResolver(); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 78 | |
| 79 | static { |
| 80 | FilterFactory[] factories = new FilterFactory[] { |
| 81 | AndFilter.FACTORY, |
| 82 | OrFilter.FACTORY, |
| 83 | NotFilter.FACTORY, |
| 84 | |
| 85 | StringFilter.ACTION, |
| 86 | StringFilter.COMPONENT, |
| 87 | StringFilter.COMPONENT_NAME, |
| 88 | StringFilter.COMPONENT_PACKAGE, |
| 89 | StringFilter.DATA, |
| 90 | StringFilter.HOST, |
| 91 | StringFilter.MIME_TYPE, |
Ben Gruver | 57e76b4 | 2013-07-31 15:41:24 -0700 | [diff] [blame] | 92 | StringFilter.SCHEME, |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 93 | StringFilter.PATH, |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 94 | StringFilter.SSP, |
| 95 | |
| 96 | CategoryFilter.FACTORY, |
| 97 | SenderFilter.FACTORY, |
| 98 | SenderPermissionFilter.FACTORY, |
| 99 | PortFilter.FACTORY |
| 100 | }; |
| 101 | |
| 102 | // load factor ~= .75 |
| 103 | factoryMap = new HashMap<String, FilterFactory>(factories.length * 4 / 3); |
| 104 | for (int i=0; i<factories.length; i++) { |
| 105 | FilterFactory factory = factories[i]; |
| 106 | factoryMap.put(factory.getTagName(), factory); |
| 107 | } |
| 108 | } |
| 109 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 110 | public IntentFirewall(AMSInterface ams, Handler handler) { |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 111 | mAms = ams; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 112 | mHandler = new FirewallHandler(handler.getLooper()); |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 113 | File rulesDir = getRulesDir(); |
| 114 | rulesDir.mkdirs(); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 115 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 116 | readRulesDir(rulesDir); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 117 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 118 | mObserver = new RuleObserver(rulesDir); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 119 | mObserver.startWatching(); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 122 | /** |
| 123 | * This is called from ActivityManager to check if a start activity intent should be allowed. |
| 124 | * It is assumed the caller is already holding the global ActivityManagerService lock. |
| 125 | */ |
Ben Gruver | dd72c9e | 2013-08-06 12:34:17 -0700 | [diff] [blame] | 126 | public boolean checkStartActivity(Intent intent, int callerUid, int callerPid, |
Ben Gruver | b622379 | 2013-07-29 16:35:40 -0700 | [diff] [blame] | 127 | String resolvedType, ApplicationInfo resolvedApp) { |
Ben Gruver | f5323fe | 2013-07-31 15:09:51 -0700 | [diff] [blame] | 128 | return checkIntent(mActivityResolver, intent.getComponent(), TYPE_ACTIVITY, intent, |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 129 | callerUid, callerPid, resolvedType, resolvedApp.uid); |
Ben Gruver | b622379 | 2013-07-29 16:35:40 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ben Gruver | f5323fe | 2013-07-31 15:09:51 -0700 | [diff] [blame] | 132 | public boolean checkService(ComponentName resolvedService, Intent intent, int callerUid, |
| 133 | int callerPid, String resolvedType, ApplicationInfo resolvedApp) { |
| 134 | return checkIntent(mServiceResolver, resolvedService, TYPE_SERVICE, intent, callerUid, |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 135 | callerPid, resolvedType, resolvedApp.uid); |
| 136 | } |
| 137 | |
| 138 | public boolean checkBroadcast(Intent intent, int callerUid, int callerPid, |
| 139 | String resolvedType, int receivingUid) { |
| 140 | return checkIntent(mBroadcastResolver, intent.getComponent(), TYPE_BROADCAST, intent, |
| 141 | callerUid, callerPid, resolvedType, receivingUid); |
Ben Gruver | f5323fe | 2013-07-31 15:09:51 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public boolean checkIntent(FirewallIntentResolver resolver, ComponentName resolvedComponent, |
| 145 | int intentType, Intent intent, int callerUid, int callerPid, String resolvedType, |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 146 | int receivingUid) { |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 147 | boolean log = false; |
| 148 | boolean block = false; |
| 149 | |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 150 | // For the first pass, find all the rules that have at least one intent-filter or |
| 151 | // component-filter that matches this intent |
| 152 | List<Rule> candidateRules; |
| 153 | candidateRules = resolver.queryIntent(intent, resolvedType, false, 0); |
| 154 | if (candidateRules == null) { |
| 155 | candidateRules = new ArrayList<Rule>(); |
| 156 | } |
| 157 | resolver.queryByComponent(resolvedComponent, candidateRules); |
| 158 | |
| 159 | // For the second pass, try to match the potentially more specific conditions in each |
| 160 | // rule against the intent |
| 161 | for (int i=0; i<candidateRules.size(); i++) { |
| 162 | Rule rule = candidateRules.get(i); |
Ben Gruver | f5323fe | 2013-07-31 15:09:51 -0700 | [diff] [blame] | 163 | if (rule.matches(this, resolvedComponent, intent, callerUid, callerPid, resolvedType, |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 164 | receivingUid)) { |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 165 | block |= rule.getBlock(); |
| 166 | log |= rule.getLog(); |
| 167 | |
| 168 | // if we've already determined that we should both block and log, there's no need |
| 169 | // to continue trying rules |
| 170 | if (block && log) { |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (log) { |
Ben Gruver | b622379 | 2013-07-29 16:35:40 -0700 | [diff] [blame] | 177 | logIntent(intentType, intent, callerUid, resolvedType); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return !block; |
| 181 | } |
| 182 | |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 183 | private static void logIntent(int intentType, Intent intent, int callerUid, |
| 184 | String resolvedType) { |
| 185 | // The component shouldn't be null, but let's double check just to be safe |
| 186 | ComponentName cn = intent.getComponent(); |
| 187 | String shortComponent = null; |
| 188 | if (cn != null) { |
| 189 | shortComponent = cn.flattenToShortString(); |
| 190 | } |
| 191 | |
| 192 | String callerPackages = null; |
| 193 | int callerPackageCount = 0; |
| 194 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 195 | if (pm != null) { |
| 196 | try { |
| 197 | String[] callerPackagesArray = pm.getPackagesForUid(callerUid); |
| 198 | if (callerPackagesArray != null) { |
| 199 | callerPackageCount = callerPackagesArray.length; |
| 200 | callerPackages = joinPackages(callerPackagesArray); |
| 201 | } |
| 202 | } catch (RemoteException ex) { |
| 203 | Slog.e(TAG, "Remote exception while retrieving packages", ex); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | EventLogTags.writeIfwIntentMatched(intentType, shortComponent, callerUid, |
| 208 | callerPackageCount, callerPackages, intent.getAction(), resolvedType, |
| 209 | intent.getDataString(), intent.getFlags()); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Joins a list of package names such that the resulting string is no more than |
| 214 | * LOG_PACKAGES_MAX_LENGTH. |
| 215 | * |
| 216 | * Only full package names will be added to the result, unless every package is longer than the |
| 217 | * limit, in which case one of the packages will be truncated and added. In this case, an |
| 218 | * additional '-' character will be added to the end of the string, to denote the truncation. |
| 219 | * |
| 220 | * If it encounters a package that won't fit in the remaining space, it will continue on to the |
| 221 | * next package, unless the total length of the built string so far is greater than |
| 222 | * LOG_PACKAGES_SUFFICIENT_LENGTH, in which case it will stop and return what it has. |
| 223 | */ |
| 224 | private static String joinPackages(String[] packages) { |
| 225 | boolean first = true; |
| 226 | StringBuilder sb = new StringBuilder(); |
| 227 | for (int i=0; i<packages.length; i++) { |
| 228 | String pkg = packages[i]; |
| 229 | |
| 230 | // + 1 length for the comma. This logic technically isn't correct for the first entry, |
| 231 | // but it's not critical. |
| 232 | if (sb.length() + pkg.length() + 1 < LOG_PACKAGES_MAX_LENGTH) { |
| 233 | if (!first) { |
| 234 | sb.append(','); |
| 235 | } else { |
| 236 | first = false; |
| 237 | } |
| 238 | sb.append(pkg); |
| 239 | } else if (sb.length() >= LOG_PACKAGES_SUFFICIENT_LENGTH) { |
| 240 | return sb.toString(); |
| 241 | } |
| 242 | } |
| 243 | if (sb.length() == 0 && packages.length > 0) { |
| 244 | String pkg = packages[0]; |
| 245 | // truncating from the end - the last part of the package name is more likely to be |
| 246 | // interesting/unique |
| 247 | return pkg.substring(pkg.length() - LOG_PACKAGES_MAX_LENGTH + 1) + '-'; |
| 248 | } |
| 249 | return null; |
| 250 | } |
| 251 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 252 | public static File getRulesDir() { |
| 253 | return RULES_DIR; |
Ben Gruver | 633dc9b | 2013-04-04 12:05:49 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 256 | /** |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 257 | * Reads rules from all xml files (*.xml) in the given directory, and replaces our set of rules |
| 258 | * with the newly read rules. |
| 259 | * |
| 260 | * We only check for files ending in ".xml", to allow for temporary files that are atomically |
| 261 | * renamed to .xml |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 262 | * |
| 263 | * All calls to this method from the file observer come through a handler and are inherently |
| 264 | * serialized |
| 265 | */ |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 266 | private void readRulesDir(File rulesDir) { |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 267 | FirewallIntentResolver[] resolvers = new FirewallIntentResolver[3]; |
| 268 | for (int i=0; i<resolvers.length; i++) { |
| 269 | resolvers[i] = new FirewallIntentResolver(); |
| 270 | } |
| 271 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 272 | File[] files = rulesDir.listFiles(); |
Ben Gruver | 248688a | 2013-09-30 17:03:34 -0700 | [diff] [blame] | 273 | if (files != null) { |
| 274 | for (int i=0; i<files.length; i++) { |
| 275 | File file = files[i]; |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 276 | |
Ben Gruver | 248688a | 2013-09-30 17:03:34 -0700 | [diff] [blame] | 277 | if (file.getName().endsWith(".xml")) { |
| 278 | readRules(file, resolvers); |
| 279 | } |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | Slog.i(TAG, "Read new rules (A:" + resolvers[TYPE_ACTIVITY].filterSet().size() + |
| 284 | " B:" + resolvers[TYPE_BROADCAST].filterSet().size() + |
| 285 | " S:" + resolvers[TYPE_SERVICE].filterSet().size() + ")"); |
| 286 | |
| 287 | synchronized (mAms.getAMSLock()) { |
| 288 | mActivityResolver = resolvers[TYPE_ACTIVITY]; |
| 289 | mBroadcastResolver = resolvers[TYPE_BROADCAST]; |
| 290 | mServiceResolver = resolvers[TYPE_SERVICE]; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Reads rules from the given file and add them to the given resolvers |
| 296 | */ |
| 297 | private void readRules(File rulesFile, FirewallIntentResolver[] resolvers) { |
| 298 | // some temporary lists to hold the rules while we parse the xml file, so that we can |
| 299 | // add the rules all at once, after we know there weren't any major structural problems |
| 300 | // with the xml file |
| 301 | List<List<Rule>> rulesByType = new ArrayList<List<Rule>>(3); |
| 302 | for (int i=0; i<3; i++) { |
| 303 | rulesByType.add(new ArrayList<Rule>()); |
| 304 | } |
| 305 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 306 | FileInputStream fis; |
| 307 | try { |
| 308 | fis = new FileInputStream(rulesFile); |
| 309 | } catch (FileNotFoundException ex) { |
| 310 | // Nope, no rules. Nothing else to do! |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | try { |
| 315 | XmlPullParser parser = Xml.newPullParser(); |
| 316 | |
| 317 | parser.setInput(fis, null); |
| 318 | |
| 319 | XmlUtils.beginDocument(parser, TAG_RULES); |
| 320 | |
| 321 | int outerDepth = parser.getDepth(); |
| 322 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 323 | int ruleType = -1; |
| 324 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 325 | String tagName = parser.getName(); |
| 326 | if (tagName.equals(TAG_ACTIVITY)) { |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 327 | ruleType = TYPE_ACTIVITY; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 328 | } else if (tagName.equals(TAG_BROADCAST)) { |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 329 | ruleType = TYPE_BROADCAST; |
| 330 | } else if (tagName.equals(TAG_SERVICE)) { |
| 331 | ruleType = TYPE_SERVICE; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 334 | if (ruleType != -1) { |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 335 | Rule rule = new Rule(); |
| 336 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 337 | List<Rule> rules = rulesByType.get(ruleType); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 338 | |
| 339 | // if we get an error while parsing a particular rule, we'll just ignore |
| 340 | // that rule and continue on with the next rule |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 341 | try { |
| 342 | rule.readFromXml(parser); |
| 343 | } catch (XmlPullParserException ex) { |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 344 | Slog.e(TAG, "Error reading an intent firewall rule from " + rulesFile, ex); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 345 | continue; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 348 | rules.add(rule); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } catch (XmlPullParserException ex) { |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 352 | // if there was an error outside of a specific rule, then there are probably |
| 353 | // structural problems with the xml file, and we should completely ignore it |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 354 | Slog.e(TAG, "Error reading intent firewall rules from " + rulesFile, ex); |
| 355 | return; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 356 | } catch (IOException ex) { |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 357 | Slog.e(TAG, "Error reading intent firewall rules from " + rulesFile, ex); |
| 358 | return; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 359 | } finally { |
| 360 | try { |
| 361 | fis.close(); |
| 362 | } catch (IOException ex) { |
| 363 | Slog.e(TAG, "Error while closing " + rulesFile, ex); |
| 364 | } |
| 365 | } |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 366 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 367 | for (int ruleType=0; ruleType<rulesByType.size(); ruleType++) { |
| 368 | List<Rule> rules = rulesByType.get(ruleType); |
| 369 | FirewallIntentResolver resolver = resolvers[ruleType]; |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 370 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 371 | for (int ruleIndex=0; ruleIndex<rules.size(); ruleIndex++) { |
| 372 | Rule rule = rules.get(ruleIndex); |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 373 | for (int i=0; i<rule.getIntentFilterCount(); i++) { |
| 374 | resolver.addFilter(rule.getIntentFilter(i)); |
| 375 | } |
| 376 | for (int i=0; i<rule.getComponentFilterCount(); i++) { |
| 377 | resolver.addComponentFilter(rule.getComponentFilter(i), rule); |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 383 | static Filter parseFilter(XmlPullParser parser) throws IOException, XmlPullParserException { |
| 384 | String elementName = parser.getName(); |
| 385 | |
| 386 | FilterFactory factory = factoryMap.get(elementName); |
| 387 | |
| 388 | if (factory == null) { |
| 389 | throw new XmlPullParserException("Unknown element in filter list: " + elementName); |
| 390 | } |
| 391 | return factory.newFilter(parser); |
| 392 | } |
| 393 | |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 394 | /** |
| 395 | * Represents a single activity/service/broadcast rule within one of the xml files. |
| 396 | * |
| 397 | * Rules are matched against an incoming intent in two phases. The goal of the first phase |
| 398 | * is to select a subset of rules that might match a given intent. |
| 399 | * |
| 400 | * For the first phase, we use a combination of intent filters (via an IntentResolver) |
| 401 | * and component filters to select which rules to check. If a rule has multiple intent or |
| 402 | * component filters, only a single filter must match for the rule to be passed on to the |
| 403 | * second phase. |
| 404 | * |
| 405 | * In the second phase, we check the specific conditions in each rule against the values in the |
| 406 | * intent. All top level conditions (but not filters) in the rule must match for the rule as a |
| 407 | * whole to match. |
| 408 | * |
| 409 | * If the rule matches, then we block or log the intent, as specified by the rule. If multiple |
| 410 | * rules match, we combine the block/log flags from any matching rule. |
| 411 | */ |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 412 | private static class Rule extends AndFilter { |
| 413 | private static final String TAG_INTENT_FILTER = "intent-filter"; |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 414 | private static final String TAG_COMPONENT_FILTER = "component-filter"; |
| 415 | private static final String ATTR_NAME = "name"; |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 416 | |
| 417 | private static final String ATTR_BLOCK = "block"; |
| 418 | private static final String ATTR_LOG = "log"; |
| 419 | |
| 420 | private final ArrayList<FirewallIntentFilter> mIntentFilters = |
| 421 | new ArrayList<FirewallIntentFilter>(1); |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 422 | private final ArrayList<ComponentName> mComponentFilters = new ArrayList<ComponentName>(0); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 423 | private boolean block; |
| 424 | private boolean log; |
| 425 | |
| 426 | @Override |
| 427 | public Rule readFromXml(XmlPullParser parser) throws IOException, XmlPullParserException { |
| 428 | block = Boolean.parseBoolean(parser.getAttributeValue(null, ATTR_BLOCK)); |
| 429 | log = Boolean.parseBoolean(parser.getAttributeValue(null, ATTR_LOG)); |
| 430 | |
| 431 | super.readFromXml(parser); |
| 432 | return this; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | protected void readChild(XmlPullParser parser) throws IOException, XmlPullParserException { |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 437 | String currentTag = parser.getName(); |
| 438 | |
| 439 | if (currentTag.equals(TAG_INTENT_FILTER)) { |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 440 | FirewallIntentFilter intentFilter = new FirewallIntentFilter(this); |
| 441 | intentFilter.readFromXml(parser); |
| 442 | mIntentFilters.add(intentFilter); |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 443 | } else if (currentTag.equals(TAG_COMPONENT_FILTER)) { |
| 444 | String componentStr = parser.getAttributeValue(null, ATTR_NAME); |
| 445 | if (componentStr == null) { |
| 446 | throw new XmlPullParserException("Component name must be specified.", |
| 447 | parser, null); |
| 448 | } |
| 449 | |
| 450 | ComponentName componentName = ComponentName.unflattenFromString(componentStr); |
| 451 | if (componentName == null) { |
| 452 | throw new XmlPullParserException("Invalid component name: " + componentStr); |
| 453 | } |
| 454 | |
| 455 | mComponentFilters.add(componentName); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 456 | } else { |
| 457 | super.readChild(parser); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | public int getIntentFilterCount() { |
| 462 | return mIntentFilters.size(); |
| 463 | } |
| 464 | |
| 465 | public FirewallIntentFilter getIntentFilter(int index) { |
| 466 | return mIntentFilters.get(index); |
| 467 | } |
| 468 | |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 469 | public int getComponentFilterCount() { |
| 470 | return mComponentFilters.size(); |
| 471 | } |
| 472 | |
| 473 | public ComponentName getComponentFilter(int index) { |
| 474 | return mComponentFilters.get(index); |
| 475 | } |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 476 | public boolean getBlock() { |
| 477 | return block; |
| 478 | } |
| 479 | |
| 480 | public boolean getLog() { |
| 481 | return log; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private static class FirewallIntentFilter extends IntentFilter { |
| 486 | private final Rule rule; |
| 487 | |
| 488 | public FirewallIntentFilter(Rule rule) { |
| 489 | this.rule = rule; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | private static class FirewallIntentResolver |
| 494 | extends IntentResolver<FirewallIntentFilter, Rule> { |
| 495 | @Override |
| 496 | protected boolean allowFilterResult(FirewallIntentFilter filter, List<Rule> dest) { |
| 497 | return !dest.contains(filter.rule); |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | protected boolean isPackageForFilter(String packageName, FirewallIntentFilter filter) { |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | protected FirewallIntentFilter[] newArray(int size) { |
| 507 | return new FirewallIntentFilter[size]; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | protected Rule newResult(FirewallIntentFilter filter, int match, int userId) { |
| 512 | return filter.rule; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | protected void sortResults(List<Rule> results) { |
| 517 | // there's no need to sort the results |
| 518 | return; |
| 519 | } |
Ben Gruver | f157b48 | 2013-08-06 15:57:01 -0700 | [diff] [blame] | 520 | |
| 521 | public void queryByComponent(ComponentName componentName, List<Rule> candidateRules) { |
| 522 | Rule[] rules = mRulesByComponent.get(componentName); |
| 523 | if (rules != null) { |
| 524 | candidateRules.addAll(Arrays.asList(rules)); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | public void addComponentFilter(ComponentName componentName, Rule rule) { |
| 529 | Rule[] rules = mRulesByComponent.get(componentName); |
| 530 | rules = ArrayUtils.appendElement(Rule.class, rules, rule); |
| 531 | mRulesByComponent.put(componentName, rules); |
| 532 | } |
| 533 | |
| 534 | private final ArrayMap<ComponentName, Rule[]> mRulesByComponent = |
| 535 | new ArrayMap<ComponentName, Rule[]>(0); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 538 | final FirewallHandler mHandler; |
| 539 | |
| 540 | private final class FirewallHandler extends Handler { |
| 541 | public FirewallHandler(Looper looper) { |
| 542 | super(looper, null, true); |
| 543 | } |
| 544 | |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 545 | @Override |
| 546 | public void handleMessage(Message msg) { |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 547 | readRulesDir(getRulesDir()); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 548 | } |
| 549 | }; |
| 550 | |
| 551 | /** |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 552 | * Monitors for the creation/deletion/modification of any .xml files in the rule directory |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 553 | */ |
| 554 | private class RuleObserver extends FileObserver { |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 555 | private static final int MONITORED_EVENTS = FileObserver.CREATE|FileObserver.MOVED_TO| |
| 556 | FileObserver.CLOSE_WRITE|FileObserver.DELETE|FileObserver.MOVED_FROM; |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 557 | |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 558 | public RuleObserver(File monitoredDir) { |
| 559 | super(monitoredDir.getAbsolutePath(), MONITORED_EVENTS); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | @Override |
| 563 | public void onEvent(int event, String path) { |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 564 | if (path.endsWith(".xml")) { |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 565 | // we wait 250ms before taking any action on an event, in order to dedup multiple |
| 566 | // events. E.g. a delete event followed by a create event followed by a subsequent |
Ben Gruver | b7c1a17 | 2013-07-23 15:05:28 -0700 | [diff] [blame] | 567 | // write+close event |
| 568 | mHandler.removeMessages(0); |
| 569 | mHandler.sendEmptyMessageDelayed(0, 250); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 574 | /** |
| 575 | * This interface contains the methods we need from ActivityManagerService. This allows AMS to |
| 576 | * export these methods to us without making them public, and also makes it easier to test this |
| 577 | * component. |
| 578 | */ |
| 579 | public interface AMSInterface { |
| 580 | int checkComponentPermission(String permission, int pid, int uid, |
| 581 | int owningUid, boolean exported); |
Ben Gruver | a4879c3 | 2013-04-08 14:27:37 -0700 | [diff] [blame] | 582 | Object getAMSLock(); |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Checks if the caller has access to a component |
| 587 | * |
| 588 | * @param permission If present, the caller must have this permission |
| 589 | * @param pid The pid of the caller |
| 590 | * @param uid The uid of the caller |
| 591 | * @param owningUid The uid of the application that owns the component |
| 592 | * @param exported Whether the component is exported |
| 593 | * @return True if the caller can access the described component |
| 594 | */ |
| 595 | boolean checkComponentPermission(String permission, int pid, int uid, int owningUid, |
| 596 | boolean exported) { |
| 597 | return mAms.checkComponentPermission(permission, pid, uid, owningUid, exported) == |
| 598 | PackageManager.PERMISSION_GRANTED; |
| 599 | } |
| 600 | |
| 601 | boolean signaturesMatch(int uid1, int uid2) { |
Ben Gruver | 8be8df2 | 2013-04-05 19:21:19 -0700 | [diff] [blame] | 602 | try { |
| 603 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 604 | return pm.checkUidSignatures(uid1, uid2) == PackageManager.SIGNATURE_MATCH; |
| 605 | } catch (RemoteException ex) { |
| 606 | Slog.e(TAG, "Remote exception while checking signatures", ex); |
| 607 | return false; |
| 608 | } |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 609 | } |
Ben Gruver | dd72c9e | 2013-08-06 12:34:17 -0700 | [diff] [blame] | 610 | |
Ben Gruver | 4efe940 | 2013-04-02 21:18:41 -0700 | [diff] [blame] | 611 | } |