util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 -1 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 -1;
  237. err = idr_get_new(&ids->ipcs_idr, new, &id);
  238. if (err)
  239. return -1;
  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. err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
  267. if (!err)
  268. return -ENOMEM;
  269. down_write(&ids->rw_mutex);
  270. err = ops->getnew(ns, params);
  271. up_write(&ids->rw_mutex);
  272. return err;
  273. }
  274. /**
  275. * ipc_check_perms - check security and permissions for an IPC
  276. * @ipcp: ipc permission set
  277. * @ops: the actual security routine to call
  278. * @params: its parameters
  279. *
  280. * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
  281. * when the key is not IPC_PRIVATE and that key already exists in the
  282. * ids IDR.
  283. *
  284. * On success, the IPC id is returned.
  285. *
  286. * It is called with ipc_ids.rw_mutex and ipcp->lock held.
  287. */
  288. static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
  289. struct ipc_params *params)
  290. {
  291. int err;
  292. if (ipcperms(ipcp, params->flg))
  293. err = -EACCES;
  294. else {
  295. err = ops->associate(ipcp, params->flg);
  296. if (!err)
  297. err = ipcp->id;
  298. }
  299. return err;
  300. }
  301. /**
  302. * ipcget_public - get an ipc object or create a new one
  303. * @ns: namespace
  304. * @ids: IPC identifer set
  305. * @ops: the actual creation routine to call
  306. * @params: its parameters
  307. *
  308. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  309. * when the key is not IPC_PRIVATE.
  310. * It adds a new entry if the key is not found and does some permission
  311. * / security checkings if the key is found.
  312. *
  313. * On success, the ipc id is returned.
  314. */
  315. int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
  316. struct ipc_ops *ops, struct ipc_params *params)
  317. {
  318. struct kern_ipc_perm *ipcp;
  319. int flg = params->flg;
  320. int err;
  321. err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
  322. /*
  323. * Take the lock as a writer since we are potentially going to add
  324. * a new entry + read locks are not "upgradable"
  325. */
  326. down_write(&ids->rw_mutex);
  327. ipcp = ipc_findkey(ids, params->key);
  328. if (ipcp == NULL) {
  329. /* key not used */
  330. if (!(flg & IPC_CREAT))
  331. err = -ENOENT;
  332. else if (!err)
  333. err = -ENOMEM;
  334. else
  335. err = ops->getnew(ns, params);
  336. } else {
  337. /* ipc object has been locked by ipc_findkey() */
  338. if (flg & IPC_CREAT && flg & IPC_EXCL)
  339. err = -EEXIST;
  340. else {
  341. err = 0;
  342. if (ops->more_checks)
  343. err = ops->more_checks(ipcp, params);
  344. if (!err)
  345. /*
  346. * ipc_check_perms returns the IPC id on
  347. * success
  348. */
  349. err = ipc_check_perms(ipcp, ops, params);
  350. }
  351. ipc_unlock(ipcp);
  352. }
  353. up_write(&ids->rw_mutex);
  354. return err;
  355. }
  356. /**
  357. * ipc_rmid - remove an IPC identifier
  358. * @ids: IPC identifier set
  359. * @ipcp: ipc perm structure containing the identifier to remove
  360. *
  361. * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
  362. * before this function is called, and remain locked on the exit.
  363. */
  364. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  365. {
  366. int lid = ipcid_to_idx(ipcp->id);
  367. idr_remove(&ids->ipcs_idr, lid);
  368. ids->in_use--;
  369. ipcp->deleted = 1;
  370. return;
  371. }
  372. /**
  373. * ipc_alloc - allocate ipc space
  374. * @size: size desired
  375. *
  376. * Allocate memory from the appropriate pools and return a pointer to it.
  377. * NULL is returned if the allocation fails
  378. */
  379. void* ipc_alloc(int size)
  380. {
  381. void* out;
  382. if(size > PAGE_SIZE)
  383. out = vmalloc(size);
  384. else
  385. out = kmalloc(size, GFP_KERNEL);
  386. return out;
  387. }
  388. /**
  389. * ipc_free - free ipc space
  390. * @ptr: pointer returned by ipc_alloc
  391. * @size: size of block
  392. *
  393. * Free a block created with ipc_alloc(). The caller must know the size
  394. * used in the allocation call.
  395. */
  396. void ipc_free(void* ptr, int size)
  397. {
  398. if(size > PAGE_SIZE)
  399. vfree(ptr);
  400. else
  401. kfree(ptr);
  402. }
  403. /*
  404. * rcu allocations:
  405. * There are three headers that are prepended to the actual allocation:
  406. * - during use: ipc_rcu_hdr.
  407. * - during the rcu grace period: ipc_rcu_grace.
  408. * - [only if vmalloc]: ipc_rcu_sched.
  409. * Their lifetime doesn't overlap, thus the headers share the same memory.
  410. * Unlike a normal union, they are right-aligned, thus some container_of
  411. * forward/backward casting is necessary:
  412. */
  413. struct ipc_rcu_hdr
  414. {
  415. int refcount;
  416. int is_vmalloc;
  417. void *data[0];
  418. };
  419. struct ipc_rcu_grace
  420. {
  421. struct rcu_head rcu;
  422. /* "void *" makes sure alignment of following data is sane. */
  423. void *data[0];
  424. };
  425. struct ipc_rcu_sched
  426. {
  427. struct work_struct work;
  428. /* "void *" makes sure alignment of following data is sane. */
  429. void *data[0];
  430. };
  431. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  432. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  433. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  434. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  435. static inline int rcu_use_vmalloc(int size)
  436. {
  437. /* Too big for a single page? */
  438. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  439. return 1;
  440. return 0;
  441. }
  442. /**
  443. * ipc_rcu_alloc - allocate ipc and rcu space
  444. * @size: size desired
  445. *
  446. * Allocate memory for the rcu header structure + the object.
  447. * Returns the pointer to the object.
  448. * NULL is returned if the allocation fails.
  449. */
  450. void* ipc_rcu_alloc(int size)
  451. {
  452. void* out;
  453. /*
  454. * We prepend the allocation with the rcu struct, and
  455. * workqueue if necessary (for vmalloc).
  456. */
  457. if (rcu_use_vmalloc(size)) {
  458. out = vmalloc(HDRLEN_VMALLOC + size);
  459. if (out) {
  460. out += HDRLEN_VMALLOC;
  461. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  462. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  463. }
  464. } else {
  465. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  466. if (out) {
  467. out += HDRLEN_KMALLOC;
  468. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  469. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  470. }
  471. }
  472. return out;
  473. }
  474. void ipc_rcu_getref(void *ptr)
  475. {
  476. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  477. }
  478. static void ipc_do_vfree(struct work_struct *work)
  479. {
  480. vfree(container_of(work, struct ipc_rcu_sched, work));
  481. }
  482. /**
  483. * ipc_schedule_free - free ipc + rcu space
  484. * @head: RCU callback structure for queued work
  485. *
  486. * Since RCU callback function is called in bh,
  487. * we need to defer the vfree to schedule_work().
  488. */
  489. static void ipc_schedule_free(struct rcu_head *head)
  490. {
  491. struct ipc_rcu_grace *grace;
  492. struct ipc_rcu_sched *sched;
  493. grace = container_of(head, struct ipc_rcu_grace, rcu);
  494. sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
  495. data[0]);
  496. INIT_WORK(&sched->work, ipc_do_vfree);
  497. schedule_work(&sched->work);
  498. }
  499. /**
  500. * ipc_immediate_free - free ipc + rcu space
  501. * @head: RCU callback structure that contains pointer to be freed
  502. *
  503. * Free from the RCU callback context.
  504. */
  505. static void ipc_immediate_free(struct rcu_head *head)
  506. {
  507. struct ipc_rcu_grace *free =
  508. container_of(head, struct ipc_rcu_grace, rcu);
  509. kfree(free);
  510. }
  511. void ipc_rcu_putref(void *ptr)
  512. {
  513. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  514. return;
  515. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  516. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  517. ipc_schedule_free);
  518. } else {
  519. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  520. ipc_immediate_free);
  521. }
  522. }
  523. /**
  524. * ipcperms - check IPC permissions
  525. * @ipcp: IPC permission set
  526. * @flag: desired permission set.
  527. *
  528. * Check user, group, other permissions for access
  529. * to ipc resources. return 0 if allowed
  530. */
  531. int ipcperms (struct kern_ipc_perm *ipcp, short flag)
  532. { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
  533. int requested_mode, granted_mode, err;
  534. if (unlikely((err = audit_ipc_obj(ipcp))))
  535. return err;
  536. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  537. granted_mode = ipcp->mode;
  538. if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
  539. granted_mode >>= 6;
  540. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  541. granted_mode >>= 3;
  542. /* is there some bit set in requested_mode but not in granted_mode? */
  543. if ((requested_mode & ~granted_mode & 0007) &&
  544. !capable(CAP_IPC_OWNER))
  545. return -1;
  546. return security_ipc_permission(ipcp, flag);
  547. }
  548. /*
  549. * Functions to convert between the kern_ipc_perm structure and the
  550. * old/new ipc_perm structures
  551. */
  552. /**
  553. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  554. * @in: kernel permissions
  555. * @out: new style IPC permissions
  556. *
  557. * Turn the kernel object @in into a set of permissions descriptions
  558. * for returning to userspace (@out).
  559. */
  560. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  561. {
  562. out->key = in->key;
  563. out->uid = in->uid;
  564. out->gid = in->gid;
  565. out->cuid = in->cuid;
  566. out->cgid = in->cgid;
  567. out->mode = in->mode;
  568. out->seq = in->seq;
  569. }
  570. /**
  571. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  572. * @in: new style IPC permissions
  573. * @out: old style IPC permissions
  574. *
  575. * Turn the new style permissions object @in into a compatibility
  576. * object and store it into the @out pointer.
  577. */
  578. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  579. {
  580. out->key = in->key;
  581. SET_UID(out->uid, in->uid);
  582. SET_GID(out->gid, in->gid);
  583. SET_UID(out->cuid, in->cuid);
  584. SET_GID(out->cgid, in->cgid);
  585. out->mode = in->mode;
  586. out->seq = in->seq;
  587. }
  588. /**
  589. * ipc_lock - Lock an ipc structure without rw_mutex held
  590. * @ids: IPC identifier set
  591. * @id: ipc id to look for
  592. *
  593. * Look for an id in the ipc ids idr and lock the associated ipc object.
  594. *
  595. * The ipc object is locked on exit.
  596. *
  597. * This is the routine that should be called when the rw_mutex is not already
  598. * held, i.e. idr tree not protected: it protects the idr tree in read mode
  599. * during the idr_find().
  600. */
  601. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  602. {
  603. struct kern_ipc_perm *out;
  604. int lid = ipcid_to_idx(id);
  605. down_read(&ids->rw_mutex);
  606. rcu_read_lock();
  607. out = idr_find(&ids->ipcs_idr, lid);
  608. if (out == NULL) {
  609. rcu_read_unlock();
  610. up_read(&ids->rw_mutex);
  611. return ERR_PTR(-EINVAL);
  612. }
  613. up_read(&ids->rw_mutex);
  614. spin_lock(&out->lock);
  615. /* ipc_rmid() may have already freed the ID while ipc_lock
  616. * was spinning: here verify that the structure is still valid
  617. */
  618. if (out->deleted) {
  619. spin_unlock(&out->lock);
  620. rcu_read_unlock();
  621. return ERR_PTR(-EINVAL);
  622. }
  623. return out;
  624. }
  625. /**
  626. * ipc_lock_down - Lock an ipc structure with rw_sem held
  627. * @ids: IPC identifier set
  628. * @id: ipc id to look for
  629. *
  630. * Look for an id in the ipc ids idr and lock the associated ipc object.
  631. *
  632. * The ipc object is locked on exit.
  633. *
  634. * This is the routine that should be called when the rw_mutex is already
  635. * held, i.e. idr tree protected.
  636. */
  637. struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
  638. {
  639. struct kern_ipc_perm *out;
  640. int lid = ipcid_to_idx(id);
  641. rcu_read_lock();
  642. out = idr_find(&ids->ipcs_idr, lid);
  643. if (out == NULL) {
  644. rcu_read_unlock();
  645. return ERR_PTR(-EINVAL);
  646. }
  647. spin_lock(&out->lock);
  648. /*
  649. * No need to verify that the structure is still valid since the
  650. * rw_mutex is held.
  651. */
  652. return out;
  653. }
  654. #ifdef __ARCH_WANT_IPC_PARSE_VERSION
  655. /**
  656. * ipc_parse_version - IPC call version
  657. * @cmd: pointer to command
  658. *
  659. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  660. * The @cmd value is turned from an encoding command and version into
  661. * just the command code.
  662. */
  663. int ipc_parse_version (int *cmd)
  664. {
  665. if (*cmd & IPC_64) {
  666. *cmd ^= IPC_64;
  667. return IPC_64;
  668. } else {
  669. return IPC_OLD;
  670. }
  671. }
  672. #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
  673. #ifdef CONFIG_PROC_FS
  674. struct ipc_proc_iter {
  675. struct ipc_namespace *ns;
  676. struct ipc_proc_iface *iface;
  677. };
  678. /*
  679. * This routine locks the ipc structure found at least at position pos.
  680. */
  681. struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  682. loff_t *new_pos)
  683. {
  684. struct kern_ipc_perm *ipc;
  685. int total, id;
  686. total = 0;
  687. for (id = 0; id < pos && total < ids->in_use; id++) {
  688. ipc = idr_find(&ids->ipcs_idr, id);
  689. if (ipc != NULL)
  690. total++;
  691. }
  692. if (total >= ids->in_use)
  693. return NULL;
  694. for ( ; pos < IPCMNI; pos++) {
  695. ipc = idr_find(&ids->ipcs_idr, pos);
  696. if (ipc != NULL) {
  697. *new_pos = pos + 1;
  698. ipc_lock_by_ptr(ipc);
  699. return ipc;
  700. }
  701. }
  702. /* Out of range - return NULL to terminate iteration */
  703. return NULL;
  704. }
  705. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  706. {
  707. struct ipc_proc_iter *iter = s->private;
  708. struct ipc_proc_iface *iface = iter->iface;
  709. struct kern_ipc_perm *ipc = it;
  710. /* If we had an ipc id locked before, unlock it */
  711. if (ipc && ipc != SEQ_START_TOKEN)
  712. ipc_unlock(ipc);
  713. return sysvipc_find_ipc(iter->ns->ids[iface->ids], *pos, pos);
  714. }
  715. /*
  716. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  717. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  718. */
  719. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  720. {
  721. struct ipc_proc_iter *iter = s->private;
  722. struct ipc_proc_iface *iface = iter->iface;
  723. struct ipc_ids *ids;
  724. ids = iter->ns->ids[iface->ids];
  725. /*
  726. * Take the lock - this will be released by the corresponding
  727. * call to stop().
  728. */
  729. down_read(&ids->rw_mutex);
  730. /* pos < 0 is invalid */
  731. if (*pos < 0)
  732. return NULL;
  733. /* pos == 0 means header */
  734. if (*pos == 0)
  735. return SEQ_START_TOKEN;
  736. /* Find the (pos-1)th ipc */
  737. return sysvipc_find_ipc(ids, *pos - 1, pos);
  738. }
  739. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  740. {
  741. struct kern_ipc_perm *ipc = it;
  742. struct ipc_proc_iter *iter = s->private;
  743. struct ipc_proc_iface *iface = iter->iface;
  744. struct ipc_ids *ids;
  745. /* If we had a locked structure, release it */
  746. if (ipc && ipc != SEQ_START_TOKEN)
  747. ipc_unlock(ipc);
  748. ids = iter->ns->ids[iface->ids];
  749. /* Release the lock we took in start() */
  750. up_read(&ids->rw_mutex);
  751. }
  752. static int sysvipc_proc_show(struct seq_file *s, void *it)
  753. {
  754. struct ipc_proc_iter *iter = s->private;
  755. struct ipc_proc_iface *iface = iter->iface;
  756. if (it == SEQ_START_TOKEN)
  757. return seq_puts(s, iface->header);
  758. return iface->show(s, it);
  759. }
  760. static struct seq_operations sysvipc_proc_seqops = {
  761. .start = sysvipc_proc_start,
  762. .stop = sysvipc_proc_stop,
  763. .next = sysvipc_proc_next,
  764. .show = sysvipc_proc_show,
  765. };
  766. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  767. {
  768. int ret;
  769. struct seq_file *seq;
  770. struct ipc_proc_iter *iter;
  771. ret = -ENOMEM;
  772. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  773. if (!iter)
  774. goto out;
  775. ret = seq_open(file, &sysvipc_proc_seqops);
  776. if (ret)
  777. goto out_kfree;
  778. seq = file->private_data;
  779. seq->private = iter;
  780. iter->iface = PDE(inode)->data;
  781. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  782. out:
  783. return ret;
  784. out_kfree:
  785. kfree(iter);
  786. goto out;
  787. }
  788. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  789. {
  790. struct seq_file *seq = file->private_data;
  791. struct ipc_proc_iter *iter = seq->private;
  792. put_ipc_ns(iter->ns);
  793. return seq_release_private(inode, file);
  794. }
  795. static const struct file_operations sysvipc_proc_fops = {
  796. .open = sysvipc_proc_open,
  797. .read = seq_read,
  798. .llseek = seq_lseek,
  799. .release = sysvipc_proc_release,
  800. };
  801. #endif /* CONFIG_PROC_FS */