KernelSU icon indicating copy to clipboard operation
KernelSU copied to clipboard

[Custom] ksu 在 lxc/chroot 裡面會起作用嗎

Open supechicken opened this issue 1 year ago • 4 comments

Describe your problem.

這不是bug,更像是一個問題😅

背景是這樣的,我在一台軟路由裏裝了個 android tv,打算在上面用 lxc 運行 openwrt 讓其同時兼任電視盒子和網關。

我的問題是既然 ksu 運行在內核層面,會不會導致容器裡面特定 uid 的權限洩漏嗎?(假如在 chroot 隔離下的 uid 2000 運行 /system/bin/su(且在 manager 裏允許 shell 提權)會觸發 ksu 的提權機制嗎?)

感謝指教!

supechicken avatar Jan 02 '24 16:01 supechicken

KernelSU 在内核层面没有处理 namespace,因此结果是未定义的。不过可能由于其他的原因(比如鉴权文件放在 /data/adb/ksu/.allowlist 导致不会起作用。最好实际尝试一下。另外,lxc 跟 chroot 不是一个东西。

tiann avatar Jan 03 '24 08:01 tiann

lxc 跟 chroot 不是一个东西

這我知道,除去 lxc 有 namespace 隔離支持之外兩者的最大差別是一個用 pivot_root() 而另一個用 chroot() 來切換 rootfs(據我所知只有在 unprivileged lxc 才會啟用 user namespace mapping,所以 privileged lxc 裡面的 uid 對於內核來說應該是跟宿主的相同 uid 等價的)

剛寫了個程式試了下,發現 ksu 雖然不能在 chroot() 下觸發(猜測是因為 path 在到達內核層後做了轉換,變回了 chroot 前的 path),~~但在 pivot_root(newroot, newroot) 下能用,而且還越獄了(在 pivot_root 後運行 ls -la / 顯示的是宿主的 rootfs)~~:

# ./a.out tmproot/
su exist in pivot_root()!
su not exist in chroot()!
:/ # ls
acct           etc              proc
apex           init             product
bin            init.environ.rc  sdcard
bugreports     linkerconfig     second_stage_resources
cache          lost+found       storage
config         metadata         sys
d              mnt              system
data           odm              system_dlkm
data_mirror    odm_dlkm         system_ext
debug_ramdisk  oem              vendor
dev            postinstall      vendor_dlkm

這是我寫的程式:

#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>

int main(int argc, char **argv) {
    pid_t pid = fork();

    if (pid == 0) {
        chroot(argv[1]);
        chdir("/");

        if (access("/system/bin/su", F_OK) == 0) {
            printf("\e[1;31m""su exist in chroot()!\n""\e[0m");
            //system("/system/bin/su");
        } else {
            printf("\e[1;31m""su not exist in chroot()!\n""\e[0m");
        }
    } else {
        syscall(SYS_pivot_root, argv[1], argv[1]);
        chdir("/");

        if (access("/system/bin/su", F_OK) == 0) {
            printf("\e[1;31m""su exist in pivot_root()!\n""\e[0m");
            system("/system/bin/su");
        }
    }

    return 0;
}

最後想問一下在內核空間裡有沒有方法針對 pivot_root() 做檢查?不然的話好像就只能用 magisk 了 :/

supechicken avatar Jan 03 '24 13:01 supechicken

呃,抱歉,關於 pivot_root() 越獄的問題好像是我的操作姿勢不正確,在隔離了 mount namespace 之後就正常了😅

另外在確定了 pivot_root 的呼叫返回正常後再運行 /system/bin/su 的結果失敗了,顯示 no such file,但在 kmsg 裏能看到 ksu 的紀錄:

[ 2315.298620][T20408] KernelSU: newfstatat su->sh!

正常在 shell 提權的紀錄是這樣的:

[ 2655.433177] KernelSU: faccessat su->sh!
[ 2655.433177] KernelSU: do_execveat_common su found!

有點奇怪,在複製了 .allowlist 之後也是一樣的結果

supechicken avatar Jan 03 '24 14:01 supechicken

呃,抱歉,關於 pivot_root() 越獄的問題好像是我的操作姿勢不正確,在隔離了 mount namespace 之後就正常了😅

另外在確定了 pivot_root 的呼叫返回正常後再運行 /system/bin/su 的結果失敗了,顯示 no such file,但在 kmsg 裏能看到 ksu 的紀錄:

[ 2315.298620][T20408] KernelSU: newfstatat su->sh!

正常在 shell 提權的紀錄是這樣的:

[ 2655.433177] KernelSU: faccessat su->sh!
[ 2655.433177] KernelSU: do_execveat_common su found!

有點奇怪,在複製了 .allowlist 之後也是一樣的結果

内核里会拦截 faccessat newfstatat execveat 三个系统调用,你可以看一下是哪个失败了

tiann avatar Jan 03 '24 14:01 tiann