Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bcac25a5 authored by Andrey Vagin's avatar Andrey Vagin Committed by Eric W. Biederman
Browse files

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: default avatarSerge Hallyn <serge@hallyn.com>
Signed-off-by: default avatarAndrei Vagin <avagin@openvz.org>
Signed-off-by: default avatarEric W. Biederman <ebiederm@xmission.com>
parent 29b4817d
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -3348,10 +3348,16 @@ static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
	return 0;
	return 0;
}
}


static struct user_namespace *mntns_owner(struct ns_common *ns)
{
	return to_mnt_ns(ns)->user_ns;
}

const struct proc_ns_operations mntns_operations = {
const struct proc_ns_operations mntns_operations = {
	.name		= "mnt",
	.name		= "mnt",
	.type		= CLONE_NEWNS,
	.type		= CLONE_NEWNS,
	.get		= mntns_get,
	.get		= mntns_get,
	.put		= mntns_put,
	.put		= mntns_put,
	.install	= mntns_install,
	.install	= mntns_install,
	.owner		= mntns_owner,
};
};
+1 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ struct proc_ns_operations {
	struct ns_common *(*get)(struct task_struct *task);
	struct ns_common *(*get)(struct task_struct *task);
	void (*put)(struct ns_common *ns);
	void (*put)(struct ns_common *ns);
	int (*install)(struct nsproxy *nsproxy, struct ns_common *ns);
	int (*install)(struct nsproxy *nsproxy, struct ns_common *ns);
	struct user_namespace *(*owner)(struct ns_common *ns);
};
};


extern const struct proc_ns_operations netns_operations;
extern const struct proc_ns_operations netns_operations;
+7 −0
Original line number Original line Diff line number Diff line
@@ -73,6 +73,8 @@ extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t,
extern int proc_setgroups_show(struct seq_file *m, void *v);
extern int proc_setgroups_show(struct seq_file *m, void *v);
extern bool userns_may_setgroups(const struct user_namespace *ns);
extern bool userns_may_setgroups(const struct user_namespace *ns);
extern bool current_in_userns(const struct user_namespace *target_ns);
extern bool current_in_userns(const struct user_namespace *target_ns);

struct ns_common *ns_get_owner(struct ns_common *ns);
#else
#else


static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
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)
{
{
	return true;
	return true;
}
}

static inline struct ns_common *ns_get_owner(struct ns_common *ns)
{
	return ERR_PTR(-EPERM);
}
#endif
#endif


#endif /* _LINUX_USER_H */
#endif /* _LINUX_USER_H */
+6 −0
Original line number Original line Diff line number Diff line
@@ -165,10 +165,16 @@ static int ipcns_install(struct nsproxy *nsproxy, struct ns_common *new)
	return 0;
	return 0;
}
}


static struct user_namespace *ipcns_owner(struct ns_common *ns)
{
	return to_ipc_ns(ns)->user_ns;
}

const struct proc_ns_operations ipcns_operations = {
const struct proc_ns_operations ipcns_operations = {
	.name		= "ipc",
	.name		= "ipc",
	.type		= CLONE_NEWIPC,
	.type		= CLONE_NEWIPC,
	.get		= ipcns_get,
	.get		= ipcns_get,
	.put		= ipcns_put,
	.put		= ipcns_put,
	.install	= ipcns_install,
	.install	= ipcns_install,
	.owner		= ipcns_owner,
};
};
+6 −0
Original line number Original line Diff line number Diff line
@@ -6403,12 +6403,18 @@ static void cgroupns_put(struct ns_common *ns)
	put_cgroup_ns(to_cg_ns(ns));
	put_cgroup_ns(to_cg_ns(ns));
}
}


static struct user_namespace *cgroupns_owner(struct ns_common *ns)
{
	return to_cg_ns(ns)->user_ns;
}

const struct proc_ns_operations cgroupns_operations = {
const struct proc_ns_operations cgroupns_operations = {
	.name		= "cgroup",
	.name		= "cgroup",
	.type		= CLONE_NEWCGROUP,
	.type		= CLONE_NEWCGROUP,
	.get		= cgroupns_get,
	.get		= cgroupns_get,
	.put		= cgroupns_put,
	.put		= cgroupns_put,
	.install	= cgroupns_install,
	.install	= cgroupns_install,
	.owner		= cgroupns_owner,
};
};


static __init int cgroup_namespaces_init(void)
static __init int cgroup_namespaces_init(void)
Loading