util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 const struct file_operations sysvipc_proc_fops;
  176. /**
  177. * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
  178. * @path: Path in procfs
  179. * @header: Banner to be printed at the beginning of the file.
  180. * @ids: ipc id table to iterate.
  181. * @show: show routine.
  182. */
  183. void __init ipc_init_proc_interface(const char *path, const char *header,
  184. int ids, int (*show)(struct seq_file *, void *))
  185. {
  186. struct proc_dir_entry *pde;
  187. struct ipc_proc_iface *iface;
  188. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  189. if (!iface)
  190. return;
  191. iface->path = path;
  192. iface->header = header;
  193. iface->ids = ids;
  194. iface->show = show;
  195. pde = create_proc_entry(path,
  196. S_IRUGO, /* world readable */
  197. NULL /* parent dir */);
  198. if (pde) {
  199. pde->data = iface;
  200. pde->proc_fops = &sysvipc_proc_fops;
  201. } else {
  202. kfree(iface);
  203. }
  204. }
  205. #endif
  206. /**
  207. * ipc_findkey - find a key in an ipc identifier set
  208. * @ids: Identifier set
  209. * @key: The key to find
  210. *
  211. * Requires ipc_ids.mutex locked.
  212. * Returns the identifier if found or -1 if not.
  213. */
  214. int ipc_findkey(struct ipc_ids* ids, key_t key)
  215. {
  216. int id;
  217. struct kern_ipc_perm* p;
  218. int max_id = ids->max_id;
  219. /*
  220. * rcu_dereference() is not needed here
  221. * since ipc_ids.mutex is held
  222. */
  223. for (id = 0; id <= max_id; id++) {
  224. p = ids->entries->p[id];
  225. if(p==NULL)
  226. continue;
  227. if (key == p->key)
  228. return id;
  229. }
  230. return -1;
  231. }
  232. /*
  233. * Requires ipc_ids.mutex locked
  234. */
  235. static int grow_ary(struct ipc_ids* ids, int newsize)
  236. {
  237. struct ipc_id_ary* new;
  238. struct ipc_id_ary* old;
  239. int i;
  240. int size = ids->entries->size;
  241. if(newsize > IPCMNI)
  242. newsize = IPCMNI;
  243. if(newsize <= size)
  244. return newsize;
  245. new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
  246. sizeof(struct ipc_id_ary));
  247. if(new == NULL)
  248. return size;
  249. new->size = newsize;
  250. memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
  251. for(i=size;i<newsize;i++) {
  252. new->p[i] = NULL;
  253. }
  254. old = ids->entries;
  255. /*
  256. * Use rcu_assign_pointer() to make sure the memcpyed contents
  257. * of the new array are visible before the new array becomes visible.
  258. */
  259. rcu_assign_pointer(ids->entries, new);
  260. __ipc_fini_ids(ids, old);
  261. return newsize;
  262. }
  263. /**
  264. * ipc_addid - add an IPC identifier
  265. * @ids: IPC identifier set
  266. * @new: new IPC permission set
  267. * @size: new size limit for the id array
  268. *
  269. * Add an entry 'new' to the IPC arrays. The permissions object is
  270. * initialised and the first free entry is set up and the id assigned
  271. * is returned. The list is returned in a locked state on success.
  272. * On failure the list is not locked and -1 is returned.
  273. *
  274. * Called with ipc_ids.mutex held.
  275. */
  276. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
  277. {
  278. int id;
  279. size = grow_ary(ids,size);
  280. /*
  281. * rcu_dereference()() is not needed here since
  282. * ipc_ids.mutex is held
  283. */
  284. for (id = 0; id < size; id++) {
  285. if(ids->entries->p[id] == NULL)
  286. goto found;
  287. }
  288. return -1;
  289. found:
  290. ids->in_use++;
  291. if (id > ids->max_id)
  292. ids->max_id = id;
  293. new->cuid = new->uid = current->euid;
  294. new->gid = new->cgid = current->egid;
  295. new->seq = ids->seq++;
  296. if(ids->seq > ids->seq_max)
  297. ids->seq = 0;
  298. spin_lock_init(&new->lock);
  299. new->deleted = 0;
  300. rcu_read_lock();
  301. spin_lock(&new->lock);
  302. ids->entries->p[id] = new;
  303. return id;
  304. }
  305. /**
  306. * ipc_rmid - remove an IPC identifier
  307. * @ids: identifier set
  308. * @id: Identifier to remove
  309. *
  310. * The identifier must be valid, and in use. The kernel will panic if
  311. * fed an invalid identifier. The entry is removed and internal
  312. * variables recomputed. The object associated with the identifier
  313. * is returned.
  314. * ipc_ids.mutex and the spinlock for this ID is hold before this function
  315. * is called, and remain locked on the exit.
  316. */
  317. struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
  318. {
  319. struct kern_ipc_perm* p;
  320. int lid = id % SEQ_MULTIPLIER;
  321. BUG_ON(lid >= ids->entries->size);
  322. /*
  323. * do not need a rcu_dereference()() here to force ordering
  324. * on Alpha, since the ipc_ids.mutex is held.
  325. */
  326. p = ids->entries->p[lid];
  327. ids->entries->p[lid] = NULL;
  328. BUG_ON(p==NULL);
  329. ids->in_use--;
  330. if (lid == ids->max_id) {
  331. do {
  332. lid--;
  333. if(lid == -1)
  334. break;
  335. } while (ids->entries->p[lid] == NULL);
  336. ids->max_id = lid;
  337. }
  338. p->deleted = 1;
  339. return p;
  340. }
  341. /**
  342. * ipc_alloc - allocate ipc space
  343. * @size: size desired
  344. *
  345. * Allocate memory from the appropriate pools and return a pointer to it.
  346. * NULL is returned if the allocation fails
  347. */
  348. void* ipc_alloc(int size)
  349. {
  350. void* out;
  351. if(size > PAGE_SIZE)
  352. out = vmalloc(size);
  353. else
  354. out = kmalloc(size, GFP_KERNEL);
  355. return out;
  356. }
  357. /**
  358. * ipc_free - free ipc space
  359. * @ptr: pointer returned by ipc_alloc
  360. * @size: size of block
  361. *
  362. * Free a block created with ipc_alloc(). The caller must know the size
  363. * used in the allocation call.
  364. */
  365. void ipc_free(void* ptr, int size)
  366. {
  367. if(size > PAGE_SIZE)
  368. vfree(ptr);
  369. else
  370. kfree(ptr);
  371. }
  372. /*
  373. * rcu allocations:
  374. * There are three headers that are prepended to the actual allocation:
  375. * - during use: ipc_rcu_hdr.
  376. * - during the rcu grace period: ipc_rcu_grace.
  377. * - [only if vmalloc]: ipc_rcu_sched.
  378. * Their lifetime doesn't overlap, thus the headers share the same memory.
  379. * Unlike a normal union, they are right-aligned, thus some container_of
  380. * forward/backward casting is necessary:
  381. */
  382. struct ipc_rcu_hdr
  383. {
  384. int refcount;
  385. int is_vmalloc;
  386. void *data[0];
  387. };
  388. struct ipc_rcu_grace
  389. {
  390. struct rcu_head rcu;
  391. /* "void *" makes sure alignment of following data is sane. */
  392. void *data[0];
  393. };
  394. struct ipc_rcu_sched
  395. {
  396. struct work_struct work;
  397. /* "void *" makes sure alignment of following data is sane. */
  398. void *data[0];
  399. };
  400. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  401. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  402. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  403. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  404. static inline int rcu_use_vmalloc(int size)
  405. {
  406. /* Too big for a single page? */
  407. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  408. return 1;
  409. return 0;
  410. }
  411. /**
  412. * ipc_rcu_alloc - allocate ipc and rcu space
  413. * @size: size desired
  414. *
  415. * Allocate memory for the rcu header structure + the object.
  416. * Returns the pointer to the object.
  417. * NULL is returned if the allocation fails.
  418. */
  419. void* ipc_rcu_alloc(int size)
  420. {
  421. void* out;
  422. /*
  423. * We prepend the allocation with the rcu struct, and
  424. * workqueue if necessary (for vmalloc).
  425. */
  426. if (rcu_use_vmalloc(size)) {
  427. out = vmalloc(HDRLEN_VMALLOC + size);
  428. if (out) {
  429. out += HDRLEN_VMALLOC;
  430. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  431. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  432. }
  433. } else {
  434. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  435. if (out) {
  436. out += HDRLEN_KMALLOC;
  437. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  438. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  439. }
  440. }
  441. return out;
  442. }
  443. void ipc_rcu_getref(void *ptr)
  444. {
  445. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  446. }
  447. static void ipc_do_vfree(struct work_struct *work)
  448. {
  449. vfree(container_of(work, struct ipc_rcu_sched, work));
  450. }
  451. /**
  452. * ipc_schedule_free - free ipc + rcu space
  453. * @head: RCU callback structure for queued work
  454. *
  455. * Since RCU callback function is called in bh,
  456. * we need to defer the vfree to schedule_work().
  457. */
  458. static void ipc_schedule_free(struct rcu_head *head)
  459. {
  460. struct ipc_rcu_grace *grace =
  461. container_of(head, struct ipc_rcu_grace, rcu);
  462. struct ipc_rcu_sched *sched =
  463. container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
  464. INIT_WORK(&sched->work, ipc_do_vfree);
  465. schedule_work(&sched->work);
  466. }
  467. /**
  468. * ipc_immediate_free - free ipc + rcu space
  469. * @head: RCU callback structure that contains pointer to be freed
  470. *
  471. * Free from the RCU callback context.
  472. */
  473. static void ipc_immediate_free(struct rcu_head *head)
  474. {
  475. struct ipc_rcu_grace *free =
  476. container_of(head, struct ipc_rcu_grace, rcu);
  477. kfree(free);
  478. }
  479. void ipc_rcu_putref(void *ptr)
  480. {
  481. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  482. return;
  483. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  484. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  485. ipc_schedule_free);
  486. } else {
  487. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  488. ipc_immediate_free);
  489. }
  490. }
  491. /**
  492. * ipcperms - check IPC permissions
  493. * @ipcp: IPC permission set
  494. * @flag: desired permission set.
  495. *
  496. * Check user, group, other permissions for access
  497. * to ipc resources. return 0 if allowed
  498. */
  499. int ipcperms (struct kern_ipc_perm *ipcp, short flag)
  500. { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
  501. int requested_mode, granted_mode, err;
  502. if (unlikely((err = audit_ipc_obj(ipcp))))
  503. return err;
  504. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  505. granted_mode = ipcp->mode;
  506. if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
  507. granted_mode >>= 6;
  508. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  509. granted_mode >>= 3;
  510. /* is there some bit set in requested_mode but not in granted_mode? */
  511. if ((requested_mode & ~granted_mode & 0007) &&
  512. !capable(CAP_IPC_OWNER))
  513. return -1;
  514. return security_ipc_permission(ipcp, flag);
  515. }
  516. /*
  517. * Functions to convert between the kern_ipc_perm structure and the
  518. * old/new ipc_perm structures
  519. */
  520. /**
  521. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  522. * @in: kernel permissions
  523. * @out: new style IPC permissions
  524. *
  525. * Turn the kernel object @in into a set of permissions descriptions
  526. * for returning to userspace (@out).
  527. */
  528. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  529. {
  530. out->key = in->key;
  531. out->uid = in->uid;
  532. out->gid = in->gid;
  533. out->cuid = in->cuid;
  534. out->cgid = in->cgid;
  535. out->mode = in->mode;
  536. out->seq = in->seq;
  537. }
  538. /**
  539. * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
  540. * @in: new style IPC permissions
  541. * @out: old style IPC permissions
  542. *
  543. * Turn the new style permissions object @in into a compatibility
  544. * object and store it into the @out pointer.
  545. */
  546. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  547. {
  548. out->key = in->key;
  549. SET_UID(out->uid, in->uid);
  550. SET_GID(out->gid, in->gid);
  551. SET_UID(out->cuid, in->cuid);
  552. SET_GID(out->cgid, in->cgid);
  553. out->mode = in->mode;
  554. out->seq = in->seq;
  555. }
  556. /*
  557. * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
  558. * is called with shm_ids.mutex locked. Since grow_ary() is also called with
  559. * shm_ids.mutex down(for Shared Memory), there is no need to add read
  560. * barriers here to gurantee the writes in grow_ary() are seen in order
  561. * here (for Alpha).
  562. *
  563. * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
  564. * if in the future ipc_get() is used by other places without ipc_ids.mutex
  565. * down, then ipc_get() needs read memery barriers as ipc_lock() does.
  566. */
  567. struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
  568. {
  569. struct kern_ipc_perm* out;
  570. int lid = id % SEQ_MULTIPLIER;
  571. if(lid >= ids->entries->size)
  572. return NULL;
  573. out = ids->entries->p[lid];
  574. return out;
  575. }
  576. struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
  577. {
  578. struct kern_ipc_perm* out;
  579. int lid = id % SEQ_MULTIPLIER;
  580. struct ipc_id_ary* entries;
  581. rcu_read_lock();
  582. entries = rcu_dereference(ids->entries);
  583. if(lid >= entries->size) {
  584. rcu_read_unlock();
  585. return NULL;
  586. }
  587. out = entries->p[lid];
  588. if(out == NULL) {
  589. rcu_read_unlock();
  590. return NULL;
  591. }
  592. spin_lock(&out->lock);
  593. /* ipc_rmid() may have already freed the ID while ipc_lock
  594. * was spinning: here verify that the structure is still valid
  595. */
  596. if (out->deleted) {
  597. spin_unlock(&out->lock);
  598. rcu_read_unlock();
  599. return NULL;
  600. }
  601. return out;
  602. }
  603. void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
  604. {
  605. rcu_read_lock();
  606. spin_lock(&perm->lock);
  607. }
  608. void ipc_unlock(struct kern_ipc_perm* perm)
  609. {
  610. spin_unlock(&perm->lock);
  611. rcu_read_unlock();
  612. }
  613. int ipc_buildid(struct ipc_ids* ids, int id, int seq)
  614. {
  615. return SEQ_MULTIPLIER*seq + id;
  616. }
  617. int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
  618. {
  619. if(uid/SEQ_MULTIPLIER != ipcp->seq)
  620. return 1;
  621. return 0;
  622. }
  623. #ifdef __ARCH_WANT_IPC_PARSE_VERSION
  624. /**
  625. * ipc_parse_version - IPC call version
  626. * @cmd: pointer to command
  627. *
  628. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  629. * The @cmd value is turned from an encoding command and version into
  630. * just the command code.
  631. */
  632. int ipc_parse_version (int *cmd)
  633. {
  634. if (*cmd & IPC_64) {
  635. *cmd ^= IPC_64;
  636. return IPC_64;
  637. } else {
  638. return IPC_OLD;
  639. }
  640. }
  641. #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
  642. #ifdef CONFIG_PROC_FS
  643. struct ipc_proc_iter {
  644. struct ipc_namespace *ns;
  645. struct ipc_proc_iface *iface;
  646. };
  647. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  648. {
  649. struct ipc_proc_iter *iter = s->private;
  650. struct ipc_proc_iface *iface = iter->iface;
  651. struct kern_ipc_perm *ipc = it;
  652. loff_t p;
  653. struct ipc_ids *ids;
  654. ids = iter->ns->ids[iface->ids];
  655. /* If we had an ipc id locked before, unlock it */
  656. if (ipc && ipc != SEQ_START_TOKEN)
  657. ipc_unlock(ipc);
  658. /*
  659. * p = *pos - 1 (because id 0 starts at position 1)
  660. * + 1 (because we increment the position by one)
  661. */
  662. for (p = *pos; p <= ids->max_id; p++) {
  663. if ((ipc = ipc_lock(ids, p)) != NULL) {
  664. *pos = p + 1;
  665. return ipc;
  666. }
  667. }
  668. /* Out of range - return NULL to terminate iteration */
  669. return NULL;
  670. }
  671. /*
  672. * File positions: pos 0 -> header, pos n -> ipc id + 1.
  673. * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
  674. */
  675. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  676. {
  677. struct ipc_proc_iter *iter = s->private;
  678. struct ipc_proc_iface *iface = iter->iface;
  679. struct kern_ipc_perm *ipc;
  680. loff_t p;
  681. struct ipc_ids *ids;
  682. ids = iter->ns->ids[iface->ids];
  683. /*
  684. * Take the lock - this will be released by the corresponding
  685. * call to stop().
  686. */
  687. mutex_lock(&ids->mutex);
  688. /* pos < 0 is invalid */
  689. if (*pos < 0)
  690. return NULL;
  691. /* pos == 0 means header */
  692. if (*pos == 0)
  693. return SEQ_START_TOKEN;
  694. /* Find the (pos-1)th ipc */
  695. for (p = *pos - 1; p <= ids->max_id; p++) {
  696. if ((ipc = ipc_lock(ids, p)) != NULL) {
  697. *pos = p + 1;
  698. return ipc;
  699. }
  700. }
  701. return NULL;
  702. }
  703. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  704. {
  705. struct kern_ipc_perm *ipc = it;
  706. struct ipc_proc_iter *iter = s->private;
  707. struct ipc_proc_iface *iface = iter->iface;
  708. struct ipc_ids *ids;
  709. /* If we had a locked segment, release it */
  710. if (ipc && ipc != SEQ_START_TOKEN)
  711. ipc_unlock(ipc);
  712. ids = iter->ns->ids[iface->ids];
  713. /* Release the lock we took in start() */
  714. mutex_unlock(&ids->mutex);
  715. }
  716. static int sysvipc_proc_show(struct seq_file *s, void *it)
  717. {
  718. struct ipc_proc_iter *iter = s->private;
  719. struct ipc_proc_iface *iface = iter->iface;
  720. if (it == SEQ_START_TOKEN)
  721. return seq_puts(s, iface->header);
  722. return iface->show(s, it);
  723. }
  724. static struct seq_operations sysvipc_proc_seqops = {
  725. .start = sysvipc_proc_start,
  726. .stop = sysvipc_proc_stop,
  727. .next = sysvipc_proc_next,
  728. .show = sysvipc_proc_show,
  729. };
  730. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  731. {
  732. int ret;
  733. struct seq_file *seq;
  734. struct ipc_proc_iter *iter;
  735. ret = -ENOMEM;
  736. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  737. if (!iter)
  738. goto out;
  739. ret = seq_open(file, &sysvipc_proc_seqops);
  740. if (ret)
  741. goto out_kfree;
  742. seq = file->private_data;
  743. seq->private = iter;
  744. iter->iface = PDE(inode)->data;
  745. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  746. out:
  747. return ret;
  748. out_kfree:
  749. kfree(iter);
  750. goto out;
  751. }
  752. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  753. {
  754. struct seq_file *seq = file->private_data;
  755. struct ipc_proc_iter *iter = seq->private;
  756. put_ipc_ns(iter->ns);
  757. return seq_release_private(inode, file);
  758. }
  759. static const struct file_operations sysvipc_proc_fops = {
  760. .open = sysvipc_proc_open,
  761. .read = seq_read,
  762. .llseek = seq_lseek,
  763. .release = sysvipc_proc_release,
  764. };
  765. #endif /* CONFIG_PROC_FS */