Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/build-lkm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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' }}
12 changes: 12 additions & 0 deletions .github/workflows/ddk-lkm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions kernel/Kbuild
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 14 additions & 5 deletions kernel/feature/sucompat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down