Skip to content

Commit c0d95d3

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Re-evaluate proto ops when psock is removed from sockmap
When a sock is added to a sock map we evaluate what proto op hooks need to be used. However, when the program is removed from the sock map we have not been evaluating if that changes the required program layout. Before the patch listed in the 'fixes' tag this was not causing failures because the base program set handles all cases. Specifically, the case with a stream parser and the case with out a stream parser are both handled. With the fix below we identified a race when running with a proto op that attempts to read skbs off both the stream parser and the skb->receive_queue. Namely, that a race existed where when the stream parser is empty checking the skb->receive_queue from recvmsg at the precies moment when the parser is paused and the receive_queue is not empty could result in skipping the stream parser. This may break a RX policy depending on the parser to run. The fix tag then loads a specific proto ops that resolved this race. But, we missed removing that proto ops recv hook when the sock is removed from the sockmap. The result is the stream parser is stopped so no more skbs will be aggregated there, but the hook and BPF program continues to be attached on the psock. User space will then get an EBUSY when trying to read the socket because the recvmsg() handler is now waiting on a stopped stream parser. To fix we rerun the proto ops init() function which will look at the new set of progs attached to the psock and rest the proto ops hook to the correct handlers. And in the above case where we remove the sock from the sock map the RX prog will no longer be listed so the proto ops is removed. Fixes: c5d2177 ("bpf, sockmap: Fix race in ingress receive verdict with redirect to self") Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 38207a5 commit c0d95d3

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

net/core/skmsg.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
11241124

11251125
void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
11261126
{
1127+
psock_set_prog(&psock->progs.stream_parser, NULL);
1128+
11271129
if (!psock->saved_data_ready)
11281130
return;
11291131

@@ -1212,6 +1214,9 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)
12121214

12131215
void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)
12141216
{
1217+
psock_set_prog(&psock->progs.stream_verdict, NULL);
1218+
psock_set_prog(&psock->progs.skb_verdict, NULL);
1219+
12151220
if (!psock->saved_data_ready)
12161221
return;
12171222

net/core/sock_map.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ static void sock_map_del_link(struct sock *sk,
167167
write_lock_bh(&sk->sk_callback_lock);
168168
if (strp_stop)
169169
sk_psock_stop_strp(sk, psock);
170-
else
170+
if (verdict_stop)
171171
sk_psock_stop_verdict(sk, psock);
172+
173+
if (psock->psock_update_sk_prot)
174+
psock->psock_update_sk_prot(sk, psock, false);
172175
write_unlock_bh(&sk->sk_callback_lock);
173176
}
174177
}

0 commit comments

Comments
 (0)