forked from M66B/XPrivacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXBinder.java
337 lines (293 loc) · 10.5 KB
/
XBinder.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package biz.bokhorst.xprivacy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Process;
import android.util.Log;
import android.util.SparseArray;
public class XBinder extends XHook {
private Methods mMethod;
private static long mToken = 0;
private static Map<String, Boolean> mMapClassSystem = new HashMap<String, Boolean>();
private static Map<String, SparseArray<String>> mMapCodeName = new HashMap<String, SparseArray<String>>();
private static final int BITS_TOKEN = 16;
private static final int FLAG_ALL = 0xFFFF;
private static final int MASK_TOKEN = 0xFFFF;
private static final int PING_TRANSACTION = ('_' << 24) | ('P' << 16) | ('N' << 8) | 'G';
private static final int DUMP_TRANSACTION = ('_' << 24) | ('D' << 16) | ('M' << 8) | 'P';
private static final int INTERFACE_TRANSACTION = ('_' << 24) | ('N' << 16) | ('T' << 8) | 'F';
private static final int TWEET_TRANSACTION = ('_' << 24) | ('T' << 16) | ('W' << 8) | 'T';
private static final int LIKE_TRANSACTION = ('_' << 24) | ('L' << 16) | ('I' << 8) | 'K';
private static final int SYSPROPS_TRANSACTION = ('_' << 24) | ('S' << 16) | ('P' << 8) | 'R';
// Service name should one-to-one correspond to the other lists
// @formatter:off
public static List<String> cServiceName = Arrays.asList(new String[] {
"account",
"activity",
"clipboard",
"connectivity",
"content",
"location",
"telephony.registry",
"telephony.msim.registry",
"package",
"iphonesubinfo",
"iphonesubinfo_msim",
"window",
"wifi",
"sip",
"isms",
"nfc",
"appwidget",
"bluetooth",
"bluetooth_manager",
"input",
"sensorservice",
"usb",
"media.camera",
"<noname>",
"<noname>",
"<noname>"
});
// @formatter:on
// @formatter:off
public static List<String> cServiceDescriptor = Arrays.asList(new String[] {
"android.accounts.IAccountManager",
"android.app.IActivityManager",
"android.content.IClipboard",
"android.net.IConnectivityManager",
"android.content.IContentService",
"android.location.ILocationManager",
"com.android.internal.telephony.ITelephonyRegistry",
"com.android.internal.telephony.ITelephonyRegistryMSim",
"android.content.pm.IPackageManager",
"com.android.internal.telephony.IPhoneSubInfo",
"com.android.internal.telephony.msim.IPhoneSubInfoMSim",
"android.view.IWindowManager",
"android.net.wifi.IWifiManager",
"android.net.sip.ISipService",
"com.android.internal.telephony.ISms",
"android.nfc.INfcAdapter",
"com.android.internal.appwidget.IAppWidgetService",
"android.bluetooth.IBluetooth",
"android.bluetooth.IBluetoothManager",
"android.hardware.input.IInputManager",
"android.gui.SensorServer",
"android.hardware.usb.IUsbManager",
"android.hardware.ICameraService",
"android.app.IApplicationThread",
"android.content.IContentProvider",
"android.view.IWindowSession"
});
// @formatter:on
// @formatter:off
public static List<String> cServiceOptional = Arrays.asList(new String[] {
"<noname>",
"iphonesubinfo",
"iphonesubinfo_msim",
"sip",
"isms",
"nfc",
"bluetooth",
"bluetooth_manager"
});
// @formatter:on
private XBinder(Methods method, String restrictionName) {
super(restrictionName, method.name(), null);
mMethod = method;
}
public String getClassName() {
return (mMethod == Methods.transact ? "android.os.BinderProxy" : "android.os.Binder");
}
public boolean isVisible() {
return (mMethod != Methods.execTransact);
}
@Override
public void setSecret(String secret) {
super.setSecret(secret);
mToken = (secret.hashCode() & MASK_TOKEN);
}
// @formatter:off
// private boolean execTransact(int code, int dataObj, int replyObj, int flags)
// public final boolean transact(int code, Parcel data, Parcel reply, int flags)
// public native boolean transact(int code, Parcel data, Parcel reply, int flags)
// frameworks/base/core/java/android/os/Binder.java
// http://developer.android.com/reference/android/os/Binder.html
// @formatter:on
private enum Methods {
execTransact, transact
};
public static List<XHook> getInstances() {
List<XHook> listHook = new ArrayList<XHook>();
listHook.add(new XBinder(Methods.execTransact, null)); // Binder
listHook.add(new XBinder(Methods.transact, null)); // BinderProxy
return listHook;
}
@Override
protected void before(XParam param) throws Throwable {
if (mMethod == Methods.execTransact) {
// execTransact calls the overridden onTransact
// Check for direct IPC
checkIPC(param);
} else if (mMethod == Methods.transact) {
markIPC(param);
} else
Util.log(this, Log.WARN, "Unknown method=" + param.method.getName());
}
@Override
protected void after(XParam param) throws Throwable {
// Do nothing
}
private void markIPC(XParam param) throws Throwable {
// Allow management transactions
int code = (Integer) param.args[0];
if (isManagementTransaction(code))
return;
// Only for applications
int uid = Binder.getCallingUid();
if (!PrivacyManager.isApplication(uid))
return;
// Check interface name
IBinder binder = (IBinder) param.thisObject;
String descriptor = (binder == null ? null : binder.getInterfaceDescriptor());
if (!cServiceDescriptor.contains(descriptor))
return;
// Search this object in call stack
boolean ok = false;
boolean found = false;
StackTraceElement[] ste = Thread.currentThread().getStackTrace();
for (int i = 0; i < ste.length; i++)
if (ste[i].getClassName().equals(param.thisObject.getClass().getName())) {
found = true;
// Check if caller class in user space
String callerClassName = (i + 2 < ste.length ? ste[i + 2].getClassName() : null);
if (callerClassName != null && !callerClassName.startsWith("java.lang.reflect."))
synchronized (mMapClassSystem) {
if (!mMapClassSystem.containsKey(callerClassName))
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class<?> clazz = Class.forName(callerClassName, false, loader);
boolean boot = Context.class.getClassLoader().equals(clazz.getClassLoader());
mMapClassSystem.put(callerClassName, boot);
} catch (ClassNotFoundException ignored) {
mMapClassSystem.put(callerClassName, true);
}
ok = mMapClassSystem.get(callerClassName);
}
break;
}
// Conditionally mark
if (ok) {
int flags = (Integer) param.args[3];
if ((flags & ~FLAG_ALL) != 0)
Util.log(this, Log.ERROR, "Unknown flags=" + Integer.toHexString(flags) + " descriptor=" + descriptor
+ " code=" + code + " uid=" + Binder.getCallingUid());
flags |= (mToken << BITS_TOKEN);
param.args[3] = flags;
} else {
int level = (found ? Log.WARN : Log.ERROR);
Util.log(this, level, "Unmarked descriptor=" + descriptor + " found=" + found + " code=" + code + " uid="
+ Binder.getCallingUid());
Util.logStack(this, level, true);
}
}
// Entry point from android_util_Binder.cpp's onTransact
private void checkIPC(XParam param) throws Throwable {
// Allow management transactions
int code = (Integer) param.args[0];
if (isManagementTransaction(code))
return;
// Only for applications
int uid = Binder.getCallingUid();
if (!PrivacyManager.isApplication(uid))
return;
// Check interface name
IBinder binder = (IBinder) param.thisObject;
String descriptor = (binder == null ? null : binder.getInterfaceDescriptor());
if (!cServiceDescriptor.contains(descriptor))
return;
// Get token
int flags = (Integer) param.args[3];
long token = (flags >> BITS_TOKEN) & MASK_TOKEN;
flags &= FLAG_ALL;
param.args[3] = flags;
// Check token
if (token != mToken) {
String[] name = descriptor.split("\\.");
String interfaceName = name[name.length - 1];
// Get transaction code name
String codeName;
synchronized (mMapCodeName) {
if (!mMapCodeName.containsKey(descriptor)) {
SparseArray<String> sa = new SparseArray<String>();
mMapCodeName.put(descriptor, sa);
List<Class<?>> listClass = new ArrayList<Class<?>>();
if (param.thisObject.getClass().getSuperclass() != null)
listClass.add(param.thisObject.getClass().getSuperclass());
try {
listClass.add(Class.forName(descriptor));
} catch (ClassNotFoundException ignored) {
}
for (Class<?> clazz : listClass)
for (Field field : clazz.getDeclaredFields())
try {
if (field.getName().startsWith("TRANSACTION_")
|| field.getName().endsWith("_TRANSACTION")) {
field.setAccessible(true);
Integer txCode = (Integer) field.get(null);
String txName = field.getName().replace("TRANSACTION_", "")
.replace("_TRANSACTION", "");
sa.put(txCode, txName);
}
} catch (Throwable ignore) {
}
}
codeName = mMapCodeName.get(descriptor).get(code);
}
if (codeName == null) {
codeName = Integer.toString(code);
Util.log(this, Log.WARN, "Unknown transaction=" + descriptor + ":" + code + " class="
+ param.thisObject.getClass() + " uid=" + Binder.getCallingUid());
Util.logStack(this, Log.INFO);
}
Util.log(this, Log.INFO, "can restrict transaction=" + interfaceName + ":" + codeName + " flags=" + flags
+ " uid=" + uid + " my=" + Process.myUid());
if (isRestrictedExtra(uid, PrivacyManager.cIPC, "Binder", interfaceName + ":" + codeName)) {
Util.log(this, Log.WARN, "Restricting " + interfaceName + ":" + codeName + " code=" + code);
// Get reply parcel
Parcel reply = null;
try {
// static protected final Parcel obtain(int obj)
// frameworks/base/core/java/android/os/Parcel.java
Method methodObtain = Parcel.class.getDeclaredMethod("obtain",
Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? int.class : long.class);
methodObtain.setAccessible(true);
reply = (Parcel) methodObtain.invoke(null, param.args[2]);
} catch (NoSuchMethodException ex) {
Util.bug(this, ex);
}
// Block IPC
if (reply == null)
Util.log(this, Log.ERROR, "reply is null uid=" + uid);
else {
reply.setDataPosition(0);
reply.writeException(new SecurityException("XPrivacy"));
}
param.setResult(true);
}
}
}
private static boolean isManagementTransaction(int code) {
return (code == PING_TRANSACTION || code == DUMP_TRANSACTION || code == INTERFACE_TRANSACTION
|| code == TWEET_TRANSACTION || code == LIKE_TRANSACTION || code == SYSPROPS_TRANSACTION);
}
}