-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathsupercall.c
More file actions
433 lines (378 loc) · 12.3 KB
/
supercall.c
File metadata and controls
433 lines (378 loc) · 12.3 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2023 bmax121. All Rights Reserved.
*/
#include <ktypes.h>
#include <uapi/scdefs.h>
#include <hook.h>
#include <common.h>
#include <log.h>
#include <predata.h>
#include <pgtable.h>
#include <linux/syscall.h>
#include <uapi/asm-generic/errno.h>
#include <linux/uaccess.h>
#include <linux/cred.h>
#include <asm/current.h>
#include <linux/string.h>
#include <linux/pid.h>
#include <linux/sched.h>
#include <linux/security.h>
#include <syscall.h>
#include <accctl.h>
#include <module.h>
#include <kputils.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <kputils.h>
#include <predata.h>
#include <linux/random.h>
#include <sucompat.h>
#include <accctl.h>
#include <kstorage.h>
#define MAX_KEY_LEN 128
#include <linux/umh.h>
static long call_test(long arg1, long arg2, long arg3)
{
return 0;
}
static long call_bootlog()
{
print_bootlog();
return 0;
}
static long call_panic()
{
unsigned long panic_addr = kallsyms_lookup_name("panic");
((void (*)(const char *fmt, ...))panic_addr)("!!!! kernel_patch panic !!!!");
return 0;
}
static long call_klog(const char __user *arg1)
{
char buf[1024];
long len = compat_strncpy_from_user(buf, arg1, sizeof(buf));
if (len <= 0) return -EINVAL;
if (len > 0) logki("user log: %s", buf);
return 0;
}
static long call_buildtime(char __user *out_buildtime, int u_len)
{
const char *buildtime = get_build_time();
int len = strlen(buildtime);
if (len >= u_len) return -ENOMEM;
int rc = compat_copy_to_user(out_buildtime, buildtime, len + 1);
return rc;
}
static long call_kpm_load(const char __user *arg1, const char *__user arg2, void *__user reserved)
{
char path[1024], args[KPM_ARGS_LEN];
long pathlen = compat_strncpy_from_user(path, arg1, sizeof(path));
if (pathlen <= 0) return -EINVAL;
long arglen = compat_strncpy_from_user(args, arg2, sizeof(args));
return load_module_path(path, arglen <= 0 ? 0 : args, reserved);
}
static long call_kpm_control(const char __user *arg1, const char *__user arg2, void *__user out_msg, int outlen)
{
char name[KPM_NAME_LEN], args[KPM_ARGS_LEN];
long namelen = compat_strncpy_from_user(name, arg1, sizeof(name));
if (namelen <= 0) return -EINVAL;
long arglen = compat_strncpy_from_user(args, arg2, sizeof(args));
return module_control0(name, arglen <= 0 ? 0 : args, out_msg, outlen);
}
static long call_kpm_unload(const char *__user arg1, void *__user reserved)
{
char name[KPM_NAME_LEN];
long len = compat_strncpy_from_user(name, arg1, sizeof(name));
if (len <= 0) return -EINVAL;
return unload_module(name, reserved);
}
static long call_kpm_nums()
{
return get_module_nums();
}
static long call_kpm_list(char *__user names, int len)
{
if (len <= 0) return -EINVAL;
char buf[4096];
int sz = list_modules(buf, sizeof(buf));
if (sz > len) return -ENOBUFS;
sz = compat_copy_to_user(names, buf, len);
return sz;
}
static long call_kpm_info(const char *__user uname, char *__user out_info, int out_len)
{
if (out_len <= 0) return -EINVAL;
char name[64];
char buf[2048];
int len = compat_strncpy_from_user(name, uname, sizeof(name));
if (len <= 0) return -EINVAL;
int sz = get_module_info(name, buf, sizeof(buf));
if (sz < 0) return sz;
if (sz > out_len) return -ENOBUFS;
sz = compat_copy_to_user(out_info, buf, sz);
return sz;
}
static long call_su(struct su_profile *__user uprofile)
{
struct su_profile *profile = memdup_user(uprofile, sizeof(struct su_profile));
if (!profile || IS_ERR(profile)) return PTR_ERR(profile);
profile->scontext[sizeof(profile->scontext) - 1] = '\0';
int rc = commit_su(profile->to_uid, profile->scontext);
kvfree(profile);
return rc;
}
static long call_su_task(pid_t pid, struct su_profile *__user uprofile)
{
struct su_profile *profile = memdup_user(uprofile, sizeof(struct su_profile));
if (!profile || IS_ERR(profile)) return PTR_ERR(profile);
profile->scontext[sizeof(profile->scontext) - 1] = '\0';
int rc = task_su(pid, profile->to_uid, profile->scontext);
kvfree(profile);
return rc;
}
static long call_skey_get(char *__user out_key, int out_len)
{
const char *key = get_superkey();
int klen = strlen(key);
if (klen >= out_len) return -ENOMEM;
int rc = compat_copy_to_user(out_key, key, klen + 1);
return rc;
}
static long call_skey_set(char *__user new_key)
{
char buf[SUPER_KEY_LEN];
int len = compat_strncpy_from_user(buf, new_key, sizeof(buf));
if (len >= SUPER_KEY_LEN && buf[SUPER_KEY_LEN - 1]) return -E2BIG;
reset_superkey(new_key);
return 0;
}
static long call_skey_root_enable(int enable)
{
enable_auth_root_key(enable);
return 0;
}
static long call_grant_uid(struct su_profile *__user uprofile)
{
struct su_profile *profile = memdup_user(uprofile, sizeof(struct su_profile));
if (!profile || IS_ERR(profile)) return PTR_ERR(profile);
int rc = su_add_allow_uid(profile->uid, profile->to_uid, profile->scontext);
kvfree(profile);
return rc;
}
static long call_revoke_uid(uid_t uid)
{
return su_remove_allow_uid(uid);
}
static long call_su_allow_uid_nums()
{
return su_allow_uid_nums();
}
#ifdef ANDROID
extern int android_is_safe_mode;
static long call_su_get_safemode()
{
int result = android_is_safe_mode;
logkfd("[call_su_get_safemode] %d\n", result);
return result;
}
extern int load_ap_package_config(void);
static long call_ap_load_package_config()
{
int result = load_ap_package_config();
logkfd("[call_ap_load_package_config] loaded %d entries\n", result);
return result;
}
#endif
static long call_su_list_allow_uid(uid_t *__user uids, int num)
{
return su_allow_uids(1, uids, num);
}
static long call_su_allow_uid_profile(uid_t uid, struct su_profile *__user uprofile)
{
return su_allow_uid_profile(1, uid, uprofile);
}
static long call_reset_su_path(const char *__user upath)
{
return su_reset_path(strndup_user(upath, SU_PATH_MAX_LEN));
}
static long call_su_get_path(char *__user ubuf, int buf_len)
{
const char *path = su_get_path();
int len = strlen(path);
if (buf_len <= len) return -ENOBUFS;
return compat_copy_to_user(ubuf, path, len + 1);
}
static long call_su_get_allow_sctx(char *__user usctx, int ulen)
{
int len = strlen(all_allow_sctx);
if (ulen <= len) return -ENOBUFS;
return compat_copy_to_user(usctx, all_allow_sctx, len + 1);
}
static long call_su_set_allow_sctx(char *__user usctx)
{
char buf[SUPERCALL_SCONTEXT_LEN];
buf[0] = '\0';
int len = compat_strncpy_from_user(buf, usctx, sizeof(buf));
if (len >= SUPERCALL_SCONTEXT_LEN && buf[SUPERCALL_SCONTEXT_LEN - 1]) return -E2BIG;
return set_all_allow_sctx(buf);
}
static long call_kstorage_read(int gid, long did, void *out_data, int offset, int dlen)
{
return read_kstorage(gid, did, out_data, offset, dlen, true);
}
static long call_kstorage_write(int gid, long did, void *data, int offset, int dlen)
{
return write_kstorage(gid, did, data, offset, dlen, true);
}
static long call_list_kstorage_ids(int gid, long *ids, int ids_len)
{
return list_kstorage_ids(gid, ids, ids_len, false);
}
static long call_kstorage_remove(int gid, long did)
{
return remove_kstorage(gid, did);
}
static long supercall(int is_key_auth, long cmd, long arg1, long arg2, long arg3, long arg4)
{
switch (cmd) {
case SUPERCALL_HELLO:
logki(SUPERCALL_HELLO_ECHO "\n");
return SUPERCALL_HELLO_MAGIC;
case SUPERCALL_KLOG:
return call_klog((const char *__user)arg1);
case SUPERCALL_KERNELPATCH_VER:
return kpver;
case SUPERCALL_KERNEL_VER:
return kver;
case SUPERCALL_BUILD_TIME:
return call_buildtime((char *__user)arg1, (int)arg2);
#ifdef ANDROID
case SUPERCALL_AP_LOAD_PACKAGE_CONFIG:
return call_ap_load_package_config();
#endif
}
switch (cmd) {
case SUPERCALL_SU:
return call_su((struct su_profile * __user) arg1);
case SUPERCALL_SU_TASK:
return call_su_task((pid_t)arg1, (struct su_profile * __user) arg2);
case SUPERCALL_SU_GRANT_UID:
return call_grant_uid((struct su_profile * __user) arg1);
case SUPERCALL_SU_REVOKE_UID:
return call_revoke_uid((uid_t)arg1);
case SUPERCALL_SU_NUMS:
return call_su_allow_uid_nums();
case SUPERCALL_SU_LIST:
return call_su_list_allow_uid((uid_t *)arg1, (int)arg2);
case SUPERCALL_SU_PROFILE:
return call_su_allow_uid_profile((uid_t)arg1, (struct su_profile * __user) arg2);
case SUPERCALL_SU_RESET_PATH:
return call_reset_su_path((const char *)arg1);
case SUPERCALL_SU_GET_PATH:
return call_su_get_path((char *__user)arg1, (int)arg2);
case SUPERCALL_SU_GET_ALLOW_SCTX:
return call_su_get_allow_sctx((char *__user)arg1, (int)arg2);
case SUPERCALL_SU_SET_ALLOW_SCTX:
return call_su_set_allow_sctx((char *__user)arg1);
case SUPERCALL_KSTORAGE_READ:
return call_kstorage_read((int)arg1, (long)arg2, (void *)arg3, (int)((long)arg4 >> 32), (long)arg4 << 32 >> 32);
case SUPERCALL_KSTORAGE_WRITE:
return call_kstorage_write((int)arg1, (long)arg2, (void *)arg3, (int)((long)arg4 >> 32),
(long)arg4 << 32 >> 32);
case SUPERCALL_KSTORAGE_LIST_IDS:
return call_list_kstorage_ids((int)arg1, (long *)arg2, (int)arg3);
case SUPERCALL_KSTORAGE_REMOVE:
return call_kstorage_remove((int)arg1, (long)arg2);
#ifdef ANDROID
case SUPERCALL_SU_GET_SAFEMODE:
return call_su_get_safemode();
#endif
default:
break;
}
switch (cmd) {
case SUPERCALL_BOOTLOG:
return call_bootlog();
case SUPERCALL_PANIC:
return call_panic();
case SUPERCALL_TEST:
return call_test(arg1, arg2, arg3);
default:
break;
}
if (!is_key_auth) return -EPERM;
switch (cmd) {
case SUPERCALL_SKEY_GET:
return call_skey_get((char *__user)arg1, (int)arg2);
case SUPERCALL_SKEY_SET:
return call_skey_set((char *__user)arg1);
case SUPERCALL_SKEY_ROOT_ENABLE:
return call_skey_root_enable((int)arg1);
break;
}
switch (cmd) {
case SUPERCALL_KPM_LOAD:
return call_kpm_load((const char *__user)arg1, (const char *__user)arg2, (void *__user)arg3);
case SUPERCALL_KPM_UNLOAD:
return call_kpm_unload((const char *__user)arg1, (void *__user)arg2);
case SUPERCALL_KPM_CONTROL:
return call_kpm_control((const char *__user)arg1, (const char *__user)arg2, (char *__user)arg3, (int)arg4);
case SUPERCALL_KPM_NUMS:
return call_kpm_nums();
case SUPERCALL_KPM_LIST:
return call_kpm_list((char *__user)arg1, (int)arg2);
case SUPERCALL_KPM_INFO:
return call_kpm_info((const char *__user)arg1, (char *__user)arg2, (int)arg3);
}
switch (cmd) {
default:
break;
}
return -ENOSYS;
}
static void before(hook_fargs6_t *args, void *udata)
{
const char *__user ukey = (const char *__user)syscall_argn(args, 0);
long ver_xx_cmd = (long)syscall_argn(args, 1);
// todo: from 0.10.5
// uint32_t ver = (ver_xx_cmd & 0xFFFFFFFF00000000ul) >> 32;
// long xx = (ver_xx_cmd & 0xFFFF0000) >> 16;
long cmd = ver_xx_cmd & 0xFFFF;
if (cmd < SUPERCALL_HELLO || cmd > SUPERCALL_MAX) return;
char key[MAX_KEY_LEN];
long len = compat_strncpy_from_user(key, ukey, MAX_KEY_LEN);
if (len <= 0) return;
int is_key_auth = 0;
if (!auth_superkey(key)) {
is_key_auth = 1;
} else if (!strcmp("su", key)) {
uid_t uid = current_uid();
if (!is_su_allow_uid(uid)) return;
} else {
return;
}
long a1 = (long)syscall_argn(args, 2);
long a2 = (long)syscall_argn(args, 3);
long a3 = (long)syscall_argn(args, 4);
long a4 = (long)syscall_argn(args, 5);
args->skip_origin = 1;
args->ret = supercall(is_key_auth, cmd, a1, a2, a3, a4);
}
int supercall_install()
{
int rc = 0;
hook_err_t err = hook_syscalln(__NR_supercall, 6, before, 0, 0);
if (err) {
log_boot("install supercall 64-bit hook error: %d\n", err);
rc = err;
goto out;
}
err = hook_compat_syscalln(92, 6, before, 0, 0); // __NR_truncate == __NR_supercall for 32-bit
if (err) {
log_boot("install supercall 32-bit hook error: %d\n", err);
rc = err;
goto out;
}
out:
return rc;
}