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
|
/*
* Copyright (C) 2023 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <android/log.h>
#include "zygisk.hpp"
using zygisk::Api;
using zygisk::ServerSpecializeArgs;
static const char *package_whitelist[] = {
"com.wireguard.android",
"com.wireguard.android.debug",
};
#define LOG_TAG "WireGuard/TunFlags"
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
static bool is_wireguard(JNIEnv *env, jobject vpn_obj)
{
jclass clazz = env->GetObjectClass(vpn_obj);
if (!clazz) {
ALOGE("Could not determine class of VPN object");
return false;
}
jfieldID package_field = env->GetFieldID(clazz, "mPackage", "Ljava/lang/String;");
if (!package_field) {
ALOGE("Could not find package field ID");
return false;
}
jstring package_obj = static_cast<jstring>(env->GetObjectField(vpn_obj, package_field));
if (!package_obj) {
ALOGE("Could not get package field");
return false;
}
const char *package = env->GetStringUTFChars(package_obj, NULL);
bool ret = false;
for (size_t i = 0; i < sizeof(package_whitelist) / sizeof(*package_whitelist); ++i) {
if (!strcmp(package_whitelist[i], package)) {
ret = true;
break;
}
}
env->ReleaseStringUTFChars(package_obj, package);
return ret;
}
static jint(*orig_create)(JNIEnv *, jobject, jint);
/* Reimplementation of https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/jni/com_android_server_connectivity_Vpn.cpp;l=59;drc=586d0eb1fef2114f01606f4275043abc0f40ff0b */
static jint create(JNIEnv *env, jobject thiz, jint mtu)
{
if (!is_wireguard(env, thiz))
return orig_create(env, thiz, mtu);
ALOGI("Using hooked function to create IFF_VNET_HDR tun device");
int tun = open("/dev/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (tun < 0) {
ALOGE("Cannot allocate TUN: %s", strerror(errno));
return -1;
}
/* Allocate interface */
ifreq ifr4 = { 0 };
ifr4.ifr_flags = IFF_TUN | IFF_NO_PI | IFF_VNET_HDR;
if (ioctl(tun, TUNSETIFF, &ifr4) < 0) {
ALOGE("Cannot allocate TUN: %s", strerror(errno));
goto error;
}
/* Activate interface */
static int inet4 = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
ifr4.ifr_flags = IFF_UP;
if (ioctl(inet4, SIOCSIFFLAGS, &ifr4) < 0) {
ALOGE("Cannot activate %s: %s", ifr4.ifr_name, strerror(errno));
goto error;
}
/* Set MTU if it is specified */
ifr4.ifr_mtu = mtu;
if (mtu > 0 && ioctl(inet4, SIOCSIFMTU, &ifr4) < 0) {
ALOGE("Cannot set MTU on %s: %s", ifr4.ifr_name, strerror(errno));
goto error;
}
return tun;
error:
close(tun);
return -1;
}
static jint(*orig_register_natives)(JNIEnv *, jclass, const JNINativeMethod *, jint);
static jint register_natives(JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint num_methods)
{
static jmethodID class_getname = env->GetMethodID(env->FindClass("java/lang/Class"), "getName", "()Ljava/lang/String;");
jstring name_string = static_cast<jstring>(env->CallObjectMethod(clazz, class_getname));
const char *name = env->GetStringUTFChars(name_string, nullptr);
bool is_vpn = !strcmp(name, "com.android.server.connectivity.Vpn");
env->ReleaseStringUTFChars(name_string, name);
if (!is_vpn)
goto out;
for (jint i = 0; i < num_methods; ++i) {
if (strcmp(methods[i].name, "jniCreate"))
continue;
orig_create = reinterpret_cast<decltype(orig_create)>(methods[i].fnPtr);
JNINativeMethod *new_methods = new JNINativeMethod[num_methods];
memcpy(new_methods, methods, num_methods * sizeof(*new_methods));
new_methods[i].fnPtr = reinterpret_cast<decltype(new_methods[i].fnPtr)>(create);
methods = new_methods;
break;
}
out:
return orig_register_natives(env, clazz, methods, num_methods);
}
class TunFlags : public zygisk::ModuleBase
{
public:
void onLoad(Api *api, JNIEnv *env) override
{
this->api = api;
this->env = env;
}
void postServerSpecialize(const ServerSpecializeArgs *args) override
{
orig_register_natives = env->functions->RegisterNatives;
JNINativeInterface *new_functions = new JNINativeInterface;
*new_functions = *env->functions;
new_functions->RegisterNatives = register_natives;
env->functions = new_functions;
}
private:
Api *api;
JNIEnv *env;
};
REGISTER_ZYGISK_MODULE(TunFlags)
|