Skip to content

Commit 7872559

Browse files
committed
Merge branch 'nsfs-ioctls' into HEAD
From: Andrey Vagin <avagin@openvz.org> Each namespace has an owning user namespace and now there is not way to discover these relationships. Pid and user namepaces are hierarchical. There is no way to discover parent-child relationships too. Why we may want to know relationships between namespaces? One use would be visualization, in order to understand the running system. Another would be to answer the question: what capability does process X have to perform operations on a resource governed by namespace Y? One more use-case (which usually called abnormal) is checkpoint/restart. In CRIU we are going to dump and restore nested namespaces. There [1] was a discussion about which interface to choose to determing relationships between namespaces. Eric suggested to add two ioctl-s [2]: > Grumble, Grumble. I think this may actually a case for creating ioctls > for these two cases. Now that random nsfs file descriptors are bind > mountable the original reason for using proc files is not as pressing. > > One ioctl for the user namespace that owns a file descriptor. > One ioctl for the parent namespace of a namespace file descriptor. Here is an implementaions of these ioctl-s. $ man man7/namespaces.7 ... Since Linux 4.X, the following ioctl(2) calls are supported for namespace file descriptors. The correct syntax is: fd = ioctl(ns_fd, ioctl_type); where ioctl_type is one of the following: NS_GET_USERNS Returns a file descriptor that refers to an owning user names‐ pace. NS_GET_PARENT Returns a file descriptor that refers to a parent namespace. This ioctl(2) can be used for pid and user namespaces. For user namespaces, NS_GET_PARENT and NS_GET_USERNS have the same meaning. In addition to generic ioctl(2) errors, the following specific ones can occur: EINVAL NS_GET_PARENT was called for a nonhierarchical namespace. EPERM The requested namespace is outside of the current namespace scope. [1] https://lkml.org/lkml/2016/7/6/158 [2] https://lkml.org/lkml/2016/7/9/101 Changes for v2: * don't return ENOENT for init_user_ns and init_pid_ns. There is nothing outside of the init namespace, so we can return EPERM in this case too. > The fewer special cases the easier the code is to get > correct, and the easier it is to read. // Eric Changes for v3: * rename ns->get_owner() to ns->owner(). get_* usually means that it grabs a reference. Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: James Bottomley <James.Bottomley@HansenPartnership.com> Cc: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com> Cc: "W. Trevor King" <wking@tremily.us> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Serge Hallyn <serge.hallyn@canonical.com>
2 parents 93f0a88 + 6ad92bf commit 7872559

15 files changed

Lines changed: 371 additions & 13 deletions

File tree

‎fs/namespace.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,10 +3368,16 @@ static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
33683368
return 0;
33693369
}
33703370

3371+
static struct user_namespace *mntns_owner(struct ns_common *ns)
3372+
{
3373+
return to_mnt_ns(ns)->user_ns;
3374+
}
3375+
33713376
const struct proc_ns_operations mntns_operations = {
33723377
.name = "mnt",
33733378
.type = CLONE_NEWNS,
33743379
.get = mntns_get,
33753380
.put = mntns_put,
33763381
.install = mntns_install,
3382+
.owner = mntns_owner,
33773383
};

‎fs/nsfs.c‎

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
#include <linux/magic.h>
66
#include <linux/ktime.h>
77
#include <linux/seq_file.h>
8+
#include <linux/user_namespace.h>
9+
#include <linux/nsfs.h>
810

911
static struct vfsmount *nsfs_mnt;
1012

13+
static long ns_ioctl(struct file *filp, unsigned int ioctl,
14+
unsigned long arg);
1115
static const struct file_operations ns_file_operations = {
1216
.llseek = no_llseek,
17+
.unlocked_ioctl = ns_ioctl,
1318
};
1419

1520
static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
@@ -44,22 +49,14 @@ static void nsfs_evict(struct inode *inode)
4449
ns->ops->put(ns);
4550
}
4651

47-
void *ns_get_path(struct path *path, struct task_struct *task,
48-
const struct proc_ns_operations *ns_ops)
52+
static void *__ns_get_path(struct path *path, struct ns_common *ns)
4953
{
5054
struct vfsmount *mnt = mntget(nsfs_mnt);
5155
struct qstr qname = { .name = "", };
5256
struct dentry *dentry;
5357
struct inode *inode;
54-
struct ns_common *ns;
5558
unsigned long d;
5659

57-
again:
58-
ns = ns_ops->get(task);
59-
if (!ns) {
60-
mntput(mnt);
61-
return ERR_PTR(-ENOENT);
62-
}
6360
rcu_read_lock();
6461
d = atomic_long_read(&ns->stashed);
6562
if (!d)
@@ -68,7 +65,7 @@ void *ns_get_path(struct path *path, struct task_struct *task,
6865
if (!lockref_get_not_dead(&dentry->d_lockref))
6966
goto slow;
7067
rcu_read_unlock();
71-
ns_ops->put(ns);
68+
ns->ops->put(ns);
7269
got_it:
7370
path->mnt = mnt;
7471
path->dentry = dentry;
@@ -77,7 +74,7 @@ void *ns_get_path(struct path *path, struct task_struct *task,
7774
rcu_read_unlock();
7875
inode = new_inode_pseudo(mnt->mnt_sb);
7976
if (!inode) {
80-
ns_ops->put(ns);
77+
ns->ops->put(ns);
8178
mntput(mnt);
8279
return ERR_PTR(-ENOMEM);
8380
}
@@ -95,17 +92,94 @@ void *ns_get_path(struct path *path, struct task_struct *task,
9592
return ERR_PTR(-ENOMEM);
9693
}
9794
d_instantiate(dentry, inode);
98-
dentry->d_fsdata = (void *)ns_ops;
95+
dentry->d_fsdata = (void *)ns->ops;
9996
d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry);
10097
if (d) {
10198
d_delete(dentry); /* make sure ->d_prune() does nothing */
10299
dput(dentry);
100+
mntput(mnt);
103101
cpu_relax();
104-
goto again;
102+
return ERR_PTR(-EAGAIN);
105103
}
106104
goto got_it;
107105
}
108106

107+
void *ns_get_path(struct path *path, struct task_struct *task,
108+
const struct proc_ns_operations *ns_ops)
109+
{
110+
struct ns_common *ns;
111+
void *ret;
112+
113+
again:
114+
ns = ns_ops->get(task);
115+
if (!ns)
116+
return ERR_PTR(-ENOENT);
117+
118+
ret = __ns_get_path(path, ns);
119+
if (IS_ERR(ret) && PTR_ERR(ret) == -EAGAIN)
120+
goto again;
121+
return ret;
122+
}
123+
124+
static int open_related_ns(struct ns_common *ns,
125+
struct ns_common *(*get_ns)(struct ns_common *ns))
126+
{
127+
struct path path = {};
128+
struct file *f;
129+
void *err;
130+
int fd;
131+
132+
fd = get_unused_fd_flags(O_CLOEXEC);
133+
if (fd < 0)
134+
return fd;
135+
136+
while (1) {
137+
struct ns_common *relative;
138+
139+
relative = get_ns(ns);
140+
if (IS_ERR(relative)) {
141+
put_unused_fd(fd);
142+
return PTR_ERR(relative);
143+
}
144+
145+
err = __ns_get_path(&path, relative);
146+
if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
147+
continue;
148+
break;
149+
}
150+
if (IS_ERR(err)) {
151+
put_unused_fd(fd);
152+
return PTR_ERR(err);
153+
}
154+
155+
f = dentry_open(&path, O_RDONLY, current_cred());
156+
path_put(&path);
157+
if (IS_ERR(f)) {
158+
put_unused_fd(fd);
159+
fd = PTR_ERR(f);
160+
} else
161+
fd_install(fd, f);
162+
163+
return fd;
164+
}
165+
166+
static long ns_ioctl(struct file *filp, unsigned int ioctl,
167+
unsigned long arg)
168+
{
169+
struct ns_common *ns = get_proc_ns(file_inode(filp));
170+
171+
switch (ioctl) {
172+
case NS_GET_USERNS:
173+
return open_related_ns(ns, ns_get_owner);
174+
case NS_GET_PARENT:
175+
if (!ns->ops->get_parent)
176+
return -EINVAL;
177+
return open_related_ns(ns, ns->ops->get_parent);
178+
default:
179+
return -ENOTTY;
180+
}
181+
}
182+
109183
int ns_get_name(char *buf, size_t size, struct task_struct *task,
110184
const struct proc_ns_operations *ns_ops)
111185
{

‎include/linux/proc_ns.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct proc_ns_operations {
1818
struct ns_common *(*get)(struct task_struct *task);
1919
void (*put)(struct ns_common *ns);
2020
int (*install)(struct nsproxy *nsproxy, struct ns_common *ns);
21+
struct user_namespace *(*owner)(struct ns_common *ns);
22+
struct ns_common *(*get_parent)(struct ns_common *ns);
2123
};
2224

2325
extern const struct proc_ns_operations netns_operations;

‎include/linux/user_namespace.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t,
106106
extern int proc_setgroups_show(struct seq_file *m, void *v);
107107
extern bool userns_may_setgroups(const struct user_namespace *ns);
108108
extern bool current_in_userns(const struct user_namespace *target_ns);
109+
110+
struct ns_common *ns_get_owner(struct ns_common *ns);
109111
#else
110112

111113
static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
@@ -139,6 +141,11 @@ static inline bool current_in_userns(const struct user_namespace *target_ns)
139141
{
140142
return true;
141143
}
144+
145+
static inline struct ns_common *ns_get_owner(struct ns_common *ns)
146+
{
147+
return ERR_PTR(-EPERM);
148+
}
142149
#endif
143150

144151
#endif /* _LINUX_USER_H */

‎include/uapi/linux/nsfs.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef __LINUX_NSFS_H
2+
#define __LINUX_NSFS_H
3+
4+
#include <linux/ioctl.h>
5+
6+
#define NSIO 0xb7
7+
8+
/* Returns a file descriptor that refers to an owning user namespace */
9+
#define NS_GET_USERNS _IO(NSIO, 0x1)
10+
/* Returns a file descriptor that refers to a parent namespace */
11+
#define NS_GET_PARENT _IO(NSIO, 0x2)
12+
13+
#endif /* __LINUX_NSFS_H */

‎ipc/namespace.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,16 @@ static int ipcns_install(struct nsproxy *nsproxy, struct ns_common *new)
188188
return 0;
189189
}
190190

191+
static struct user_namespace *ipcns_owner(struct ns_common *ns)
192+
{
193+
return to_ipc_ns(ns)->user_ns;
194+
}
195+
191196
const struct proc_ns_operations ipcns_operations = {
192197
.name = "ipc",
193198
.type = CLONE_NEWIPC,
194199
.get = ipcns_get,
195200
.put = ipcns_put,
196201
.install = ipcns_install,
202+
.owner = ipcns_owner,
197203
};

‎kernel/cgroup.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6421,12 +6421,18 @@ static void cgroupns_put(struct ns_common *ns)
64216421
put_cgroup_ns(to_cg_ns(ns));
64226422
}
64236423

6424+
static struct user_namespace *cgroupns_owner(struct ns_common *ns)
6425+
{
6426+
return to_cg_ns(ns)->user_ns;
6427+
}
6428+
64246429
const struct proc_ns_operations cgroupns_operations = {
64256430
.name = "cgroup",
64266431
.type = CLONE_NEWCGROUP,
64276432
.get = cgroupns_get,
64286433
.put = cgroupns_put,
64296434
.install = cgroupns_install,
6435+
.owner = cgroupns_owner,
64306436
};
64316437

64326438
static __init int cgroup_namespaces_init(void)

‎kernel/pid_namespace.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,37 @@ static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
405405
return 0;
406406
}
407407

408+
static struct ns_common *pidns_get_parent(struct ns_common *ns)
409+
{
410+
struct pid_namespace *active = task_active_pid_ns(current);
411+
struct pid_namespace *pid_ns, *p;
412+
413+
/* See if the parent is in the current namespace */
414+
pid_ns = p = to_pid_ns(ns)->parent;
415+
for (;;) {
416+
if (!p)
417+
return ERR_PTR(-EPERM);
418+
if (p == active)
419+
break;
420+
p = p->parent;
421+
}
422+
423+
return &get_pid_ns(pid_ns)->ns;
424+
}
425+
426+
static struct user_namespace *pidns_owner(struct ns_common *ns)
427+
{
428+
return to_pid_ns(ns)->user_ns;
429+
}
430+
408431
const struct proc_ns_operations pidns_operations = {
409432
.name = "pid",
410433
.type = CLONE_NEWPID,
411434
.get = pidns_get,
412435
.put = pidns_put,
413436
.install = pidns_install,
437+
.owner = pidns_owner,
438+
.get_parent = pidns_get_parent,
414439
};
415440

416441
static __init int pid_namespaces_init(void)

‎kernel/user_namespace.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,12 +1050,37 @@ static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
10501050
return commit_creds(cred);
10511051
}
10521052

1053+
struct ns_common *ns_get_owner(struct ns_common *ns)
1054+
{
1055+
struct user_namespace *my_user_ns = current_user_ns();
1056+
struct user_namespace *owner, *p;
1057+
1058+
/* See if the owner is in the current user namespace */
1059+
owner = p = ns->ops->owner(ns);
1060+
for (;;) {
1061+
if (!p)
1062+
return ERR_PTR(-EPERM);
1063+
if (p == my_user_ns)
1064+
break;
1065+
p = p->parent;
1066+
}
1067+
1068+
return &get_user_ns(owner)->ns;
1069+
}
1070+
1071+
static struct user_namespace *userns_owner(struct ns_common *ns)
1072+
{
1073+
return to_user_ns(ns)->parent;
1074+
}
1075+
10531076
const struct proc_ns_operations userns_operations = {
10541077
.name = "user",
10551078
.type = CLONE_NEWUSER,
10561079
.get = userns_get,
10571080
.put = userns_put,
10581081
.install = userns_install,
1082+
.owner = userns_owner,
1083+
.get_parent = ns_get_owner,
10591084
};
10601085

10611086
static __init int user_namespaces_init(void)

‎kernel/utsname.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,16 @@ static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
154154
return 0;
155155
}
156156

157+
static struct user_namespace *utsns_owner(struct ns_common *ns)
158+
{
159+
return to_uts_ns(ns)->user_ns;
160+
}
161+
157162
const struct proc_ns_operations utsns_operations = {
158163
.name = "uts",
159164
.type = CLONE_NEWUTS,
160165
.get = utsns_get,
161166
.put = utsns_put,
162167
.install = utsns_install,
168+
.owner = utsns_owner,
163169
};

0 commit comments

Comments
 (0)