util.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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/smp_lock.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/slab.h>
  26. #include <linux/capability.h>
  27. #include <linux/highuid.h>
  28. #include <linux/security.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/audit.h>
  34. #include <linux/nsproxy.h>
  35. #include <asm/unistd.h>
  36. #include "util.h"
  37. struct ipc_proc_iface {
  38. const char *path;
  39. const char *header;
  40. int ids;
  41. int (*show)(struct seq_file *, void *);
  42. };
  43. struct ipc_namespace init_ipc_ns = {
  44. .kref = {
  45. .refcount = ATOMIC_INIT(2),
  46. },
  47. };
  48. #ifdef CONFIG_IPC_NS
  49. static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
  50. {
  51. int err;
  52. struct ipc_namespace *ns;
  53. err = -ENOMEM;
  54. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  55. if (ns == NULL)
  56. goto err_mem;
  57. err = sem_init_ns(ns);
  58. if (err)
  59. goto err_sem;
  60. err = msg_init_ns(ns);
  61. if (err)
  62. goto err_msg;
  63. err = shm_init_ns(ns);
  64. if (err)
  65. goto err_shm;
  66. kref_init(&ns->kref);
  67. return ns;
  68. err_shm:
  69. msg_exit_ns(ns);
  70. err_msg:
  71. sem_exit_ns(ns);
  72. err_sem:
  73. kfree(ns);
  74. err_mem:
  75. return ERR_PTR(err);
  76. }
  77. int unshare_ipcs(unsigned long unshare_flags, struct ipc_namespace **new_ipc)
  78. {
  79. struct ipc_namespace *new;
  80. if (unshare_flags & CLONE_NEWIPC) {
  81. if (!capable(CAP_SYS_ADMIN))
  82. return -EPERM;
  83. new = clone_ipc_ns(current->nsproxy->ipc_ns);
  84. if (IS_ERR(new))
  85. return PTR_ERR(new);
  86. *new_ipc = new;
  87. }
  88. return 0;
  89. }
  90. int copy_ipcs(unsigned long flags, struct task_struct *tsk)
  91. {
  92. struct ipc_namespace *old_ns = tsk->nsproxy->ipc_ns;
  93. struct ipc_namespace *new_ns;
  94. int err = 0;
  95. if (!old_ns)
  96. return 0;
  97. get_ipc_ns(old_ns);
  98. if (!(flags & CLONE_NEWIPC))
  99. return 0;
  100. if (!capable(CAP_SYS_ADMIN)) {
  101. err = -EPERM;
  102. goto out;
  103. }
  104. new_ns = clone_ipc_ns(old_ns);
  105. if (!new_ns) {
  106. err = -ENOMEM;
  107. goto out;
  108. }
  109. tsk->nsproxy->ipc_ns = new_ns;
  110. out:
  111. put_ipc_ns(old_ns);
  112. return err;
  113. }
  114. void free_ipc_ns(struct kref *kref)
  115. {
  116. struct ipc_namespace *ns;
  117. ns = container_of(kref, struct ipc_namespace, kref);
  118. sem_exit_ns(ns);
  119. msg_exit_ns(ns);
  120. shm_exit_ns(ns);
  121. kfree(ns);
  122. }
  123. #endif
  124. /**
  125. * ipc_init - initialise IPC subsystem
  126. *
  127. * The various system5 IPC resources (semaphores, messages and shared
  128. * memory are initialised
  129. */
  130. static int __init ipc_init(void)
  131. {
  132. sem_init();
  133. msg_init();
  134. shm_init();
  135. return 0;
  136. }
  137. __initcall(ipc_init);
  138. /**
  139. * ipc_init_ids - initialise IPC identifiers
  140. * @ids: Identifier set
  141. * @size: Number of identifiers
  142. *
  143. * Given a size for the ipc identifier range (limited below IPCMNI)
  144. * set up the sequence range to use then allocate and initialise the
  145. * array itself.
  146. */
  147. void __ipc_init ipc_init_ids(struct ipc_ids* ids, int size)
  148. {
  149. int i;
  150. mutex_init(&ids->mutex);
  151. if(size > IPCMNI)
  152. size = IPCMNI;
  153. ids->in_use = 0;
  154. ids->max_id = -1;
  155. ids->seq = 0;
  156. {
  157. int seq_limit = INT_MAX/SEQ_MULTIPLIER;
  158. if(seq_limit > USHRT_MAX)
  159. ids->seq_max = USHRT_MAX;
  160. else
  161. ids->seq_max = seq_limit;
  162. }
  163. ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
  164. sizeof(struct ipc_id_ary));
  165. if(ids->entries == NULL) {
  166. printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
  167. size = 0;
  168. ids->entries = &ids->nullentry;
  169. }
  170. ids->entries->size = size;
  171. for(i=0;i<size;i++)
  172. ids->entries->p[i] = NULL;
  173. }
  174. #ifdef CONFIG_PROC_FS
  175. static struct file_operations sysvipc_proc_fops;
  176. /**
  177. * ipc_init_proc_interface - Create a proc interface for sysipc types
  178. * using a seq_file interface.
  179. * @path: Path in procfs
  180. * @header: Banner to be printed at the beginning of the file.
  181. * @ids: ipc id table to iterate.
  182. * @show: show routine.
  183. */
  184. void __init ipc_init_proc_interface(const char *path, const char *header,
  185. int ids, int (*show)(struct seq_file *, void *))
  186. {
  187. struct proc_dir_entry *pde;
  188. struct ipc_proc_iface *iface;
  189. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  190. if (!iface)
  191. return;
  192. iface->path = path;
  193. iface->header = header;
  194. iface->ids = ids;
  195. iface->show = show;
  196. pde = create_proc_entry(path,
  197. S_IRUGO, /* world readable */
  198. NULL /* parent dir */);
  199. if (pde) {
  200. pde->data = iface;
  201. pde->proc_fops = &sysvipc_proc_fops;
  202. } else {
  203. kfree(iface);
  204. }
  205. }
  206. #endif
  207. /**
  208. * ipc_findkey - find a key in an ipc identifier set
  209. * @ids: Identifier set
  210. * @key: The key to find
  211. *
  212. * Requires ipc_ids.mutex locked.
  213. * Returns the identifier if found or -1 if not.
  214. */
  215. int ipc_findkey(struct ipc_ids* ids, key_t key)
  216. {
  217. int id;
  218. struct kern_ipc_perm* p;
  219. int max_id = ids->max_id;
  220. /*
  221. * rcu_dereference() is not needed here
  222. * since ipc_ids.mutex is held
  223. */
  224. for (id = 0; id <= max_id; id++) {
  225. p = ids->entries->p[id];
  226. if(p==NULL)
  227. continue;
  228. if (key == p->key)
  229. return id;
  230. }
  231. return -1;
  232. }
  233. /*
  234. * Requires ipc_ids.mutex locked
  235. */
  236. static int grow_ary(struct ipc_ids* ids, int newsize)
  237. {
  238. struct ipc_id_ary* new;
  239. struct ipc_id_ary* old;
  240. int i;
  241. int size = ids->entries->size;
  242. if(newsize > IPCMNI)
  243. newsize = IPCMNI;
  244. if(newsize <= size)
  245. return newsize;
  246. new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
  247. sizeof(struct ipc_id_ary));
  248. if(new == NULL)
  249. return size;
  250. new->size = newsize;
  251. memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
  252. for(i=size;i<newsize;i++) {
  253. new->p[i] = NULL;
  254. }
  255. old = ids->entries;
  256. /*
  257. * Use rcu_assign_pointer() to make sure the memcpyed contents
  258. * of the new array are visible before the new array becomes visible.
  259. */
  260. rcu_assign_pointer(ids->entries, new);
  261. ipc_rcu_putref(old);
  262. return newsize;
  263. }
  264. /**
  265. * ipc_addid - add an IPC identifier
  266. * @ids: IPC identifier set
  267. * @new: new IPC permission set
  268. * @size: new size limit for the id array
  269. *
  270. * Add an entry 'new' to the IPC arrays. The permissions object is
  271. * initialised and the first free entry is set up and the id assigned
  272. * is returned. The list is returned in a locked state on success.
  273. * On failure the list is not locked and -1 is returned.
  274. *
  275. * Called with ipc_ids.mutex held.
  276. */
  277. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
  278. {
  279. int id;
  280. size = grow_ary(ids,size);
  281. /*
  282. * rcu_dereference()() is not needed here since
  283. * ipc_ids.mutex is held
  284. */
  285. for (id = 0; id < size; id++) {
  286. if(ids->entries->p[id] == NULL)
  287. goto found;
  288. }
  289. return -1;
  290. found:
  291. ids->in_use++;
  292. if (id > ids->max_id)
  293. ids->max_id = id;
  294. new->cuid = new->uid = current->euid;
  295. new->gid = new->cgid = current->egid;
  296. new->seq = ids->seq++;
  297. if(ids->seq > ids->seq_max)
  298. ids->seq = 0;
  299. spin_lock_init(&new->lock);
  300. new->deleted = 0;
  301. rcu_read_lock();
  302. spin_lock(&new->lock);
  303. ids->entries->p[id] = new;
  304. return id;
  305. }
  306. /**
  307. * ipc_rmid - remove an IPC identifier
  308. * @ids: identifier set
  309. * @id: Identifier to remove
  310. *
  311. * The identifier must be valid, and in use. The kernel will panic if
  312. * fed an invalid identifier. The entry is removed and internal
  313. * variables recomputed. The object associated with the identifier
  314. * is returned.
  315. * ipc_ids.mutex and the spinlock for this ID is hold before this function
  316. * is called, and remain locked on the exit.
  317. */
  318. struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
  319. {
  320. struct kern_ipc_perm* p;
  321. int lid = id % SEQ_MULTIPLIER;
  322. BUG_ON(lid >= ids->entries->size);
  323. /*
  324. * do not need a rcu_dereference()() here to force ordering
  325. * on Alpha, since the ipc_ids.mutex is held.
  326. */
  327. p = ids->entries->p[lid];
  328. ids->entries->p[lid] = NULL;
  329. BUG_ON(p==NULL);
  330. ids->in_use--;
  331. if (lid == ids->max_id) {
  332. do {
  333. lid--;
  334. if(lid == -1)
  335. break;
  336. } while (ids->entries->p[lid] == NULL);
  337. ids->max_id = lid;
  338. }
  339. p->deleted = 1;
  340. return p;
  341. }
  342. /**
  343. * ipc_alloc - allocate ipc space
  344. * @size: size desired
  345. *
  346. * Allocate memory from the appropriate pools and return a pointer to it.
  347. * NULL is returned if the allocation fails
  348. */
  349. void* ipc_alloc(int size)
  350. {
  351. void* out;
  352. if(size > PAGE_SIZE)
  353. out = vmalloc(size);
  354. else
  355. out = kmalloc(size, GFP_KERNEL);
  356. return out;
  357. }
  358. /**
  359. * ipc_free - free ipc space
  360. * @ptr: pointer returned by ipc_alloc
  361. * @size: size of block
  362. *
  363. * Free a block created with ipc_alloc. The caller must know the size
  364. * used in the allocation call.
  365. */
  366. void ipc_free(void* ptr, int size)
  367. {
  368. if(size > PAGE_SIZE)
  369. vfree(ptr);
  370. else
  371. kfree(ptr);
  372. }
  373. /*
  374. * rcu allocations:
  375. * There are three headers that are prepended to the actual allocation:
  376. * - during use: ipc_rcu_hdr.
  377. * - during the rcu grace period: ipc_rcu_grace.
  378. * - [only if vmalloc]: ipc_rcu_sched.
  379. * Their lifetime doesn't overlap, thus the headers share the same memory.
  380. * Unlike a normal union, they are right-aligned, thus some container_of
  381. * forward/backward casting is necessary:
  382. */
  383. struct ipc_rcu_hdr
  384. {
  385. int refcount;
  386. int is_vmalloc;
  387. void *data[0];
  388. };
  389. struct ipc_rcu_grace
  390. {
  391. struct rcu_head rcu;
  392. /* "void *" makes sure alignment of following data is sane. */
  393. void *data[0];
  394. };
  395. struct ipc_rcu_sched
  396. {
  397. struct work_struct work;
  398. /* "void *" makes sure alignment of following data is sane. */
  399. void *data[0];
  400. };
  401. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  402. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  403. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  404. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  405. static inline int rcu_use_vmalloc(int size)
  406. {
  407. /* Too big for a single page? */
  408. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  409. return 1;
  410. return 0;
  411. }
  412. /**
  413. * ipc_rcu_alloc - allocate ipc and rcu space
  414. * @size: size desired
  415. *
  416. * Allocate memory for the rcu header structure + the object.
  417. * Returns the pointer to the object.
  418. * NULL is returned if the allocation fails.
  419. */
  420. void* ipc_rcu_alloc(int size)
  421. {
  422. void* out;
  423. /*
  424. * We prepend the allocation with the rcu struct, and
  425. * workqueue if necessary (for vmalloc).
  426. */
  427. if (rcu_use_vmalloc(size)) {
  428. out = vmalloc(HDRLEN_VMALLOC + size);
  429. if (out) {
  430. out += HDRLEN_VMALLOC;
  431. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  432. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  433. }
  434. } else {
  435. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  436. if (out) {
  437. out += HDRLEN_KMALLOC;
  438. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  439. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  440. }
  441. }
  442. return out;
  443. }
  444. void ipc_rcu_getref(void *ptr)
  445. {
  446. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  447. }
  448. /**
  449. * ipc_schedule_free - free ipc + rcu space
  450. * @head: RCU callback structure for queued work
  451. *
  452. * Since RCU callback function is called in bh,
  453. * we need to defer the vfree to schedule_work
  454. */
  455. static void ipc_schedule_free(struct rcu_head *head)
  456. {
  457. struct ipc_rcu_grace *grace =
  458. container_of(head, struct ipc_rcu_grace, rcu);
  459. struct ipc_rcu_sched *sched =
  460. container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
  461. INIT_WORK(&sched->work, vfree, sched);
  462. schedule_work(&sched->work);
  463. }
  464. /**
  465. * ipc_immediate_free - free ipc + rcu space
  466. * @head: RCU callback structure that contains pointer to be freed
  467. *
  468. * Free from the RCU callback context
  469. */
  470. static void ipc_immediate_free(struct rcu_head *head)
  471. {
  472. struct ipc_rcu_grace *free =
  473. container_of(head, struct ipc_rcu_grace, rcu);
  474. kfree(free);
  475. }
  476. void ipc_rcu_putref(void *ptr)
  477. {
  478. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  479. return;
  480. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  481. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  482. ipc_schedule_free);
  483. } else {
  484. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  485. ipc_immediate_free);
  486. }
  487. }
  488. /**
  489. * ipcperms - check IPC permissions
  490. * @ipcp: IPC permission set
  491. * @flag: desired permission set.
  492. *
  493. * Check user, group, other permissions for access
  494. * to ipc resources. return 0 if allowed
  495. */
  496. int ipcperms (struct kern_ipc_perm *ipcp, short flag)
  497. { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
  498. int requested_mode, granted_mode, err;
  499. if (unlikely((err = audit_ipc_obj(ipcp))))
  500. return err;
  501. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  502. granted_mode = ipcp->mode;
  503. if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
  504. granted_mode >>= 6;
  505. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  506. granted_mode >>= 3;
  507. /* is there some bit set in requested_mode but not in granted_mode? */
  508. if ((requested_mode & ~granted_mode & 0007) &&
  509. !capable(CAP_IPC_OWNER))
  510. return -1;
  511. return security_ipc_permission(ipcp, flag);
  512. }
  513. /*
  514. * Functions to convert between the kern_ipc_perm structure and the
  515. * old/new ipc_perm structures
  516. */
  517. /**
  518. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  519. * @in: kernel permissions
  520. * @out: new style IPC permissions
  521. *
  522. * Turn the kernel object 'in' into a set of permissions descriptions
  523. * for returning to userspace (out).
  524. */
  525. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  526. {
  527. out->key = in->key;
  528. out->uid = in->uid;
  529. out->gid = in->gid;
  530. out->cuid = in->cuid;
  531. out->cgid = in->cgid;
  532. out->mode = in->mode;
  533. out->seq = in->seq;
  534. }
  535. /**
  536. * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
  537. * @in: new style IPC permissions
  538. * @out: old style IPC permissions
  539. *
  540. * Turn the new style permissions object in into a compatibility
  541. * object and store it into the 'out' pointer.
  542. */
  543. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  544. {
  545. out->key = in->key;
  546. SET_UID(out->uid, in->uid);
  547. SET_GID(out->gid, in->gid);
  548. SET_UID(out->cuid, in->cuid);
  549. SET_GID(out->cgid, in->cgid);
  550. out->mode = in->mode;
  551. out->seq = in->seq;
  552. }
  553. /*
  554. * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
  555. * is called with shm_ids.mutex locked. Since grow_ary() is also called with
  556. * shm_ids.mutex down(for Shared Memory), there is no need to add read
  557. * barriers here to gurantee the writes in grow_ary() are seen in order
  558. * here (for Alpha).
  559. *
  560. * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
  561. * if in the future ipc_get() is used by other places without ipc_ids.mutex
  562. * down, then ipc_get() needs read memery barriers as ipc_lock() does.
  563. */
  564. struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
  565. {
  566. struct kern_ipc_perm* out;
  567. int lid = id % SEQ_MULTIPLIER;
  568. if(lid >= ids->entries->size)
  569. return NULL;
  570. out = ids->entries->p[lid];
  571. return out;
  572. }
  573. struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
  574. {
  575. struct kern_ipc_perm* out;
  576. int lid = id % SEQ_MULTIPLIER;
  577. struct ipc_id_ary* entries;
  578. rcu_read_lock();
  579. entries = rcu_dereference(ids->entries);
  580. if(lid >= entries->size) {
  581. rcu_read_unlock();
  582. return NULL;
  583. }
  584. out = entries->p[lid];
  585. if(out == NULL) {
  586. rcu_read_unlock();
  587. return NULL;
  588. }
  589. spin_lock(&out->lock);
  590. /* ipc_rmid() may have already freed the ID while ipc_lock
  591. * was spinning: here verify that the structure is still valid
  592. */
  593. if (out->deleted) {
  594. spin_unlock(&out->lock);
  595. rcu_read_unlock();
  596. return NULL;
  597. }
  598. return out;
  599. }
  600. void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
  601. {
  602. rcu_read_lock();
  603. spin_lock(&perm->lock);
  604. }
  605. void ipc_unlock(struct kern_ipc_perm* perm)
  606. {
  607. spin_unlock(&perm->lock);
  608. rcu_read_unlock();
  609. }
  610. int ipc_buildid(struct ipc_ids* ids, int id, int seq)
  611. {
  612. return SEQ_MULTIPLIER*seq + id;
  613. }
  614. int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
  615. {
  616. if(uid/SEQ_MULTIPLIER != ipcp->seq)
  617. return 1;
  618. return 0;
  619. }
  620. #ifdef __ARCH_WANT_IPC_PARSE_VERSION
  621. /**
  622. * ipc_parse_version - IPC call version
  623. * @cmd: pointer to command
  624. *
  625. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  626. * The cmd value is turned from an encoding command and version into
  627. * just the command code.
  628. */
  629. int ipc_parse_version (int *cmd)
  630. {
  631. if (*cmd & IPC_64) {
  632. *cmd ^= IPC_64;
  633. return IPC_64;
  634. } else {
  635. return IPC_OLD;
  636. }
  637. }
  638. #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
  639. #ifdef CONFIG_PROC_FS
  640. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  641. {
  642. struct ipc_proc_iface *iface = s->private;
  643. struct kern_ipc_perm *ipc = it;
  644. loff_t p;
  645. struct ipc_ids *ids;
  646. ids = current->nsproxy->ipc_ns->ids[iface->ids];
  647. /* If we had an ipc id locked before, unlock it */
  648. if (ipc && ipc != SEQ_START_TOKEN)
  649. ipc_unlock(ipc);
  650. /*
  651. * p = *pos - 1 (because id 0 starts at position 1)
  652. * + 1 (because we increment the position by one)
  653. */
  654. for (p = *pos; p <= ids->max_id; p++) {
  655. if ((ipc = ipc_lock(ids, p)) != NULL) {
  656. *pos = p + 1;
  657. return ipc;
  658. }
  659. }
  660. /* Out of range - return NULL to terminate iteration */
  661. return NULL;
  662. }
  663. /*
  664. * File positions: pos 0 -> header, pos n -> ipc id + 1.
  665. * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
  666. */
  667. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  668. {
  669. struct ipc_proc_iface *iface = s->private;
  670. struct kern_ipc_perm *ipc;
  671. loff_t p;
  672. struct ipc_ids *ids;
  673. ids = current->nsproxy->ipc_ns->ids[iface->ids];
  674. /*
  675. * Take the lock - this will be released by the corresponding
  676. * call to stop().
  677. */
  678. mutex_lock(&ids->mutex);
  679. /* pos < 0 is invalid */
  680. if (*pos < 0)
  681. return NULL;
  682. /* pos == 0 means header */
  683. if (*pos == 0)
  684. return SEQ_START_TOKEN;
  685. /* Find the (pos-1)th ipc */
  686. for (p = *pos - 1; p <= ids->max_id; p++) {
  687. if ((ipc = ipc_lock(ids, p)) != NULL) {
  688. *pos = p + 1;
  689. return ipc;
  690. }
  691. }
  692. return NULL;
  693. }
  694. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  695. {
  696. struct kern_ipc_perm *ipc = it;
  697. struct ipc_proc_iface *iface = s->private;
  698. struct ipc_ids *ids;
  699. /* If we had a locked segment, release it */
  700. if (ipc && ipc != SEQ_START_TOKEN)
  701. ipc_unlock(ipc);
  702. ids = current->nsproxy->ipc_ns->ids[iface->ids];
  703. /* Release the lock we took in start() */
  704. mutex_unlock(&ids->mutex);
  705. }
  706. static int sysvipc_proc_show(struct seq_file *s, void *it)
  707. {
  708. struct ipc_proc_iface *iface = s->private;
  709. if (it == SEQ_START_TOKEN)
  710. return seq_puts(s, iface->header);
  711. return iface->show(s, it);
  712. }
  713. static struct seq_operations sysvipc_proc_seqops = {
  714. .start = sysvipc_proc_start,
  715. .stop = sysvipc_proc_stop,
  716. .next = sysvipc_proc_next,
  717. .show = sysvipc_proc_show,
  718. };
  719. static int sysvipc_proc_open(struct inode *inode, struct file *file) {
  720. int ret;
  721. struct seq_file *seq;
  722. ret = seq_open(file, &sysvipc_proc_seqops);
  723. if (!ret) {
  724. seq = file->private_data;
  725. seq->private = PDE(inode)->data;
  726. }
  727. return ret;
  728. }
  729. static struct file_operations sysvipc_proc_fops = {
  730. .open = sysvipc_proc_open,
  731. .read = seq_read,
  732. .llseek = seq_lseek,
  733. .release = seq_release,
  734. };
  735. #endif /* CONFIG_PROC_FS */