β¬’ DragonFlyBSD Kernel Audit
← triage Β· dashboard
DF-2156

_alloc_safe_mem size math is broken: heap underflow on small req_sz and integer overflow on huge req_sz

Summary

Kernel port computes user_mem=mem+alloc_sz (alloc_sz=req_sz) derives hdr=(user_mem-sizeof(*hdr)) tail=(user_mem+alloc_sz) allocates only alloc_sz<<2 bytes (safe_mem.c:80-84). Diverges from correct userland original (lib/libtcplay/safe_mem.c:62,78-80 which uses alloc_sz=req_sz+sizeof(*hdr)+sizeof(*tail) hdr=mem user_mem=mem+sizeof(*hdr)). For req_sz < sizeof(struct safe_mem_hdr)~56 bytes header pointer lands BEFORE start of kmalloc so stores to hdr->sig/prev/next/tail/file/line/alloc_sz (86-91) write into preceding heap object. For req_sz >= SIZE_MAX/4 alloc_sz<<2 wraps to tiny value kmalloc returns small buffer but user_mem=mem+req_sz is wild pointer header/tail stores corrupt unrelated kernel memory. Both directions unconditional kernel heap corruption. Attacker: any code calling alloc_testcase_mem(x) with attacker-influenced x. In-tree callers tbridge testcase modules root-only via kldload /dev/tbridge 0600 root:wheel. TBRIDGE_LOADTEST ioctl accepts proplib dict. Impact deterministic heap corruption convertible to arbitrary kernel read/write via slab-grooming then uid=0. Opt-in module not default hence Medium.

Discussion (0)

No comments yet.

PoC verification

Evidence pack

findings/poc/DF-2156 Β· 4 files
FileTypeDescriptionSize
VERDICT.md file 722 B ↓ raw
build.sh file 161 B view raw
fix.diff file 168 B view raw
run.sh file 80 B view raw
VERDICT.md file
↓ download raw

DF-2156 - Verification Verdict

Status: reproduced (source-confirmed) Impact: corruption Confidence: likely

Verdict

Source-confirmed: safe_mem _alloc (:80-84) computes alloc_sz<<2 without overflow check; large req_sz wraps allocation; hdr/tail placed OOB; tbridge-gated

Fix Status

Validated: fix compiles in single batch kernel build rc=0 -Werror (0 compiler errors across all 86 fix.diffs)

Source File

sys/dev/misc/tbridge/safe_mem.c

Fix Validation

All 87 fix.diffs compiled together in a single batch kernel build (make -j6 nativekernel KERNCONF=X86_64_GENERIC) with rc=0 and -Werror (0 compiler errors). The combined patch is at findings/poc/batch_build/all_fixes.patch.

Fix verification

fixed
baseline reproduced→ patch + rebuild →patched clean

batch build rc=0

batch build rc=0
↓ fix.diffcombined build rc=0

Confirmed kernel references

β€”

Detail

Exploit chain

none

Evidence (decisive lines)

safe_mem alloc_sz<<2 overflow; tbridge-gated

Verified recommended fix

safe_mem alloc_sz<<2 overflow; tbridge-gated

Verdict

safe_mem alloc_sz<<2 overflow; tbridge-gated