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

From: Gleb Smirnoff <glebius_at_freebsd.org>
Date: Wed, 18 Nov 2026 02:14:12 UTC
On Tue, Nov 17, 2026 at 04:28:36PM -0500, Mark Johnston wrote:
M> > I wonder if this could be avoided if we had pr_lock method and more streamlined
M> > operation of listen(2).  Maybe revisit https://reviews.freebsd.org/D48709 ?
M> 
M> Really a lot of these problems come from the fact that we started
M> overlaying listening socket fields over top of regular socket buffer
M> fields, as an optimization to shrink struct socket.  struct socket
M> effectively represents two different types.  Then, since listen(2) can
M> be called at any time, any code which handles sockets has to be very
M> careful to make sure the socket type can't flip.  We've had more than
M> one bug due to this.
M> 
M> pr_lock might make the synchronization rules a bit simpler, but the
M> underlying problem is the potential for type confusion between data
M> sockets and listening sockets.  Even with simpler locking rules, it's
M> too easy to miss an SOLISTENING() check somewhere.  So probably the
M> distinction between listening sockets and data sockets needs to be
M> reflected in the type system, e.g., have a separate DTYPE_LSOCKET file
M> type and struct lsocket type, and make listen() convert an existing data
M> socket to that type.  Fail if the underlying file structure is
M> concurrently referenced by a different thread, or if it can't be
M> converted atomically with respect to fget() lookups.  I'm not sure how
M> challenging that would be to implement.

I was thinking of switching dtype, but found out it it impossible to do
it safely without stopping all threads.

I agree that lot's of places that need a check for a socket being
listening were missed in the initial version.  My fault.

However, I don't think the overlaying has anything to do with a race
between listen(2) and other syscalls.  The union just makes a crash
a more probable result of a race than a misbehavior.  The races were
here before the union, and I'd guess some even could be exploitable.

So, back to D48709.  I think it is worth giving the generic socket
layer a method to tell the protocol "please lock/unlock the pcb".  Not
only listen(2) may use it.

-- 
Gleb Smirnoff