Blame ipc/util.c

f2d475
// SPDX-License-Identifier: GPL-2.0
f2d475
/*
f2d475
 * linux/ipc/util.c
f2d475
 * Copyright (C) 1992 Krishna Balasubramanian
f2d475
 *
f2d475
 * Sep 1997 - Call suser() last after "normal" permission checks so we
f2d475
 *            get BSD style process accounting right.
f2d475
 *            Occurs in several places in the IPC code.
f2d475
 *            Chris Evans, <chris@ferret.lmh.ox.ac.uk>
f2d475
 * Nov 1999 - ipc helper functions, unified SMP locking
f2d475
 *	      Manfred Spraul <manfred@colorfullife.com>
f2d475
 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
f2d475
 *            Mingming Cao <cmm@us.ibm.com>
f2d475
 * Mar 2006 - support for audit of ipc object properties
f2d475
 *            Dustin Kirkland <dustin.kirkland@us.ibm.com>
f2d475
 * Jun 2006 - namespaces ssupport
f2d475
 *            OpenVZ, SWsoft Inc.
f2d475
 *            Pavel Emelianov <xemul@openvz.org>
f2d475
 *
f2d475
 * General sysv ipc locking scheme:
f2d475
 *	rcu_read_lock()
f2d475
 *          obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
f2d475
 *	    tree.
f2d475
 *	    - perform initial checks (capabilities, auditing and permission,
f2d475
 *	      etc).
f2d475
 *	    - perform read-only operations, such as INFO command, that
f2d475
 *	      do not demand atomicity
f2d475
 *	      acquire the ipc lock (kern_ipc_perm.lock) through
f2d475
 *	      ipc_lock_object()
f2d475
 *		- perform read-only operations that demand atomicity,
f2d475
 *		  such as STAT command.
f2d475
 *		- perform data updates, such as SET, RMID commands and
f2d475
 *		  mechanism-specific operations (semop/semtimedop,
f2d475
 *		  msgsnd/msgrcv, shmat/shmdt).
f2d475
 *	    drop the ipc lock, through ipc_unlock_object().
f2d475
 *	rcu_read_unlock()
f2d475
 *
f2d475
 *  The ids->rwsem must be taken when:
f2d475
 *	- creating, removing and iterating the existing entries in ipc
f2d475
 *	  identifier sets.
f2d475
 *	- iterating through files under /proc/sysvipc/
f2d475
 *
f2d475
 *  Note that sems have a special fast path that avoids kern_ipc_perm.lock -
f2d475
 *  see sem_lock().
f2d475
 */
f2d475
f2d475
#include <linux/mm.h>
f2d475
#include <linux/shm.h>
f2d475
#include <linux/init.h>
f2d475
#include <linux/msg.h>
f2d475
#include <linux/vmalloc.h>
f2d475
#include <linux/slab.h>
f2d475
#include <linux/notifier.h>
f2d475
#include <linux/capability.h>
f2d475
#include <linux/highuid.h>
f2d475
#include <linux/security.h>
f2d475
#include <linux/rcupdate.h>
f2d475
#include <linux/workqueue.h>
f2d475
#include <linux/seq_file.h>
f2d475
#include <linux/proc_fs.h>
f2d475
#include <linux/audit.h>
f2d475
#include <linux/nsproxy.h>
f2d475
#include <linux/rwsem.h>
f2d475
#include <linux/memory.h>
f2d475
#include <linux/ipc_namespace.h>
f2d475
#include <linux/rhashtable.h>
f2d475
f2d475
#include <asm/unistd.h>
f2d475
f2d475
#include "util.h"
f2d475
f2d475
struct ipc_proc_iface {
f2d475
	const char *path;
f2d475
	const char *header;
f2d475
	int ids;
f2d475
	int (*show)(struct seq_file *, void *);
f2d475
};
f2d475
f2d475
/**
f2d475
 * ipc_init - initialise ipc subsystem
f2d475
 *
f2d475
 * The various sysv ipc resources (semaphores, messages and shared
f2d475
 * memory) are initialised.
f2d475
 *
f2d475
 * A callback routine is registered into the memory hotplug notifier
f2d475
 * chain: since msgmni scales to lowmem this callback routine will be
f2d475
 * called upon successful memory add / remove to recompute msmgni.
f2d475
 */
f2d475
static int __init ipc_init(void)
f2d475
{
f2d475
	proc_mkdir("sysvipc", NULL);
f2d475
	sem_init();
f2d475
	msg_init();
f2d475
	shm_init();
f2d475
f2d475
	return 0;
f2d475
}
f2d475
device_initcall(ipc_init);
f2d475
f2d475
static const struct rhashtable_params ipc_kht_params = {
f2d475
	.head_offset		= offsetof(struct kern_ipc_perm, khtnode),
f2d475
	.key_offset		= offsetof(struct kern_ipc_perm, key),
f2d475
	.key_len		= FIELD_SIZEOF(struct kern_ipc_perm, key),
f2d475
	.locks_mul		= 1,
f2d475
	.automatic_shrinking	= true,
f2d475
};
f2d475
f2d475
/**
f2d475
 * ipc_init_ids	- initialise ipc identifiers
f2d475
 * @ids: ipc identifier set
f2d475
 *
f2d475
 * Set up the sequence range to use for the ipc identifier range (limited
f2d475
 * below ipc_mni) then initialise the keys hashtable and ids idr.
f2d475
 */
f2d475
void ipc_init_ids(struct ipc_ids *ids)
f2d475
{
f2d475
	ids->in_use = 0;
f2d475
	ids->seq = 0;
f2d475
	init_rwsem(&ids->rwsem);
f2d475
	rhashtable_init(&ids->key_ht, &ipc_kht_params);
f2d475
	idr_init(&ids->ipcs_idr);
f2d475
	ids->max_idx = -1;
f2d475
	ids->last_idx = -1;
f2d475
#ifdef CONFIG_CHECKPOINT_RESTORE
f2d475
	ids->next_id = -1;
f2d475
#endif
f2d475
}
f2d475
f2d475
#ifdef CONFIG_PROC_FS
f2d475
static const struct file_operations sysvipc_proc_fops;
f2d475
/**
f2d475
 * ipc_init_proc_interface -  create a proc interface for sysipc types using a seq_file interface.
f2d475
 * @path: Path in procfs
f2d475
 * @header: Banner to be printed at the beginning of the file.
f2d475
 * @ids: ipc id table to iterate.
f2d475
 * @show: show routine.
f2d475
 */
f2d475
void __init ipc_init_proc_interface(const char *path, const char *header,
f2d475
		int ids, int (*show)(struct seq_file *, void *))
f2d475
{
f2d475
	struct proc_dir_entry *pde;
f2d475
	struct ipc_proc_iface *iface;
f2d475
f2d475
	iface = kmalloc(sizeof(*iface), GFP_KERNEL);
f2d475
	if (!iface)
f2d475
		return;
f2d475
	iface->path	= path;
f2d475
	iface->header	= header;
f2d475
	iface->ids	= ids;
f2d475
	iface->show	= show;
f2d475
f2d475
	pde = proc_create_data(path,
f2d475
			       S_IRUGO,        /* world readable */
f2d475
			       NULL,           /* parent dir */
f2d475
			       &sysvipc_proc_fops,
f2d475
			       iface);
f2d475
	if (!pde)
f2d475
		kfree(iface);
f2d475
}
f2d475
#endif
f2d475
f2d475
/**
f2d475
 * ipc_findkey	- find a key in an ipc identifier set
f2d475
 * @ids: ipc identifier set
f2d475
 * @key: key to find
f2d475
 *
f2d475
 * Returns the locked pointer to the ipc structure if found or NULL
f2d475
 * otherwise. If key is found ipc points to the owning ipc structure
f2d475
 *
f2d475
 * Called with writer ipc_ids.rwsem held.
f2d475
 */
f2d475
static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
f2d475
{
f2d475
	struct kern_ipc_perm *ipcp;
f2d475
f2d475
	ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
f2d475
					      ipc_kht_params);
f2d475
	if (!ipcp)
f2d475
		return NULL;
f2d475
f2d475
	rcu_read_lock();
f2d475
	ipc_lock_object(ipcp);
f2d475
	return ipcp;
f2d475
}
f2d475
f2d475
/*
f2d475
 * Insert new IPC object into idr tree, and set sequence number and id
f2d475
 * in the correct order.
f2d475
 * Especially:
f2d475
 * - the sequence number must be set before inserting the object into the idr,
f2d475
 *   because the sequence number is accessed without a lock.
f2d475
 * - the id can/must be set after inserting the object into the idr.
f2d475
 *   All accesses must be done after getting kern_ipc_perm.lock.
f2d475
 *
f2d475
 * The caller must own kern_ipc_perm.lock.of the new object.
f2d475
 * On error, the function returns a (negative) error code.
f2d475
 *
f2d475
 * To conserve sequence number space, especially with extended ipc_mni,
f2d475
 * the sequence number is incremented only when the returned ID is less than
f2d475
 * the last one.
f2d475
 */
f2d475
static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
f2d475
{
f2d475
	int idx, next_id = -1;
f2d475
f2d475
#ifdef CONFIG_CHECKPOINT_RESTORE
f2d475
	next_id = ids->next_id;
f2d475
	ids->next_id = -1;
f2d475
#endif
f2d475
f2d475
	/*
f2d475
	 * As soon as a new object is inserted into the idr,
f2d475
	 * ipc_obtain_object_idr() or ipc_obtain_object_check() can find it,
f2d475
	 * and the lockless preparations for ipc operations can start.
f2d475
	 * This means especially: permission checks, audit calls, allocation
f2d475
	 * of undo structures, ...
f2d475
	 *
f2d475
	 * Thus the object must be fully initialized, and if something fails,
f2d475
	 * then the full tear-down sequence must be followed.
f2d475
	 * (i.e.: set new->deleted, reduce refcount, call_rcu())
f2d475
	 */
f2d475
f2d475
	if (next_id < 0) { /* !CHECKPOINT_RESTORE or next_id is unset */
f2d475
		int max_idx;
f2d475
f2d475
		max_idx = max(ids->in_use*3/2, ipc_min_cycle);
f2d475
		max_idx = min(max_idx, ipc_mni);
f2d475
f2d475
		/* allocate the idx, with a NULL struct kern_ipc_perm */
f2d475
		idx = idr_alloc_cyclic(&ids->ipcs_idr, NULL, 0, max_idx,
f2d475
					GFP_NOWAIT);
f2d475
f2d475
		if (idx >= 0) {
f2d475
			/*
f2d475
			 * idx got allocated successfully.
f2d475
			 * Now calculate the sequence number and set the
f2d475
			 * pointer for real.
f2d475
			 */
f2d475
			if (idx <= ids->last_idx) {
f2d475
				ids->seq++;
f2d475
				if (ids->seq >= ipcid_seq_max())
f2d475
					ids->seq = 0;
f2d475
			}
f2d475
			ids->last_idx = idx;
f2d475
f2d475
			new->seq = ids->seq;
f2d475
			/* no need for smp_wmb(), this is done
f2d475
			 * inside idr_replace, as part of
f2d475
			 * rcu_assign_pointer
f2d475
			 */
f2d475
			idr_replace(&ids->ipcs_idr, new, idx);
f2d475
		}
f2d475
	} else {
f2d475
		new->seq = ipcid_to_seqx(next_id);
f2d475
		idx = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id),
f2d475
				0, GFP_NOWAIT);
f2d475
	}
f2d475
	if (idx >= 0)
f2d475
		new->id = (new->seq << ipcmni_seq_shift()) + idx;
f2d475
	return idx;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_addid - add an ipc identifier
f2d475
 * @ids: ipc identifier set
f2d475
 * @new: new ipc permission set
f2d475
 * @limit: limit for the number of used ids
f2d475
 *
f2d475
 * Add an entry 'new' to the ipc ids idr. The permissions object is
f2d475
 * initialised and the first free entry is set up and the index assigned
f2d475
 * is returned. The 'new' entry is returned in a locked state on success.
f2d475
 *
f2d475
 * On failure the entry is not locked and a negative err-code is returned.
f2d475
 * The caller must use ipc_rcu_putref() to free the identifier.
f2d475
 *
f2d475
 * Called with writer ipc_ids.rwsem held.
f2d475
 */
f2d475
int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
f2d475
{
f2d475
	kuid_t euid;
f2d475
	kgid_t egid;
f2d475
	int idx, err;
f2d475
f2d475
	/* 1) Initialize the refcount so that ipc_rcu_putref works */
f2d475
	refcount_set(&new->refcount, 1);
f2d475
f2d475
	if (limit > ipc_mni)
f2d475
		limit = ipc_mni;
f2d475
f2d475
	if (ids->in_use >= limit)
f2d475
		return -ENOSPC;
f2d475
f2d475
	idr_preload(GFP_KERNEL);
f2d475
f2d475
	spin_lock_init(&new->lock);
f2d475
	rcu_read_lock();
f2d475
	spin_lock(&new->lock);
f2d475
f2d475
	current_euid_egid(&euid, &egid);
f2d475
	new->cuid = new->uid = euid;
f2d475
	new->gid = new->cgid = egid;
f2d475
f2d475
	new->deleted = false;
f2d475
f2d475
	idx = ipc_idr_alloc(ids, new);
f2d475
	idr_preload_end();
f2d475
f2d475
	if (idx >= 0 && new->key != IPC_PRIVATE) {
f2d475
		err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
f2d475
					     ipc_kht_params);
f2d475
		if (err < 0) {
f2d475
			idr_remove(&ids->ipcs_idr, idx);
f2d475
			idx = err;
f2d475
		}
f2d475
	}
f2d475
	if (idx < 0) {
f2d475
		new->deleted = true;
f2d475
		spin_unlock(&new->lock);
f2d475
		rcu_read_unlock();
f2d475
		return idx;
f2d475
	}
f2d475
f2d475
	ids->in_use++;
f2d475
	if (idx > ids->max_idx)
f2d475
		ids->max_idx = idx;
f2d475
	return idx;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipcget_new -	create a new ipc object
f2d475
 * @ns: ipc namespace
f2d475
 * @ids: ipc identifier set
f2d475
 * @ops: the actual creation routine to call
f2d475
 * @params: its parameters
f2d475
 *
f2d475
 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
f2d475
 * when the key is IPC_PRIVATE.
f2d475
 */
f2d475
static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
f2d475
		const struct ipc_ops *ops, struct ipc_params *params)
f2d475
{
f2d475
	int err;
f2d475
f2d475
	down_write(&ids->rwsem);
f2d475
	err = ops->getnew(ns, params);
f2d475
	up_write(&ids->rwsem);
f2d475
	return err;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_check_perms - check security and permissions for an ipc object
f2d475
 * @ns: ipc namespace
f2d475
 * @ipcp: ipc permission set
f2d475
 * @ops: the actual security routine to call
f2d475
 * @params: its parameters
f2d475
 *
f2d475
 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
f2d475
 * when the key is not IPC_PRIVATE and that key already exists in the
f2d475
 * ds IDR.
f2d475
 *
f2d475
 * On success, the ipc id is returned.
f2d475
 *
f2d475
 * It is called with ipc_ids.rwsem and ipcp->lock held.
f2d475
 */
f2d475
static int ipc_check_perms(struct ipc_namespace *ns,
f2d475
			   struct kern_ipc_perm *ipcp,
f2d475
			   const struct ipc_ops *ops,
f2d475
			   struct ipc_params *params)
f2d475
{
f2d475
	int err;
f2d475
f2d475
	if (ipcperms(ns, ipcp, params->flg))
f2d475
		err = -EACCES;
f2d475
	else {
f2d475
		err = ops->associate(ipcp, params->flg);
f2d475
		if (!err)
f2d475
			err = ipcp->id;
f2d475
	}
f2d475
f2d475
	return err;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipcget_public - get an ipc object or create a new one
f2d475
 * @ns: ipc namespace
f2d475
 * @ids: ipc identifier set
f2d475
 * @ops: the actual creation routine to call
f2d475
 * @params: its parameters
f2d475
 *
f2d475
 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
f2d475
 * when the key is not IPC_PRIVATE.
f2d475
 * It adds a new entry if the key is not found and does some permission
f2d475
 * / security checkings if the key is found.
f2d475
 *
f2d475
 * On success, the ipc id is returned.
f2d475
 */
f2d475
static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
f2d475
		const struct ipc_ops *ops, struct ipc_params *params)
f2d475
{
f2d475
	struct kern_ipc_perm *ipcp;
f2d475
	int flg = params->flg;
f2d475
	int err;
f2d475
f2d475
	/*
f2d475
	 * Take the lock as a writer since we are potentially going to add
f2d475
	 * a new entry + read locks are not "upgradable"
f2d475
	 */
f2d475
	down_write(&ids->rwsem);
f2d475
	ipcp = ipc_findkey(ids, params->key);
f2d475
	if (ipcp == NULL) {
f2d475
		/* key not used */
f2d475
		if (!(flg & IPC_CREAT))
f2d475
			err = -ENOENT;
f2d475
		else
f2d475
			err = ops->getnew(ns, params);
f2d475
	} else {
f2d475
		/* ipc object has been locked by ipc_findkey() */
f2d475
f2d475
		if (flg & IPC_CREAT && flg & IPC_EXCL)
f2d475
			err = -EEXIST;
f2d475
		else {
f2d475
			err = 0;
f2d475
			if (ops->more_checks)
f2d475
				err = ops->more_checks(ipcp, params);
f2d475
			if (!err)
f2d475
				/*
f2d475
				 * ipc_check_perms returns the IPC id on
f2d475
				 * success
f2d475
				 */
f2d475
				err = ipc_check_perms(ns, ipcp, ops, params);
f2d475
		}
f2d475
		ipc_unlock(ipcp);
f2d475
	}
f2d475
	up_write(&ids->rwsem);
f2d475
f2d475
	return err;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_kht_remove - remove an ipc from the key hashtable
f2d475
 * @ids: ipc identifier set
f2d475
 * @ipcp: ipc perm structure containing the key to remove
f2d475
 *
f2d475
 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
f2d475
 * before this function is called, and remain locked on the exit.
f2d475
 */
f2d475
static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
f2d475
{
f2d475
	if (ipcp->key != IPC_PRIVATE)
f2d475
		rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
f2d475
				       ipc_kht_params);
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_rmid - remove an ipc identifier
f2d475
 * @ids: ipc identifier set
f2d475
 * @ipcp: ipc perm structure containing the identifier to remove
f2d475
 *
f2d475
 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
f2d475
 * before this function is called, and remain locked on the exit.
f2d475
 */
f2d475
void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
f2d475
{
f2d475
	int idx = ipcid_to_idx(ipcp->id);
f2d475
f2d475
	idr_remove(&ids->ipcs_idr, idx);
f2d475
	ipc_kht_remove(ids, ipcp);
f2d475
	ids->in_use--;
f2d475
	ipcp->deleted = true;
f2d475
f2d475
	if (unlikely(idx == ids->max_idx)) {
f2d475
		do {
f2d475
			idx--;
f2d475
			if (idx == -1)
f2d475
				break;
f2d475
		} while (!idr_find(&ids->ipcs_idr, idx));
f2d475
		ids->max_idx = idx;
f2d475
	}
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
f2d475
 * @ids: ipc identifier set
f2d475
 * @ipcp: ipc perm structure containing the key to modify
f2d475
 *
f2d475
 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
f2d475
 * before this function is called, and remain locked on the exit.
f2d475
 */
f2d475
void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
f2d475
{
f2d475
	ipc_kht_remove(ids, ipcp);
f2d475
	ipcp->key = IPC_PRIVATE;
f2d475
}
f2d475
f2d475
bool ipc_rcu_getref(struct kern_ipc_perm *ptr)
f2d475
{
f2d475
	return refcount_inc_not_zero(&ptr->refcount);
f2d475
}
f2d475
f2d475
void ipc_rcu_putref(struct kern_ipc_perm *ptr,
f2d475
			void (*func)(struct rcu_head *head))
f2d475
{
f2d475
	if (!refcount_dec_and_test(&ptr->refcount))
f2d475
		return;
f2d475
f2d475
	call_rcu(&ptr->rcu, func);
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipcperms - check ipc permissions
f2d475
 * @ns: ipc namespace
f2d475
 * @ipcp: ipc permission set
f2d475
 * @flag: desired permission set
f2d475
 *
f2d475
 * Check user, group, other permissions for access
f2d475
 * to ipc resources. return 0 if allowed
f2d475
 *
f2d475
 * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
f2d475
 */
f2d475
int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
f2d475
{
f2d475
	kuid_t euid = current_euid();
f2d475
	int requested_mode, granted_mode;
f2d475
f2d475
	audit_ipc_obj(ipcp);
f2d475
	requested_mode = (flag >> 6) | (flag >> 3) | flag;
f2d475
	granted_mode = ipcp->mode;
f2d475
	if (uid_eq(euid, ipcp->cuid) ||
f2d475
	    uid_eq(euid, ipcp->uid))
f2d475
		granted_mode >>= 6;
f2d475
	else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
f2d475
		granted_mode >>= 3;
f2d475
	/* is there some bit set in requested_mode but not in granted_mode? */
f2d475
	if ((requested_mode & ~granted_mode & 0007) &&
f2d475
	    !ns_capable(ns->user_ns, CAP_IPC_OWNER))
f2d475
		return -1;
f2d475
f2d475
	return security_ipc_permission(ipcp, flag);
f2d475
}
f2d475
f2d475
/*
f2d475
 * Functions to convert between the kern_ipc_perm structure and the
f2d475
 * old/new ipc_perm structures
f2d475
 */
f2d475
f2d475
/**
f2d475
 * kernel_to_ipc64_perm	- convert kernel ipc permissions to user
f2d475
 * @in: kernel permissions
f2d475
 * @out: new style ipc permissions
f2d475
 *
f2d475
 * Turn the kernel object @in into a set of permissions descriptions
f2d475
 * for returning to userspace (@out).
f2d475
 */
f2d475
void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
f2d475
{
f2d475
	out->key	= in->key;
f2d475
	out->uid	= from_kuid_munged(current_user_ns(), in->uid);
f2d475
	out->gid	= from_kgid_munged(current_user_ns(), in->gid);
f2d475
	out->cuid	= from_kuid_munged(current_user_ns(), in->cuid);
f2d475
	out->cgid	= from_kgid_munged(current_user_ns(), in->cgid);
f2d475
	out->mode	= in->mode;
f2d475
	out->seq	= in->seq;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
f2d475
 * @in: new style ipc permissions
f2d475
 * @out: old style ipc permissions
f2d475
 *
f2d475
 * Turn the new style permissions object @in into a compatibility
f2d475
 * object and store it into the @out pointer.
f2d475
 */
f2d475
void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
f2d475
{
f2d475
	out->key	= in->key;
f2d475
	SET_UID(out->uid, in->uid);
f2d475
	SET_GID(out->gid, in->gid);
f2d475
	SET_UID(out->cuid, in->cuid);
f2d475
	SET_GID(out->cgid, in->cgid);
f2d475
	out->mode	= in->mode;
f2d475
	out->seq	= in->seq;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_obtain_object_idr
f2d475
 * @ids: ipc identifier set
f2d475
 * @id: ipc id to look for
f2d475
 *
f2d475
 * Look for an id in the ipc ids idr and return associated ipc object.
f2d475
 *
f2d475
 * Call inside the RCU critical section.
f2d475
 * The ipc object is *not* locked on exit.
f2d475
 */
f2d475
struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
f2d475
{
f2d475
	struct kern_ipc_perm *out;
f2d475
	int idx = ipcid_to_idx(id);
f2d475
f2d475
	out = idr_find(&ids->ipcs_idr, idx);
f2d475
	if (!out)
f2d475
		return ERR_PTR(-EINVAL);
f2d475
f2d475
	return out;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_obtain_object_check
f2d475
 * @ids: ipc identifier set
f2d475
 * @id: ipc id to look for
f2d475
 *
f2d475
 * Similar to ipc_obtain_object_idr() but also checks the ipc object
f2d475
 * sequence number.
f2d475
 *
f2d475
 * Call inside the RCU critical section.
f2d475
 * The ipc object is *not* locked on exit.
f2d475
 */
f2d475
struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
f2d475
{
f2d475
	struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
f2d475
f2d475
	if (IS_ERR(out))
f2d475
		goto out;
f2d475
f2d475
	if (ipc_checkid(out, id))
f2d475
		return ERR_PTR(-EINVAL);
f2d475
out:
f2d475
	return out;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipcget - Common sys_*get() code
f2d475
 * @ns: namespace
f2d475
 * @ids: ipc identifier set
f2d475
 * @ops: operations to be called on ipc object creation, permission checks
f2d475
 *       and further checks
f2d475
 * @params: the parameters needed by the previous operations.
f2d475
 *
f2d475
 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
f2d475
 */
f2d475
int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
f2d475
			const struct ipc_ops *ops, struct ipc_params *params)
f2d475
{
f2d475
	if (params->key == IPC_PRIVATE)
f2d475
		return ipcget_new(ns, ids, ops, params);
f2d475
	else
f2d475
		return ipcget_public(ns, ids, ops, params);
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipc_update_perm - update the permissions of an ipc object
f2d475
 * @in:  the permission given as input.
f2d475
 * @out: the permission of the ipc to set.
f2d475
 */
f2d475
int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
f2d475
{
f2d475
	kuid_t uid = make_kuid(current_user_ns(), in->uid);
f2d475
	kgid_t gid = make_kgid(current_user_ns(), in->gid);
f2d475
	if (!uid_valid(uid) || !gid_valid(gid))
f2d475
		return -EINVAL;
f2d475
f2d475
	out->uid = uid;
f2d475
	out->gid = gid;
f2d475
	out->mode = (out->mode & ~S_IRWXUGO)
f2d475
		| (in->mode & S_IRWXUGO);
f2d475
f2d475
	return 0;
f2d475
}
f2d475
f2d475
/**
f2d475
 * ipcctl_obtain_check - retrieve an ipc object and check permissions
f2d475
 * @ns:  ipc namespace
f2d475
 * @ids:  the table of ids where to look for the ipc
f2d475
 * @id:   the id of the ipc to retrieve
f2d475
 * @cmd:  the cmd to check
f2d475
 * @perm: the permission to set
f2d475
 * @extra_perm: one extra permission parameter used by msq
f2d475
 *
f2d475
 * This function does some common audit and permissions check for some IPC_XXX
f2d475
 * cmd and is called from semctl_down, shmctl_down and msgctl_down.
f2d475
 *
f2d475
 * It:
f2d475
 *   - retrieves the ipc object with the given id in the given table.
f2d475
 *   - performs some audit and permission check, depending on the given cmd
f2d475
 *   - returns a pointer to the ipc object or otherwise, the corresponding
f2d475
 *     error.
f2d475
 *
f2d475
 * Call holding the both the rwsem and the rcu read lock.
f2d475
 */
f2d475
struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
f2d475
					struct ipc_ids *ids, int id, int cmd,
f2d475
					struct ipc64_perm *perm, int extra_perm)
f2d475
{
f2d475
	kuid_t euid;
f2d475
	int err = -EPERM;
f2d475
	struct kern_ipc_perm *ipcp;
f2d475
f2d475
	ipcp = ipc_obtain_object_check(ids, id);
f2d475
	if (IS_ERR(ipcp)) {
f2d475
		err = PTR_ERR(ipcp);
f2d475
		goto err;
f2d475
	}
f2d475
f2d475
	audit_ipc_obj(ipcp);
f2d475
	if (cmd == IPC_SET)
f2d475
		audit_ipc_set_perm(extra_perm, perm->uid,
f2d475
				   perm->gid, perm->mode);
f2d475
f2d475
	euid = current_euid();
f2d475
	if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid)  ||
f2d475
	    ns_capable(ns->user_ns, CAP_SYS_ADMIN))
f2d475
		return ipcp; /* successful lookup */
f2d475
err:
f2d475
	return ERR_PTR(err);
f2d475
}
f2d475
f2d475
#ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
f2d475
f2d475
f2d475
/**
f2d475
 * ipc_parse_version - ipc call version
f2d475
 * @cmd: pointer to command
f2d475
 *
f2d475
 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
f2d475
 * The @cmd value is turned from an encoding command and version into
f2d475
 * just the command code.
f2d475
 */
f2d475
int ipc_parse_version(int *cmd)
f2d475
{
f2d475
	if (*cmd & IPC_64) {
f2d475
		*cmd ^= IPC_64;
f2d475
		return IPC_64;
f2d475
	} else {
f2d475
		return IPC_OLD;
f2d475
	}
f2d475
}
f2d475
f2d475
#endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
f2d475
f2d475
#ifdef CONFIG_PROC_FS
f2d475
struct ipc_proc_iter {
f2d475
	struct ipc_namespace *ns;
f2d475
	struct pid_namespace *pid_ns;
f2d475
	struct ipc_proc_iface *iface;
f2d475
};
f2d475
f2d475
struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s)
f2d475
{
f2d475
	struct ipc_proc_iter *iter = s->private;
f2d475
	return iter->pid_ns;
f2d475
}
f2d475
f2d475
/*
f2d475
 * This routine locks the ipc structure found at least at position pos.
f2d475
 */
f2d475
static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
f2d475
					      loff_t *new_pos)
f2d475
{
f2d475
	struct kern_ipc_perm *ipc;
f2d475
	int total, id;
f2d475
f2d475
	total = 0;
f2d475
	for (id = 0; id < pos && total < ids->in_use; id++) {
f2d475
		ipc = idr_find(&ids->ipcs_idr, id);
f2d475
		if (ipc != NULL)
f2d475
			total++;
f2d475
	}
f2d475
f2d475
	if (total >= ids->in_use)
f2d475
		return NULL;
f2d475
f2d475
	for (; pos < ipc_mni; pos++) {
f2d475
		ipc = idr_find(&ids->ipcs_idr, pos);
f2d475
		if (ipc != NULL) {
f2d475
			*new_pos = pos + 1;
f2d475
			rcu_read_lock();
f2d475
			ipc_lock_object(ipc);
f2d475
			return ipc;
f2d475
		}
f2d475
	}
f2d475
f2d475
	/* Out of range - return NULL to terminate iteration */
f2d475
	return NULL;
f2d475
}
f2d475
f2d475
static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
f2d475
{
f2d475
	struct ipc_proc_iter *iter = s->private;
f2d475
	struct ipc_proc_iface *iface = iter->iface;
f2d475
	struct kern_ipc_perm *ipc = it;
f2d475
f2d475
	/* If we had an ipc id locked before, unlock it */
f2d475
	if (ipc && ipc != SEQ_START_TOKEN)
f2d475
		ipc_unlock(ipc);
f2d475
f2d475
	return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
f2d475
}
f2d475
f2d475
/*
f2d475
 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
f2d475
 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
f2d475
 */
f2d475
static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
f2d475
{
f2d475
	struct ipc_proc_iter *iter = s->private;
f2d475
	struct ipc_proc_iface *iface = iter->iface;
f2d475
	struct ipc_ids *ids;
f2d475
f2d475
	ids = &iter->ns->ids[iface->ids];
f2d475
f2d475
	/*
f2d475
	 * Take the lock - this will be released by the corresponding
f2d475
	 * call to stop().
f2d475
	 */
f2d475
	down_read(&ids->rwsem);
f2d475
f2d475
	/* pos < 0 is invalid */
f2d475
	if (*pos < 0)
f2d475
		return NULL;
f2d475
f2d475
	/* pos == 0 means header */
f2d475
	if (*pos == 0)
f2d475
		return SEQ_START_TOKEN;
f2d475
f2d475
	/* Find the (pos-1)th ipc */
f2d475
	return sysvipc_find_ipc(ids, *pos - 1, pos);
f2d475
}
f2d475
f2d475
static void sysvipc_proc_stop(struct seq_file *s, void *it)
f2d475
{
f2d475
	struct kern_ipc_perm *ipc = it;
f2d475
	struct ipc_proc_iter *iter = s->private;
f2d475
	struct ipc_proc_iface *iface = iter->iface;
f2d475
	struct ipc_ids *ids;
f2d475
f2d475
	/* If we had a locked structure, release it */
f2d475
	if (ipc && ipc != SEQ_START_TOKEN)
f2d475
		ipc_unlock(ipc);
f2d475
f2d475
	ids = &iter->ns->ids[iface->ids];
f2d475
	/* Release the lock we took in start() */
f2d475
	up_read(&ids->rwsem);
f2d475
}
f2d475
f2d475
static int sysvipc_proc_show(struct seq_file *s, void *it)
f2d475
{
f2d475
	struct ipc_proc_iter *iter = s->private;
f2d475
	struct ipc_proc_iface *iface = iter->iface;
f2d475
f2d475
	if (it == SEQ_START_TOKEN) {
f2d475
		seq_puts(s, iface->header);
f2d475
		return 0;
f2d475
	}
f2d475
f2d475
	return iface->show(s, it);
f2d475
}
f2d475
f2d475
static const struct seq_operations sysvipc_proc_seqops = {
f2d475
	.start = sysvipc_proc_start,
f2d475
	.stop  = sysvipc_proc_stop,
f2d475
	.next  = sysvipc_proc_next,
f2d475
	.show  = sysvipc_proc_show,
f2d475
};
f2d475
f2d475
static int sysvipc_proc_open(struct inode *inode, struct file *file)
f2d475
{
f2d475
	struct ipc_proc_iter *iter;
f2d475
f2d475
	iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
f2d475
	if (!iter)
f2d475
		return -ENOMEM;
f2d475
f2d475
	iter->iface = PDE_DATA(inode);
f2d475
	iter->ns    = get_ipc_ns(current->nsproxy->ipc_ns);
f2d475
	iter->pid_ns = get_pid_ns(task_active_pid_ns(current));
f2d475
f2d475
	return 0;
f2d475
}
f2d475
f2d475
static int sysvipc_proc_release(struct inode *inode, struct file *file)
f2d475
{
f2d475
	struct seq_file *seq = file->private_data;
f2d475
	struct ipc_proc_iter *iter = seq->private;
f2d475
	put_ipc_ns(iter->ns);
f2d475
	put_pid_ns(iter->pid_ns);
f2d475
	return seq_release_private(inode, file);
f2d475
}
f2d475
f2d475
static const struct file_operations sysvipc_proc_fops = {
f2d475
	.open    = sysvipc_proc_open,
f2d475
	.read    = seq_read,
f2d475
	.llseek  = seq_lseek,
f2d475
	.release = sysvipc_proc_release,
f2d475
};
f2d475
#endif /* CONFIG_PROC_FS */