xref: /openbmc/linux/ipc/util.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/ipc/util.c
41da177e4SLinus Torvalds  * Copyright (C) 1992 Krishna Balasubramanian
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Sep 1997 - Call suser() last after "normal" permission checks so we
71da177e4SLinus Torvalds  *            get BSD style process accounting right.
81da177e4SLinus Torvalds  *            Occurs in several places in the IPC code.
91da177e4SLinus Torvalds  *            Chris Evans, <chris@ferret.lmh.ox.ac.uk>
101da177e4SLinus Torvalds  * Nov 1999 - ipc helper functions, unified SMP locking
11624dffcbSChristian Kujau  *	      Manfred Spraul <manfred@colorfullife.com>
121da177e4SLinus Torvalds  * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
131da177e4SLinus Torvalds  *            Mingming Cao <cmm@us.ibm.com>
14073115d6SSteve Grubb  * Mar 2006 - support for audit of ipc object properties
15073115d6SSteve Grubb  *            Dustin Kirkland <dustin.kirkland@us.ibm.com>
1673ea4130SKirill Korotaev  * Jun 2006 - namespaces ssupport
1773ea4130SKirill Korotaev  *            OpenVZ, SWsoft Inc.
1873ea4130SKirill Korotaev  *            Pavel Emelianov <xemul@openvz.org>
1905603c44SDavidlohr Bueso  *
2005603c44SDavidlohr Bueso  * General sysv ipc locking scheme:
2105603c44SDavidlohr Bueso  *	rcu_read_lock()
2218ccee26SDavidlohr Bueso  *          obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
2318ccee26SDavidlohr Bueso  *	    tree.
2418ccee26SDavidlohr Bueso  *	    - perform initial checks (capabilities, auditing and permission,
2518ccee26SDavidlohr Bueso  *	      etc).
2687ad4b0dSPhilippe Mikoyan  *	    - perform read-only operations, such as INFO command, that
2787ad4b0dSPhilippe Mikoyan  *	      do not demand atomicity
2818ccee26SDavidlohr Bueso  *	      acquire the ipc lock (kern_ipc_perm.lock) through
2918ccee26SDavidlohr Bueso  *	      ipc_lock_object()
3087ad4b0dSPhilippe Mikoyan  *		- perform read-only operations that demand atomicity,
3187ad4b0dSPhilippe Mikoyan  *		  such as STAT command.
3218ccee26SDavidlohr Bueso  *		- perform data updates, such as SET, RMID commands and
3318ccee26SDavidlohr Bueso  *		  mechanism-specific operations (semop/semtimedop,
3418ccee26SDavidlohr Bueso  *		  msgsnd/msgrcv, shmat/shmdt).
3518ccee26SDavidlohr Bueso  *	    drop the ipc lock, through ipc_unlock_object().
3618ccee26SDavidlohr Bueso  *	rcu_read_unlock()
3718ccee26SDavidlohr Bueso  *
3818ccee26SDavidlohr Bueso  *  The ids->rwsem must be taken when:
3918ccee26SDavidlohr Bueso  *	- creating, removing and iterating the existing entries in ipc
4018ccee26SDavidlohr Bueso  *	  identifier sets.
4118ccee26SDavidlohr Bueso  *	- iterating through files under /proc/sysvipc/
4218ccee26SDavidlohr Bueso  *
4318ccee26SDavidlohr Bueso  *  Note that sems have a special fast path that avoids kern_ipc_perm.lock -
4418ccee26SDavidlohr Bueso  *  see sem_lock().
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <linux/mm.h>
481da177e4SLinus Torvalds #include <linux/shm.h>
491da177e4SLinus Torvalds #include <linux/init.h>
501da177e4SLinus Torvalds #include <linux/msg.h>
511da177e4SLinus Torvalds #include <linux/vmalloc.h>
521da177e4SLinus Torvalds #include <linux/slab.h>
538f68fa2dSAndrew Morton #include <linux/notifier.h>
54c59ede7bSRandy.Dunlap #include <linux/capability.h>
551da177e4SLinus Torvalds #include <linux/highuid.h>
561da177e4SLinus Torvalds #include <linux/security.h>
571da177e4SLinus Torvalds #include <linux/rcupdate.h>
581da177e4SLinus Torvalds #include <linux/workqueue.h>
59ae781774SMike Waychison #include <linux/seq_file.h>
60ae781774SMike Waychison #include <linux/proc_fs.h>
61073115d6SSteve Grubb #include <linux/audit.h>
6273ea4130SKirill Korotaev #include <linux/nsproxy.h>
633e148c79SNadia Derbey #include <linux/rwsem.h>
64b6b337adSNadia Derbey #include <linux/memory.h>
65ae5e1b22SPavel Emelyanov #include <linux/ipc_namespace.h>
660eb71a9dSNeilBrown #include <linux/rhashtable.h>
67b869d5beSManfred Spraul #include <linux/log2.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <asm/unistd.h>
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #include "util.h"
721da177e4SLinus Torvalds 
73ae781774SMike Waychison struct ipc_proc_iface {
74ae781774SMike Waychison 	const char *path;
75ae781774SMike Waychison 	const char *header;
7673ea4130SKirill Korotaev 	int ids;
77ae781774SMike Waychison 	int (*show)(struct seq_file *, void *);
78ae781774SMike Waychison };
79ae781774SMike Waychison 
801da177e4SLinus Torvalds /**
818001c858SDavidlohr Bueso  * ipc_init - initialise ipc subsystem
821da177e4SLinus Torvalds  *
838001c858SDavidlohr Bueso  * The various sysv ipc resources (semaphores, messages and shared
848001c858SDavidlohr Bueso  * memory) are initialised.
858001c858SDavidlohr Bueso  *
86b6b337adSNadia Derbey  * A callback routine is registered into the memory hotplug notifier
87b6b337adSNadia Derbey  * chain: since msgmni scales to lowmem this callback routine will be
88b6b337adSNadia Derbey  * called upon successful memory add / remove to recompute msmgni.
891da177e4SLinus Torvalds  */
ipc_init(void)901da177e4SLinus Torvalds static int __init ipc_init(void)
911da177e4SLinus Torvalds {
92e74a0effSAlexey Dobriyan 	proc_mkdir("sysvipc", NULL);
93eae04d25SDavidlohr Bueso 	sem_init();
94eae04d25SDavidlohr Bueso 	msg_init();
951da177e4SLinus Torvalds 	shm_init();
960cfb6aeeSGuillaume Knispel 
97eae04d25SDavidlohr Bueso 	return 0;
981da177e4SLinus Torvalds }
996d08a256SDavidlohr Bueso device_initcall(ipc_init);
1001da177e4SLinus Torvalds 
1010cfb6aeeSGuillaume Knispel static const struct rhashtable_params ipc_kht_params = {
1020cfb6aeeSGuillaume Knispel 	.head_offset		= offsetof(struct kern_ipc_perm, khtnode),
1030cfb6aeeSGuillaume Knispel 	.key_offset		= offsetof(struct kern_ipc_perm, key),
104c593642cSPankaj Bharadiya 	.key_len		= sizeof_field(struct kern_ipc_perm, key),
1050cfb6aeeSGuillaume Knispel 	.automatic_shrinking	= true,
1060cfb6aeeSGuillaume Knispel };
1070cfb6aeeSGuillaume Knispel 
1081da177e4SLinus Torvalds /**
1098001c858SDavidlohr Bueso  * ipc_init_ids	- initialise ipc identifiers
1108001c858SDavidlohr Bueso  * @ids: ipc identifier set
1111da177e4SLinus Torvalds  *
1127ca7e564SNadia Derbey  * Set up the sequence range to use for the ipc identifier range (limited
1135ac893b8SWaiman Long  * below ipc_mni) then initialise the keys hashtable and ids idr.
1141da177e4SLinus Torvalds  */
ipc_init_ids(struct ipc_ids * ids)115eae04d25SDavidlohr Bueso void ipc_init_ids(struct ipc_ids *ids)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	ids->in_use = 0;
1181da177e4SLinus Torvalds 	ids->seq = 0;
119daf948c7SDavidlohr Bueso 	init_rwsem(&ids->rwsem);
120eae04d25SDavidlohr Bueso 	rhashtable_init(&ids->key_ht, &ipc_kht_params);
1217ca7e564SNadia Derbey 	idr_init(&ids->ipcs_idr);
12227c331a1SManfred Spraul 	ids->max_idx = -1;
1233278a2c2SManfred Spraul 	ids->last_idx = -1;
124b8fd9983SDavidlohr Bueso #ifdef CONFIG_CHECKPOINT_RESTORE
125b8fd9983SDavidlohr Bueso 	ids->next_id = -1;
126b8fd9983SDavidlohr Bueso #endif
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129ae781774SMike Waychison #ifdef CONFIG_PROC_FS
13097a32539SAlexey Dobriyan static const struct proc_ops sysvipc_proc_ops;
131ae781774SMike Waychison /**
1328001c858SDavidlohr Bueso  * ipc_init_proc_interface -  create a proc interface for sysipc types using a seq_file interface.
133ae781774SMike Waychison  * @path: Path in procfs
134ae781774SMike Waychison  * @header: Banner to be printed at the beginning of the file.
135ae781774SMike Waychison  * @ids: ipc id table to iterate.
136ae781774SMike Waychison  * @show: show routine.
137ae781774SMike Waychison  */
ipc_init_proc_interface(const char * path,const char * header,int ids,int (* show)(struct seq_file *,void *))138ae781774SMike Waychison void __init ipc_init_proc_interface(const char *path, const char *header,
13973ea4130SKirill Korotaev 		int ids, int (*show)(struct seq_file *, void *))
140ae781774SMike Waychison {
141ae781774SMike Waychison 	struct proc_dir_entry *pde;
142ae781774SMike Waychison 	struct ipc_proc_iface *iface;
143ae781774SMike Waychison 
144ae781774SMike Waychison 	iface = kmalloc(sizeof(*iface), GFP_KERNEL);
145ae781774SMike Waychison 	if (!iface)
146ae781774SMike Waychison 		return;
147ae781774SMike Waychison 	iface->path	= path;
148ae781774SMike Waychison 	iface->header	= header;
149ae781774SMike Waychison 	iface->ids	= ids;
150ae781774SMike Waychison 	iface->show	= show;
151ae781774SMike Waychison 
1526a6375dbSDenis V. Lunev 	pde = proc_create_data(path,
153ae781774SMike Waychison 			       S_IRUGO,        /* world readable */
1546a6375dbSDenis V. Lunev 			       NULL,           /* parent dir */
15597a32539SAlexey Dobriyan 			       &sysvipc_proc_ops,
1566a6375dbSDenis V. Lunev 			       iface);
1573ab08fe2SDavidlohr Bueso 	if (!pde)
158ae781774SMike Waychison 		kfree(iface);
159ae781774SMike Waychison }
160ae781774SMike Waychison #endif
161ae781774SMike Waychison 
1621da177e4SLinus Torvalds /**
1631da177e4SLinus Torvalds  * ipc_findkey	- find a key in an ipc identifier set
1648001c858SDavidlohr Bueso  * @ids: ipc identifier set
1658001c858SDavidlohr Bueso  * @key: key to find
1661da177e4SLinus Torvalds  *
1678001c858SDavidlohr Bueso  * Returns the locked pointer to the ipc structure if found or NULL
1688001c858SDavidlohr Bueso  * otherwise. If key is found ipc points to the owning ipc structure
1698001c858SDavidlohr Bueso  *
1700cfb6aeeSGuillaume Knispel  * Called with writer ipc_ids.rwsem held.
1711da177e4SLinus Torvalds  */
ipc_findkey(struct ipc_ids * ids,key_t key)1727748dbfaSNadia Derbey static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
1731da177e4SLinus Torvalds {
174dc2c8c84SDavidlohr Bueso 	struct kern_ipc_perm *ipcp;
1751da177e4SLinus Torvalds 
1760cfb6aeeSGuillaume Knispel 	ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
1770cfb6aeeSGuillaume Knispel 					      ipc_kht_params);
178dc2c8c84SDavidlohr Bueso 	if (!ipcp)
179dc2c8c84SDavidlohr Bueso 		return NULL;
1807ca7e564SNadia Derbey 
18132a27500SDavidlohr Bueso 	rcu_read_lock();
1820cfb6aeeSGuillaume Knispel 	ipc_lock_object(ipcp);
1830cfb6aeeSGuillaume Knispel 	return ipcp;
1847ca7e564SNadia Derbey }
1857ca7e564SNadia Derbey 
186b8fd9983SDavidlohr Bueso /*
187e2652ae6SManfred Spraul  * Insert new IPC object into idr tree, and set sequence number and id
188e2652ae6SManfred Spraul  * in the correct order.
189e2652ae6SManfred Spraul  * Especially:
190e2652ae6SManfred Spraul  * - the sequence number must be set before inserting the object into the idr,
191e2652ae6SManfred Spraul  *   because the sequence number is accessed without a lock.
192e2652ae6SManfred Spraul  * - the id can/must be set after inserting the object into the idr.
193e2652ae6SManfred Spraul  *   All accesses must be done after getting kern_ipc_perm.lock.
194e2652ae6SManfred Spraul  *
195e2652ae6SManfred Spraul  * The caller must own kern_ipc_perm.lock.of the new object.
196e2652ae6SManfred Spraul  * On error, the function returns a (negative) error code.
1973278a2c2SManfred Spraul  *
1983278a2c2SManfred Spraul  * To conserve sequence number space, especially with extended ipc_mni,
1993278a2c2SManfred Spraul  * the sequence number is incremented only when the returned ID is less than
2003278a2c2SManfred Spraul  * the last one.
201b8fd9983SDavidlohr Bueso  */
ipc_idr_alloc(struct ipc_ids * ids,struct kern_ipc_perm * new)202e2652ae6SManfred Spraul static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
203b8fd9983SDavidlohr Bueso {
204e2652ae6SManfred Spraul 	int idx, next_id = -1;
205e2652ae6SManfred Spraul 
206e2652ae6SManfred Spraul #ifdef CONFIG_CHECKPOINT_RESTORE
207e2652ae6SManfred Spraul 	next_id = ids->next_id;
208b8fd9983SDavidlohr Bueso 	ids->next_id = -1;
209e2652ae6SManfred Spraul #endif
210b8fd9983SDavidlohr Bueso 
211e2652ae6SManfred Spraul 	/*
212e2652ae6SManfred Spraul 	 * As soon as a new object is inserted into the idr,
213e2652ae6SManfred Spraul 	 * ipc_obtain_object_idr() or ipc_obtain_object_check() can find it,
214e2652ae6SManfred Spraul 	 * and the lockless preparations for ipc operations can start.
215e2652ae6SManfred Spraul 	 * This means especially: permission checks, audit calls, allocation
216e2652ae6SManfred Spraul 	 * of undo structures, ...
217e2652ae6SManfred Spraul 	 *
218e2652ae6SManfred Spraul 	 * Thus the object must be fully initialized, and if something fails,
219e2652ae6SManfred Spraul 	 * then the full tear-down sequence must be followed.
220e2652ae6SManfred Spraul 	 * (i.e.: set new->deleted, reduce refcount, call_rcu())
221e2652ae6SManfred Spraul 	 */
222b8fd9983SDavidlohr Bueso 
223e2652ae6SManfred Spraul 	if (next_id < 0) { /* !CHECKPOINT_RESTORE or next_id is unset */
22499db46eaSManfred Spraul 		int max_idx;
22599db46eaSManfred Spraul 
22699db46eaSManfred Spraul 		max_idx = max(ids->in_use*3/2, ipc_min_cycle);
22799db46eaSManfred Spraul 		max_idx = min(max_idx, ipc_mni);
2283278a2c2SManfred Spraul 
2293278a2c2SManfred Spraul 		/* allocate the idx, with a NULL struct kern_ipc_perm */
23099db46eaSManfred Spraul 		idx = idr_alloc_cyclic(&ids->ipcs_idr, NULL, 0, max_idx,
23199db46eaSManfred Spraul 					GFP_NOWAIT);
2323278a2c2SManfred Spraul 
2333278a2c2SManfred Spraul 		if (idx >= 0) {
2343278a2c2SManfred Spraul 			/*
2353278a2c2SManfred Spraul 			 * idx got allocated successfully.
2363278a2c2SManfred Spraul 			 * Now calculate the sequence number and set the
2373278a2c2SManfred Spraul 			 * pointer for real.
2383278a2c2SManfred Spraul 			 */
2393278a2c2SManfred Spraul 			if (idx <= ids->last_idx) {
2403278a2c2SManfred Spraul 				ids->seq++;
2413278a2c2SManfred Spraul 				if (ids->seq >= ipcid_seq_max())
242b8fd9983SDavidlohr Bueso 					ids->seq = 0;
2433278a2c2SManfred Spraul 			}
2443278a2c2SManfred Spraul 			ids->last_idx = idx;
2453278a2c2SManfred Spraul 
2463278a2c2SManfred Spraul 			new->seq = ids->seq;
2473278a2c2SManfred Spraul 			/* no need for smp_wmb(), this is done
2483278a2c2SManfred Spraul 			 * inside idr_replace, as part of
2493278a2c2SManfred Spraul 			 * rcu_assign_pointer
2503278a2c2SManfred Spraul 			 */
2513278a2c2SManfred Spraul 			idr_replace(&ids->ipcs_idr, new, idx);
2523278a2c2SManfred Spraul 		}
253e2652ae6SManfred Spraul 	} else {
254e2652ae6SManfred Spraul 		new->seq = ipcid_to_seqx(next_id);
255e2652ae6SManfred Spraul 		idx = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id),
256e2652ae6SManfred Spraul 				0, GFP_NOWAIT);
257b8fd9983SDavidlohr Bueso 	}
258e2652ae6SManfred Spraul 	if (idx >= 0)
2593278a2c2SManfred Spraul 		new->id = (new->seq << ipcmni_seq_shift()) + idx;
260e2652ae6SManfred Spraul 	return idx;
261e2652ae6SManfred Spraul }
262b8fd9983SDavidlohr Bueso 
2631da177e4SLinus Torvalds /**
2648001c858SDavidlohr Bueso  * ipc_addid - add an ipc identifier
2658001c858SDavidlohr Bueso  * @ids: ipc identifier set
2668001c858SDavidlohr Bueso  * @new: new ipc permission set
267ebf66799SDavidlohr Bueso  * @limit: limit for the number of used ids
2681da177e4SLinus Torvalds  *
2698001c858SDavidlohr Bueso  * Add an entry 'new' to the ipc ids idr. The permissions object is
27027c331a1SManfred Spraul  * initialised and the first free entry is set up and the index assigned
271f4566f04SNadia Derbey  * is returned. The 'new' entry is returned in a locked state on success.
27239cfffd7SManfred Spraul  *
273283bb7faSPierre Peiffer  * On failure the entry is not locked and a negative err-code is returned.
27439cfffd7SManfred Spraul  * The caller must use ipc_rcu_putref() to free the identifier.
2751da177e4SLinus Torvalds  *
276d9a605e4SDavidlohr Bueso  * Called with writer ipc_ids.rwsem held.
2771da177e4SLinus Torvalds  */
ipc_addid(struct ipc_ids * ids,struct kern_ipc_perm * new,int limit)278ebf66799SDavidlohr Bueso int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
2791da177e4SLinus Torvalds {
2801efdb69bSEric W. Biederman 	kuid_t euid;
2811efdb69bSEric W. Biederman 	kgid_t egid;
282e2652ae6SManfred Spraul 	int idx, err;
2831da177e4SLinus Torvalds 
28439cfffd7SManfred Spraul 	/* 1) Initialize the refcount so that ipc_rcu_putref works */
28539cfffd7SManfred Spraul 	refcount_set(&new->refcount, 1);
28639cfffd7SManfred Spraul 
2875ac893b8SWaiman Long 	if (limit > ipc_mni)
2885ac893b8SWaiman Long 		limit = ipc_mni;
2897ca7e564SNadia Derbey 
290dc2c8c84SDavidlohr Bueso 	if (ids->in_use >= limit)
291283bb7faSPierre Peiffer 		return -ENOSPC;
2927ca7e564SNadia Derbey 
29354924ea3STejun Heo 	idr_preload(GFP_KERNEL);
29454924ea3STejun Heo 
295e00b4ff7SNadia Derbey 	spin_lock_init(&new->lock);
296e00b4ff7SNadia Derbey 	rcu_read_lock();
297e00b4ff7SNadia Derbey 	spin_lock(&new->lock);
298e00b4ff7SNadia Derbey 
299b9a53227SLinus Torvalds 	current_euid_egid(&euid, &egid);
300b9a53227SLinus Torvalds 	new->cuid = new->uid = euid;
301b9a53227SLinus Torvalds 	new->gid = new->cgid = egid;
302b9a53227SLinus Torvalds 
30339cfffd7SManfred Spraul 	new->deleted = false;
30439cfffd7SManfred Spraul 
305e2652ae6SManfred Spraul 	idx = ipc_idr_alloc(ids, new);
30654924ea3STejun Heo 	idr_preload_end();
3070cfb6aeeSGuillaume Knispel 
308e2652ae6SManfred Spraul 	if (idx >= 0 && new->key != IPC_PRIVATE) {
3090cfb6aeeSGuillaume Knispel 		err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
3100cfb6aeeSGuillaume Knispel 					     ipc_kht_params);
3110cfb6aeeSGuillaume Knispel 		if (err < 0) {
312e2652ae6SManfred Spraul 			idr_remove(&ids->ipcs_idr, idx);
313e2652ae6SManfred Spraul 			idx = err;
3140cfb6aeeSGuillaume Knispel 		}
3150cfb6aeeSGuillaume Knispel 	}
316e2652ae6SManfred Spraul 	if (idx < 0) {
31739cfffd7SManfred Spraul 		new->deleted = true;
318e00b4ff7SNadia Derbey 		spin_unlock(&new->lock);
319e00b4ff7SNadia Derbey 		rcu_read_unlock();
320e2652ae6SManfred Spraul 		return idx;
321e00b4ff7SNadia Derbey 	}
3227ca7e564SNadia Derbey 
3231da177e4SLinus Torvalds 	ids->in_use++;
32427c331a1SManfred Spraul 	if (idx > ids->max_idx)
32527c331a1SManfred Spraul 		ids->max_idx = idx;
326e2652ae6SManfred Spraul 	return idx;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /**
3307748dbfaSNadia Derbey  * ipcget_new -	create a new ipc object
3318001c858SDavidlohr Bueso  * @ns: ipc namespace
332da3dae54SMasanari Iida  * @ids: ipc identifier set
3337748dbfaSNadia Derbey  * @ops: the actual creation routine to call
3347748dbfaSNadia Derbey  * @params: its parameters
3357748dbfaSNadia Derbey  *
336f4566f04SNadia Derbey  * This routine is called by sys_msgget, sys_semget() and sys_shmget()
337f4566f04SNadia Derbey  * when the key is IPC_PRIVATE.
3387748dbfaSNadia Derbey  */
ipcget_new(struct ipc_namespace * ns,struct ipc_ids * ids,const struct ipc_ops * ops,struct ipc_params * params)339b2d75cddSPavel Emelyanov static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
340eb66ec44SMathias Krause 		const struct ipc_ops *ops, struct ipc_params *params)
3417748dbfaSNadia Derbey {
3427748dbfaSNadia Derbey 	int err;
3437748dbfaSNadia Derbey 
344d9a605e4SDavidlohr Bueso 	down_write(&ids->rwsem);
3457748dbfaSNadia Derbey 	err = ops->getnew(ns, params);
346d9a605e4SDavidlohr Bueso 	up_write(&ids->rwsem);
3477748dbfaSNadia Derbey 	return err;
3487748dbfaSNadia Derbey }
3497748dbfaSNadia Derbey 
3507748dbfaSNadia Derbey /**
3518001c858SDavidlohr Bueso  * ipc_check_perms - check security and permissions for an ipc object
3528001c858SDavidlohr Bueso  * @ns: ipc namespace
3537748dbfaSNadia Derbey  * @ipcp: ipc permission set
3547748dbfaSNadia Derbey  * @ops: the actual security routine to call
3557748dbfaSNadia Derbey  * @params: its parameters
356f4566f04SNadia Derbey  *
357f4566f04SNadia Derbey  * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
358f4566f04SNadia Derbey  * when the key is not IPC_PRIVATE and that key already exists in the
3598001c858SDavidlohr Bueso  * ds IDR.
360f4566f04SNadia Derbey  *
3618001c858SDavidlohr Bueso  * On success, the ipc id is returned.
362f4566f04SNadia Derbey  *
363d9a605e4SDavidlohr Bueso  * It is called with ipc_ids.rwsem and ipcp->lock held.
3647748dbfaSNadia Derbey  */
ipc_check_perms(struct ipc_namespace * ns,struct kern_ipc_perm * ipcp,const struct ipc_ops * ops,struct ipc_params * params)365b0e77598SSerge E. Hallyn static int ipc_check_perms(struct ipc_namespace *ns,
366b0e77598SSerge E. Hallyn 			   struct kern_ipc_perm *ipcp,
367eb66ec44SMathias Krause 			   const struct ipc_ops *ops,
3687748dbfaSNadia Derbey 			   struct ipc_params *params)
3697748dbfaSNadia Derbey {
3707748dbfaSNadia Derbey 	int err;
3717748dbfaSNadia Derbey 
372b0e77598SSerge E. Hallyn 	if (ipcperms(ns, ipcp, params->flg))
3737748dbfaSNadia Derbey 		err = -EACCES;
3747748dbfaSNadia Derbey 	else {
3757748dbfaSNadia Derbey 		err = ops->associate(ipcp, params->flg);
3767748dbfaSNadia Derbey 		if (!err)
3777748dbfaSNadia Derbey 			err = ipcp->id;
3787748dbfaSNadia Derbey 	}
3797748dbfaSNadia Derbey 
3807748dbfaSNadia Derbey 	return err;
3817748dbfaSNadia Derbey }
3827748dbfaSNadia Derbey 
3837748dbfaSNadia Derbey /**
3847748dbfaSNadia Derbey  * ipcget_public - get an ipc object or create a new one
3858001c858SDavidlohr Bueso  * @ns: ipc namespace
386da3dae54SMasanari Iida  * @ids: ipc identifier set
3877748dbfaSNadia Derbey  * @ops: the actual creation routine to call
3887748dbfaSNadia Derbey  * @params: its parameters
3897748dbfaSNadia Derbey  *
390f4566f04SNadia Derbey  * This routine is called by sys_msgget, sys_semget() and sys_shmget()
391f4566f04SNadia Derbey  * when the key is not IPC_PRIVATE.
392f4566f04SNadia Derbey  * It adds a new entry if the key is not found and does some permission
393f4566f04SNadia Derbey  * / security checkings if the key is found.
394f4566f04SNadia Derbey  *
395f4566f04SNadia Derbey  * On success, the ipc id is returned.
3967748dbfaSNadia Derbey  */
ipcget_public(struct ipc_namespace * ns,struct ipc_ids * ids,const struct ipc_ops * ops,struct ipc_params * params)397b2d75cddSPavel Emelyanov static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
398eb66ec44SMathias Krause 		const struct ipc_ops *ops, struct ipc_params *params)
3997748dbfaSNadia Derbey {
4007748dbfaSNadia Derbey 	struct kern_ipc_perm *ipcp;
4017748dbfaSNadia Derbey 	int flg = params->flg;
4027748dbfaSNadia Derbey 	int err;
4037748dbfaSNadia Derbey 
4043e148c79SNadia Derbey 	/*
4053e148c79SNadia Derbey 	 * Take the lock as a writer since we are potentially going to add
4063e148c79SNadia Derbey 	 * a new entry + read locks are not "upgradable"
4073e148c79SNadia Derbey 	 */
408d9a605e4SDavidlohr Bueso 	down_write(&ids->rwsem);
4097748dbfaSNadia Derbey 	ipcp = ipc_findkey(ids, params->key);
4107748dbfaSNadia Derbey 	if (ipcp == NULL) {
4117748dbfaSNadia Derbey 		/* key not used */
4127748dbfaSNadia Derbey 		if (!(flg & IPC_CREAT))
4137748dbfaSNadia Derbey 			err = -ENOENT;
4147748dbfaSNadia Derbey 		else
4157748dbfaSNadia Derbey 			err = ops->getnew(ns, params);
4167748dbfaSNadia Derbey 	} else {
4177748dbfaSNadia Derbey 		/* ipc object has been locked by ipc_findkey() */
4187748dbfaSNadia Derbey 
4197748dbfaSNadia Derbey 		if (flg & IPC_CREAT && flg & IPC_EXCL)
4207748dbfaSNadia Derbey 			err = -EEXIST;
4217748dbfaSNadia Derbey 		else {
4227748dbfaSNadia Derbey 			err = 0;
4237748dbfaSNadia Derbey 			if (ops->more_checks)
4247748dbfaSNadia Derbey 				err = ops->more_checks(ipcp, params);
4257748dbfaSNadia Derbey 			if (!err)
426f4566f04SNadia Derbey 				/*
427f4566f04SNadia Derbey 				 * ipc_check_perms returns the IPC id on
428f4566f04SNadia Derbey 				 * success
429f4566f04SNadia Derbey 				 */
430b0e77598SSerge E. Hallyn 				err = ipc_check_perms(ns, ipcp, ops, params);
4317748dbfaSNadia Derbey 		}
4327748dbfaSNadia Derbey 		ipc_unlock(ipcp);
4337748dbfaSNadia Derbey 	}
434d9a605e4SDavidlohr Bueso 	up_write(&ids->rwsem);
4357748dbfaSNadia Derbey 
4367748dbfaSNadia Derbey 	return err;
4377748dbfaSNadia Derbey }
4387748dbfaSNadia Derbey 
4390cfb6aeeSGuillaume Knispel /**
4400cfb6aeeSGuillaume Knispel  * ipc_kht_remove - remove an ipc from the key hashtable
4410cfb6aeeSGuillaume Knispel  * @ids: ipc identifier set
4420cfb6aeeSGuillaume Knispel  * @ipcp: ipc perm structure containing the key to remove
4430cfb6aeeSGuillaume Knispel  *
4440cfb6aeeSGuillaume Knispel  * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
4450cfb6aeeSGuillaume Knispel  * before this function is called, and remain locked on the exit.
4460cfb6aeeSGuillaume Knispel  */
ipc_kht_remove(struct ipc_ids * ids,struct kern_ipc_perm * ipcp)4470cfb6aeeSGuillaume Knispel static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
4480cfb6aeeSGuillaume Knispel {
4490cfb6aeeSGuillaume Knispel 	if (ipcp->key != IPC_PRIVATE)
450126e8beeSAlexander Mikhalitsyn 		WARN_ON_ONCE(rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
451126e8beeSAlexander Mikhalitsyn 				       ipc_kht_params));
4520cfb6aeeSGuillaume Knispel }
4537748dbfaSNadia Derbey 
4547748dbfaSNadia Derbey /**
455b869d5beSManfred Spraul  * ipc_search_maxidx - search for the highest assigned index
456b869d5beSManfred Spraul  * @ids: ipc identifier set
457b869d5beSManfred Spraul  * @limit: known upper limit for highest assigned index
458b869d5beSManfred Spraul  *
459b869d5beSManfred Spraul  * The function determines the highest assigned index in @ids. It is intended
460b869d5beSManfred Spraul  * to be called when ids->max_idx needs to be updated.
461b869d5beSManfred Spraul  * Updating ids->max_idx is necessary when the current highest index ipc
462b869d5beSManfred Spraul  * object is deleted.
463b869d5beSManfred Spraul  * If no ipc object is allocated, then -1 is returned.
464b869d5beSManfred Spraul  *
465b869d5beSManfred Spraul  * ipc_ids.rwsem needs to be held by the caller.
466b869d5beSManfred Spraul  */
ipc_search_maxidx(struct ipc_ids * ids,int limit)467b869d5beSManfred Spraul static int ipc_search_maxidx(struct ipc_ids *ids, int limit)
468b869d5beSManfred Spraul {
469b869d5beSManfred Spraul 	int tmpidx;
470b869d5beSManfred Spraul 	int i;
471b869d5beSManfred Spraul 	int retval;
472b869d5beSManfred Spraul 
473b869d5beSManfred Spraul 	i = ilog2(limit+1);
474b869d5beSManfred Spraul 
475b869d5beSManfred Spraul 	retval = 0;
476b869d5beSManfred Spraul 	for (; i >= 0; i--) {
477b869d5beSManfred Spraul 		tmpidx = retval | (1<<i);
478b869d5beSManfred Spraul 		/*
479b869d5beSManfred Spraul 		 * "0" is a possible index value, thus search using
480b869d5beSManfred Spraul 		 * e.g. 15,7,3,1,0 instead of 16,8,4,2,1.
481b869d5beSManfred Spraul 		 */
482b869d5beSManfred Spraul 		tmpidx = tmpidx-1;
483b869d5beSManfred Spraul 		if (idr_get_next(&ids->ipcs_idr, &tmpidx))
484b869d5beSManfred Spraul 			retval |= (1<<i);
485b869d5beSManfred Spraul 	}
486b869d5beSManfred Spraul 	return retval - 1;
487b869d5beSManfred Spraul }
488b869d5beSManfred Spraul 
489b869d5beSManfred Spraul /**
4908001c858SDavidlohr Bueso  * ipc_rmid - remove an ipc identifier
4918001c858SDavidlohr Bueso  * @ids: ipc identifier set
492f4566f04SNadia Derbey  * @ipcp: ipc perm structure containing the identifier to remove
4931da177e4SLinus Torvalds  *
494d9a605e4SDavidlohr Bueso  * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
4953e148c79SNadia Derbey  * before this function is called, and remain locked on the exit.
4961da177e4SLinus Torvalds  */
ipc_rmid(struct ipc_ids * ids,struct kern_ipc_perm * ipcp)4977ca7e564SNadia Derbey void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
4981da177e4SLinus Torvalds {
49927c331a1SManfred Spraul 	int idx = ipcid_to_idx(ipcp->id);
5001da177e4SLinus Torvalds 
501126e8beeSAlexander Mikhalitsyn 	WARN_ON_ONCE(idr_remove(&ids->ipcs_idr, idx) != ipcp);
5020cfb6aeeSGuillaume Knispel 	ipc_kht_remove(ids, ipcp);
5031da177e4SLinus Torvalds 	ids->in_use--;
50472a8ff2fSRafael Aquini 	ipcp->deleted = true;
50515df03c8SDavidlohr Bueso 
50627c331a1SManfred Spraul 	if (unlikely(idx == ids->max_idx)) {
507b869d5beSManfred Spraul 		idx = ids->max_idx-1;
508b869d5beSManfred Spraul 		if (idx >= 0)
509b869d5beSManfred Spraul 			idx = ipc_search_maxidx(ids, idx);
51027c331a1SManfred Spraul 		ids->max_idx = idx;
51115df03c8SDavidlohr Bueso 	}
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5140cfb6aeeSGuillaume Knispel /**
5150cfb6aeeSGuillaume Knispel  * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
5160cfb6aeeSGuillaume Knispel  * @ids: ipc identifier set
5170cfb6aeeSGuillaume Knispel  * @ipcp: ipc perm structure containing the key to modify
5180cfb6aeeSGuillaume Knispel  *
5190cfb6aeeSGuillaume Knispel  * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
5200cfb6aeeSGuillaume Knispel  * before this function is called, and remain locked on the exit.
5210cfb6aeeSGuillaume Knispel  */
ipc_set_key_private(struct ipc_ids * ids,struct kern_ipc_perm * ipcp)5220cfb6aeeSGuillaume Knispel void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
5230cfb6aeeSGuillaume Knispel {
5240cfb6aeeSGuillaume Knispel 	ipc_kht_remove(ids, ipcp);
5250cfb6aeeSGuillaume Knispel 	ipcp->key = IPC_PRIVATE;
5260cfb6aeeSGuillaume Knispel }
5270cfb6aeeSGuillaume Knispel 
ipc_rcu_getref(struct kern_ipc_perm * ptr)5282a9d6481SManfred Spraul bool ipc_rcu_getref(struct kern_ipc_perm *ptr)
5291da177e4SLinus Torvalds {
5309405c03eSElena Reshetova 	return refcount_inc_not_zero(&ptr->refcount);
53165f27f38SDavid Howells }
53265f27f38SDavid Howells 
ipc_rcu_putref(struct kern_ipc_perm * ptr,void (* func)(struct rcu_head * head))533dba4cdd3SManfred Spraul void ipc_rcu_putref(struct kern_ipc_perm *ptr,
534dba4cdd3SManfred Spraul 			void (*func)(struct rcu_head *head))
5351da177e4SLinus Torvalds {
5369405c03eSElena Reshetova 	if (!refcount_dec_and_test(&ptr->refcount))
5371da177e4SLinus Torvalds 		return;
5381da177e4SLinus Torvalds 
539dba4cdd3SManfred Spraul 	call_rcu(&ptr->rcu, func);
5401da177e4SLinus Torvalds }
54153dad6d3SDavidlohr Bueso 
5421da177e4SLinus Torvalds /**
5438001c858SDavidlohr Bueso  * ipcperms - check ipc permissions
5448001c858SDavidlohr Bueso  * @ns: ipc namespace
5458001c858SDavidlohr Bueso  * @ipcp: ipc permission set
5468001c858SDavidlohr Bueso  * @flag: desired permission set
5471da177e4SLinus Torvalds  *
5481da177e4SLinus Torvalds  * Check user, group, other permissions for access
5491da177e4SLinus Torvalds  * to ipc resources. return 0 if allowed
550b0e77598SSerge E. Hallyn  *
5510e056eb5Smchehab@s-opensource.com  * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
5521da177e4SLinus Torvalds  */
ipcperms(struct ipc_namespace * ns,struct kern_ipc_perm * ipcp,short flag)553b0e77598SSerge E. Hallyn int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
554b0e77598SSerge E. Hallyn {
5551efdb69bSEric W. Biederman 	kuid_t euid = current_euid();
556a33e6751SAl Viro 	int requested_mode, granted_mode;
5571da177e4SLinus Torvalds 
558a33e6751SAl Viro 	audit_ipc_obj(ipcp);
5591da177e4SLinus Torvalds 	requested_mode = (flag >> 6) | (flag >> 3) | flag;
5601da177e4SLinus Torvalds 	granted_mode = ipcp->mode;
5611efdb69bSEric W. Biederman 	if (uid_eq(euid, ipcp->cuid) ||
5621efdb69bSEric W. Biederman 	    uid_eq(euid, ipcp->uid))
5631da177e4SLinus Torvalds 		granted_mode >>= 6;
5641da177e4SLinus Torvalds 	else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
5651da177e4SLinus Torvalds 		granted_mode >>= 3;
5661da177e4SLinus Torvalds 	/* is there some bit set in requested_mode but not in granted_mode? */
5671da177e4SLinus Torvalds 	if ((requested_mode & ~granted_mode & 0007) &&
568b0e77598SSerge E. Hallyn 	    !ns_capable(ns->user_ns, CAP_IPC_OWNER))
5691da177e4SLinus Torvalds 		return -1;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	return security_ipc_permission(ipcp, flag);
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /*
5751da177e4SLinus Torvalds  * Functions to convert between the kern_ipc_perm structure and the
5761da177e4SLinus Torvalds  * old/new ipc_perm structures
5771da177e4SLinus Torvalds  */
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /**
5801da177e4SLinus Torvalds  * kernel_to_ipc64_perm	- convert kernel ipc permissions to user
5811da177e4SLinus Torvalds  * @in: kernel permissions
5828001c858SDavidlohr Bueso  * @out: new style ipc permissions
5831da177e4SLinus Torvalds  *
58472fd4a35SRobert P. J. Day  * Turn the kernel object @in into a set of permissions descriptions
58572fd4a35SRobert P. J. Day  * for returning to userspace (@out).
5861da177e4SLinus Torvalds  */
kernel_to_ipc64_perm(struct kern_ipc_perm * in,struct ipc64_perm * out)5871da177e4SLinus Torvalds void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	out->key	= in->key;
5901efdb69bSEric W. Biederman 	out->uid	= from_kuid_munged(current_user_ns(), in->uid);
5911efdb69bSEric W. Biederman 	out->gid	= from_kgid_munged(current_user_ns(), in->gid);
5921efdb69bSEric W. Biederman 	out->cuid	= from_kuid_munged(current_user_ns(), in->cuid);
5931efdb69bSEric W. Biederman 	out->cgid	= from_kgid_munged(current_user_ns(), in->cgid);
5941da177e4SLinus Torvalds 	out->mode	= in->mode;
5951da177e4SLinus Torvalds 	out->seq	= in->seq;
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /**
599f4566f04SNadia Derbey  * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
6008001c858SDavidlohr Bueso  * @in: new style ipc permissions
6018001c858SDavidlohr Bueso  * @out: old style ipc permissions
6021da177e4SLinus Torvalds  *
60372fd4a35SRobert P. J. Day  * Turn the new style permissions object @in into a compatibility
60472fd4a35SRobert P. J. Day  * object and store it into the @out pointer.
6051da177e4SLinus Torvalds  */
ipc64_perm_to_ipc_perm(struct ipc64_perm * in,struct ipc_perm * out)6061da177e4SLinus Torvalds void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	out->key	= in->key;
6091da177e4SLinus Torvalds 	SET_UID(out->uid, in->uid);
6101da177e4SLinus Torvalds 	SET_GID(out->gid, in->gid);
6111da177e4SLinus Torvalds 	SET_UID(out->cuid, in->cuid);
6121da177e4SLinus Torvalds 	SET_GID(out->cgid, in->cgid);
6131da177e4SLinus Torvalds 	out->mode	= in->mode;
6141da177e4SLinus Torvalds 	out->seq	= in->seq;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
617f4566f04SNadia Derbey /**
6180cfb6aeeSGuillaume Knispel  * ipc_obtain_object_idr
6194d2bff5eSDavidlohr Bueso  * @ids: ipc identifier set
6204d2bff5eSDavidlohr Bueso  * @id: ipc id to look for
6214d2bff5eSDavidlohr Bueso  *
6224d2bff5eSDavidlohr Bueso  * Look for an id in the ipc ids idr and return associated ipc object.
6234d2bff5eSDavidlohr Bueso  *
6244d2bff5eSDavidlohr Bueso  * Call inside the RCU critical section.
6254d2bff5eSDavidlohr Bueso  * The ipc object is *not* locked on exit.
6264d2bff5eSDavidlohr Bueso  */
ipc_obtain_object_idr(struct ipc_ids * ids,int id)62755b7ae50SDavidlohr Bueso struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
6284d2bff5eSDavidlohr Bueso {
6294d2bff5eSDavidlohr Bueso 	struct kern_ipc_perm *out;
63027c331a1SManfred Spraul 	int idx = ipcid_to_idx(id);
6314d2bff5eSDavidlohr Bueso 
63227c331a1SManfred Spraul 	out = idr_find(&ids->ipcs_idr, idx);
6334d2bff5eSDavidlohr Bueso 	if (!out)
6344d2bff5eSDavidlohr Bueso 		return ERR_PTR(-EINVAL);
6354d2bff5eSDavidlohr Bueso 
6364d2bff5eSDavidlohr Bueso 	return out;
6374d2bff5eSDavidlohr Bueso }
6384d2bff5eSDavidlohr Bueso 
6394d2bff5eSDavidlohr Bueso /**
6404d2bff5eSDavidlohr Bueso  * ipc_obtain_object_check
6414d2bff5eSDavidlohr Bueso  * @ids: ipc identifier set
6424d2bff5eSDavidlohr Bueso  * @id: ipc id to look for
6434d2bff5eSDavidlohr Bueso  *
6442e5ceb45SManfred Spraul  * Similar to ipc_obtain_object_idr() but also checks the ipc object
6452e5ceb45SManfred Spraul  * sequence number.
6464d2bff5eSDavidlohr Bueso  *
6474d2bff5eSDavidlohr Bueso  * Call inside the RCU critical section.
6484d2bff5eSDavidlohr Bueso  * The ipc object is *not* locked on exit.
6494d2bff5eSDavidlohr Bueso  */
ipc_obtain_object_check(struct ipc_ids * ids,int id)6504d2bff5eSDavidlohr Bueso struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
6514d2bff5eSDavidlohr Bueso {
65255b7ae50SDavidlohr Bueso 	struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
6534d2bff5eSDavidlohr Bueso 
6544d2bff5eSDavidlohr Bueso 	if (IS_ERR(out))
6554d2bff5eSDavidlohr Bueso 		goto out;
6564d2bff5eSDavidlohr Bueso 
6574d2bff5eSDavidlohr Bueso 	if (ipc_checkid(out, id))
6586157dbbfSDavidlohr Bueso 		return ERR_PTR(-EINVAL);
6594d2bff5eSDavidlohr Bueso out:
6603e148c79SNadia Derbey 	return out;
6613e148c79SNadia Derbey }
6623e148c79SNadia Derbey 
663b2d75cddSPavel Emelyanov /**
664b2d75cddSPavel Emelyanov  * ipcget - Common sys_*get() code
665da3dae54SMasanari Iida  * @ns: namespace
6668001c858SDavidlohr Bueso  * @ids: ipc identifier set
667b2d75cddSPavel Emelyanov  * @ops: operations to be called on ipc object creation, permission checks
668b2d75cddSPavel Emelyanov  *       and further checks
669b2d75cddSPavel Emelyanov  * @params: the parameters needed by the previous operations.
670b2d75cddSPavel Emelyanov  *
671b2d75cddSPavel Emelyanov  * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
672b2d75cddSPavel Emelyanov  */
ipcget(struct ipc_namespace * ns,struct ipc_ids * ids,const struct ipc_ops * ops,struct ipc_params * params)673b2d75cddSPavel Emelyanov int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
674eb66ec44SMathias Krause 			const struct ipc_ops *ops, struct ipc_params *params)
675b2d75cddSPavel Emelyanov {
676b2d75cddSPavel Emelyanov 	if (params->key == IPC_PRIVATE)
677b2d75cddSPavel Emelyanov 		return ipcget_new(ns, ids, ops, params);
678b2d75cddSPavel Emelyanov 	else
679b2d75cddSPavel Emelyanov 		return ipcget_public(ns, ids, ops, params);
680b2d75cddSPavel Emelyanov }
681b2d75cddSPavel Emelyanov 
6828f4a3809SPierre Peiffer /**
6838001c858SDavidlohr Bueso  * ipc_update_perm - update the permissions of an ipc object
6848f4a3809SPierre Peiffer  * @in:  the permission given as input.
6858f4a3809SPierre Peiffer  * @out: the permission of the ipc to set.
6868f4a3809SPierre Peiffer  */
ipc_update_perm(struct ipc64_perm * in,struct kern_ipc_perm * out)6871efdb69bSEric W. Biederman int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
6888f4a3809SPierre Peiffer {
6891efdb69bSEric W. Biederman 	kuid_t uid = make_kuid(current_user_ns(), in->uid);
6901efdb69bSEric W. Biederman 	kgid_t gid = make_kgid(current_user_ns(), in->gid);
6911efdb69bSEric W. Biederman 	if (!uid_valid(uid) || !gid_valid(gid))
6921efdb69bSEric W. Biederman 		return -EINVAL;
6931efdb69bSEric W. Biederman 
6941efdb69bSEric W. Biederman 	out->uid = uid;
6951efdb69bSEric W. Biederman 	out->gid = gid;
6968f4a3809SPierre Peiffer 	out->mode = (out->mode & ~S_IRWXUGO)
6978f4a3809SPierre Peiffer 		| (in->mode & S_IRWXUGO);
6981efdb69bSEric W. Biederman 
6991efdb69bSEric W. Biederman 	return 0;
7008f4a3809SPierre Peiffer }
7018f4a3809SPierre Peiffer 
702a5f75e7fSPierre Peiffer /**
7034241c1a3SManfred Spraul  * ipcctl_obtain_check - retrieve an ipc object and check permissions
7048001c858SDavidlohr Bueso  * @ns:  ipc namespace
705a5f75e7fSPierre Peiffer  * @ids:  the table of ids where to look for the ipc
706a5f75e7fSPierre Peiffer  * @id:   the id of the ipc to retrieve
707a5f75e7fSPierre Peiffer  * @cmd:  the cmd to check
708a5f75e7fSPierre Peiffer  * @perm: the permission to set
709a5f75e7fSPierre Peiffer  * @extra_perm: one extra permission parameter used by msq
710a5f75e7fSPierre Peiffer  *
711a5f75e7fSPierre Peiffer  * This function does some common audit and permissions check for some IPC_XXX
712a5f75e7fSPierre Peiffer  * cmd and is called from semctl_down, shmctl_down and msgctl_down.
7130e056eb5Smchehab@s-opensource.com  *
7144241c1a3SManfred Spraul  * It:
7154241c1a3SManfred Spraul  *   - retrieves the ipc object with the given id in the given table.
716a5f75e7fSPierre Peiffer  *   - performs some audit and permission check, depending on the given cmd
7170e056eb5Smchehab@s-opensource.com  *   - returns a pointer to the ipc object or otherwise, the corresponding
7180e056eb5Smchehab@s-opensource.com  *     error.
7197b4cc5d8SDavidlohr Bueso  *
720d9a605e4SDavidlohr Bueso  * Call holding the both the rwsem and the rcu read lock.
721a5f75e7fSPierre Peiffer  */
ipcctl_obtain_check(struct ipc_namespace * ns,struct ipc_ids * ids,int id,int cmd,struct ipc64_perm * perm,int extra_perm)7224241c1a3SManfred Spraul struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
723444d0f62SDavidlohr Bueso 					struct ipc_ids *ids, int id, int cmd,
724444d0f62SDavidlohr Bueso 					struct ipc64_perm *perm, int extra_perm)
725444d0f62SDavidlohr Bueso {
7261efdb69bSEric W. Biederman 	kuid_t euid;
727444d0f62SDavidlohr Bueso 	int err = -EPERM;
728444d0f62SDavidlohr Bueso 	struct kern_ipc_perm *ipcp;
729a5f75e7fSPierre Peiffer 
730444d0f62SDavidlohr Bueso 	ipcp = ipc_obtain_object_check(ids, id);
731a5f75e7fSPierre Peiffer 	if (IS_ERR(ipcp)) {
732a5f75e7fSPierre Peiffer 		err = PTR_ERR(ipcp);
7337b4cc5d8SDavidlohr Bueso 		goto err;
734a5f75e7fSPierre Peiffer 	}
735a5f75e7fSPierre Peiffer 
736a33e6751SAl Viro 	audit_ipc_obj(ipcp);
737e816f370SAl Viro 	if (cmd == IPC_SET)
738e816f370SAl Viro 		audit_ipc_set_perm(extra_perm, perm->uid,
739a5f75e7fSPierre Peiffer 				   perm->gid, perm->mode);
740414c0708SDavid Howells 
741414c0708SDavid Howells 	euid = current_euid();
7421efdb69bSEric W. Biederman 	if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid)  ||
743b0e77598SSerge E. Hallyn 	    ns_capable(ns->user_ns, CAP_SYS_ADMIN))
7447b4cc5d8SDavidlohr Bueso 		return ipcp; /* successful lookup */
7457b4cc5d8SDavidlohr Bueso err:
746a5f75e7fSPierre Peiffer 	return ERR_PTR(err);
747a5f75e7fSPierre Peiffer }
748a5f75e7fSPierre Peiffer 
749c1d7e01dSWill Deacon #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /**
7538001c858SDavidlohr Bueso  * ipc_parse_version - ipc call version
7541da177e4SLinus Torvalds  * @cmd: pointer to command
7551da177e4SLinus Torvalds  *
7561da177e4SLinus Torvalds  * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
75772fd4a35SRobert P. J. Day  * The @cmd value is turned from an encoding command and version into
7581da177e4SLinus Torvalds  * just the command code.
7591da177e4SLinus Torvalds  */
ipc_parse_version(int * cmd)7601da177e4SLinus Torvalds int ipc_parse_version(int *cmd)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	if (*cmd & IPC_64) {
7631da177e4SLinus Torvalds 		*cmd ^= IPC_64;
7641da177e4SLinus Torvalds 		return IPC_64;
7651da177e4SLinus Torvalds 	} else {
7661da177e4SLinus Torvalds 		return IPC_OLD;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
770c1d7e01dSWill Deacon #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
771ae781774SMike Waychison 
772ae781774SMike Waychison #ifdef CONFIG_PROC_FS
773bc1fc6d8SEric W. Biederman struct ipc_proc_iter {
774bc1fc6d8SEric W. Biederman 	struct ipc_namespace *ns;
77503f1fc09SEric W. Biederman 	struct pid_namespace *pid_ns;
776bc1fc6d8SEric W. Biederman 	struct ipc_proc_iface *iface;
777bc1fc6d8SEric W. Biederman };
778bc1fc6d8SEric W. Biederman 
ipc_seq_pid_ns(struct seq_file * s)77903f1fc09SEric W. Biederman struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s)
78003f1fc09SEric W. Biederman {
78103f1fc09SEric W. Biederman 	struct ipc_proc_iter *iter = s->private;
78203f1fc09SEric W. Biederman 	return iter->pid_ns;
78303f1fc09SEric W. Biederman }
78403f1fc09SEric W. Biederman 
785*58b5c203SManfred Spraul /**
786*58b5c203SManfred Spraul  * sysvipc_find_ipc - Find and lock the ipc structure based on seq pos
787*58b5c203SManfred Spraul  * @ids: ipc identifier set
788*58b5c203SManfred Spraul  * @pos: expected position
789*58b5c203SManfred Spraul  *
790*58b5c203SManfred Spraul  * The function finds an ipc structure, based on the sequence file
791*58b5c203SManfred Spraul  * position @pos. If there is no ipc structure at position @pos, then
792*58b5c203SManfred Spraul  * the successor is selected.
793*58b5c203SManfred Spraul  * If a structure is found, then it is locked (both rcu_read_lock() and
794*58b5c203SManfred Spraul  * ipc_lock_object()) and  @pos is set to the position needed to locate
795*58b5c203SManfred Spraul  * the found ipc structure.
796*58b5c203SManfred Spraul  * If nothing is found (i.e. EOF), @pos is not modified.
797*58b5c203SManfred Spraul  *
798*58b5c203SManfred Spraul  * The function returns the found ipc structure, or NULL at EOF.
799ae781774SMike Waychison  */
sysvipc_find_ipc(struct ipc_ids * ids,loff_t * pos)800*58b5c203SManfred Spraul static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t *pos)
8017ca7e564SNadia Derbey {
802*58b5c203SManfred Spraul 	int tmpidx;
803*58b5c203SManfred Spraul 	struct kern_ipc_perm *ipc;
8047ca7e564SNadia Derbey 
805*58b5c203SManfred Spraul 	/* convert from position to idr index -> "-1" */
806*58b5c203SManfred Spraul 	tmpidx = *pos - 1;
8077ca7e564SNadia Derbey 
808*58b5c203SManfred Spraul 	ipc = idr_get_next(&ids->ipcs_idr, &tmpidx);
8097ca7e564SNadia Derbey 	if (ipc != NULL) {
81032a27500SDavidlohr Bueso 		rcu_read_lock();
81132a27500SDavidlohr Bueso 		ipc_lock_object(ipc);
812*58b5c203SManfred Spraul 
813*58b5c203SManfred Spraul 		/* convert from idr index to position  -> "+1" */
814*58b5c203SManfred Spraul 		*pos = tmpidx + 1;
8155e698222SVasily Averin 	}
816ae781774SMike Waychison 	return ipc;
817ae781774SMike Waychison }
818ae781774SMike Waychison 
sysvipc_proc_next(struct seq_file * s,void * it,loff_t * pos)8197ca7e564SNadia Derbey static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
8207ca7e564SNadia Derbey {
8217ca7e564SNadia Derbey 	struct ipc_proc_iter *iter = s->private;
8227ca7e564SNadia Derbey 	struct ipc_proc_iface *iface = iter->iface;
8237ca7e564SNadia Derbey 	struct kern_ipc_perm *ipc = it;
8247ca7e564SNadia Derbey 
8257ca7e564SNadia Derbey 	/* If we had an ipc id locked before, unlock it */
8267ca7e564SNadia Derbey 	if (ipc && ipc != SEQ_START_TOKEN)
8277ca7e564SNadia Derbey 		ipc_unlock(ipc);
8287ca7e564SNadia Derbey 
829*58b5c203SManfred Spraul 	/* Next -> search for *pos+1 */
830*58b5c203SManfred Spraul 	(*pos)++;
831*58b5c203SManfred Spraul 	return sysvipc_find_ipc(&iter->ns->ids[iface->ids], pos);
8327ca7e564SNadia Derbey }
8337ca7e564SNadia Derbey 
834ae781774SMike Waychison /*
835*58b5c203SManfred Spraul  * File positions: pos 0 -> header, pos n -> ipc idx = n - 1.
836f4566f04SNadia Derbey  * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
837ae781774SMike Waychison  */
sysvipc_proc_start(struct seq_file * s,loff_t * pos)838ae781774SMike Waychison static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
839ae781774SMike Waychison {
840bc1fc6d8SEric W. Biederman 	struct ipc_proc_iter *iter = s->private;
841bc1fc6d8SEric W. Biederman 	struct ipc_proc_iface *iface = iter->iface;
84273ea4130SKirill Korotaev 	struct ipc_ids *ids;
84373ea4130SKirill Korotaev 
844ed2ddbf8SPierre Peiffer 	ids = &iter->ns->ids[iface->ids];
845ae781774SMike Waychison 
846ae781774SMike Waychison 	/*
847ae781774SMike Waychison 	 * Take the lock - this will be released by the corresponding
848ae781774SMike Waychison 	 * call to stop().
849ae781774SMike Waychison 	 */
850d9a605e4SDavidlohr Bueso 	down_read(&ids->rwsem);
851ae781774SMike Waychison 
852ae781774SMike Waychison 	/* pos < 0 is invalid */
853ae781774SMike Waychison 	if (*pos < 0)
854ae781774SMike Waychison 		return NULL;
855ae781774SMike Waychison 
856ae781774SMike Waychison 	/* pos == 0 means header */
857ae781774SMike Waychison 	if (*pos == 0)
858ae781774SMike Waychison 		return SEQ_START_TOKEN;
859ae781774SMike Waychison 
860*58b5c203SManfred Spraul 	/* Otherwise return the correct ipc structure */
861*58b5c203SManfred Spraul 	return sysvipc_find_ipc(ids, pos);
862ae781774SMike Waychison }
863ae781774SMike Waychison 
sysvipc_proc_stop(struct seq_file * s,void * it)864ae781774SMike Waychison static void sysvipc_proc_stop(struct seq_file *s, void *it)
865ae781774SMike Waychison {
866ae781774SMike Waychison 	struct kern_ipc_perm *ipc = it;
867bc1fc6d8SEric W. Biederman 	struct ipc_proc_iter *iter = s->private;
868bc1fc6d8SEric W. Biederman 	struct ipc_proc_iface *iface = iter->iface;
86973ea4130SKirill Korotaev 	struct ipc_ids *ids;
870ae781774SMike Waychison 
871f4566f04SNadia Derbey 	/* If we had a locked structure, release it */
872ae781774SMike Waychison 	if (ipc && ipc != SEQ_START_TOKEN)
873ae781774SMike Waychison 		ipc_unlock(ipc);
874ae781774SMike Waychison 
875ed2ddbf8SPierre Peiffer 	ids = &iter->ns->ids[iface->ids];
876ae781774SMike Waychison 	/* Release the lock we took in start() */
877d9a605e4SDavidlohr Bueso 	up_read(&ids->rwsem);
878ae781774SMike Waychison }
879ae781774SMike Waychison 
sysvipc_proc_show(struct seq_file * s,void * it)880ae781774SMike Waychison static int sysvipc_proc_show(struct seq_file *s, void *it)
881ae781774SMike Waychison {
882bc1fc6d8SEric W. Biederman 	struct ipc_proc_iter *iter = s->private;
883bc1fc6d8SEric W. Biederman 	struct ipc_proc_iface *iface = iter->iface;
884ae781774SMike Waychison 
8857f032d6eSJoe Perches 	if (it == SEQ_START_TOKEN) {
8867f032d6eSJoe Perches 		seq_puts(s, iface->header);
8877f032d6eSJoe Perches 		return 0;
8887f032d6eSJoe Perches 	}
889ae781774SMike Waychison 
890ae781774SMike Waychison 	return iface->show(s, it);
891ae781774SMike Waychison }
892ae781774SMike Waychison 
89388e9d34cSJames Morris static const struct seq_operations sysvipc_proc_seqops = {
894ae781774SMike Waychison 	.start = sysvipc_proc_start,
895ae781774SMike Waychison 	.stop  = sysvipc_proc_stop,
896ae781774SMike Waychison 	.next  = sysvipc_proc_next,
897ae781774SMike Waychison 	.show  = sysvipc_proc_show,
898ae781774SMike Waychison };
899ae781774SMike Waychison 
sysvipc_proc_open(struct inode * inode,struct file * file)900bc1fc6d8SEric W. Biederman static int sysvipc_proc_open(struct inode *inode, struct file *file)
901bc1fc6d8SEric W. Biederman {
902bc1fc6d8SEric W. Biederman 	struct ipc_proc_iter *iter;
903bc1fc6d8SEric W. Biederman 
904d66a0520SRob Jones 	iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
905bc1fc6d8SEric W. Biederman 	if (!iter)
906d66a0520SRob Jones 		return -ENOMEM;
907bc1fc6d8SEric W. Biederman 
908359745d7SMuchun Song 	iter->iface = pde_data(inode);
909bc1fc6d8SEric W. Biederman 	iter->ns    = get_ipc_ns(current->nsproxy->ipc_ns);
91003f1fc09SEric W. Biederman 	iter->pid_ns = get_pid_ns(task_active_pid_ns(current));
911d66a0520SRob Jones 
912d66a0520SRob Jones 	return 0;
913bc1fc6d8SEric W. Biederman }
914bc1fc6d8SEric W. Biederman 
sysvipc_proc_release(struct inode * inode,struct file * file)915bc1fc6d8SEric W. Biederman static int sysvipc_proc_release(struct inode *inode, struct file *file)
916bc1fc6d8SEric W. Biederman {
917bc1fc6d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
918bc1fc6d8SEric W. Biederman 	struct ipc_proc_iter *iter = seq->private;
919bc1fc6d8SEric W. Biederman 	put_ipc_ns(iter->ns);
92003f1fc09SEric W. Biederman 	put_pid_ns(iter->pid_ns);
921bc1fc6d8SEric W. Biederman 	return seq_release_private(inode, file);
922ae781774SMike Waychison }
923ae781774SMike Waychison 
92497a32539SAlexey Dobriyan static const struct proc_ops sysvipc_proc_ops = {
925d919b33dSAlexey Dobriyan 	.proc_flags	= PROC_ENTRY_PERMANENT,
92697a32539SAlexey Dobriyan 	.proc_open	= sysvipc_proc_open,
92797a32539SAlexey Dobriyan 	.proc_read	= seq_read,
92897a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
92997a32539SAlexey Dobriyan 	.proc_release	= sysvipc_proc_release,
930ae781774SMike Waychison };
931ae781774SMike Waychison #endif /* CONFIG_PROC_FS */
932