54 Commits

Author SHA1 Message Date
Rifat Azad
45ad73e9dd kernel: guard syscall hook types
- for kernel syscall hooks we need to pass additional guards for ksun (#ifdef CONFIG_KSU -> #if defined(CONFIG_KSU) && !defined(CONFIG_KSU_KPROBES_HOOK)) or else it will fail to build because of undefined symbol

- reference https://github.com/KernelSU-Next/kernel_patches/blob/main/syscall_hook/min_scope_syscall_hooks_v1.4.patch
2025-07-21 02:10:36 +06:00
Edrick Sinsuan
81f4f09d0c ksud: Address pagefault in ksu_handle_execveat_ksud (#662)
* ksud: Address pagefault in ksu_handle_execveat_ksud

As pointed out by @backslashxx, when strncpy pagefaults, it causes
the first_arg to be completely NULL in some systems. This causes
second_stage initialization to fail hence causing SU to be
non-functional.

This patch copies ksu_strncpy_from_user_retry from @backslashxx's
commit:
e2fe25e485

This adds a fallback to perform a normal strncpy_from_user when nofault
fails which allows us to get the first_arg in such cases.

Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: Edrick Sinsuan <evcsinsuan@gmail.com>

* Revert "ksud: Add second_stage init variant (#653)"

This reverts commit c6b60a24e8.

---------

Signed-off-by: Edrick Sinsuan <evcsinsuan@gmail.com>
Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-07-15 21:24:20 +06:00
Edrick Sinsuan
c6b60a24e8 ksud: Add second_stage init variant (#653)
There are some ROMs based on AOSP that calls on second stage init
with argc: 2 but with first_arg: "". This causes KSU to not work
properly on those systems.

Signed-off-by: Edrick Sinsuan <evcsinsuan@gmail.com>
2025-07-13 05:26:10 +06:00
backslashxx
80bd797737 kernel: ksud: remove remove read_iter requirement
nothing uses this on old kernels, so even backporting this to file_operations
is not really needed

https://elixir.bootlin.com/linux/v3.16.85/source/include/linux/fs.h#L1487

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-06-14 20:14:12 +06:00
backslashxx
f15d9b18e9 kernel: ksud: d_is_reg to IS_REG
d_is_reg requires 4.0
 - e36cb0b89c
IS_REG is still there on 6.15 so I do NOT see any issues forcing it for all.

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-06-14 20:13:13 +06:00
F-19-F
adce657583 kernel: ksud: provide is_ksu_transition check v2
context: this is known by many as `selinux hook`, `4.9 hook`

add is_ksu_transition check which allows ksud execution under nosuid.
it also eases up integration on 3.X kernels that does not have check_nnp_nosuid.

Usage:
	if (is_ksu_transition(old_tsec, new_tsec))
		return 0;

on either check_nnp_nosuid or selinux_bprm_set_creds (after execve sid reset)

reference: dfe003c9fd

taken from:
`allow init exec ksud under nosuid`
- 3df9df42a6
- https://github.com/tiann/KernelSU/pull/166#issue-1565872173

250611-edit:
- remove ksu_execveat_hook entry check
- turns out some devices needs the transition for multiple times

Reported-by: edenadversary <143865198+edenadversary@users.noreply.github.com>
Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-06-14 20:07:16 +06:00
Rifat Azad
c91f9c18ec Revert "kernel: ksud, throne_tracker: small changes for UL"
This reverts commit c4deee1e49.
2025-06-14 19:54:59 +06:00
backslashxx
c4deee1e49 kernel: ksud, throne_tracker: small changes for UL
Safe Ultra-Legacy changes that don't deserve their own commit

d_is_reg requires 4.0
 - e36cb0b89c
IS_REG is still there on 6.15 so I do NOT see any issues forcing it for all.

strscpy requires 4.3
strscpy on this usage can be replaced with strncpy + null term.
kernel gives us an option though.
strlcpy is fast af, hotrod fast. It’s just memcpy + null term, so lets go with that.
it got dropped in 6.8 due to risk concerns, so for those, lets use og strscpy.
ref: openwrt/packages #26453

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-06-01 20:09:20 +06:00
rifsxd
eed685507a kernel: rename KSU_WITH_KPROBES to KSU_KPROBES_HOOK for better self explanitory 2025-05-21 15:13:17 +06:00
backslashxx
5f871cd713 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-05-18 03:23:12 +06:00
backslashxx
7218a504c9 kernel: ksud: add ksu_handle_execve_ksud (#217)
adapted from sys_execve_handler_pre()
upstream, https://github.com/tiann/KernelSU/commit/2027ac3

this completes the puzzle where all hooks are on syscalls

ksu_handle_execve_ksud
- sets argv to __argv, dunno what this is for, I just copied.
- creates dummy struct `filename_in` to store filename in `filename_in.name`
- strncpy filename to path, assign path to .name
- simply a shim for ksu_handle_execveat_ksud

usage: `ksu_handle_execve_ksud(filename, argv);` on sys_execve

tested on 4.14, 6.1

Tested-by: selfmusing <mirandamehek@gmail.com>
Tested-by: Adam W. Willis <return.of.octobot@gmail.com>

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Co-authored-by: Another Guy <25584417+anotherjin@users.noreply.github.com>
2025-03-09 14:33:26 +06:00
backslashxx
cd0031e8a5 kernel: expose variables used by manual hooks always 2025-02-23 22:58:00 +06:00
backslashxx
097451d578 kernel: guard functionality that requires kprobes
Since upstream is now making kprobes a requirement, we adapt this tree to still allow
building with manual hooks in an easier way. Furthermore, this also allows a user to
build with manual hooks even with kprobes enabled.

Offending commit:
	kernel: remove unused CONFIG guard becuase GKI kernel enable kprobe by default
	upstream: https://github.com/tiann/KernelSU/commit/500ff9b

Other changes:
	ksud.c, stop_input_hook(), short-circuit redundant logic left by this change.
	cherry pick from: https://github.com/backslashxx/KernelSU/commit/a3d3e93

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
2025-02-23 21:00:59 +06:00
Rifat Azad
8fbddc959a Revert "kernel: added susfs v1.5.3"
This reverts commit bdf81c0880.
2024-12-24 08:21:10 +06:00
Rifat Azad
bdf81c0880 kernel: added susfs v1.5.3 2024-12-24 07:34:30 +06:00
Rifat Azad
1a84f69f5c Revert "[1.0] Drop Non-GKI Support (#1483)"
This reverts commit 97d70b40fc.
2024-12-24 06:54:08 +06:00
Rifat Azad
8d2b017c96 kernel: sync with upstream 2024-12-24 06:53:54 +06:00
Rifat Azad
f2fcb3687b kernel: cleaned up remaining susfs code 2024-12-16 00:14:07 +06:00
Rifat Azad
e1d30d55af kernel: implement SuSFS v1.5.2 2024-12-04 16:30:28 +06:00
Ylarod
97d70b40fc [1.0] Drop Non-GKI Support (#1483)
Co-authored-by: weishu <twsxtd@gmail.com>
2024-06-01 14:50:46 +08:00
weishu
89818cf85f kernel: transition devpts in kernel 2024-05-08 21:15:49 +08:00
weishu
2efefcea72 kernel: use library import 2024-04-27 09:55:24 +08:00
weishu
cd73307cd4 kernel: refactor PR_REAL_REGS 2024-04-26 13:25:29 +08:00
buildbot
714990b7c6 kernel: no need to hook 2024-04-26 12:10:58 +08:00
buildbot
2dc2dbfd45 kernel: use syscall hook for all version, remove code next version if works well 2024-04-26 11:42:24 +08:00
Another Guy
e2b0721af5 Hook syscalls and stable symbols (#1657)
1. Replace `do_execveat_common` with `sys_execve` and `sys_execveat`
2. Replace `input_handle_event` with `input_event` and
`input_inject_event`

Tested on android12-5.10-2024-04, android13-5.15-2024-04.
android14-6.1-2024-04
2024-04-26 11:27:48 +08:00
weishu
d8671b14d1 kernel: clean memory when exit 2024-03-24 16:41:53 +08:00
weishu
1ccb61c0fa kernel: we should take ownership over fd 2024-03-21 14:15:11 +08:00
weishu
895ad601d2 kernel: remove unused headers 2024-03-20 23:14:30 +08:00
weishu
2fdcdb2b4b kernel: hook syscall instead of unstable symbol 2024-03-20 14:21:19 +08:00
Soo-Hwan Na
8a36c36e3e kernel: Make it compile on 3.18 (maybe older) kernels (#1460)
input-event-codes.h:

Input: add input-event-codes header file
(f902dd8934)
This was in 4.4-rc, so 4.4.0 or above has it else no.

aio.h:
fs: move struct kiocb to fs.h
(e2e40f2c1e)

Below this version, we need to explicitly include aio.h for struct kiocb
This was in 4.1-rc, so 4.0 or below should do the include

uaccess.h, sched.h was present for long times, but 4.10 splited out to
include/sched/ but the current ifdef is not including uaccess.h for
lower versions than 4.4. Fix it.
2024-03-18 13:13:00 +08:00
4qwerty7
d5985f7450 kernel: fix null pointer dereference for some case (#1075)
#973 __never_use_envp 被改名为 envp 并使用。

这导致 GKI 版本一旦代码运行到
[213](86dcb02f72/kernel/ksud.c (L213))
行(或许只有 WSA 等类似情况会跑到这?),就会触发一个空指针解引用。

此PR意在修复此问题,且已在WSA上测试。
2023-10-24 07:00:52 +08:00
weishu
86dcb02f72 kernel: make compiler happy 2023-10-21 22:43:58 +08:00
longhuan1999
b14ea03dc4 kernel: Adapt to low version Android init process (#973)
1. Adapt to low version Android init process
2. Add stop hook output
3. Fix output with missing line breaks
2023-10-02 12:00:24 +08:00
4qwerty7
cd19ce2e86 Distinguish different PT_REGS_PARM4 under x86 (#711)
1. `PT_REGS_CCALL_PARM4` 表示存放C调用约定的第4个参数的寄存器
2. `PT_REGS_SYSCALL_PARM4` 表示存放linux syscall调用约定的第4个参数的寄存器
3. 将原有 `PT_REGS_PARM4` 改为上述之一
4. 将原有 `ksu_handle_execveat_ksud` 和 `ksu_handle_execveat_sucompat` 可能被
kprobe 传递错误实参、且不使用的形参标记为 never_used 并传递 `NULL`
5. 为 `ksu_handle_execveat_ksud` 提供正确的 argv 参数用以在 x86 下也能正确识别 `init
second_stage`

---------

Co-authored-by: weishu <twsxtd@gmail.com>
2023-07-06 09:01:35 +08:00
4qwerty7
c62c5f1d78 kernel: support the case that init_task.mnt_ns != zygote.mnt_ns(WSA) (#698)
Basic support for the case that init_task.mnt_ns != zygote.mnt_ns(WSA),
just copy nsproxy and fs pointers for solve #276.

Note the copy in `apk_sign.c` is not required but suggested for
secure(ensure the checked mnt_ns is what ns android running, not created
by user, although many distributions does not have user ns.).

Tested with latest release on Win10 19045.3086(with WSAPatch).

Further review required for:
- [x] Security of this operation (without locking).
- [x] The impact of these modifications on other Android distributions.
2023-07-02 00:20:01 +08:00
weishu
c7c5a07f0a kernel: fix compile err on lower kernel 2023-06-27 20:46:09 +08:00
Coconut
29169ddeb9 kernel:Fix the issue of incompatible __maybe_unused in the GCC compiler kernel used in versions 4.4. x to 4.9. x. (#660) 2023-06-20 10:35:07 +08:00
Juhyung Park
9fa6a0ce85 Hook improvements (take 2) (#563)
Hi @tiann.

Thanks for the great project, I had great fun playing around with it.

This PR mainly tries to further minimize the possible delays caused by
KernelSU hooking.

There are 3 major changes:
- Processes with 0 < UID < 2000 are blocked straight-up before going
through the allow_list.
I don't see any need for such processes to be interested in root, and
this allows returning early before going through a more expensive
lookup.
If there's an expected breakage due to this change, I'll remove it. Let
me know.
- A page-sized (4K) bitmap is added.
This allows O(1) lookup for UID <= 32767.
This speeds up `ksu_is_allow_uid()` by about 4.8x by sacrificing a 4K
memory. IMHO, a good trade-off.
Most notably, this reduces the 99.999% result previously from worrying
milliseconds scale to microseconds scale.
For UID > 32767, another page-sized (4K) sequential array is used to
cache allow_list.

Compared to the previous PR #557, this new approach gives another nice
25% performance boost in average, 63-96% boost in worst cases.

Benchmark results are available at
https://docs.google.com/spreadsheets/d/1w_tO1zRLPNMFRer49pL1TQfL6ndEhilRrDU1XFIcWXY/edit?usp=sharing

Thanks!

---------

Signed-off-by: Juhyung Park <qkrwngud825@gmail.com>
2023-06-16 19:53:15 +08:00
weishu
6b326ffbff kernel: fix warning on x86_64, close #637 2023-06-16 19:32:48 +08:00
tiann
9d24e59240 kernel: revert init stage for x86_64 temporarily 2023-05-11 17:20:55 +08:00
Huy Minh
5b284d27ee ksud.c : Include compat.h (#450)
Fix compiling on Android-x86

Signed-off-by: hmtheboy154 <buingoc67@gmail.com>
2023-05-04 11:49:21 +08:00
tiann
8a5c94c368 kernel: Make sure logd has started before post-fs-data. 2023-04-15 00:17:28 +08:00
chen2021-web
3a3fcaad4c Try to solve 4.14 build don't pass problem (#368) 2023-04-11 23:13:34 +08:00
weishu
8ba6ef20ea kernel: make /system/bin/init second_stage more precisely (#357) 2023-04-11 10:55:17 +08:00
weishu
dbcdc29ba8 kernel: optimize vfs_read probe. 2023-02-23 12:12:50 +07:00
weishu
70829a428a kernel: count time of volumedown to enter safemode 2023-02-13 22:01:59 +07:00
weishu
20fcc971e1 kernel: press KEY_VOLUMEDOWN over 2 seconds will enter safemode and disable all modules
Co-authored-by: Ylarod <me@ylarod.cn>
2023-02-13 21:23:28 +07:00
小さい猫
4a811576fc fix some typos introduced in #166 (#181)
Signed-off-by: Ookiineko <chiisaineko@protonmail.com>
Co-authored-by: f19 <58457605+F-19-F@users.noreply.github.com>
Co-authored-by: Scirese <nuclearlight91@gmail.com>
2023-02-02 23:38:04 +08:00
f19
b085db50dc kernel: backport to 4.4 (#166)
These changes make KernelSU work on kernel4.4
[link](https://github.com/F-19-F/android_kernel_oneplus_msm8998).
LINUX_VERSION_CODE macro changes have been vertied on 4.4 4.9 4.14.
For kernel 4.4,just pick two commits
* [introduce
KernelSU](2993524f2f)
* [allow init exec ksud under
nosuid](3df9df42a6)
2023-02-01 19:48:36 +08:00