Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1258056pxb; Wed, 10 Feb 2021 04:16:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJy0kTbtBTSPoui9cmHMJIWlKN3n5IU59zMXoFZB0yGcMfkRbO/jhV5piSm7uCrPk+dImCR+ X-Received: by 2002:a17:907:2897:: with SMTP id em23mr2691412ejc.550.1612959410924; Wed, 10 Feb 2021 04:16:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612959410; cv=none; d=google.com; s=arc-20160816; b=J6CzgVlieqFqRajb2caS6czlwhzUuAvsgHBCNRosfpDE+a7o9CRLqktM94yJ8oc7ZW hIW7DueYP6klHmtrCwh1rOwHLHxou/qkZ8uKsAIDyfcZh2+eqJbyNjqS7AI/YiZ8M9MB 76ukK1CgWw6YsViaI6MUxWOBxzCDBWpaefUDD1TATipiDFnCElh+yBc15DVxaA7A/U3v YPeAjwjAmjQQnhXuWTy4E6+Jz0M7Nq7pRLGSEfpskl1hMpCDr9NzKJ4kezXm9eb4uzB0 GhN0k5u6RYOfqmpyCWABzl510ZJ3BTVo1qXcYNrFDRSbZ/To2arKXOhzzPpVORxWuA6O 4rsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qMWtdr/D/b55YVpH+AH+ThithGxQzKx+2a1eg5+/BUc=; b=iyNEZwTdyuA9MBR30VJb6pLvrHepuef+vq+WxmfpDhSlXp+dUj3IWtRdys2iBT2op4 ty9bO3oR+eJbffWDeEzpGOXeZqIomVmi193Z6LmgaG7ihsm3NRKTQcD+TbnGhIE2gxPn eq1710rf9NX05XUaBq6Jvav6bBiJpsWieVvsPqaHJdd2+YzR4H0226ptqas31Q1xc9HZ dghGcjE9+aS3wyURaWUXloFL2tgOfGoLYArJ8JUUMHb/9j74JF8qqLVgiLtmiRFUrzte ASZsD/5Nt7JOqQpif/OHlE4vPT+52srQDowQ5a0gEnfLwwqFOsIdGNkHo1MGu804mNGw KVUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=x7fU89uX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i8si1256510edg.104.2021.02.10.04.16.27; Wed, 10 Feb 2021 04:16:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=x7fU89uX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231659AbhBJMNu (ORCPT + 99 others); Wed, 10 Feb 2021 07:13:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231551AbhBJMLO (ORCPT ); Wed, 10 Feb 2021 07:11:14 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB4F4C061A2E for ; Wed, 10 Feb 2021 04:04:40 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id l12so2230000wry.2 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=Yyz84MnbgDL/2zsJNBK0lH/PdvDQSrC8uSOmMF6vbu2kFV2z1OqIz/nDpbhC5qAa/1 R9EFWHMe/k/cLfaomzVaS6ud71mhDl52Vkw90tuNG0ztBwLursl3EOOoL2r4uDssLvjt PoWCu0YvmE+aKFMvRp87aNIr67UeBywCdu+lS2GGQweS1dfqQkcJY620Nl/J0D2eq3ix DhCs6rQN/fVfL7Y2JCqdCiIKQzsM2/6zAgZPUyT86ep9Y2Sh07ggSs+DH69R+KyYfn4n l1GPrz8jzAZbfP3PP9sckN/ufWMzIl0gmPVoIqXUR73PJ+++udgGRgtdMA1NT2WR6rqZ x0cQ== X-Gm-Message-State: AOAM531kcqjtCYiKbJudYTzT2pmIXYazJSbjm17fY536a+JtE443ny9S rC3v9NTNiOMn2IuixCE3Bs5HJQ== 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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. Cc: linux-api@vger.kernel.org Signed-off-by: Lorenz Bauer --- 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 */ -- 2.27.0