[Bug 300234] Dup does not use the lowest available descriptor
Date: Thu, 29 Oct 2026 08:45:03 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300234
Bug ID: 300234
Summary: Dup does not use the lowest available descriptor
Product: Base System
Version: CURRENT
Hardware: Any
OS: Any
Status: New
Severity: Affects Only Me
Priority: ---
Component: kern
Assignee: bugs@FreeBSD.org
Reporter: ml@netfence.it
Created attachment 276671
--> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=276671&action=edit
Fix fdcopy
Man page dup(2) clearly states:
The new descriptor returned by the call is the lowest numbered descriptor
currently not in use by the process.
On 15 (and I believe also in main) this is not always true.
For the curious, I found this out because one of the pratical effect is that
Cyrus IMAP fails to start automatically at boot in a jail. See:
https://mail-archive.freebsd.org/cgi/getmsg.cgi?fetch=124562+0+archive/2026/freebsd-ports/20261026.freebsd-ports
The problem seems to arise in function fdcopy.
This was changed in 14 -> 15 in order to skip duplication in some more cases;
alas, when that happens, it produces an incoherent filedesc structure.
All other functions assume such a structure is correct and never check, so
there is no recovery from this situation. The skipped number is forever lost.
I'm attaching a patch that seeems to solve.
In a test VM now Cyrus starts properly again.
However I'm no kernel expert, so I don't dare put it in production unless
someone with more knowledge than me reviews it.
While at it, I came to this conclusion by adding some KASSERTs around that
tested the content of filedesc structures: these are not in the patch (mainly
because I'm quite sure the way I added them was not orthodox). Would it be
worth to add them?
--
You are receiving this mail because:
You are the assignee for the bug.