-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathdispatch.c
More file actions
847 lines (719 loc) · 20.6 KB
/
Copy pathdispatch.c
File metadata and controls
847 lines (719 loc) · 20.6 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
#include <linux/capability.h>
#include <linux/cred.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include "uapi/supercall.h"
#include "supercall/internal.h"
#include "arch.h" // IWYU pragma: keep
#include "policy/allowlist.h"
#include "policy/feature.h"
#include "klog.h" // IWYU pragma: keep
#include "ksu.h"
#include "runtime/ksud_boot.h"
#include "feature/kernel_umount.h"
#include "manager/manager_identity.h"
#include "selinux/selinux.h"
#include "infra/file_wrapper.h"
#include "hook/tp_marker.h"
#include "policy/app_profile.h"
#include "sulog/event.h"
#include "sulog/fd.h"
#include "supercall/supercall.h"
static int do_grant_root(void __user *arg)
{
int ret;
__u32 audit_uid = current_uid().val;
__u32 audit_euid = current_euid().val;
// we already check uid above on allowed_for_su()
pr_info("allow root for: %d\n", audit_uid);
ret = escape_with_root_profile();
ksu_sulog_emit_grant_root(ret, audit_uid, audit_euid, GFP_KERNEL);
return ret;
}
static int do_get_info(void __user *arg)
{
struct ksu_get_info_cmd cmd = { .version = KERNEL_SU_VERSION, .flags = 0 };
#ifdef MODULE
cmd.flags |= KSU_GET_INFO_FLAG_LKM;
#endif
if (is_manager()) {
cmd.flags |= KSU_GET_INFO_FLAG_MANAGER;
}
if (ksu_late_loaded) {
cmd.flags |= KSU_GET_INFO_FLAG_LATE_LOAD;
}
#ifdef EXPECTED_SIZE2
cmd.flags |= KSU_GET_INFO_FLAG_PR_BUILD;
#endif
cmd.features = KSU_FEATURE_MAX;
cmd.uapi_version = KERNEL_SU_UAPI_VERSION;
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("get_version: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_report_event(void __user *arg)
{
struct ksu_report_event_cmd cmd;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
return -EFAULT;
}
switch (cmd.event) {
case EVENT_POST_FS_DATA: {
static bool post_fs_data_lock = false;
if (!post_fs_data_lock) {
post_fs_data_lock = true;
if (ksu_late_loaded) {
pr_info("post-fs-data skipped (late load)\n");
} else {
pr_info("post-fs-data triggered\n");
on_post_fs_data();
}
}
break;
}
case EVENT_BOOT_COMPLETED: {
static bool boot_complete_lock = false;
if (!boot_complete_lock) {
boot_complete_lock = true;
if (ksu_late_loaded) {
pr_info("boot_complete skipped (late load)\n");
} else {
pr_info("boot_complete triggered\n");
on_boot_completed();
}
}
break;
}
case EVENT_MODULE_MOUNTED: {
pr_info("module mounted!\n");
on_module_mounted();
break;
}
default:
break;
}
return 0;
}
static int do_set_sepolicy(void __user *arg)
{
struct ksu_set_sepolicy_cmd cmd;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
return -EFAULT;
}
return handle_sepolicy((void __user *)cmd.data, cmd.data_len);
}
static int do_check_safemode(void __user *arg)
{
struct ksu_check_safemode_cmd cmd;
cmd.in_safe_mode = ksu_is_safe_mode();
if (cmd.in_safe_mode) {
pr_warn("safemode enabled!\n");
}
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("check_safemode: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_new_get_allow_list_common(void __user *arg, bool allow)
{
struct ksu_new_get_allow_list_cmd cmd;
int *arr = NULL;
int err = 0;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
return -EFAULT;
}
if (cmd.count) {
arr = kmalloc(sizeof(int) * cmd.count, GFP_KERNEL);
if (!arr) {
return -ENOMEM;
}
}
bool success = ksu_get_allow_list(arr, cmd.count, &cmd.count, &cmd.total_count, allow);
if (!success) {
err = -EFAULT;
goto out;
}
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("new_get_allow_list: copy_to_user count failed\n");
err = -EFAULT;
goto out;
}
if (cmd.count && copy_to_user(&((struct ksu_new_get_allow_list_cmd *)arg)->uids, arr, sizeof(int) * cmd.count)) {
pr_err("new_get_allow_list: copy_to_user uids failed\n");
err = -EFAULT;
}
out:
if (arr) {
kfree(arr);
}
return err;
}
static int do_new_get_deny_list(void __user *arg)
{
return do_new_get_allow_list_common(arg, false);
}
static int do_new_get_allow_list(void __user *arg)
{
return do_new_get_allow_list_common(arg, true);
}
static int do_get_allow_list_common(void __user *arg, bool allow)
{
int *arr = NULL;
int err = 0;
u16 count;
u32 out_count;
static const u16 kSize = 128;
arr = kmalloc(sizeof(int) * kSize, GFP_KERNEL);
if (!arr) {
return -ENOMEM;
}
bool success = ksu_get_allow_list(arr, kSize, &count, NULL, allow);
if (!success) {
err = -EFAULT;
goto out;
}
out_count = count;
if (copy_to_user(arg + offsetof(struct ksu_get_allow_list_cmd, count), &out_count, sizeof(u32))) {
pr_err("get_allow_list: copy_to_user count failed\n");
err = -EFAULT;
goto out;
}
if (copy_to_user(arg, arr, sizeof(u32) * count)) {
pr_err("get_allow_list: copy_to_user uids failed\n");
err = -EFAULT;
}
out:
if (arr) {
kfree(arr);
}
return err;
}
static int do_get_deny_list(void __user *arg)
{
return do_get_allow_list_common(arg, false);
}
static int do_get_allow_list(void __user *arg)
{
return do_get_allow_list_common(arg, true);
}
static int do_uid_granted_root(void __user *arg)
{
struct ksu_uid_granted_root_cmd cmd;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
return -EFAULT;
}
cmd.granted = ksu_is_allow_uid_for_current(cmd.uid);
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("uid_granted_root: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_uid_should_umount(void __user *arg)
{
struct ksu_uid_should_umount_cmd cmd;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
return -EFAULT;
}
cmd.should_umount = ksu_uid_should_umount(cmd.uid);
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("uid_should_umount: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_get_manager_appid(void __user *arg)
{
struct ksu_get_manager_appid_cmd cmd;
cmd.appid = ksu_get_manager_appid();
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("get_manager_appid: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_get_app_profile(void __user *arg)
{
#ifdef CONFIG_KSU_DISABLE_POLICY
return -EOPNOTSUPP;
#endif
uid_t uid;
struct app_profile *profile;
int ret = 0;
if (copy_from_user(&uid, (char __user *)arg + offsetof(struct ksu_get_app_profile_cmd, profile.curr_uid),
sizeof(uid_t))) {
pr_err("get_app_profile: copy_from_user failed\n");
return -EFAULT;
}
rcu_read_lock();
profile = ksu_get_app_profile(uid);
rcu_read_unlock();
if (!profile) {
ret = -ENOENT;
} else {
if (copy_to_user((char __user *)arg + offsetof(struct ksu_get_app_profile_cmd, profile), profile,
sizeof(struct app_profile))) {
pr_err("get_app_profile: copy_to_user failed\n");
ret = -EFAULT;
}
ksu_put_app_profile(profile);
}
return ret;
}
static int do_set_app_profile(void __user *arg)
{
#ifdef CONFIG_KSU_DISABLE_POLICY
return -EOPNOTSUPP;
#endif
struct ksu_set_app_profile_cmd cmd;
int ret;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
pr_err("set_app_profile: copy_from_user failed\n");
return -EFAULT;
}
ret = ksu_set_app_profile(&cmd.profile);
if (!ret) {
ksu_persistent_allow_list();
ksu_mark_running_process();
}
return ret;
}
static int do_get_feature(void __user *arg)
{
struct ksu_get_feature_cmd cmd;
bool supported;
int ret;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
pr_err("get_feature: copy_from_user failed\n");
return -EFAULT;
}
ret = ksu_get_feature(cmd.feature_id, &cmd.value, &supported);
cmd.supported = supported ? 1 : 0;
if (ret && supported) {
pr_err("get_feature: failed for feature %u: %d\n", cmd.feature_id, ret);
return ret;
}
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("get_feature: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_set_feature(void __user *arg)
{
struct ksu_set_feature_cmd cmd;
int ret;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
pr_err("set_feature: copy_from_user failed\n");
return -EFAULT;
}
ret = ksu_set_feature(cmd.feature_id, cmd.value);
if (ret) {
pr_err("set_feature: failed for feature %u: %d\n", cmd.feature_id, ret);
return ret;
}
return 0;
}
static int do_get_wrapper_fd(void __user *arg)
{
if (!ksu_file_sid) {
return -EINVAL;
}
struct ksu_get_wrapper_fd_cmd cmd;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
pr_err("get_wrapper_fd: copy_from_user failed\n");
return -EFAULT;
}
return ksu_install_file_wrapper(cmd.fd);
}
static int do_manage_mark(void __user *arg)
{
struct ksu_manage_mark_cmd cmd;
int ret = 0;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
pr_err("manage_mark: copy_from_user failed\n");
return -EFAULT;
}
switch (cmd.operation) {
case KSU_MARK_GET: {
// Get task mark status
ret = ksu_get_task_mark(cmd.pid);
if (ret < 0) {
pr_err("manage_mark: get failed for pid %d: %d\n", cmd.pid, ret);
return ret;
}
cmd.result = (u32)ret;
break;
}
case KSU_MARK_MARK: {
if (cmd.pid == 0) {
ksu_mark_all_process();
} else {
ret = ksu_set_task_mark(cmd.pid, true);
if (ret < 0) {
pr_err("manage_mark: set_mark failed for pid %d: %d\n", cmd.pid, ret);
return ret;
}
}
break;
}
case KSU_MARK_UNMARK: {
if (cmd.pid == 0) {
ksu_unmark_all_process();
} else {
ret = ksu_set_task_mark(cmd.pid, false);
if (ret < 0) {
pr_err("manage_mark: set_unmark failed for pid %d: %d\n", cmd.pid, ret);
return ret;
}
}
break;
}
case KSU_MARK_REFRESH: {
ksu_mark_running_process();
pr_info("manage_mark: refreshed running processes\n");
break;
}
default: {
pr_err("manage_mark: invalid operation %u\n", cmd.operation);
return -EINVAL;
}
}
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
pr_err("manage_mark: copy_to_user failed\n");
return -EFAULT;
}
return 0;
}
static int do_nuke_ext4_sysfs(void __user *arg)
{
struct ksu_nuke_ext4_sysfs_cmd cmd;
char mnt[256];
long ret;
if (copy_from_user(&cmd, arg, sizeof(cmd)))
return -EFAULT;
if (!cmd.arg)
return -EINVAL;
memset(mnt, 0, sizeof(mnt));
ret = strncpy_from_user(mnt, cmd.arg, sizeof(mnt));
if (ret < 0) {
pr_err("nuke ext4 copy mnt failed: %ld\n", ret);
return -EFAULT;
}
if (ret == sizeof(mnt)) {
pr_err("nuke ext4 mnt path too long\n");
return -ENAMETOOLONG;
}
pr_info("do_nuke_ext4_sysfs: %s\n", mnt);
return nuke_ext4_sysfs(mnt);
}
struct list_head mount_list = LIST_HEAD_INIT(mount_list);
DECLARE_RWSEM(mount_list_lock);
static int add_try_umount(void __user *arg)
{
struct mount_entry *new_entry, *entry, *tmp;
struct ksu_add_try_umount_cmd cmd;
char buf[256] = { 0 };
if (copy_from_user(&cmd, arg, sizeof cmd))
return -EFAULT;
switch (cmd.mode) {
case KSU_UMOUNT_WIPE: {
struct mount_entry *entry, *tmp;
down_write(&mount_list_lock);
list_for_each_entry_safe (entry, tmp, &mount_list, list) {
pr_info("wipe_umount_list: removing entry: %s\n", entry->umountable);
list_del(&entry->list);
kfree(entry->umountable);
kfree(entry);
}
up_write(&mount_list_lock);
return 0;
}
case KSU_UMOUNT_ADD: {
long len = strncpy_from_user(buf, (const char __user *)cmd.arg, 256);
if (len <= 0)
return -EFAULT;
buf[sizeof(buf) - 1] = '\0';
new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
if (!new_entry)
return -ENOMEM;
new_entry->umountable = kstrdup(buf, GFP_KERNEL);
if (!new_entry->umountable) {
kfree(new_entry);
return -ENOMEM;
}
down_write(&mount_list_lock);
// disallow dupes
// if this gets too many, we can consider moving this whole task to a kthread
list_for_each_entry (entry, &mount_list, list) {
if (!strcmp(entry->umountable, buf)) {
pr_info("cmd_add_try_umount: %s is already here!\n", buf);
up_write(&mount_list_lock);
kfree(new_entry->umountable);
kfree(new_entry);
return -EEXIST;
}
}
// now check flags and add
// this also serves as a null check
if (cmd.flags)
new_entry->flags = cmd.flags;
else
new_entry->flags = 0;
// debug
list_add(&new_entry->list, &mount_list);
up_write(&mount_list_lock);
pr_info("cmd_add_try_umount: %s added!\n", buf);
return 0;
}
// this is just strcmp'd wipe anyway
case KSU_UMOUNT_DEL: {
long len = strncpy_from_user(buf, (const char __user *)cmd.arg, sizeof(buf) - 1);
if (len <= 0)
return -EFAULT;
buf[sizeof(buf) - 1] = '\0';
down_write(&mount_list_lock);
list_for_each_entry_safe (entry, tmp, &mount_list, list) {
if (!strcmp(entry->umountable, buf)) {
pr_info("cmd_add_try_umount: entry removed: %s\n", entry->umountable);
list_del(&entry->list);
kfree(entry->umountable);
kfree(entry);
}
}
up_write(&mount_list_lock);
return 0;
}
default: {
pr_err("cmd_add_try_umount: invalid operation %u\n", cmd.mode);
return -EINVAL;
}
} // switch(cmd.mode)
return 0;
}
static int do_set_init_pgrp(void __user *arg)
{
int err;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
struct pid *pids[PIDTYPE_MAX] = { 0 };
#endif
write_lock_irq(&tasklist_lock);
struct task_struct *p = current->group_leader;
struct pid *init_group = task_pgrp(&init_task);
err = -EPERM;
if (task_session(p) != task_session(&init_task))
goto out;
err = 0;
if (task_pgrp(p) != init_group) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
change_pid(pids, p, PIDTYPE_PGID, init_group);
#else
change_pid(p, PIDTYPE_PGID, init_group);
#endif
}
out:
write_unlock_irq(&tasklist_lock);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
free_pids(pids);
#endif
return err;
}
static int do_get_sulog_fd(void __user *arg)
{
struct ksu_get_sulog_fd_cmd cmd;
if (copy_from_user(&cmd, arg, sizeof(cmd))) {
pr_err("get_sulog_fd: copy_from_user failed\n");
return -EFAULT;
}
if (cmd.flags) {
pr_err("get_sulog_fd: unsupported flags 0x%x\n", cmd.flags);
return -EINVAL;
}
return ksu_install_sulog_fd();
}
// IOCTL handlers mapping table
// clang-format off
static const struct ksu_ioctl_cmd_map ksu_ioctl_handlers[] = {
{
.cmd = KSU_IOCTL_GRANT_ROOT,
.name = "GRANT_ROOT",
.handler = do_grant_root,
.perm_check = allowed_for_su
},
{
.cmd = KSU_IOCTL_GET_INFO,
.name = "GET_INFO",
.handler = do_get_info,
.perm_check = always_allow
},
{
.cmd = KSU_IOCTL_REPORT_EVENT,
.name = "REPORT_EVENT",
.handler = do_report_event,
.perm_check = only_root
},
{
.cmd = KSU_IOCTL_SET_SEPOLICY,
.name = "SET_SEPOLICY",
.handler = do_set_sepolicy,
.perm_check = only_root
},
{
.cmd = KSU_IOCTL_CHECK_SAFEMODE,
.name = "CHECK_SAFEMODE",
.handler = do_check_safemode,
.perm_check = always_allow
},
{
.cmd = KSU_IOCTL_GET_ALLOW_LIST,
.name = "GET_ALLOW_LIST",
.handler = do_get_allow_list,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_GET_DENY_LIST,
.name = "GET_DENY_LIST",
.handler = do_get_deny_list,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_NEW_GET_ALLOW_LIST,
.name = "NEW_GET_ALLOW_LIST",
.handler = do_new_get_allow_list,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_NEW_GET_DENY_LIST,
.name = "NEW_GET_DENY_LIST",
.handler = do_new_get_deny_list,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_UID_GRANTED_ROOT,
.name = "UID_GRANTED_ROOT",
.handler = do_uid_granted_root,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_UID_SHOULD_UMOUNT,
.name = "UID_SHOULD_UMOUNT",
.handler = do_uid_should_umount,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_GET_MANAGER_APPID,
.name = "GET_MANAGER_APPID",
.handler = do_get_manager_appid,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_GET_APP_PROFILE,
.name = "GET_APP_PROFILE",
.handler = do_get_app_profile,
.perm_check = only_manager
},
{
.cmd = KSU_IOCTL_SET_APP_PROFILE,
.name = "SET_APP_PROFILE",
.handler = do_set_app_profile,
.perm_check = only_manager
},
{
.cmd = KSU_IOCTL_GET_FEATURE,
.name = "GET_FEATURE",
.handler = do_get_feature,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_SET_FEATURE,
.name = "SET_FEATURE",
.handler = do_set_feature,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_GET_WRAPPER_FD,
.name = "GET_WRAPPER_FD",
.handler = do_get_wrapper_fd,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_MANAGE_MARK,
.name = "MANAGE_MARK",
.handler = do_manage_mark,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_NUKE_EXT4_SYSFS,
.name = "NUKE_EXT4_SYSFS",
.handler = do_nuke_ext4_sysfs,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_ADD_TRY_UMOUNT,
.name = "ADD_TRY_UMOUNT",
.handler = add_try_umount,
.perm_check = manager_or_root
},
{
.cmd = KSU_IOCTL_SET_INIT_PGRP,
.name = "SET_INIT_PGRP",
.handler = do_set_init_pgrp,
.perm_check = only_root
},
{
.cmd = KSU_IOCTL_GET_SULOG_FD,
.name = "GET_SULOG_FD",
.handler = do_get_sulog_fd,
.perm_check = only_root
},
{
.cmd = 0,
.name = NULL,
.handler = NULL,
.perm_check = NULL
} // Sentinel
};
// clang-format on
long ksu_supercall_handle_ioctl(unsigned int cmd, void __user *argp)
{
int i;
#ifdef CONFIG_KSU_DEBUG
pr_info("ksu ioctl: cmd=0x%x from uid=%d\n", cmd, current_uid().val);
#endif
for (i = 0; ksu_ioctl_handlers[i].handler; i++) {
if (cmd == ksu_ioctl_handlers[i].cmd) {
// Check permission first
if (ksu_ioctl_handlers[i].perm_check && !ksu_ioctl_handlers[i].perm_check()) {
pr_warn("ksu ioctl: permission denied for cmd=0x%x uid=%d\n", cmd, current_uid().val);
return -EPERM;
}
// Execute handler
return ksu_ioctl_handlers[i].handler(argp);
}
}
pr_warn("ksu ioctl: unsupported command 0x%x\n", cmd);
return -ENOTTY;
}
void __init ksu_supercall_dump_commands(void)
{
int i;
pr_info("KernelSU IOCTL Commands:\n");
for (i = 0; ksu_ioctl_handlers[i].handler; i++) {
pr_info(" %-18s = 0x%08x\n", ksu_ioctl_handlers[i].name, ksu_ioctl_handlers[i].cmd);
}
}
void ksu_supercall_cleanup_state(void)
{
struct mount_entry *entry, *tmp;
down_write(&mount_list_lock);
list_for_each_entry_safe (entry, tmp, &mount_list, list) {
list_del(&entry->list);
kfree(entry->umountable);
kfree(entry);
}
up_write(&mount_list_lock);
}