diff --git a/.github/workflows/build-lkm.yml b/.github/workflows/build-lkm.yml index 74e9909af9cc..adf86e02c7c6 100644 --- a/.github/workflows/build-lkm.yml +++ b/.github/workflows/build-lkm.yml @@ -17,6 +17,11 @@ on: required: false default: '' type: string + module_cfi: + description: '1 = kCFI module build, 0 = no kCFI (kernels with CONFIG_CFI_CLANG=n)' + required: false + default: '0' + type: string workflow_dispatch: jobs: build-lkm: @@ -37,3 +42,4 @@ jobs: package_name: ${{ inputs.package_name || '' }} expected_size: ${{ inputs.expected_size || '' }} expected_hash: ${{ inputs.expected_hash || '' }} + module_cfi: ${{ inputs.module_cfi || '0' }} diff --git a/.github/workflows/ddk-lkm.yml b/.github/workflows/ddk-lkm.yml index fc94aef86126..44e7a9542a36 100644 --- a/.github/workflows/ddk-lkm.yml +++ b/.github/workflows/ddk-lkm.yml @@ -27,6 +27,11 @@ on: required: false default: '' type: string + module_cfi: + description: '1 = build the module with kCFI checks (GKI kernels), 0 = no KCFI (Samsung/MTK kernels with CONFIG_CFI_CLANG=n)' + required: false + default: '0' + type: string jobs: build-kernelsu-ko: @@ -45,6 +50,7 @@ jobs: PACKAGE_NAME: ${{ inputs.package_name }} EXPECTED_SIZE: ${{ inputs.expected_size }} EXPECTED_HASH: ${{ inputs.expected_hash }} + MODULE_CFI: ${{ inputs.module_cfi }} run: | git config --global --add safe.directory /__w/KernelSU/KernelSU @@ -62,6 +68,12 @@ jobs: if [ -n "$EXPECTED_HASH" ]; then EXTRA_ARGS="$EXTRA_ARGS KSU_EXPECTED_HASH=$EXPECTED_HASH" fi + # Kernels built with CONFIG_CFI_CLANG=n (Samsung/MTK android15-6.6, e.g. + # SM-A055F) have no kCFI hashes, so a module compiled with checks traps on + # the first indirect call. Pass KSU_MODULE_CFI=0 for those targets. + if [ -n "$MODULE_CFI" ]; then + EXTRA_ARGS="$EXTRA_ARGS KSU_MODULE_CFI=$MODULE_CFI" + fi if [ "${{ github.event_name }}" = "pull_request" ]; then EXTRA_ARGS="$EXTRA_ARGS KSU_IS_PR_BUILD=1" fi diff --git a/kernel/Kbuild b/kernel/Kbuild index 430e8feaf003..c3819ab8a03f 100644 --- a/kernel/Kbuild +++ b/kernel/Kbuild @@ -1,3 +1,18 @@ +# KCFI. The module is built against DDK/GKI kernel trees, which set CONFIG_CFI_CLANG=y, +# so every indirect call in this module carries a kCFI type check. Kernels can ship +# the opposite configuration (CONFIG_CFI_CLANG=n): Samsung/MTK android15-6.6 on +# SM-A055F reports "# CONFIG_CFI_CLANG is not set". Neither their kernel functions nor +# our own __nocfi helpers carry a type hash, so any indirect call out of a CFI-enabled +# module function traps with "Internal error: BRK handler: f2008228". +# +# Default stays CFI-enabled, matching GKI. Build with KSU_MODULE_CFI=0 (opt-in) for +# kernels that do not enforce KCFI. +KSU_MODULE_CFI ?= 1 + +ifeq ($(KSU_MODULE_CFI),0) +ccflags-y += -fno-sanitize=kcfi +endif + kernelsu-objs := core/init.o kernelsu-objs += compat/jailbreak.o compat/kdp.o compat/defex.o compat/rkp.o diff --git a/kernel/feature/sucompat.c b/kernel/feature/sucompat.c index f5cd7bf232a4..3d0433362c38 100644 --- a/kernel/feature/sucompat.c +++ b/kernel/feature/sucompat.c @@ -88,7 +88,16 @@ static bool is_ksud_exists() return true; } -long ksu_handle_faccessat_sucompat(int orig_nr, struct pt_regs *regs) +/* + * These handlers call ksu_syscall_table[orig_nr](regs). The table entry is one of + * our own __nocfi hooks (ksu_hook_faccessat / ksu_hook_newfstatat / ksu_hook_execve*), + * so a KCFI type check emitted here compares the compiled-in prototype hash against + * whatever bytes happen to precede a __nocfi target and traps: + * Internal error: BRK handler: f2008228 (kCFI) + * pc: ksu_handle_stat_sucompat+0x68/0x17c + * Marking the callers __nocfi drops the check, which is what these functions need. + */ +long __nocfi ksu_handle_faccessat_sucompat(int orig_nr, struct pt_regs *regs) { const char __user **filename_user, *orig_filename; long ret; @@ -123,7 +132,7 @@ long ksu_handle_faccessat_sucompat(int orig_nr, struct pt_regs *regs) return ksu_syscall_table[orig_nr](regs); } -long ksu_handle_stat_sucompat(int orig_nr, struct pt_regs *regs) +long __nocfi ksu_handle_stat_sucompat(int orig_nr, struct pt_regs *regs) { const char __user **filename_user, *orig_filename; long ret; @@ -158,9 +167,9 @@ long ksu_handle_stat_sucompat(int orig_nr, struct pt_regs *regs) return ksu_syscall_table[orig_nr](regs); } -static long ksu_handle_execve_sucompat_common(const char __user **filename_user, - const char __user *const __user *argv_user, unsigned long envp, - bool execveat, int orig_nr, struct pt_regs *regs) +static long __nocfi ksu_handle_execve_sucompat_common(const char __user **filename_user, + const char __user *const __user *argv_user, unsigned long envp, + bool execveat, int orig_nr, struct pt_regs *regs) { const char __user *fn; struct ksu_sulog_pending_event *pending_sucompat = NULL;