util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. *
  375. * Free a block created with ipc_alloc().
  376. */
  377. void ipc_free(void *ptr)
  378. {
  379. kvfree(ptr);
  380. }
  381. /**
  382. * ipc_rcu_alloc - allocate ipc and rcu space
  383. * @size: size desired
  384. *
  385. * Allocate memory for the rcu header structure + the object.
  386. * Returns the pointer to the object or NULL upon failure.
  387. */
  388. void *ipc_rcu_alloc(int size)
  389. {
  390. /*
  391. * We prepend the allocation with the rcu struct
  392. */
  393. struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size);
  394. if (unlikely(!out))
  395. return NULL;
  396. atomic_set(&out->refcount, 1);
  397. return out + 1;
  398. }
  399. int ipc_rcu_getref(void *ptr)
  400. {
  401. struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
  402. return atomic_inc_not_zero(&p->refcount);
  403. }
  404. void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head))
  405. {
  406. struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
  407. if (!atomic_dec_and_test(&p->refcount))
  408. return;
  409. call_rcu(&p->rcu, func);
  410. }
  411. void ipc_rcu_free(struct rcu_head *head)
  412. {
  413. struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
  414. kvfree(p);
  415. }
  416. /**
  417. * ipcperms - check ipc permissions
  418. * @ns: ipc namespace
  419. * @ipcp: ipc permission set
  420. * @flag: desired permission set
  421. *
  422. * Check user, group, other permissions for access
  423. * to ipc resources. return 0 if allowed
  424. *
  425. * @flag will most probably be 0 or S_...UGO from <linux/stat.h>
  426. */
  427. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  428. {
  429. kuid_t euid = current_euid();
  430. int requested_mode, granted_mode;
  431. audit_ipc_obj(ipcp);
  432. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  433. granted_mode = ipcp->mode;
  434. if (uid_eq(euid, ipcp->cuid) ||
  435. uid_eq(euid, ipcp->uid))
  436. granted_mode >>= 6;
  437. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  438. granted_mode >>= 3;
  439. /* is there some bit set in requested_mode but not in granted_mode? */
  440. if ((requested_mode & ~granted_mode & 0007) &&
  441. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  442. return -1;
  443. return security_ipc_permission(ipcp, flag);
  444. }
  445. /*
  446. * Functions to convert between the kern_ipc_perm structure and the
  447. * old/new ipc_perm structures
  448. */
  449. /**
  450. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  451. * @in: kernel permissions
  452. * @out: new style ipc permissions
  453. *
  454. * Turn the kernel object @in into a set of permissions descriptions
  455. * for returning to userspace (@out).
  456. */
  457. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
  458. {
  459. out->key = in->key;
  460. out->uid = from_kuid_munged(current_user_ns(), in->uid);
  461. out->gid = from_kgid_munged(current_user_ns(), in->gid);
  462. out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
  463. out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
  464. out->mode = in->mode;
  465. out->seq = in->seq;
  466. }
  467. /**
  468. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  469. * @in: new style ipc permissions
  470. * @out: old style ipc permissions
  471. *
  472. * Turn the new style permissions object @in into a compatibility
  473. * object and store it into the @out pointer.
  474. */
  475. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
  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. /**
  486. * ipc_obtain_object
  487. * @ids: ipc identifier set
  488. * @id: ipc id to look for
  489. *
  490. * Look for an id in the ipc ids idr and return associated ipc object.
  491. *
  492. * Call inside the RCU critical section.
  493. * The ipc object is *not* locked on exit.
  494. */
  495. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
  496. {
  497. struct kern_ipc_perm *out;
  498. int lid = ipcid_to_idx(id);
  499. out = idr_find(&ids->ipcs_idr, lid);
  500. if (!out)
  501. return ERR_PTR(-EINVAL);
  502. return out;
  503. }
  504. /**
  505. * ipc_lock - lock an ipc structure without rwsem held
  506. * @ids: ipc identifier set
  507. * @id: ipc id to look for
  508. *
  509. * Look for an id in the ipc ids idr and lock the associated ipc object.
  510. *
  511. * The ipc object is locked on successful exit.
  512. */
  513. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  514. {
  515. struct kern_ipc_perm *out;
  516. rcu_read_lock();
  517. out = ipc_obtain_object_idr(ids, id);
  518. if (IS_ERR(out))
  519. goto err;
  520. spin_lock(&out->lock);
  521. /*
  522. * ipc_rmid() may have already freed the ID while ipc_lock()
  523. * was spinning: here verify that the structure is still valid.
  524. * Upon races with RMID, return -EIDRM, thus indicating that
  525. * the ID points to a removed identifier.
  526. */
  527. if (ipc_valid_object(out))
  528. return out;
  529. spin_unlock(&out->lock);
  530. out = ERR_PTR(-EIDRM);
  531. err:
  532. rcu_read_unlock();
  533. return out;
  534. }
  535. /**
  536. * ipc_obtain_object_check
  537. * @ids: ipc identifier set
  538. * @id: ipc id to look for
  539. *
  540. * Similar to ipc_obtain_object_idr() but also checks
  541. * the ipc object reference counter.
  542. *
  543. * Call inside the RCU critical section.
  544. * The ipc object is *not* locked on exit.
  545. */
  546. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  547. {
  548. struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
  549. if (IS_ERR(out))
  550. goto out;
  551. if (ipc_checkid(out, id))
  552. return ERR_PTR(-EINVAL);
  553. out:
  554. return out;
  555. }
  556. /**
  557. * ipcget - Common sys_*get() code
  558. * @ns: namespace
  559. * @ids: ipc identifier set
  560. * @ops: operations to be called on ipc object creation, permission checks
  561. * and further checks
  562. * @params: the parameters needed by the previous operations.
  563. *
  564. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  565. */
  566. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  567. const struct ipc_ops *ops, struct ipc_params *params)
  568. {
  569. if (params->key == IPC_PRIVATE)
  570. return ipcget_new(ns, ids, ops, params);
  571. else
  572. return ipcget_public(ns, ids, ops, params);
  573. }
  574. /**
  575. * ipc_update_perm - update the permissions of an ipc object
  576. * @in: the permission given as input.
  577. * @out: the permission of the ipc to set.
  578. */
  579. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  580. {
  581. kuid_t uid = make_kuid(current_user_ns(), in->uid);
  582. kgid_t gid = make_kgid(current_user_ns(), in->gid);
  583. if (!uid_valid(uid) || !gid_valid(gid))
  584. return -EINVAL;
  585. out->uid = uid;
  586. out->gid = gid;
  587. out->mode = (out->mode & ~S_IRWXUGO)
  588. | (in->mode & S_IRWXUGO);
  589. return 0;
  590. }
  591. /**
  592. * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
  593. * @ns: ipc namespace
  594. * @ids: the table of ids where to look for the ipc
  595. * @id: the id of the ipc to retrieve
  596. * @cmd: the cmd to check
  597. * @perm: the permission to set
  598. * @extra_perm: one extra permission parameter used by msq
  599. *
  600. * This function does some common audit and permissions check for some IPC_XXX
  601. * cmd and is called from semctl_down, shmctl_down and msgctl_down.
  602. * It must be called without any lock held and
  603. * - retrieves the ipc with the given id in the given table.
  604. * - performs some audit and permission check, depending on the given cmd
  605. * - returns a pointer to the ipc object or otherwise, the corresponding error.
  606. *
  607. * Call holding the both the rwsem and the rcu read lock.
  608. */
  609. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  610. struct ipc_ids *ids, int id, int cmd,
  611. struct ipc64_perm *perm, int extra_perm)
  612. {
  613. kuid_t euid;
  614. int err = -EPERM;
  615. struct kern_ipc_perm *ipcp;
  616. ipcp = ipc_obtain_object_check(ids, id);
  617. if (IS_ERR(ipcp)) {
  618. err = PTR_ERR(ipcp);
  619. goto err;
  620. }
  621. audit_ipc_obj(ipcp);
  622. if (cmd == IPC_SET)
  623. audit_ipc_set_perm(extra_perm, perm->uid,
  624. perm->gid, perm->mode);
  625. euid = current_euid();
  626. if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
  627. ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  628. return ipcp; /* successful lookup */
  629. err:
  630. return ERR_PTR(err);
  631. }
  632. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  633. /**
  634. * ipc_parse_version - ipc call version
  635. * @cmd: pointer to command
  636. *
  637. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  638. * The @cmd value is turned from an encoding command and version into
  639. * just the command code.
  640. */
  641. int ipc_parse_version(int *cmd)
  642. {
  643. if (*cmd & IPC_64) {
  644. *cmd ^= IPC_64;
  645. return IPC_64;
  646. } else {
  647. return IPC_OLD;
  648. }
  649. }
  650. #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
  651. #ifdef CONFIG_PROC_FS
  652. struct ipc_proc_iter {
  653. struct ipc_namespace *ns;
  654. struct ipc_proc_iface *iface;
  655. };
  656. /*
  657. * This routine locks the ipc structure found at least at position pos.
  658. */
  659. static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  660. loff_t *new_pos)
  661. {
  662. struct kern_ipc_perm *ipc;
  663. int total, id;
  664. total = 0;
  665. for (id = 0; id < pos && total < ids->in_use; id++) {
  666. ipc = idr_find(&ids->ipcs_idr, id);
  667. if (ipc != NULL)
  668. total++;
  669. }
  670. if (total >= ids->in_use)
  671. return NULL;
  672. for (; pos < IPCMNI; pos++) {
  673. ipc = idr_find(&ids->ipcs_idr, pos);
  674. if (ipc != NULL) {
  675. *new_pos = pos + 1;
  676. rcu_read_lock();
  677. ipc_lock_object(ipc);
  678. return ipc;
  679. }
  680. }
  681. /* Out of range - return NULL to terminate iteration */
  682. return NULL;
  683. }
  684. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  685. {
  686. struct ipc_proc_iter *iter = s->private;
  687. struct ipc_proc_iface *iface = iter->iface;
  688. struct kern_ipc_perm *ipc = it;
  689. /* If we had an ipc id locked before, unlock it */
  690. if (ipc && ipc != SEQ_START_TOKEN)
  691. ipc_unlock(ipc);
  692. return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
  693. }
  694. /*
  695. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  696. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  697. */
  698. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  699. {
  700. struct ipc_proc_iter *iter = s->private;
  701. struct ipc_proc_iface *iface = iter->iface;
  702. struct ipc_ids *ids;
  703. ids = &iter->ns->ids[iface->ids];
  704. /*
  705. * Take the lock - this will be released by the corresponding
  706. * call to stop().
  707. */
  708. down_read(&ids->rwsem);
  709. /* pos < 0 is invalid */
  710. if (*pos < 0)
  711. return NULL;
  712. /* pos == 0 means header */
  713. if (*pos == 0)
  714. return SEQ_START_TOKEN;
  715. /* Find the (pos-1)th ipc */
  716. return sysvipc_find_ipc(ids, *pos - 1, pos);
  717. }
  718. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  719. {
  720. struct kern_ipc_perm *ipc = it;
  721. struct ipc_proc_iter *iter = s->private;
  722. struct ipc_proc_iface *iface = iter->iface;
  723. struct ipc_ids *ids;
  724. /* If we had a locked structure, release it */
  725. if (ipc && ipc != SEQ_START_TOKEN)
  726. ipc_unlock(ipc);
  727. ids = &iter->ns->ids[iface->ids];
  728. /* Release the lock we took in start() */
  729. up_read(&ids->rwsem);
  730. }
  731. static int sysvipc_proc_show(struct seq_file *s, void *it)
  732. {
  733. struct ipc_proc_iter *iter = s->private;
  734. struct ipc_proc_iface *iface = iter->iface;
  735. if (it == SEQ_START_TOKEN) {
  736. seq_puts(s, iface->header);
  737. return 0;
  738. }
  739. return iface->show(s, it);
  740. }
  741. static const struct seq_operations sysvipc_proc_seqops = {
  742. .start = sysvipc_proc_start,
  743. .stop = sysvipc_proc_stop,
  744. .next = sysvipc_proc_next,
  745. .show = sysvipc_proc_show,
  746. };
  747. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  748. {
  749. struct ipc_proc_iter *iter;
  750. iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
  751. if (!iter)
  752. return -ENOMEM;
  753. iter->iface = PDE_DATA(inode);
  754. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  755. return 0;
  756. }
  757. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  758. {
  759. struct seq_file *seq = file->private_data;
  760. struct ipc_proc_iter *iter = seq->private;
  761. put_ipc_ns(iter->ns);
  762. return seq_release_private(inode, file);
  763. }
  764. static const struct file_operations sysvipc_proc_fops = {
  765. .open = sysvipc_proc_open,
  766. .read = seq_read,
  767. .llseek = seq_lseek,
  768. .release = sysvipc_proc_release,
  769. };
  770. #endif /* CONFIG_PROC_FS */