util.c 21 KB

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