util.c 23 KB

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