Skip to content

Commit 38207a5

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Attach map progs to psock early for feature probes
When a TCP socket is added to a sock map we look at the programs attached to the map to determine what proto op hooks need to be changed. Before the patch in the 'fixes' tag there were only two categories -- the empty set of programs or a TX policy. In any case the base set handled the receive case. After the fix we have an optimized program for receive that closes a small, but possible, race on receive. This program is loaded only when the map the psock is being added to includes a RX policy. Otherwise, the race is not possible so we don't need to handle the race condition. In order for the call to sk_psock_init() to correctly evaluate the above conditions all progs need to be set in the psock before the call. However, in the current code this is not the case. We end up evaluating the requirements on the old prog state. If your psock is attached to multiple maps -- for example a tx map and rx map -- then the second update would pull in the correct maps. But, the other pattern with a single rx enabled map the correct receive hooks are not used. The result is the race fixed by the patch in the fixes tag below may still be seen in this case. To fix we simply set all psock->progs before doing the call into sock_map_init(). With this the init() call gets the full list of programs and chooses the correct proto ops on the first iteration instead of requiring the second update to pull them in. This fixes the race case when only a single map is used. 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 f45b297 commit 38207a5

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

net/core/sock_map.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
282282

283283
if (msg_parser)
284284
psock_set_prog(&psock->progs.msg_parser, msg_parser);
285+
if (stream_parser)
286+
psock_set_prog(&psock->progs.stream_parser, stream_parser);
287+
if (stream_verdict)
288+
psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
289+
if (skb_verdict)
290+
psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
285291

286292
ret = sock_map_init_proto(sk, psock);
287293
if (ret < 0)
@@ -292,14 +298,10 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
292298
ret = sk_psock_init_strp(sk, psock);
293299
if (ret)
294300
goto out_unlock_drop;
295-
psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
296-
psock_set_prog(&psock->progs.stream_parser, stream_parser);
297301
sk_psock_start_strp(sk, psock);
298302
} else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
299-
psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
300303
sk_psock_start_verdict(sk,psock);
301304
} else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
302-
psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
303305
sk_psock_start_verdict(sk, psock);
304306
}
305307
write_unlock_bh(&sk->sk_callback_lock);

0 commit comments

Comments
 (0)