Commit Graph

294 Commits

Author SHA1 Message Date
ShirkNeko
ca67913f05 Brought back the toggle of umount for zygote spawned isolated service; Synced...
Brought back the toggle of umount for zygote spawned isolated service; Synced with KernelSU main branch with latest commit 37ef0d27067d3d7e7bf07a80547a1949864789c4

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-09-30 22:54:19 +08:00
ShirkNeko
9d2ede6aec kernel & KernelSU: Another attempt to optimize SUS_MOUNT and SUS_SU
Changes:

1. Simplified the flow of mnt_id reorder, now it happens only after all sus mounts are umounted in ksu_handle_setuid()

2. All mounts mounted by ksu process will be flagged as sus mount and allocated with a fake mnt_id/mnt_group_id
   during ONLY the post-fs-data and service stage, which means other ksu mounts during/after boot-completed stage
   will not be handled by susfs.

3. Renaming:
   - susfs_mnt_id_ida => susfs_ksu_mnt_id_ida
   - susfs_mnt_group_ida => susfs_ksu_mnt_group_ida
   - DEFAULT_SUS_MNT_ID => DEFAULT_KSU_MNT_ID
   - DEFAULT_SUS_MNT_GROUP_ID => DEFAULT_KSU_MNT_GROUP_ID

4. Increased the default fake mnt_id to 300000, and default fake mnt_group_id to 3000.

5. Unify susfs_is_current_non_root_user_app_proc() and susfs_is_current_proc_su_not_allowed() as susfs_is_current_proc_umounted()
   Unify susfs_set_current_non_root_user_app_proc() and susfs_set_current_proc_su_not_allowed() as susfs_set_current_proc_umounted()
   Unify TIF_NON_ROOT_USER_APP_PROC and TIF_PROC_SU_NOT_ALLOWED as TIF_PROC_UMOUNTED

6. Removed unused susfs_is_allow_su()

7. Reworked a custom ksu_handle_setuid() which will be used if CONFIG_KSU_SUSFS is defined

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-09-30 22:50:48 +08:00
ShirkNeko
51e6a1b6c7 kernel: Add CONFIG_KSU_MANUAL_SU configuration
- Use random passphrase protection for manual su functionality
2025-09-29 06:50:39 +08:00
ShirkNeko
1281fa70d7 Kernel: Enhanced temporary record UID functionality and elevated privileges 2025-09-29 04:28:31 +08:00
ShirkNeko
3a4775306e Revert "kernel: sucompat: increase reliability, commonize and micro-optimize tiann #2656"
This reverts commit dd1eb98963.
2025-09-28 19:36:36 +08:00
ShirkNeko
7c4334e03a kernel: Add the ability to manually elevate privileges for programs using prctl by specifying UID or PID. 2025-09-28 19:33:08 +08:00
ShirkNeko
3f0f34e5b1 kernel: core_hook: add support for KernelNoSU ([#270](https://github.com/SukiSU-Ultra/SukiSU-Ultra/issues/270))
reorder ksu_handle_prctl checks a bit to allow non-manager to use CMD 15
this allows us to piggyback a small su to KernelSU's permission system after
disabling kernel sucompat

from:
Relax prctl perm check
- 95125c32f9
Allow prctl only for root or manager or su binary
- fa7af67d94
Refine prctl access check, allow /product/bin/su
- dd466dc1b6
Refine prctl check a little bit more
- e7c5b24efa

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Co-authored-by: nampud <nampud@users.noreply.github.com>
2025-09-27 23:59:21 +08:00
ShirkNeko
3140d9671f kernel: Revert partial changes 2025-09-27 22:45:23 +08:00
ShirkNeko
3a8b5561ec kernel: fmt ,optimization Log 2025-09-27 21:07:01 +08:00
ShirkNeko
37d5b9e046 kernel: Use the prctl command to provide switches for scanning functionality in user space 2025-09-27 19:33:20 +08:00
ShirkNeko
589c38530c kernel: Temporarily add a hook to the ksu_handle_execve_sucompat function to intercept the su command 2025-09-25 22:39:16 +08:00
JackA1ltman
8f60dacc17 susfs: merged latest commit.
Commit:
8e8150bf1e
de64f31024
0e19a05277

Co-authored-by: JackAltman
<82563979+JackA1ltman@users.noreply.github.com>
2025-09-25 21:28:50 +08:00
ShirkNeko
dd1eb98963 kernel: sucompat: increase reliability, commonize and micro-optimize tiann #2656
On plain ARMv8.0 devices (A53,A57,A73), strncpy_from_user_nofault() sometimes
fails to copy `filename_user` string correctly. This breaks su ofc, breaking
some apps like Termux (Play Store ver), ZArchiver and Root Explorer.

Apply the susfs patch

This does NOT seem to affect newer ARMv8.2+ CPUs (A75/A76 and newer)

My speculation? ARMv8.0 has weak speculation :)

here we replace `ksu_strncpy_from_user_nofault` with ksu_strncpy_from_user_retry:
- ksu_strncpy_from_user_nofault as fast-path copy
- fallback to access_ok to validate the pointer + strncpy_from_user
- manual null-termination just in case, as strncpy_from_user_nofault also does it
- remove that memset, seems useless as it is an strncpy, not strncat

basically, we retry on pagefualt

for usercopies, its not like were doing
    memset(dest, 0, sizeof(dest));
    strncat(dest, var, bytes);

that memset seems unneeded. instead we use strncpy itself to do proper
error and oob check and null term it after.

as for optimizations
- just return early if unauthorized
- commonized logic
- reduced duplication

Tested on:
- ARMv8.0 A73.a53, A57.a53, A53.a53
- ARMv8.2 A76.a55

Stale: tiann #2656

Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
Co-authored-by: rsuntk <rsuntk@yukiprjkt.my.id>
Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-09-25 19:41:26 +08:00
ShirkNeko
a85ce2bc35 kernel: Fixed su_kps not being referenced when CONFIG_KSU_KPROBES_HOOK is disabled. close #423 2025-09-25 17:08:51 +08:00
ShirkNeko
45837f6126 Revert "kernel: Handle unmount for isolated process correctly"
This reverts commit e0da36d9a9.
2025-09-25 16:16:39 +08:00
ShirkNeko
a81380c735 Revert "Sync with latest official KernelSU commit 4d3560b12bec5f238fe11f908a246f0ac97e9c27"
This reverts commit dd29fa162a.
2025-09-25 16:14:48 +08:00
ShirkNeko
97e1250d11 bump KSU_VERSION_API to 3.2.0 2025-09-24 22:36:14 +08:00
ShirkNeko
22e5c9f202 kernel: Bump tracepoint_hooks to version v1.1
Remove `devpts_hook` and `execveat_hook` from `tracepoint_hooks`
2025-09-24 22:04:17 +08:00
ShirkNeko
41f7921993 Revert "kernel: throne_tracker: offload to kthread tiann #2632"
This reverts commit 8364d6d4a4.
2025-09-24 16:26:27 +08:00
backslashxx
89fe18d929 pullout envp 2025-09-24 16:19:33 +08:00
backslashxx
c8b2c68b18 move debug back up 2025-09-24 16:19:28 +08:00
ShirkNeko
8364d6d4a4 kernel: throne_tracker: offload to kthread tiann #2632
Run throne_tracker() in kthread instead of blocking the caller.
Prevents full lockup during installation and removing the manager.

By default, first run remains synchronous for compatibility purposes
(FDE, FBEv1, FBEv2)

Features:
- looks and waits for manager UID in /data/system/packages.list
- run track_throne() in a kthread after the first synchronous run
- prevent duplicate thread creation with a single-instance check
- spinlock-on-d_lock based polling adressing possible race conditions.

Race conditions adressed
- single instance kthread lock, smp_mb()
- track_throne_function, packages.list, spinlock-on-d_lock based polling
- is_manager_apk, apk, spinlock-on-d_lock based polling

This is a squash of:
https://github.com/tiann/KernelSU/pull/2632

Original skeleton based on:
`kernelsu: move throne_tracker() to kthread`
`kernelsu: check locking before accessing files and dirs during searching manager`
`kernelsu: look for manager UID in /data/system/packages.list, not /data/system/packages.list.tmp`
0b05e927...8783badd

Co-Authored-By: backslashxx <118538522+backslashxx@users.noreply.github.com>
Co-Authored-By: Yaroslav Zviezda <10716792+acroreiser@users.noreply.github.com>
Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-09-24 01:50:05 +08:00
ShirkNeko
427000a9ec kernel: sys_execve bprm simplified
Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-09-23 23:54:50 +08:00
ShirkNeko
22634a71d6 kernel ksud: Attempt registration with bprm_check_kp first. If it fails, fall back to sys_execve_kp.
Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-09-23 17:26:37 +08:00
ShirkNeko
f6ba3f654f kernel: Add the missing endif 2025-09-23 00:07:39 +08:00
ShirkNeko
04a1b82564 kernel: core_hook: intercept devpts via security_inode_permission LSM
`ksu handles devpts with selinux lsm hook` - aviraxp

- no, not yet, but yes we can, thats a good idea.

This change tries to do that, so instead of hooking pts_unix98_lookup or
devpts_get_priv, we just watch security_inode_permission, if its devpts,
pass it along to the original handler.

EDIT: define devpts super magic if its undefined
- yeah I aint gonna include a conditional include of a header just for this
- while we can just fully remove the macro and inline, readability loss is bad

Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-09-22 23:58:03 +08:00
backslashxx
face2de7e5 kernel: selinux: fix pointer mismatch with 32-bit ksud on 64-bit kernels
Since KernelSU Manager can now be built for 32-bit, theres this problematic
setup where userspace is 32-bit (armeabi-v7a) and kernel is 64bit (aarch64).

On 64-bit kernels with CONFIG_COMPAT=y, 32-bit userspace passes 32-bit pointers.
These values are interpreted as 64-bit pointers without proper casting and that
results in invalid or near-null memory access.

This patch adds proper compat-mode handling with the ff changes:
- introduce a dedicated struct (`sepol_compat_data`) using u32 fields
- use `compat_ptr()` to safely convert 32-bit user pointers to kernel pointers
- adding a runtime `ksu_is_compat` flag to dynamically select between struct layouts

This prevents a near-null pointer dereference when handling SELinux
policy updates from 32-bit ksud in a 64-bit kernel.

Truth table:

kernel 32 + ksud 32, struct is u32, no compat_ptr
kernel 64 + ksud 32, struct is u32, yes compat_ptr
kernel 64 + ksud 64, struct is u64, no compat_ptr

Preprocessor check

64BIT=y COMPAT=y: define both structs, select dynamically
64BIT=y COMPAT=n: struct u64
64BIT=n: struct u32

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-09-22 23:31:33 +08:00
backslashxx
e00923f0bc ksud: migrate ksud execution to security_bprm_check (tiann#2653)
This migrates ksud execution decision-making to bprm_check_security.
This requires passing proper argv and envp to a modified _ksud handler
aptly named 'ksu_handle_bprm_ksud'.

Introduces:
int ksu_handle_bprm_ksud(const char *filename, const char *argv1,
const char *envp, size_t envp_len)

which is adapted from:
int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
struct user_arg_ptr *argv,
struct user_arg_ptr *envp,
int *flags)

ksu_handle_bprm_ksud handles all the decision making, it decides when it is
time to apply_kernelsu_rules depending if it sees "second_stage".

For LSM hook, turns out we can pull out argv and envp from mm_struct.
The code in here explains itself on how to do it.

whole blob exists on arg_start to arg_end, so we just pull it out and grab next
array after the first null terminator.

as for envp, we pass the pointer then hunt for it when needed

My reasoning on adding a fallback on usercopy is that on some devices a fault
happens, and it copies garbled data. On my creation of this, I actually had to lock
that _nofault copy on a spinlock as a way to mimic preempt_disable/enable without
actually doing it. As per user reports, no failed _nofault copies anyway but we
have-to-have a fallback for resilience.

References:
- old version1 6efcd8193e
- old version2 37d5938e66
- bad usercopy #21

This now provides a small helper function, ksu_copy_from_user_retry, which explains
itself. First we attempt a _nofault copy, if that fails, we try plain.

With that, It also provides an inlined copy_from_user_nofault for < 5.8.

While using strncpy_from_user_nofault was considered, this wont do, this will
only copy up to the first \0.

devlog:
16e5dce9e7...16c1f5f521
28642e60d7...728de0c571

References:
https://elixir.bootlin.com/linux/v4.14.1/source/include/linux/mm_types.h#L429
https://elixir.bootlin.com/linux/v4.14.1/source/include/linux/lsm_hooks.h

Stale: https://github.com/tiann/KernelSU/pull/2653

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-09-22 23:29:03 +08:00
ShirkNeko
d542fd0672 kernel: Added legacy throne tracker support, using packages.list to scan application UIDs 2025-09-21 17:39:07 +08:00
ShirkNeko
8e43b2d64d makefile: Checking support for proc_ops in the kernel 2025-09-20 13:30:34 +08:00
JackAltman
328b4012b9 feat: detect proc_ops support via header parsing instead of version check. (#411)
Some kernels (e.g. 5.4 with backports) include proc_ops despite being
older than 5.6.0. Replace hardcoded version check with runtime header
detection to handle these cases.

- Check for "struct proc_ops" in include/linux/proc_fs.h
- Use KSU_COMPAT_HAS_PROC_OPS macro for conditional compilation
- Fixes build failures on kernels with backported proc_ops

Signed-off-by: JackAltman
2025-09-20 13:28:05 +08:00
ShirkNeko
a555134a6e Add vfs_getattr compatibility for kernels < 4.14
Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-09-20 13:26:38 +08:00
ShirkNeko
a937ddc268 kernel: Use /data/user_de/ for rollback instead of packages.list 2025-09-19 21:04:42 +08:00
ShirkNeko
3639d5bda6 Kernel: Improved throne communication module for user space UID rescan 2025-09-18 22:58:35 +08:00
ShirkNeko
dd29fa162a Sync with latest official KernelSU commit 4d3560b12bec5f238fe11f908a246f0ac97e9c27
Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-09-17 17:54:58 +08:00
Wang Han
089afa2d7d Unmount isolated process which forks from zygote unconditionally (#2747)
Rethink about this patch, isolated process which directly forks from
zygote is just like normal app, so there is no reason apps won't crash
but Isolated process will. Also zygote reopens fd before actually fork,
so it should be fine.

This reverts commit 2a1741de96a789957555053cf5a397cbef1eb3e4.
2025-09-17 17:54:47 +08:00
ShirkNeko
eebde9be16 kernel: handle optional backport for selinux_inode
* For supporting kernel 4.19 with 5.10 bpf backports.

Co-authored-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-09-12 14:37:39 +08:00
ShirkNeko
840f7a0ff2 kernel: Add the real UID by parsing the UID from the /data/user_de/0/[app] directory
Prioritize retrieving the application UID from /data/user_de. If this fails, fall back to retrieving it from packages.list.

Fix unstable application UID acquisition

Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
2025-09-12 14:03:02 +08:00
ShirkNeko
fc5cd9e9ed kernel: Reworking Dynamic Manager Index Configuration 2025-09-06 15:06:10 +08:00
rsuntk
c520e57f9b kernel: remove unused ifdef
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-09-04 16:15:46 +08:00
rsuntk
3161671a7d kernel: stop intercepting devpts inode permission via LSM
* Somehow, it just does not work properly. (sometimes)

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-09-04 16:15:11 +08:00
rsuntk
dd00678da1 kernel: return the value of ksu_sys_umount
* Potentially causing compilation error?

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-09-02 14:56:07 +08:00
rsuntk
22eb11c391 kernel: fix -Wstrict-prototypes warnings/errors
* On newer kernel for some reason -Wno-strict-prototypes still does not fix the errors or warnings.
* To fix it, we just need to add void type.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-09-02 14:56:00 +08:00
dabao1955
6e5226efbb kernel: Removs extra strip for hook choice (#361)
* kernel: Removs extra strip for hook choice

extra changes for #353

* kernel: Fix Makefile
2025-08-28 10:23:40 +08:00
backslashxx
3ad2241722 kernel: micro-opt escape_to_root
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-27 15:20:18 +08:00
rsuntk
f3f2089a13 kernel: nuke creds wrapper
* Little bit complicated, so let's remove it.

Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
2025-08-27 15:20:08 +08:00
ShirkNeko
165558d588 kernel: Change ksu_su_compat_enabled from static to global 2025-08-25 20:55:42 +08:00
ShirkNeko
e81989302f kernel: Remove the ksu_ prefix from the dynamic manager signature. 2025-08-25 20:17:55 +08:00
ShirkNeko
e41fa8ef62 Revert renaming symbols in KernelSU driver
- In order to reduce the extra patch code and increase the rate of hunk succeed, we do not rename the symbols in KernelSU drvier but instead do it all in kernel/kallsym.c

- Make susfs_starts_with() global as it maybe useful in future

Co-authored-by: simonpunk <simonpunk2016@gmail.com>
2025-08-25 19:29:14 +08:00
ShirkNeko
20b58dcf44 kernel: Bump KSU_VERSION_API to 3.1.9 2025-08-24 11:46:28 +08:00