Re: git: 6144315d1612 - main - unix: Fix a race between shutdown and listen
- In reply to: Gleb Smirnoff : "Re: git: 6144315d1612 - main - unix: Fix a race between shutdown and listen"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 18 Nov 2026 14:40:35 UTC
On Tue, Nov 17, 2026 at 06:14:12PM -0800, Gleb Smirnoff wrote: > 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. How? The exploitability arises from the type confusion that happens when a socket is converted to a listening socket. For instance, one exploit for the race fixed here is achieved by abusing the fact that the sol_sbrcv_timeo and sol_sbsnd_timeo fields cover a data socket's mbuf queue pointers. I'm sure some races existed before the union was added, but as far as I know they were benign. For instance, what happens if unp_dispose() is called on a listening socket, but there is no union? It would simply observe that the socket buffers are empty and harmlessly set SBS_CANTRCVMORE. > > 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. I think it's probably useful, but without more details I can't see how it would have helped here. Note that uipc_shutdown() dropped all locks in order to acquire the socket buffer I/O lock, and that's where the race arises.