Commit Graph

239 Commits

Author SHA1 Message Date
rsuntk
d970e45f14 kernel: guard path_umount logging
* path_umount pretty much guaranteed to work as is, so it would not need
  much logging.

Unlike sys_umount which is an alternative to path_umount for older kernel, so, sys_umount need constant logging.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-22 11:41:01 +08:00
rsuntk
34dd78dd0c kernel: handle spinlock from escape_to_root
* Likely fix the freeze in a few kernel version.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-21 13:11:00 +08:00
backslashxx
8bc11247fe kernel: apk_sign: fix return check for ksu_sha256
upstream used IS_ERR to check for negative return and that is int,
so correct it.

This is one headache for old compilers.

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-08-21 13:06:56 +08:00
rsuntk
b8989cb07b kernel: remove unnecessary logging in disable_seccomp
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-21 13:06:38 +08:00
ShirkNeko
b6d654aaf7 Kernel: Improved permission tracking logic when dynamic signature manager existence checks are enabled
- Fixed a panic issue caused by repeated scans in certain cases where dynamic signatures were not enabled
2025-08-19 18:10:40 +08:00
ShirkNeko
cf152f368d kernel: refactor APK signature verification functions for clarity and efficiency 2025-08-17 23:12:28 +08:00
ShirkNeko
8a0d52a2ee 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

Co-authored-by: Wang Han <416810799@qq.com>
Co-authored-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-16 14:25:17 +08:00
rsuntk
69a13ff999 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 13:40:42 +08:00
ShirkNeko
6a79ca32ac 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
438bd5f#commitcomment-163785768)

Test: Checker pass.

* Any issue? Let me know.

Tested-by: rsuntk <rsuntk@yukiprjkt.my.id>
Co-authored-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-16 13:15:51 +08:00
Prslc
ef399712dc 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

These patches is based on https://github.com/backslashxx/KernelSU/issues/5

Co-authored-by: Cloud_Yun <1770669041@qq.com>
Co-authored-by: Prslc <prslc113@gmail.com>
2025-08-14 20:59:44 +08:00
ShirkNeko
ffdaf13fbd Revert "kernel: Handle unmount for isolated process correctly"
This reverts commit e0da36d9a9.
2025-08-11 18:13:47 +08:00
rsuntk
2a2ddec7ef 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:16:07 +08:00
rsuntk
7ce301d564 kernel: fix mismerge
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-10 18:15:06 +08:00
Wang Han
e0da36d9a9 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:14:51 +08:00
Tashfin Shakeer Rhythm
8727664fa9 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:09:06 +08:00
Tashfin Shakeer Rhythm
ccee5e51c6 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:08:48 +08:00
rsuntk
71f8944ffb 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:06:55 +08:00
Prslc
da89b00d06 kernel(kpm): Fix typo in printk logs (#311)
Signed-off-by: Prslc <prslc113@gmail.com>
2025-08-10 17:59:45 +08:00
ShirkNeko
e2ea0138db kernel & KernelSU & ksu_susfs: Temp fix for avc log message being leaked by...
kernel & KernelSU & ksu_susfs: Temp fix for avc log message being leaked by logd and added new toggle for enabling or disabling avc log spoofing

- See full details:
  https://android-review.googlesource.com/c/platform/system/logging/+/3725346/2
  https://github.com/aviraxp/ZN-AuditPatch

- Since spoofing avc log in kernel may make things harder when users trying to debug with some permission issues or selinux issues, so thats why the toggle is created here and this is just a temp fix implemented in kernel side

- usage: ksu_susfs enable_avc_log_spoofing <0|1>

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-08-10 16:35:19 +08:00
rsuntk
9e29b2c3d7 kernel: Replace ksu_access_ok with macro
Signed-off-by: rsuntk <rissu.ntk@gmail.com>
2025-08-03 23:20:04 +08:00
ShirkNeko
f37092e2f0 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:19:35 +08:00
rsuntk
98552980ca kernel: rename ksu_common_ksud_execve to ksu_ksud_execve_common
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 23:13:45 +08:00
rsuntk
7ec07812a1 kernel: code clean up and some inlining
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-03 23:12:54 +08:00
ShirkNeko
d2ed7ee19b KernelSU: Fix SUS_SU bootloops on some device because of improper sizeof(su) in sucompat.c
- There must be a reason for doing [sizeof(su) + 1 ] wthin the syscall handlers in the original sucompat.c but I dont know why, but it does fix the bootloop for some devices.
- I will be shocked if sizeof(su) produces different result on different kernel, since it works on pixel8 and no bootloops whatsoever.

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-08-03 22:46:51 +08:00
ShirkNeko
9ff53ce683 kermel: Refactoring and separating dynamic signature-related structures and functions and implement hot-signature updates 2025-08-03 22:46:00 +08:00
ShirkNeko
bdf675b448 kernel: bump KSU_VERSION_API to 3.1.8 2025-07-21 21:25:11 +08:00
ShirkNeko
66ff2fa812 Fix the KSU_SUSFS_HAS_MAGIC_MOUNT dependency to ensure it depends on KSU_SUSFS 2025-07-21 16:29:51 +08:00
ShirkNeko
d3c37a293d kernel & KernelSU: Added new ksu_susfs cli add_sus_path_loop
- When path is added via add_sus_path_loop, the path will be re-flagged as SUS_PATH on each non-root user app or isolated service starts,
  this may address some issues that the added path via add_sus_path getting its inode status reset or inode re-created in kernel for whatever reason.

- Please also note that only path NOT inside /storage/ and /sdcard/ can be added via add_sus_path_loop.

- usage: ksu_susfs add_sus_path_loop </path/that/is/not/inside/sdcard/>

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-07-21 16:23:17 +08:00
ShirkNeko
329b7f59dc KernelSU: SUS_SU: Initiate path with 0 and no memset is needed
1. kernel & KernelSU: More optimization for SUS_SU mode 2 (Experimental)

2. Always flag zygote spawned isolated service as non-root user app process and no su allowed process

3. Change susfs_fake_qstr_name, shorter length

4. KernelSU: SUS_SU: Initiate path with 0 and no memset is needed

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-07-15 10:24:43 +08:00
ShirkNeko
0b03cd9faf kernel: simplified signature verification 2025-07-09 12:07:41 +08:00
ShirkNeko
f5541e215f kernel & KernelSU: Replace kabi member susfs_task_state of tast_struct and use...
kernel & KernelSU: Replace kabi member susfs_task_state of tast_struct and use tast_struct->thread_info.flags instead; Fix for sus_path not working on some OEM devices; KSU_SUSFS requires CONFIG_THREAD_INFO_IN_TASK now;

- Since the kabi member is not guaranteed to be unused by OEM, that is why some deivces may conflict with the kabi member added by susfs, we should use less kabi member as much as possible.
- Luckily we have task_struct->thread_info that is enabled by all gki kernels just like kprobes, so we can utilize thread_info->flags for faster check, but it needs CONFIG_THREAD_INFO_IN_TASK to be on which should always be enabled
- Big thanks to fatalcoder524 (https://github.com/fatalcoder524) who did an excellent debug process for me

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-07-07 13:13:07 +08:00
ShirkNeko
b8f9a448f2 kernel: Add the ability to get active managers for multi-manager APKs 2025-07-06 00:25:42 +08:00
ShirkNeko
f7dcc82be5 kernel: add multi-manager support 2025-07-05 21:27:04 +08:00
ShirkNeko
f5c236c925 Add dynamic signature support 2025-07-05 15:59:58 +08:00
ShirkNeko
e5f58caf11 kernel & KernelSU: Refactor show enabled_features
- This simplies the logic in ksu_susfs tool, and it should be more convenient for some ksu manager variants that integrate susfs since they can just pass the char buffer and its size to get the result now, which means they no longer need to update the bit value each time a new susfs features deprecated or added.

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-07-03 17:58:38 +08:00
ShirkNeko
cc2f28f1c1 kernel: Update CMD_GET_FULL_VERSION to a new value 2025-07-03 00:09:52 +08:00
WenHao2130
5a4a8da820 kernel: Fix typo error
Revise the explanation for KSU_MULTI_MANAGER_SUPPORT

Change-Id: I097b4e0d1a30c02fadd75d4123c7bcf346782f18
Signed-off-by: WenHao2130 <wenhao2130@outlook.com>
2025-07-01 12:47:58 +08:00
ShirkNeko
5a0801334c kernel: bump KSU_VERSION to 3.1.7 2025-06-30 17:06:25 +08:00
YC酱luyancib
4ad011c988 Update Kconfig (#224)
make sure kpm config requrement was enabled
2025-06-30 16:32:09 +08:00
ShirkNeko
f008459be1 kernel & KernelSU: Added switch for enabling/disabling umount for zygote...
kernel & KernelSU: Added switch for enabling/disabling umount for zygote spawned isolated service process

- Some modules that overlay sysyem files like framework or other overlay apk may crash when all iso services are umounted by susfs, so a switch is made here for users to switch on/off this funtion at anytime when they need it or want to test it
- By default it is disabled in kernel, run "ksu_susfs umount_for_zygote_iso_service <0|1>" to switch on/off the function in userspace
- Also user can create new file to "/data/adb/susfs_umount_for_zygote_iso_service" to enable it in kernel on each boot
- Alternatively, disable this function and use other modules to handle the umount for iso services until a better solution is found

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-06-30 16:29:45 +08:00
rsuntk
bb02b12a69 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:31:38 +08:00
Tashfin Shakeer Rhythm
c71ef3ce29 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:31:27 +08:00
古塵
d0cb0cf25a 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:24:14 +08:00
ShirkNeko
8e192aaead kernel: add support for multi KernelSU manager in Kconfig 2025-06-27 00:10:19 +08:00
ShirkNeko
3404a4fea8 KernelSU: Fix sus mounts not umounted for iso service that spawned by zygote directly
Co-authored-by: simonpunk <simonpunk2016@gmail.com>
Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-06-25 12:12:17 +08:00
ShirkNeko
8c9692c350 kernel/Makefile: enhance version retrieval logic with GitHub commit count 2025-06-24 22:53:12 +08:00
ShirkNeko
f876a523f6 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:07:56 +08:00
Prslc
0f3a9de958 Kernel: use main branch commit count for consistent versioning (#209) 2025-06-23 20:05:16 +08:00
ShirkNeko
3437c7cf3b kernel: remove deprecated CONFIG_KSU_SUSFS_SUS_OVERLAYFS state initialization checks 2025-06-21 18:20:44 +08:00
ShirkNeko
8716b764ee 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:12:03 +08:00