util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. #include <linux/mm.h>
  20. #include <linux/shm.h>
  21. #include <linux/init.h>
  22. #include <linux/msg.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/capability.h>
  26. #include <linux/highuid.h>
  27. #include <linux/security.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/audit.h>
  33. #include <linux/nsproxy.h>
  34. #include <linux/rwsem.h>
  35. #include <asm/unistd.h>
  36. #include "util.h"
  37. struct ipc_proc_iface {
  38. const char *path;
  39. const char *header;
  40. int ids;
  41. int (*show)(struct seq_file *, void *);
  42. };
  43. struct ipc_namespace init_ipc_ns = {
  44. .kref = {
  45. .refcount = ATOMIC_INIT(2),
  46. },
  47. };
  48. static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
  49. {
  50. int err;
  51. struct ipc_namespace *ns;
  52. err = -ENOMEM;
  53. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  54. if (ns == NULL)
  55. goto err_mem;
  56. err = sem_init_ns(ns);
  57. if (err)
  58. goto err_sem;
  59. err = msg_init_ns(ns);
  60. if (err)
  61. goto err_msg;
  62. err = shm_init_ns(ns);
  63. if (err)
  64. goto err_shm;
  65. kref_init(&ns->kref);
  66. return ns;
  67. err_shm:
  68. msg_exit_ns(ns);
  69. err_msg:
  70. sem_exit_ns(ns);
  71. err_sem:
  72. kfree(ns);
  73. err_mem:
  74. return ERR_PTR(err);
  75. }
  76. struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
  77. {
  78. struct ipc_namespace *new_ns;
  79. BUG_ON(!ns);
  80. get_ipc_ns(ns);
  81. if (!(flags & CLONE_NEWIPC))
  82. return ns;
  83. new_ns = clone_ipc_ns(ns);
  84. put_ipc_ns(ns);
  85. return new_ns;
  86. }
  87. void free_ipc_ns(struct kref *kref)
  88. {
  89. struct ipc_namespace *ns;
  90. ns = container_of(kref, struct ipc_namespace, kref);
  91. sem_exit_ns(ns);
  92. msg_exit_ns(ns);
  93. shm_exit_ns(ns);
  94. kfree(ns);
  95. }
  96. /**
  97. * ipc_init - initialise IPC subsystem
  98. *
  99. * The various system5 IPC resources (semaphores, messages and shared
  100. * memory) are initialised
  101. */
  102. static int __init ipc_init(void)
  103. {
  104. sem_init();
  105. msg_init();
  106. shm_init();
  107. return 0;
  108. }
  109. __initcall(ipc_init);
  110. /**
  111. * ipc_init_ids - initialise IPC identifiers
  112. * @ids: Identifier set
  113. *
  114. * Set up the sequence range to use for the ipc identifier range (limited
  115. * below IPCMNI) then initialise the ids idr.
  116. */
  117. void ipc_init_ids(struct ipc_ids *ids)
  118. {
  119. init_rwsem(&ids->rw_mutex);
  120. ids->in_use = 0;
  121. ids->seq = 0;
  122. {
  123. int seq_limit = INT_MAX/SEQ_MULTIPLIER;
  124. if(seq_limit > USHRT_MAX)
  125. ids->seq_max = USHRT_MAX;
  126. else
  127. ids->seq_max = seq_limit;
  128. }
  129. idr_init(&ids->ipcs_idr);
  130. }
  131. #ifdef CONFIG_PROC_FS
  132. static const struct file_operations sysvipc_proc_fops;
  133. /**
  134. * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
  135. * @path: Path in procfs
  136. * @header: Banner to be printed at the beginning of the file.
  137. * @ids: ipc id table to iterate.
  138. * @show: show routine.
  139. */
  140. void __init ipc_init_proc_interface(const char *path, const char *header,
  141. int ids, int (*show)(struct seq_file *, void *))
  142. {
  143. struct proc_dir_entry *pde;
  144. struct ipc_proc_iface *iface;
  145. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  146. if (!iface)
  147. return;
  148. iface->path = path;
  149. iface->header = header;
  150. iface->ids = ids;
  151. iface->show = show;
  152. pde = create_proc_entry(path,
  153. S_IRUGO, /* world readable */
  154. NULL /* parent dir */);
  155. if (pde) {
  156. pde->data = iface;
  157. pde->proc_fops = &sysvipc_proc_fops;
  158. } else {
  159. kfree(iface);
  160. }
  161. }
  162. #endif
  163. /**
  164. * ipc_findkey - find a key in an ipc identifier set
  165. * @ids: Identifier set
  166. * @key: The key to find
  167. *
  168. * Requires ipc_ids.rw_mutex locked.
  169. * Returns the LOCKED pointer to the ipc structure if found or NULL
  170. * if not.
  171. * If key is found ipc points to the owning ipc structure
  172. */
  173. static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
  174. {
  175. struct kern_ipc_perm *ipc;
  176. int next_id;
  177. int total;
  178. for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
  179. ipc = idr_find(&ids->ipcs_idr, next_id);
  180. if (ipc == NULL)
  181. continue;
  182. if (ipc->key != key) {
  183. total++;
  184. continue;
  185. }
  186. ipc_lock_by_ptr(ipc);
  187. return ipc;
  188. }
  189. return NULL;
  190. }
  191. /**
  192. * ipc_get_maxid - get the last assigned id
  193. * @ids: IPC identifier set
  194. *
  195. * Called with ipc_ids.rw_mutex held.
  196. */
  197. int ipc_get_maxid(struct ipc_ids *ids)
  198. {
  199. struct kern_ipc_perm *ipc;
  200. int max_id = -1;
  201. int total, id;
  202. if (ids->in_use == 0)
  203. return -1;
  204. if (ids->in_use == IPCMNI)
  205. return IPCMNI - 1;
  206. /* Look for the last assigned id */
  207. total = 0;
  208. for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
  209. ipc = idr_find(&ids->ipcs_idr, id);
  210. if (ipc != NULL) {
  211. max_id = id;
  212. total++;
  213. }
  214. }
  215. return max_id;
  216. }
  217. /**
  218. * ipc_addid - add an IPC identifier
  219. * @ids: IPC identifier set
  220. * @new: new IPC permission set
  221. * @size: limit for the number of used ids
  222. *
  223. * Add an entry 'new' to the IPC ids idr. The permissions object is
  224. * initialised and the first free entry is set up and the id assigned
  225. * is returned. The 'new' entry is returned in a locked state on success.
  226. * On failure the entry is not locked and a negative err-code is returned.
  227. *
  228. * Called with ipc_ids.rw_mutex held as a writer.
  229. */
  230. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
  231. {
  232. int id, err;
  233. if (size > IPCMNI)
  234. size = IPCMNI;
  235. if (ids->in_use >= size)
  236. return -ENOSPC;
  237. err = idr_get_new(&ids->ipcs_idr, new, &id);
  238. if (err)
  239. return err;
  240. ids->in_use++;
  241. new->cuid = new->uid = current->euid;
  242. new->gid = new->cgid = current->egid;
  243. new->seq = ids->seq++;
  244. if(ids->seq > ids->seq_max)
  245. ids->seq = 0;
  246. spin_lock_init(&new->lock);
  247. new->deleted = 0;
  248. rcu_read_lock();
  249. spin_lock(&new->lock);
  250. return id;
  251. }
  252. /**
  253. * ipcget_new - create a new ipc object
  254. * @ns: namespace
  255. * @ids: IPC identifer set
  256. * @ops: the actual creation routine to call
  257. * @params: its parameters
  258. *
  259. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  260. * when the key is IPC_PRIVATE.
  261. */
  262. int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
  263. struct ipc_ops *ops, struct ipc_params *params)
  264. {
  265. int err;
  266. retry:
  267. err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
  268. if (!err)
  269. return -ENOMEM;
  270. down_write(&ids->rw_mutex);
  271. err = ops->getnew(ns, params);
  272. up_write(&ids->rw_mutex);
  273. if (err == -EAGAIN)
  274. goto retry;
  275. return err;
  276. }
  277. /**
  278. * ipc_check_perms - check security and permissions for an IPC
  279. * @ipcp: ipc permission set
  280. * @ops: the actual security routine to call
  281. * @params: its parameters
  282. *
  283. * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
  284. * when the key is not IPC_PRIVATE and that key already exists in the
  285. * ids IDR.
  286. *
  287. * On success, the IPC id is returned.
  288. *
  289. * It is called with ipc_ids.rw_mutex and ipcp->lock held.
  290. */
  291. static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
  292. struct ipc_params *params)
  293. {
  294. int err;
  295. if (ipcperms(ipcp, params->flg))
  296. err = -EACCES;
  297. else {
  298. err = ops->associate(ipcp, params->flg);
  299. if (!err)
  300. err = ipcp->id;
  301. }
  302. return err;
  303. }
  304. /**
  305. * ipcget_public - get an ipc object or create a new one
  306. * @ns: namespace
  307. * @ids: IPC identifer set
  308. * @ops: the actual creation routine to call
  309. * @params: its parameters
  310. *
  311. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  312. * when the key is not IPC_PRIVATE.
  313. * It adds a new entry if the key is not found and does some permission
  314. * / security checkings if the key is found.
  315. *
  316. * On success, the ipc id is returned.
  317. */
  318. int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
  319. struct ipc_ops *ops, struct ipc_params *params)
  320. {
  321. struct kern_ipc_perm *ipcp;
  322. int flg = params->flg;
  323. int err;
  324. retry:
  325. err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
  326. /*
  327. * Take the lock as a writer since we are potentially going to add
  328. * a new entry + read locks are not "upgradable"
  329. */
  330. down_write(&ids->rw_mutex);
  331. ipcp = ipc_findkey(ids, params->key);
  332. if (ipcp == NULL) {
  333. /* key not used */
  334. if (!(flg & IPC_CREAT))
  335. err = -ENOENT;
  336. else if (!err)
  337. err = -ENOMEM;
  338. else
  339. err = ops->getnew(ns, params);
  340. } else {
  341. /* ipc object has been locked by ipc_findkey() */
  342. if (flg & IPC_CREAT && flg & IPC_EXCL)
  343. err = -EEXIST;
  344. else {
  345. err = 0;
  346. if (ops->more_checks)
  347. err = ops->more_checks(ipcp, params);
  348. if (!err)
  349. /*
  350. * ipc_check_perms returns the IPC id on
  351. * success
  352. */
  353. err = ipc_check_perms(ipcp, ops, params);
  354. }
  355. ipc_unlock(ipcp);
  356. }
  357. up_write(&ids->rw_mutex);
  358. if (err == -EAGAIN)
  359. goto retry;
  360. return err;
  361. }
  362. /**
  363. * ipc_rmid - remove an IPC identifier
  364. * @ids: IPC identifier set
  365. * @ipcp: ipc perm structure containing the identifier to remove
  366. *
  367. * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
  368. * before this function is called, and remain locked on the exit.
  369. */
  370. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  371. {
  372. int lid = ipcid_to_idx(ipcp->id);
  373. idr_remove(&ids->ipcs_idr, lid);
  374. ids->in_use--;
  375. ipcp->deleted = 1;
  376. return;
  377. }
  378. /**
  379. * ipc_alloc - allocate ipc space
  380. * @size: size desired
  381. *
  382. * Allocate memory from the appropriate pools and return a pointer to it.
  383. * NULL is returned if the allocation fails
  384. */
  385. void* ipc_alloc(int size)
  386. {
  387. void* out;
  388. if(size > PAGE_SIZE)
  389. out = vmalloc(size);
  390. else
  391. out = kmalloc(size, GFP_KERNEL);
  392. return out;
  393. }
  394. /**
  395. * ipc_free - free ipc space
  396. * @ptr: pointer returned by ipc_alloc
  397. * @size: size of block
  398. *
  399. * Free a block created with ipc_alloc(). The caller must know the size
  400. * used in the allocation call.
  401. */
  402. void ipc_free(void* ptr, int size)
  403. {
  404. if(size > PAGE_SIZE)
  405. vfree(ptr);
  406. else
  407. kfree(ptr);
  408. }
  409. /*
  410. * rcu allocations:
  411. * There are three headers that are prepended to the actual allocation:
  412. * - during use: ipc_rcu_hdr.
  413. * - during the rcu grace period: ipc_rcu_grace.
  414. * - [only if vmalloc]: ipc_rcu_sched.
  415. * Their lifetime doesn't overlap, thus the headers share the same memory.
  416. * Unlike a normal union, they are right-aligned, thus some container_of
  417. * forward/backward casting is necessary:
  418. */
  419. struct ipc_rcu_hdr
  420. {
  421. int refcount;
  422. int is_vmalloc;
  423. void *data[0];
  424. };
  425. struct ipc_rcu_grace
  426. {
  427. struct rcu_head rcu;
  428. /* "void *" makes sure alignment of following data is sane. */
  429. void *data[0];
  430. };
  431. struct ipc_rcu_sched
  432. {
  433. struct work_struct work;
  434. /* "void *" makes sure alignment of following data is sane. */
  435. void *data[0];
  436. };
  437. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  438. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  439. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  440. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  441. static inline int rcu_use_vmalloc(int size)
  442. {
  443. /* Too big for a single page? */
  444. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  445. return 1;
  446. return 0;
  447. }
  448. /**
  449. * ipc_rcu_alloc - allocate ipc and rcu space
  450. * @size: size desired
  451. *
  452. * Allocate memory for the rcu header structure + the object.
  453. * Returns the pointer to the object.
  454. * NULL is returned if the allocation fails.
  455. */
  456. void* ipc_rcu_alloc(int size)
  457. {
  458. void* out;
  459. /*
  460. * We prepend the allocation with the rcu struct, and
  461. * workqueue if necessary (for vmalloc).
  462. */
  463. if (rcu_use_vmalloc(size)) {
  464. out = vmalloc(HDRLEN_VMALLOC + size);
  465. if (out) {
  466. out += HDRLEN_VMALLOC;
  467. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  468. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  469. }
  470. } else {
  471. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  472. if (out) {
  473. out += HDRLEN_KMALLOC;
  474. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  475. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  476. }
  477. }
  478. return out;
  479. }
  480. void ipc_rcu_getref(void *ptr)
  481. {
  482. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  483. }
  484. static void ipc_do_vfree(struct work_struct *work)
  485. {
  486. vfree(container_of(work, struct ipc_rcu_sched, work));
  487. }
  488. /**
  489. * ipc_schedule_free - free ipc + rcu space
  490. * @head: RCU callback structure for queued work
  491. *
  492. * Since RCU callback function is called in bh,
  493. * we need to defer the vfree to schedule_work().
  494. */
  495. static void ipc_schedule_free(struct rcu_head *head)
  496. {
  497. struct ipc_rcu_grace *grace;
  498. struct ipc_rcu_sched *sched;
  499. grace = container_of(head, struct ipc_rcu_grace, rcu);
  500. sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
  501. data[0]);
  502. INIT_WORK(&sched->work, ipc_do_vfree);
  503. schedule_work(&sched->work);
  504. }
  505. /**
  506. * ipc_immediate_free - free ipc + rcu space
  507. * @head: RCU callback structure that contains pointer to be freed
  508. *
  509. * Free from the RCU callback context.
  510. */
  511. static void ipc_immediate_free(struct rcu_head *head)
  512. {
  513. struct ipc_rcu_grace *free =
  514. container_of(head, struct ipc_rcu_grace, rcu);
  515. kfree(free);
  516. }
  517. void ipc_rcu_putref(void *ptr)
  518. {
  519. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  520. return;
  521. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  522. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  523. ipc_schedule_free);
  524. } else {
  525. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  526. ipc_immediate_free);
  527. }
  528. }
  529. /**
  530. * ipcperms - check IPC permissions
  531. * @ipcp: IPC permission set
  532. * @flag: desired permission set.
  533. *
  534. * Check user, group, other permissions for access
  535. * to ipc resources. return 0 if allowed
  536. */
  537. int ipcperms (struct kern_ipc_perm *ipcp, short flag)
  538. { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
  539. int requested_mode, granted_mode, err;
  540. if (unlikely((err = audit_ipc_obj(ipcp))))
  541. return err;
  542. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  543. granted_mode = ipcp->mode;
  544. if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
  545. granted_mode >>= 6;
  546. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  547. granted_mode >>= 3;
  548. /* is there some bit set in requested_mode but not in granted_mode? */
  549. if ((requested_mode & ~granted_mode & 0007) &&
  550. !capable(CAP_IPC_OWNER))
  551. return -1;
  552. return security_ipc_permission(ipcp, flag);
  553. }
  554. /*
  555. * Functions to convert between the kern_ipc_perm structure and the
  556. * old/new ipc_perm structures
  557. */
  558. /**
  559. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  560. * @in: kernel permissions
  561. * @out: new style IPC permissions
  562. *
  563. * Turn the kernel object @in into a set of permissions descriptions
  564. * for returning to userspace (@out).
  565. */
  566. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  567. {
  568. out->key = in->key;
  569. out->uid = in->uid;
  570. out->gid = in->gid;
  571. out->cuid = in->cuid;
  572. out->cgid = in->cgid;
  573. out->mode = in->mode;
  574. out->seq = in->seq;
  575. }
  576. /**
  577. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  578. * @in: new style IPC permissions
  579. * @out: old style IPC permissions
  580. *
  581. * Turn the new style permissions object @in into a compatibility
  582. * object and store it into the @out pointer.
  583. */
  584. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  585. {
  586. out->key = in->key;
  587. SET_UID(out->uid, in->uid);
  588. SET_GID(out->gid, in->gid);
  589. SET_UID(out->cuid, in->cuid);
  590. SET_GID(out->cgid, in->cgid);
  591. out->mode = in->mode;
  592. out->seq = in->seq;
  593. }
  594. /**
  595. * ipc_lock - Lock an ipc structure without rw_mutex held
  596. * @ids: IPC identifier set
  597. * @id: ipc id to look for
  598. *
  599. * Look for an id in the ipc ids idr and lock the associated ipc object.
  600. *
  601. * The ipc object is locked on exit.
  602. *
  603. * This is the routine that should be called when the rw_mutex is not already
  604. * held, i.e. idr tree not protected: it protects the idr tree in read mode
  605. * during the idr_find().
  606. */
  607. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  608. {
  609. struct kern_ipc_perm *out;
  610. int lid = ipcid_to_idx(id);
  611. down_read(&ids->rw_mutex);
  612. rcu_read_lock();
  613. out = idr_find(&ids->ipcs_idr, lid);
  614. if (out == NULL) {
  615. rcu_read_unlock();
  616. up_read(&ids->rw_mutex);
  617. return ERR_PTR(-EINVAL);
  618. }
  619. up_read(&ids->rw_mutex);
  620. spin_lock(&out->lock);
  621. /* ipc_rmid() may have already freed the ID while ipc_lock
  622. * was spinning: here verify that the structure is still valid
  623. */
  624. if (out->deleted) {
  625. spin_unlock(&out->lock);
  626. rcu_read_unlock();
  627. return ERR_PTR(-EINVAL);
  628. }
  629. return out;
  630. }
  631. /**
  632. * ipc_lock_down - Lock an ipc structure with rw_sem held
  633. * @ids: IPC identifier set
  634. * @id: ipc id to look for
  635. *
  636. * Look for an id in the ipc ids idr and lock the associated ipc object.
  637. *
  638. * The ipc object is locked on exit.
  639. *
  640. * This is the routine that should be called when the rw_mutex is already
  641. * held, i.e. idr tree protected.
  642. */
  643. struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
  644. {
  645. struct kern_ipc_perm *out;
  646. int lid = ipcid_to_idx(id);
  647. rcu_read_lock();
  648. out = idr_find(&ids->ipcs_idr, lid);
  649. if (out == NULL) {
  650. rcu_read_unlock();
  651. return ERR_PTR(-EINVAL);
  652. }
  653. spin_lock(&out->lock);
  654. /*
  655. * No need to verify that the structure is still valid since the
  656. * rw_mutex is held.
  657. */
  658. return out;
  659. }
  660. #ifdef __ARCH_WANT_IPC_PARSE_VERSION
  661. /**
  662. * ipc_parse_version - IPC call version
  663. * @cmd: pointer to command
  664. *
  665. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  666. * The @cmd value is turned from an encoding command and version into
  667. * just the command code.
  668. */
  669. int ipc_parse_version (int *cmd)
  670. {
  671. if (*cmd & IPC_64) {
  672. *cmd ^= IPC_64;
  673. return IPC_64;
  674. } else {
  675. return IPC_OLD;
  676. }
  677. }
  678. #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
  679. #ifdef CONFIG_PROC_FS
  680. struct ipc_proc_iter {
  681. struct ipc_namespace *ns;
  682. struct ipc_proc_iface *iface;
  683. };
  684. /*
  685. * This routine locks the ipc structure found at least at position pos.
  686. */
  687. struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  688. loff_t *new_pos)
  689. {
  690. struct kern_ipc_perm *ipc;
  691. int total, id;
  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. }
  698. if (total >= ids->in_use)
  699. return NULL;
  700. for ( ; pos < IPCMNI; pos++) {
  701. ipc = idr_find(&ids->ipcs_idr, pos);
  702. if (ipc != NULL) {
  703. *new_pos = pos + 1;
  704. ipc_lock_by_ptr(ipc);
  705. return ipc;
  706. }
  707. }
  708. /* Out of range - return NULL to terminate iteration */
  709. return NULL;
  710. }
  711. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  712. {
  713. struct ipc_proc_iter *iter = s->private;
  714. struct ipc_proc_iface *iface = iter->iface;
  715. struct kern_ipc_perm *ipc = it;
  716. /* If we had an ipc id locked before, unlock it */
  717. if (ipc && ipc != SEQ_START_TOKEN)
  718. ipc_unlock(ipc);
  719. return sysvipc_find_ipc(iter->ns->ids[iface->ids], *pos, pos);
  720. }
  721. /*
  722. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  723. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  724. */
  725. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  726. {
  727. struct ipc_proc_iter *iter = s->private;
  728. struct ipc_proc_iface *iface = iter->iface;
  729. struct ipc_ids *ids;
  730. ids = iter->ns->ids[iface->ids];
  731. /*
  732. * Take the lock - this will be released by the corresponding
  733. * call to stop().
  734. */
  735. down_read(&ids->rw_mutex);
  736. /* pos < 0 is invalid */
  737. if (*pos < 0)
  738. return NULL;
  739. /* pos == 0 means header */
  740. if (*pos == 0)
  741. return SEQ_START_TOKEN;
  742. /* Find the (pos-1)th ipc */
  743. return sysvipc_find_ipc(ids, *pos - 1, pos);
  744. }
  745. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  746. {
  747. struct kern_ipc_perm *ipc = it;
  748. struct ipc_proc_iter *iter = s->private;
  749. struct ipc_proc_iface *iface = iter->iface;
  750. struct ipc_ids *ids;
  751. /* If we had a locked structure, release it */
  752. if (ipc && ipc != SEQ_START_TOKEN)
  753. ipc_unlock(ipc);
  754. ids = iter->ns->ids[iface->ids];
  755. /* Release the lock we took in start() */
  756. up_read(&ids->rw_mutex);
  757. }
  758. static int sysvipc_proc_show(struct seq_file *s, void *it)
  759. {
  760. struct ipc_proc_iter *iter = s->private;
  761. struct ipc_proc_iface *iface = iter->iface;
  762. if (it == SEQ_START_TOKEN)
  763. return seq_puts(s, iface->header);
  764. return iface->show(s, it);
  765. }
  766. static struct seq_operations sysvipc_proc_seqops = {
  767. .start = sysvipc_proc_start,
  768. .stop = sysvipc_proc_stop,
  769. .next = sysvipc_proc_next,
  770. .show = sysvipc_proc_show,
  771. };
  772. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  773. {
  774. int ret;
  775. struct seq_file *seq;
  776. struct ipc_proc_iter *iter;
  777. ret = -ENOMEM;
  778. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  779. if (!iter)
  780. goto out;
  781. ret = seq_open(file, &sysvipc_proc_seqops);
  782. if (ret)
  783. goto out_kfree;
  784. seq = file->private_data;
  785. seq->private = iter;
  786. iter->iface = PDE(inode)->data;
  787. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  788. out:
  789. return ret;
  790. out_kfree:
  791. kfree(iter);
  792. goto out;
  793. }
  794. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  795. {
  796. struct seq_file *seq = file->private_data;
  797. struct ipc_proc_iter *iter = seq->private;
  798. put_ipc_ns(iter->ns);
  799. return seq_release_private(inode, file);
  800. }
  801. static const struct file_operations sysvipc_proc_fops = {
  802. .open = sysvipc_proc_open,
  803. .read = seq_read,
  804. .llseek = seq_lseek,
  805. .release = sysvipc_proc_release,
  806. };
  807. #endif /* CONFIG_PROC_FS */