From patchwork Wed Feb 10 12:04:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12080763 X-Patchwork-Delegate: bpf@iogearbox.net 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 B1346C4332D for ; Wed, 10 Feb 2021 12:13:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8583864E3E for ; Wed, 10 Feb 2021 12:13:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231492AbhBJMMi (ORCPT ); Wed, 10 Feb 2021 07:12:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231659AbhBJMK2 (ORCPT ); Wed, 10 Feb 2021 07:10:28 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8609C061A28 for ; Wed, 10 Feb 2021 04:04:39 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id u14so1622640wmq.4 for ; Wed, 10 Feb 2021 04:04:39 -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=7YlFkcwEzuK8xj1bqWFb4J+pbM9jNy7GJX0y3deB8As=; b=syPwiZlFCfO6MHc40aH33LzcMM6KfDmzRv+D2pLSH5QhqhfJeAI8pcSdpGXIuefrhT QANrLqzWqVKdKDRMI9LkGu7/nHBiH+rC1ieNTWfFHSmkspO5VBaxfKVXb2X2Squ8dPwc gqbcZAWKlW0ioCr6UjiF2eRNBO1Q7kMUruhv0= 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=7YlFkcwEzuK8xj1bqWFb4J+pbM9jNy7GJX0y3deB8As=; b=rah/IZYeAjpqqmvatYADnTsqdYUsYO5aOxd75k2Kitu40HqzQZ25i/4NLuPZoDLgyQ GXy5rT0ukZPRl+zLH9/z26MVUpwjB/zvXD9fKOeBNltbKou9QR+y9qeNgOyqe1YTayuK vUbDaJqVTD2wfrMSJQ/huNtS882V9xYfBB15cSNhk5JDntqCmfHOSvNOIegyeVt6YlZ2 NmSlPgD6XCr2xH6ZvTsSiVRt0F62HJ3tGG7XEidiocZL7uMncyxVfzndjCcPt3JPcMZo GUAi3wfuTe63AQhFPsXN+edux6Q0MWIibne49+8ee5weHfGz4TGmDxtYDAUG5JNPdlfA kgnQ== X-Gm-Message-State: AOAM530ws3R2ZmXVwFgex/+TrdMB1SWd7IRUOtmJSXksgQXVtb8WPduE qcc5frqC9f0ToX47wfYC+NT5ew== X-Google-Smtp-Source: ABdhPJzWH8Tw+178GsaKwHlgz0XiwPg9erze0gT5N1I/sfwQGjRhxr9XRlOCiSD9NdzdTDxtpCscUA== X-Received: by 2002:a1c:f70c:: with SMTP id v12mr2614677wmh.77.1612958678358; Wed, 10 Feb 2021 04:04:38 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:38 -0800 (PST) From: Lorenz Bauer To: Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Jakub Kicinski Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-api@vger.kernel.org, linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf 1/4] net: add SO_NETNS_COOKIE socket option Date: Wed, 10 Feb 2021 12:04:22 +0000 Message-Id: <20210210120425.53438-2-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: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net We need to distinguish which network namespace a socket belongs to. BPF has the useful bpf_get_netns_cookie helper for this, but accessing it from user space isn't possible. Add a read-only socket option that returns the netns cookie, similar to SO_COOKIE. If network namespaces are disabled, SO_NETNS_COOKIE returns the cookie of init_net. The BPF helpers change slightly: instead of returning 0 when network namespaces are disabled we return the init_net cookie as for the socket option. Cc: linux-api@vger.kernel.org Signed-off-by: Lorenz Bauer --- arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ include/linux/sock_diag.h | 20 ++++++++++++++++++++ include/uapi/asm-generic/socket.h | 2 ++ net/core/filter.c | 9 ++++----- net/core/sock.c | 7 +++++++ 8 files changed, 41 insertions(+), 5 deletions(-) diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h index 57420356ce4c..6b3daba60987 100644 --- a/arch/alpha/include/uapi/asm/socket.h +++ b/arch/alpha/include/uapi/asm/socket.h @@ -127,6 +127,8 @@ #define SO_PREFER_BUSY_POLL 69 #define SO_BUSY_POLL_BUDGET 70 +#define SO_NETNS_COOKIE 71 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h index 2d949969313b..cdf404a831b2 100644 --- a/arch/mips/include/uapi/asm/socket.h +++ b/arch/mips/include/uapi/asm/socket.h @@ -138,6 +138,8 @@ #define SO_PREFER_BUSY_POLL 69 #define SO_BUSY_POLL_BUDGET 70 +#define SO_NETNS_COOKIE 71 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h index f60904329bbc..5b5351cdcb33 100644 --- a/arch/parisc/include/uapi/asm/socket.h +++ b/arch/parisc/include/uapi/asm/socket.h @@ -119,6 +119,8 @@ #define SO_PREFER_BUSY_POLL 0x4043 #define SO_BUSY_POLL_BUDGET 0x4044 +#define SO_NETNS_COOKIE 0x4045 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h index 848a22fbac20..ff79db753dce 100644 --- a/arch/sparc/include/uapi/asm/socket.h +++ b/arch/sparc/include/uapi/asm/socket.h @@ -120,6 +120,8 @@ #define SO_PREFER_BUSY_POLL 0x0048 #define SO_BUSY_POLL_BUDGET 0x0049 +#define SO_NETNS_COOKIE 0x004a + #if !defined(__KERNEL__) diff --git a/include/linux/sock_diag.h b/include/linux/sock_diag.h index 0b9ecd8cf979..6e88436097b1 100644 --- a/include/linux/sock_diag.h +++ b/include/linux/sock_diag.h @@ -38,6 +38,26 @@ static inline u64 sock_gen_cookie(struct sock *sk) return cookie; } +static inline u64 __sock_gen_netns_cookie(struct sock *sk) +{ +#ifdef CONFIG_NET_NS + return __net_gen_cookie(sk->sk_net.net); +#else + return __net_gen_cookie(&init_net); +#endif +} + +static inline u64 sock_gen_netns_cookie(struct sock *sk) +{ + u64 cookie; + + preempt_disable(); + cookie = __sock_gen_netns_cookie(sk); + preempt_enable(); + + return cookie; +} + int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie); void sock_diag_save_cookie(struct sock *sk, __u32 *cookie); diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index 4dcd13d097a9..d588c244ec2f 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -122,6 +122,8 @@ #define SO_PREFER_BUSY_POLL 69 #define SO_BUSY_POLL_BUDGET 70 +#define SO_NETNS_COOKIE 71 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__)) diff --git a/net/core/filter.c b/net/core/filter.c index e15d4741719a..51f47b6913f1 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4645,11 +4645,10 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = { static u64 __bpf_get_netns_cookie(struct sock *sk) { -#ifdef CONFIG_NET_NS - return __net_gen_cookie(sk ? sk->sk_net.net : &init_net); -#else - return 0; -#endif + if (sk) + return __sock_gen_netns_cookie(sk); + + return __net_gen_cookie(&init_net); } BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) diff --git a/net/core/sock.c b/net/core/sock.c index bbcd4b97eddd..2db201c210ca 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1606,6 +1606,13 @@ int sock_getsockopt(struct socket *sock, int level, int optname, v.val = sk->sk_bound_dev_if; break; + case SO_NETNS_COOKIE: + lv = sizeof(u64); + if (len < lv) + return -EINVAL; + v.val64 = sock_gen_netns_cookie(sk); + break; + default: /* We implement the SO_SNDLOWAT etc to not be settable * (1003.1g 7). From patchwork Wed Feb 10 12:04:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12080765 X-Patchwork-Delegate: bpf@iogearbox.net 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=-16.0 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, UNWANTED_LANGUAGE_BODY,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 7DC4EC4332D for ; Wed, 10 Feb 2021 12:13:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5B69964E2A for ; Wed, 10 Feb 2021 12:13:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230302AbhBJMNS (ORCPT ); Wed, 10 Feb 2021 07:13:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231256AbhBJMLC (ORCPT ); Wed, 10 Feb 2021 07:11:02 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2654C061A2D for ; Wed, 10 Feb 2021 04:04:40 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id v15so2210414wrx.4 for ; Wed, 10 Feb 2021 04:04:40 -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=qMWtdr/D/b55YVpH+AH+ThithGxQzKx+2a1eg5+/BUc=; b=x7fU89uX9eU/xJfahZiU/Ny8AdzUWeHHw+gFzpzgqIOauzU3Jq6Er6V0wmk2XSukN0 Bzqvom9K69BfvyB72sT7xlEPLFRin4iZvsXgkt8cYupgLjBUw143n7qRX55CVHxYsmTP 5dLTsw9fQ9lSZJb9gNKAtxDPwKWn9BpKOCrhg= 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=qMWtdr/D/b55YVpH+AH+ThithGxQzKx+2a1eg5+/BUc=; b=HuEA+FTok1BlCFlmHLUjlW3Ytc/GUn/5eVBIHULXwnS8yYQmHle/qLSAuYNMb8UIFS PFOJtm32qS3eWpe2m80qupyO6LZD/pbMwDq6Q1bidmjYY0yMTjhPQJYMiCvXfrl9oUfH dJ6o7+9fChj5eZZ134/vSLPqYYkdSXYvXBCd7h4gbjkRPvAGZCvdTS5sdR3o1dYZDMOM khKLAvCoKuUkwW4Svpy8APne7uTruX8dhEwIwj6ZrEsOQnitC9wigNyzEqrb+iUFVqxd gH/YdufsUNmM2WP3UTMF8dvhhl/4cG8K8uFwv8cfDsw4+T+TIT2aWGfA7HfqTBe7FJmJ IIRQ== X-Gm-Message-State: AOAM533wDDAf0SDdU8NAkGb6xbuwY4SuncNr7XaDeZ2sbF1BjsMCA6Db LPdcHqktJFsCCVwhQES1W9wFpA== X-Google-Smtp-Source: ABdhPJy7hn6DuyMJ5KdhWZVNZqUeriQtKlVgZ6WCm5DKOJwRRp+IhOVht7MCUa5UsZ3yuzCtjf9H1Q== X-Received: by 2002:a5d:5549:: with SMTP id g9mr3483473wrw.244.1612958679629; Wed, 10 Feb 2021 04:04:39 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:39 -0800 (PST) From: Lorenz Bauer To: Alexander Viro , "David S. Miller" , Jakub Kicinski , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf 2/4] nsfs: add an ioctl to discover the network namespace cookie Date: Wed, 10 Feb 2021 12:04:23 +0000 Message-Id: <20210210120425.53438-3-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: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Network namespaces have a globally unique non-zero identifier aka a cookie, in line with socket cookies. Add an ioctl to retrieve the cookie from user space without going via BPF. Cc: linux-api@vger.kernel.org Signed-off-by: Lorenz Bauer Reported-by: kernel test robot Reported-by: kernel test robot --- fs/nsfs.c | 9 +++++++++ include/net/net_namespace.h | 11 +++++++++++ include/uapi/linux/nsfs.h | 2 ++ 3 files changed, 22 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index 800c1d0eb0d0..d7865e39c049 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "internal.h" @@ -191,6 +192,8 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, struct user_namespace *user_ns; struct ns_common *ns = get_proc_ns(file_inode(filp)); uid_t __user *argp; + struct net *net_ns; + u64 cookie; uid_t uid; switch (ioctl) { @@ -209,6 +212,12 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid = from_kuid_munged(current_user_ns(), user_ns->owner); return put_user(uid, argp); + case NS_GET_COOKIE: + if (ns->ops->type != CLONE_NEWNET) + return -EINVAL; + net_ns = container_of(ns, struct net, ns); + cookie = net_gen_cookie(net_ns); + return put_user(cookie, (u64 __user *)arg); default: return -ENOTTY; } diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index 29567875f428..bbd22dfa9345 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -226,6 +226,17 @@ struct net *get_net_ns_by_fd(int fd); u64 __net_gen_cookie(struct net *net); +static inline u64 net_gen_cookie(struct net *net) +{ + u64 cookie; + + preempt_disable(); + cookie = __net_gen_cookie(net); + preempt_enable(); + + return cookie; +} + #ifdef CONFIG_SYSCTL void ipx_register_sysctl(void); void ipx_unregister_sysctl(void); diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h index a0c8552b64ee..86611c2cf908 100644 --- a/include/uapi/linux/nsfs.h +++ b/include/uapi/linux/nsfs.h @@ -15,5 +15,7 @@ #define NS_GET_NSTYPE _IO(NSIO, 0x3) /* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) +/* Returns a unique non-zero identifier for a network namespace */ +#define NS_GET_COOKIE _IO(NSIO, 0x5) #endif /* __LINUX_NSFS_H */ 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: 12080767 X-Patchwork-Delegate: bpf@iogearbox.net 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=ham 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 37521C43381 for ; Wed, 10 Feb 2021 12:13:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0D14B64E31 for ; Wed, 10 Feb 2021 12:13:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231398AbhBJMNW (ORCPT ); Wed, 10 Feb 2021 07:13:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231562AbhBJMLQ (ORCPT ); Wed, 10 Feb 2021 07:11:16 -0500 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D808C061A31 for ; Wed, 10 Feb 2021 04:04:42 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id 190so1621323wmz.0 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=S7p85HW0jskrh46+wDOpcMMJJ28mA9bGoktv/L13tXHGXwjjm0n+CXdeTv3h3axXR6 0vxxqhTyOwMBdrFyoxmURqcdUnPihEWFCH0iWVqrxx00wNhTrxaglVv2NMo/I6IbCFTp NEQirJlyXbeMQgEMfZPPcPu0pCRVAfAfVSo7Ywwidvg0HE9u5s0DbWflhADtIoYkta3q hXQhG4He2ARnF/d3AhICikoy630HygUafyTLRj68tApGFF+y+ysjWo8dGddwGQdyJlVB Ltn2kjia9IFQzI9v2H2uOd7vUrXJKS4mqm2IIu7A0KOBhWnknBd8eudHmcGpKF6vke3V mW9Q== X-Gm-Message-State: AOAM532IUSKtaOiMPhq6Z+KKRinaXyfCnwB/ePMPXfIeCAa4L1YHBwOq hmF5NYAzcLDoAndEwjHygSZQVQ== 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: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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: 12080769 X-Patchwork-Delegate: bpf@iogearbox.net 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=ham 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 3C81BC433DB 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 DAE9C64E2A for ; Wed, 10 Feb 2021 12:15:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231793AbhBJMPN (ORCPT ); Wed, 10 Feb 2021 07:15:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231774AbhBJMNB (ORCPT ); Wed, 10 Feb 2021 07:13:01 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF2BDC061B3F for ; Wed, 10 Feb 2021 04:04:43 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id l12so2230155wry.2 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=lPsLvbrrtJ+70X0Gv7NvdQQ4FMlRvP2wJk8gtf4kbCOjTaEgV3e3MNnnAWU/bWj0ZN jcPNMcMCuGVTk/u8DAGtGp70WE6+Uo/e5mOSMmfAApMOu1ZdKyoebZfOAY0srjId7BcN vKywI6VDq5Bz4YIIp79N/wCS5UbdPj0Zw4gTvjxN8ucXGHhv3eLp0QlsGQglKj/+63p/ fKiC7LdhsLoj3WhT5L6x7cHVllX9NTTXnHL4z++yNw+fR/qJri6Yw5oz7PpCZeB9/AaG 259YbBUE4+nzk71vr7TRH6NWtjlCB6R4X0VhDSBp27CBMO4KtX/yrJWRxy/QF2cOFWTF tqeQ== X-Gm-Message-State: AOAM533m68u00N6/GpTcqWIQXiA6yfXpo8crZXtqKEk039O2lTy+Dmwv v23c52itN6RekCwun+C33Jxb0Q== 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: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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; +}