You've already forked KernelSU-Next
mirror of
https://github.com/KernelSU-Next/KernelSU-Next.git
synced 2025-08-27 23:46:34 +00:00
9fa6a0ce85
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>
252 lines
8.8 KiB
Markdown
252 lines
8.8 KiB
Markdown
# 如何为非 GKI 内核集成 KernelSU {#introduction}
|
|
|
|
KernelSU 可以被集成到非 GKI 内核中,现在它最低支持到内核 4.14 版本;理论上也可以支持更低的版本。
|
|
|
|
由于非 GKI 内核的碎片化极其严重,因此通常没有统一的方法来编译它,所以我们也无法为非 GKI 设备提供 boot 镜像。但你完全可以自己集成 KernelSU 然后编译内核使用。
|
|
|
|
首先,你必须有能力从你设备的内核源码编译出一个可以开机并且能正常使用的内核,如果内核不开源,这通常难以做到。
|
|
|
|
如果你已经做好了上述准备,那有两个方法来集成 KernelSU 到你的内核之中。
|
|
|
|
1. 借助 `kprobe` 自动集成
|
|
2. 手动修改内核源码
|
|
|
|
## 使用 kprobe 集成 {#using-kprobes}
|
|
|
|
KernelSU 使用 kprobe 机制来做内核的相关 hook,如果 *kprobe* 可以在你编译的内核中正常运行,那么推荐用这个方法来集成。
|
|
|
|
首先,把 KernelSU 添加到你的内核源码树,在内核的根目录执行以下命令:
|
|
|
|
- 最新tag(稳定版本)
|
|
|
|
```sh
|
|
curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -
|
|
```
|
|
|
|
- main分支(开发版本)
|
|
|
|
```sh
|
|
curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -s main
|
|
```
|
|
|
|
- 指定tag(比如v0.5.2)
|
|
|
|
```sh
|
|
curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -s v0.5.2
|
|
```
|
|
|
|
然后,你需要检查你的内核是否开启了 *kprobe* 相关的配置,如果没有开启,需要添加以下配置:
|
|
|
|
```
|
|
CONFIG_KPROBES=y
|
|
CONFIG_HAVE_KPROBES=y
|
|
CONFIG_KPROBE_EVENTS=y
|
|
```
|
|
|
|
最后,重新编译你的内核即可。
|
|
|
|
如果你发现KPROBES仍未生效,很有可能是因为它的依赖项`CONFIG_MODULES`没有被启用(如果还是未生效请键入`make menuconfig`搜索KPROBES 的其它依赖并启用 )
|
|
|
|
如果你在集成 KernelSU 之后手机无法启动,那么很可能你的内核中 **kprobe 工作不正常**,你需要修复这个 bug 或者用第二种方法。
|
|
|
|
:::tip 如何验证是否是 kprobe 的问题?
|
|
|
|
注释掉 `KernelSU/kernel/ksu.c` 中 `ksu_enable_sucompat()` 和 `ksu_enable_ksud()`,如果正常开机,那么就是 kprobe 的问题;或者你可以手动尝试使用 kprobe 功能,如果不正常,手机会直接重启。
|
|
:::
|
|
|
|
## 手动修改内核源码 {#modify-kernel-source-code}
|
|
|
|
如果 kprobe 工作不正常(通常是上游的 bug 或者内核版本过低),那你可以尝试这种方法:
|
|
|
|
首先,把 KernelSU 添加到你的内核源码树,在内核的根目录执行以下命令:
|
|
|
|
```sh
|
|
curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -
|
|
```
|
|
|
|
然后,手动修改内核源码,你可以参考下面这个 patch:
|
|
|
|
```diff
|
|
diff --git a/fs/exec.c b/fs/exec.c
|
|
index ac59664eaecf..bdd585e1d2cc 100644
|
|
--- a/fs/exec.c
|
|
+++ b/fs/exec.c
|
|
@@ -1890,11 +1890,14 @@ static int __do_execve_file(int fd, struct filename *filename,
|
|
return retval;
|
|
}
|
|
|
|
+extern bool ksu_execveat_hook __read_mostly;
|
|
+extern int ksu_handle_execveat(int *fd, struct filename **filename_ptr, void *argv,
|
|
+ void *envp, int *flags);
|
|
+extern int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
|
|
+ void *argv, void *envp, int *flags);
|
|
static int do_execveat_common(int fd, struct filename *filename,
|
|
struct user_arg_ptr argv,
|
|
struct user_arg_ptr envp,
|
|
int flags)
|
|
{
|
|
+ if (unlikely(ksu_execveat_hook))
|
|
+ ksu_handle_execveat(&fd, &filename, &argv, &envp, &flags);
|
|
+ else
|
|
+ ksu_handle_execveat_sucompat(&fd, &filename, &argv, &envp, &flags);
|
|
return __do_execve_file(fd, filename, argv, envp, flags, NULL);
|
|
}
|
|
|
|
diff --git a/fs/open.c b/fs/open.c
|
|
index 05036d819197..965b84d486b8 100644
|
|
--- a/fs/open.c
|
|
+++ b/fs/open.c
|
|
@@ -348,6 +348,8 @@ SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
|
|
return ksys_fallocate(fd, mode, offset, len);
|
|
}
|
|
|
|
+extern int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
|
|
+ int *flags);
|
|
/*
|
|
* access() needs to use the real uid/gid, not the effective uid/gid.
|
|
* We do this by temporarily clearing all FS-related capabilities and
|
|
@@ -355,6 +357,7 @@ SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
|
|
*/
|
|
long do_faccessat(int dfd, const char __user *filename, int mode)
|
|
{
|
|
+ ksu_handle_faccessat(&dfd, &filename, &mode, NULL);
|
|
const struct cred *old_cred;
|
|
struct cred *override_cred;
|
|
struct path path;
|
|
diff --git a/fs/read_write.c b/fs/read_write.c
|
|
index 650fc7e0f3a6..55be193913b6 100644
|
|
--- a/fs/read_write.c
|
|
+++ b/fs/read_write.c
|
|
@@ -434,10 +434,14 @@ ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
|
|
}
|
|
EXPORT_SYMBOL(kernel_read);
|
|
|
|
+extern bool ksu_vfs_read_hook __read_mostly;
|
|
+extern int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
|
|
+ size_t *count_ptr, loff_t **pos);
|
|
ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
|
|
{
|
|
ssize_t ret;
|
|
|
|
+ if (unlikely(ksu_vfs_read_hook))
|
|
+ ksu_handle_vfs_read(&file, &buf, &count, &pos);
|
|
+
|
|
if (!(file->f_mode & FMODE_READ))
|
|
return -EBADF;
|
|
if (!(file->f_mode & FMODE_CAN_READ))
|
|
diff --git a/fs/stat.c b/fs/stat.c
|
|
index 376543199b5a..82adcef03ecc 100644
|
|
--- a/fs/stat.c
|
|
+++ b/fs/stat.c
|
|
@@ -148,6 +148,8 @@ int vfs_statx_fd(unsigned int fd, struct kstat *stat,
|
|
}
|
|
EXPORT_SYMBOL(vfs_statx_fd);
|
|
|
|
+extern int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags);
|
|
+
|
|
/**
|
|
* vfs_statx - Get basic and extra attributes by filename
|
|
* @dfd: A file descriptor representing the base dir for a relative filename
|
|
@@ -170,6 +172,7 @@ int vfs_statx(int dfd, const char __user *filename, int flags,
|
|
int error = -EINVAL;
|
|
unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
|
|
|
|
+ ksu_handle_stat(&dfd, &filename, &flags);
|
|
if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
|
|
AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
|
|
return -EINVAL;
|
|
```
|
|
|
|
主要是要改四个地方:
|
|
|
|
1. do_faccessat,通常位于 `fs/open.c`
|
|
2. do_execveat_common,通常位于 `fs/exec.c`
|
|
3. vfs_read,通常位于 `fs/read_write.c`
|
|
4. vfs_statx,通常位于 `fs/stat.c`
|
|
|
|
如果你的内核没有 `vfs_statx`, 使用 `vfs_fstatat` 来代替它:
|
|
|
|
```diff
|
|
diff --git a/fs/stat.c b/fs/stat.c
|
|
index 068fdbcc9e26..5348b7bb9db2 100644
|
|
--- a/fs/stat.c
|
|
+++ b/fs/stat.c
|
|
@@ -87,6 +87,8 @@ int vfs_fstat(unsigned int fd, struct kstat *stat)
|
|
}
|
|
EXPORT_SYMBOL(vfs_fstat);
|
|
|
|
+extern int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags);
|
|
+
|
|
int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
|
|
int flag)
|
|
{
|
|
@@ -94,6 +96,8 @@ int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
|
|
int error = -EINVAL;
|
|
unsigned int lookup_flags = 0;
|
|
|
|
+ ksu_handle_stat(&dfd, &filename, &flag);
|
|
+
|
|
if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
|
|
AT_EMPTY_PATH)) != 0)
|
|
goto out;
|
|
```
|
|
|
|
对于早于 4.17 的内核,如果没有 `do_faccessat`,可以直接找到 `faccessat` 系统调用的定义然后修改:
|
|
|
|
```diff
|
|
diff --git a/fs/open.c b/fs/open.c
|
|
index 2ff887661237..e758d7db7663 100644
|
|
--- a/fs/open.c
|
|
+++ b/fs/open.c
|
|
@@ -355,6 +355,9 @@ SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
|
|
return error;
|
|
}
|
|
|
|
+extern int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
|
|
+ int *flags);
|
|
+
|
|
/*
|
|
* access() needs to use the real uid/gid, not the effective uid/gid.
|
|
* We do this by temporarily clearing all FS-related capabilities and
|
|
@@ -370,6 +373,8 @@ SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
|
|
int res;
|
|
unsigned int lookup_flags = LOOKUP_FOLLOW;
|
|
|
|
+ ksu_handle_faccessat(&dfd, &filename, &mode, NULL);
|
|
+
|
|
if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
|
|
return -EINVAL;
|
|
```
|
|
|
|
要使用 KernelSU 内置的安全模式,你还需要修改 `drivers/input/input.c` 中的 `input_handle_event` 方法:
|
|
|
|
:::tip
|
|
强烈建议开启此功能,对用户救砖会非常有帮助!
|
|
:::
|
|
|
|
```diff
|
|
diff --git a/drivers/input/input.c b/drivers/input/input.c
|
|
index 45306f9ef247..815091ebfca4 100755
|
|
--- a/drivers/input/input.c
|
|
+++ b/drivers/input/input.c
|
|
@@ -367,10 +367,13 @@ static int input_get_disposition(struct input_dev *dev,
|
|
return disposition;
|
|
}
|
|
|
|
+extern bool ksu_input_hook __read_mostly;
|
|
+extern int ksu_handle_input_handle_event(unsigned int *type, unsigned int *code, int *value);
|
|
+
|
|
static void input_handle_event(struct input_dev *dev,
|
|
unsigned int type, unsigned int code, int value)
|
|
{
|
|
int disposition = input_get_disposition(dev, type, code, &value);
|
|
+
|
|
+ if (unlikely(ksu_input_hook))
|
|
+ ksu_handle_input_handle_event(&type, &code, &value);
|
|
|
|
if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
|
|
add_input_randomness(type, code, value);
|
|
```
|
|
|
|
改完之后重新编译内核即可。
|