| Issue |
240313
|
| Summary |
[KCFI][Clang] CodeGenFunction::EmitKCFIOperandBundle Requires FunctionProtoType, Silently Dropping KCFI Checks on Indirect Calls Through K&R Non-Prototype Function Pointers
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
zjy-dev
|
In CodeGenFunction::EmitKCFIOperandBundle (clang/lib/CodeGen/CodeGenFunction.cpp:3004-3016), called for every indirect call under `-fsanitize=kcfi` from `CGCall.cpp:6258-6260`, Clang queries `const FunctionProtoType *FP = CI.getCalleeFunctionProtoType(); if (!FP) return;`. In C89/C99/C11/C17 modes (`-std=gnu17`, `-std=c17`), a function pointer declared with empty parentheses `int (*fp)()` has AST type `FunctionNoProtoType` rather than `FunctionProtoType`. `getCalleeFunctionProtoType()` returns `nullptr`, causing `EmitKCFIOperandBundle` to return early without attaching the `"kcfi"` operand bundle. Consequently, the backend emits a raw, uninstrumented indirect call (`callq *%reg` / `jmpq *%reg` / `blr x8`) with no KCFI hash verification, even though K&R function definitions in the same translation unit still receive `!kcfi_type` metadata and entry type hashes.
### Reproducer (`trigger.c`)
```c
/* kCFI: indirect call through a pointer to a NON-PROTOTYPE function type.
Expect: @target carries !kcfi_type and a type word before its entry, but
call_noproto's indirect call has NO [ "kcfi"(i32 ...) ] bundle. */
int target(void) { return 1; }
typedef int (*noproto_t)();
int call_noproto(noproto_t f) { return f(); }
noproto_t volatile gp = target;
int entry(void) { return call_noproto(gp); }
```
Compile / reproduction command (`['--target=x86_64-unknown-linux-gnu', '-std=c17', '-fsanitize=kcfi', '-O2', '-S', '-emit-llvm']`):
```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon /opt/llvm23/bin/clang --target=x86_64-unknown-linux-gnu -std=c17 -fsanitize=kcfi -O2 -x c - -S -emit-llvm -o -
```
Observed output on LLVM 23.1.2:
```text
define dso_local noundef i32 @target() #0 !kcfi_type !11 {
ret i32 1
}
define dso_local i32 @call_noproto(ptr nofree noundef readonly captures(none) %0) local_unnamed_addr #1 !kcfi_type !12 {
%2 = tail call i32 (...) %0() #2
ret i32 %2
}
```
### Expected behavior
Indirect calls through `FunctionNoProtoType` pointers under `-fsanitize=kcfi` in pre-C23 C modes must either compute a KCFI type hash (matching `CodeGenModule::CreateKCFITypeId` on `FunctionNoProtoType` or synthesized from the call-site argument types) or be rejected/warned under `-fsanitize=kcfi`, never silently emitted as unchecked indirect calls.
### Root cause & impact
- **Source location**: `clang/lib/CodeGen/CodeGenFunction.cpp:3004-3016; clang/lib/CodeGen/CGCall.cpp:6258-6260`
- **Impact**: Any indirect call through a non-prototype function pointer (`int (*fp)()`) in C code compiled with `-std=gnu17` / `-std=c17 -fsanitize=kcfi` completely bypasses Kernel CFI enforcement and executes an unchecked indirect branch.
### Suggested fix
In `CodeGenFunction::EmitKCFIOperandBundle` (CodeGenFunction.cpp:3006-3015), fall back to `CI.getCalleeFunctionType()` (supporting `FunctionNoProtoType` via `CGM.CreateKCFITypeId`) when `getCalleeFunctionProtoType()` is null.
Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `x86_64, aarch64, riscv64`.
Reproduced with clang trunk on Compiler Explorer (2026-11-09); the IR still has no `"kcfi"` operand bundle on the indirect call.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs