util.c 22 KB

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