Skip to content
Permalink
Browse files
nsfs: add an ioctl to discover the network namespace cookie
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 <lmb@cloudflare.com>
  • Loading branch information
lmb authored and intel-lab-lkp committed Feb 10, 2021
1 parent f3d9973 commit ec096a87ec268b0634a140515fb54b21a2c67036
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
@@ -11,6 +11,7 @@
#include <linux/user_namespace.h>
#include <linux/nsfs.h>
#include <linux/uaccess.h>
#include <net/net_namespace.h>

#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;
}
@@ -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);
@@ -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 */

0 comments on commit ec096a8

Please sign in to comment.