Patch "bpf: sockmap, fix proto update hook to avoid dup calls" has been added to the 6.7-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    bpf: sockmap, fix proto update hook to avoid dup calls

to the 6.7-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     bpf-sockmap-fix-proto-update-hook-to-avoid-dup-calls.patch
and it can be found in the queue-6.7 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit a7ecb3c82627c38caf99f79a7c8ea36bd843bd19
Author: John Fastabend <john.fastabend@xxxxxxxxx>
Date:   Thu Dec 21 15:23:23 2023 -0800

    bpf: sockmap, fix proto update hook to avoid dup calls
    
    [ Upstream commit 16b2f264983dc264c1560cc0170e760dec1bf54f ]
    
    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: 8866730aed51 ("bpf, sockmap: af_unix stream sockets need to hold ref for pair sock")
    Reported-by: Xingwei Lee <xrivendell7@xxxxxxxxx>
    Signed-off-by: John Fastabend <john.fastabend@xxxxxxxxx>
    Signed-off-by: Martin KaFai Lau <martin.lau@xxxxxxxxxx>
    Reviewed-by: Jakub Sitnicki <jakub@xxxxxxxxxxxxxx>
    Link: https://lore.kernel.org/r/20231221232327.43678-2-john.fastabend@xxxxxxxxx
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/net/unix/unix_bpf.c b/net/unix/unix_bpf.c
index 7ea7c3a0d0d0..bd84785bf8d6 100644
--- a/net/unix/unix_bpf.c
+++ b/net/unix/unix_bpf.c
@@ -161,15 +161,30 @@ int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool r
 {
 	struct sock *sk_pair;
 
+	/* Restore does not decrement the sk_pair reference yet because we must
+	 * keep the a reference to the socket until after an RCU grace period
+	 * and any pending sends have completed.
+	 */
 	if (restore) {
 		sk->sk_write_space = psock->saved_write_space;
 		sock_replace_proto(sk, psock->sk_proto);
 		return 0;
 	}
 
-	sk_pair = unix_peer(sk);
-	sock_hold(sk_pair);
-	psock->sk_pair = sk_pair;
+	/* psock_update_sk_prot can be called multiple times if psock is
+	 * added to multiple maps and/or slots in the same map. There is
+	 * also an edge case where replacing a psock with itself can trigger
+	 * an extra psock_update_sk_prot during the insert process. So it
+	 * must be safe to do multiple calls. Here we need to ensure we don't
+	 * increment the refcnt through sock_hold many times. There will only
+	 * be a single matching destroy operation.
+	 */
+	if (!psock->sk_pair) {
+		sk_pair = unix_peer(sk);
+		sock_hold(sk_pair);
+		psock->sk_pair = sk_pair;
+	}
+
 	unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
 	sock_replace_proto(sk, &unix_stream_bpf_prot);
 	return 0;




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux