Skip to content

Commit e4aca5f

Browse files
committed
kernel: remove workqueue for allowlist
1 parent 2b4f88f commit e4aca5f

File tree

5 files changed

+50
-73
lines changed

5 files changed

+50
-73
lines changed

kernel/allowlist.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ static uint8_t allow_list_bitmap[PAGE_SIZE] __read_mostly __aligned(PAGE_SIZE);
9090

9191
#define KERNEL_SU_ALLOWLIST "/data/adb/ksu/.allowlist"
9292

93-
static struct work_struct ksu_save_work;
94-
static struct work_struct ksu_load_work;
95-
96-
bool persistent_allow_list(void);
93+
void persistent_allow_list(void);
9794

9895
void ksu_show_allow_list(void)
9996
{
@@ -353,7 +350,7 @@ bool ksu_get_allow_list(int *array, int *length, bool allow)
353350
return true;
354351
}
355352

356-
void do_save_allow_list(struct work_struct *work)
353+
void persistent_allow_list()
357354
{
358355
u32 magic = FILE_MAGIC;
359356
u32 version = FILE_FORMAT_VERSION;
@@ -362,7 +359,7 @@ void do_save_allow_list(struct work_struct *work)
362359
loff_t off = 0;
363360

364361
struct file *fp =
365-
ksu_filp_open_compat(KERNEL_SU_ALLOWLIST, O_WRONLY | O_CREAT | O_TRUNC, 0644);
362+
filp_open(KERNEL_SU_ALLOWLIST, O_WRONLY | O_CREAT | O_TRUNC, 0644);
366363
if (IS_ERR(fp)) {
367364
pr_err("save_allow_list create file failed: %ld\n", PTR_ERR(fp));
368365
return;
@@ -395,7 +392,7 @@ void do_save_allow_list(struct work_struct *work)
395392
filp_close(fp, 0);
396393
}
397394

398-
void do_load_allow_list(struct work_struct *work)
395+
void ksu_load_allow_list()
399396
{
400397
loff_t off = 0;
401398
ssize_t ret = 0;
@@ -409,7 +406,7 @@ void do_load_allow_list(struct work_struct *work)
409406
#endif
410407

411408
// load allowlist now!
412-
fp = ksu_filp_open_compat(KERNEL_SU_ALLOWLIST, O_RDONLY, 0);
409+
fp = filp_open(KERNEL_SU_ALLOWLIST, O_RDONLY, 0);
413410
if (IS_ERR(fp)) {
414411
pr_err("load_allow_list open file failed: %ld\n", PTR_ERR(fp));
415412
return;
@@ -484,17 +481,6 @@ void ksu_prune_allowlist(bool (*is_uid_valid)(uid_t, char *, void *), void *data
484481
}
485482
}
486483

487-
// make sure allow list works cross boot
488-
bool persistent_allow_list(void)
489-
{
490-
return ksu_queue_work(&ksu_save_work);
491-
}
492-
493-
bool ksu_load_allow_list(void)
494-
{
495-
return ksu_queue_work(&ksu_load_work);
496-
}
497-
498484
void ksu_allowlist_init(void)
499485
{
500486
int i;
@@ -507,9 +493,6 @@ void ksu_allowlist_init(void)
507493

508494
INIT_LIST_HEAD(&allow_list);
509495

510-
INIT_WORK(&ksu_save_work, do_save_allow_list);
511-
INIT_WORK(&ksu_load_work, do_load_allow_list);
512-
513496
init_default_profiles();
514497
}
515498

@@ -518,7 +501,7 @@ void ksu_allowlist_exit(void)
518501
struct perm_data *np = NULL;
519502
struct perm_data *n = NULL;
520503

521-
do_save_allow_list(NULL);
504+
persistent_allow_list();
522505

523506
// free allowlist
524507
mutex_lock(&allowlist_mutex);

kernel/allowlist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void ksu_allowlist_init(void);
88

99
void ksu_allowlist_exit(void);
1010

11-
bool ksu_load_allow_list(void);
11+
void ksu_load_allow_list(void);
1212

1313
void ksu_show_allow_list(void);
1414

kernel/ksu.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
#include "ksud.h"
1616
#include "supercalls.h"
1717

18-
static struct workqueue_struct *ksu_workqueue;
19-
20-
bool ksu_queue_work(struct work_struct *work)
21-
{
22-
return queue_work(ksu_workqueue, work);
23-
}
24-
2518
int __init kernelsu_init(void)
2619
{
2720
#ifdef CONFIG_KSU_DEBUG
@@ -40,8 +33,6 @@ int __init kernelsu_init(void)
4033

4134
ksu_core_init();
4235

43-
ksu_workqueue = alloc_ordered_workqueue("kernelsu_work_queue", 0);
44-
4536
ksu_allowlist_init();
4637

4738
ksu_throne_tracker_init();
@@ -70,8 +61,6 @@ void kernelsu_exit(void)
7061

7162
ksu_observer_exit();
7263

73-
destroy_workqueue(ksu_workqueue);
74-
7564
#ifdef CONFIG_KPROBES
7665
ksu_ksud_exit();
7766
ksu_sucompat_exit();

kernel/ksu.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ struct app_profile {
8383
};
8484
};
8585

86-
bool ksu_queue_work(struct work_struct *work);
87-
8886
static inline int startswith(char *s, char *prefix)
8987
{
9088
return strncmp(s, prefix, strlen(prefix));

kernel/ksud.c

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <linux/rcupdate.h>
2+
#include <linux/slab.h>
3+
#include <linux/task_work.h>
14
#include "manager.h"
25
#include <asm/current.h>
36
#include <linux/compat.h>
@@ -149,10 +152,18 @@ static int __maybe_unused count(struct user_arg_ptr argv, int max)
149152
return i;
150153
}
151154

155+
static void on_post_fs_data_cbfun(struct callback_head *cb)
156+
{
157+
on_post_fs_data();
158+
}
159+
160+
static struct callback_head on_post_fs_data_cb = { .func =
161+
on_post_fs_data_cbfun };
162+
152163
// IMPORTANT NOTE: the call from execve_handler_pre WON'T provided correct value for envp and flags in GKI version
153164
int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
154-
struct user_arg_ptr *argv,
155-
struct user_arg_ptr *envp, int *flags)
165+
struct user_arg_ptr *argv,
166+
struct user_arg_ptr *envp, int *flags)
156167
{
157168
#ifndef CONFIG_KPROBES
158169
if (!ksu_execveat_hook) {
@@ -179,19 +190,17 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
179190
}
180191

181192
if (unlikely(!memcmp(filename->name, system_bin_init,
182-
sizeof(system_bin_init) - 1) &&
183-
argv)) {
193+
sizeof(system_bin_init) - 1) &&
194+
argv)) {
184195
// /system/bin/init executed
185196
int argc = count(*argv, MAX_ARG_STRINGS);
186197
pr_info("/system/bin/init argc: %d\n", argc);
187198
if (argc > 1 && !init_second_stage_executed) {
188199
const char __user *p = get_user_arg_ptr(*argv, 1);
189200
if (p && !IS_ERR(p)) {
190201
char first_arg[16];
191-
ksu_strncpy_from_user_nofault(
192-
first_arg, p, sizeof(first_arg));
193-
pr_info("/system/bin/init first arg: %s\n",
194-
first_arg);
202+
ksu_strncpy_from_user_nofault(first_arg, p, sizeof(first_arg));
203+
pr_info("/system/bin/init first arg: %s\n", first_arg);
195204
if (!strcmp(first_arg, "second_stage")) {
196205
pr_info("/system/bin/init second_stage executed\n");
197206
apply_kernelsu_rules();
@@ -203,8 +212,8 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
203212
}
204213
}
205214
} else if (unlikely(!memcmp(filename->name, old_system_init,
206-
sizeof(old_system_init) - 1) &&
207-
argv)) {
215+
sizeof(old_system_init) - 1) &&
216+
argv)) {
208217
// /init executed
209218
int argc = count(*argv, MAX_ARG_STRINGS);
210219
pr_info("/init argc: %d\n", argc);
@@ -213,8 +222,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
213222
const char __user *p = get_user_arg_ptr(*argv, 1);
214223
if (p && !IS_ERR(p)) {
215224
char first_arg[16];
216-
ksu_strncpy_from_user_nofault(
217-
first_arg, p, sizeof(first_arg));
225+
ksu_strncpy_from_user_nofault(first_arg, p, sizeof(first_arg));
218226
pr_info("/init first arg: %s\n", first_arg);
219227
if (!strcmp(first_arg, "--second-stage")) {
220228
pr_info("/init second_stage executed\n");
@@ -231,15 +239,13 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
231239
if (envc > 0) {
232240
int n;
233241
for (n = 1; n <= envc; n++) {
234-
const char __user *p =
235-
get_user_arg_ptr(*envp, n);
242+
const char __user *p = get_user_arg_ptr(*envp, n);
236243
if (!p || IS_ERR(p)) {
237244
continue;
238245
}
239246
char env[256];
240247
// Reading environment variable strings from user space
241-
if (ksu_strncpy_from_user_nofault(
242-
env, p, sizeof(env)) < 0)
248+
if (ksu_strncpy_from_user_nofault(env, p, sizeof(env)) < 0)
243249
continue;
244250
// Parsing environment variable names and values
245251
char *env_name = env;
@@ -250,14 +256,12 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
250256
*env_value = '\0';
251257
env_value++;
252258
// Check if the environment variable name and value are matching
253-
if (!strcmp(env_name,
254-
"INIT_SECOND_STAGE") &&
259+
if (!strcmp(env_name, "INIT_SECOND_STAGE") &&
255260
(!strcmp(env_value, "1") ||
256261
!strcmp(env_value, "true"))) {
257262
pr_info("/init second_stage executed\n");
258263
apply_kernelsu_rules();
259-
init_second_stage_executed =
260-
true;
264+
init_second_stage_executed = true;
261265
ksu_android_ns_fs_check();
262266
}
263267
}
@@ -266,11 +270,18 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
266270
}
267271

268272
if (unlikely(first_app_process && !memcmp(filename->name, app_process,
269-
sizeof(app_process) - 1))) {
273+
sizeof(app_process) - 1))) {
270274
first_app_process = false;
271275
pr_info("exec app_process, /data prepared, second_stage: %d\n",
272-
init_second_stage_executed);
273-
on_post_fs_data(); // we keep this for old ksud
276+
init_second_stage_executed);
277+
struct task_struct *init_task;
278+
rcu_read_lock();
279+
init_task = rcu_dereference(current->parent);
280+
if (init_task) {
281+
task_work_add(init_task, &on_post_fs_data_cb, TWA_RESUME);
282+
}
283+
rcu_read_unlock();
284+
274285
stop_execve_hook();
275286
}
276287

@@ -283,13 +294,12 @@ static struct file_operations fops_proxy;
283294
static ssize_t read_count_append = 0;
284295

285296
static ssize_t read_proxy(struct file *file, char __user *buf, size_t count,
286-
loff_t *pos)
297+
loff_t *pos)
287298
{
288299
bool first_read = file->f_pos == 0;
289300
ssize_t ret = orig_read(file, buf, count, pos);
290301
if (first_read) {
291-
pr_info("read_proxy append %ld + %ld\n", ret,
292-
read_count_append);
302+
pr_info("read_proxy append %ld + %ld\n", ret, read_count_append);
293303
ret += read_count_append;
294304
}
295305
return ret;
@@ -300,15 +310,14 @@ static ssize_t read_iter_proxy(struct kiocb *iocb, struct iov_iter *to)
300310
bool first_read = iocb->ki_pos == 0;
301311
ssize_t ret = orig_read_iter(iocb, to);
302312
if (first_read) {
303-
pr_info("read_iter_proxy append %ld + %ld\n", ret,
304-
read_count_append);
313+
pr_info("read_iter_proxy append %ld + %ld\n", ret, read_count_append);
305314
ret += read_count_append;
306315
}
307316
return ret;
308317
}
309318

310319
static int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
311-
size_t *count_ptr, loff_t **pos)
320+
size_t *count_ptr, loff_t **pos)
312321
{
313322
#ifndef CONFIG_KPROBES
314323
if (!ksu_vfs_read_hook) {
@@ -366,7 +375,7 @@ static int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
366375
size_t rc_count = strlen(KERNEL_SU_RC);
367376

368377
pr_info("vfs_read: %s, comm: %s, count: %zu, rc_count: %zu\n", dpath,
369-
current->comm, count, rc_count);
378+
current->comm, count, rc_count);
370379

371380
if (count < rc_count) {
372381
pr_err("count: %zu < rc_count: %zu\n", count, rc_count);
@@ -402,7 +411,7 @@ static int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
402411
}
403412

404413
static int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr,
405-
size_t *count_ptr)
414+
size_t *count_ptr)
406415
{
407416
struct file *file = fget(fd);
408417
if (!file) {
@@ -421,7 +430,7 @@ static bool is_volumedown_enough(unsigned int count)
421430
}
422431

423432
int ksu_handle_input_handle_event(unsigned int *type, unsigned int *code,
424-
int *value)
433+
int *value)
425434
{
426435
#ifndef CONFIG_KPROBES
427436
if (!ksu_input_hook) {
@@ -486,8 +495,7 @@ static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
486495
filename_in.name = path;
487496

488497
filename_p = &filename_in;
489-
return ksu_handle_execveat_ksud(AT_FDCWD, &filename_p, &argv, NULL,
490-
NULL);
498+
return ksu_handle_execveat_ksud(AT_FDCWD, &filename_p, &argv, NULL, NULL);
491499
}
492500

493501
static int sys_read_handler_pre(struct kprobe *p, struct pt_regs *regs)
@@ -501,7 +509,7 @@ static int sys_read_handler_pre(struct kprobe *p, struct pt_regs *regs)
501509
}
502510

503511
static int input_handle_event_handler_pre(struct kprobe *p,
504-
struct pt_regs *regs)
512+
struct pt_regs *regs)
505513
{
506514
unsigned int *type = (unsigned int *)&PT_REGS_PARM2(regs);
507515
unsigned int *code = (unsigned int *)&PT_REGS_PARM3(regs);
@@ -519,7 +527,6 @@ static struct kprobe vfs_read_kp = {
519527
.pre_handler = sys_read_handler_pre,
520528
};
521529

522-
523530
static struct kprobe input_event_kp = {
524531
.symbol_name = "input_event",
525532
.pre_handler = input_handle_event_handler_pre,

0 commit comments

Comments
 (0)