Skip to content

Commit 218d747

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Fix double bpf_prog_put on error case in map_link
sock_map_link() is called to update a sockmap entry with a sk. But, if the sock_map_init_proto() call fails then we return an error to the map_update op against the sockmap. In the error path though we need to cleanup psock and dec the refcnt on any programs associated with the map, because we refcnt them early in the update process to ensure they are pinned for the psock. (This avoids a race where user deletes programs while also updating the map with new socks.) In current code we do the prog refcnt dec explicitely by calling bpf_prog_put() when the program was found in the map. But, after commit '38207a5e81230' in this error path we've already done the prog to psock assignment so the programs have a reference from the psock as well. This then causes the psock tear down logic, invoked by sk_psock_put() in the error path, to similarly call bpf_prog_put on the programs there. To be explicit this logic does the prog->psock assignment: if (msg_*) psock_set_prog(...) Then the error path under the out_progs label does a similar check and dec with: if (msg_*) bpf_prog_put(...) And the teardown logic sk_psock_put() does ... psock_set_prog(msg_*, NULL) ... triggering another bpf_prog_put(...). Then KASAN gives us this splat, found by syzbot because we've created an inbalance between bpf_prog_inc and bpf_prog_put calling put twice on the program. BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_put kernel/bpf/syscall.c:1812 [inline] BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_put kernel/bpf/syscall.c:1812 [inline] kernel/bpf/syscall.c:1829 BUG: KASAN: vmalloc-out-of-bounds in bpf_prog_put+0x8c/0x4f0 kernel/bpf/syscall.c:1829 kernel/bpf/syscall.c:1829 Read of size 8 at addr ffffc90000e76038 by task syz-executor020/3641 To fix clean up error path so it doesn't try to do the bpf_prog_put in the error path once progs are assigned then it relies on the normal psock tear down logic to do complete cleanup. For completness we also cover the case whereh sk_psock_init_strp() fails, but this is not expected because it indicates an incorrect socket type and should be caught earlier. Fixes: 38207a5 ("bpf, sockmap: Attach map progs to psock early for feature probes") Reported-by: [email protected] Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5b2c554 commit 218d747

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

net/core/sock_map.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,23 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
292292
if (skb_verdict)
293293
psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
294294

295+
/* msg_* and stream_* programs references tracked in psock after this
296+
* point. Reference dec and cleanup will occur through psock destructor
297+
*/
295298
ret = sock_map_init_proto(sk, psock);
296-
if (ret < 0)
297-
goto out_drop;
299+
if (ret < 0) {
300+
sk_psock_put(sk, psock);
301+
goto out;
302+
}
298303

299304
write_lock_bh(&sk->sk_callback_lock);
300305
if (stream_parser && stream_verdict && !psock->saved_data_ready) {
301306
ret = sk_psock_init_strp(sk, psock);
302-
if (ret)
303-
goto out_unlock_drop;
307+
if (ret) {
308+
write_unlock_bh(&sk->sk_callback_lock);
309+
sk_psock_put(sk, psock);
310+
goto out;
311+
}
304312
sk_psock_start_strp(sk, psock);
305313
} else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
306314
sk_psock_start_verdict(sk,psock);
@@ -309,10 +317,6 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
309317
}
310318
write_unlock_bh(&sk->sk_callback_lock);
311319
return 0;
312-
out_unlock_drop:
313-
write_unlock_bh(&sk->sk_callback_lock);
314-
out_drop:
315-
sk_psock_put(sk, psock);
316320
out_progs:
317321
if (skb_verdict)
318322
bpf_prog_put(skb_verdict);
@@ -325,6 +329,7 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
325329
out_put_stream_verdict:
326330
if (stream_verdict)
327331
bpf_prog_put(stream_verdict);
332+
out:
328333
return ret;
329334
}
330335

0 commit comments

Comments
 (0)