util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. * General sysv ipc locking scheme:
  20. * rcu_read_lock()
  21. * obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
  22. * tree.
  23. * - perform initial checks (capabilities, auditing and permission,
  24. * etc).
  25. * - perform read-only operations, such as STAT, INFO commands.
  26. * acquire the ipc lock (kern_ipc_perm.lock) through
  27. * ipc_lock_object()
  28. * - perform data updates, such as SET, RMID commands and
  29. * mechanism-specific operations (semop/semtimedop,
  30. * msgsnd/msgrcv, shmat/shmdt).
  31. * drop the ipc lock, through ipc_unlock_object().
  32. * rcu_read_unlock()
  33. *
  34. * The ids->rwsem must be taken when:
  35. * - creating, removing and iterating the existing entries in ipc
  36. * identifier sets.
  37. * - iterating through files under /proc/sysvipc/
  38. *
  39. * Note that sems have a special fast path that avoids kern_ipc_perm.lock -
  40. * see sem_lock().
  41. */
  42. #include <linux/mm.h>
  43. #include <linux/shm.h>
  44. #include <linux/init.h>
  45. #include <linux/msg.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/slab.h>
  48. #include <linux/notifier.h>
  49. #include <linux/capability.h>
  50. #include <linux/highuid.h>
  51. #include <linux/security.h>
  52. #include <linux/rcupdate.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/proc_fs.h>
  56. #include <linux/audit.h>
  57. #include <linux/nsproxy.h>
  58. #include <linux/rwsem.h>
  59. #include <linux/memory.h>
  60. #include <linux/ipc_namespace.h>
  61. #include <asm/unistd.h>
  62. #include "util.h"
  63. struct ipc_proc_iface {
  64. const char *path;
  65. const char *header;
  66. int ids;
  67. int (*show)(struct seq_file *, void *);
  68. };
  69. /**
  70. * ipc_init - initialise ipc subsystem
  71. *
  72. * The various sysv ipc resources (semaphores, messages and shared
  73. * memory) are initialised.
  74. *
  75. * A callback routine is registered into the memory hotplug notifier
  76. * chain: since msgmni scales to lowmem this callback routine will be
  77. * called upon successful memory add / remove to recompute msmgni.
  78. */
  79. static int __init ipc_init(void)
  80. {
  81. sem_init();
  82. msg_init();
  83. shm_init();
  84. return 0;
  85. }
  86. device_initcall(ipc_init);
  87. /**
  88. * ipc_init_ids - initialise ipc identifiers
  89. * @ids: ipc identifier set
  90. *
  91. * Set up the sequence range to use for the ipc identifier range (limited
  92. * below IPCMNI) then initialise the ids idr.
  93. */
  94. void ipc_init_ids(struct ipc_ids *ids)
  95. {
  96. ids->in_use = 0;
  97. ids->seq = 0;
  98. ids->next_id = -1;
  99. init_rwsem(&ids->rwsem);
  100. idr_init(&ids->ipcs_idr);
  101. }
  102. #ifdef CONFIG_PROC_FS
  103. static const struct file_operations sysvipc_proc_fops;
  104. /**
  105. * ipc_init_proc_interface - create a proc interface for sysipc types using a seq_file interface.
  106. * @path: Path in procfs
  107. * @header: Banner to be printed at the beginning of the file.
  108. * @ids: ipc id table to iterate.
  109. * @show: show routine.
  110. */
  111. void __init ipc_init_proc_interface(const char *path, const char *header,
  112. int ids, int (*show)(struct seq_file *, void *))
  113. {
  114. struct proc_dir_entry *pde;
  115. struct ipc_proc_iface *iface;
  116. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  117. if (!iface)
  118. return;
  119. iface->path = path;
  120. iface->header = header;
  121. iface->ids = ids;
  122. iface->show = show;
  123. pde = proc_create_data(path,
  124. S_IRUGO, /* world readable */
  125. NULL, /* parent dir */
  126. &sysvipc_proc_fops,
  127. iface);
  128. if (!pde)
  129. kfree(iface);
  130. }
  131. #endif
  132. /**
  133. * ipc_findkey - find a key in an ipc identifier set
  134. * @ids: ipc identifier set
  135. * @key: key to find
  136. *
  137. * Returns the locked pointer to the ipc structure if found or NULL
  138. * otherwise. If key is found ipc points to the owning ipc structure
  139. *
  140. * Called with ipc_ids.rwsem held.
  141. */
  142. static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
  143. {
  144. struct kern_ipc_perm *ipc;
  145. int next_id;
  146. int total;
  147. for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
  148. ipc = idr_find(&ids->ipcs_idr, next_id);
  149. if (ipc == NULL)
  150. continue;
  151. if (ipc->key != key) {
  152. total++;
  153. continue;
  154. }
  155. rcu_read_lock();
  156. ipc_lock_object(ipc);
  157. return ipc;
  158. }
  159. return NULL;
  160. }
  161. /**
  162. * ipc_get_maxid - get the last assigned id
  163. * @ids: ipc identifier set
  164. *
  165. * Called with ipc_ids.rwsem held.
  166. */
  167. int ipc_get_maxid(struct ipc_ids *ids)
  168. {
  169. struct kern_ipc_perm *ipc;
  170. int max_id = -1;
  171. int total, id;
  172. if (ids->in_use == 0)
  173. return -1;
  174. if (ids->in_use == IPCMNI)
  175. return IPCMNI - 1;
  176. /* Look for the last assigned id */
  177. total = 0;
  178. for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
  179. ipc = idr_find(&ids->ipcs_idr, id);
  180. if (ipc != NULL) {
  181. max_id = id;
  182. total++;
  183. }
  184. }
  185. return max_id;
  186. }
  187. /**
  188. * ipc_addid - add an ipc identifier
  189. * @ids: ipc identifier set
  190. * @new: new ipc permission set
  191. * @size: limit for the number of used ids
  192. *
  193. * Add an entry 'new' to the ipc ids idr. The permissions object is
  194. * initialised and the first free entry is set up and the id assigned
  195. * is returned. The 'new' entry is returned in a locked state on success.
  196. * On failure the entry is not locked and a negative err-code is returned.
  197. *
  198. * Called with writer ipc_ids.rwsem held.
  199. */
  200. int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
  201. {
  202. kuid_t euid;
  203. kgid_t egid;
  204. int id;
  205. int next_id = ids->next_id;
  206. if (size > IPCMNI)
  207. size = IPCMNI;
  208. if (ids->in_use >= size)
  209. return -ENOSPC;
  210. idr_preload(GFP_KERNEL);
  211. atomic_set(&new->refcount, 1);
  212. spin_lock_init(&new->lock);
  213. new->deleted = false;
  214. rcu_read_lock();
  215. spin_lock(&new->lock);
  216. current_euid_egid(&euid, &egid);
  217. new->cuid = new->uid = euid;
  218. new->gid = new->cgid = egid;
  219. id = idr_alloc(&ids->ipcs_idr, new,
  220. (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
  221. GFP_NOWAIT);
  222. idr_preload_end();
  223. if (id < 0) {
  224. spin_unlock(&new->lock);
  225. rcu_read_unlock();
  226. return id;
  227. }
  228. ids->in_use++;
  229. if (next_id < 0) {
  230. new->seq = ids->seq++;
  231. if (ids->seq > IPCID_SEQ_MAX)
  232. ids->seq = 0;
  233. } else {
  234. new->seq = ipcid_to_seqx(next_id);
  235. ids->next_id = -1;
  236. }
  237. new->id = ipc_buildid(id, new->seq);
  238. return id;
  239. }
  240. /**
  241. * ipcget_new - create a new ipc object
  242. * @ns: ipc namespace
  243. * @ids: ipc identifier set
  244. * @ops: the actual creation routine to call
  245. * @params: its parameters
  246. *
  247. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  248. * when the key is IPC_PRIVATE.
  249. */
  250. static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
  251. const struct ipc_ops *ops, struct ipc_params *params)
  252. {
  253. int err;
  254. down_write(&ids->rwsem);
  255. err = ops->getnew(ns, params);
  256. up_write(&ids->rwsem);
  257. return err;
  258. }
  259. /**
  260. * ipc_check_perms - check security and permissions for an ipc object
  261. * @ns: ipc namespace
  262. * @ipcp: ipc permission set
  263. * @ops: the actual security routine to call
  264. * @params: its parameters
  265. *
  266. * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
  267. * when the key is not IPC_PRIVATE and that key already exists in the
  268. * ds IDR.
  269. *
  270. * On success, the ipc id is returned.
  271. *
  272. * It is called with ipc_ids.rwsem and ipcp->lock held.
  273. */
  274. static int ipc_check_perms(struct ipc_namespace *ns,
  275. struct kern_ipc_perm *ipcp,
  276. const struct ipc_ops *ops,
  277. struct ipc_params *params)
  278. {
  279. int err;
  280. if (ipcperms(ns, ipcp, params->flg))
  281. err = -EACCES;
  282. else {
  283. err = ops->associate(ipcp, params->flg);
  284. if (!err)
  285. err = ipcp->id;
  286. }
  287. return err;
  288. }
  289. /**
  290. * ipcget_public - get an ipc object or create a new one
  291. * @ns: ipc namespace
  292. * @ids: ipc identifier set
  293. * @ops: the actual creation routine to call
  294. * @params: its parameters
  295. *
  296. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  297. * when the key is not IPC_PRIVATE.
  298. * It adds a new entry if the key is not found and does some permission
  299. * / security checkings if the key is found.
  300. *
  301. * On success, the ipc id is returned.
  302. */
  303. static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
  304. const struct ipc_ops *ops, struct ipc_params *params)
  305. {
  306. struct kern_ipc_perm *ipcp;
  307. int flg = params->flg;
  308. int err;
  309. /*
  310. * Take the lock as a writer since we are potentially going to add
  311. * a new entry + read locks are not "upgradable"
  312. */
  313. down_write(&ids->rwsem);
  314. ipcp = ipc_findkey(ids, params->key);
  315. if (ipcp == NULL) {
  316. /* key not used */
  317. if (!(flg & IPC_CREAT))
  318. err = -ENOENT;
  319. else
  320. err = ops->getnew(ns, params);
  321. } else {
  322. /* ipc object has been locked by ipc_findkey() */
  323. if (flg & IPC_CREAT && flg & IPC_EXCL)
  324. err = -EEXIST;
  325. else {
  326. err = 0;
  327. if (ops->more_checks)
  328. err = ops->more_checks(ipcp, params);
  329. if (!err)
  330. /*
  331. * ipc_check_perms returns the IPC id on
  332. * success
  333. */
  334. err = ipc_check_perms(ns, ipcp, ops, params);
  335. }
  336. ipc_unlock(ipcp);
  337. }
  338. up_write(&ids->rwsem);
  339. return err;
  340. }
  341. /**
  342. * ipc_rmid - remove an ipc identifier
  343. * @ids: ipc identifier set
  344. * @ipcp: ipc perm structure containing the identifier to remove
  345. *
  346. * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
  347. * before this function is called, and remain locked on the exit.
  348. */
  349. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  350. {
  351. int lid = ipcid_to_idx(ipcp->id);
  352. idr_remove(&ids->ipcs_idr, lid);
  353. ids->in_use--;
  354. ipcp->deleted = true;
  355. }
  356. int ipc_rcu_getref(struct kern_ipc_perm *ptr)
  357. {
  358. return atomic_inc_not_zero(&ptr->refcount);
  359. }
  360. void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  361. void (*func)(struct rcu_head *head))
  362. {
  363. if (!atomic_dec_and_test(&ptr->refcount))
  364. return;
  365. call_rcu(&ptr->rcu, func);
  366. }
  367. /**
  368. * ipcperms - check ipc permissions
  369. * @ns: ipc namespace
  370. * @ipcp: ipc permission set
  371. * @flag: desired permission set
  372. *
  373. * Check user, group, other permissions for access
  374. * to ipc resources. return 0 if allowed
  375. *
  376. * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
  377. */
  378. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  379. {
  380. kuid_t euid = current_euid();
  381. int requested_mode, granted_mode;
  382. audit_ipc_obj(ipcp);
  383. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  384. granted_mode = ipcp->mode;
  385. if (uid_eq(euid, ipcp->cuid) ||
  386. uid_eq(euid, ipcp->uid))
  387. granted_mode >>= 6;
  388. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  389. granted_mode >>= 3;
  390. /* is there some bit set in requested_mode but not in granted_mode? */
  391. if ((requested_mode & ~granted_mode & 0007) &&
  392. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  393. return -1;
  394. return security_ipc_permission(ipcp, flag);
  395. }
  396. /*
  397. * Functions to convert between the kern_ipc_perm structure and the
  398. * old/new ipc_perm structures
  399. */
  400. /**
  401. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  402. * @in: kernel permissions
  403. * @out: new style ipc permissions
  404. *
  405. * Turn the kernel object @in into a set of permissions descriptions
  406. * for returning to userspace (@out).
  407. */
  408. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
  409. {
  410. out->key = in->key;
  411. out->uid = from_kuid_munged(current_user_ns(), in->uid);
  412. out->gid = from_kgid_munged(current_user_ns(), in->gid);
  413. out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
  414. out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
  415. out->mode = in->mode;
  416. out->seq = in->seq;
  417. }
  418. /**
  419. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  420. * @in: new style ipc permissions
  421. * @out: old style ipc permissions
  422. *
  423. * Turn the new style permissions object @in into a compatibility
  424. * object and store it into the @out pointer.
  425. */
  426. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
  427. {
  428. out->key = in->key;
  429. SET_UID(out->uid, in->uid);
  430. SET_GID(out->gid, in->gid);
  431. SET_UID(out->cuid, in->cuid);
  432. SET_GID(out->cgid, in->cgid);
  433. out->mode = in->mode;
  434. out->seq = in->seq;
  435. }
  436. /**
  437. * ipc_obtain_object
  438. * @ids: ipc identifier set
  439. * @id: ipc id to look for
  440. *
  441. * Look for an id in the ipc ids idr and return associated ipc object.
  442. *
  443. * Call inside the RCU critical section.
  444. * The ipc object is *not* locked on exit.
  445. */
  446. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
  447. {
  448. struct kern_ipc_perm *out;
  449. int lid = ipcid_to_idx(id);
  450. out = idr_find(&ids->ipcs_idr, lid);
  451. if (!out)
  452. return ERR_PTR(-EINVAL);
  453. return out;
  454. }
  455. /**
  456. * ipc_lock - lock an ipc structure without rwsem held
  457. * @ids: ipc identifier set
  458. * @id: ipc id to look for
  459. *
  460. * Look for an id in the ipc ids idr and lock the associated ipc object.
  461. *
  462. * The ipc object is locked on successful exit.
  463. */
  464. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  465. {
  466. struct kern_ipc_perm *out;
  467. rcu_read_lock();
  468. out = ipc_obtain_object_idr(ids, id);
  469. if (IS_ERR(out))
  470. goto err;
  471. spin_lock(&out->lock);
  472. /*
  473. * ipc_rmid() may have already freed the ID while ipc_lock()
  474. * was spinning: here verify that the structure is still valid.
  475. * Upon races with RMID, return -EIDRM, thus indicating that
  476. * the ID points to a removed identifier.
  477. */
  478. if (ipc_valid_object(out))
  479. return out;
  480. spin_unlock(&out->lock);
  481. out = ERR_PTR(-EIDRM);
  482. err:
  483. rcu_read_unlock();
  484. return out;
  485. }
  486. /**
  487. * ipc_obtain_object_check
  488. * @ids: ipc identifier set
  489. * @id: ipc id to look for
  490. *
  491. * Similar to ipc_obtain_object_idr() but also checks
  492. * the ipc object reference counter.
  493. *
  494. * Call inside the RCU critical section.
  495. * The ipc object is *not* locked on exit.
  496. */
  497. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  498. {
  499. struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
  500. if (IS_ERR(out))
  501. goto out;
  502. if (ipc_checkid(out, id))
  503. return ERR_PTR(-EINVAL);
  504. out:
  505. return out;
  506. }
  507. /**
  508. * ipcget - Common sys_*get() code
  509. * @ns: namespace
  510. * @ids: ipc identifier set
  511. * @ops: operations to be called on ipc object creation, permission checks
  512. * and further checks
  513. * @params: the parameters needed by the previous operations.
  514. *
  515. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  516. */
  517. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  518. const struct ipc_ops *ops, struct ipc_params *params)
  519. {
  520. if (params->key == IPC_PRIVATE)
  521. return ipcget_new(ns, ids, ops, params);
  522. else
  523. return ipcget_public(ns, ids, ops, params);
  524. }
  525. /**
  526. * ipc_update_perm - update the permissions of an ipc object
  527. * @in: the permission given as input.
  528. * @out: the permission of the ipc to set.
  529. */
  530. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  531. {
  532. kuid_t uid = make_kuid(current_user_ns(), in->uid);
  533. kgid_t gid = make_kgid(current_user_ns(), in->gid);
  534. if (!uid_valid(uid) || !gid_valid(gid))
  535. return -EINVAL;
  536. out->uid = uid;
  537. out->gid = gid;
  538. out->mode = (out->mode & ~S_IRWXUGO)
  539. | (in->mode & S_IRWXUGO);
  540. return 0;
  541. }
  542. /**
  543. * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
  544. * @ns: ipc namespace
  545. * @ids: the table of ids where to look for the ipc
  546. * @id: the id of the ipc to retrieve
  547. * @cmd: the cmd to check
  548. * @perm: the permission to set
  549. * @extra_perm: one extra permission parameter used by msq
  550. *
  551. * This function does some common audit and permissions check for some IPC_XXX
  552. * cmd and is called from semctl_down, shmctl_down and msgctl_down.
  553. * It must be called without any lock held and:
  554. *
  555. * - retrieves the ipc with the given id in the given table.
  556. * - performs some audit and permission check, depending on the given cmd
  557. * - returns a pointer to the ipc object or otherwise, the corresponding
  558. * error.
  559. *
  560. * Call holding the both the rwsem and the rcu read lock.
  561. */
  562. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  563. struct ipc_ids *ids, int id, int cmd,
  564. struct ipc64_perm *perm, int extra_perm)
  565. {
  566. kuid_t euid;
  567. int err = -EPERM;
  568. struct kern_ipc_perm *ipcp;
  569. ipcp = ipc_obtain_object_check(ids, id);
  570. if (IS_ERR(ipcp)) {
  571. err = PTR_ERR(ipcp);
  572. goto err;
  573. }
  574. audit_ipc_obj(ipcp);
  575. if (cmd == IPC_SET)
  576. audit_ipc_set_perm(extra_perm, perm->uid,
  577. perm->gid, perm->mode);
  578. euid = current_euid();
  579. if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
  580. ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  581. return ipcp; /* successful lookup */
  582. err:
  583. return ERR_PTR(err);
  584. }
  585. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  586. /**
  587. * ipc_parse_version - ipc call version
  588. * @cmd: pointer to command
  589. *
  590. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  591. * The @cmd value is turned from an encoding command and version into
  592. * just the command code.
  593. */
  594. int ipc_parse_version(int *cmd)
  595. {
  596. if (*cmd & IPC_64) {
  597. *cmd ^= IPC_64;
  598. return IPC_64;
  599. } else {
  600. return IPC_OLD;
  601. }
  602. }
  603. #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
  604. #ifdef CONFIG_PROC_FS
  605. struct ipc_proc_iter {
  606. struct ipc_namespace *ns;
  607. struct ipc_proc_iface *iface;
  608. };
  609. /*
  610. * This routine locks the ipc structure found at least at position pos.
  611. */
  612. static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  613. loff_t *new_pos)
  614. {
  615. struct kern_ipc_perm *ipc;
  616. int total, id;
  617. total = 0;
  618. for (id = 0; id < pos && total < ids->in_use; id++) {
  619. ipc = idr_find(&ids->ipcs_idr, id);
  620. if (ipc != NULL)
  621. total++;
  622. }
  623. if (total >= ids->in_use)
  624. return NULL;
  625. for (; pos < IPCMNI; pos++) {
  626. ipc = idr_find(&ids->ipcs_idr, pos);
  627. if (ipc != NULL) {
  628. *new_pos = pos + 1;
  629. rcu_read_lock();
  630. ipc_lock_object(ipc);
  631. return ipc;
  632. }
  633. }
  634. /* Out of range - return NULL to terminate iteration */
  635. return NULL;
  636. }
  637. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  638. {
  639. struct ipc_proc_iter *iter = s->private;
  640. struct ipc_proc_iface *iface = iter->iface;
  641. struct kern_ipc_perm *ipc = it;
  642. /* If we had an ipc id locked before, unlock it */
  643. if (ipc && ipc != SEQ_START_TOKEN)
  644. ipc_unlock(ipc);
  645. return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
  646. }
  647. /*
  648. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  649. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  650. */
  651. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  652. {
  653. struct ipc_proc_iter *iter = s->private;
  654. struct ipc_proc_iface *iface = iter->iface;
  655. struct ipc_ids *ids;
  656. ids = &iter->ns->ids[iface->ids];
  657. /*
  658. * Take the lock - this will be released by the corresponding
  659. * call to stop().
  660. */
  661. down_read(&ids->rwsem);
  662. /* pos < 0 is invalid */
  663. if (*pos < 0)
  664. return NULL;
  665. /* pos == 0 means header */
  666. if (*pos == 0)
  667. return SEQ_START_TOKEN;
  668. /* Find the (pos-1)th ipc */
  669. return sysvipc_find_ipc(ids, *pos - 1, pos);
  670. }
  671. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  672. {
  673. struct kern_ipc_perm *ipc = it;
  674. struct ipc_proc_iter *iter = s->private;
  675. struct ipc_proc_iface *iface = iter->iface;
  676. struct ipc_ids *ids;
  677. /* If we had a locked structure, release it */
  678. if (ipc && ipc != SEQ_START_TOKEN)
  679. ipc_unlock(ipc);
  680. ids = &iter->ns->ids[iface->ids];
  681. /* Release the lock we took in start() */
  682. up_read(&ids->rwsem);
  683. }
  684. static int sysvipc_proc_show(struct seq_file *s, void *it)
  685. {
  686. struct ipc_proc_iter *iter = s->private;
  687. struct ipc_proc_iface *iface = iter->iface;
  688. if (it == SEQ_START_TOKEN) {
  689. seq_puts(s, iface->header);
  690. return 0;
  691. }
  692. return iface->show(s, it);
  693. }
  694. static const struct seq_operations sysvipc_proc_seqops = {
  695. .start = sysvipc_proc_start,
  696. .stop = sysvipc_proc_stop,
  697. .next = sysvipc_proc_next,
  698. .show = sysvipc_proc_show,
  699. };
  700. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  701. {
  702. struct ipc_proc_iter *iter;
  703. iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
  704. if (!iter)
  705. return -ENOMEM;
  706. iter->iface = PDE_DATA(inode);
  707. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  708. return 0;
  709. }
  710. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  711. {
  712. struct seq_file *seq = file->private_data;
  713. struct ipc_proc_iter *iter = seq->private;
  714. put_ipc_ns(iter->ns);
  715. return seq_release_private(inode, file);
  716. }
  717. static const struct file_operations sysvipc_proc_fops = {
  718. .open = sysvipc_proc_open,
  719. .read = seq_read,
  720. .llseek = seq_lseek,
  721. .release = sysvipc_proc_release,
  722. };
  723. #endif /* CONFIG_PROC_FS */