util.c 21 KB

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