util.c 20 KB

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