util.c 21 KB

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