-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The selrecord contract\n\nFreeBSD's kernel supports `poll(2)` and `select(2)` on a wide variety of file descriptor types. Each type that wants to be pollable embeds a `struct selinfo` in its owner object and exposes a `_poll` callback. When userspace blocks on a pollable fd, the kernel runs the callback. The callback calls `selrecord(td, &owner->selinfo)`. `selrecord()` allocates a `struct selfd`, sets `selfd->sf_si = &owner->selinfo`, and links the selfd into both the thread's per-call list and the owner's `si_tdlist`, so a subsequent `selwakeup()` or timeout can find every blocked thread and clean up after it.\n\nThe contract has two halves. The owner is responsible for both.\n\nFirst, when an event occurs that should wake the waiters, the owner calls `selwakeup(&owner->selinfo)`. `selwakeup()` walks `si_tdlist`, wakes each blocked thread, and clears the per-thread selfd's `sf_si` pointer so a subsequent timeout in that thread does not dereference the owner.\n\nSecond, and this is the half that recurs in the bug reports, when the owner is being freed, the owner is supposed to call `seldrain(&owner->selinfo)`. `seldrain()` is a wake-and-clear pass designed for the final-free path where the owner is about to disappear. Without it, any blocked thread that wakes later, from timeout, signal, or unrelated poll-set teardown, runs `selfdfree()` on a stale `selfd` whose `sf_si` and `sf_mtx` point at memory the kernel has already returned to the allocator.\n\nThe contract is documented in tree commentary on selinfo-bearing structs and in long-standing patches that retrofitted `seldrain()` into other subsystems. The contract is not enforced by the type system, by the build, or by any kernel lint.\n\n## procdesc_free does not honor it\n\n`procdesc` is the FreeBSD subsystem that lets a process hold a descriptor referring to another process. The descriptor is created by `pdfork(2)`, an unprivileged syscall, and the resulting fd supports `poll(2)`. The owner object is `struct procdesc`, defined in `sys/sys/procdesc.h`. Its selinfo field is `pd->pd_selinfo`.\n\nThe constructor at `sys/kern/sys_procdesc.c:223` initializes it:\n\n```c\nknlist_init_mtx(&pd->pd_selinfo.si_note, &pd->pd_lock);\n```\n\nThe poll handler at `procdesc_poll()` registers the caller:\n\n```c\nselrecord(td, &pd->pd_selinfo);\n```\n\nThe final-free path at `procdesc_free()` ships the bug:\n\n```c\nif (refcount_release(&pd->pd_refcount)) {\n ...\n knlist_destroy(&pd->pd_selinfo.si_note);\n PROCDESC_LOCK_DESTROY(pd);\n free(pd, M_PROCDESC);\n}\n```\n\nThere is no `seldrain(&pd->pd_selinfo)` in `procdesc_free()`. There never has been. The function was committed in 2012 with the procdesc subsystem, and every revision between then and the May 2026 patch shipped the same omission. The branch scan in Calif's writeup confirms the span: `main`, `stable/15`, `stable/14`, `stable/13`, `stable/12`, `stable/11`, `stable/10`, `stable/9`, and every release branch inside each, all marked `VULN`.\n\nThe patch is a single line. Commit `4b6a23e`, authored by markjdb on 20 May 2026, title \"procdesc: Make sure to drain selinfo sleepers in procdesc_free()\":\n\n```diff\n if (refcount_release(&pd->pd_refcount)) {\n ...\n+ seldrain(&pd->pd_selinfo);\n knlist_destroy(&pd->pd_selinfo.si_note);\n PROCDESC_LOCK_DESTROY(pd);\n free(pd, M_PROCDESC);\n }\n```\n\n## From timeout to kernel pointer write\n\nThe chain Calif demonstrates is end-to-end local privilege escalation. The unprivileged process does this:\n\n1. `pdfork(2)` to create a new `struct procdesc` and a child.\n2. Spawn two threads, each blocked in `poll(2)` on the procdesc fd with a finite timeout.\n3. From a third thread, `close()` the procdesc fd while the child remains alive.\n\nThe close drops the last fd reference, `procdesc_free()` runs, the `struct procdesc` is released back to its UMA bucket. The two blocked threads still hold `struct selfd` records whose `sf_si` and `sf_mtx` point into the freed memory.\n\nThe attacker reclaims the slot with `SCM_RIGHTS`. UNIX-domain sockets allow sending file descriptors as ancillary data. The kernel internalizes them with:\n\n```c\nfdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, M_WAITOK);\n```\n\nat `sys/kern/uipc_usrreq.c:3773`. `struct filedescent` is `0x30` bytes on the tested kernel; passing two file descriptors allocates `0x60`, which lands in the same UMA bucket as the freed `0x80` procdesc. By priming the passed fd with `cap_ioctls_limit(passfd, dummy, 8)` before the send, the resulting filedescent has `fde_caps.fc_nioctls == 8` and `fde_caps.fc_ioctls` pointing at an attacker-allocated 8-qword buffer. The offset of `fde_caps.fc_ioctls` inside `struct filedescent` is `0x18`. The offset of `pd_selinfo.si_tdlist.tqh_first` inside `struct procdesc` is also `0x18`.\n\nWhen the first poll thread's timeout fires, `seltdclear()` calls `selfdfree()`, which runs:\n\n```c\nmtx_lock(sfp->sf_mtx);\nTAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);\nmtx_unlock(sfp->sf_mtx);\nfree(sfp, M_SELFD);\n```\n\n`sfp->sf_si` is the freed procdesc's selinfo address, which is the freed procdesc plus a fixed offset. The macro expansion of `TAILQ_REMOVE()` writes the second waiter's `struct selfd *` into `(old_procdesc + 0x18)`. That offset, post-reclaim, is `fdev[0].fde_caps.fc_ioctls`.\n\nThe receiving end of the `SCM_RIGHTS` exchange now holds an fd whose Capsicum ioctl pointer is a kernel pointer to a live `struct selfd`. `cap_ioctls_get(recvfd, leak, 8)` copies 64 bytes from that pointer to userspace; the attacker has leaked the second selfd's `sf_si`, `sf_mtx`, owning thread, and poll cookie. `cap_ioctls_limit(recvfd, NULL, 0)` frees that selfd. `cap_ioctls_limit(fakefd, fake_qwords, 8)` reclaims the freed slot with attacker-controlled qwords.\n\nWhen the second poll thread's timeout fires, the same `TAILQ_REMOVE()` runs over the attacker's forged selfd. The forged `sf_threads.tqe_next` and `sf_threads.tqe_prev` drive the macro's two writes. One is an attacker-chosen value to an attacker-chosen kernel address.\n\nThe final useful write targets a polling thread's `td_ucred` field. The value is the kernel address of a fake `struct ucred` the attacker placed inside a pipe buffer earlier in the chain. When that thread returns from `poll()`, its `td_ucred` briefly points at a forged root credential. It uses the window to `chown(argv[0], 0, 0)` and `chmod(argv[0], 04755)` on the exploit binary. The exploit then exits, the SUID-root binary remains on disk, and `--shell` runs `/bin/sh -p` to drop a root shell.\n\n## The Capsicum primitive triad\n\nThe chain uses Capsicum's per-fd ioctl allowlist API as a three-way primitive over the same `fc_ioctls` field. The roles, from Calif's writeup:\n\n```text\ncap_ioctls_limit(passfd, dummy, 8)\n primes an fd so SCM_RIGHTS copies fc_nioctls=8 into fdev[0].\n\ncap_ioctls_get(recvfd, leak, 8)\n treats corrupted fc_ioctls as a kernel pointer and copies 64 bytes\n from that address to userspace.\n\ncap_ioctls_limit(recvfd, NULL, 0)\n frees the leaked live selfd because fc_ioctls now points at it.\n\ncap_ioctls_limit(fakefd, fake_qwords, 8)\n allocates a fresh 64-byte M_FILECAPS ioctl array, reclaiming the\n freed selfd slot with attacker-controlled qwords.\n```\n\nThe same field is the read source, the free target, and the reclaim destination. The Capsicum helpers were designed to constrain capabilities. In this chain they are the ladder.\n\n## The same advisory covers netmap\n\nFreeBSD-SA-26:19 is filed against the `file` module with a single CVE. Commit `53a78e582a6f`, by markjdb on the same day as the procdesc fix, ships in the same security release. Its title is \"netmap: Drain selinfo sleepers in nm_os_selinfo_uninit()\". The diff adds, at `sys/dev/netmap/netmap_freebsd.c`, the line:\n\n```c\nseldrain(&si->si);\n```\n\n`procdesc` was one subsystem missing the call. `netmap` was another. The two patches ship together because they are the same bug class with two demonstrated instances.\n\nThis is a [design-debt-driver](/patterns/design-debt-driver). The `selrecord`/`seldrain` contract is a kernel invariant that every selinfo owner must independently honor. The contract is not captured by the API surface, not enforced by the type system, and not checked by any lint. Each subsystem implements its own teardown, and each one is free to forget. We have written about this shape before, where a [single-line fix in one Apache Camel component left five sibling subclasses unchanged](/posts/camel-cve-2026-40453-five-subclasses-needed-the-same-line), and where a [2012-era nginx patch turned out to cover one half of its own structural class fourteen years later](/posts/nginx-cve-2026-42945-the-other-half-of-the-2012-patch). The selinfo case is the same shape at kernel scope. The patch closes procdesc and netmap. The contract holds the primitive.\n\nHow many other selinfo owners across the FreeBSD tree are missing a `seldrain()` on a final-free path is a question the advisory does not answer. Calif's writeup names six audit properties; the first two are the question. The remaining selinfo owners are the future advisories.\n\n## Calif published fifteen bugs and one audit skill\n\nCalif's writeup includes an \"Audit Pattern Checklist\". It enumerates six properties the procdesc bug matched:\n\n- Embedded `struct selinfo` / `knlist` owner lifetime requires waiter drain before owner free.\n- Every `selrecord()` path requires a matching `seldrain()` on every final-free path of its owner.\n- `selwakeup()` is insufficient unless guaranteed on every free path.\n- `selfdfree()` stale `TAILQ_REMOVE()` is a controlled write primitive.\n- `SCM_RIGHTS struct filedescent[]` allocations are a same-bucket reclaim target.\n- Capsicum `cap_ioctls_get()` and `cap_ioctls_limit()` are read, free, and reclaim primitives over `fc_ioctls`.\n\nThe first three are auditor rules. The last three are exploit primitives. Together they are a portable recipe. Calif's blog post says they shared \"the audit skill\" with the FreeBSD team. The deliverable was the recipe.\n\nThe same audit produced fifteen kernel bugs on a first pass: five LPEs, three RCEs, one bhyve guest-to-host escape, and several memory disclosures and DoS. Three of the LPEs, including this one, were published with full exploits and writeups. The blog post that announces the work thanks \"OpenAI and Anthropic, for the tokens.\"\n\nThe path embedded in Calif's writeup names its working directory: `/home/x/kernel/codex_fbsd_exec/procdesc_filecaps_lpe15.c`. `codex_fbsd_exec` is the working directory of OpenAI's Codex coding system, executing against a local FreeBSD 15 build. The exploit is 882 lines of C, with kernel offsets, UMA-bucket reclaim arithmetic, and a thread-borrowing credential primitive. Calif publishes it verbatim, with the stated intent of keeping the AI text \"as-is, as a historical artifact showing what AI vulnerability research looked like in 2026.\"\n\nEvery FreeBSD release between January 2012 and May 2026 shipped this one-line UAF. No human auditor reading `sys_procdesc.c` ever asked the question that found it. The patch closes procdesc and netmap. The question Calif wrote down does not.\n\nPoC: [califio/publications](https://github.com/califio/publications/tree/main/MADBugs/freebsd/file-CVE-2026-45251)","closing_line":"Every kernel that ships a selrecord-shaped primitive has a new auditor pointed at it. The auditor scales with tokens.","hook_md":"procdesc_free() in the FreeBSD kernel destroys a struct procdesc that holds an embedded struct selinfo. It does not call seldrain() to wake the threads blocked on that selinfo before tearing the object down. The omission has been continuously in the tree since FreeBSD 9.0, released January 2012.\n\nCVE-2026-45251 is the consequence. Calif's audit found it on a first pass, by enumerating every selrecord() caller in the kernel and asking the same question of each one: is there a matching seldrain() on every final-free path of the owner? The audit was written by an AI. The exploits were written by an AI. The audit found fourteen other kernel bugs the same way. The bugs were the demo. The deliverable was the question.","post_id":533,"slug":"freebsd-procdesc-cve-2026-45251-fourteen-years-without-seldrain","title":"CVE-2026-45251: procdesc_free Has Been Missing seldrain Since 2012. The First Auditor To Ask Was An AI.","type":"initial","unreadable_sentence":"Every FreeBSD release between January 2012 and May 2026 shipped this one-line UAF. No human auditor reading sys_procdesc.c ever asked the question that found it."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCapXinAAKCRDeZjl4jgkQ JugEAP4kg63O5UqV687XSQvKRy8BBLGRuwp378BiJsMNZ9CBswD9E54+iotkK4F3 DW6y78HdDL2BuRj8qy4fIG7NvQfpaAo= =nqpD -----END PGP SIGNATURE-----