Skip to content

Commit 16b2f26

Browse files
jrfastabMartin KaFai Lau
authored andcommitted
bpf: sockmap, fix proto update hook to avoid dup calls
When sockets are added to a sockmap or sockhash we allocate and init a psock. Then update the proto ops with sock_map_init_proto the flow is sock_hash_update_common sock_map_link psock = sock_map_psock_get_checked() <-returns existing psock sock_map_init_proto(sk, psock) <- updates sk_proto If the socket is already in a map this results in the sock_map_init_proto being called multiple times on the same socket. We do this because when a socket is added to multiple maps this might result in a new set of BPF programs being attached to the socket requiring an updated ops struct. This creates a rule where it must be safe to call psock_update_sk_prot multiple times. When we added a fix for UAF through unix sockets in patch 4dd9a38a753fc we broke this rule by adding a sock_hold in that path to ensure the sock is not released. The result is if a af_unix stream sock is placed in multiple maps it results in a memory leak because we call sock_hold multiple times with only a single sock_put on it. Fixes: 8866730 ("bpf, sockmap: af_unix stream sockets need to hold ref for pair sock") Reported-by: Xingwei Lee <[email protected]> Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent b456005 commit 16b2f26

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

net/unix/unix_bpf.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,30 @@ int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool r
161161
{
162162
struct sock *sk_pair;
163163

164+
/* Restore does not decrement the sk_pair reference yet because we must
165+
* keep the a reference to the socket until after an RCU grace period
166+
* and any pending sends have completed.
167+
*/
164168
if (restore) {
165169
sk->sk_write_space = psock->saved_write_space;
166170
sock_replace_proto(sk, psock->sk_proto);
167171
return 0;
168172
}
169173

170-
sk_pair = unix_peer(sk);
171-
sock_hold(sk_pair);
172-
psock->sk_pair = sk_pair;
174+
/* psock_update_sk_prot can be called multiple times if psock is
175+
* added to multiple maps and/or slots in the same map. There is
176+
* also an edge case where replacing a psock with itself can trigger
177+
* an extra psock_update_sk_prot during the insert process. So it
178+
* must be safe to do multiple calls. Here we need to ensure we don't
179+
* increment the refcnt through sock_hold many times. There will only
180+
* be a single matching destroy operation.
181+
*/
182+
if (!psock->sk_pair) {
183+
sk_pair = unix_peer(sk);
184+
sock_hold(sk_pair);
185+
psock->sk_pair = sk_pair;
186+
}
187+
173188
unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
174189
sock_replace_proto(sk, &unix_stream_bpf_prot);
175190
return 0;

0 commit comments

Comments
 (0)