util.c 21 KB

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