|
|
f2d475 |
// SPDX-License-Identifier: GPL-2.0
|
|
|
f2d475 |
/*
|
|
|
f2d475 |
* linux/ipc/namespace.c
|
|
|
f2d475 |
* Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
|
|
|
f2d475 |
*/
|
|
|
f2d475 |
|
|
|
f2d475 |
#include <linux/ipc.h>
|
|
|
f2d475 |
#include <linux/msg.h>
|
|
|
f2d475 |
#include <linux/ipc_namespace.h>
|
|
|
f2d475 |
#include <linux/rcupdate.h>
|
|
|
f2d475 |
#include <linux/nsproxy.h>
|
|
|
f2d475 |
#include <linux/slab.h>
|
|
|
f2d475 |
#include <linux/cred.h>
|
|
|
f2d475 |
#include <linux/fs.h>
|
|
|
f2d475 |
#include <linux/mount.h>
|
|
|
f2d475 |
#include <linux/user_namespace.h>
|
|
|
f2d475 |
#include <linux/proc_ns.h>
|
|
|
f2d475 |
#include <linux/sched/task.h>
|
|
|
f2d475 |
|
|
|
f2d475 |
#include "util.h"
|
|
|
f2d475 |
|
|
|
f2d475 |
static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static void dec_ipc_namespaces(struct ucounts *ucounts)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
|
|
|
f2d475 |
struct ipc_namespace *old_ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
struct ipc_namespace *ns;
|
|
|
f2d475 |
struct ucounts *ucounts;
|
|
|
f2d475 |
int err;
|
|
|
f2d475 |
|
|
|
f2d475 |
err = -ENOSPC;
|
|
|
f2d475 |
ucounts = inc_ipc_namespaces(user_ns);
|
|
|
f2d475 |
if (!ucounts)
|
|
|
f2d475 |
goto fail;
|
|
|
f2d475 |
|
|
|
f2d475 |
err = -ENOMEM;
|
|
|
f2d475 |
ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
|
|
|
f2d475 |
if (ns == NULL)
|
|
|
f2d475 |
goto fail_dec;
|
|
|
f2d475 |
|
|
|
f2d475 |
err = ns_alloc_inum(&ns->ns);
|
|
|
f2d475 |
if (err)
|
|
|
f2d475 |
goto fail_free;
|
|
|
f2d475 |
ns->ns.ops = &ipcns_operations;
|
|
|
f2d475 |
|
|
|
f2d475 |
refcount_set(&ns->count, 1);
|
|
|
f2d475 |
ns->user_ns = get_user_ns(user_ns);
|
|
|
f2d475 |
ns->ucounts = ucounts;
|
|
|
f2d475 |
|
|
|
f2d475 |
err = mq_init_ns(ns);
|
|
|
f2d475 |
if (err)
|
|
|
f2d475 |
goto fail_put;
|
|
|
f2d475 |
|
|
|
f2d475 |
sem_init_ns(ns);
|
|
|
f2d475 |
msg_init_ns(ns);
|
|
|
f2d475 |
shm_init_ns(ns);
|
|
|
f2d475 |
|
|
|
f2d475 |
return ns;
|
|
|
f2d475 |
|
|
|
f2d475 |
fail_put:
|
|
|
f2d475 |
put_user_ns(ns->user_ns);
|
|
|
f2d475 |
ns_free_inum(&ns->ns);
|
|
|
f2d475 |
fail_free:
|
|
|
f2d475 |
kfree(ns);
|
|
|
f2d475 |
fail_dec:
|
|
|
f2d475 |
dec_ipc_namespaces(ucounts);
|
|
|
f2d475 |
fail:
|
|
|
f2d475 |
return ERR_PTR(err);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
struct ipc_namespace *copy_ipcs(unsigned long flags,
|
|
|
f2d475 |
struct user_namespace *user_ns, struct ipc_namespace *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
if (!(flags & CLONE_NEWIPC))
|
|
|
f2d475 |
return get_ipc_ns(ns);
|
|
|
f2d475 |
return create_ipc_ns(user_ns, ns);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
/*
|
|
|
f2d475 |
* free_ipcs - free all ipcs of one type
|
|
|
f2d475 |
* @ns: the namespace to remove the ipcs from
|
|
|
f2d475 |
* @ids: the table of ipcs to free
|
|
|
f2d475 |
* @free: the function called to free each individual ipc
|
|
|
f2d475 |
*
|
|
|
f2d475 |
* Called for each kind of ipc when an ipc_namespace exits.
|
|
|
f2d475 |
*/
|
|
|
f2d475 |
void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
|
|
|
f2d475 |
void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
|
|
|
f2d475 |
{
|
|
|
f2d475 |
struct kern_ipc_perm *perm;
|
|
|
f2d475 |
int next_id;
|
|
|
f2d475 |
int total, in_use;
|
|
|
f2d475 |
|
|
|
f2d475 |
down_write(&ids->rwsem);
|
|
|
f2d475 |
|
|
|
f2d475 |
in_use = ids->in_use;
|
|
|
f2d475 |
|
|
|
f2d475 |
for (total = 0, next_id = 0; total < in_use; next_id++) {
|
|
|
f2d475 |
perm = idr_find(&ids->ipcs_idr, next_id);
|
|
|
f2d475 |
if (perm == NULL)
|
|
|
f2d475 |
continue;
|
|
|
f2d475 |
rcu_read_lock();
|
|
|
f2d475 |
ipc_lock_object(perm);
|
|
|
f2d475 |
free(ns, perm);
|
|
|
f2d475 |
total++;
|
|
|
f2d475 |
}
|
|
|
f2d475 |
up_write(&ids->rwsem);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static void free_ipc_ns(struct ipc_namespace *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
sem_exit_ns(ns);
|
|
|
f2d475 |
msg_exit_ns(ns);
|
|
|
f2d475 |
shm_exit_ns(ns);
|
|
|
f2d475 |
|
|
|
f2d475 |
dec_ipc_namespaces(ns->ucounts);
|
|
|
f2d475 |
put_user_ns(ns->user_ns);
|
|
|
f2d475 |
ns_free_inum(&ns->ns);
|
|
|
f2d475 |
kfree(ns);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
/*
|
|
|
f2d475 |
* put_ipc_ns - drop a reference to an ipc namespace.
|
|
|
f2d475 |
* @ns: the namespace to put
|
|
|
f2d475 |
*
|
|
|
f2d475 |
* If this is the last task in the namespace exiting, and
|
|
|
f2d475 |
* it is dropping the refcount to 0, then it can race with
|
|
|
f2d475 |
* a task in another ipc namespace but in a mounts namespace
|
|
|
f2d475 |
* which has this ipcns's mqueuefs mounted, doing some action
|
|
|
f2d475 |
* with one of the mqueuefs files. That can raise the refcount.
|
|
|
f2d475 |
* So dropping the refcount, and raising the refcount when
|
|
|
f2d475 |
* accessing it through the VFS, are protected with mq_lock.
|
|
|
f2d475 |
*
|
|
|
f2d475 |
* (Clearly, a task raising the refcount on its own ipc_ns
|
|
|
f2d475 |
* needn't take mq_lock since it can't race with the last task
|
|
|
f2d475 |
* in the ipcns exiting).
|
|
|
f2d475 |
*/
|
|
|
f2d475 |
void put_ipc_ns(struct ipc_namespace *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
|
|
|
f2d475 |
mq_clear_sbinfo(ns);
|
|
|
f2d475 |
spin_unlock(&mq_lock);
|
|
|
f2d475 |
mq_put_mnt(ns);
|
|
|
f2d475 |
free_ipc_ns(ns);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
return container_of(ns, struct ipc_namespace, ns);
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static struct ns_common *ipcns_get(struct task_struct *task)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
struct ipc_namespace *ns = NULL;
|
|
|
f2d475 |
struct nsproxy *nsproxy;
|
|
|
f2d475 |
|
|
|
f2d475 |
task_lock(task);
|
|
|
f2d475 |
nsproxy = task->nsproxy;
|
|
|
f2d475 |
if (nsproxy)
|
|
|
f2d475 |
ns = get_ipc_ns(nsproxy->ipc_ns);
|
|
|
f2d475 |
task_unlock(task);
|
|
|
f2d475 |
|
|
|
f2d475 |
return ns ? &ns->ns : NULL;
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static void ipcns_put(struct ns_common *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
return put_ipc_ns(to_ipc_ns(ns));
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static int ipcns_install(struct nsproxy *nsproxy, struct ns_common *new)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
struct ipc_namespace *ns = to_ipc_ns(new);
|
|
|
f2d475 |
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
|
|
|
f2d475 |
!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
|
|
|
f2d475 |
return -EPERM;
|
|
|
f2d475 |
|
|
|
f2d475 |
/* Ditch state from the old ipc namespace */
|
|
|
f2d475 |
exit_sem(current);
|
|
|
f2d475 |
put_ipc_ns(nsproxy->ipc_ns);
|
|
|
f2d475 |
nsproxy->ipc_ns = get_ipc_ns(ns);
|
|
|
f2d475 |
return 0;
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
static struct user_namespace *ipcns_owner(struct ns_common *ns)
|
|
|
f2d475 |
{
|
|
|
f2d475 |
return to_ipc_ns(ns)->user_ns;
|
|
|
f2d475 |
}
|
|
|
f2d475 |
|
|
|
f2d475 |
const struct proc_ns_operations ipcns_operations = {
|
|
|
f2d475 |
.name = "ipc",
|
|
|
f2d475 |
.type = CLONE_NEWIPC,
|
|
|
f2d475 |
.get = ipcns_get,
|
|
|
f2d475 |
.put = ipcns_put,
|
|
|
f2d475 |
.install = ipcns_install,
|
|
|
f2d475 |
.owner = ipcns_owner,
|
|
|
f2d475 |
};
|