Re: git: 6144315d1612 - main - unix: Fix a race between shutdown and listen

From: Mark Johnston <markj_at_freebsd.org>
Date: Tue, 17 Nov 2026 21:28:36 UTC
On Tue, Nov 17, 2026 at 12:20:22PM -0800, Gleb Smirnoff wrote:
> On Tue, Nov 17, 2026 at 05:01:49PM +0000, Mark Johnston wrote:
> M> The branch main has been updated by markj:
> M> 
> M> URL: https://cgit.FreeBSD.org/src/commit/?id=6144315d1612bc0c3a7ffd22e75aa1fe06dee1ca
> M> 
> M> commit 6144315d1612bc0c3a7ffd22e75aa1fe06dee1ca
> M> Author:     Mark Johnston <markj@FreeBSD.org>
> M> AuthorDate: 2026-11-09 22:27:45 +0000
> M> Commit:     Mark Johnston <markj@FreeBSD.org>
> M> CommitDate: 2026-11-17 16:57:01 +0000
> M> 
> M>     unix: Fix a race between shutdown and listen
> M>     
> M>     In uipc_shutdown(), the SOLISTENING() check is stale as soon as the
> M>     socket lock is dropped.  Acquire the unpcb lock earlier and use that to
> M>     interlock with other work that touches the socket buffers which are
> M>     invalid for a listening socket.  Fix unp_dispose() to handle errors when
> M>     acquiring the I/O lock; contrary to the assertions, such races are
> M>     possible.
> M>     
> M>     Approved by:    so
> M>     Security:       FreeBSD-SA-26:83.unix
> M>     Security:       CVE-2026-127751
> M>     Reported by:    OpenAI Security Research Team
> M>     Sponsored by:   The FreeBSD Foundation
> 
> I wonder if this could be avoided if we had pr_lock method and more streamlined
> operation of listen(2).  Maybe revisit https://reviews.freebsd.org/D48709 ?

Really a lot of these problems come from the fact that we started
overlaying listening socket fields over top of regular socket buffer
fields, as an optimization to shrink struct socket.  struct socket
effectively represents two different types.  Then, since listen(2) can
be called at any time, any code which handles sockets has to be very
careful to make sure the socket type can't flip.  We've had more than
one bug due to this.

pr_lock might make the synchronization rules a bit simpler, but the
underlying problem is the potential for type confusion between data
sockets and listening sockets.  Even with simpler locking rules, it's
too easy to miss an SOLISTENING() check somewhere.  So probably the
distinction between listening sockets and data sockets needs to be
reflected in the type system, e.g., have a separate DTYPE_LSOCKET file
type and struct lsocket type, and make listen() convert an existing data
socket to that type.  Fail if the underlying file structure is
concurrently referenced by a different thread, or if it can't be
converted atomically with respect to fget() lookups.  I'm not sure how
challenging that would be to implement.