-
Notifications
You must be signed in to change notification settings - Fork 8
/
ActivityManagerServiceHook.java
279 lines (248 loc) · 11.9 KB
/
ActivityManagerServiceHook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package me.piebridge.prevent.framework;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import me.piebridge.prevent.BuildConfig;
import me.piebridge.prevent.common.Configuration;
import me.piebridge.prevent.common.GmsUtils;
import me.piebridge.prevent.common.PackageUtils;
import me.piebridge.prevent.framework.util.AccountUtils;
import me.piebridge.prevent.framework.util.LogUtils;
import me.piebridge.prevent.framework.util.SafeActionUtils;
/**
* Created by thom on 15/8/11.
*/
public class ActivityManagerServiceHook {
private static Context mContext;
private static Map<String, Boolean> mPreventPackages;
// normally, there is only one
private static Collection<String> settingsPackages = new HashSet<String>();
private static Collection<String> importantSystemPackages = new HashSet<String>();
private static ScheduledThreadPoolExecutor cleanUpExecutor = new ScheduledThreadPoolExecutor(0x1);
private ActivityManagerServiceHook() {
}
public static void setContext(Context context, Map<String, Boolean> preventPackages) {
mContext = context;
mPreventPackages = preventPackages;
}
public static boolean hookStartProcessLocked(Context context, ApplicationInfo info, String hostingType, ComponentName hostingName, String sender) {
String packageName = info.packageName;
if (mContext == null && context != null) {
SystemHook.retrievePreventsIfNeeded(context);
}
if (BuildConfig.DEBUG) {
if (hostingName != null) {
PreventLog.v("startProcessLocked, hostingName: " + hostingName.flattenToShortString() + ", hostingType: " + hostingType + ", sender: " + sender);
} else {
PreventLog.v("startProcessLocked, packageName: " + packageName + ", hostingType: " + hostingType + ", sender: " + sender);
}
}
if (mPreventPackages == null) {
PreventLog.e("prevent list shouldn't be null");
return true;
}
boolean prevents = Boolean.TRUE.equals(mPreventPackages.get(packageName));
if ("activity".equals(hostingType)) {
SystemHook.cancelCheck(packageName);
SystemHook.updateRunningGapps(packageName, true);
if (prevents) {
// never block activity
mPreventPackages.put(packageName, false);
prevents = false;
}
LogUtils.logStartProcess(packageName, hostingType, hostingName, sender);
}
return !prevents || hookDependency(hostingName, hostingType, packageName, sender);
}
private static boolean hookDependency(ComponentName hostingName, String hostingType, String packageName, String sender) {
if (packageName.equals(sender)) {
LogUtils.logStartProcess(packageName, hostingType + "(self)", hostingName, sender);
return true;
}
if ("broadcast".equals(hostingType)) {
// always block broadcast
return hookBroadcast(hostingName, hostingType, packageName, sender);
} else if ("service".equals(hostingType)) {
return hookService(hostingName, hostingType, packageName, sender);
} else if ("content provider".equals(hostingType) && !SafeActionUtils.isSafeContentProvider(hostingName)) {
LogUtils.logStartProcess(true, packageName, hostingType, hostingName, sender);
return false;
}
SystemHook.checkRunningServices(packageName, false);
LogUtils.logStartProcess(packageName, hostingType + "(should safe)", hostingName, sender);
return true;
}
private static boolean hookBroadcast(ComponentName hostingName, String hostingType, String packageName, String sender) {
if (SafeActionUtils.isSafeBroadcast(hostingName)) {
SystemHook.checkRunningServices(packageName, false);
LogUtils.logStartProcess(packageName, hostingType + "(safe)", hostingName, sender);
return true;
} else {
LogUtils.logStartProcess(true, packageName, hostingType, hostingName, sender);
return false;
}
}
private static boolean hookService(ComponentName hostingName, String hostingType, String packageName, String sender) {
if (SafeActionUtils.isSyncService(mContext, hostingName, sender)) {
return hookSyncService(hostingName, hostingType, packageName, sender);
}
if (SafeActionUtils.isAccountService(mContext, hostingName, sender)) {
return hookAccountService(hostingName, hostingType, packageName, sender);
}
if (GmsUtils.isGms(packageName)) {
return hookGmsService(hostingName, hostingType, packageName, sender);
}
if (cannotPrevent(sender, packageName, hostingName)) {
SystemHook.checkRunningServices(packageName, true);
LogUtils.logStartProcess(packageName, hostingType, hostingName, sender);
return true;
} else {
LogUtils.logStartProcess(true, packageName, hostingType, hostingName, sender);
return false;
}
}
private static boolean hookAccountService(ComponentName hostingName, String hostingType, String packageName, String sender) {
String currentPackageName = SystemHook.getCurrentPackageName();
PreventLog.d("account authenticator, current package: " + currentPackageName + ", " + hostingName.flattenToShortString());
if (settingsPackages.isEmpty()) {
retrieveSettingsPackage(mContext.getPackageManager(), settingsPackages);
}
if (isSettingPackageName(currentPackageName) || (GmsUtils.isGapps(packageName) && GmsUtils.isGapps(currentPackageName))) {
handleSafeService(packageName);
SystemHook.checkRunningServices(packageName, true);
LogUtils.logStartProcess(packageName, hostingType + "(account)", hostingName, sender);
return true;
} else {
LogUtils.logStartProcess(true, packageName, hostingType + "(account)", hostingName, sender);
return false;
}
}
private static boolean isSettingPackageName(String currentPackageName) {
return settingsPackages.contains(currentPackageName);
}
private static boolean cannotPrevent(String sender, String packageName, ComponentName hostingName) {
if (SafeActionUtils.isUnsafeService(hostingName)) {
return false;
} else if (SafeActionUtils.isSafeService(hostingName) || SafeActionUtils.cannotPrevent(mContext, hostingName)) {
return true;
} else if (sender == null) {
return Configuration.getDefault().isAllowEmptySender();
} else {
return cannotPrevent(sender, packageName);
}
}
private static boolean hookGmsService(ComponentName hostingName, String hostingType, String packageName, String sender) {
if (cannotPreventGms(hostingName, sender)) {
// only allow gapps to use gms
SystemHook.checkRunningServices(packageName, true);
LogUtils.logStartProcess(packageName, hostingType, hostingName, sender);
return true;
} else {
LogUtils.logStartProcess(true, packageName, hostingType, hostingName, sender);
return false;
}
}
private static void retrieveSettingsPackage(PackageManager pm, Collection<String> packages) {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", BuildConfig.APPLICATION_ID, null));
for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) {
String packageName = resolveInfo.activityInfo.packageName;
if (SystemHook.isSystemPackage(packageName)) {
PreventLog.d("add " + packageName + " as settings");
packages.add(packageName);
}
}
}
private static boolean cannotPreventGms(ComponentName component, String sender) {
if (GmsUtils.isGmsRegister(mContext, component)) {
return true;
}
PackageManager pm = mContext.getPackageManager();
if (settingsPackages.isEmpty()) {
retrieveSettingsPackage(pm, settingsPackages);
}
if (settingsPackages.contains(sender)) {
return true;
}
if (!GmsUtils.isGapps(sender)) {
return false;
}
return pm.getLaunchIntentForPackage(sender) == null || SystemHook.hasRunningActivity(sender);
}
private static boolean cannotPrevent(String sender, String packageName) {
if (SystemHook.isFramework(packageName)) {
return true;
} else if (TextUtils.isDigitsOnly(sender)) {
return Integer.parseInt(sender) < PackageUtils.FIRST_APPLICATION_UID;
} else if (cannotPrevent(sender)
|| mContext.getPackageManager().getLaunchIntentForPackage(sender) == null
|| (SystemHook.isSystemPackage(packageName) && SystemHook.hasRunningActivity(sender))) {
// the sender cannot be prevent
// the sender has no launcher
// running sender call system package
return true;
}
return false;
}
public static boolean cannotPrevent(String packageName) {
if (importantSystemPackages.contains(packageName)) {
return true;
}
try {
PackageManager pm = mContext.getPackageManager();
ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
boolean result = !PackageUtils.canPrevent(pm, info);
if (result && PackageUtils.isSystemPackage(info.flags)) {
importantSystemPackages.add(packageName);
}
return result;
} catch (PackageManager.NameNotFoundException e) {
PreventLog.d("cannot find package: " + packageName, e);
return false;
}
}
private static boolean hookSyncService(ComponentName hostingName, String hostingType, String packageName, String sender) {
if (ContentResolver.getMasterSyncAutomatically() && AccountUtils.isComponentSyncable(mContext, hostingName)) {
handleSafeService(packageName);
SystemHook.checkRunningServices(packageName, true);
LogUtils.logStartProcess(packageName, hostingType + "(sync)", hostingName, sender);
return true;
} else {
LogUtils.logStartProcess(true, packageName, hostingType + "(sync)", hostingName, sender);
return false;
}
}
private static void handleSafeService(String packageName) {
if (Boolean.TRUE.equals(mPreventPackages.get(packageName))) {
PreventLog.i("allow " + packageName + " for next service/broadcast");
mPreventPackages.put(packageName, false);
SystemHook.restoreLater(packageName);
}
}
public static boolean onCleanUpRemovedTask(final String packageName) {
SystemHook.updateRunningGapps(packageName, false);
if (packageName != null && mPreventPackages != null && mPreventPackages.containsKey(packageName)) {
mPreventPackages.put(packageName, true);
cleanUpExecutor.execute(new Runnable() {
@Override
public void run() {
if (Boolean.TRUE.equals(mPreventPackages.get(packageName))) {
LogUtils.logForceStop("removeTask", packageName, "");
SystemHook.forceStopPackage(packageName, true);
}
}
});
}
return true;
}
}