Skip to content

Commit bcac25a

Browse files
avaginebiederm
authored andcommitted
kernel: add a helper to get an owning user namespace for a namespace
Return -EPERM if an owning user namespace is outside of a process current user namespace. v2: In a first version ns_get_owner returned ENOENT for init_user_ns. This special cases was removed from this version. There is nothing outside of init_user_ns, so we can return EPERM. v3: rename ns->get_owner() to ns->owner(). get_* usually means that it grabs a reference. Acked-by: Serge Hallyn <serge@hallyn.com> Signed-off-by: Andrei Vagin <avagin@openvz.org> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent 29b4817 commit bcac25a

9 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎fs/namespace.c‎

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

3351+
static struct user_namespace *mntns_owner(struct ns_common *ns)
3352+
{
3353+
return to_mnt_ns(ns)->user_ns;
3354+
}
3355+
33513356
const struct proc_ns_operations mntns_operations = {
33523357
.name = "mnt",
33533358
.type = CLONE_NEWNS,
33543359
.get = mntns_get,
33553360
.put = mntns_put,
33563361
.install = mntns_install,
3362+
.owner = mntns_owner,
33573363
};

‎include/linux/proc_ns.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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);
2122
};
2223

2324
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
@@ -73,6 +73,8 @@ extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t,
7373
extern int proc_setgroups_show(struct seq_file *m, void *v);
7474
extern bool userns_may_setgroups(const struct user_namespace *ns);
7575
extern bool current_in_userns(const struct user_namespace *target_ns);
76+
77+
struct ns_common *ns_get_owner(struct ns_common *ns);
7678
#else
7779

7880
static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
@@ -106,6 +108,11 @@ static inline bool current_in_userns(const struct user_namespace *target_ns)
106108
{
107109
return true;
108110
}
111+
112+
static inline struct ns_common *ns_get_owner(struct ns_common *ns)
113+
{
114+
return ERR_PTR(-EPERM);
115+
}
109116
#endif
110117

111118
#endif /* _LINUX_USER_H */

‎ipc/namespace.c‎

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

168+
static struct user_namespace *ipcns_owner(struct ns_common *ns)
169+
{
170+
return to_ipc_ns(ns)->user_ns;
171+
}
172+
168173
const struct proc_ns_operations ipcns_operations = {
169174
.name = "ipc",
170175
.type = CLONE_NEWIPC,
171176
.get = ipcns_get,
172177
.put = ipcns_put,
173178
.install = ipcns_install,
179+
.owner = ipcns_owner,
174180
};

‎kernel/cgroup.c‎

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

6406+
static struct user_namespace *cgroupns_owner(struct ns_common *ns)
6407+
{
6408+
return to_cg_ns(ns)->user_ns;
6409+
}
6410+
64066411
const struct proc_ns_operations cgroupns_operations = {
64076412
.name = "cgroup",
64086413
.type = CLONE_NEWCGROUP,
64096414
.get = cgroupns_get,
64106415
.put = cgroupns_put,
64116416
.install = cgroupns_install,
6417+
.owner = cgroupns_owner,
64126418
};
64136419

64146420
static __init int cgroup_namespaces_init(void)

‎kernel/pid_namespace.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,18 @@ static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
388388
return 0;
389389
}
390390

391+
static struct user_namespace *pidns_owner(struct ns_common *ns)
392+
{
393+
return to_pid_ns(ns)->user_ns;
394+
}
395+
391396
const struct proc_ns_operations pidns_operations = {
392397
.name = "pid",
393398
.type = CLONE_NEWPID,
394399
.get = pidns_get,
395400
.put = pidns_put,
396401
.install = pidns_install,
402+
.owner = pidns_owner,
397403
};
398404

399405
static __init int pid_namespaces_init(void)

‎kernel/user_namespace.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,36 @@ static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
10041004
return commit_creds(cred);
10051005
}
10061006

1007+
struct ns_common *ns_get_owner(struct ns_common *ns)
1008+
{
1009+
struct user_namespace *my_user_ns = current_user_ns();
1010+
struct user_namespace *owner, *p;
1011+
1012+
/* See if the owner is in the current user namespace */
1013+
owner = p = ns->ops->owner(ns);
1014+
for (;;) {
1015+
if (!p)
1016+
return ERR_PTR(-EPERM);
1017+
if (p == my_user_ns)
1018+
break;
1019+
p = p->parent;
1020+
}
1021+
1022+
return &get_user_ns(owner)->ns;
1023+
}
1024+
1025+
static struct user_namespace *userns_owner(struct ns_common *ns)
1026+
{
1027+
return to_user_ns(ns)->parent;
1028+
}
1029+
10071030
const struct proc_ns_operations userns_operations = {
10081031
.name = "user",
10091032
.type = CLONE_NEWUSER,
10101033
.get = userns_get,
10111034
.put = userns_put,
10121035
.install = userns_install,
1036+
.owner = userns_owner,
10131037
};
10141038

10151039
static __init int user_namespaces_init(void)

‎kernel/utsname.c‎

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

133+
static struct user_namespace *utsns_owner(struct ns_common *ns)
134+
{
135+
return to_uts_ns(ns)->user_ns;
136+
}
137+
133138
const struct proc_ns_operations utsns_operations = {
134139
.name = "uts",
135140
.type = CLONE_NEWUTS,
136141
.get = utsns_get,
137142
.put = utsns_put,
138143
.install = utsns_install,
144+
.owner = utsns_owner,
139145
};

‎net/core/net_namespace.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,17 @@ static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
996996
return 0;
997997
}
998998

999+
static struct user_namespace *netns_owner(struct ns_common *ns)
1000+
{
1001+
return to_net_ns(ns)->user_ns;
1002+
}
1003+
9991004
const struct proc_ns_operations netns_operations = {
10001005
.name = "net",
10011006
.type = CLONE_NEWNET,
10021007
.get = netns_get,
10031008
.put = netns_put,
10041009
.install = netns_install,
1010+
.owner = netns_owner,
10051011
};
10061012
#endif

0 commit comments

Comments
 (0)