blob: 7cd1b7bab99e05545f690c494e4ebfed6cd6e1e9 [file] [log] [blame]
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -07001/*
2 * Copyright (C) 2014 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
17package com.android.server.connectivity;
18
19import static android.Manifest.permission.CHANGE_NETWORK_STATE;
20import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
Hugo Benichi514da602016-07-19 15:59:27 +090021import static android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS;
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -070022import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
23import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
24import static android.content.pm.PackageManager.GET_PERMISSIONS;
25
26import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.pm.PackageInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.pm.UserInfo;
34import android.net.Uri;
35import android.os.INetworkManagementService;
36import android.os.RemoteException;
37import android.os.UserHandle;
38import android.os.UserManager;
39import android.text.TextUtils;
40import android.util.Log;
41
42import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.HashSet;
45import java.util.List;
46import java.util.Map.Entry;
47import java.util.Map;
48import java.util.Set;
49
50/**
51 * A utility class to inform Netd of UID permisisons.
52 * Does a mass update at boot and then monitors for app install/remove.
53 *
54 * @hide
55 */
56public class PermissionMonitor {
57 private static final String TAG = "PermissionMonitor";
Joe Onorato12acbd72016-02-01 17:49:31 -080058 private static final boolean DBG = false;
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -070059 private static final boolean SYSTEM = true;
60 private static final boolean NETWORK = false;
61
62 private final Context mContext;
63 private final PackageManager mPackageManager;
64 private final UserManager mUserManager;
65 private final INetworkManagementService mNetd;
66 private final BroadcastReceiver mIntentReceiver;
67
68 // Values are User IDs.
Hugo Benichi514da602016-07-19 15:59:27 +090069 private final Set<Integer> mUsers = new HashSet<>();
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -070070
71 // Keys are App IDs. Values are true for SYSTEM permission and false for NETWORK permission.
Hugo Benichi514da602016-07-19 15:59:27 +090072 private final Map<Integer, Boolean> mApps = new HashMap<>();
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -070073
74 public PermissionMonitor(Context context, INetworkManagementService netd) {
75 mContext = context;
76 mPackageManager = context.getPackageManager();
77 mUserManager = UserManager.get(context);
78 mNetd = netd;
79 mIntentReceiver = new BroadcastReceiver() {
80 @Override
81 public void onReceive(Context context, Intent intent) {
82 String action = intent.getAction();
83 int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
84 int appUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
85 Uri appData = intent.getData();
86 String appName = appData != null ? appData.getSchemeSpecificPart() : null;
87
88 if (Intent.ACTION_USER_ADDED.equals(action)) {
89 onUserAdded(user);
90 } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
91 onUserRemoved(user);
92 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
93 onAppAdded(appName, appUid);
94 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
95 onAppRemoved(appUid);
96 }
97 }
98 };
99 }
100
101 // Intended to be called only once at startup, after the system is ready. Installs a broadcast
102 // receiver to monitor ongoing UID changes, so this shouldn't/needn't be called again.
103 public synchronized void startMonitoring() {
104 log("Monitoring");
105
106 IntentFilter intentFilter = new IntentFilter();
107 intentFilter.addAction(Intent.ACTION_USER_ADDED);
108 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
109 mContext.registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, intentFilter, null, null);
110
111 intentFilter = new IntentFilter();
112 intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
113 intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
114 intentFilter.addDataScheme("package");
115 mContext.registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, intentFilter, null, null);
116
117 List<PackageInfo> apps = mPackageManager.getInstalledPackages(GET_PERMISSIONS);
118 if (apps == null) {
119 loge("No apps");
120 return;
121 }
122
123 for (PackageInfo app : apps) {
124 int uid = app.applicationInfo != null ? app.applicationInfo.uid : -1;
125 if (uid < 0) {
126 continue;
127 }
128
129 boolean isNetwork = hasNetworkPermission(app);
Hugo Benichi514da602016-07-19 15:59:27 +0900130 boolean hasRestrictedPermission = hasRestrictedNetworkPermission(app);
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700131
Hugo Benichi514da602016-07-19 15:59:27 +0900132 if (isNetwork || hasRestrictedPermission) {
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700133 Boolean permission = mApps.get(uid);
134 // If multiple packages share a UID (cf: android:sharedUserId) and ask for different
135 // permissions, don't downgrade (i.e., if it's already SYSTEM, leave it as is).
136 if (permission == null || permission == NETWORK) {
Hugo Benichi514da602016-07-19 15:59:27 +0900137 mApps.put(uid, hasRestrictedPermission);
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700138 }
139 }
140 }
141
142 List<UserInfo> users = mUserManager.getUsers(true); // exclude dying users
143 if (users != null) {
144 for (UserInfo user : users) {
145 mUsers.add(user.id);
146 }
147 }
148
149 log("Users: " + mUsers.size() + ", Apps: " + mApps.size());
150 update(mUsers, mApps, true);
151 }
152
153 private boolean hasPermission(PackageInfo app, String permission) {
154 if (app.requestedPermissions != null) {
155 for (String p : app.requestedPermissions) {
156 if (permission.equals(p)) {
157 return true;
158 }
159 }
160 }
161 return false;
162 }
163
164 private boolean hasNetworkPermission(PackageInfo app) {
165 return hasPermission(app, CHANGE_NETWORK_STATE);
166 }
167
Hugo Benichi514da602016-07-19 15:59:27 +0900168 private boolean hasRestrictedNetworkPermission(PackageInfo app) {
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700169 int flags = app.applicationInfo != null ? app.applicationInfo.flags : 0;
170 if ((flags & FLAG_SYSTEM) != 0 || (flags & FLAG_UPDATED_SYSTEM_APP) != 0) {
171 return true;
172 }
Hugo Benichi514da602016-07-19 15:59:27 +0900173 return hasPermission(app, CONNECTIVITY_INTERNAL)
174 || hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS);
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700175 }
176
177 private int[] toIntArray(List<Integer> list) {
178 int[] array = new int[list.size()];
179 for (int i = 0; i < list.size(); i++) {
180 array[i] = list.get(i);
181 }
182 return array;
183 }
184
185 private void update(Set<Integer> users, Map<Integer, Boolean> apps, boolean add) {
Hugo Benichi514da602016-07-19 15:59:27 +0900186 List<Integer> network = new ArrayList<>();
187 List<Integer> system = new ArrayList<>();
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700188 for (Entry<Integer, Boolean> app : apps.entrySet()) {
189 List<Integer> list = app.getValue() ? system : network;
190 for (int user : users) {
191 list.add(UserHandle.getUid(user, app.getKey()));
192 }
193 }
194 try {
195 if (add) {
Sreeram Ramachandran0f8f1202014-11-04 10:15:03 -0800196 mNetd.setPermission("NETWORK", toIntArray(network));
197 mNetd.setPermission("SYSTEM", toIntArray(system));
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700198 } else {
199 mNetd.clearPermission(toIntArray(network));
200 mNetd.clearPermission(toIntArray(system));
201 }
202 } catch (RemoteException e) {
203 loge("Exception when updating permissions: " + e);
204 }
205 }
206
207 private synchronized void onUserAdded(int user) {
208 if (user < 0) {
209 loge("Invalid user in onUserAdded: " + user);
210 return;
211 }
212 mUsers.add(user);
213
Hugo Benichi514da602016-07-19 15:59:27 +0900214 Set<Integer> users = new HashSet<>();
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700215 users.add(user);
216 update(users, mApps, true);
217 }
218
219 private synchronized void onUserRemoved(int user) {
220 if (user < 0) {
221 loge("Invalid user in onUserRemoved: " + user);
222 return;
223 }
224 mUsers.remove(user);
225
Hugo Benichi514da602016-07-19 15:59:27 +0900226 Set<Integer> users = new HashSet<>();
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700227 users.add(user);
228 update(users, mApps, false);
229 }
230
231 private synchronized void onAppAdded(String appName, int appUid) {
232 if (TextUtils.isEmpty(appName) || appUid < 0) {
233 loge("Invalid app in onAppAdded: " + appName + " | " + appUid);
234 return;
235 }
236
237 try {
238 PackageInfo app = mPackageManager.getPackageInfo(appName, GET_PERMISSIONS);
239 boolean isNetwork = hasNetworkPermission(app);
Hugo Benichi514da602016-07-19 15:59:27 +0900240 boolean hasRestrictedPermission = hasRestrictedNetworkPermission(app);
241 if (isNetwork || hasRestrictedPermission) {
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700242 Boolean permission = mApps.get(appUid);
243 // If multiple packages share a UID (cf: android:sharedUserId) and ask for different
244 // permissions, don't downgrade (i.e., if it's already SYSTEM, leave it as is).
245 if (permission == null || permission == NETWORK) {
Hugo Benichi514da602016-07-19 15:59:27 +0900246 mApps.put(appUid, hasRestrictedPermission);
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700247
Hugo Benichi514da602016-07-19 15:59:27 +0900248 Map<Integer, Boolean> apps = new HashMap<>();
249 apps.put(appUid, hasRestrictedPermission);
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700250 update(mUsers, apps, true);
251 }
252 }
253 } catch (NameNotFoundException e) {
254 loge("NameNotFoundException in onAppAdded: " + e);
255 }
256 }
257
258 private synchronized void onAppRemoved(int appUid) {
259 if (appUid < 0) {
260 loge("Invalid app in onAppRemoved: " + appUid);
261 return;
262 }
263 mApps.remove(appUid);
264
Hugo Benichi514da602016-07-19 15:59:27 +0900265 Map<Integer, Boolean> apps = new HashMap<>();
Sreeram Ramachandrane4a05af2014-09-24 09:16:19 -0700266 apps.put(appUid, NETWORK); // doesn't matter which permission we pick here
267 update(mUsers, apps, false);
268 }
269
270 private static void log(String s) {
271 if (DBG) {
272 Log.d(TAG, s);
273 }
274 }
275
276 private static void loge(String s) {
277 Log.e(TAG, s);
278 }
279}