Issue 240691
Summary [CFI][compiler-rt] Cross-DSO CFI Shadow Mapping Has Off-by-One Bounds Check in ShadowValue::load (shadow_offset > GetShadowSize()) and Unchecked 16-Bit Page-Offset Overflow in ShadowBuilder::Add
Labels new issue
Assignees
Reporter zjy-dev
    In `compiler-rt/lib/cfi/cfi.cpp`, two bounds-enforcement defects exist in the Cross-DSO CFI shadow table implementation: (1) In `ShadowValue::load(uptr addr)` (lines 118-127), the shadow buffer allocated by `MmapNoReserveOrDie(GetShadowSize(), "CFI shadow")` has valid byte offsets in `[0, GetShadowSize() - 2]`, yet line 122 checks `if (shadow_offset > GetShadowSize())` (compiled to `cmpq shadow_size(%rip), %rax; ja ...` instead of `>=` / `jae`). When `shadow_offset == GetShadowSize()` (an address in the page at `1ULL << kCfiShadowLimitsStorageSize`), `load()` reads `*(uint16_t *)(shadow_base + GetShadowSize())` 2 bytes past the end of the `mmap` region. (2) In `ShadowBuilder::Add(uptr begin, uptr end, uptr cfi_check)` (lines 164-176), the loop `uint16_t sv = ((begin - cfi_check) >> kShadowGranularity) + 1; for (; s <= s_end; s++, sv++) *s = sv;` never checks that `(end - cfi_check) >> 12` fits in `< 0xFFFE`. For any DSO whose executable segment spans > 256 MB above `__cfi_check`, `sv` overflows `uint16_t` into `0xFFFF` (`kUncheckedShadow`, marking the page unchecked fail-open!), then `0x0000` (`kInvalidShadow`), and then wraps around to point `get_cfi_check()` into the middle of the DSO.

### Reproducer (`trigger.cpp`)

```cpp
extern "C" void __cfi_slowpath(unsigned long long, void *);

typedef int (*Fn)(int);
Fn volatile g_target;
int target(int x) { return x + 5; }

int probe(unsigned long long a) {
 __cfi_slowpath(0xdeadbeefcafef00dULL, (void *)a);
  Fn p = g_target;
 return p ? p(1) : 0;
}

int main(void) {
  g_target = target;
 probe(0);
  probe(0x800000000000ULL);   /* first address past the 47-bit user range on x86_64 */
  probe(0x7fffffffffffULL);  /* max valid user address */
  probe(~0ULL);
  return 0;
}
```

Runtime reproduction command (LLVM 23.1.2):

```bash
podman exec defuzz-toolchain-daemon /opt/llvm23/bin/llvm-objdump -dr \
 /opt/llvm23/lib/clang/23/lib/x86_64-unknown-linux-gnu/libclang_rt.cfi_diag.a | \
  sed -n '/<__cfi_slowpath_diag>:/,+30p'
```

On x86_64, `GetShadowSize()` is `(0x7fffffffffff >> 11) + 1 = 0x40000000`,
and `MemToShadowOffset(0x20000000000) = 0x40000000`.  The `ja` at 0x2d is
therefore not taken for that address, and the 16-bit load at 0x36 reads two
bytes past the end of the `mmap`ed shadow region.

Observed output on LLVM 23.1.2:

```text
1b: 48 89 f0                     	movq	%rsi, %rax
      1e: 48 c1 e8 0b                  	shrq	$0xb, %rax
      22: 48 83 e0 fe                  	andq	$-0x2, %rax
      26: 48 3b 05 00 00 00 00 	cmpq	(%rip), %rax            # 0x2d <__cfi_slowpath_diag+0x2d>
		0000000000000029: R_X86_64_PC32	.bss._ZN5__cfiL25cfi_shadow_limits_storageE+0x4
      2d: 77 16                        	ja	0x45 <__cfi_slowpath_diag+0x45>
      2f: 48 8b 0d 00 00 00 00         	movq	(%rip), %rcx            # 0x36 <__cfi_slowpath_diag+0x36>
		0000000000000032: R_X86_64_PC32	.bss._ZN5__cfiL25cfi_shadow_limits_storageE-0x4
      36: 0f b7 04 01                  	movzwl	(%rcx,%rax), %eax
```

### Expected behavior

`ShadowValue::load` at `compiler-rt/lib/cfi/cfi.cpp:122` must use `if (shadow_offset >= GetShadowSize())`, and `ShadowBuilder::Add` at lines 164-176 must `CHECK` (or clamp) that `((end - 1 - cfi_check) >> kShadowGranularity) + 1 < kUncheckedShadow` (`0xFFFF`).

### Root cause & impact

- **Source location**: `compiler-rt/lib/cfi/cfi.cpp:118-127, 164-176`
- **Impact**: Out-of-bounds 2-byte read one slot past the end of the CFI shadow mapping when `shadow_offset == GetShadowSize()`, and silent fail-open (`sv == 0xFFFF` = `kUncheckedShadow`) plus arbitrary `cfi_check` pointer corruption for DSOs with code segments > 256 MB.

### Suggested fix

Change `if (shadow_offset > GetShadowSize())` to `if (shadow_offset >= GetShadowSize())` in `compiler-rt/lib/cfi/cfi.cpp:122`, and add `CHECK(((end - 1 - cfi_check) >> kShadowGranularity) + 1 < kUncheckedShadow);` in `ShadowBuilder::Add` at line 174.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `x86_64, aarch64, riscv64, loongarch64`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to