blob: c27f0e92f48988016875461ffd5e5db0a8abd08c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 *
5 * Sep 1997 - Call suser() last after "normal" permission checks so we
6 * get BSD style process accounting right.
7 * Occurs in several places in the IPC code.
8 * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
9 * Nov 1999 - ipc helper functions, unified SMP locking
Christian Kujau624dffc2006-01-15 02:43:54 +010010 * Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
Steve Grubb073115d2006-04-02 17:07:33 -040013 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070015 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/shm.h>
22#include <linux/init.h>
23#include <linux/msg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/vmalloc.h>
25#include <linux/slab.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/highuid.h>
28#include <linux/security.h>
29#include <linux/rcupdate.h>
30#include <linux/workqueue.h>
Mike Waychisonae781772005-09-06 15:17:09 -070031#include <linux/seq_file.h>
32#include <linux/proc_fs.h>
Steve Grubb073115d2006-04-02 17:07:33 -040033#include <linux/audit.h>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070034#include <linux/nsproxy.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070035#include <linux/rwsem.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080036#include <linux/ipc_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/unistd.h>
39
40#include "util.h"
41
Mike Waychisonae781772005-09-06 15:17:09 -070042struct ipc_proc_iface {
43 const char *path;
44 const char *header;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070045 int ids;
Mike Waychisonae781772005-09-06 15:17:09 -070046 int (*show)(struct seq_file *, void *);
47};
48
Kirill Korotaev73ea4132006-10-02 02:18:20 -070049struct ipc_namespace init_ipc_ns = {
50 .kref = {
51 .refcount = ATOMIC_INIT(2),
52 },
53};
54
Nadia Derbey4d89dc62008-04-29 01:00:40 -070055atomic_t nr_ipc_ns = ATOMIC_INIT(1);
56
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/**
59 * ipc_init - initialise IPC subsystem
60 *
61 * The various system5 IPC resources (semaphores, messages and shared
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080062 * memory) are initialised
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
64
65static int __init ipc_init(void)
66{
67 sem_init();
68 msg_init();
69 shm_init();
70 return 0;
71}
72__initcall(ipc_init);
73
74/**
75 * ipc_init_ids - initialise IPC identifiers
76 * @ids: Identifier set
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
Nadia Derbey7ca7e562007-10-18 23:40:48 -070078 * Set up the sequence range to use for the ipc identifier range (limited
79 * below IPCMNI) then initialise the ids idr.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
Nadia Derbey7ca7e562007-10-18 23:40:48 -070082void ipc_init_ids(struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Nadia Derbey3e148c72007-10-18 23:40:54 -070084 init_rwsem(&ids->rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 ids->in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ids->seq = 0;
88 {
89 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
90 if(seq_limit > USHRT_MAX)
91 ids->seq_max = USHRT_MAX;
92 else
93 ids->seq_max = seq_limit;
94 }
95
Nadia Derbey7ca7e562007-10-18 23:40:48 -070096 idr_init(&ids->ipcs_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Mike Waychisonae781772005-09-06 15:17:09 -070099#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -0800100static const struct file_operations sysvipc_proc_fops;
Mike Waychisonae781772005-09-06 15:17:09 -0700101/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800102 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
Mike Waychisonae781772005-09-06 15:17:09 -0700103 * @path: Path in procfs
104 * @header: Banner to be printed at the beginning of the file.
105 * @ids: ipc id table to iterate.
106 * @show: show routine.
107 */
108void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700109 int ids, int (*show)(struct seq_file *, void *))
Mike Waychisonae781772005-09-06 15:17:09 -0700110{
111 struct proc_dir_entry *pde;
112 struct ipc_proc_iface *iface;
113
114 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
115 if (!iface)
116 return;
117 iface->path = path;
118 iface->header = header;
119 iface->ids = ids;
120 iface->show = show;
121
122 pde = create_proc_entry(path,
123 S_IRUGO, /* world readable */
124 NULL /* parent dir */);
125 if (pde) {
126 pde->data = iface;
127 pde->proc_fops = &sysvipc_proc_fops;
128 } else {
129 kfree(iface);
130 }
131}
132#endif
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/**
135 * ipc_findkey - find a key in an ipc identifier set
136 * @ids: Identifier set
137 * @key: The key to find
138 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700139 * Requires ipc_ids.rw_mutex locked.
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700140 * Returns the LOCKED pointer to the ipc structure if found or NULL
141 * if not.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700142 * If key is found ipc points to the owning ipc structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
144
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700145static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700147 struct kern_ipc_perm *ipc;
148 int next_id;
149 int total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700151 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
152 ipc = idr_find(&ids->ipcs_idr, next_id);
153
154 if (ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700156
157 if (ipc->key != key) {
158 total++;
159 continue;
160 }
161
162 ipc_lock_by_ptr(ipc);
163 return ipc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700165
166 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700169/**
170 * ipc_get_maxid - get the last assigned id
171 * @ids: IPC identifier set
172 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700173 * Called with ipc_ids.rw_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700175
176int ipc_get_maxid(struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700178 struct kern_ipc_perm *ipc;
179 int max_id = -1;
180 int total, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700182 if (ids->in_use == 0)
183 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700185 if (ids->in_use == IPCMNI)
186 return IPCMNI - 1;
187
188 /* Look for the last assigned id */
189 total = 0;
190 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
191 ipc = idr_find(&ids->ipcs_idr, id);
192 if (ipc != NULL) {
193 max_id = id;
194 total++;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700197 return max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/**
201 * ipc_addid - add an IPC identifier
202 * @ids: IPC identifier set
203 * @new: new IPC permission set
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700204 * @size: limit for the number of used ids
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700206 * Add an entry 'new' to the IPC ids idr. The permissions object is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * initialised and the first free entry is set up and the id assigned
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700208 * is returned. The 'new' entry is returned in a locked state on success.
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700209 * On failure the entry is not locked and a negative err-code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700211 * Called with ipc_ids.rw_mutex held as a writer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
213
214int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
215{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700216 int id, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700218 if (size > IPCMNI)
219 size = IPCMNI;
220
221 if (ids->in_use >= size)
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700222 return -ENOSPC;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700223
224 err = idr_get_new(&ids->ipcs_idr, new, &id);
225 if (err)
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700226 return err;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ids->in_use++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 new->cuid = new->uid = current->euid;
231 new->gid = new->cgid = current->egid;
232
233 new->seq = ids->seq++;
234 if(ids->seq > ids->seq_max)
235 ids->seq = 0;
236
Pierre Peiffer48dea402008-04-29 01:00:35 -0700237 new->id = ipc_buildid(id, new->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_lock_init(&new->lock);
239 new->deleted = 0;
240 rcu_read_lock();
241 spin_lock(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return id;
243}
244
245/**
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700246 * ipcget_new - create a new ipc object
247 * @ns: namespace
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700248 * @ids: IPC identifer set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700249 * @ops: the actual creation routine to call
250 * @params: its parameters
251 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700252 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
253 * when the key is IPC_PRIVATE.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700254 */
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800255static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700256 struct ipc_ops *ops, struct ipc_params *params)
257{
258 int err;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700259retry:
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700260 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
261
262 if (!err)
263 return -ENOMEM;
264
Nadia Derbey3e148c72007-10-18 23:40:54 -0700265 down_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700266 err = ops->getnew(ns, params);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700267 up_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700268
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700269 if (err == -EAGAIN)
270 goto retry;
271
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700272 return err;
273}
274
275/**
276 * ipc_check_perms - check security and permissions for an IPC
277 * @ipcp: ipc permission set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700278 * @ops: the actual security routine to call
279 * @params: its parameters
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700280 *
281 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
282 * when the key is not IPC_PRIVATE and that key already exists in the
283 * ids IDR.
284 *
285 * On success, the IPC id is returned.
286 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700287 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700288 */
289static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
290 struct ipc_params *params)
291{
292 int err;
293
294 if (ipcperms(ipcp, params->flg))
295 err = -EACCES;
296 else {
297 err = ops->associate(ipcp, params->flg);
298 if (!err)
299 err = ipcp->id;
300 }
301
302 return err;
303}
304
305/**
306 * ipcget_public - get an ipc object or create a new one
307 * @ns: namespace
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700308 * @ids: IPC identifer set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700309 * @ops: the actual creation routine to call
310 * @params: its parameters
311 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700312 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
313 * when the key is not IPC_PRIVATE.
314 * It adds a new entry if the key is not found and does some permission
315 * / security checkings if the key is found.
316 *
317 * On success, the ipc id is returned.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700318 */
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800319static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700320 struct ipc_ops *ops, struct ipc_params *params)
321{
322 struct kern_ipc_perm *ipcp;
323 int flg = params->flg;
324 int err;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700325retry:
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700326 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
327
Nadia Derbey3e148c72007-10-18 23:40:54 -0700328 /*
329 * Take the lock as a writer since we are potentially going to add
330 * a new entry + read locks are not "upgradable"
331 */
332 down_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700333 ipcp = ipc_findkey(ids, params->key);
334 if (ipcp == NULL) {
335 /* key not used */
336 if (!(flg & IPC_CREAT))
337 err = -ENOENT;
338 else if (!err)
339 err = -ENOMEM;
340 else
341 err = ops->getnew(ns, params);
342 } else {
343 /* ipc object has been locked by ipc_findkey() */
344
345 if (flg & IPC_CREAT && flg & IPC_EXCL)
346 err = -EEXIST;
347 else {
348 err = 0;
349 if (ops->more_checks)
350 err = ops->more_checks(ipcp, params);
351 if (!err)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700352 /*
353 * ipc_check_perms returns the IPC id on
354 * success
355 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700356 err = ipc_check_perms(ipcp, ops, params);
357 }
358 ipc_unlock(ipcp);
359 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700360 up_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700361
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700362 if (err == -EAGAIN)
363 goto retry;
364
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700365 return err;
366}
367
368
369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * ipc_rmid - remove an IPC identifier
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700371 * @ids: IPC identifier set
372 * @ipcp: ipc perm structure containing the identifier to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700374 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
375 * before this function is called, and remain locked on the exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
377
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700378void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Nadia Derbeyce621f52007-10-18 23:40:52 -0700380 int lid = ipcid_to_idx(ipcp->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700382 idr_remove(&ids->ipcs_idr, lid);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 ids->in_use--;
385
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700386 ipcp->deleted = 1;
387
388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/**
392 * ipc_alloc - allocate ipc space
393 * @size: size desired
394 *
395 * Allocate memory from the appropriate pools and return a pointer to it.
396 * NULL is returned if the allocation fails
397 */
398
399void* ipc_alloc(int size)
400{
401 void* out;
402 if(size > PAGE_SIZE)
403 out = vmalloc(size);
404 else
405 out = kmalloc(size, GFP_KERNEL);
406 return out;
407}
408
409/**
410 * ipc_free - free ipc space
411 * @ptr: pointer returned by ipc_alloc
412 * @size: size of block
413 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800414 * Free a block created with ipc_alloc(). The caller must know the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * used in the allocation call.
416 */
417
418void ipc_free(void* ptr, int size)
419{
420 if(size > PAGE_SIZE)
421 vfree(ptr);
422 else
423 kfree(ptr);
424}
425
426/*
427 * rcu allocations:
428 * There are three headers that are prepended to the actual allocation:
429 * - during use: ipc_rcu_hdr.
430 * - during the rcu grace period: ipc_rcu_grace.
431 * - [only if vmalloc]: ipc_rcu_sched.
432 * Their lifetime doesn't overlap, thus the headers share the same memory.
433 * Unlike a normal union, they are right-aligned, thus some container_of
434 * forward/backward casting is necessary:
435 */
436struct ipc_rcu_hdr
437{
438 int refcount;
439 int is_vmalloc;
440 void *data[0];
441};
442
443
444struct ipc_rcu_grace
445{
446 struct rcu_head rcu;
447 /* "void *" makes sure alignment of following data is sane. */
448 void *data[0];
449};
450
451struct ipc_rcu_sched
452{
453 struct work_struct work;
454 /* "void *" makes sure alignment of following data is sane. */
455 void *data[0];
456};
457
458#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
459 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
460#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
461 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
462
463static inline int rcu_use_vmalloc(int size)
464{
465 /* Too big for a single page? */
466 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
467 return 1;
468 return 0;
469}
470
471/**
472 * ipc_rcu_alloc - allocate ipc and rcu space
473 * @size: size desired
474 *
475 * Allocate memory for the rcu header structure + the object.
476 * Returns the pointer to the object.
477 * NULL is returned if the allocation fails.
478 */
479
480void* ipc_rcu_alloc(int size)
481{
482 void* out;
483 /*
484 * We prepend the allocation with the rcu struct, and
485 * workqueue if necessary (for vmalloc).
486 */
487 if (rcu_use_vmalloc(size)) {
488 out = vmalloc(HDRLEN_VMALLOC + size);
489 if (out) {
490 out += HDRLEN_VMALLOC;
491 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
492 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
493 }
494 } else {
495 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
496 if (out) {
497 out += HDRLEN_KMALLOC;
498 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
499 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
500 }
501 }
502
503 return out;
504}
505
506void ipc_rcu_getref(void *ptr)
507{
508 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
509}
510
David Howells65f27f32006-11-22 14:55:48 +0000511static void ipc_do_vfree(struct work_struct *work)
512{
513 vfree(container_of(work, struct ipc_rcu_sched, work));
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800517 * ipc_schedule_free - free ipc + rcu space
518 * @head: RCU callback structure for queued work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *
520 * Since RCU callback function is called in bh,
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800521 * we need to defer the vfree to schedule_work().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523static void ipc_schedule_free(struct rcu_head *head)
524{
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700525 struct ipc_rcu_grace *grace;
526 struct ipc_rcu_sched *sched;
527
528 grace = container_of(head, struct ipc_rcu_grace, rcu);
529 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
530 data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
David Howells65f27f32006-11-22 14:55:48 +0000532 INIT_WORK(&sched->work, ipc_do_vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 schedule_work(&sched->work);
534}
535
536/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800537 * ipc_immediate_free - free ipc + rcu space
538 * @head: RCU callback structure that contains pointer to be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800540 * Free from the RCU callback context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static void ipc_immediate_free(struct rcu_head *head)
543{
544 struct ipc_rcu_grace *free =
545 container_of(head, struct ipc_rcu_grace, rcu);
546 kfree(free);
547}
548
549void ipc_rcu_putref(void *ptr)
550{
551 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
552 return;
553
554 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
555 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
556 ipc_schedule_free);
557 } else {
558 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
559 ipc_immediate_free);
560 }
561}
562
563/**
564 * ipcperms - check IPC permissions
565 * @ipcp: IPC permission set
566 * @flag: desired permission set.
567 *
568 * Check user, group, other permissions for access
569 * to ipc resources. return 0 if allowed
570 */
571
572int ipcperms (struct kern_ipc_perm *ipcp, short flag)
573{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
Steve Grubb073115d2006-04-02 17:07:33 -0400574 int requested_mode, granted_mode, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Steve Grubb073115d2006-04-02 17:07:33 -0400576 if (unlikely((err = audit_ipc_obj(ipcp))))
577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 requested_mode = (flag >> 6) | (flag >> 3) | flag;
579 granted_mode = ipcp->mode;
580 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
581 granted_mode >>= 6;
582 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
583 granted_mode >>= 3;
584 /* is there some bit set in requested_mode but not in granted_mode? */
585 if ((requested_mode & ~granted_mode & 0007) &&
586 !capable(CAP_IPC_OWNER))
587 return -1;
588
589 return security_ipc_permission(ipcp, flag);
590}
591
592/*
593 * Functions to convert between the kern_ipc_perm structure and the
594 * old/new ipc_perm structures
595 */
596
597/**
598 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
599 * @in: kernel permissions
600 * @out: new style IPC permissions
601 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800602 * Turn the kernel object @in into a set of permissions descriptions
603 * for returning to userspace (@out).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605
606
607void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
608{
609 out->key = in->key;
610 out->uid = in->uid;
611 out->gid = in->gid;
612 out->cuid = in->cuid;
613 out->cgid = in->cgid;
614 out->mode = in->mode;
615 out->seq = in->seq;
616}
617
618/**
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700619 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * @in: new style IPC permissions
621 * @out: old style IPC permissions
622 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800623 * Turn the new style permissions object @in into a compatibility
624 * object and store it into the @out pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
626
627void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
628{
629 out->key = in->key;
630 SET_UID(out->uid, in->uid);
631 SET_GID(out->gid, in->gid);
632 SET_UID(out->cuid, in->cuid);
633 SET_GID(out->cgid, in->cgid);
634 out->mode = in->mode;
635 out->seq = in->seq;
636}
637
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700638/**
Nadia Derbey3e148c72007-10-18 23:40:54 -0700639 * ipc_lock - Lock an ipc structure without rw_mutex held
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700640 * @ids: IPC identifier set
641 * @id: ipc id to look for
642 *
643 * Look for an id in the ipc ids idr and lock the associated ipc object.
644 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700645 * The ipc object is locked on exit.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700646 *
647 * This is the routine that should be called when the rw_mutex is not already
648 * held, i.e. idr tree not protected: it protects the idr tree in read mode
649 * during the idr_find().
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700650 */
651
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700652struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700654 struct kern_ipc_perm *out;
Nadia Derbeyce621f52007-10-18 23:40:52 -0700655 int lid = ipcid_to_idx(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Nadia Derbey3e148c72007-10-18 23:40:54 -0700657 down_read(&ids->rw_mutex);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rcu_read_lock();
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700660 out = idr_find(&ids->ipcs_idr, lid);
661 if (out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 rcu_read_unlock();
Nadia Derbey3e148c72007-10-18 23:40:54 -0700663 up_read(&ids->rw_mutex);
Nadia Derbey023a5352007-10-18 23:40:51 -0700664 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700666
Nadia Derbey3e148c72007-10-18 23:40:54 -0700667 up_read(&ids->rw_mutex);
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 spin_lock(&out->lock);
670
671 /* ipc_rmid() may have already freed the ID while ipc_lock
672 * was spinning: here verify that the structure is still valid
673 */
674 if (out->deleted) {
675 spin_unlock(&out->lock);
676 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -0700677 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return out;
681}
682
Nadia Derbey3e148c72007-10-18 23:40:54 -0700683/**
684 * ipc_lock_down - Lock an ipc structure with rw_sem held
685 * @ids: IPC identifier set
686 * @id: ipc id to look for
687 *
688 * Look for an id in the ipc ids idr and lock the associated ipc object.
689 *
690 * The ipc object is locked on exit.
691 *
692 * This is the routine that should be called when the rw_mutex is already
693 * held, i.e. idr tree protected.
694 */
695
696struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
697{
698 struct kern_ipc_perm *out;
699 int lid = ipcid_to_idx(id);
700
701 rcu_read_lock();
702 out = idr_find(&ids->ipcs_idr, lid);
703 if (out == NULL) {
704 rcu_read_unlock();
705 return ERR_PTR(-EINVAL);
706 }
707
708 spin_lock(&out->lock);
709
710 /*
711 * No need to verify that the structure is still valid since the
712 * rw_mutex is held.
713 */
714 return out;
715}
716
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800717struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
718{
719 struct kern_ipc_perm *out;
720
721 out = ipc_lock_down(ids, id);
722 if (IS_ERR(out))
723 return out;
724
725 if (ipc_checkid(out, id)) {
726 ipc_unlock(out);
727 return ERR_PTR(-EIDRM);
728 }
729
730 return out;
731}
732
733struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
734{
735 struct kern_ipc_perm *out;
736
737 out = ipc_lock(ids, id);
738 if (IS_ERR(out))
739 return out;
740
741 if (ipc_checkid(out, id)) {
742 ipc_unlock(out);
743 return ERR_PTR(-EIDRM);
744 }
745
746 return out;
747}
748
749/**
750 * ipcget - Common sys_*get() code
751 * @ns : namsepace
752 * @ids : IPC identifier set
753 * @ops : operations to be called on ipc object creation, permission checks
754 * and further checks
755 * @params : the parameters needed by the previous operations.
756 *
757 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
758 */
759int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
760 struct ipc_ops *ops, struct ipc_params *params)
761{
762 if (params->key == IPC_PRIVATE)
763 return ipcget_new(ns, ids, ops, params);
764 else
765 return ipcget_public(ns, ids, ops, params);
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#ifdef __ARCH_WANT_IPC_PARSE_VERSION
769
770
771/**
772 * ipc_parse_version - IPC call version
773 * @cmd: pointer to command
774 *
775 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800776 * The @cmd value is turned from an encoding command and version into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * just the command code.
778 */
779
780int ipc_parse_version (int *cmd)
781{
782 if (*cmd & IPC_64) {
783 *cmd ^= IPC_64;
784 return IPC_64;
785 } else {
786 return IPC_OLD;
787 }
788}
789
790#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
Mike Waychisonae781772005-09-06 15:17:09 -0700791
792#ifdef CONFIG_PROC_FS
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800793struct ipc_proc_iter {
794 struct ipc_namespace *ns;
795 struct ipc_proc_iface *iface;
796};
797
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700798/*
799 * This routine locks the ipc structure found at least at position pos.
800 */
Adrian Bunkb524b9a2008-02-06 01:36:28 -0800801static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
802 loff_t *new_pos)
Mike Waychisonae781772005-09-06 15:17:09 -0700803{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700804 struct kern_ipc_perm *ipc;
805 int total, id;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700806
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700807 total = 0;
808 for (id = 0; id < pos && total < ids->in_use; id++) {
809 ipc = idr_find(&ids->ipcs_idr, id);
810 if (ipc != NULL)
811 total++;
812 }
Mike Waychisonae781772005-09-06 15:17:09 -0700813
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700814 if (total >= ids->in_use)
815 return NULL;
Mike Waychisonae781772005-09-06 15:17:09 -0700816
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700817 for ( ; pos < IPCMNI; pos++) {
818 ipc = idr_find(&ids->ipcs_idr, pos);
819 if (ipc != NULL) {
820 *new_pos = pos + 1;
821 ipc_lock_by_ptr(ipc);
Mike Waychisonae781772005-09-06 15:17:09 -0700822 return ipc;
823 }
824 }
825
826 /* Out of range - return NULL to terminate iteration */
827 return NULL;
828}
829
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700830static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
831{
832 struct ipc_proc_iter *iter = s->private;
833 struct ipc_proc_iface *iface = iter->iface;
834 struct kern_ipc_perm *ipc = it;
835
836 /* If we had an ipc id locked before, unlock it */
837 if (ipc && ipc != SEQ_START_TOKEN)
838 ipc_unlock(ipc);
839
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800840 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700841}
842
Mike Waychisonae781772005-09-06 15:17:09 -0700843/*
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700844 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
845 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
Mike Waychisonae781772005-09-06 15:17:09 -0700846 */
847static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
848{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800849 struct ipc_proc_iter *iter = s->private;
850 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700851 struct ipc_ids *ids;
852
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800853 ids = &iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700854
855 /*
856 * Take the lock - this will be released by the corresponding
857 * call to stop().
858 */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700859 down_read(&ids->rw_mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700860
861 /* pos < 0 is invalid */
862 if (*pos < 0)
863 return NULL;
864
865 /* pos == 0 means header */
866 if (*pos == 0)
867 return SEQ_START_TOKEN;
868
869 /* Find the (pos-1)th ipc */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700870 return sysvipc_find_ipc(ids, *pos - 1, pos);
Mike Waychisonae781772005-09-06 15:17:09 -0700871}
872
873static void sysvipc_proc_stop(struct seq_file *s, void *it)
874{
875 struct kern_ipc_perm *ipc = it;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800876 struct ipc_proc_iter *iter = s->private;
877 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700878 struct ipc_ids *ids;
Mike Waychisonae781772005-09-06 15:17:09 -0700879
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700880 /* If we had a locked structure, release it */
Mike Waychisonae781772005-09-06 15:17:09 -0700881 if (ipc && ipc != SEQ_START_TOKEN)
882 ipc_unlock(ipc);
883
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800884 ids = &iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700885 /* Release the lock we took in start() */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700886 up_read(&ids->rw_mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700887}
888
889static int sysvipc_proc_show(struct seq_file *s, void *it)
890{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800891 struct ipc_proc_iter *iter = s->private;
892 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700893
894 if (it == SEQ_START_TOKEN)
895 return seq_puts(s, iface->header);
896
897 return iface->show(s, it);
898}
899
900static struct seq_operations sysvipc_proc_seqops = {
901 .start = sysvipc_proc_start,
902 .stop = sysvipc_proc_stop,
903 .next = sysvipc_proc_next,
904 .show = sysvipc_proc_show,
905};
906
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800907static int sysvipc_proc_open(struct inode *inode, struct file *file)
908{
Mike Waychisonae781772005-09-06 15:17:09 -0700909 int ret;
910 struct seq_file *seq;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800911 struct ipc_proc_iter *iter;
912
913 ret = -ENOMEM;
914 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
915 if (!iter)
916 goto out;
Mike Waychisonae781772005-09-06 15:17:09 -0700917
918 ret = seq_open(file, &sysvipc_proc_seqops);
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800919 if (ret)
920 goto out_kfree;
921
922 seq = file->private_data;
923 seq->private = iter;
924
925 iter->iface = PDE(inode)->data;
926 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
927out:
Mike Waychisonae781772005-09-06 15:17:09 -0700928 return ret;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800929out_kfree:
930 kfree(iter);
931 goto out;
932}
933
934static int sysvipc_proc_release(struct inode *inode, struct file *file)
935{
936 struct seq_file *seq = file->private_data;
937 struct ipc_proc_iter *iter = seq->private;
938 put_ipc_ns(iter->ns);
939 return seq_release_private(inode, file);
Mike Waychisonae781772005-09-06 15:17:09 -0700940}
941
Arjan van de Ven9a321442007-02-12 00:55:35 -0800942static const struct file_operations sysvipc_proc_fops = {
Mike Waychisonae781772005-09-06 15:17:09 -0700943 .open = sysvipc_proc_open,
944 .read = seq_read,
945 .llseek = seq_lseek,
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800946 .release = sysvipc_proc_release,
Mike Waychisonae781772005-09-06 15:17:09 -0700947};
948#endif /* CONFIG_PROC_FS */