-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathksu.cc
More file actions
209 lines (175 loc) · 4.93 KB
/
Copy pathksu.cc
File metadata and controls
209 lines (175 loc) · 4.93 KB
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
//
// Created by weishu on 2022/12/9.
//
#include <sys/prctl.h>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <utility>
#include <android/log.h>
#include <dirent.h>
#include <cstdlib>
#include <unistd.h>
#include <climits>
#include <sys/syscall.h>
#include "ksu.h"
static int fd = -1;
static inline int scan_driver_fd() {
const char *kName = "[ksu_driver]";
DIR *dir = opendir("/proc/self/fd");
if (!dir) {
return -1;
}
int found = -1;
struct dirent *de;
char path[64];
char target[PATH_MAX];
while ((de = readdir(dir)) != NULL) {
if (de->d_name[0] == '.') {
continue;
}
char *endptr = NULL;
long fd_long = strtol(de->d_name, &endptr, 10);
if (!de->d_name[0] || *endptr != '\0' || fd_long < 0 || fd_long > INT_MAX) {
continue;
}
snprintf(path, sizeof(path), "/proc/self/fd/%s", de->d_name);
ssize_t n = readlink(path, target, sizeof(target) - 1);
if (n < 0) {
continue;
}
target[n] = '\0';
const char *base = strrchr(target, '/');
base = base ? base + 1 : target;
if (strstr(base, kName)) {
found = (int)fd_long;
break;
}
}
closedir(dir);
return found;
}
template<typename... Args>
static int ksuctl(unsigned long op, Args &&... args) {
if (fd < 0) {
fd = scan_driver_fd();
}
static_assert(sizeof...(Args) <= 1, "ioctl expects at most one extra argument");
return ioctl(fd, op, std::forward<Args>(args)...);
}
static struct ksu_get_info_cmd g_version {};
struct ksu_get_info_cmd get_info() {
if (!g_version.version) {
ksuctl(KSU_IOCTL_GET_INFO, &g_version);
}
return g_version;
}
bool is_uapi_version_mismatch() {
auto info = get_info();
return info.uapi_version != KERNEL_SU_UAPI_VERSION;
}
uint32_t get_version() {
auto info = get_info();
return info.version;
}
bool get_allow_list(struct ksu_new_get_allow_list_cmd *cmd) {
return ksuctl(KSU_IOCTL_NEW_GET_ALLOW_LIST, cmd) == 0;
}
bool is_safe_mode() {
struct ksu_check_safemode_cmd cmd = {};
ksuctl(KSU_IOCTL_CHECK_SAFEMODE, &cmd);
return cmd.in_safe_mode;
}
bool is_lkm_mode() {
auto info = get_info();
if (info.version > 0) {
return (info.flags & KSU_GET_INFO_FLAG_LKM) != 0;
}
return (legacy_get_info().second & KSU_GET_INFO_FLAG_LKM) != 0;
}
bool is_late_load_mode() {
auto info = get_info();
if (info.version > 0) {
return (info.flags & KSU_GET_INFO_FLAG_LATE_LOAD) != 0;
}
return false;
}
bool is_manager() {
auto info = get_info();
if (info.version > 0) {
return (info.flags & KSU_GET_INFO_FLAG_MANAGER) != 0;
}
return legacy_get_info().first > 0;
}
bool is_pr_build() {
auto info = get_info();
if (info.version > 0) {
return (info.flags & KSU_GET_INFO_FLAG_PR_BUILD) != 0;
}
return false;
}
bool uid_should_umount(int uid) {
struct ksu_uid_should_umount_cmd cmd = {};
cmd.uid = uid;
ksuctl(KSU_IOCTL_UID_SHOULD_UMOUNT, &cmd);
return cmd.should_umount;
}
bool set_app_profile(const app_profile *profile) {
struct ksu_set_app_profile_cmd cmd = {};
cmd.profile = *profile;
return ksuctl(KSU_IOCTL_SET_APP_PROFILE, &cmd) == 0;
}
int get_app_profile(app_profile *profile) {
struct ksu_get_app_profile_cmd cmd = {.profile = *profile};
int ret = ksuctl(KSU_IOCTL_GET_APP_PROFILE, &cmd);
*profile = cmd.profile;
return ret;
}
bool set_su_enabled(bool enabled) {
struct ksu_set_feature_cmd cmd = {};
cmd.feature_id = KSU_FEATURE_SU_COMPAT;
cmd.value = enabled ? 1 : 0;
return ksuctl(KSU_IOCTL_SET_FEATURE, &cmd) == 0;
}
bool is_su_enabled() {
struct ksu_get_feature_cmd cmd = {};
cmd.feature_id = KSU_FEATURE_SU_COMPAT;
if (ksuctl(KSU_IOCTL_GET_FEATURE, &cmd) != 0) {
return false;
}
if (!cmd.supported) {
return false;
}
return cmd.value != 0;
}
static inline bool get_feature(uint32_t feature_id, uint64_t *out_value, bool *out_supported) {
struct ksu_get_feature_cmd cmd = {};
cmd.feature_id = feature_id;
if (ksuctl(KSU_IOCTL_GET_FEATURE, &cmd) != 0) {
return false;
}
if (out_value) *out_value = cmd.value;
if (out_supported) *out_supported = cmd.supported;
return true;
}
static inline bool set_feature(uint32_t feature_id, uint64_t value) {
struct ksu_set_feature_cmd cmd = {};
cmd.feature_id = feature_id;
cmd.value = value;
return ksuctl(KSU_IOCTL_SET_FEATURE, &cmd) == 0;
}
bool set_kernel_umount_enabled(bool enabled) {
return set_feature(KSU_FEATURE_KERNEL_UMOUNT, enabled ? 1 : 0);
}
bool is_kernel_umount_enabled() {
uint64_t value = 0;
bool supported = false;
if (!get_feature(KSU_FEATURE_KERNEL_UMOUNT, &value, &supported)) {
return false;
}
if (!supported) {
return false;
}
return value != 0;
}