forked from rovo89/Xposed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libxposed_common.cpp
379 lines (314 loc) · 13.6 KB
/
libxposed_common.cpp
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* This file includes functions shared by different runtimes.
*/
#define LOG_TAG "Xposed"
#include "libxposed_common.h"
#include "JNIHelp.h"
#include <ScopedUtfChars.h>
#define private public
#if PLATFORM_SDK_VERSION == 15
#include <utils/ResourceTypes.h>
#else
#include <androidfw/ResourceTypes.h>
#endif
#undef private
namespace xposed {
////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////
bool xposedLoadedSuccessfully = false;
xposed::XposedShared* xposed = NULL;
jclass classXposedBridge = NULL;
static jclass classXResources = NULL;
static jclass classFileResult = NULL;
jmethodID methodXposedBridgeHandleHookedMethod = NULL;
static jmethodID methodXResourcesTranslateResId = NULL;
static jmethodID methodXResourcesTranslateAttrId = NULL;
static jmethodID constructorFileResult = NULL;
////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////
static int register_natives_XposedBridge(JNIEnv* env, jclass clazz);
static int register_natives_XResources(JNIEnv* env, jclass clazz);
static int register_natives_ZygoteService(JNIEnv* env, jclass clazz);
////////////////////////////////////////////////////////////
// Utility methods
////////////////////////////////////////////////////////////
/** Read an integer value from a configuration file. */
int readIntConfig(const char* fileName, int defaultValue) {
FILE *fp = fopen(fileName, "r");
if (fp == NULL)
return defaultValue;
int result;
int success = fscanf(fp, "%i", &result);
fclose(fp);
return (success >= 1) ? result : defaultValue;
}
////////////////////////////////////////////////////////////
// Library initialization
////////////////////////////////////////////////////////////
bool initXposedBridge(JNIEnv* env) {
classXposedBridge = env->FindClass(CLASS_XPOSED_BRIDGE);
if (classXposedBridge == NULL) {
ALOGE("Error while loading Xposed class '%s':", CLASS_XPOSED_BRIDGE);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
classXposedBridge = reinterpret_cast<jclass>(env->NewGlobalRef(classXposedBridge));
ALOGI("Found Xposed class '%s', now initializing", CLASS_XPOSED_BRIDGE);
if (register_natives_XposedBridge(env, classXposedBridge) != JNI_OK) {
ALOGE("Could not register natives for '%s'", CLASS_XPOSED_BRIDGE);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
methodXposedBridgeHandleHookedMethod = env->GetStaticMethodID(classXposedBridge, "handleHookedMethod",
"(Ljava/lang/reflect/Member;ILjava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
if (methodXposedBridgeHandleHookedMethod == NULL) {
ALOGE("ERROR: could not find method %s.handleHookedMethod(Member, int, Object, Object, Object[])", CLASS_XPOSED_BRIDGE);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
return true;
}
bool initZygoteService(JNIEnv* env) {
jclass zygoteServiceClass = env->FindClass(CLASS_ZYGOTE_SERVICE);
if (zygoteServiceClass == NULL) {
ALOGE("Error while loading ZygoteService class '%s':", CLASS_ZYGOTE_SERVICE);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
if (register_natives_ZygoteService(env, zygoteServiceClass) != JNI_OK) {
ALOGE("Could not register natives for '%s'", CLASS_ZYGOTE_SERVICE);
env->ExceptionClear();
return false;
}
classFileResult = env->FindClass(CLASS_FILE_RESULT);
if (classFileResult == NULL) {
ALOGE("Error while loading FileResult class '%s':", CLASS_FILE_RESULT);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
classFileResult = reinterpret_cast<jclass>(env->NewGlobalRef(classFileResult));
constructorFileResult = env->GetMethodID(classFileResult, "<init>", "(JJ)V");
if (constructorFileResult == NULL) {
ALOGE("ERROR: could not find constructor %s(long, long)", CLASS_FILE_RESULT);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
return true;
}
void onVmCreatedCommon(JNIEnv* env) {
if (!initXposedBridge(env) || !initZygoteService(env)) {
return;
}
if (!onVmCreated(env)) {
return;
}
xposedLoadedSuccessfully = true;
return;
}
////////////////////////////////////////////////////////////
// JNI methods
////////////////////////////////////////////////////////////
jboolean XposedBridge_hadInitErrors(JNIEnv*, jclass) {
return !xposedLoadedSuccessfully;
}
jobject XposedBridge_getStartClassName(JNIEnv* env, jclass) {
return env->NewStringUTF(xposed->startClassName);
}
jboolean XposedBridge_startsSystemServer(JNIEnv*, jclass) {
return xposed->startSystemServer;
}
jint XposedBridge_getXposedVersion(JNIEnv*, jclass) {
return xposed->xposedVersionInt;
}
jboolean XposedBridge_initXResourcesNative(JNIEnv* env, jclass) {
classXResources = env->FindClass(CLASS_XRESOURCES);
if (classXResources == NULL) {
ALOGE("Error while loading XResources class '%s':", CLASS_XRESOURCES);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
classXResources = reinterpret_cast<jclass>(env->NewGlobalRef(classXResources));
if (register_natives_XResources(env, classXResources) != JNI_OK) {
ALOGE("Could not register natives for '%s'", CLASS_XRESOURCES);
env->ExceptionClear();
return false;
}
methodXResourcesTranslateResId = env->GetStaticMethodID(classXResources, "translateResId",
"(ILandroid/content/res/XResources;Landroid/content/res/Resources;)I");
if (methodXResourcesTranslateResId == NULL) {
ALOGE("ERROR: could not find method %s.translateResId(int, XResources, Resources)", CLASS_XRESOURCES);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
methodXResourcesTranslateAttrId = env->GetStaticMethodID(classXResources, "translateAttrId",
"(Ljava/lang/String;Landroid/content/res/XResources;)I");
if (methodXResourcesTranslateAttrId == NULL) {
ALOGE("ERROR: could not find method %s.findAttrId(String, XResources)", CLASS_XRESOURCES);
logExceptionStackTrace();
env->ExceptionClear();
return false;
}
return true;
}
void XResources_rewriteXmlReferencesNative(JNIEnv* env, jclass,
jlong parserPtr, jobject origRes, jobject repRes) {
using namespace android;
ResXMLParser* parser = (ResXMLParser*)parserPtr;
const ResXMLTree& mTree = parser->mTree;
uint32_t* mResIds = (uint32_t*)mTree.mResIds;
ResXMLTree_attrExt* tag;
int attrCount;
if (parser == NULL)
return;
do {
switch (parser->next()) {
case ResXMLParser::START_TAG:
tag = (ResXMLTree_attrExt*)parser->mCurExt;
attrCount = dtohs(tag->attributeCount);
for (int idx = 0; idx < attrCount; idx++) {
ResXMLTree_attribute* attr = (ResXMLTree_attribute*)
(((const uint8_t*)tag)
+ dtohs(tag->attributeStart)
+ (dtohs(tag->attributeSize)*idx));
// find resource IDs for attribute names
int32_t attrNameID = parser->getAttributeNameID(idx);
// only replace attribute name IDs for app packages
if (attrNameID >= 0 && (size_t)attrNameID < mTree.mNumResIds && dtohl(mResIds[attrNameID]) >= 0x7f000000) {
size_t attNameLen;
const char16_t* attrName = mTree.mStrings.stringAt(attrNameID, &attNameLen);
jint attrResID = env->CallStaticIntMethod(classXResources, methodXResourcesTranslateAttrId,
env->NewString((const jchar*)attrName, attNameLen), origRes);
if (env->ExceptionCheck())
goto leave;
mResIds[attrNameID] = htodl(attrResID);
}
// find original resource IDs for reference values (app packages only)
if (attr->typedValue.dataType != Res_value::TYPE_REFERENCE)
continue;
jint oldValue = dtohl(attr->typedValue.data);
if (oldValue < 0x7f000000)
continue;
jint newValue = env->CallStaticIntMethod(classXResources, methodXResourcesTranslateResId,
oldValue, origRes, repRes);
if (env->ExceptionCheck())
goto leave;
if (newValue != oldValue)
attr->typedValue.data = htodl(newValue);
}
continue;
case ResXMLParser::END_DOCUMENT:
case ResXMLParser::BAD_DOCUMENT:
goto leave;
default:
continue;
}
} while (true);
leave:
parser->restart();
}
jboolean ZygoteService_checkFileAccess(JNIEnv* env, jclass, jstring filenameJ, jint mode) {
#if XPOSED_WITH_SELINUX
ScopedUtfChars filename(env, filenameJ);
return xposed->zygoteservice_accessFile(filename.c_str(), mode) == 0;
#else // XPOSED_WITH_SELINUX
return false;
#endif // XPOSED_WITH_SELINUX
}
jobject ZygoteService_statFile(JNIEnv* env, jclass, jstring filenameJ) {
#if XPOSED_WITH_SELINUX
ScopedUtfChars filename(env, filenameJ);
struct stat st;
int result = xposed->zygoteservice_statFile(filename.c_str(), &st);
if (result != 0) {
if (errno == ENOENT) {
jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "No such file or directory: %s", filename.c_str());
} else {
jniThrowExceptionFmt(env, "java/io/IOException", "%s while reading %s", strerror(errno), filename.c_str());
}
return NULL;
}
return env->NewObject(classFileResult, constructorFileResult, (jlong) st.st_size, (jlong) st.st_mtime);
#else // XPOSED_WITH_SELINUX
return NULL;
#endif // XPOSED_WITH_SELINUX
}
jbyteArray ZygoteService_readFile(JNIEnv* env, jclass, jstring filenameJ) {
#if XPOSED_WITH_SELINUX
ScopedUtfChars filename(env, filenameJ);
int bytesRead = 0;
char* content = xposed->zygoteservice_readFile(filename.c_str(), &bytesRead);
if (content == NULL) {
if (errno == ENOENT) {
jniThrowExceptionFmt(env, "java/io/FileNotFoundException", "No such file or directory: %s", filename.c_str());
} else {
jniThrowExceptionFmt(env, "java/io/IOException", "%s while reading %s", strerror(errno), filename.c_str());
}
return NULL;
}
jbyteArray ret = env->NewByteArray(bytesRead);
if (ret != NULL) {
jbyte* arrptr = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
if (arrptr) {
memcpy(arrptr, content, bytesRead);
env->ReleasePrimitiveArrayCritical(ret, arrptr, 0);
}
}
free(content);
return ret;
#else // XPOSED_WITH_SELINUX
return NULL;
#endif // XPOSED_WITH_SELINUX
}
////////////////////////////////////////////////////////////
// JNI methods registrations
////////////////////////////////////////////////////////////
int register_natives_XposedBridge(JNIEnv* env, jclass clazz) {
const JNINativeMethod methods[] = {
NATIVE_METHOD(XposedBridge, hadInitErrors, "()Z"),
NATIVE_METHOD(XposedBridge, getStartClassName, "()Ljava/lang/String;"),
NATIVE_METHOD(XposedBridge, getRuntime, "()I"),
NATIVE_METHOD(XposedBridge, startsSystemServer, "()Z"),
NATIVE_METHOD(XposedBridge, getXposedVersion, "()I"),
NATIVE_METHOD(XposedBridge, initXResourcesNative, "()Z"),
NATIVE_METHOD(XposedBridge, hookMethodNative, "(Ljava/lang/reflect/Member;Ljava/lang/Class;ILjava/lang/Object;)V"),
NATIVE_METHOD(XposedBridge, setObjectClassNative, "(Ljava/lang/Object;Ljava/lang/Class;)V"),
NATIVE_METHOD(XposedBridge, dumpObjectNative, "(Ljava/lang/Object;)V"),
NATIVE_METHOD(XposedBridge, cloneToSubclassNative, "(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;"),
NATIVE_METHOD(XposedBridge, removeFinalFlagNative, "(Ljava/lang/Class;)V"),
#if PLATFORM_SDK_VERSION >= 21
NATIVE_METHOD(XposedBridge, invokeOriginalMethodNative,
"!(Ljava/lang/reflect/Member;I[Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
NATIVE_METHOD(XposedBridge, closeFilesBeforeForkNative, "()V"),
NATIVE_METHOD(XposedBridge, reopenFilesAfterForkNative, "()V"),
#endif
#if PLATFORM_SDK_VERSION >= 24
NATIVE_METHOD(XposedBridge, invalidateCallersNative, "([Ljava/lang/reflect/Member;)V"),
#endif
};
return env->RegisterNatives(clazz, methods, NELEM(methods));
}
int register_natives_XResources(JNIEnv* env, jclass clazz) {
const JNINativeMethod methods[] = {
NATIVE_METHOD(XResources, rewriteXmlReferencesNative, "(JLandroid/content/res/XResources;Landroid/content/res/Resources;)V"),
};
return env->RegisterNatives(clazz, methods, NELEM(methods));
}
int register_natives_ZygoteService(JNIEnv* env, jclass clazz) {
const JNINativeMethod methods[] = {
NATIVE_METHOD(ZygoteService, checkFileAccess, "(Ljava/lang/String;I)Z"),
NATIVE_METHOD(ZygoteService, statFile, "(Ljava/lang/String;)L" CLASS_FILE_RESULT ";"),
NATIVE_METHOD(ZygoteService, readFile, "(Ljava/lang/String;)[B"),
};
return env->RegisterNatives(clazz, methods, NELEM(methods));
}
} // namespace xposed