1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ae5e1b22SPavel Emelyanov /*
3ae5e1b22SPavel Emelyanov * linux/ipc/namespace.c
4ae5e1b22SPavel Emelyanov * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
5ae5e1b22SPavel Emelyanov */
6ae5e1b22SPavel Emelyanov
7ae5e1b22SPavel Emelyanov #include <linux/ipc.h>
8ae5e1b22SPavel Emelyanov #include <linux/msg.h>
9ae5e1b22SPavel Emelyanov #include <linux/ipc_namespace.h>
10ae5e1b22SPavel Emelyanov #include <linux/rcupdate.h>
11ae5e1b22SPavel Emelyanov #include <linux/nsproxy.h>
12ae5e1b22SPavel Emelyanov #include <linux/slab.h>
135b825c3aSIngo Molnar #include <linux/cred.h>
147eafd7c7SSerge E. Hallyn #include <linux/fs.h>
157eafd7c7SSerge E. Hallyn #include <linux/mount.h>
16b515498fSSerge E. Hallyn #include <linux/user_namespace.h>
170bb80f24SDavid Howells #include <linux/proc_ns.h>
18f719ff9bSIngo Molnar #include <linux/sched/task.h>
19ae5e1b22SPavel Emelyanov
20ae5e1b22SPavel Emelyanov #include "util.h"
21ae5e1b22SPavel Emelyanov
22a80c4adcSRik van Riel /*
23a80c4adcSRik van Riel * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount.
24a80c4adcSRik van Riel */
25a80c4adcSRik van Riel static void free_ipc(struct work_struct *unused);
26a80c4adcSRik van Riel static DECLARE_WORK(free_ipc_work, free_ipc);
27a80c4adcSRik van Riel
inc_ipc_namespaces(struct user_namespace * ns)28aba35661SEric W. Biederman static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns)
29aba35661SEric W. Biederman {
30aba35661SEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES);
31aba35661SEric W. Biederman }
32aba35661SEric W. Biederman
dec_ipc_namespaces(struct ucounts * ucounts)33aba35661SEric W. Biederman static void dec_ipc_namespaces(struct ucounts *ucounts)
34aba35661SEric W. Biederman {
35aba35661SEric W. Biederman dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
36aba35661SEric W. Biederman }
37aba35661SEric W. Biederman
create_ipc_ns(struct user_namespace * user_ns,struct ipc_namespace * old_ns)38bcf58e72SEric W. Biederman static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
39b0e77598SSerge E. Hallyn struct ipc_namespace *old_ns)
40ae5e1b22SPavel Emelyanov {
41ae5e1b22SPavel Emelyanov struct ipc_namespace *ns;
42aba35661SEric W. Biederman struct ucounts *ucounts;
437eafd7c7SSerge E. Hallyn int err;
44ae5e1b22SPavel Emelyanov
45df75e774SEric W. Biederman err = -ENOSPC;
46a80c4adcSRik van Riel again:
47aba35661SEric W. Biederman ucounts = inc_ipc_namespaces(user_ns);
48a80c4adcSRik van Riel if (!ucounts) {
49a80c4adcSRik van Riel /*
50a80c4adcSRik van Riel * IPC namespaces are freed asynchronously, by free_ipc_work.
51a80c4adcSRik van Riel * If frees were pending, flush_work will wait, and
52a80c4adcSRik van Riel * return true. Fail the allocation if no frees are pending.
53a80c4adcSRik van Riel */
54a80c4adcSRik van Riel if (flush_work(&free_ipc_work))
55a80c4adcSRik van Riel goto again;
56aba35661SEric W. Biederman goto fail;
57a80c4adcSRik van Riel }
58aba35661SEric W. Biederman
59aba35661SEric W. Biederman err = -ENOMEM;
6030acd0bdSVasily Averin ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
61ae5e1b22SPavel Emelyanov if (ns == NULL)
62aba35661SEric W. Biederman goto fail_dec;
63ae5e1b22SPavel Emelyanov
646344c433SAl Viro err = ns_alloc_inum(&ns->ns);
65aba35661SEric W. Biederman if (err)
66aba35661SEric W. Biederman goto fail_free;
6733c42940SAl Viro ns->ns.ops = &ipcns_operations;
6898f842e6SEric W. Biederman
69137ec390SKirill Tkhai refcount_set(&ns->ns.count, 1);
70b236017aSEric W. Biederman ns->user_ns = get_user_ns(user_ns);
71aba35661SEric W. Biederman ns->ucounts = ucounts;
72b236017aSEric W. Biederman
730cfb6aeeSGuillaume Knispel err = mq_init_ns(ns);
740cfb6aeeSGuillaume Knispel if (err)
75eae04d25SDavidlohr Bueso goto fail_put;
76eae04d25SDavidlohr Bueso
77dc55e35fSAlexey Gladkov err = -ENOMEM;
78dc55e35fSAlexey Gladkov if (!setup_mq_sysctls(ns))
79dc55e35fSAlexey Gladkov goto fail_put;
80dc55e35fSAlexey Gladkov
811f5c135eSAlexey Gladkov if (!setup_ipc_sysctls(ns))
82db7cfc38SAlexey Gladkov goto fail_mq;
831f5c135eSAlexey Gladkov
8472d1e611SJiebin Sun err = msg_init_ns(ns);
8572d1e611SJiebin Sun if (err)
86*10209665SMa Wupeng goto fail_ipc;
8772d1e611SJiebin Sun
88eae04d25SDavidlohr Bueso sem_init_ns(ns);
89eae04d25SDavidlohr Bueso shm_init_ns(ns);
90ae5e1b22SPavel Emelyanov
91ae5e1b22SPavel Emelyanov return ns;
92aba35661SEric W. Biederman
93*10209665SMa Wupeng fail_ipc:
94*10209665SMa Wupeng retire_ipc_sysctls(ns);
95db7cfc38SAlexey Gladkov fail_mq:
96db7cfc38SAlexey Gladkov retire_mq_sysctls(ns);
97db7cfc38SAlexey Gladkov
98aba35661SEric W. Biederman fail_put:
99aba35661SEric W. Biederman put_user_ns(ns->user_ns);
100aba35661SEric W. Biederman ns_free_inum(&ns->ns);
101aba35661SEric W. Biederman fail_free:
102aba35661SEric W. Biederman kfree(ns);
103aba35661SEric W. Biederman fail_dec:
104aba35661SEric W. Biederman dec_ipc_namespaces(ucounts);
105aba35661SEric W. Biederman fail:
106aba35661SEric W. Biederman return ERR_PTR(err);
107ae5e1b22SPavel Emelyanov }
108ae5e1b22SPavel Emelyanov
copy_ipcs(unsigned long flags,struct user_namespace * user_ns,struct ipc_namespace * ns)109b0e77598SSerge E. Hallyn struct ipc_namespace *copy_ipcs(unsigned long flags,
110bcf58e72SEric W. Biederman struct user_namespace *user_ns, struct ipc_namespace *ns)
111ae5e1b22SPavel Emelyanov {
112ae5e1b22SPavel Emelyanov if (!(flags & CLONE_NEWIPC))
11364424289SAlexey Dobriyan return get_ipc_ns(ns);
114bcf58e72SEric W. Biederman return create_ipc_ns(user_ns, ns);
115ae5e1b22SPavel Emelyanov }
116ae5e1b22SPavel Emelyanov
11701b8b07aSPierre Peiffer /*
11801b8b07aSPierre Peiffer * free_ipcs - free all ipcs of one type
11901b8b07aSPierre Peiffer * @ns: the namespace to remove the ipcs from
12001b8b07aSPierre Peiffer * @ids: the table of ipcs to free
12101b8b07aSPierre Peiffer * @free: the function called to free each individual ipc
12201b8b07aSPierre Peiffer *
12301b8b07aSPierre Peiffer * Called for each kind of ipc when an ipc_namespace exits.
12401b8b07aSPierre Peiffer */
free_ipcs(struct ipc_namespace * ns,struct ipc_ids * ids,void (* free)(struct ipc_namespace *,struct kern_ipc_perm *))12501b8b07aSPierre Peiffer void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
12601b8b07aSPierre Peiffer void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
12701b8b07aSPierre Peiffer {
12801b8b07aSPierre Peiffer struct kern_ipc_perm *perm;
12901b8b07aSPierre Peiffer int next_id;
13001b8b07aSPierre Peiffer int total, in_use;
13101b8b07aSPierre Peiffer
132d9a605e4SDavidlohr Bueso down_write(&ids->rwsem);
13301b8b07aSPierre Peiffer
13401b8b07aSPierre Peiffer in_use = ids->in_use;
13501b8b07aSPierre Peiffer
13601b8b07aSPierre Peiffer for (total = 0, next_id = 0; total < in_use; next_id++) {
13701b8b07aSPierre Peiffer perm = idr_find(&ids->ipcs_idr, next_id);
13801b8b07aSPierre Peiffer if (perm == NULL)
13901b8b07aSPierre Peiffer continue;
14032a27500SDavidlohr Bueso rcu_read_lock();
14132a27500SDavidlohr Bueso ipc_lock_object(perm);
14201b8b07aSPierre Peiffer free(ns, perm);
14301b8b07aSPierre Peiffer total++;
14401b8b07aSPierre Peiffer }
145d9a605e4SDavidlohr Bueso up_write(&ids->rwsem);
14601b8b07aSPierre Peiffer }
14701b8b07aSPierre Peiffer
free_ipc_ns(struct ipc_namespace * ns)148b4188defSAlexey Dobriyan static void free_ipc_ns(struct ipc_namespace *ns)
149b4188defSAlexey Dobriyan {
150da27f796SRik van Riel /*
151da27f796SRik van Riel * Caller needs to wait for an RCU grace period to have passed
152da27f796SRik van Riel * after making the mount point inaccessible to new accesses.
153e1eb26faSGiuseppe Scrivano */
154da27f796SRik van Riel mntput(ns->mq_mnt);
155b4188defSAlexey Dobriyan sem_exit_ns(ns);
156b4188defSAlexey Dobriyan msg_exit_ns(ns);
157b4188defSAlexey Dobriyan shm_exit_ns(ns);
158b4188defSAlexey Dobriyan
159dc55e35fSAlexey Gladkov retire_mq_sysctls(ns);
1601f5c135eSAlexey Gladkov retire_ipc_sysctls(ns);
161dc55e35fSAlexey Gladkov
162aba35661SEric W. Biederman dec_ipc_namespaces(ns->ucounts);
163b515498fSSerge E. Hallyn put_user_ns(ns->user_ns);
1646344c433SAl Viro ns_free_inum(&ns->ns);
165be4d250aSXiaotian Feng kfree(ns);
166b4188defSAlexey Dobriyan }
167b4188defSAlexey Dobriyan
168e1eb26faSGiuseppe Scrivano static LLIST_HEAD(free_ipc_list);
free_ipc(struct work_struct * unused)169e1eb26faSGiuseppe Scrivano static void free_ipc(struct work_struct *unused)
170e1eb26faSGiuseppe Scrivano {
171e1eb26faSGiuseppe Scrivano struct llist_node *node = llist_del_all(&free_ipc_list);
172e1eb26faSGiuseppe Scrivano struct ipc_namespace *n, *t;
173e1eb26faSGiuseppe Scrivano
174e1eb26faSGiuseppe Scrivano llist_for_each_entry_safe(n, t, node, mnt_llist)
175da27f796SRik van Riel mnt_make_shortterm(n->mq_mnt);
176da27f796SRik van Riel
177da27f796SRik van Riel /* Wait for any last users to have gone away. */
178da27f796SRik van Riel synchronize_rcu();
179da27f796SRik van Riel
180da27f796SRik van Riel llist_for_each_entry_safe(n, t, node, mnt_llist)
181e1eb26faSGiuseppe Scrivano free_ipc_ns(n);
182e1eb26faSGiuseppe Scrivano }
183e1eb26faSGiuseppe Scrivano
184e1eb26faSGiuseppe Scrivano /*
1857eafd7c7SSerge E. Hallyn * put_ipc_ns - drop a reference to an ipc namespace.
1867eafd7c7SSerge E. Hallyn * @ns: the namespace to put
1877eafd7c7SSerge E. Hallyn *
1887eafd7c7SSerge E. Hallyn * If this is the last task in the namespace exiting, and
1897eafd7c7SSerge E. Hallyn * it is dropping the refcount to 0, then it can race with
1907eafd7c7SSerge E. Hallyn * a task in another ipc namespace but in a mounts namespace
1917eafd7c7SSerge E. Hallyn * which has this ipcns's mqueuefs mounted, doing some action
1927eafd7c7SSerge E. Hallyn * with one of the mqueuefs files. That can raise the refcount.
1937eafd7c7SSerge E. Hallyn * So dropping the refcount, and raising the refcount when
1947eafd7c7SSerge E. Hallyn * accessing it through the VFS, are protected with mq_lock.
1957eafd7c7SSerge E. Hallyn *
1967eafd7c7SSerge E. Hallyn * (Clearly, a task raising the refcount on its own ipc_ns
1977eafd7c7SSerge E. Hallyn * needn't take mq_lock since it can't race with the last task
1987eafd7c7SSerge E. Hallyn * in the ipcns exiting).
1997eafd7c7SSerge E. Hallyn */
put_ipc_ns(struct ipc_namespace * ns)2007eafd7c7SSerge E. Hallyn void put_ipc_ns(struct ipc_namespace *ns)
201ae5e1b22SPavel Emelyanov {
202137ec390SKirill Tkhai if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) {
2037eafd7c7SSerge E. Hallyn mq_clear_sbinfo(ns);
2047eafd7c7SSerge E. Hallyn spin_unlock(&mq_lock);
205e1eb26faSGiuseppe Scrivano
206e1eb26faSGiuseppe Scrivano if (llist_add(&ns->mnt_llist, &free_ipc_list))
207e1eb26faSGiuseppe Scrivano schedule_work(&free_ipc_work);
2087eafd7c7SSerge E. Hallyn }
2097eafd7c7SSerge E. Hallyn }
210a00eaf11SEric W. Biederman
to_ipc_ns(struct ns_common * ns)2113c041184SAl Viro static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
2123c041184SAl Viro {
2133c041184SAl Viro return container_of(ns, struct ipc_namespace, ns);
2143c041184SAl Viro }
2153c041184SAl Viro
ipcns_get(struct task_struct * task)21664964528SAl Viro static struct ns_common *ipcns_get(struct task_struct *task)
217a00eaf11SEric W. Biederman {
218a00eaf11SEric W. Biederman struct ipc_namespace *ns = NULL;
219a00eaf11SEric W. Biederman struct nsproxy *nsproxy;
220a00eaf11SEric W. Biederman
221728dba3aSEric W. Biederman task_lock(task);
222728dba3aSEric W. Biederman nsproxy = task->nsproxy;
223a00eaf11SEric W. Biederman if (nsproxy)
224a00eaf11SEric W. Biederman ns = get_ipc_ns(nsproxy->ipc_ns);
225728dba3aSEric W. Biederman task_unlock(task);
226a00eaf11SEric W. Biederman
2273c041184SAl Viro return ns ? &ns->ns : NULL;
228a00eaf11SEric W. Biederman }
229a00eaf11SEric W. Biederman
ipcns_put(struct ns_common * ns)23064964528SAl Viro static void ipcns_put(struct ns_common *ns)
231a00eaf11SEric W. Biederman {
2323c041184SAl Viro return put_ipc_ns(to_ipc_ns(ns));
233a00eaf11SEric W. Biederman }
234a00eaf11SEric W. Biederman
ipcns_install(struct nsset * nsset,struct ns_common * new)235f2a8d52eSChristian Brauner static int ipcns_install(struct nsset *nsset, struct ns_common *new)
236a00eaf11SEric W. Biederman {
237f2a8d52eSChristian Brauner struct nsproxy *nsproxy = nsset->nsproxy;
2383c041184SAl Viro struct ipc_namespace *ns = to_ipc_ns(new);
2395e4a0847SEric W. Biederman if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
240f2a8d52eSChristian Brauner !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
241142e1d1dSEric W. Biederman return -EPERM;
242142e1d1dSEric W. Biederman
243a00eaf11SEric W. Biederman put_ipc_ns(nsproxy->ipc_ns);
244a00eaf11SEric W. Biederman nsproxy->ipc_ns = get_ipc_ns(ns);
245a00eaf11SEric W. Biederman return 0;
246a00eaf11SEric W. Biederman }
247a00eaf11SEric W. Biederman
ipcns_owner(struct ns_common * ns)248bcac25a5SAndrey Vagin static struct user_namespace *ipcns_owner(struct ns_common *ns)
249bcac25a5SAndrey Vagin {
250bcac25a5SAndrey Vagin return to_ipc_ns(ns)->user_ns;
251bcac25a5SAndrey Vagin }
252bcac25a5SAndrey Vagin
253a00eaf11SEric W. Biederman const struct proc_ns_operations ipcns_operations = {
254a00eaf11SEric W. Biederman .name = "ipc",
255a00eaf11SEric W. Biederman .type = CLONE_NEWIPC,
256a00eaf11SEric W. Biederman .get = ipcns_get,
257a00eaf11SEric W. Biederman .put = ipcns_put,
258a00eaf11SEric W. Biederman .install = ipcns_install,
259bcac25a5SAndrey Vagin .owner = ipcns_owner,
260a00eaf11SEric W. Biederman };
261