From patchwork Wed Feb 10 12:04:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12080757 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25FC2C433DB for ; Wed, 10 Feb 2021 12:15:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E530764E66 for ; Wed, 10 Feb 2021 12:15:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231566AbhBJMPD (ORCPT ); Wed, 10 Feb 2021 07:15:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231764AbhBJMMw (ORCPT ); Wed, 10 Feb 2021 07:12:52 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F42EC061A86 for ; Wed, 10 Feb 2021 04:04:42 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id f16so1607277wmq.5 for ; Wed, 10 Feb 2021 04:04:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OSkPPwU1Tuk4mhkI6x3HPNgww1cxj5Q62PGKwYGTKNY=; b=NLp2isIeKm3p7bKBYZ+D2qSKM0rJhJ/2WySCYUP3OlU0+xBqJTZLX3aQPLYZSl0V+I H8xodj6XFViiqc3kwBVfr8HkmXkbuRDrSn1RFjycwaIFkUpeE5EO7bTho83PSLVbfaP3 fm0GNWYRYietbSVI6OcmUJOUriEgJFYy4gHUg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OSkPPwU1Tuk4mhkI6x3HPNgww1cxj5Q62PGKwYGTKNY=; b=N08btsEhpDcO9alNI8+I6yhDyzpzZfXjkL7/2xhiSF3rKNZ2AR1WfMD970hu7B9MlS 15DfhWe35ncbooUB8hwNHJqmkLya1M2RNXJggHWSrdJsZEboJ+2hvGvoBTyaiWTA2i0V u5u/CArtu78/Em3cjPai2IG3ft7kv8DpKI98sDDALKxppr9St7uLoslN1+6mnlBTHIaJ FiMj6zlR+rEfvHYtAgjqF7NZEp+8stm82p6CVCF13u0xGvkE8ZZIN254BEEY+aheMFLf I90pZxw2aKJZiu3zQPMX6ptaN7vh4TriShpFskXEkWN5tyV4vifSQhkDO+Ot9mjKdL34 JrdQ== X-Gm-Message-State: AOAM532mRnBM9gtTLqEMMiMIpkXy0ptDfUNAQfzJeu1r9Y6r7p7vDVoR 6v8EkZXe/IgTCGHKdtO+zuGWIw== X-Google-Smtp-Source: ABdhPJz7fgGWTAVn5RiLUsMsBCacdSGGenk55/ibwhftoFqarBpfndNyw/4EicivNQWRIS6xxFeFjw== X-Received: by 2002:a1c:6487:: with SMTP id y129mr2680722wmb.106.1612958681086; Wed, 10 Feb 2021 04:04:41 -0800 (PST) Received: from antares.lan (c.3.c.9.d.d.c.e.0.a.6.8.a.9.e.c.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:ce9a:86a0:ecdd:9c3c]) by smtp.gmail.com with ESMTPSA id j7sm2837854wrp.72.2021.02.10.04.04.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:40 -0800 (PST) From: Lorenz Bauer To: Shuah Khan , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf 3/4] tools/testing: add test for NS_GET_COOKIE Date: Wed, 10 Feb 2021 12:04:24 +0000 Message-Id: <20210210120425.53438-4-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210210120425.53438-1-lmb@cloudflare.com> References: <20210210120425.53438-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Check that NS_GET_COOKIE returns a non-zero value, and that distinct network namespaces have different cookies. Signed-off-by: Lorenz Bauer --- tools/testing/selftests/nsfs/.gitignore | 1 + tools/testing/selftests/nsfs/Makefile | 2 +- tools/testing/selftests/nsfs/netns.c | 57 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/nsfs/netns.c diff --git a/tools/testing/selftests/nsfs/.gitignore b/tools/testing/selftests/nsfs/.gitignore index ed79ebdf286e..ca31b216215b 100644 --- a/tools/testing/selftests/nsfs/.gitignore +++ b/tools/testing/selftests/nsfs/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only owner pidns +netns diff --git a/tools/testing/selftests/nsfs/Makefile b/tools/testing/selftests/nsfs/Makefile index dd9bd50b7b93..93793cdb5a7c 100644 --- a/tools/testing/selftests/nsfs/Makefile +++ b/tools/testing/selftests/nsfs/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -TEST_GEN_PROGS := owner pidns +TEST_GEN_PROGS := owner pidns netns CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/nsfs/netns.c b/tools/testing/selftests/nsfs/netns.c new file mode 100644 index 000000000000..8ab862667b45 --- /dev/null +++ b/tools/testing/selftests/nsfs/netns.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#define NSIO 0xb7 +#define NS_GET_COOKIE _IO(NSIO, 0x5) + +#define pr_err(fmt, ...) \ + ({ \ + fprintf(stderr, "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + 1; \ + }) + +int main(int argc, char *argvp[]) +{ + uint64_t cookie1, cookie2; + char path[128]; + int ns; + + snprintf(path, sizeof(path), "/proc/%d/ns/net", getpid()); + ns = open(path, O_RDONLY); + if (ns < 0) + return pr_err("Unable to open %s", path); + + if (ioctl(ns, NS_GET_COOKIE, &cookie1)) + return pr_err("Unable to get first namespace cookie"); + + if (!cookie1) + return pr_err("NS_GET_COOKIE returned zero first cookie"); + + close(ns); + if (unshare(CLONE_NEWNET)) + return pr_err("unshare"); + + ns = open(path, O_RDONLY); + if (ns < 0) + return pr_err("Unable to open %s", path); + + if (ioctl(ns, NS_GET_COOKIE, &cookie2)) + return pr_err("Unable to get second namespace cookie"); + + if (!cookie2) + return pr_err("NS_GET_COOKIE returned zero second cookie"); + + if (cookie1 == cookie2) + return pr_err("NS_GET_COOKIE returned identical cookies for distinct ns"); + + close(ns); + return 0; +} From patchwork Wed Feb 10 12:04:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12080759 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E09E9C433E0 for ; Wed, 10 Feb 2021 12:15:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AAA2C64E31 for ; Wed, 10 Feb 2021 12:15:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231387AbhBJMPW (ORCPT ); Wed, 10 Feb 2021 07:15:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229789AbhBJMNL (ORCPT ); Wed, 10 Feb 2021 07:13:11 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0E2FC061D7C for ; Wed, 10 Feb 2021 04:04:43 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id b3so2205365wrj.5 for ; Wed, 10 Feb 2021 04:04:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ko2o/BbXoOQINYLWQy5SvIqF+PCLP66Gm4DbDrcbqNM=; b=INbfSkQg1MSBCfmNYdHCKyxLjYiYcwGcPh5Atqoum9f5NufD6C8b+vdGduKpIoe3K0 fmruz5F2fsNNtukmEbB+Zsx0Ywyswi5lPn1ecF7Q7zmfHWFUQoYD22Li6Yg6EUxhTvzj S3alZrS2RzHl+pogD/2sqMwxUbnPhRdizvm+8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ko2o/BbXoOQINYLWQy5SvIqF+PCLP66Gm4DbDrcbqNM=; b=a/vesvKg2qUqNoLrr9DHvJaeSUoKEvSoC2HggPbi5vXzSfAopkHNvxDZNU4J1uYOJq m1l3zL4bWszZ3fXZJ8SYGgxThoLk4JVwx2Xjr9f4tdC8m1LANdIbCwpRyZYRqRvlJ30p IiliA4Ccl7F3lGPxuE6FBIDeBsLMde+8DOlFUUDnk4mPCGEjVa8CGNuFToMneTyndFbV GJlF9sGpkrlk674FzSYWt7uJmhZ7MlgV618ktmFeJx6cYdsAjo1Y0vGjOr8rrDC9WXTz mbxcJUq8ZQOK9EBCBnD7zZP+12I5cfnWrL1MGJY/UbpSwu1o71aPyadITTwhMkYR/BeV PGZw== X-Gm-Message-State: AOAM5334VVYAh5a+cW7Njb1RQpLe5rto3mQ0oD46FXc2YYGcz+M8DyMS iu+z2+KcF8+MT3h1XShPL/4lfw== X-Google-Smtp-Source: ABdhPJylIINkWlSCZPDmbOuXA4zKfLnXjVImcpE9euugMX8Qa7mIZlbiIr60HYTkGaa1Uv4BQnMfvw== X-Received: by 2002:a05:6000:1189:: with SMTP id g9mr3287526wrx.230.1612958682501; Wed, 10 Feb 2021 04:04:42 -0800 (PST) Received: from antares.lan (c.3.c.9.d.d.c.e.0.a.6.8.a.9.e.c.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:ce9a:86a0:ecdd:9c3c]) by smtp.gmail.com with ESMTPSA id j7sm2837854wrp.72.2021.02.10.04.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:42 -0800 (PST) From: Lorenz Bauer To: "David S. Miller" , Jakub Kicinski , Shuah Khan , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf 4/4] tools/testing: add a selftest for SO_NETNS_COOKIE Date: Wed, 10 Feb 2021 12:04:25 +0000 Message-Id: <20210210120425.53438-5-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210210120425.53438-1-lmb@cloudflare.com> References: <20210210120425.53438-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Make sure that SO_NETNS_COOKIE returns a non-zero value, and that sockets from different namespaces have a distinct cookie value. Signed-off-by: Lorenz Bauer --- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/so_netns_cookie.c | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/net/so_netns_cookie.c diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore index 61ae899cfc17..19deb9cdf72f 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -30,3 +30,4 @@ hwtstamp_config rxtimestamp timestamping txtimestamp +so_netns_cookie diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index fa5fa425d148..a0f45a71a8f1 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -27,7 +27,7 @@ TEST_GEN_FILES = socket nettest TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy reuseport_addr_any TEST_GEN_FILES += tcp_mmap tcp_inq psock_snd txring_overwrite TEST_GEN_FILES += udpgso udpgso_bench_tx udpgso_bench_rx ip_defrag -TEST_GEN_FILES += so_txtime ipv6_flowlabel ipv6_flowlabel_mgr +TEST_GEN_FILES += so_txtime ipv6_flowlabel ipv6_flowlabel_mgr so_netns_cookie TEST_GEN_FILES += tcp_fastopen_backup_key TEST_GEN_FILES += fin_ack_lat TEST_GEN_FILES += reuseaddr_ports_exhausted diff --git a/tools/testing/selftests/net/so_netns_cookie.c b/tools/testing/selftests/net/so_netns_cookie.c new file mode 100644 index 000000000000..b39e87e967cd --- /dev/null +++ b/tools/testing/selftests/net/so_netns_cookie.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef SO_NETNS_COOKIE +#define SO_NETNS_COOKIE 71 +#endif + +#define pr_err(fmt, ...) \ + ({ \ + fprintf(stderr, "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + 1; \ + }) + +int main(int argc, char *argvp[]) +{ + uint64_t cookie1, cookie2; + socklen_t vallen; + int sock1, sock2; + + sock1 = socket(AF_INET, SOCK_STREAM, 0); + if (sock1 < 0) + return pr_err("Unable to create TCP socket"); + + vallen = sizeof(cookie1); + if (getsockopt(sock1, SOL_SOCKET, SO_NETNS_COOKIE, &cookie1, &vallen) != 0) + return pr_err("getsockopt(SOL_SOCKET, SO_NETNS_COOKIE)"); + + if (!cookie1) + return pr_err("SO_NETNS_COOKIE returned zero cookie"); + + if (unshare(CLONE_NEWNET)) + return pr_err("unshare"); + + sock2 = socket(AF_INET, SOCK_STREAM, 0); + if (sock2 < 0) + return pr_err("Unable to create TCP socket"); + + vallen = sizeof(cookie2); + if (getsockopt(sock2, SOL_SOCKET, SO_NETNS_COOKIE, &cookie2, &vallen) != 0) + return pr_err("getsockopt(SOL_SOCKET, SO_NETNS_COOKIE)"); + + if (!cookie2) + return pr_err("SO_NETNS_COOKIE returned zero cookie"); + + if (cookie1 == cookie2) + return pr_err("SO_NETNS_COOKIE returned identical cookies for distinct ns"); + + close(sock1); + close(sock2); + return 0; +}