]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - ipc/util.c
ipc/util: drop ipc_rcu_alloc()
[mirror_ubuntu-bionic-kernel.git] / ipc / util.c
CommitLineData
1da177e4
LT
1/*
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
624dffcb 10 * Manfred Spraul <manfred@colorfullife.com>
1da177e4
LT
11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
073115d6
SG
13 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
73ea4130
KK
15 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
05603c44
DB
18 *
19 * General sysv ipc locking scheme:
18ccee26
DB
20 * rcu_read_lock()
21 * obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
22 * tree.
23 * - perform initial checks (capabilities, auditing and permission,
24 * etc).
25 * - perform read-only operations, such as STAT, INFO commands.
26 * acquire the ipc lock (kern_ipc_perm.lock) through
27 * ipc_lock_object()
28 * - perform data updates, such as SET, RMID commands and
29 * mechanism-specific operations (semop/semtimedop,
30 * msgsnd/msgrcv, shmat/shmdt).
31 * drop the ipc lock, through ipc_unlock_object().
32 * rcu_read_unlock()
33 *
34 * The ids->rwsem must be taken when:
35 * - creating, removing and iterating the existing entries in ipc
36 * identifier sets.
37 * - iterating through files under /proc/sysvipc/
38 *
39 * Note that sems have a special fast path that avoids kern_ipc_perm.lock -
40 * see sem_lock().
1da177e4
LT
41 */
42
1da177e4
LT
43#include <linux/mm.h>
44#include <linux/shm.h>
45#include <linux/init.h>
46#include <linux/msg.h>
1da177e4
LT
47#include <linux/vmalloc.h>
48#include <linux/slab.h>
8f68fa2d 49#include <linux/notifier.h>
c59ede7b 50#include <linux/capability.h>
1da177e4
LT
51#include <linux/highuid.h>
52#include <linux/security.h>
53#include <linux/rcupdate.h>
54#include <linux/workqueue.h>
ae781774
MW
55#include <linux/seq_file.h>
56#include <linux/proc_fs.h>
073115d6 57#include <linux/audit.h>
73ea4130 58#include <linux/nsproxy.h>
3e148c79 59#include <linux/rwsem.h>
b6b337ad 60#include <linux/memory.h>
ae5e1b22 61#include <linux/ipc_namespace.h>
1da177e4
LT
62
63#include <asm/unistd.h>
64
65#include "util.h"
66
ae781774
MW
67struct ipc_proc_iface {
68 const char *path;
69 const char *header;
73ea4130 70 int ids;
ae781774
MW
71 int (*show)(struct seq_file *, void *);
72};
73
1da177e4 74/**
8001c858 75 * ipc_init - initialise ipc subsystem
1da177e4 76 *
8001c858
DB
77 * The various sysv ipc resources (semaphores, messages and shared
78 * memory) are initialised.
79 *
80 * A callback routine is registered into the memory hotplug notifier
81 * chain: since msgmni scales to lowmem this callback routine will be
82 * called upon successful memory add / remove to recompute msmgni.
1da177e4 83 */
1da177e4
LT
84static int __init ipc_init(void)
85{
86 sem_init();
87 msg_init();
88 shm_init();
89 return 0;
90}
6d08a256 91device_initcall(ipc_init);
1da177e4
LT
92
93/**
8001c858
DB
94 * ipc_init_ids - initialise ipc identifiers
95 * @ids: ipc identifier set
1da177e4 96 *
8001c858
DB
97 * Set up the sequence range to use for the ipc identifier range (limited
98 * below IPCMNI) then initialise the ids idr.
1da177e4 99 */
7ca7e564 100void ipc_init_ids(struct ipc_ids *ids)
1da177e4 101{
1da177e4 102 ids->in_use = 0;
1da177e4 103 ids->seq = 0;
03f59566 104 ids->next_id = -1;
daf948c7 105 init_rwsem(&ids->rwsem);
7ca7e564 106 idr_init(&ids->ipcs_idr);
1da177e4
LT
107}
108
ae781774 109#ifdef CONFIG_PROC_FS
9a32144e 110static const struct file_operations sysvipc_proc_fops;
ae781774 111/**
8001c858
DB
112 * ipc_init_proc_interface - create a proc interface for sysipc types using a seq_file interface.
113 * @path: Path in procfs
114 * @header: Banner to be printed at the beginning of the file.
115 * @ids: ipc id table to iterate.
116 * @show: show routine.
ae781774
MW
117 */
118void __init ipc_init_proc_interface(const char *path, const char *header,
73ea4130 119 int ids, int (*show)(struct seq_file *, void *))
ae781774
MW
120{
121 struct proc_dir_entry *pde;
122 struct ipc_proc_iface *iface;
123
124 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
125 if (!iface)
126 return;
127 iface->path = path;
128 iface->header = header;
129 iface->ids = ids;
130 iface->show = show;
131
6a6375db
DL
132 pde = proc_create_data(path,
133 S_IRUGO, /* world readable */
134 NULL, /* parent dir */
135 &sysvipc_proc_fops,
136 iface);
3ab08fe2 137 if (!pde)
ae781774 138 kfree(iface);
ae781774
MW
139}
140#endif
141
1da177e4 142/**
8001c858
DB
143 * ipc_findkey - find a key in an ipc identifier set
144 * @ids: ipc identifier set
145 * @key: key to find
46c0a8ca 146 *
8001c858
DB
147 * Returns the locked pointer to the ipc structure if found or NULL
148 * otherwise. If key is found ipc points to the owning ipc structure
149 *
150 * Called with ipc_ids.rwsem held.
1da177e4 151 */
7748dbfa 152static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
1da177e4 153{
7ca7e564
ND
154 struct kern_ipc_perm *ipc;
155 int next_id;
156 int total;
1da177e4 157
7ca7e564
ND
158 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
159 ipc = idr_find(&ids->ipcs_idr, next_id);
160
161 if (ipc == NULL)
1da177e4 162 continue;
7ca7e564
ND
163
164 if (ipc->key != key) {
165 total++;
166 continue;
167 }
168
32a27500
DB
169 rcu_read_lock();
170 ipc_lock_object(ipc);
7ca7e564 171 return ipc;
1da177e4 172 }
7ca7e564
ND
173
174 return NULL;
1da177e4
LT
175}
176
7ca7e564 177/**
8001c858
DB
178 * ipc_get_maxid - get the last assigned id
179 * @ids: ipc identifier set
7ca7e564 180 *
8001c858 181 * Called with ipc_ids.rwsem held.
1da177e4 182 */
7ca7e564
ND
183int ipc_get_maxid(struct ipc_ids *ids)
184{
185 struct kern_ipc_perm *ipc;
186 int max_id = -1;
187 int total, id;
188
189 if (ids->in_use == 0)
190 return -1;
1da177e4 191
7ca7e564
ND
192 if (ids->in_use == IPCMNI)
193 return IPCMNI - 1;
194
195 /* Look for the last assigned id */
196 total = 0;
197 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
198 ipc = idr_find(&ids->ipcs_idr, id);
199 if (ipc != NULL) {
200 max_id = id;
201 total++;
202 }
203 }
204 return max_id;
1da177e4
LT
205}
206
207/**
8001c858
DB
208 * ipc_addid - add an ipc identifier
209 * @ids: ipc identifier set
210 * @new: new ipc permission set
211 * @size: limit for the number of used ids
1da177e4 212 *
8001c858
DB
213 * Add an entry 'new' to the ipc ids idr. The permissions object is
214 * initialised and the first free entry is set up and the id assigned
215 * is returned. The 'new' entry is returned in a locked state on success.
216 * On failure the entry is not locked and a negative err-code is returned.
1da177e4 217 *
8001c858 218 * Called with writer ipc_ids.rwsem held.
1da177e4 219 */
239521f3 220int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
1da177e4 221{
1efdb69b
EB
222 kuid_t euid;
223 kgid_t egid;
54924ea3 224 int id;
03f59566 225 int next_id = ids->next_id;
1da177e4 226
7ca7e564
ND
227 if (size > IPCMNI)
228 size = IPCMNI;
229
230 if (ids->in_use >= size)
283bb7fa 231 return -ENOSPC;
7ca7e564 232
54924ea3
TH
233 idr_preload(GFP_KERNEL);
234
e00b4ff7 235 spin_lock_init(&new->lock);
72a8ff2f 236 new->deleted = false;
e00b4ff7
ND
237 rcu_read_lock();
238 spin_lock(&new->lock);
239
b9a53227
LT
240 current_euid_egid(&euid, &egid);
241 new->cuid = new->uid = euid;
242 new->gid = new->cgid = egid;
243
54924ea3
TH
244 id = idr_alloc(&ids->ipcs_idr, new,
245 (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
246 GFP_NOWAIT);
247 idr_preload_end();
248 if (id < 0) {
e00b4ff7
ND
249 spin_unlock(&new->lock);
250 rcu_read_unlock();
54924ea3 251 return id;
e00b4ff7 252 }
7ca7e564 253
1da177e4 254 ids->in_use++;
1da177e4 255
03f59566
SK
256 if (next_id < 0) {
257 new->seq = ids->seq++;
daf948c7 258 if (ids->seq > IPCID_SEQ_MAX)
03f59566
SK
259 ids->seq = 0;
260 } else {
261 new->seq = ipcid_to_seqx(next_id);
262 ids->next_id = -1;
263 }
1da177e4 264
48dea404 265 new->id = ipc_buildid(id, new->seq);
1da177e4
LT
266 return id;
267}
268
7748dbfa 269/**
8001c858
DB
270 * ipcget_new - create a new ipc object
271 * @ns: ipc namespace
da3dae54 272 * @ids: ipc identifier set
8001c858
DB
273 * @ops: the actual creation routine to call
274 * @params: its parameters
275 *
276 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
277 * when the key is IPC_PRIVATE.
7748dbfa 278 */
b2d75cdd 279static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44 280 const struct ipc_ops *ops, struct ipc_params *params)
7748dbfa
ND
281{
282 int err;
7748dbfa 283
d9a605e4 284 down_write(&ids->rwsem);
7748dbfa 285 err = ops->getnew(ns, params);
d9a605e4 286 up_write(&ids->rwsem);
7748dbfa
ND
287 return err;
288}
289
290/**
8001c858
DB
291 * ipc_check_perms - check security and permissions for an ipc object
292 * @ns: ipc namespace
293 * @ipcp: ipc permission set
294 * @ops: the actual security routine to call
295 * @params: its parameters
f4566f04 296 *
8001c858
DB
297 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
298 * when the key is not IPC_PRIVATE and that key already exists in the
299 * ds IDR.
f4566f04 300 *
8001c858 301 * On success, the ipc id is returned.
f4566f04 302 *
8001c858 303 * It is called with ipc_ids.rwsem and ipcp->lock held.
7748dbfa 304 */
b0e77598
SH
305static int ipc_check_perms(struct ipc_namespace *ns,
306 struct kern_ipc_perm *ipcp,
eb66ec44 307 const struct ipc_ops *ops,
b0e77598 308 struct ipc_params *params)
7748dbfa
ND
309{
310 int err;
311
b0e77598 312 if (ipcperms(ns, ipcp, params->flg))
7748dbfa
ND
313 err = -EACCES;
314 else {
315 err = ops->associate(ipcp, params->flg);
316 if (!err)
317 err = ipcp->id;
318 }
319
320 return err;
321}
322
323/**
8001c858
DB
324 * ipcget_public - get an ipc object or create a new one
325 * @ns: ipc namespace
da3dae54 326 * @ids: ipc identifier set
8001c858
DB
327 * @ops: the actual creation routine to call
328 * @params: its parameters
329 *
330 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
331 * when the key is not IPC_PRIVATE.
332 * It adds a new entry if the key is not found and does some permission
333 * / security checkings if the key is found.
334 *
335 * On success, the ipc id is returned.
7748dbfa 336 */
b2d75cdd 337static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44 338 const struct ipc_ops *ops, struct ipc_params *params)
7748dbfa
ND
339{
340 struct kern_ipc_perm *ipcp;
341 int flg = params->flg;
342 int err;
7748dbfa 343
3e148c79
ND
344 /*
345 * Take the lock as a writer since we are potentially going to add
346 * a new entry + read locks are not "upgradable"
347 */
d9a605e4 348 down_write(&ids->rwsem);
7748dbfa
ND
349 ipcp = ipc_findkey(ids, params->key);
350 if (ipcp == NULL) {
351 /* key not used */
352 if (!(flg & IPC_CREAT))
353 err = -ENOENT;
7748dbfa
ND
354 else
355 err = ops->getnew(ns, params);
356 } else {
357 /* ipc object has been locked by ipc_findkey() */
358
359 if (flg & IPC_CREAT && flg & IPC_EXCL)
360 err = -EEXIST;
361 else {
362 err = 0;
363 if (ops->more_checks)
364 err = ops->more_checks(ipcp, params);
365 if (!err)
f4566f04
ND
366 /*
367 * ipc_check_perms returns the IPC id on
368 * success
369 */
b0e77598 370 err = ipc_check_perms(ns, ipcp, ops, params);
7748dbfa
ND
371 }
372 ipc_unlock(ipcp);
373 }
d9a605e4 374 up_write(&ids->rwsem);
7748dbfa
ND
375
376 return err;
377}
378
379
1da177e4 380/**
8001c858
DB
381 * ipc_rmid - remove an ipc identifier
382 * @ids: ipc identifier set
383 * @ipcp: ipc perm structure containing the identifier to remove
1da177e4 384 *
8001c858
DB
385 * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
386 * before this function is called, and remain locked on the exit.
1da177e4 387 */
7ca7e564 388void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
1da177e4 389{
ce621f5b 390 int lid = ipcid_to_idx(ipcp->id);
7ca7e564
ND
391
392 idr_remove(&ids->ipcs_idr, lid);
1da177e4 393 ids->in_use--;
72a8ff2f 394 ipcp->deleted = true;
1da177e4
LT
395}
396
dba4cdd3 397int ipc_rcu_getref(struct kern_ipc_perm *ptr)
1da177e4 398{
dba4cdd3 399 return atomic_inc_not_zero(&ptr->refcount);
65f27f38
DH
400}
401
dba4cdd3
MS
402void ipc_rcu_putref(struct kern_ipc_perm *ptr,
403 void (*func)(struct rcu_head *head))
1da177e4 404{
dba4cdd3 405 if (!atomic_dec_and_test(&ptr->refcount))
1da177e4
LT
406 return;
407
dba4cdd3 408 call_rcu(&ptr->rcu, func);
53dad6d3
DB
409}
410
1da177e4 411/**
8001c858
DB
412 * ipcperms - check ipc permissions
413 * @ns: ipc namespace
414 * @ipcp: ipc permission set
415 * @flag: desired permission set
1da177e4 416 *
8001c858
DB
417 * Check user, group, other permissions for access
418 * to ipc resources. return 0 if allowed
b0e77598 419 *
0e056eb5 420 * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
1da177e4 421 */
b0e77598
SH
422int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
423{
1efdb69b 424 kuid_t euid = current_euid();
a33e6751 425 int requested_mode, granted_mode;
1da177e4 426
a33e6751 427 audit_ipc_obj(ipcp);
1da177e4
LT
428 requested_mode = (flag >> 6) | (flag >> 3) | flag;
429 granted_mode = ipcp->mode;
1efdb69b
EB
430 if (uid_eq(euid, ipcp->cuid) ||
431 uid_eq(euid, ipcp->uid))
1da177e4
LT
432 granted_mode >>= 6;
433 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
434 granted_mode >>= 3;
435 /* is there some bit set in requested_mode but not in granted_mode? */
46c0a8ca 436 if ((requested_mode & ~granted_mode & 0007) &&
b0e77598 437 !ns_capable(ns->user_ns, CAP_IPC_OWNER))
1da177e4
LT
438 return -1;
439
440 return security_ipc_permission(ipcp, flag);
441}
442
443/*
444 * Functions to convert between the kern_ipc_perm structure and the
445 * old/new ipc_perm structures
446 */
447
448/**
8001c858
DB
449 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
450 * @in: kernel permissions
451 * @out: new style ipc permissions
1da177e4 452 *
8001c858
DB
453 * Turn the kernel object @in into a set of permissions descriptions
454 * for returning to userspace (@out).
1da177e4 455 */
239521f3 456void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
1da177e4
LT
457{
458 out->key = in->key;
1efdb69b
EB
459 out->uid = from_kuid_munged(current_user_ns(), in->uid);
460 out->gid = from_kgid_munged(current_user_ns(), in->gid);
461 out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
462 out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
1da177e4
LT
463 out->mode = in->mode;
464 out->seq = in->seq;
465}
466
467/**
8001c858
DB
468 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
469 * @in: new style ipc permissions
470 * @out: old style ipc permissions
1da177e4 471 *
8001c858
DB
472 * Turn the new style permissions object @in into a compatibility
473 * object and store it into the @out pointer.
1da177e4 474 */
239521f3 475void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
1da177e4
LT
476{
477 out->key = in->key;
478 SET_UID(out->uid, in->uid);
479 SET_GID(out->gid, in->gid);
480 SET_UID(out->cuid, in->cuid);
481 SET_GID(out->cgid, in->cgid);
482 out->mode = in->mode;
483 out->seq = in->seq;
484}
485
4d2bff5e
DB
486/**
487 * ipc_obtain_object
488 * @ids: ipc identifier set
489 * @id: ipc id to look for
490 *
491 * Look for an id in the ipc ids idr and return associated ipc object.
492 *
493 * Call inside the RCU critical section.
494 * The ipc object is *not* locked on exit.
495 */
55b7ae50 496struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
4d2bff5e
DB
497{
498 struct kern_ipc_perm *out;
499 int lid = ipcid_to_idx(id);
500
501 out = idr_find(&ids->ipcs_idr, lid);
502 if (!out)
503 return ERR_PTR(-EINVAL);
504
505 return out;
506}
507
f4566f04 508/**
8001c858
DB
509 * ipc_lock - lock an ipc structure without rwsem held
510 * @ids: ipc identifier set
f4566f04
ND
511 * @id: ipc id to look for
512 *
513 * Look for an id in the ipc ids idr and lock the associated ipc object.
514 *
4d2bff5e 515 * The ipc object is locked on successful exit.
f4566f04 516 */
7ca7e564 517struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
1da177e4 518{
7ca7e564 519 struct kern_ipc_perm *out;
1da177e4
LT
520
521 rcu_read_lock();
55b7ae50 522 out = ipc_obtain_object_idr(ids, id);
4d2bff5e 523 if (IS_ERR(out))
f8b59184 524 goto err;
7ca7e564 525
1da177e4 526 spin_lock(&out->lock);
4d2bff5e 527
f8b59184
DB
528 /*
529 * ipc_rmid() may have already freed the ID while ipc_lock()
530 * was spinning: here verify that the structure is still valid.
531 * Upon races with RMID, return -EIDRM, thus indicating that
532 * the ID points to a removed identifier.
1da177e4 533 */
72a8ff2f 534 if (ipc_valid_object(out))
4d2bff5e
DB
535 return out;
536
537 spin_unlock(&out->lock);
f8b59184
DB
538 out = ERR_PTR(-EIDRM);
539err:
4d2bff5e
DB
540 rcu_read_unlock();
541 return out;
542}
7ca7e564 543
4d2bff5e
DB
544/**
545 * ipc_obtain_object_check
546 * @ids: ipc identifier set
547 * @id: ipc id to look for
548 *
55b7ae50 549 * Similar to ipc_obtain_object_idr() but also checks
4d2bff5e
DB
550 * the ipc object reference counter.
551 *
552 * Call inside the RCU critical section.
553 * The ipc object is *not* locked on exit.
554 */
555struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
556{
55b7ae50 557 struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
4d2bff5e
DB
558
559 if (IS_ERR(out))
560 goto out;
561
562 if (ipc_checkid(out, id))
6157dbbf 563 return ERR_PTR(-EINVAL);
4d2bff5e 564out:
1da177e4
LT
565 return out;
566}
567
b2d75cdd
PE
568/**
569 * ipcget - Common sys_*get() code
da3dae54 570 * @ns: namespace
8001c858
DB
571 * @ids: ipc identifier set
572 * @ops: operations to be called on ipc object creation, permission checks
573 * and further checks
574 * @params: the parameters needed by the previous operations.
b2d75cdd
PE
575 *
576 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
577 */
578int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44 579 const struct ipc_ops *ops, struct ipc_params *params)
b2d75cdd
PE
580{
581 if (params->key == IPC_PRIVATE)
582 return ipcget_new(ns, ids, ops, params);
583 else
584 return ipcget_public(ns, ids, ops, params);
585}
586
8f4a3809 587/**
8001c858 588 * ipc_update_perm - update the permissions of an ipc object
8f4a3809
PP
589 * @in: the permission given as input.
590 * @out: the permission of the ipc to set.
591 */
1efdb69b 592int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
8f4a3809 593{
1efdb69b
EB
594 kuid_t uid = make_kuid(current_user_ns(), in->uid);
595 kgid_t gid = make_kgid(current_user_ns(), in->gid);
596 if (!uid_valid(uid) || !gid_valid(gid))
597 return -EINVAL;
598
599 out->uid = uid;
600 out->gid = gid;
8f4a3809
PP
601 out->mode = (out->mode & ~S_IRWXUGO)
602 | (in->mode & S_IRWXUGO);
1efdb69b
EB
603
604 return 0;
8f4a3809
PP
605}
606
a5f75e7f 607/**
3b1c4ad3 608 * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
8001c858 609 * @ns: ipc namespace
a5f75e7f
PP
610 * @ids: the table of ids where to look for the ipc
611 * @id: the id of the ipc to retrieve
612 * @cmd: the cmd to check
613 * @perm: the permission to set
614 * @extra_perm: one extra permission parameter used by msq
615 *
616 * This function does some common audit and permissions check for some IPC_XXX
617 * cmd and is called from semctl_down, shmctl_down and msgctl_down.
0e056eb5
MCC
618 * It must be called without any lock held and:
619 *
620 * - retrieves the ipc with the given id in the given table.
621 * - performs some audit and permission check, depending on the given cmd
622 * - returns a pointer to the ipc object or otherwise, the corresponding
623 * error.
7b4cc5d8 624 *
d9a605e4 625 * Call holding the both the rwsem and the rcu read lock.
a5f75e7f 626 */
444d0f62 627struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
3b1c4ad3
DB
628 struct ipc_ids *ids, int id, int cmd,
629 struct ipc64_perm *perm, int extra_perm)
444d0f62 630{
1efdb69b 631 kuid_t euid;
444d0f62
DB
632 int err = -EPERM;
633 struct kern_ipc_perm *ipcp;
a5f75e7f 634
444d0f62 635 ipcp = ipc_obtain_object_check(ids, id);
a5f75e7f
PP
636 if (IS_ERR(ipcp)) {
637 err = PTR_ERR(ipcp);
7b4cc5d8 638 goto err;
a5f75e7f
PP
639 }
640
a33e6751 641 audit_ipc_obj(ipcp);
e816f370
AV
642 if (cmd == IPC_SET)
643 audit_ipc_set_perm(extra_perm, perm->uid,
444d0f62 644 perm->gid, perm->mode);
414c0708
DH
645
646 euid = current_euid();
1efdb69b 647 if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
b0e77598 648 ns_capable(ns->user_ns, CAP_SYS_ADMIN))
7b4cc5d8
DB
649 return ipcp; /* successful lookup */
650err:
a5f75e7f
PP
651 return ERR_PTR(err);
652}
653
c1d7e01d 654#ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
1da177e4
LT
655
656
657/**
8001c858
DB
658 * ipc_parse_version - ipc call version
659 * @cmd: pointer to command
1da177e4 660 *
8001c858
DB
661 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
662 * The @cmd value is turned from an encoding command and version into
663 * just the command code.
1da177e4 664 */
239521f3 665int ipc_parse_version(int *cmd)
1da177e4
LT
666{
667 if (*cmd & IPC_64) {
668 *cmd ^= IPC_64;
669 return IPC_64;
670 } else {
671 return IPC_OLD;
672 }
673}
674
c1d7e01d 675#endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
ae781774
MW
676
677#ifdef CONFIG_PROC_FS
bc1fc6d8
EB
678struct ipc_proc_iter {
679 struct ipc_namespace *ns;
680 struct ipc_proc_iface *iface;
681};
682
7ca7e564
ND
683/*
684 * This routine locks the ipc structure found at least at position pos.
685 */
b524b9ad
AB
686static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
687 loff_t *new_pos)
ae781774 688{
7ca7e564
ND
689 struct kern_ipc_perm *ipc;
690 int total, id;
73ea4130 691
7ca7e564
ND
692 total = 0;
693 for (id = 0; id < pos && total < ids->in_use; id++) {
694 ipc = idr_find(&ids->ipcs_idr, id);
695 if (ipc != NULL)
696 total++;
697 }
ae781774 698
7ca7e564
ND
699 if (total >= ids->in_use)
700 return NULL;
ae781774 701
239521f3 702 for (; pos < IPCMNI; pos++) {
7ca7e564
ND
703 ipc = idr_find(&ids->ipcs_idr, pos);
704 if (ipc != NULL) {
705 *new_pos = pos + 1;
32a27500
DB
706 rcu_read_lock();
707 ipc_lock_object(ipc);
ae781774
MW
708 return ipc;
709 }
710 }
711
712 /* Out of range - return NULL to terminate iteration */
713 return NULL;
714}
715
7ca7e564
ND
716static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
717{
718 struct ipc_proc_iter *iter = s->private;
719 struct ipc_proc_iface *iface = iter->iface;
720 struct kern_ipc_perm *ipc = it;
721
722 /* If we had an ipc id locked before, unlock it */
723 if (ipc && ipc != SEQ_START_TOKEN)
724 ipc_unlock(ipc);
725
ed2ddbf8 726 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
7ca7e564
ND
727}
728
ae781774 729/*
f4566f04
ND
730 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
731 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
ae781774
MW
732 */
733static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
734{
bc1fc6d8
EB
735 struct ipc_proc_iter *iter = s->private;
736 struct ipc_proc_iface *iface = iter->iface;
73ea4130
KK
737 struct ipc_ids *ids;
738
ed2ddbf8 739 ids = &iter->ns->ids[iface->ids];
ae781774
MW
740
741 /*
742 * Take the lock - this will be released by the corresponding
743 * call to stop().
744 */
d9a605e4 745 down_read(&ids->rwsem);
ae781774
MW
746
747 /* pos < 0 is invalid */
748 if (*pos < 0)
749 return NULL;
750
751 /* pos == 0 means header */
752 if (*pos == 0)
753 return SEQ_START_TOKEN;
754
755 /* Find the (pos-1)th ipc */
7ca7e564 756 return sysvipc_find_ipc(ids, *pos - 1, pos);
ae781774
MW
757}
758
759static void sysvipc_proc_stop(struct seq_file *s, void *it)
760{
761 struct kern_ipc_perm *ipc = it;
bc1fc6d8
EB
762 struct ipc_proc_iter *iter = s->private;
763 struct ipc_proc_iface *iface = iter->iface;
73ea4130 764 struct ipc_ids *ids;
ae781774 765
f4566f04 766 /* If we had a locked structure, release it */
ae781774
MW
767 if (ipc && ipc != SEQ_START_TOKEN)
768 ipc_unlock(ipc);
769
ed2ddbf8 770 ids = &iter->ns->ids[iface->ids];
ae781774 771 /* Release the lock we took in start() */
d9a605e4 772 up_read(&ids->rwsem);
ae781774
MW
773}
774
775static int sysvipc_proc_show(struct seq_file *s, void *it)
776{
bc1fc6d8
EB
777 struct ipc_proc_iter *iter = s->private;
778 struct ipc_proc_iface *iface = iter->iface;
ae781774 779
7f032d6e
JP
780 if (it == SEQ_START_TOKEN) {
781 seq_puts(s, iface->header);
782 return 0;
783 }
ae781774
MW
784
785 return iface->show(s, it);
786}
787
88e9d34c 788static const struct seq_operations sysvipc_proc_seqops = {
ae781774
MW
789 .start = sysvipc_proc_start,
790 .stop = sysvipc_proc_stop,
791 .next = sysvipc_proc_next,
792 .show = sysvipc_proc_show,
793};
794
bc1fc6d8
EB
795static int sysvipc_proc_open(struct inode *inode, struct file *file)
796{
bc1fc6d8
EB
797 struct ipc_proc_iter *iter;
798
d66a0520 799 iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
bc1fc6d8 800 if (!iter)
d66a0520 801 return -ENOMEM;
bc1fc6d8 802
d9dda78b 803 iter->iface = PDE_DATA(inode);
bc1fc6d8 804 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
d66a0520
RJ
805
806 return 0;
bc1fc6d8
EB
807}
808
809static int sysvipc_proc_release(struct inode *inode, struct file *file)
810{
811 struct seq_file *seq = file->private_data;
812 struct ipc_proc_iter *iter = seq->private;
813 put_ipc_ns(iter->ns);
814 return seq_release_private(inode, file);
ae781774
MW
815}
816
9a32144e 817static const struct file_operations sysvipc_proc_fops = {
ae781774
MW
818 .open = sysvipc_proc_open,
819 .read = seq_read,
820 .llseek = seq_lseek,
bc1fc6d8 821 .release = sysvipc_proc_release,
ae781774
MW
822};
823#endif /* CONFIG_PROC_FS */