| Issue |
241108
|
| Summary |
[KCFI] KCFI check scratch registers overwrite a live call argument on X86 (preserve_none R11, nest R10), RISC-V (nest t2, 9th fastcc argument in t3) and ARM (nest r12)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
The KCFI check emitted before an indirect call uses hard-wired scratch registers chosen after register allocation, and only avoids the register that holds the call target. It never checks whether the scratch register also carries an outgoing argument of the same call. On three targets (five cases) the scratch is an argument register, so the callee receives the type hash, 0, or the function pointer instead of the argument. This is the same pattern as #236673 (AArch64 `W9` vs the 23rd `preserve_none` argument), which only covers AArch64.
In all cases the same call without the `"kcfi"` bundle is correct.
### 1. X86: 11th `preserve_none` argument in R11 (from C)
`X86TargetLowering::EmitKCFICheck` unfolds a memory call target into a hard-coded `R11`. The 11th `preserve_none` integer argument is `R11`. Caller and callee must be in separate TUs, otherwise the result is constant-propagated.
```c
// t.c
__attribute__((preserve_none, noinline)) long
ptarget(long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8,
long a9, long a10, long a11) {
return a11;
}
__attribute__((preserve_none)) long (*fp)(long, long, long, long, long, long,
long, long, long, long, long);
__attribute__((noinline)) long pcaller(long s) {
return fp(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, s);
}
```
```c
// driver.c
#include <stdio.h>
__attribute__((preserve_none)) long ptarget(long, long, long, long, long, long,
long, long, long, long, long);
extern __attribute__((preserve_none)) long (*fp)(long, long, long, long, long,
long, long, long, long, long, long);
long pcaller(long);
int main(void) {
fp = ptarget;
long got = pcaller(305419896);
printf("got=%ld same=%d\n", got, got == 305419896L);
return got != 305419896L;
}
```
```console
$ clang -O2 -fsanitize=kcfi -fno-stack-protector -c t.c driver.c && clang t.o driver.o && ./a.out
got=94419338088784 same=0 # the address of ptarget
$ clang -O2 -fno-stack-protector -c t.c driver.c && clang t.o driver.o && ./a.out
got=305419896 same=1
```
`-O1`, `-O2`, `-O3` and `-Os` with `-fsanitize=kcfi` are wrong; `-O0` is correct.
```asm
pcaller:
movq %rdi, %r11 # 11th argument
...
movq fp(%rip), %r11 # unfolded call target overwrites it
movl $2170348587, %r10d
addl -4(%r11), %r10d
je .Ltmp0
ud2
.Ltmp0:
callq *%r11
```
Godbolt (trunk, `t.c` with and without `-fsanitize=kcfi`): https://godbolt.org/z/3qbPfc615
### 2. X86: `nest` argument in R10
`X86AsmPrinter::LowerKCFI_CHECK` always writes the negated type id into `R10D` (or `R11D` when the target is `R10`). SysV `nest` is `R10`. Because the check is expanded post-RA, `-O0` is wrong too.
```llvm
target triple = "x86_64-unknown-linux-gnu"
@fmt = private unnamed_addr constant [19 x i8] c"kcfi=%llu ok=%llu\0A\00", align 1
declare i32 @printf(ptr, i64, i64)
define i64 @target(ptr nest %n) !kcfi_type !1 {
%v = ptrtoint ptr %n to i64
ret i64 %v
}
@fp = global ptr null
define i64 @caller(i64 %s) noinline {
%p = load volatile ptr, ptr @fp, align 8
%n = inttoptr i64 %s to ptr
%r = call i64 %p(ptr nest %n) [ "kcfi"(i32 12345678) ]
ret i64 %r
}
define i64 @caller_ok(i64 %s) noinline {
%p = load volatile ptr, ptr @fp, align 8
%n = inttoptr i64 %s to ptr
%r = call i64 %p(ptr nest %n)
ret i64 %r
}
define i32 @main() {
store ptr @target, ptr @fp, align 8
%a = call i64 @caller(i64 305419896)
%b = call i64 @caller_ok(i64 305419896)
%c = call i32 @printf(ptr @fmt, i64 %a, i64 %b)
ret i32 0
}
!llvm.module.flags = !{!0}
!0 = !{i32 4, !"kcfi", i32 1}
!1 = !{i32 12345678}
```
```console
$ clang -x ir t.ll -O2 -o t && ./t
kcfi=0 ok=305419896 # expected kcfi=305419896
```
```asm
caller:
movq %rdi, %r10 # nest
movl $4282621618, %r10d # KCFI check overwrites nest
addl -4(%rax), %r10d # a matching type leaves r10d = 0
je .Ltmp0
ud2
.Ltmp0:
jmpq *%rax
```
### 3. RISC-V: `nest` argument in t2 (X7)
`RISCVAsmPrinter::LowerKCFI_CHECK` uses `X6`/`X7` as scratch and writes the expected hash into `X7`. Without Zicfilp, `nest` is `X7` (`t2`). Same IR as section 2 with `target triple = "riscv64-unknown-linux-gnu"`:
```console
$ clang --target=riscv64-linux-gnu -static -O2 -x ir t.ll -o t && qemu-riscv64 ./t
kcfi=12345678 ok=305419896 # nest now holds the type id
```
```asm
caller:
ld t1, 0(a1)
lw t3, -4(t1)
lui t2, 3014
addiw t2, t2, 334 # t2 = 12345678, nest is gone
beq t3, t2, .Ltmp0
ebreak
.Ltmp0:
jr t1
```
### 4. RISC-V: 9th `fastcc` argument in t3 (X28)
When the call target is in `X6`/`t1`, `LowerKCFI_CHECK` falls back to `X28`-`X31` for the first scratch register. `fastcc` passes integer arguments 9 to 12 in `t3`-`t6` (`X28`-`X31`), so with 12 `fastcc` arguments the type load `lw t3, -4(t1)` replaces the 9th argument. No reserved registers or other flags are needed; it happens at plain `-O0` and `-O2`.
```llvm
target triple = "riscv64-unknown-linux-gnu"
@fp = global ptr null
@res = global i64 0
@resok = global i64 0
@fmt = private constant [19 x i8] c"kcfi=%#lx ok=%#lx\0A\00"
declare i32 @printf(ptr, ...)
define fastcc i64 @ptarget(i64 %a1, i64 %a2, i64 %a3, i64 %a4, i64 %a5,
i64 %a6, i64 %a7, i64 %a8, i64 %a9, i64 %a10,
i64 %a11, i64 %a12) !kcfi_type !1 {
ret i64 %a9
}
define void @pcaller() noinline {
%p = load volatile ptr, ptr @fp
%r = call fastcc i64 %p(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7,
i64 8, i64 305419896, i64 10, i64 11, i64 12)
[ "kcfi"(i32 12345678) ]
store i64 %r, ptr @res
ret void
}
define void @pcaller_ok() noinline {
%p = load volatile ptr, ptr @fp
%r = call fastcc i64 %p(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7,
i64 8, i64 305419896, i64 10, i64 11, i64 12)
store i64 %r, ptr @resok
ret void
}
define i32 @main() {
store ptr @ptarget, ptr @fp
call void @pcaller()
call void @pcaller_ok()
%a = load i64, ptr @res
%b = load i64, ptr @resok
%c = call i32 (ptr, ...) @printf(ptr @fmt, i64 %a, i64 %b)
ret i32 0
}
!llvm.module.flags = !{!0}
!0 = !{i32 4, !"kcfi", i32 1}
!1 = !{i32 12345678}
```
```console
$ clang --target=riscv64-linux-gnu -O2 -c -x ir tfast.ll && clang --target=riscv64-linux-gnu -static tfast.o -o t && qemu-riscv64 ./t
kcfi=0xbc614e ok=0x12345678 # 0xbc614e = 12345678, the type id loaded into t3
```
```asm
pcaller:
ld t1, 0(a0) # call target in t1
...
addi t3, a2, 1656 # 9th argument = 0x12345678
li t4, 10
li t5, 11
li t6, 12
lw t3, -4(t1) # KCFI fallback scratch overwrites it
lui t2, 3014
addiw t2, t2, 334
beq t3, t2, .Ltmp0
```
The open PR #240347 makes the RISC-V lowering avoid `X7` only when `cf-protection=branch` is enabled (Zicfilp landing-pad label), and then falls back to `X28`, the same register as in this section. A `nest` argument in `X7` without Zicfilp, and `fastcc` arguments in `X28`-`X31`, are still overwritten.
### 5. ARM (soft-float AAPCS): `nest` argument in r12
`ARMAsmPrinter::EmitKCFI_CHECK_ARM32` (and `EmitKCFI_CHECK_Thumb2`) use `R12` as scratch, and only spill the `R3` fallback when it is a call argument. Soft-float AAPCS passes `nest` in `R12`. Same `caller`/`caller_ok` pair as section 2 with `target triple = "armv7-unknown-linux-gnueabi"`, run as a freestanding binary whose callee returns `r12`:
```console
$ clang --target=armv7-unknown-linux-gnueabi -fuse-ld=lld -nostdlib -static -O2 -x ir t.ll -o t && qemu-arm ./t
kcfi=00000000 ok=12345678
```
```asm
caller:
mov r12, r0 @ nest
ldr r1, [r1]
bic r12, r1, #1 @ KCFI scratch overwrites nest
ldr r12, [r12, #-4]
eor r12, r12, #78
eor r12, r12, #24832
eor r12, r12, #12320768
eors r12, r12, #0 @ match => r12 = 0
beq .Ltmp0
udf #33761
.Ltmp0:
bx r1
```
Hard-float (`gnueabihf`) is not affected because `CC_ARM_AAPCS_VFP` has no `CCIfNest` rule.
### Versions
- X86 `preserve_none` R11: wrong with clang 19 to 23 and trunk (24.0.0, e2b36dd0611a) at `-O1` and above. clang 15 has no `-fsanitize=kcfi`; clang 16 and 17 ignore `preserve_none`.
- X86 `nest` R10: wrong with clang 16.0.4, 17.0.6, 19 to 23 and trunk at `-O0` and `-O2`. clang 15.0.6 does not overwrite `R10`.
- RISC-V `nest` t2: wrong with clang 17.0.6, 19 to 23 and trunk at `-O0` and `-O2`. clang 15.0.6 is correct; clang 16 rejects the bundle on RISC-V.
- RISC-V `fastcc` t3: wrong with clang 19 to 23 and trunk at `-O0` and `-O2`.
- ARM `nest` r12: wrong with clang 22, 23 and trunk at `-O0` and `-O2` (ARM KCFI support landed in #163698). Older versions reject the bundle.
### Where
- `llvm/lib/Target/X86/X86ISelLowering.cpp`, `X86TargetLowering::EmitKCFICheck`: `unfoldMemoryOperand(..., X86::R11, /*UnfoldLoad=*/true, ...)`.
- `llvm/lib/Target/X86/X86MCInstLower.cpp`, `X86AsmPrinter::LowerKCFI_CHECK`: `unsigned TempReg = AddrReg == X86::R10 ? X86::R11D : X86::R10D;`.
- `llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp`, `RISCVAsmPrinter::LowerKCFI_CHECK`: `ScratchRegs[] = {X6, X7}` with fallback `X28`-`X31`; `isRegAvailable` only rejects the target and user-reserved registers.
- `llvm/lib/Target/ARM/ARMAsmPrinter.cpp`, `EmitKCFI_CHECK_ARM32` and `EmitKCFI_CHECK_Thumb2`: `ScratchReg = ARM::R12`; only `R3` is spilled when it is live into the call.
None of the `KCFI_CHECK` pseudos has an implicit def of its scratch register, so register allocation is free to keep an argument there.
### Possible fix (not tested)
In each lowering, treat registers that are implicit uses of the bundled call as unavailable when choosing the scratch register, as is already done for the call target (and for `R3` on ARM). X86 can unfold into a free register instead of a fixed `R11` in `EmitKCFICheck` and pick a scratch other than an implicit use in `LowerKCFI_CHECK`; RISC-V can add argument registers (`X7`, and `X28`-`X31` for `fastcc`) to the `isRegAvailable` exclusions; ARM can reuse its existing spill path for `R12`. A shared "is this register live into the call" helper would also cover the AArch64 case in #236673.
cc @samitolvanen @topperc
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs