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

mtx_notlocked_ex() returns the exact opposite of its documented contract (inverted exclusive-lock test in shipped header)

Field Value
ID DF-2787
Status new
Severity Info
CVSS 3.1 CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:N
CWE CWE-628 Functionally Incorrect Contract Implementation
File sys/kern/kern_mutex.c
Lines mutex2.h:436-442 (header consumer of this file's locking)
Area kern
Confidence certain
Discovered 2026-08-31
Pass 2 (GLM 5.3 second pass)
Bucket base:kern
Reported pending
Known CVE none
CVE match novel

Summary

The comment says "Return TRUE (non-zero) if the mutex is not locked exclusively" but the implementation returns ((mtx->mtx_lock & MTX_EXCLUSIVE) != 0) β€” TRUE when the mutex IS exclusively locked. Zero in-tree callers (only the correct sibling mtx_notlocked() is used), so a latent API trap, not a live defect.

Invert the test to == 0 (matches the contract and the mtx_notlocked() naming convention).

Timeline

  • 2026-08-31 Discovered during pass-2 audit of kern_mutex.c (GLM 5.3).

Discussion (0)

No comments yet.

PoC verification

Evidence pack

findings/poc/DF-2787 Β· 10 files
FileTypeDescriptionSize
README.md β€” 744 B ↓ raw
VERDICT.md β€” 775 B ↓ raw
build.sh β€” 50 B view raw
env.txt β€” 509 B view raw
fix.diff β€” 297 B view raw
h41.c β€” 14.9 KB view raw
harness.log β€” 2.0 KB view raw
manifest.json β€” 495 B view raw
run.sh β€” 105 B view raw
verdict.json β€” 1.7 KB view raw

DF-2787 β€” mtx_notlocked_ex inverted contract (Info-tier hardening verification)

What

mtx_notlocked_ex inverted contract

Build

cd /root/h41 && make

Run

kldload ./h41.ko; sysctl -w debug.h41.cmd=13

Expected

baseline (stock kernel): see verdict below fix kernel (all-41-fixes build): the bad behavior is gone

A/B one-liner: baseline: 1 then 0 (inverted) | fix: 0 then 1

Files

README.md VERDICT.md build.sh env.txt fix.diff h41.c harness.log manifest.json run.sh verdict.json

Shared harness: h41.c (combined KLD, sysctl debug.h41.cmd=N driven). Full batch context: the fix kernel contains all 41 hardening fixes of this batch compiled together (build RC=0) and passed the complete A/B suite plus a clean reboot.

VERDICT.md
↓ download raw

DF-2787 β€” VERDICT

status: reproduced | reproduced: 1 | impact: none | confidence: certain

Confirmed both by reading and mechanically: mtx_notlocked_ex (sys/sys/mutex2.h:436-442) documents 'Return TRUE (non-zero) if the mutex is not locked exclusively' but returns (mtx_lock & MTX_EXCLUSIVE) != 0 - TRUE when it IS exclusively locked. Harness T13 on the stock guest: while exclusively locked the function returned 1 (doc says 0); after unlock it returned 0 (doc says 1) - the exact inversion. Zero in-tree callers (rg: only the definition), so latent API trap only. On the fix kernel T13 returns 0 then 1 per the documented contract.

Baseline vs fix kernel

baseline: baseline: 1 then 0 (inverted) | fix: 0 then 1 fix: behavior changed as designed (see verdict)

Fix verification

fixed
baseline reproduced→ patch + rebuild →patched clean

A/B: baseline T13 while-ex-locked=1/after-unlock=0 (inverted); fix kernel 0/1 (doc-correct).

↓ fix.diffDragonFly dfbsd 6.5-DEVELOPMENT #1: Mon Oct 26 01:57:23 UTC 2026 root@dfbsd:/usr/obj/usr/src/sys/X86_64_GENERIC x86_64 (h41 fix kernel, all 41 hardening fixes applied)

Confirmed kernel references

Detail

Evidence (decisive lines)

harness.log T13 lines; fix.diff (== 0 inversion)

PoC changes

none

Verified recommended fix

Return ((mtx->mtx_lock & MTX_EXCLUSIVE) == 0)

Verdict

Confirmed both by reading and mechanically: mtx_notlocked_ex (sys/sys/mutex2.h:436-442) documents Return TRUE (non-zero) if the mutex is not locked exclusively but returns (mtx_lock & MTX_EXCLUSIVE) != 0 - TRUE when it IS exclusively locked. Harness T13 on the stock guest: while exclusively locked the function returned 1 (doc says 0); after unlock it returned 0 (doc says 1) - the exact inversion. Zero in-tree callers (rg: only the definition), so latent API trap only. On the fix kernel T13 returns 0 then 1 per the documented contract.