util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. spin_lock_init(&new->lock);
  212. new->deleted = false;
  213. rcu_read_lock();
  214. spin_lock(&new->lock);
  215. current_euid_egid(&euid, &egid);
  216. new->cuid = new->uid = euid;
  217. new->gid = new->cgid = egid;
  218. id = idr_alloc(&ids->ipcs_idr, new,
  219. (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
  220. GFP_NOWAIT);
  221. idr_preload_end();
  222. if (id < 0) {
  223. spin_unlock(&new->lock);
  224. rcu_read_unlock();
  225. return id;
  226. }
  227. ids->in_use++;
  228. if (next_id < 0) {
  229. new->seq = ids->seq++;
  230. if (ids->seq > IPCID_SEQ_MAX)
  231. ids->seq = 0;
  232. } else {
  233. new->seq = ipcid_to_seqx(next_id);
  234. ids->next_id = -1;
  235. }
  236. new->id = ipc_buildid(id, new->seq);
  237. return id;
  238. }
  239. /**
  240. * ipcget_new - create a new ipc object
  241. * @ns: ipc namespace
  242. * @ids: ipc identifier set
  243. * @ops: the actual creation routine to call
  244. * @params: its parameters
  245. *
  246. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  247. * when the key is IPC_PRIVATE.
  248. */
  249. static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
  250. const struct ipc_ops *ops, struct ipc_params *params)
  251. {
  252. int err;
  253. down_write(&ids->rwsem);
  254. err = ops->getnew(ns, params);
  255. up_write(&ids->rwsem);
  256. return err;
  257. }
  258. /**
  259. * ipc_check_perms - check security and permissions for an ipc object
  260. * @ns: ipc namespace
  261. * @ipcp: ipc permission set
  262. * @ops: the actual security routine to call
  263. * @params: its parameters
  264. *
  265. * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
  266. * when the key is not IPC_PRIVATE and that key already exists in the
  267. * ds IDR.
  268. *
  269. * On success, the ipc id is returned.
  270. *
  271. * It is called with ipc_ids.rwsem and ipcp->lock held.
  272. */
  273. static int ipc_check_perms(struct ipc_namespace *ns,
  274. struct kern_ipc_perm *ipcp,
  275. const struct ipc_ops *ops,
  276. struct ipc_params *params)
  277. {
  278. int err;
  279. if (ipcperms(ns, ipcp, params->flg))
  280. err = -EACCES;
  281. else {
  282. err = ops->associate(ipcp, params->flg);
  283. if (!err)
  284. err = ipcp->id;
  285. }
  286. return err;
  287. }
  288. /**
  289. * ipcget_public - get an ipc object or create a new one
  290. * @ns: ipc namespace
  291. * @ids: ipc identifier set
  292. * @ops: the actual creation routine to call
  293. * @params: its parameters
  294. *
  295. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  296. * when the key is not IPC_PRIVATE.
  297. * It adds a new entry if the key is not found and does some permission
  298. * / security checkings if the key is found.
  299. *
  300. * On success, the ipc id is returned.
  301. */
  302. static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
  303. const struct ipc_ops *ops, struct ipc_params *params)
  304. {
  305. struct kern_ipc_perm *ipcp;
  306. int flg = params->flg;
  307. int err;
  308. /*
  309. * Take the lock as a writer since we are potentially going to add
  310. * a new entry + read locks are not "upgradable"
  311. */
  312. down_write(&ids->rwsem);
  313. ipcp = ipc_findkey(ids, params->key);
  314. if (ipcp == NULL) {
  315. /* key not used */
  316. if (!(flg & IPC_CREAT))
  317. err = -ENOENT;
  318. else
  319. err = ops->getnew(ns, params);
  320. } else {
  321. /* ipc object has been locked by ipc_findkey() */
  322. if (flg & IPC_CREAT && flg & IPC_EXCL)
  323. err = -EEXIST;
  324. else {
  325. err = 0;
  326. if (ops->more_checks)
  327. err = ops->more_checks(ipcp, params);
  328. if (!err)
  329. /*
  330. * ipc_check_perms returns the IPC id on
  331. * success
  332. */
  333. err = ipc_check_perms(ns, ipcp, ops, params);
  334. }
  335. ipc_unlock(ipcp);
  336. }
  337. up_write(&ids->rwsem);
  338. return err;
  339. }
  340. /**
  341. * ipc_rmid - remove an ipc identifier
  342. * @ids: ipc identifier set
  343. * @ipcp: ipc perm structure containing the identifier to remove
  344. *
  345. * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
  346. * before this function is called, and remain locked on the exit.
  347. */
  348. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  349. {
  350. int lid = ipcid_to_idx(ipcp->id);
  351. idr_remove(&ids->ipcs_idr, lid);
  352. ids->in_use--;
  353. ipcp->deleted = true;
  354. }
  355. int ipc_rcu_getref(struct kern_ipc_perm *ptr)
  356. {
  357. return atomic_inc_not_zero(&ptr->refcount);
  358. }
  359. void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  360. void (*func)(struct rcu_head *head))
  361. {
  362. if (!atomic_dec_and_test(&ptr->refcount))
  363. return;
  364. call_rcu(&ptr->rcu, func);
  365. }
  366. /**
  367. * ipcperms - check ipc permissions
  368. * @ns: ipc namespace
  369. * @ipcp: ipc permission set
  370. * @flag: desired permission set
  371. *
  372. * Check user, group, other permissions for access
  373. * to ipc resources. return 0 if allowed
  374. *
  375. * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
  376. */
  377. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  378. {
  379. kuid_t euid = current_euid();
  380. int requested_mode, granted_mode;
  381. audit_ipc_obj(ipcp);
  382. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  383. granted_mode = ipcp->mode;
  384. if (uid_eq(euid, ipcp->cuid) ||
  385. uid_eq(euid, ipcp->uid))
  386. granted_mode >>= 6;
  387. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  388. granted_mode >>= 3;
  389. /* is there some bit set in requested_mode but not in granted_mode? */
  390. if ((requested_mode & ~granted_mode & 0007) &&
  391. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  392. return -1;
  393. return security_ipc_permission(ipcp, flag);
  394. }
  395. /*
  396. * Functions to convert between the kern_ipc_perm structure and the
  397. * old/new ipc_perm structures
  398. */
  399. /**
  400. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  401. * @in: kernel permissions
  402. * @out: new style ipc permissions
  403. *
  404. * Turn the kernel object @in into a set of permissions descriptions
  405. * for returning to userspace (@out).
  406. */
  407. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
  408. {
  409. out->key = in->key;
  410. out->uid = from_kuid_munged(current_user_ns(), in->uid);
  411. out->gid = from_kgid_munged(current_user_ns(), in->gid);
  412. out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
  413. out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
  414. out->mode = in->mode;
  415. out->seq = in->seq;
  416. }
  417. /**
  418. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  419. * @in: new style ipc permissions
  420. * @out: old style ipc permissions
  421. *
  422. * Turn the new style permissions object @in into a compatibility
  423. * object and store it into the @out pointer.
  424. */
  425. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
  426. {
  427. out->key = in->key;
  428. SET_UID(out->uid, in->uid);
  429. SET_GID(out->gid, in->gid);
  430. SET_UID(out->cuid, in->cuid);
  431. SET_GID(out->cgid, in->cgid);
  432. out->mode = in->mode;
  433. out->seq = in->seq;
  434. }
  435. /**
  436. * ipc_obtain_object
  437. * @ids: ipc identifier set
  438. * @id: ipc id to look for
  439. *
  440. * Look for an id in the ipc ids idr and return associated ipc object.
  441. *
  442. * Call inside the RCU critical section.
  443. * The ipc object is *not* locked on exit.
  444. */
  445. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
  446. {
  447. struct kern_ipc_perm *out;
  448. int lid = ipcid_to_idx(id);
  449. out = idr_find(&ids->ipcs_idr, lid);
  450. if (!out)
  451. return ERR_PTR(-EINVAL);
  452. return out;
  453. }
  454. /**
  455. * ipc_lock - lock an ipc structure without rwsem held
  456. * @ids: ipc identifier set
  457. * @id: ipc id to look for
  458. *
  459. * Look for an id in the ipc ids idr and lock the associated ipc object.
  460. *
  461. * The ipc object is locked on successful exit.
  462. */
  463. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  464. {
  465. struct kern_ipc_perm *out;
  466. rcu_read_lock();
  467. out = ipc_obtain_object_idr(ids, id);
  468. if (IS_ERR(out))
  469. goto err;
  470. spin_lock(&out->lock);
  471. /*
  472. * ipc_rmid() may have already freed the ID while ipc_lock()
  473. * was spinning: here verify that the structure is still valid.
  474. * Upon races with RMID, return -EIDRM, thus indicating that
  475. * the ID points to a removed identifier.
  476. */
  477. if (ipc_valid_object(out))
  478. return out;
  479. spin_unlock(&out->lock);
  480. out = ERR_PTR(-EIDRM);
  481. err:
  482. rcu_read_unlock();
  483. return out;
  484. }
  485. /**
  486. * ipc_obtain_object_check
  487. * @ids: ipc identifier set
  488. * @id: ipc id to look for
  489. *
  490. * Similar to ipc_obtain_object_idr() but also checks
  491. * the ipc object reference counter.
  492. *
  493. * Call inside the RCU critical section.
  494. * The ipc object is *not* locked on exit.
  495. */
  496. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  497. {
  498. struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
  499. if (IS_ERR(out))
  500. goto out;
  501. if (ipc_checkid(out, id))
  502. return ERR_PTR(-EINVAL);
  503. out:
  504. return out;
  505. }
  506. /**
  507. * ipcget - Common sys_*get() code
  508. * @ns: namespace
  509. * @ids: ipc identifier set
  510. * @ops: operations to be called on ipc object creation, permission checks
  511. * and further checks
  512. * @params: the parameters needed by the previous operations.
  513. *
  514. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  515. */
  516. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  517. const struct ipc_ops *ops, struct ipc_params *params)
  518. {
  519. if (params->key == IPC_PRIVATE)
  520. return ipcget_new(ns, ids, ops, params);
  521. else
  522. return ipcget_public(ns, ids, ops, params);
  523. }
  524. /**
  525. * ipc_update_perm - update the permissions of an ipc object
  526. * @in: the permission given as input.
  527. * @out: the permission of the ipc to set.
  528. */
  529. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  530. {
  531. kuid_t uid = make_kuid(current_user_ns(), in->uid);
  532. kgid_t gid = make_kgid(current_user_ns(), in->gid);
  533. if (!uid_valid(uid) || !gid_valid(gid))
  534. return -EINVAL;
  535. out->uid = uid;
  536. out->gid = gid;
  537. out->mode = (out->mode & ~S_IRWXUGO)
  538. | (in->mode & S_IRWXUGO);
  539. return 0;
  540. }
  541. /**
  542. * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
  543. * @ns: ipc namespace
  544. * @ids: the table of ids where to look for the ipc
  545. * @id: the id of the ipc to retrieve
  546. * @cmd: the cmd to check
  547. * @perm: the permission to set
  548. * @extra_perm: one extra permission parameter used by msq
  549. *
  550. * This function does some common audit and permissions check for some IPC_XXX
  551. * cmd and is called from semctl_down, shmctl_down and msgctl_down.
  552. * It must be called without any lock held and:
  553. *
  554. * - retrieves the ipc with the given id in the given table.
  555. * - performs some audit and permission check, depending on the given cmd
  556. * - returns a pointer to the ipc object or otherwise, the corresponding
  557. * error.
  558. *
  559. * Call holding the both the rwsem and the rcu read lock.
  560. */
  561. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  562. struct ipc_ids *ids, int id, int cmd,
  563. struct ipc64_perm *perm, int extra_perm)
  564. {
  565. kuid_t euid;
  566. int err = -EPERM;
  567. struct kern_ipc_perm *ipcp;
  568. ipcp = ipc_obtain_object_check(ids, id);
  569. if (IS_ERR(ipcp)) {
  570. err = PTR_ERR(ipcp);
  571. goto err;
  572. }
  573. audit_ipc_obj(ipcp);
  574. if (cmd == IPC_SET)
  575. audit_ipc_set_perm(extra_perm, perm->uid,
  576. perm->gid, perm->mode);
  577. euid = current_euid();
  578. if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
  579. ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  580. return ipcp; /* successful lookup */
  581. err:
  582. return ERR_PTR(err);
  583. }
  584. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  585. /**
  586. * ipc_parse_version - ipc call version
  587. * @cmd: pointer to command
  588. *
  589. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  590. * The @cmd value is turned from an encoding command and version into
  591. * just the command code.
  592. */
  593. int ipc_parse_version(int *cmd)
  594. {
  595. if (*cmd & IPC_64) {
  596. *cmd ^= IPC_64;
  597. return IPC_64;
  598. } else {
  599. return IPC_OLD;
  600. }
  601. }
  602. #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
  603. #ifdef CONFIG_PROC_FS
  604. struct ipc_proc_iter {
  605. struct ipc_namespace *ns;
  606. struct ipc_proc_iface *iface;
  607. };
  608. /*
  609. * This routine locks the ipc structure found at least at position pos.
  610. */
  611. static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  612. loff_t *new_pos)
  613. {
  614. struct kern_ipc_perm *ipc;
  615. int total, id;
  616. total = 0;
  617. for (id = 0; id < pos && total < ids->in_use; id++) {
  618. ipc = idr_find(&ids->ipcs_idr, id);
  619. if (ipc != NULL)
  620. total++;
  621. }
  622. if (total >= ids->in_use)
  623. return NULL;
  624. for (; pos < IPCMNI; pos++) {
  625. ipc = idr_find(&ids->ipcs_idr, pos);
  626. if (ipc != NULL) {
  627. *new_pos = pos + 1;
  628. rcu_read_lock();
  629. ipc_lock_object(ipc);
  630. return ipc;
  631. }
  632. }
  633. /* Out of range - return NULL to terminate iteration */
  634. return NULL;
  635. }
  636. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  637. {
  638. struct ipc_proc_iter *iter = s->private;
  639. struct ipc_proc_iface *iface = iter->iface;
  640. struct kern_ipc_perm *ipc = it;
  641. /* If we had an ipc id locked before, unlock it */
  642. if (ipc && ipc != SEQ_START_TOKEN)
  643. ipc_unlock(ipc);
  644. return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
  645. }
  646. /*
  647. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  648. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  649. */
  650. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  651. {
  652. struct ipc_proc_iter *iter = s->private;
  653. struct ipc_proc_iface *iface = iter->iface;
  654. struct ipc_ids *ids;
  655. ids = &iter->ns->ids[iface->ids];
  656. /*
  657. * Take the lock - this will be released by the corresponding
  658. * call to stop().
  659. */
  660. down_read(&ids->rwsem);
  661. /* pos < 0 is invalid */
  662. if (*pos < 0)
  663. return NULL;
  664. /* pos == 0 means header */
  665. if (*pos == 0)
  666. return SEQ_START_TOKEN;
  667. /* Find the (pos-1)th ipc */
  668. return sysvipc_find_ipc(ids, *pos - 1, pos);
  669. }
  670. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  671. {
  672. struct kern_ipc_perm *ipc = it;
  673. struct ipc_proc_iter *iter = s->private;
  674. struct ipc_proc_iface *iface = iter->iface;
  675. struct ipc_ids *ids;
  676. /* If we had a locked structure, release it */
  677. if (ipc && ipc != SEQ_START_TOKEN)
  678. ipc_unlock(ipc);
  679. ids = &iter->ns->ids[iface->ids];
  680. /* Release the lock we took in start() */
  681. up_read(&ids->rwsem);
  682. }
  683. static int sysvipc_proc_show(struct seq_file *s, void *it)
  684. {
  685. struct ipc_proc_iter *iter = s->private;
  686. struct ipc_proc_iface *iface = iter->iface;
  687. if (it == SEQ_START_TOKEN) {
  688. seq_puts(s, iface->header);
  689. return 0;
  690. }
  691. return iface->show(s, it);
  692. }
  693. static const struct seq_operations sysvipc_proc_seqops = {
  694. .start = sysvipc_proc_start,
  695. .stop = sysvipc_proc_stop,
  696. .next = sysvipc_proc_next,
  697. .show = sysvipc_proc_show,
  698. };
  699. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  700. {
  701. struct ipc_proc_iter *iter;
  702. iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
  703. if (!iter)
  704. return -ENOMEM;
  705. iter->iface = PDE_DATA(inode);
  706. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  707. return 0;
  708. }
  709. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  710. {
  711. struct seq_file *seq = file->private_data;
  712. struct ipc_proc_iter *iter = seq->private;
  713. put_ipc_ns(iter->ns);
  714. return seq_release_private(inode, file);
  715. }
  716. static const struct file_operations sysvipc_proc_fops = {
  717. .open = sysvipc_proc_open,
  718. .read = seq_read,
  719. .llseek = seq_lseek,
  720. .release = sysvipc_proc_release,
  721. };
  722. #endif /* CONFIG_PROC_FS */