From patchwork Fri Feb 19 09:51:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 385547 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 3D201C433DB for ; Fri, 19 Feb 2021 09:53:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1E5E64EC0 for ; Fri, 19 Feb 2021 09:53:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230183AbhBSJxH (ORCPT ); Fri, 19 Feb 2021 04:53:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbhBSJxF (ORCPT ); Fri, 19 Feb 2021 04:53:05 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E63ECC061786 for ; Fri, 19 Feb 2021 01:52:14 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id f7so6136055wrt.12 for ; Fri, 19 Feb 2021 01:52:14 -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=7GJI5hJf7kClUGYltrQmcJtEGXTgG/I5Sto2S8X228I=; b=wEphN6x4pP0T0M0aunICcoa1NJ2lyNjVwrrLEsac1MPXRd3Rw6B6AsdDvRBqYuGH97 /591AJdoA0OQYdedSNxjLtoh5HTg5C+GZGu8b37sqQWZoWnq6CoypBpvs+P+fCfEVEj2 kqRCTUq3ftgbbHr3QkjzlXSGbp0WKShDNHgz4= 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=7GJI5hJf7kClUGYltrQmcJtEGXTgG/I5Sto2S8X228I=; b=Mu0LRYVlx93XP1298kf271rpwDukl34MDbXdnw1RxCRzcUYikkFXhwYF6L+od3+2Fe +Aq7pYfO4WUdYwszfDPLbB+UuiIHP7j9E0pvWCOjRNl1XPScsP41gAzeUhqZnYKynvAp D+OLHFtVod5Hfbib6bqyRYMhCvGuv8uwRrNNT7dZOJXsZyHuwpkWUHG1qUkmXH2/eX1S qTg4upfrzBLnD3InX3UKVYac8WO6EyLTE2ruJUx93zuuV4mQp3UdBj8gZUAAC96Ey7JW Yz8pqG9PJtOvonPQGbPtz2R21fgkxXXNEyj6yhkjxmXnry9e4xS1NA20p/JIf67eVDN0 Zq/w== X-Gm-Message-State: AOAM5305/SrXqNT3Mc/RFP58bxYY5dwgCEhhp7NhpJrHEBoFGnGdUQ8R ClIqr4B1nvRDzYV2KGeXNx6UTA== X-Google-Smtp-Source: ABdhPJyCttIQw2Whak4S1eQFBwJwesciW5s24GgbIAH99N2WPmw62T4SYecu5R2KDth5F9034ZAYag== X-Received: by 2002:adf:fc48:: with SMTP id e8mr8479338wrs.154.1613728333570; Fri, 19 Feb 2021 01:52:13 -0800 (PST) Received: from antares.lan (b.3.5.8.9.a.e.c.e.a.6.2.c.1.9.b.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:b91c:26ae:cea9:853b]) by smtp.gmail.com with ESMTPSA id a21sm13174910wmb.5.2021.02.19.01.52.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Feb 2021 01:52:13 -0800 (PST) From: Lorenz Bauer To: daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next v2 1/4] net: add SO_NETNS_COOKIE socket option Date: Fri, 19 Feb 2021 09:51:46 +0000 Message-Id: <20210219095149.50346-2-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210219095149.50346-1-lmb@cloudflare.com> References: <20210219095149.50346-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. 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/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 11 +++++++++++ 6 files changed, 21 insertions(+) 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/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/sock.c b/net/core/sock.c index 0ed98f20448a..de4644aeb58d 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1614,6 +1614,17 @@ 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; +#ifdef CONFIG_NET_NS + v.val64 = sock_net(sk)->net_cookie; +#else + v.val64 = init_net.net_cookie; +#endif + break; + default: /* We implement the SO_SNDLOWAT etc to not be settable * (1003.1g 7). From patchwork Fri Feb 19 09:51:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 385546 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=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 1C415C433E0 for ; Fri, 19 Feb 2021 09:53:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D325364EC0 for ; Fri, 19 Feb 2021 09:53:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230194AbhBSJxi (ORCPT ); Fri, 19 Feb 2021 04:53:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230117AbhBSJxT (ORCPT ); Fri, 19 Feb 2021 04:53:19 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAB8BC06178A for ; Fri, 19 Feb 2021 01:52:15 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id l17so6456310wmq.2 for ; Fri, 19 Feb 2021 01:52:15 -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=sEgT8c4oZnUxglFmZCEU+sDrUl3YaIldCLk4AxLlgKg=; b=fnPyns3lbkPFdMhBxQ2qbAZtgWqeBcQShhGC1X/uOnDBBIl17kF2dBJO5dKLwrHUg+ HROEiqPQB9eIC+TvA3f11T58VD7kvIdenFU+taAl0BSOv3pt2egK7trj8GBYS5DDy5N/ 3G/qpMMLvOIqM3ukKW0qaaISeDHFsIP9rivaM= 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=sEgT8c4oZnUxglFmZCEU+sDrUl3YaIldCLk4AxLlgKg=; b=nthIv7DObkBcszMZBm3kMH6EZeAKAbgwSZPAM/vUMZFI7j6xRiEXJ6aGMVGEiJr1Vy 4enrwmpf9JsmljoliWEOJ6oQTu6M5NplYa1hKbJwd8JJnQS87X8RrzxgQI520fO+/Y9h w/4qKQjH4ifQ/aPd/V1/2LozI0nRl47aJ2EgDtHcmaPuW7WNuh5tYLiKhzzWCD9vTZoj FQlG8MmpMrRKGBFiKK+hQd/8kYuothgY2U7sCERyiDQ6xLDm8U7YM1jTSNidNBWF67Pu Tz1e44fSDE+q6TF/LL5uQ2+OTgH0Um18oFbOnTeMtB0C8cipdcsHwbUCQHCVoPpWYNX9 24wg== X-Gm-Message-State: AOAM532HO5ZPbchBRhRYPu8W50htLTUDoF089ws8Hi4np9z3iXXz+7Bv EWRkLB3PFonwBx1w7WVy6GloKA== X-Google-Smtp-Source: ABdhPJx81zveBGfFzqcue+zDv5fRej1c7E5XmkxDnLS8Hig1VlCDAL5D+SVN67ultnWErC/IYYUZRQ== X-Received: by 2002:a1c:c3c5:: with SMTP id t188mr7254113wmf.167.1613728334418; Fri, 19 Feb 2021 01:52:14 -0800 (PST) Received: from antares.lan (b.3.5.8.9.a.e.c.e.a.6.2.c.1.9.b.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:b91c:26ae:cea9:853b]) by smtp.gmail.com with ESMTPSA id a21sm13174910wmb.5.2021.02.19.01.52.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Feb 2021 01:52:14 -0800 (PST) From: Lorenz Bauer To: daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next v2 2/4] nsfs: add an ioctl to discover the network namespace cookie Date: Fri, 19 Feb 2021 09:51:47 +0000 Message-Id: <20210219095149.50346-3-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210219095149.50346-1-lmb@cloudflare.com> References: <20210219095149.50346-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. Signed-off-by: Lorenz Bauer --- fs/nsfs.c | 8 ++++++++ include/uapi/linux/nsfs.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index 800c1d0eb0d0..b7e70ab80257 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,11 @@ 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); + return put_user(net_ns->net_cookie, (u64 __user *)arg); default: return -ENOTTY; } 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 Fri Feb 19 09:51:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 385219 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 7781FC433E6 for ; Fri, 19 Feb 2021 09:54:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29D7A64EC0 for ; Fri, 19 Feb 2021 09:54:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230180AbhBSJyc (ORCPT ); Fri, 19 Feb 2021 04:54:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229587AbhBSJx6 (ORCPT ); Fri, 19 Feb 2021 04:53:58 -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 A0A5AC061793 for ; Fri, 19 Feb 2021 01:52:16 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id m1so6936363wml.2 for ; Fri, 19 Feb 2021 01:52:16 -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=cI+4Q9B2K94JT+5DcTEoKVkp9ZKpdCHdFbImC42RINU=; b=sOjSX4XLxkhRgKrDWLPNLVFyYL2nxwz8wDFyy9n47iTLgx+oAFtTXLXx5GyakWUSuk JY+c0/KXN3bSzzzgdn03/tLhVOs1D6SD5JK7tPOzkxkCcupzAYqxNlo3Q/cqUv/crvPT /YUxHd1vwi5NKyxkr4x0WC0O4u1pJG6WBlXO0= 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=cI+4Q9B2K94JT+5DcTEoKVkp9ZKpdCHdFbImC42RINU=; b=WmdQYO8ksbzOy8/dWqa1dwDjSDfiZGc4GG7/ZMDEsw/K727QTrcW3zP8FhvjxsQq3r UkPLjR/+7quUHef23BsVKMnst5a97KR6IOnS+ozSlFBRTm739Yd/r0IrOv3Nx+Dy5PKM 5eyObPS5FhGAIi/03aWcrXSCYFE0vIAfC130s9c8KJjiwr5u7vBdm4ULabBQ72Rkk2N/ WzhLHdpi+G0fOglg4wpoJFFJhailTFrIBrXN/ZFStluyIfKIs3j7Y20/xJlet2R0K+LP c+30tl0Y2YzQneOrEaIEzBOBR4tn5ZB0PWnu3wy4Pr8Q2O5Dqwq3vXTm/KZ18aWgQaiN cuuw== X-Gm-Message-State: AOAM533EOGYZbVu+ao3OpIdszlsyMG2iJvbxsZjlPiy6bVSxWsbwg5SR dewd5FXucoKDNpo9fZ+LXpkYZw== X-Google-Smtp-Source: ABdhPJz2buEuxtL4Cu0yPivg2+t1fFlai5FSk7Pn7XapoLj7gwquVDbZrvzPXiPIcKciflar2OuubA== X-Received: by 2002:a05:600c:3551:: with SMTP id i17mr7318051wmq.92.1613728335326; Fri, 19 Feb 2021 01:52:15 -0800 (PST) Received: from antares.lan (b.3.5.8.9.a.e.c.e.a.6.2.c.1.9.b.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:b91c:26ae:cea9:853b]) by smtp.gmail.com with ESMTPSA id a21sm13174910wmb.5.2021.02.19.01.52.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Feb 2021 01:52:15 -0800 (PST) From: Lorenz Bauer To: daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next v2 3/4] tools/testing: add test for NS_GET_COOKIE Date: Fri, 19 Feb 2021 09:51:48 +0000 Message-Id: <20210219095149.50346-4-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210219095149.50346-1-lmb@cloudflare.com> References: <20210219095149.50346-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@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/config | 1 + tools/testing/selftests/nsfs/netns.c | 57 +++++++++++++++++++++++++ 4 files changed, 60 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/config b/tools/testing/selftests/nsfs/config index 598d0a225fc9..ea654f6a4cd9 100644 --- a/tools/testing/selftests/nsfs/config +++ b/tools/testing/selftests/nsfs/config @@ -1,3 +1,4 @@ CONFIG_USER_NS=y CONFIG_UTS_NS=y CONFIG_PID_NS=y +CONFIG_NET_NS=y 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 Fri Feb 19 09:51:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 385545 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 AD121C433DB for ; Fri, 19 Feb 2021 09:54:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6F03D64EC0 for ; Fri, 19 Feb 2021 09:54:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230205AbhBSJys (ORCPT ); Fri, 19 Feb 2021 04:54:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229799AbhBSJyA (ORCPT ); Fri, 19 Feb 2021 04:54:00 -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 69578C061797 for ; Fri, 19 Feb 2021 01:52:17 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id a132so6502410wmc.0 for ; Fri, 19 Feb 2021 01:52:17 -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=+r5nIbn7SC0nctyFfGqm8+YsIS188S6/wSNE2V/0k+8=; b=hz5k1PGlu7AgjdzMWIweRUlAUAzqqza8aCOthWLj9TWZXqYDGMqVBB4uU/slTI4TM8 rV9kip87vvfakahU6qmPzbA+5Tx+92WJZawJbG95+KAzXc8GrU3OpGQxTufmxYV+RWiU nn+Yv/GwC++3yT+jfEN5iuuFyVALTVGW/ByKY= 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=+r5nIbn7SC0nctyFfGqm8+YsIS188S6/wSNE2V/0k+8=; b=P3Oq0T5ibsnLz66vLPl9ti5OsycMgeyC4fBN0olm1/cW34yiP9nC6inV3xi53v+0e5 qfcyUz48668DALbrYsyuHSYPVRFwbuolP5vH4XThst/4W1AMH4OBki3R3tE2AIwaT8e9 Wv/N+BlNnxiSKZXzijVpKDhTgyLD9rr8I4J11Hiv5V9ua5u1w+vbu/Di/idq1gUOckYp T1IShW/euV/eyAnoj0fwhaQ++6i3NpxKP32os5NFRDvCHzh8FNqiZTiGVvVpBtDr+kmq cqy2O0p495YUIJK1mX9OS6HPvl/kjM2y2MtPG/BnmbVo5UFhiA8dbiOieE06wC2yZGf+ UEdg== X-Gm-Message-State: AOAM531I59vX6aj3984h/ugpDJ5GVgtIoGF2JvUY+LmZfGxxM9SFQmqq P8TlCzZcTcKr0GrdO+WLhuJ1YQ== X-Google-Smtp-Source: ABdhPJyU0YFdbxdN5d7ID5JXFUD4nhAPxnvV0rbDB0tu1HAf2V/jj9ih5CNIgfoYdfZfuq92hP311Q== X-Received: by 2002:a1c:f002:: with SMTP id a2mr7266197wmb.117.1613728336072; Fri, 19 Feb 2021 01:52:16 -0800 (PST) Received: from antares.lan (b.3.5.8.9.a.e.c.e.a.6.2.c.1.9.b.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:b91c:26ae:cea9:853b]) by smtp.gmail.com with ESMTPSA id a21sm13174910wmb.5.2021.02.19.01.52.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Feb 2021 01:52:15 -0800 (PST) From: Lorenz Bauer To: daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next v2 4/4] tools/testing: add a selftest for SO_NETNS_COOKIE Date: Fri, 19 Feb 2021 09:51:49 +0000 Message-Id: <20210219095149.50346-5-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210219095149.50346-1-lmb@cloudflare.com> References: <20210219095149.50346-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@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/config | 1 + tools/testing/selftests/net/so_netns_cookie.c | 61 +++++++++++++++++++ 4 files changed, 64 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 25f198bec0b2..91bb372f5ba5 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -28,7 +28,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/config b/tools/testing/selftests/net/config index 614d5477365a..6f905b53904f 100644 --- a/tools/testing/selftests/net/config +++ b/tools/testing/selftests/net/config @@ -1,4 +1,5 @@ CONFIG_USER_NS=y +CONFIG_NET_NS=y CONFIG_BPF_SYSCALL=y CONFIG_TEST_BPF=m CONFIG_NUMA=y 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; +}