🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
4 messages
4 total messages Started by Michal Luczaj Thu, 29 Oct 2026 21:39
Re: [PATCH net-next v4 4/7] vsock: add a netlink command to assign the g2h device to a netns
#8708
Author: Michal Luczaj
Date: Thu, 29 Oct 2026 21:39
148 lines
15264 bytes
On 10/22/26 12:47, Stefano Garzarella wrote:
>>>> +/* Move @vsk to TCP_ESTABLISHED and into the connected table, unless the device
>>>> + * has moved to a namespace @vsk cannot reach. Returns false without doing
>>>> + * either in that case.
>>>> + *
>>>> + * The reset sweep walks the same table under the same lock, so an assign
>>>> + * cannot land between the check and the insert: either the sweep finds @vsk
>>>> + * and closes it, or @vsk is never added.
>>>> + */
>>>> +bool vsock_maybe_set_connected(struct vsock_sock *vsk)
>>>> +{
>>>> +	struct list_head *list;
>>>> +	bool reachable;
>>>> +
>>>> +	list = vsock_connected_sockets(&vsk->remote_addr, &vsk->local_addr);
>>>> +
>>>> +	spin_lock_bh(&vsock_table_lock);
>>>> +	reachable = vsock_g2h_reachable_sk(vsk);
>>>> +	if (reachable) {
>>>> +		sk_vsock(vsk)->sk_state = TCP_ESTABLISHED;
>>>> +		__vsock_insert_connected(list, vsk);
>>>> +	}
>>>> +	spin_unlock_bh(&vsock_table_lock);
>>>> +
>>>> +	return reachable;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(vsock_maybe_set_connected);
>>>> +
>>>> +static bool vsock_g2h_should_reset(struct vsock_sock *vsk,
>>>> +				   const struct vsock_transport *t)
>>>> +{
>>>> +	return vsk->transport == t &&
>>>> +	       READ_ONCE(sk_vsock(vsk)->sk_state) != TCP_CLOSE &&
>>>> +	       !READ_ONCE(vsk->close_work_scheduled) &&
>>>> +	       !vsock_g2h_reachable_sk(vsk);
>>>> +}
>>>> +
>>>> +static struct vsock_sock *
>>>> +vsock_g2h_next_unreachable(const struct vsock_transport *t, unsigned int *i)
>>>> +{
>>>> +	struct vsock_sock *vsk;
>>>> +
>>>> +	spin_lock_bh(&vsock_table_lock);
>>>> +
>>>> +	for (; *i < ARRAY_SIZE(vsock_connected_table); (*i)++) {
>>>> +		list_for_each_entry(vsk, &vsock_connected_table[*i],
>>>> +				    connected_table) {
>>>> +			if (vsock_g2h_should_reset(vsk, t)) {
>>>> +				sock_hold(sk_vsock(vsk));
>>>> +				spin_unlock_bh(&vsock_table_lock);
>>>> +				return vsk;
>>>> +			}
>>>> +		}
>>>> +	}
>>>> +
>>>> +	spin_unlock_bh(&vsock_table_lock);
>>>> +
>>>> +	return NULL;
>>>> +}
>>>> +
>>>> +/* Close every connected socket of @t that can no longer reach the g2h device.
>>>> + */
>>>> +static void vsock_g2h_reset_unreachable(const struct vsock_transport *t)
>>>> +{
>>>> +	struct vsock_sock *vsk;
>>>> +	unsigned int i = 0;
>>>> +	struct sock *sk;
>>>> +	bool reset;
>>>> +
>>>> +	lockdep_assert_held(&vsock_g2h_assign_mutex);
>>>> +	lockdep_assert_not_held(&vsock_register_mutex);
>>>> +
>>>> +	while ((vsk = vsock_g2h_next_unreachable(t, &i))) {
>>>
>>> can we reuse vsock_for_each_connected_socket() ?
>>>
>>> If not, maybe a comment here will be nice and maybe I'll try to merge
>>> vsock_g2h_next_unreachable() here if it's not used somewhere else.
>>
>> I think we would need a variant that returns the socket, since we need
>> to be able to acquire the lock_sock after releasing the
>> vsock_table_lock.
> 
> That would be nice if it's not too complicated.
> 
>>
>> Maybe the callback could return a bool, if true then return socket
>> otherwise skip. Then the cb could also grab the sock_hold ref under the
>> table lock.
> 
> Yeah, looks promising to me!
> 
>>
>>>
>>> I find these functions a bit hard to read.
>>
>> Yeah its all kind of a pain. The problem is that we need to hold
>> sock_lock to set sk_state and sk_err, but can't acquire it under the
>> table lock. Instead, we grab the socket ref sock_hold under the table
>> lock to guarantee it stays alive between releasing the table lock and
>> grabbing the socket lock.
>>
>> I was really tempted to not do this lock_sock dance though. The race
>> condition I'm avoiding here is that if this reset races with an RST
>> receive, then the socket could end up with TCP_CLOSING + sk_err
>> ECONNRESET... but I'm not sure this actually propagates up into a real
>> problem for any potential user.

Right, vsock_connect() is fragile. Two things to be cautious about:
1. Don't let it reset the already connected socket. Because that would
allow user to "re-connect()" and make mess in connected_table.
2. Don't let it re-assign transport of an already connected socket. Because
that breaks the contract in few places.

State 1 is not an issue: vsock_g2h_reset_unreachable() calls
__vsock_remove_connected().

State 2 may be problematic: if vsock_g2h_reset_unreachable() sets TCP_CLOSE
when connect() is scheduled out, then after the wake-up sock may get reset
to SS_UNCONNECTED. Then user re-connect()s and hits the transport
re-assignment logic. I'm worried that, as we re-assign, someone may be
holding a reference to such "connected" (even if not in connected_table
anymore) vsock.

So I don't know, maybe it's vsock_connect() that should be fixed/hardened
instead?

Or at least disallow transport *re*-assignment? Does the "re-" part have a
use case?

Hope it's not too off-topic.

>> It also avoided some racing with the tx path, but now that send_pkt()
>> also checks reachability I think that path is safe.
>>
>> What do you think about doing the below hunk (TCP_CLOSE, ECONNRESET,
>> sk_error_report) with no lock_sock?
> 
> Looking at the code, it seems that in all the places where we close all 
> connections (virtio_vsock_reset_sock, vhost_vsock_reset_orphans) we do 
> with no lock_sock but with vsock_table_lock.
> 
> So, makes sense to me.

virtio_vsock_reset_sock() and vhost_vsock_reset_orphans() are my pet
peeves: state switcheroo without taking lock_sock(). Which, again, races
vsock_connect().
Re: [PATCH net-next v4 4/7] vsock: add a netlink command to assign the g2h device to a netns
#9425
Author: Michal Luczaj
Date: Mon, 02 Nov 2026 01:21
86 lines
14078 bytes
On 10/30/26 21:30, Bobby Eshleman wrote:
>>>> Yeah its all kind of a pain. The problem is that we need to hold
>>>> sock_lock to set sk_state and sk_err, but can't acquire it under the
>>>> table lock. Instead, we grab the socket ref sock_hold under the table
>>>> lock to guarantee it stays alive between releasing the table lock and
>>>> grabbing the socket lock.
>>>>
>>>> I was really tempted to not do this lock_sock dance though. The race
>>>> condition I'm avoiding here is that if this reset races with an RST
>>>> receive, then the socket could end up with TCP_CLOSING + sk_err
>>>> ECONNRESET... but I'm not sure this actually propagates up into a real
>>>> problem for any potential user.
>>
>> Right, vsock_connect() is fragile. Two things to be cautious about:
>> 1. Don't let it reset the already connected socket. Because that would
>> allow user to "re-connect()" and make mess in connected_table.
>> 2. Don't let it re-assign transport of an already connected socket. Because
>> that breaks the contract in few places.
>>
>> State 1 is not an issue: vsock_g2h_reset_unreachable() calls
>> __vsock_remove_connected().
>>
>> State 2 may be problematic: if vsock_g2h_reset_unreachable() sets TCP_CLOSE
>> when connect() is scheduled out, then after the wake-up sock may get reset
>> to SS_UNCONNECTED. Then user re-connect()s and hits the transport
>> re-assignment logic. I'm worried that, as we re-assign, someone may be
>> holding a reference to such "connected" (even if not in connected_table
>> anymore) vsock.
> 
> I see path you're looking at... after vsock_connect() gets scheduled
> back in we set TCP_CLOSE/SS_UNCONNECTED if sock_error returns non-zero.
> Could you elaborate on your last concern there about someone holding a
> reference? I'm not sure I fully understood.

Ah, sorry, let me explain. This case reminded me of the vsock's TCP_CLOSING
bug[1]: reporter mentioned vsk->trans getting freed when skb was still in
flight. I did not reproduce the exact scenario, but it did sound plausible
and I thought it applies here: virtio_transport_destruct() frees vsk->trans
the moment vsock_loopback_work() invokes virtio_transport_consume_skb_sent().

Now, to get there let's start with vsock_connect() setting SS_UNCONNECTED
(on a connected) socket after vsock_g2h_reset_unreachable(). Then user is
able to re-assign the transport, which involves
virtio_transport_destruct(). But since we're dealing with a connected
socket, (before the reset) some send() might have triggered
vsock_loopback_work(). Does this make sense to you?

Another thing is this feature of vsock_assign_transport(): in some
re-assign failure paths you may end up with vsk->transport = NULL. In the
past this caused issues with sockmap: see WARN_ON_ONCE in
vsock_bpf_recvmsg(). Problem here is that normally vsock gets removed from
sockmap on close(), but in our (reset+reassign) case close() is never invoked.

[1]: https://lore.kernel.org/netdev/anzT1fREOSyHT99k@v4bel/

>> So I don't know, maybe it's vsock_connect() that should be fixed/hardened
>> instead?
>>
>> Or at least disallow transport *re*-assignment? Does the "re-" part have a
>> use case?
>>
>> Hope it's not too off-topic.
>>
>>>> It also avoided some racing with the tx path, but now that send_pkt()
>>>> also checks reachability I think that path is safe.
>>>>
>>>> What do you think about doing the below hunk (TCP_CLOSE, ECONNRESET,
>>>> sk_error_report) with no lock_sock?
>>>
>>> Looking at the code, it seems that in all the places where we close all 
>>> connections (virtio_vsock_reset_sock, vhost_vsock_reset_orphans) we do 
>>> with no lock_sock but with vsock_table_lock.
>>>
>>> So, makes sense to me.
>>
>> virtio_vsock_reset_sock() and vhost_vsock_reset_orphans() are my pet
>> peeves: state switcheroo without taking lock_sock(). Which, again, races
>> vsock_connect().
> 
> True, I don't love that either, but I feel the impact is at least
> constrained by the fact that it only happens when the module or device
> is already going away.

Doesn't it happen on suspend as well? Is so, with a poorly timed connect()
things can go south on resume.
Re: [PATCH net-next v4 4/7] vsock: add a netlink command to assign the g2h device to a netns
#9597
Author: Michal Luczaj
Date: Mon, 02 Nov 2026 11:27
19 lines
10687 bytes
On 11/2/26 02:21, Michal Luczaj wrote:
> On 10/30/26 21:30, Bobby Eshleman wrote:
>>> virtio_vsock_reset_sock() and vhost_vsock_reset_orphans() are my pet
>>> peeves: state switcheroo without taking lock_sock(). Which, again, races
>>> vsock_connect().
>>
>> True, I don't love that either, but I feel the impact is at least
>> constrained by the fact that it only happens when the module or device
>> is already going away.
> 
> Doesn't it happen on suspend as well? Is so, with a poorly timed connect()
> things can go south on resume.

What I wrote doesn't really make sense, does it? vsock_connect() cannot
freeze midway, so SS_UNCONNECTED (due to virtio_vsock_reset_sock() flipping
state to TCP_CLOSE) can't be set. Sorry.

But it is still the case for bind/unbind, right?
Re: [PATCH net-next v4 4/7] vsock: add a netlink command to assign the g2h device to a netns
#10578
Author: Michal Luczaj
Date: Tue, 03 Nov 2026 15:57
102 lines
14286 bytes
On 11/2/26 20:43, Bobby Eshleman wrote:
> On Mon, Nov 02, 2026 at 02:21:27AM +0100, Michal Luczaj wrote:
>> On 10/30/26 21:30, Bobby Eshleman wrote:
>>>>>> Yeah its all kind of a pain. The problem is that we need to hold
>>>>>> sock_lock to set sk_state and sk_err, but can't acquire it under the
>>>>>> table lock. Instead, we grab the socket ref sock_hold under the table
>>>>>> lock to guarantee it stays alive between releasing the table lock and
>>>>>> grabbing the socket lock.
>>>>>>
>>>>>> I was really tempted to not do this lock_sock dance though. The race
>>>>>> condition I'm avoiding here is that if this reset races with an RST
>>>>>> receive, then the socket could end up with TCP_CLOSING + sk_err
>>>>>> ECONNRESET... but I'm not sure this actually propagates up into a real
>>>>>> problem for any potential user.
>>>>
>>>> Right, vsock_connect() is fragile. Two things to be cautious about:
>>>> 1. Don't let it reset the already connected socket. Because that would
>>>> allow user to "re-connect()" and make mess in connected_table.
>>>> 2. Don't let it re-assign transport of an already connected socket. Because
>>>> that breaks the contract in few places.
>>>>
>>>> State 1 is not an issue: vsock_g2h_reset_unreachable() calls
>>>> __vsock_remove_connected().
>>>>
>>>> State 2 may be problematic: if vsock_g2h_reset_unreachable() sets TCP_CLOSE
>>>> when connect() is scheduled out, then after the wake-up sock may get reset
>>>> to SS_UNCONNECTED. Then user re-connect()s and hits the transport
>>>> re-assignment logic. I'm worried that, as we re-assign, someone may be
>>>> holding a reference to such "connected" (even if not in connected_table
>>>> anymore) vsock.
>>>
>>> I see path you're looking at... after vsock_connect() gets scheduled
>>> back in we set TCP_CLOSE/SS_UNCONNECTED if sock_error returns non-zero.
>>> Could you elaborate on your last concern there about someone holding a
>>> reference? I'm not sure I fully understood.
>>
>> Ah, sorry, let me explain. This case reminded me of the vsock's TCP_CLOSING
>> bug[1]: reporter mentioned vsk->trans getting freed when skb was still in
>> flight. I did not reproduce the exact scenario, but it did sound plausible
>> and I thought it applies here: virtio_transport_destruct() frees vsk->trans
>> the moment vsock_loopback_work() invokes virtio_transport_consume_skb_sent().
>>
>> Now, to get there let's start with vsock_connect() setting SS_UNCONNECTED
>> (on a connected) socket after vsock_g2h_reset_unreachable(). Then user is
>> able to re-assign the transport, which involves
>> virtio_transport_destruct(). But since we're dealing with a connected
>> socket, (before the reset) some send() might have triggered
>> vsock_loopback_work(). Does this make sense to you?
> 
> Ah you're right. I think that your approach of making vsock_connect()
> more robust is probably the right way to go...
> 
> I think this is the new race...
> 
> 1) vsock_connect(cid=VMADDR_CID_HOST)
>    -> enqueue pkt
>    -> sleep
> 
> 2) rx worker receives OP_RESPONSE
>    ->  sets TCP_ESTABLISHED/SS_CONNECTED
> 
>    (vsock_connect() still asleep)
> 
> 2) sendmsg()
>    -> sees SS_CONNECTED
>    -> enqueues skb
> 
> 3) netns reset sets TCP_CLOSE and sk_err=ECONNRESET
> 
> 4) vsock_connect()
>    -> scheduled back in
>    -> sees sk_err != 0
>    -> sets SS_UNCONNECTED
> 
> 5) vsock_connect(CID=VMADDR_CID_LOCAL) call #2
>    -> sees SS_UNCONNECTED
>    -> frees old vsk->trans
> 
> 6) sendmsg's skb tx completes
>    -> tx worker dereferences vsk->trans for the UAF

Yes, that's exactly what I've meant.

> If connect() checks for SS_CONNECTING before setting SS_UNCONNECTED,
> then in step 4 sock->state stays as SS_CONNECTING and the second
> connect() call will hit -EISCONN instead of freeing vsk->trans.
> 
> Seems to work for a racing vsock_shutdown() too, where sock->state would
> become SS_DISCONNECTING, and the second connect() would hit -EINVAL
> instead of freeing the vsk->trans.
> 
> I guess the rule would be that vsock_connect() sets SS_CONNECTING before
> sleeping and only changes it to SS_UNCONNECTED after waking if and only
> if it is still SS_CONNECTING.
> 
> WDYT?

I think it's a great idea. Does seem to solve quite a lot.

thanks,
Michal
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads