Commit Graph

612 Commits

Author SHA1 Message Date
ShirkNeko
351dc15d08 Fix lld link ksu_current_uid problem 2025-08-16 14:34:51 +08:00
M. Faris
ff6a68221f kernel: add wrapper for creds, refine disable_seccomp, revert some changes (#131)
1. Wrapper for creds:
* Some older kernel does not have {.val}, so, for nicer compatibility support and clean code,
make some wrapper for credential use.
* After this change, do not use current_uid().val, instead, use ksu_current_uid(). For more
info, check kernel/include/ksu_creds.h.

2. Refine disable_seccomp (need to add k6.11+ support)
https://github.com/tiann/KernelSU/pull/2708
https://github.com/tiann/KernelSU/issues/2706

3. Revert "Handle unmount for isolated process correctly"
Reason: https://github.com/tiann/KernelSU/pull/2696#issuecomment-3181866301

4. consolidate most of the gaps

Co-authored-by: Wang Han <416810799@qq.com>
2025-08-16 13:11:58 +08:00
rsuntk
665091f37d kernel: selinux: dontaudit untrusted_app su dir { getattr }
* Following the advice that was given by member in rksu group, by replacing ALL to untrusted_app.

$ /system/bin/stat /proc/1
Result:
08-15 14:57:54.370 20062 20062 W stat    : type=1400 audit(0.0:9564): avc:  denied  { getattr } for  path="/proc/1" dev="proc" ino=12308 scontext=u:r:untrusted_app_27:s0:c27,c258,c512,c768 tcontext=u:r:init:s0 tclass=dir permissive=0 app=com.termux
(issue
438bd5fd6d (commitcomment-163785768))

Test: Checker pass.

* Any issue? Let me know.

Tested-by: rsuntk <rsuntk@yukiprjkt.my.id>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-16 12:45:04 +08:00
rsuntk
963717e000 Revert "kernel: selinux: dontaudit * su dir getattr"
* Maybe there's much better solution than this.

This reverts commit 438bd5fd6dac74ba63ef627124f0d2f552b1cb31.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-16 12:44:55 +08:00
rsuntk
deed7a7903 kernel: move some defs to single file
* Much cleaner code, although setenforce is not used anymore
* Guard is_ksu_transition only for 4.19 and under.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-16 12:44:41 +08:00
ShirkNeko
68f2f5a0ae kernel: Introducing Tracepoint Hook Type Support
Tracepoint is a predefined hook point in the kernel, compared to Kprobe,
it is more stable and has lower performance overhead, although compatibility
is relatively poor, it is still worth trying

By the way, we have also included the config definitions related to hook types
in Kconfig, to enhance cleanliness

Improve and merge types that do not require hooks

Introducing the hook type prctl

These patches is based on backslashxx/KernelSU#5

Co-authored-by: Cloud_Yun <1770669041@qq.com>
Co-authored-by: Prslc <prslc113@gmail.com>
Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>

Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-08-14 22:19:07 +08:00
rsuntk
3a5bcb0e09 kernel: selinux: dontaudit * su dir getattr
* Likely a detection point for newer android.

* I am not sure about this, but a module try to address this: https://github.com/aviraxp/ZN-AuditPatch

* Need more testing.

Suggested-by: fatalcoder524 <11532648+fatalcoder524@users.noreply.github.com>
Tested-by: rsuntk <rsuntk@yukiprjkt.my.id>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-10 18:03:02 +08:00
rsuntk
441d06b065 kernel: fix mismerge
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-10 18:02:52 +08:00
Wang Han
347ffa389e kernel: Handle unmount for isolated process correctly
Isolated processes can be directly forked from zygote, but current code doesn't handle it well. Fix it by unmounting unconditionally if isolated process is forked from zygote.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-10 18:02:41 +08:00
Tashfin Shakeer Rhythm
157df04c8b kernel: selinux: rules: Remove unnecessary RCU dereference in get_policydb()
get_policydb() uses rcu_dereference() to read pointers to selinux_state.policy.
But in the SELinux implementation, these pointers are assigned once during
initialization and never changed with rcu_assign_pointer(), rendering the
rcu_dereference() call in get_policydb() completely useless. This just adds
unwanted overhead and implies concurrency pattern that is not even present in
the kernel.

Therefore, read the pointers directly since it's safe.

* selinux_state.ss needs more context.

Signed-off-by: Tashfin Shakeer Rhythm <tashfinshakeerrhythm@gmail.com>
2025-08-10 18:02:29 +08:00
Tashfin Shakeer Rhythm
8eb2c79471 kernel: selinux: rules: Fix illegal RCU lock usage in handle_sepolicy()
Currently, handle_sepolicy() holds an RCU read lock across the entire
function including calls to strncpy_from_user() which can sleep, which
is illegal in RCU semantics.

This triggers the following warning when the kernel is compiled with
CONFIG_DEBUG_ATOMIC_SLEEP enabled:

[    8.526345] BUG: sleeping function called from invalid context at lib/strncpy_from_user.c:40
[    8.526349] in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 683, name: ksud
[    8.526351] preempt_count: 0, expected: 0
[    8.526352] RCU nest depth: 1, expected: 0
[    8.526354] 1 lock held by ksud/683:
[    8.526355] #0: ffffffe013e1b970 (rcu_read_lock){....}, at: handle_sepolicy+0xe4/0xaa0
[    8.526365] CPU: 6 PID: 683 Comm: ksud Tainted: G        W         5.4.289-Scarlet-v2.2-beta2 #1
[    8.526366] Hardware name: redwood based Qualcomm Technologies, Inc. SM7325 (DT)
[    8.526367] Call trace:
[    8.526371] dump_backtrace+0x0/0x1c0
[    8.526374] dump_stack+0x90/0xcc
[    8.526376] __might_sleep+0x1a0/0x200
[    8.526378] __might_fault+0x28/0x40
[    8.526381] strncpy_from_user+0xac/0x300
[    8.526383] handle_sepolicy+0x588/0xaa0
[    8.526385] ksu_handle_prctl+0x368/0xd60
[    8.526386] ksu_task_prctl+0xc/0x20
[    8.526389] security_task_prctl+0x5c/0xa0
[    8.526391] __arm64_sys_prctl+0x58/0x7e0
[    8.526393] do_el0_svc+0x68/0x120
[    8.526394] el0_sync_handler+0x11c/0x1c0
[    8.526395] el0_sync+0x140/0x180

To fix this, replace the rcu_read_lock() with the `ksu_rules` mutex_lock()
introduced with commit 9014c663d1eb4 ("kernel: selinux: rules: Fix illegal RCU
lock usage in apply_kernelsu_rules()") which allows sleeping.

This mutex_lock() ensures mutual exclusion between threads invoking dynamic
policy modifications via handle_sepolicy() and those applying KernelSU rules
via apply_kernelsu_rules(), both of which access the policydb structure through
get_policydb().

Signed-off-by: Tashfin Shakeer Rhythm <tashfinshakeerrhythm@gmail.com>
2025-08-10 18:02:12 +08:00
rsuntk
78a95ae82b kernel: selinux: replace rcu_read_{lock, unlock} to mutex_{lock, unlock}
* We got a splat related to atomic sleep.
* The trace is from strncpy_from_user and might_fault

Same case:
e47115e009

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-10 18:01:58 +08:00
Prslc
a7b9b4c390 kernel(kpm): Fix typo in printk logs (#311)
Signed-off-by: Prslc <prslc113@gmail.com>
2025-08-10 17:59:58 +08:00
rsuntk
f63dbca3fa kernel: rename sh to ksud_path
* Questionable naming.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 23:01:15 +08:00
rsuntk
672041b4d6 kernel: Replace ksu_access_ok with macro
Signed-off-by: rsuntk <rissu.ntk@gmail.com>
2025-08-03 23:01:04 +08:00
rsuntk
0c87765958 kernel: increase reliability, add ksu_access_ok to simplify
Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 23:00:42 +08:00
rsuntk
39811e311f kernel: fixup sucompat
* For whatever reason, sh is ksud.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 23:00:17 +08:00
rsuntk
cf50966952 kernel: rename ksu_common_ksud_execve to ksu_ksud_execve_common
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 23:00:08 +08:00
rsuntk
da477fd588 kernel: fixup! su_path->su, make it as an array
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 22:59:55 +08:00
rsuntk
fc85270a35 kernel: code clean up and some inlining
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 22:58:46 +08:00
ShirkNeko
a9e3c1cc8f kermel: Refactoring and separating dynamic signature-related structures and functions and implement hot-signature updates 2025-08-03 22:51:38 +08:00
ShirkNeko
dd4cf956dd kernel: bump KSU_VERSION_API to 3.1.8 2025-07-21 21:24:32 +08:00
ShirkNeko
ac9acf6c0a kernel: simplified signature verification 2025-07-09 12:07:33 +08:00
TwinbornPlate75
340a94ef30 kernel/throne_tracker: Fix missing variable in function my_actor (#266) 2025-07-08 22:34:01 +08:00
ShirkNeko
6e0fb0b388 kernel: Add the ability to get active managers for multi-manager APKs 2025-07-06 00:53:47 +08:00
ShirkNeko
ed95981d09 kernel: add multi-manager support 2025-07-05 22:30:16 +08:00
ShirkNeko
942210459f Add dynamic signature support 2025-07-05 16:29:40 +08:00
WenHao2130
828290059d kernel: Drop Elysia key
The key owner has lost it

Change-Id: Id9e0037a490a545dc35b90312fc110b54465e153
Signed-off-by: WenHao2130 <wenhao2130@outlook.com>
2025-07-04 18:51:42 +08:00
ShirkNeko
6af65779d1 kernel: Update CMD_GET_FULL_VERSION to a new value 2025-07-03 00:10:06 +08:00
ShirkNeko
6bbb47bad4 kernel: bump KSU_VERSION to 3.1.7 2025-06-30 17:05:44 +08:00
YC酱luyancib
b44dd7d020 Update Kconfig (#224)
make sure kpm config requrement was enabled
2025-06-30 16:33:09 +08:00
WenHao2130
67c4111bbd kernel: fmt
Change-Id: Ie84bde7b2944152eb08f5e8f12df1c3aa86f8ca5
Signed-off-by: WenHao2130 <wenhao2130@outlook.com>
2025-06-28 21:26:56 +08:00
愛莉希雅
4908e3b633 愛莉希雅小姐嘅惠禮 (#226) 2025-06-28 14:43:59 +08:00
Tashfin Shakeer Rhythm
0c3049ec03 kernel: selinux: rules: Fix illegal RCU lock usage in apply_kernelsu_rules()
When kernel is compiled with CONFIG_DEBUG_ATOMIC_SLEEP enabled, it prints
the following splat in dmesg during post boot:

[ 6.739169] init: Opening SELinux policy
[ 6.751520] init: Loading SELinux policy
[ 6.894684] SELinux: policy capability network_peer_controls=1
[ 6.894688] SELinux: policy capability open_perms=1
[ 6.894690] SELinux: policy capability extended_socket_class=1
[ 6.894691] SELinux: policy capability always_check_network=0
[ 6.894693] SELinux: policy capability cgroup_seclabel=0
[ 6.894695] SELinux: policy capability nnp_nosuid_transition=1
[ 7.214323] selinux: SELinux: Loaded file context from:
[ 7.214332] selinux: /system/etc/selinux/plat_file_contexts
[ 7.214339] selinux: /system_ext/etc/selinux/system_ext_file_contexts
[ 7.214345] selinux: /product/etc/selinux/product_file_contexts
[ 7.214350] selinux: /vendor/etc/selinux/vendor_file_contexts
[ 7.214356] selinux: /odm/etc/selinux/odm_file_contexts
[ 7.216398] KernelSU: /system/bin/init argc: 2
[ 7.216401] KernelSU: /system/bin/init first arg: second_stage
[ 7.216403] KernelSU: /system/bin/init second_stage executed
[ 7.216506] BUG: sleeping function called from invalid context at security/selinux/ss/hashtab.c:47
[ 7.216512] in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 1, name: init
[ 7.216516] preempt_count: 0, expected: 0
[ 7.216518] RCU nest depth: 1, expected: 0
[ 7.216524] CPU: 6 PID: 1 Comm: init Not tainted 5.4.289-Scarlet-v2.0-beta3 #1
[ 7.216526] Hardware name: redwood based Qualcomm Technologies, Inc. SM7325 (DT)
[ 7.216528] Call trace:
[ 7.216536] dump_backtrace+0x0/0x210
[ 7.216539] show_stack+0x14/0x20
[ 7.216544] dump_stack+0x9c/0xec
[ 7.216548] __might_resched+0x1f0/0x210
[ 7.216552] hashtab_insert+0x38/0x230
[ 7.216557] add_type+0xd4/0x2e0
[ 7.216559] ksu_type+0x24/0x60
[ 7.216562] apply_kernelsu_rules+0xa8/0x650
[ 7.216565] ksu_handle_execveat_ksud+0x2a8/0x460
[ 7.216568] ksu_handle_execveat+0x2c/0x60
[ 7.216571] __arm64_sys_execve+0xe8/0xf0
[ 7.216574] el0_svc_common+0xf4/0x1a0
[ 7.216577] do_el0_svc+0x2c/0x40
[ 7.216579] el0_sync_handler+0x18c/0x200
[ 7.216582] el0_sync+0x140/0x180

This is because apply_kernelsu_rules() uses rcu_read_lock() to protect
SELinux policy modifications. However, cond_resched() from
hashtab_insert() at security/selinux/ss/hashtab.c is internally called
and it sleeps which is illegal under an RCU read-side critical section.

While replacing it with a spinlock would suppress the warning, this is
fundamentally incorrect because sleeping is illegal while holding a
spinlock and spinlock would turn off preemption which isn't an ideal
solution since it intentionally turns off rescheduling, and can lead
to deadlocks.

Instead, replace the RCU lock with a mutex lock. Mutex lock allows
sleeping when necessary, which is appropriate here because
apply_kernelsu_rules() runs in process context, not in atomic or
interrupt context. As apply_kernelsu_rules() is invoked only once during
post boot (SYSTEM_RUNNING), the mutex lock does not introduce any major
runtime performance regression and provides correct synchronization.

Fixes: tiann#2637
Signed-off-by: Tashfin Shakeer Rhythm <tashfinshakeerrhythm@gmail.com>
2025-06-27 12:27:45 +08:00
rsuntk
f820b9aaa8 kernel: phase out devpts_hook
* Since it's interceptable from LSM Hook,
then we just need to remove ksu_handle_devpts and
make a decoy for it.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-27 12:26:55 +08:00
古塵
f1ba7127b8 kernel: refactor CMD_GET_FULL_VERSION to safely initialize version string (#220)
Use strscpy()/strlcpy() to populate the version buffer in CMD_GET_FULL_VERSION
instead of relying on uninitialized memory. This ensures the returned string
is null-terminated and avoids exposing garbage data to user space.

Signed-off-by: schqiushui <orochi9999@gmail.com>
2025-06-27 00:26:17 +08:00
ShirkNeko
2564dce9ed kernel/core_hook: fix error handling in ksu_handle_prctl for CMD_GET_FULL_VERSION 2025-06-25 21:21:16 +08:00
ShirkNeko
1a43244288 kernel/Makefile: enhance version retrieval logic with GitHub commit count 2025-06-24 23:12:42 +08:00
ShirkNeko
8752b82fdc kernel: Rewrite kernle version code management
Co-authored-by: lamadaemon <i@lama.icu>
Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-06-24 20:12:36 +08:00
Prslc
ddea10e0d8 Kernel: use main branch commit count for consistent versioning (#209) 2025-06-23 20:04:50 +08:00
ShirkNeko
4c4dce98f4 kernel/Makefile: check kernelsu driver version from online git repo first, if fails then check local .git and if that also fails then use hardcoded fallback
Co-authored-by: rifsxd <rifat.44.azad.rifs@gmail.com>
2025-06-21 17:13:42 +08:00
ShirkNeko
aef862e91a kernel: clean up ksu_inode_permission by removing unused code 2025-06-21 14:59:42 +08:00
ShirkNeko
a437f69586 kernel: change ksu_key_permission to public for broader access 2025-06-20 00:38:31 +08:00
rsuntk
ea7e2f4db6 kernel: allow GKI kprobes to use LSM hook for ksu_handle_devpts
* Also minor changes on comments

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-20 00:28:20 +08:00
rsuntk
ae475cba67 kernel: selinux: expose is_ksu_transition to all linux version
* Allow newer kernel to use is_ksu_transition function.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-20 00:27:17 +08:00
rsuntk
8987312fc1 [skip ci] kernel: just search for ksu_handle_devpts
* only to make 100% sure?

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-19 18:23:17 +08:00
backslashxx
2394fc67fc kernel: intercept devpts via security_inode_permission LSM
* This changes:
  + Avoid conflicts with other devpts hooks.
  + We keep pts_unix98_pre for KPROBES for simplifying things.

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-19 18:23:03 +08:00
rsuntk
d13233f566 [skip ci] kernel: remove and fmt
* People who understands kernel, likely the didn't need this.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-19 18:22:05 +08:00
backslashxx
d582e619f0 kernel: minor optimization on throne tracker
* Picked from https://github.com/tiann/KernelSU/pull/2633
* Add missed filp_close and don't make data_app_magic static + __read_mostly

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-12 17:35:19 +08:00
rsuntk
722b5ab944 [skip ci] kernel: core_hook: add missing path_put
[  101.572296] CPU: 0 PID: 8674 Comm: main Tainted: G        WC OE     5.15.148-Ghost@NVG-064-gce02b349fb2b #1
[  101.572305] Hardware name: Qualcomm Technologies, Inc. KHAJE IDP nopmi topaz (DT)
[  101.572309] pstate: 20400005 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[  101.572315] pc : mntput_no_expire+0x25c/0x300
[  101.572328] lr : mntput_no_expire+0x134/0x300
[  101.572334] sp : ffffffc01d163c10
[  101.572336] x29: ffffffc01d163c30 x28: ffffffdb2c74bff0 x27: 0000000000000000
[  101.572345] x26: ffffff806cf11200 x25: ffffff806cf11200 x24: ffffffdb2db93000
[  101.572353] x23: ffffff807df66da0 x22: ffffff807df66d80 x21: ffffff807df66d80
[  101.572361] x20: ffffffdb2db89380 x19: ffffff806cf11200 x18: ffffffc013aad068
[  101.572369] x17: 0000000000000001 x16: ffffffa6c928b000 x15: 0000000000000000
[  101.572378] x14: 0000000000000020 x13: ffffffdb2db9f860 x12: 0000000000000020
[  101.572385] x11: ffffffffffffffff x10: 00000000000000ff x9 : 0000000000000008
[  101.572393] x8 : ffffff807df66d80 x7 : 61705f75736b203a x6 : 55536c656e72654b
[  101.572401] x5 : ffffffdb2de14332 x4 : ffffff81f6c435c1 x3 : 0000000000000000
[  101.572409] x2 : 0000000200000000 x1 : 0000000000000000 x0 : 00000000ffffffff
[  101.572418] Call trace:
[  101.572422] mntput_no_expire+0x25c/0x300
[  101.572431] path_put+0x3c/0x58
[  101.572438] ksu_try_umount+0x14c/0x174
[  101.572445] susfs_try_umount_all+0x6c/0x190
[  101.572450] ksu_handle_setuid+0x20c/0x320
[  101.572454] ksu_task_fix_setuid+0x18/0x2c
[  101.572459] __sys_setresuid+0x1e0/0x3dc
[  101.572466] __arm64_sys_setresuid+0x28/0x38
[  101.572472] invoke_syscall+0x64/0x154
[  101.572479] el0_svc_common+0x90/0xf4
[  101.572484] do_el0_svc+0x2c/0x9c
[  101.572489] el0_svc+0x28/0x60
[  101.572496] el0t_64_sync_handler+0xd4/0xf0
[  101.572501] el0t_64_sync+0x1b8/0x1bc
[  101.572508] ---[ end trace b57c69edb246930f ]---
[  101.572626] ------------[ cut here ]------------

Suggested-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-06-12 17:34:32 +08:00