From patchwork Thu Dec 21 23:23:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13502815 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CEA679941; Thu, 21 Dec 2023 23:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hWLVZvK2" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1d3eae5c1d7so9513675ad.2; Thu, 21 Dec 2023 15:23:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703201011; x=1703805811; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tYMrRNT3ZdfPIRMKKtuwipMRSvkNVNrUffEPckkXHwY=; b=hWLVZvK2ButB838RFi0PyTN+KlxaTfq3OrhxkEQ3c2MyqdbgAM/GguunGXqDpI/tdY uF5z51kEqpgXRgMAlgaTYLnRyTZMEMPpCBEdJrGESxRRjZupTPOyzJzWBGXUgrJvZRlr s9QyL4CYOCyILRzUeOgk9vaXORrIGJpZjp5uCYmhtNNl9jWFYoZl7h3DGXSgQLEDggzK YKLWlPMxYVomrIOCgSJR9oW6dJshmUZG7PdkogtItwEh2DVVUqDy7McLRKh8m/mxTeGa CyNAHeC6yQOlWWkpa4yzIJvJ49rH47IpRQ7JVek+LjW28z9xt2tXmzjAtGLJ6AxcLo9E RiGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703201011; x=1703805811; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tYMrRNT3ZdfPIRMKKtuwipMRSvkNVNrUffEPckkXHwY=; b=HAMdokSIoHWx5dTpzRhW6kVBw1VGUiu0LPzhRa9Fctwd1l2bZDV0/WT0jqDhg5pT4L ar2sqFR6I1Mh3hKrRmiY2blaudUlQb/EXwssSIG4Zdw3Rx4+L5uMxLBZgnyg3Vi/S8Yk dMcYf01KNWjBsW2kKIouhkYRoFEG9M6XPPv+deBcMrLJUyL4bWHNaelf5iuWQ6JCJopB Cu1l4UX228v4pUsMpQTBjT6NyANBRoq/3u2tKuJ9AdESpNuQu12xPJvMDlXphzKQD4ST tC6dXIc2TshRAslVAeJm2FuxVNeHI4J34WiTvWxvqhurozLY91cRDvWivf2tAtmXqGkn FdKg== X-Gm-Message-State: AOJu0Yw6umT5g7xg6N4pfRnypizvd/V2zjMnfV4Xtbt/VbQNLs+SxBI4 OJo9RDVM/0/UXajWaGigxB1MUB7mCZo= X-Google-Smtp-Source: AGHT+IHvrzHlxxO27n2Ek17hhVD/oGI8djQfznT41AbNXW84b1KJw2qiboCZZmUdPELRcwZUb+RqKg== X-Received: by 2002:a17:902:c94d:b0:1d0:c7e0:c826 with SMTP id i13-20020a170902c94d00b001d0c7e0c826mr353888pla.17.1703201011564; Thu, 21 Dec 2023 15:23:31 -0800 (PST) Received: from john.rmac-pubwifi.localzone (fw.royalmoore.com. [72.21.11.210]) by smtp.gmail.com with ESMTPSA id g15-20020a1709029f8f00b001d3e33a73d5sm2139641plq.279.2023.12.21.15.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 15:23:30 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, rivendell7@gmail.com, kuniyu@amazon.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf 1/5] bpf: sockmap, fix proto update hook to avoid dup calls Date: Thu, 21 Dec 2023 15:23:23 -0800 Message-Id: <20231221232327.43678-2-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20231221232327.43678-1-john.fastabend@gmail.com> References: <20231221232327.43678-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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: 4dd9a38a753fc ("bpf: sockmap, fix proto update hook to avoid dup calls") Rebported-by: Xingwei Lee Signed-off-by: John Fastabend --- net/unix/unix_bpf.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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; From patchwork Thu Dec 21 23:23:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13502816 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42FEA7995A; Thu, 21 Dec 2023 23:23:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="i+vjpzfC" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1d4006b2566so9711025ad.0; Thu, 21 Dec 2023 15:23:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703201013; x=1703805813; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iIDhSXU72ufOCSNS4Jhi/nn4DUZAV20AxMwH9mUdxFQ=; b=i+vjpzfCl16g3jwFc8TMnPNiW9dXDFT0DbzmwAO3agR+s4e8RdNfb4P/I1Oy+ZBe8a NoSlHNOGfBN/MAFZJrsfdJHz5QHJvl86CvtqkFdN9J5lDHiKFa9Y7QkqkpA+9i87SCHB D/fOGSqKZaPN9UCyS5adIxP6pAy1AbMPQ2tPH2cKRv4hrvTIE+Fc7ZlaEUsNQ6Kid3gp kXLRBIvWxK72fjQ14hZpGKO8SjA98JcoR58fk/xlGDoIByf8ToQbjgJOEOr4BVNgYnqU 7pQDMhOYnRtRyoFwRKRTZ6u9aWkrW+34kIADfG0kbsAAk6as88HnWvqGc8AV6xZNrz7A Qt6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703201013; x=1703805813; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iIDhSXU72ufOCSNS4Jhi/nn4DUZAV20AxMwH9mUdxFQ=; b=JSALOg0QCSbnRGR2bjBYZJ5E133UF/XLOwIiz+BvYmk6lO34UPBFcE96BOJv2DkJT1 2dzlX+KreHUo6Q4FmerfsG0Lto+IULj/Aojd9DZyagpHJZGqxlX+af2DfblDBbLvCGj7 HkVTi46q1BE2E3kd/zm0YfCLlnldoMm0vtOANxHvpSGNKh+/e4diSBp2BmqFZIVNP8Aq LGyIGar59oGpvdqzURLR5mkq2BQUhXybBVfk30VcKiYx/2xHflUtX3YUMtCA1R1UgrHp RHyEXNPpw84NVO5P0zFErkeklHK9djQ/hSGIFYVCW7T59M4fdcByj/68kTbbUol4MeKH azKQ== X-Gm-Message-State: AOJu0YwB1b9qP05cDOXaPsirGuMU0XmeKECLe/VRzsujA/TABnc+MMY4 YNatB0RCF/izykzpXYfUq4o= X-Google-Smtp-Source: AGHT+IHSpwqwpGl18k2ksrteewYySCIroa1U7wGmZ0lM/9C31F0QYYjG3RQvGJPmuDuKuyLrL4H4wg== X-Received: by 2002:a17:902:da8b:b0:1d3:f43a:a2d4 with SMTP id j11-20020a170902da8b00b001d3f43aa2d4mr423431plx.114.1703201013469; Thu, 21 Dec 2023 15:23:33 -0800 (PST) Received: from john.rmac-pubwifi.localzone (fw.royalmoore.com. [72.21.11.210]) by smtp.gmail.com with ESMTPSA id g15-20020a1709029f8f00b001d3e33a73d5sm2139641plq.279.2023.12.21.15.23.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 15:23:32 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, rivendell7@gmail.com, kuniyu@amazon.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf 2/5] bpf: sockmap, added comments describing update proto rules Date: Thu, 21 Dec 2023 15:23:24 -0800 Message-Id: <20231221232327.43678-3-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20231221232327.43678-1-john.fastabend@gmail.com> References: <20231221232327.43678-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add a comment describing that the psock update proto callbback can be called multiple times and this must be safe. Signed-off-by: John Fastabend --- include/linux/skmsg.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index c953b8c0d2f4..888a4b217829 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -100,6 +100,11 @@ struct sk_psock { void (*saved_close)(struct sock *sk, long timeout); void (*saved_write_space)(struct sock *sk); void (*saved_data_ready)(struct sock *sk); + /* psock_update_sk_prot may be called with restore=false many times + * so the handler must be safe for this case. It will be called + * exactly once with restore=true when the psock is being destroyed + * and psock refcnt is zero, but before an RCU grace period. + */ int (*psock_update_sk_prot)(struct sock *sk, struct sk_psock *psock, bool restore); struct proto *sk_proto; From patchwork Thu Dec 21 23:23:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13502817 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B5AC7AE72; Thu, 21 Dec 2023 23:23:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZqFQAGTm" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1d40eec5e12so6966875ad.1; Thu, 21 Dec 2023 15:23:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703201015; x=1703805815; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CnCWdeXTPDOxyHWiRsUb8ioOTz8NlNC6gPY1DELcE5o=; b=ZqFQAGTm0BkcGElF5fWd4htbffs0lV17U+I8GXrxd3KVrP7+VO3kPEAjpEvgMLYcDd sotxjC+UXSVoA0LSXFp/PLQpM7FG3kZRAaUy9UZxzH5iSkMuThKIaOPwBGDEK3WeRmAM /qzpwCawatd9K29qQIpyQEfyuvSJ/VtGNsilB0GNyfdbYcuKV/VQhNm8TMpmJtqkJyPU oyEp6hzQOwP3LbeifrRt0UYc9cuG55imzXhW3Xg5jticI6gA2cOzwwgPKblWxmW6YcGe u/Kuy7UK4u111q5WK6qGwA71uZDz5k0HbBiDqrWMRUJn7wDyT8NUxpfhroUPTeUxWVIF 4KyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703201015; x=1703805815; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CnCWdeXTPDOxyHWiRsUb8ioOTz8NlNC6gPY1DELcE5o=; b=aFzy3vyCSnoG1d6fBMvate7jIEkaFAesxELEcc+X/pHkGCcB0lRVBCIIDJu0cJaTNL ybjgRPoS5jXkawr4jkFM/3NuLMoXozRMXZ9lm79iUX5i23A4n6GYavMGzhRfUxaXz+kh MKZzojCsC+vY+3jduMHNddsVUh9sjvzOMd+FQwRad+2dV8K+NM6LKhDtwgTElJn8+iD5 AUktB5cRhiRmY6O6xAp8rcUoErTz6YnK+VOTZ5af9gswT1hJlZTNYAQzdCa5o7zgRJyd Q4FKN+zoB5Q6HnYBNL+lfG6rWQwQoV0tdBQ6KM4iyrnQbDvtI8afyUjs8yIKmY6h56Ee QbaQ== X-Gm-Message-State: AOJu0YxNPoaYcy1LatRQqzBN5+AOT3ncF2PYZ4dq4XHmxM5ZQ1tLPNjf q/qF4D/nlXnw4ZlZiYJQf0k= X-Google-Smtp-Source: AGHT+IHNFIbODp+7ob/S/stMEfSWPBelG2bJF/iIm/bzf1wUSi0QDffq8aBxqeJ/UOCh5oKOKFmtoQ== X-Received: by 2002:a17:902:eb8b:b0:1d3:aa36:386 with SMTP id q11-20020a170902eb8b00b001d3aa360386mr393622plg.70.1703201015417; Thu, 21 Dec 2023 15:23:35 -0800 (PST) Received: from john.rmac-pubwifi.localzone (fw.royalmoore.com. [72.21.11.210]) by smtp.gmail.com with ESMTPSA id g15-20020a1709029f8f00b001d3e33a73d5sm2139641plq.279.2023.12.21.15.23.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 15:23:34 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, rivendell7@gmail.com, kuniyu@amazon.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf 3/5] bpf: sockmap, add tests for proto updates many to single map Date: Thu, 21 Dec 2023 15:23:25 -0800 Message-Id: <20231221232327.43678-4-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20231221232327.43678-1-john.fastabend@gmail.com> References: <20231221232327.43678-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add test with a single map where each socket is inserted multiple times. Test protocols: TCP, UDP, stream af_unix and dgram af_unix. Signed-off-by: John Fastabend --- .../selftests/bpf/prog_tests/sockmap_basic.c | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c index 7c2241fae19a..22240eeb798b 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c @@ -555,6 +555,69 @@ static void test_sockmap_unconnected_unix(void) close(dgram); } +static void test_sockmap_many_socket(void) +{ + struct test_sockmap_pass_prog *skel; + int stream[2], dgram, udp, tcp; + int i, err, map, entry = 0; + + skel = test_sockmap_pass_prog__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + map = bpf_map__fd(skel->maps.sock_map_rx); + + dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0); + if (dgram < 0) + return; + + tcp = connected_socket_v4(); + if (!ASSERT_GE(tcp, 0, "connected_socket_v4")) { + close(dgram); + return; + } + + udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); + if (udp < 0) { + close(dgram); + close(tcp); + return; + } + + err = socketpair(AF_UNIX, SOCK_STREAM, 0, stream); + ASSERT_OK(err, "socketpair(af_unix, sock_stream)"); + if (err) + goto out; + + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map, &entry, &stream[0], BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(stream)"); + } + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map, &entry, &dgram, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(dgram)"); + } + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map, &entry, &udp, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(udp)"); + } + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map, &entry, &tcp, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(tcp)"); + } + for (entry--; entry >= 0; entry--) { + err = bpf_map_delete_elem(map, &entry); + ASSERT_OK(err, "bpf_map_delete_elem(entry)"); + } + + close(stream[0]); + close(stream[1]); +out: + close(dgram); + close(tcp); + close(udp); +} + void test_sockmap_basic(void) { if (test__start_subtest("sockmap create_update_free")) @@ -597,7 +660,8 @@ void test_sockmap_basic(void) test_sockmap_skb_verdict_fionread(false); if (test__start_subtest("sockmap skb_verdict msg_f_peek")) test_sockmap_skb_verdict_peek(); - if (test__start_subtest("sockmap unconnected af_unix")) test_sockmap_unconnected_unix(); + if (test__start_subtest("sockmap one socket to many map entries")) + test_sockmap_many_socket(); } From patchwork Thu Dec 21 23:23:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13502818 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A51A7997C; Thu, 21 Dec 2023 23:23:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZhiHVE7c" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1d3eae5c1d7so9513815ad.2; Thu, 21 Dec 2023 15:23:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703201017; x=1703805817; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wmCEPynfpBGFyS6GGX7437i0lA+GuTcbygWqF7dFGkQ=; b=ZhiHVE7c0x+bsyf3NSE04xq0pozD8BwAkeoXBNd+t6/XKFPnF7WS8eXYUMsAELNn22 hG4ndWBpX7TecWDEDx5Skx6AsbX3qW3lHH15QY3KiHBnAUVRLHneRT6cAwIP2NQfDM5S SqLVQ36mm4TYsTfiTMSvWhbLcwyiEbrgEPx0im3W1qR4dmaqTWES5n6nLUQaSxrKwwLr 4259nGcIEKB+8uFgnCzGqXru/dfC1j/Y0CxTp6AVa7QY7uGE+PMKIXcOUaMRX5Kz5g6P k5GPqTBGaw4HXaSSfhmQK0uC5Q1yZTk7Lz+DGaMkHwLSwKRRXPBCLMNkYeuX7lTf10yr 6KoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703201017; x=1703805817; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wmCEPynfpBGFyS6GGX7437i0lA+GuTcbygWqF7dFGkQ=; b=arMz1YtOBsK9HWKV4CP2jWOuSqN8Zo8Hzj0XOXXeX8MgoCx9HcGC5nnilEv4FUE56e Cq4/FOUAPmy9Zb/kvvnrViMcVykpiWcqFT5BT/jO+SovLlfYCgKN2eK3EWVF0ZyZ6atQ DNf2lyDIOdHEaHIlLDLFGFTnDZiZJot8l5KgQBm4MfMculY0BMEw47CIS9K0PIMccElX N5QQpDiFQCoVtgZsAST/j5pK0nk6etRzOHyVI3cIDlKjgfJWXYIMbgURuUuMqc6RUIoU ZcBgFBh0ooecaHQpeIJ7WNHK9XtOjUy12bbIDAcAd8Z2s6BNCAriI2nqobRikqoJe3lv Dugw== X-Gm-Message-State: AOJu0YzsmK3hQNwx5tGTi3p554BVSLI2d6dd4xL+5kWt6NzoL6P/7KX6 yHevSFQdVjSHXJXZUYRf8gljOIZZZLE= X-Google-Smtp-Source: AGHT+IF55bEhutOJVNWIwUXoYi8nRd2yxsnP648y+tnqSf0MsiK99PedaKGXW8sWh8AVjKUHeToCUg== X-Received: by 2002:a17:902:b94c:b0:1d0:711b:c926 with SMTP id h12-20020a170902b94c00b001d0711bc926mr347252pls.110.1703201017421; Thu, 21 Dec 2023 15:23:37 -0800 (PST) Received: from john.rmac-pubwifi.localzone (fw.royalmoore.com. [72.21.11.210]) by smtp.gmail.com with ESMTPSA id g15-20020a1709029f8f00b001d3e33a73d5sm2139641plq.279.2023.12.21.15.23.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 15:23:35 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, rivendell7@gmail.com, kuniyu@amazon.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf 4/5] bpf: sockmap, add tests for proto updates single socket to many map Date: Thu, 21 Dec 2023 15:23:26 -0800 Message-Id: <20231221232327.43678-5-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20231221232327.43678-1-john.fastabend@gmail.com> References: <20231221232327.43678-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add test with multiple maps where each socket is inserted in multiple maps. Test protocols: TCP, UDP, stream af_unix and dgram af_unix. Signed-off-by: John Fastabend --- .../selftests/bpf/prog_tests/sockmap_basic.c | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c index 22240eeb798b..337c92cfb4aa 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c @@ -618,6 +618,73 @@ static void test_sockmap_many_socket(void) close(udp); } +static void test_sockmap_many_maps(void) +{ + struct test_sockmap_pass_prog *skel; + int stream[2], dgram, udp, tcp; + int i, err, map[2], entry = 0; + + skel = test_sockmap_pass_prog__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + map[0] = bpf_map__fd(skel->maps.sock_map_rx); + map[1] = bpf_map__fd(skel->maps.sock_map_tx); + + dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0); + if (dgram < 0) + return; + + tcp = connected_socket_v4(); + if (!ASSERT_GE(tcp, 0, "connected_socket_v4")) { + close(dgram); + return; + } + + udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); + if (udp < 0) { + close(dgram); + close(tcp); + return; + } + + err = socketpair(AF_UNIX, SOCK_STREAM, 0, stream); + ASSERT_OK(err, "socketpair(af_unix, sock_stream)"); + if (err) + goto out; + + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map[i], &entry, &stream[0], BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(stream)"); + } + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map[i], &entry, &dgram, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(dgram)"); + } + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map[i], &entry, &udp, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(udp)"); + } + for (i = 0; i < 2; i++, entry++) { + err = bpf_map_update_elem(map[i], &entry, &tcp, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(tcp)"); + } + for (entry--; entry >= 0; entry--) { + err = bpf_map_delete_elem(map[1], &entry); + entry--; + ASSERT_OK(err, "bpf_map_delete_elem(entry)"); + err = bpf_map_delete_elem(map[0], &entry); + ASSERT_OK(err, "bpf_map_delete_elem(entry)"); + } + + close(stream[0]); + close(stream[1]); +out: + close(dgram); + close(tcp); + close(udp); +} + void test_sockmap_basic(void) { if (test__start_subtest("sockmap create_update_free")) @@ -664,4 +731,6 @@ void test_sockmap_basic(void) test_sockmap_unconnected_unix(); if (test__start_subtest("sockmap one socket to many map entries")) test_sockmap_many_socket(); + if (test__start_subtest("sockmap one socket to many maps")) + test_sockmap_many_maps(); } From patchwork Thu Dec 21 23:23:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13502819 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5000E7AE9A; Thu, 21 Dec 2023 23:23:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bbKzr+UC" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1d3f2985425so8483585ad.3; Thu, 21 Dec 2023 15:23:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703201020; x=1703805820; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f+x0ahGfIuY9ZBZ2nRz7NtYxAUGL6RGhLBHqlN87RGY=; b=bbKzr+UCn2i6CiPqtCHUVDjdIhCYNm3c1FOfvI1FD/TMFT+U4T45yhH9od58peo/1e vw/FM/5j6DG4dN768bQGmzwsQpglCnfNPzhtPlx1wp77AYwLrHD5slqSlOHP5cEbtsIM PcvnkjmlUjCnDr4C6TXslXPSJU6cI7srt9+NMjl2H+RLolXuV5sDpvOTmKawfo00Bkvq fNzqzQcJGw1ZvnwjqJqFaDqjVlO74nrcgf0bcbj9W5qMjPTdagrfrdPjDgbJp0OHHkEZ dTYyqv8CZbwi/t0D4wm+NWak3I85B/qZSHcvQe3+Le5+UylAm6jfYxwNU4jYKdqiO+Jk H3VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703201020; x=1703805820; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f+x0ahGfIuY9ZBZ2nRz7NtYxAUGL6RGhLBHqlN87RGY=; b=oOwGqw/B4JkuoMK/XgN1Syk0KuEtaKff465I+IUx1cdO502cGJNPX5WNS/RkgVIwbn CBl+C1DHMhGy6pH8Cw2u09v1H/RRou36pD+11iz39IGnUkqnZ4qQR2szx3b/6Kfg+Dy4 d7XRMIlzRzWFbILqQuWptMaFnZhDqiM3ZvydkMOVcHE2fWoiQJBhrAEfJ0ALV1QKm4Yu o3y9icaG+W5uq502m4JWCyxsz44v31zwvbDk5jEYqZAYYXS+d8Hz26vekoBuEez1QgYF /Zf2fro14QL6PfXH3vRRl9VEuInfzf5wyHixk61gW7kTA5RD9+DAmzJInnhUpdiM5gP5 aYTw== X-Gm-Message-State: AOJu0YyoTpHZZQvP0B6twGsQWlDcstpu8XZkz+aFjTUxHx42xX/q2ZYR FQY6kOegvEPdgyB4oXisGSQZt9wW90U= X-Google-Smtp-Source: AGHT+IGtSsxUk7FY/DqMi/dyyaORaLvb96RcRn+3K/EWhkI/Z5vZL9XHtohlnt7ke4173HXsgKIwRw== X-Received: by 2002:a17:902:ec82:b0:1d0:8f0d:b6e4 with SMTP id x2-20020a170902ec8200b001d08f0db6e4mr396354plg.111.1703201019523; Thu, 21 Dec 2023 15:23:39 -0800 (PST) Received: from john.rmac-pubwifi.localzone (fw.royalmoore.com. [72.21.11.210]) by smtp.gmail.com with ESMTPSA id g15-20020a1709029f8f00b001d3e33a73d5sm2139641plq.279.2023.12.21.15.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 15:23:37 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, rivendell7@gmail.com, kuniyu@amazon.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf 5/5] bpf: sockmap, add tests for proto updates replace socket Date: Thu, 21 Dec 2023 15:23:27 -0800 Message-Id: <20231221232327.43678-6-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20231221232327.43678-1-john.fastabend@gmail.com> References: <20231221232327.43678-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add test that replaces the same socket with itself. This exercises a corner case where old element and new element have the same posck. Test protocols: TCP, UDP, stream af_unix and dgram af_unix. Signed-off-by: John Fastabend --- .../selftests/bpf/prog_tests/sockmap_basic.c | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c index 337c92cfb4aa..b5eb287912d1 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c @@ -685,6 +685,68 @@ static void test_sockmap_many_maps(void) close(udp); } +static void test_sockmap_same_sock(void) +{ + struct test_sockmap_pass_prog *skel; + int stream[2], dgram, udp, tcp; + int i, err, map, zero = 0; + + skel = test_sockmap_pass_prog__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + map = bpf_map__fd(skel->maps.sock_map_rx); + + dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0); + if (dgram < 0) + return; + + tcp = connected_socket_v4(); + if (!ASSERT_GE(tcp, 0, "connected_socket_v4")) { + close(dgram); + return; + } + + udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); + if (udp < 0) { + close(dgram); + close(tcp); + return; + } + + err = socketpair(AF_UNIX, SOCK_STREAM, 0, stream); + ASSERT_OK(err, "socketpair(af_unix, sock_stream)"); + if (err) + goto out; + + for (i = 0; i < 2; i++) { + err = bpf_map_update_elem(map, &zero, &stream[0], BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(stream)"); + } + for (i = 0; i < 2; i++) { + err = bpf_map_update_elem(map, &zero, &dgram, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(dgram)"); + } + for (i = 0; i < 2; i++) { + err = bpf_map_update_elem(map, &zero, &udp, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(udp)"); + } + for (i = 0; i < 2; i++) { + err = bpf_map_update_elem(map, &zero, &tcp, BPF_ANY); + ASSERT_OK(err, "bpf_map_update_elem(tcp)"); + } + + err = bpf_map_delete_elem(map, &zero); + ASSERT_OK(err, "bpf_map_delete_elem(entry)"); + + close(stream[0]); + close(stream[1]); +out: + close(dgram); + close(tcp); + close(udp); +} + void test_sockmap_basic(void) { if (test__start_subtest("sockmap create_update_free")) @@ -733,4 +795,6 @@ void test_sockmap_basic(void) test_sockmap_many_socket(); if (test__start_subtest("sockmap one socket to many maps")) test_sockmap_many_maps(); + if (test__start_subtest("sockmap same socket replace")) + test_sockmap_same_sock(); }