util.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/ipc/util.c
  4. * Copyright (C) 1992 Krishna Balasubramanian
  5. *
  6. * Sep 1997 - Call suser() last after "normal" permission checks so we
  7. * get BSD style process accounting right.
  8. * Occurs in several places in the IPC code.
  9. * Chris Evans, <[email protected]>
  10. * Nov 1999 - ipc helper functions, unified SMP locking
  11. * Manfred Spraul <[email protected]>
  12. * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
  13. * Mingming Cao <[email protected]>
  14. * Mar 2006 - support for audit of ipc object properties
  15. * Dustin Kirkland <[email protected]>
  16. * Jun 2006 - namespaces ssupport
  17. * OpenVZ, SWsoft Inc.
  18. * Pavel Emelianov <[email protected]>
  19. *
  20. * General sysv ipc locking scheme:
  21. * rcu_read_lock()
  22. * obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
  23. * tree.
  24. * - perform initial checks (capabilities, auditing and permission,
  25. * etc).
  26. * - perform read-only operations, such as INFO command, that
  27. * do not demand atomicity
  28. * acquire the ipc lock (kern_ipc_perm.lock) through
  29. * ipc_lock_object()
  30. * - perform read-only operations that demand atomicity,
  31. * such as STAT command.
  32. * - perform data updates, such as SET, RMID commands and
  33. * mechanism-specific operations (semop/semtimedop,
  34. * msgsnd/msgrcv, shmat/shmdt).
  35. * drop the ipc lock, through ipc_unlock_object().
  36. * rcu_read_unlock()
  37. *
  38. * The ids->rwsem must be taken when:
  39. * - creating, removing and iterating the existing entries in ipc
  40. * identifier sets.
  41. * - iterating through files under /proc/sysvipc/
  42. *
  43. * Note that sems have a special fast path that avoids kern_ipc_perm.lock -
  44. * see sem_lock().
  45. */
  46. #include <linux/mm.h>
  47. #include <linux/shm.h>
  48. #include <linux/init.h>
  49. #include <linux/msg.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/slab.h>
  52. #include <linux/notifier.h>
  53. #include <linux/capability.h>
  54. #include <linux/highuid.h>
  55. #include <linux/security.h>
  56. #include <linux/rcupdate.h>
  57. #include <linux/workqueue.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/proc_fs.h>
  60. #include <linux/audit.h>
  61. #include <linux/nsproxy.h>
  62. #include <linux/rwsem.h>
  63. #include <linux/memory.h>
  64. #include <linux/ipc_namespace.h>
  65. #include <linux/rhashtable.h>
  66. #include <linux/log2.h>
  67. #include <asm/unistd.h>
  68. #include "util.h"
  69. struct ipc_proc_iface {
  70. const char *path;
  71. const char *header;
  72. int ids;
  73. int (*show)(struct seq_file *, void *);
  74. };
  75. /**
  76. * ipc_init - initialise ipc subsystem
  77. *
  78. * The various sysv ipc resources (semaphores, messages and shared
  79. * memory) are initialised.
  80. *
  81. * A callback routine is registered into the memory hotplug notifier
  82. * chain: since msgmni scales to lowmem this callback routine will be
  83. * called upon successful memory add / remove to recompute msmgni.
  84. */
  85. static int __init ipc_init(void)
  86. {
  87. proc_mkdir("sysvipc", NULL);
  88. sem_init();
  89. msg_init();
  90. shm_init();
  91. return 0;
  92. }
  93. device_initcall(ipc_init);
  94. static const struct rhashtable_params ipc_kht_params = {
  95. .head_offset = offsetof(struct kern_ipc_perm, khtnode),
  96. .key_offset = offsetof(struct kern_ipc_perm, key),
  97. .key_len = sizeof_field(struct kern_ipc_perm, key),
  98. .automatic_shrinking = true,
  99. };
  100. /**
  101. * ipc_init_ids - initialise ipc identifiers
  102. * @ids: ipc identifier set
  103. *
  104. * Set up the sequence range to use for the ipc identifier range (limited
  105. * below ipc_mni) then initialise the keys hashtable and ids idr.
  106. */
  107. void ipc_init_ids(struct ipc_ids *ids)
  108. {
  109. ids->in_use = 0;
  110. ids->seq = 0;
  111. init_rwsem(&ids->rwsem);
  112. rhashtable_init(&ids->key_ht, &ipc_kht_params);
  113. idr_init(&ids->ipcs_idr);
  114. ids->max_idx = -1;
  115. ids->last_idx = -1;
  116. #ifdef CONFIG_CHECKPOINT_RESTORE
  117. ids->next_id = -1;
  118. #endif
  119. }
  120. #ifdef CONFIG_PROC_FS
  121. static const struct proc_ops sysvipc_proc_ops;
  122. /**
  123. * ipc_init_proc_interface - create a proc interface for sysipc types using a seq_file interface.
  124. * @path: Path in procfs
  125. * @header: Banner to be printed at the beginning of the file.
  126. * @ids: ipc id table to iterate.
  127. * @show: show routine.
  128. */
  129. void __init ipc_init_proc_interface(const char *path, const char *header,
  130. int ids, int (*show)(struct seq_file *, void *))
  131. {
  132. struct proc_dir_entry *pde;
  133. struct ipc_proc_iface *iface;
  134. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  135. if (!iface)
  136. return;
  137. iface->path = path;
  138. iface->header = header;
  139. iface->ids = ids;
  140. iface->show = show;
  141. pde = proc_create_data(path,
  142. S_IRUGO, /* world readable */
  143. NULL, /* parent dir */
  144. &sysvipc_proc_ops,
  145. iface);
  146. if (!pde)
  147. kfree(iface);
  148. }
  149. #endif
  150. /**
  151. * ipc_findkey - find a key in an ipc identifier set
  152. * @ids: ipc identifier set
  153. * @key: key to find
  154. *
  155. * Returns the locked pointer to the ipc structure if found or NULL
  156. * otherwise. If key is found ipc points to the owning ipc structure
  157. *
  158. * Called with writer ipc_ids.rwsem held.
  159. */
  160. static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
  161. {
  162. struct kern_ipc_perm *ipcp;
  163. ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
  164. ipc_kht_params);
  165. if (!ipcp)
  166. return NULL;
  167. rcu_read_lock();
  168. ipc_lock_object(ipcp);
  169. return ipcp;
  170. }
  171. /*
  172. * Insert new IPC object into idr tree, and set sequence number and id
  173. * in the correct order.
  174. * Especially:
  175. * - the sequence number must be set before inserting the object into the idr,
  176. * because the sequence number is accessed without a lock.
  177. * - the id can/must be set after inserting the object into the idr.
  178. * All accesses must be done after getting kern_ipc_perm.lock.
  179. *
  180. * The caller must own kern_ipc_perm.lock.of the new object.
  181. * On error, the function returns a (negative) error code.
  182. *
  183. * To conserve sequence number space, especially with extended ipc_mni,
  184. * the sequence number is incremented only when the returned ID is less than
  185. * the last one.
  186. */
  187. static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
  188. {
  189. int idx, next_id = -1;
  190. #ifdef CONFIG_CHECKPOINT_RESTORE
  191. next_id = ids->next_id;
  192. ids->next_id = -1;
  193. #endif
  194. /*
  195. * As soon as a new object is inserted into the idr,
  196. * ipc_obtain_object_idr() or ipc_obtain_object_check() can find it,
  197. * and the lockless preparations for ipc operations can start.
  198. * This means especially: permission checks, audit calls, allocation
  199. * of undo structures, ...
  200. *
  201. * Thus the object must be fully initialized, and if something fails,
  202. * then the full tear-down sequence must be followed.
  203. * (i.e.: set new->deleted, reduce refcount, call_rcu())
  204. */
  205. if (next_id < 0) { /* !CHECKPOINT_RESTORE or next_id is unset */
  206. int max_idx;
  207. max_idx = max(ids->in_use*3/2, ipc_min_cycle);
  208. max_idx = min(max_idx, ipc_mni);
  209. /* allocate the idx, with a NULL struct kern_ipc_perm */
  210. idx = idr_alloc_cyclic(&ids->ipcs_idr, NULL, 0, max_idx,
  211. GFP_NOWAIT);
  212. if (idx >= 0) {
  213. /*
  214. * idx got allocated successfully.
  215. * Now calculate the sequence number and set the
  216. * pointer for real.
  217. */
  218. if (idx <= ids->last_idx) {
  219. ids->seq++;
  220. if (ids->seq >= ipcid_seq_max())
  221. ids->seq = 0;
  222. }
  223. ids->last_idx = idx;
  224. new->seq = ids->seq;
  225. /* no need for smp_wmb(), this is done
  226. * inside idr_replace, as part of
  227. * rcu_assign_pointer
  228. */
  229. idr_replace(&ids->ipcs_idr, new, idx);
  230. }
  231. } else {
  232. new->seq = ipcid_to_seqx(next_id);
  233. idx = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id),
  234. 0, GFP_NOWAIT);
  235. }
  236. if (idx >= 0)
  237. new->id = (new->seq << ipcmni_seq_shift()) + idx;
  238. return idx;
  239. }
  240. /**
  241. * ipc_addid - add an ipc identifier
  242. * @ids: ipc identifier set
  243. * @new: new ipc permission set
  244. * @limit: limit for the number of used ids
  245. *
  246. * Add an entry 'new' to the ipc ids idr. The permissions object is
  247. * initialised and the first free entry is set up and the index assigned
  248. * is returned. The 'new' entry is returned in a locked state on success.
  249. *
  250. * On failure the entry is not locked and a negative err-code is returned.
  251. * The caller must use ipc_rcu_putref() to free the identifier.
  252. *
  253. * Called with writer ipc_ids.rwsem held.
  254. */
  255. int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
  256. {
  257. kuid_t euid;
  258. kgid_t egid;
  259. int idx, err;
  260. /* 1) Initialize the refcount so that ipc_rcu_putref works */
  261. refcount_set(&new->refcount, 1);
  262. if (limit > ipc_mni)
  263. limit = ipc_mni;
  264. if (ids->in_use >= limit)
  265. return -ENOSPC;
  266. idr_preload(GFP_KERNEL);
  267. spin_lock_init(&new->lock);
  268. rcu_read_lock();
  269. spin_lock(&new->lock);
  270. current_euid_egid(&euid, &egid);
  271. new->cuid = new->uid = euid;
  272. new->gid = new->cgid = egid;
  273. new->deleted = false;
  274. idx = ipc_idr_alloc(ids, new);
  275. idr_preload_end();
  276. if (idx >= 0 && new->key != IPC_PRIVATE) {
  277. err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
  278. ipc_kht_params);
  279. if (err < 0) {
  280. idr_remove(&ids->ipcs_idr, idx);
  281. idx = err;
  282. }
  283. }
  284. if (idx < 0) {
  285. new->deleted = true;
  286. spin_unlock(&new->lock);
  287. rcu_read_unlock();
  288. return idx;
  289. }
  290. ids->in_use++;
  291. if (idx > ids->max_idx)
  292. ids->max_idx = idx;
  293. return idx;
  294. }
  295. /**
  296. * ipcget_new - create a new ipc object
  297. * @ns: ipc namespace
  298. * @ids: ipc identifier set
  299. * @ops: the actual creation routine to call
  300. * @params: its parameters
  301. *
  302. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  303. * when the key is IPC_PRIVATE.
  304. */
  305. static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
  306. const struct ipc_ops *ops, struct ipc_params *params)
  307. {
  308. int err;
  309. down_write(&ids->rwsem);
  310. err = ops->getnew(ns, params);
  311. up_write(&ids->rwsem);
  312. return err;
  313. }
  314. /**
  315. * ipc_check_perms - check security and permissions for an ipc object
  316. * @ns: ipc namespace
  317. * @ipcp: ipc permission set
  318. * @ops: the actual security routine to call
  319. * @params: its parameters
  320. *
  321. * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
  322. * when the key is not IPC_PRIVATE and that key already exists in the
  323. * ds IDR.
  324. *
  325. * On success, the ipc id is returned.
  326. *
  327. * It is called with ipc_ids.rwsem and ipcp->lock held.
  328. */
  329. static int ipc_check_perms(struct ipc_namespace *ns,
  330. struct kern_ipc_perm *ipcp,
  331. const struct ipc_ops *ops,
  332. struct ipc_params *params)
  333. {
  334. int err;
  335. if (ipcperms(ns, ipcp, params->flg))
  336. err = -EACCES;
  337. else {
  338. err = ops->associate(ipcp, params->flg);
  339. if (!err)
  340. err = ipcp->id;
  341. }
  342. return err;
  343. }
  344. /**
  345. * ipcget_public - get an ipc object or create a new one
  346. * @ns: ipc namespace
  347. * @ids: ipc identifier set
  348. * @ops: the actual creation routine to call
  349. * @params: its parameters
  350. *
  351. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  352. * when the key is not IPC_PRIVATE.
  353. * It adds a new entry if the key is not found and does some permission
  354. * / security checkings if the key is found.
  355. *
  356. * On success, the ipc id is returned.
  357. */
  358. static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
  359. const struct ipc_ops *ops, struct ipc_params *params)
  360. {
  361. struct kern_ipc_perm *ipcp;
  362. int flg = params->flg;
  363. int err;
  364. /*
  365. * Take the lock as a writer since we are potentially going to add
  366. * a new entry + read locks are not "upgradable"
  367. */
  368. down_write(&ids->rwsem);
  369. ipcp = ipc_findkey(ids, params->key);
  370. if (ipcp == NULL) {
  371. /* key not used */
  372. if (!(flg & IPC_CREAT))
  373. err = -ENOENT;
  374. else
  375. err = ops->getnew(ns, params);
  376. } else {
  377. /* ipc object has been locked by ipc_findkey() */
  378. if (flg & IPC_CREAT && flg & IPC_EXCL)
  379. err = -EEXIST;
  380. else {
  381. err = 0;
  382. if (ops->more_checks)
  383. err = ops->more_checks(ipcp, params);
  384. if (!err)
  385. /*
  386. * ipc_check_perms returns the IPC id on
  387. * success
  388. */
  389. err = ipc_check_perms(ns, ipcp, ops, params);
  390. }
  391. ipc_unlock(ipcp);
  392. }
  393. up_write(&ids->rwsem);
  394. return err;
  395. }
  396. /**
  397. * ipc_kht_remove - remove an ipc from the key hashtable
  398. * @ids: ipc identifier set
  399. * @ipcp: ipc perm structure containing the key to remove
  400. *
  401. * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
  402. * before this function is called, and remain locked on the exit.
  403. */
  404. static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  405. {
  406. if (ipcp->key != IPC_PRIVATE)
  407. WARN_ON_ONCE(rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
  408. ipc_kht_params));
  409. }
  410. /**
  411. * ipc_search_maxidx - search for the highest assigned index
  412. * @ids: ipc identifier set
  413. * @limit: known upper limit for highest assigned index
  414. *
  415. * The function determines the highest assigned index in @ids. It is intended
  416. * to be called when ids->max_idx needs to be updated.
  417. * Updating ids->max_idx is necessary when the current highest index ipc
  418. * object is deleted.
  419. * If no ipc object is allocated, then -1 is returned.
  420. *
  421. * ipc_ids.rwsem needs to be held by the caller.
  422. */
  423. static int ipc_search_maxidx(struct ipc_ids *ids, int limit)
  424. {
  425. int tmpidx;
  426. int i;
  427. int retval;
  428. i = ilog2(limit+1);
  429. retval = 0;
  430. for (; i >= 0; i--) {
  431. tmpidx = retval | (1<<i);
  432. /*
  433. * "0" is a possible index value, thus search using
  434. * e.g. 15,7,3,1,0 instead of 16,8,4,2,1.
  435. */
  436. tmpidx = tmpidx-1;
  437. if (idr_get_next(&ids->ipcs_idr, &tmpidx))
  438. retval |= (1<<i);
  439. }
  440. return retval - 1;
  441. }
  442. /**
  443. * ipc_rmid - remove an ipc identifier
  444. * @ids: ipc identifier set
  445. * @ipcp: ipc perm structure containing the identifier to remove
  446. *
  447. * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
  448. * before this function is called, and remain locked on the exit.
  449. */
  450. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  451. {
  452. int idx = ipcid_to_idx(ipcp->id);
  453. WARN_ON_ONCE(idr_remove(&ids->ipcs_idr, idx) != ipcp);
  454. ipc_kht_remove(ids, ipcp);
  455. ids->in_use--;
  456. ipcp->deleted = true;
  457. if (unlikely(idx == ids->max_idx)) {
  458. idx = ids->max_idx-1;
  459. if (idx >= 0)
  460. idx = ipc_search_maxidx(ids, idx);
  461. ids->max_idx = idx;
  462. }
  463. }
  464. /**
  465. * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
  466. * @ids: ipc identifier set
  467. * @ipcp: ipc perm structure containing the key to modify
  468. *
  469. * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
  470. * before this function is called, and remain locked on the exit.
  471. */
  472. void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  473. {
  474. ipc_kht_remove(ids, ipcp);
  475. ipcp->key = IPC_PRIVATE;
  476. }
  477. bool ipc_rcu_getref(struct kern_ipc_perm *ptr)
  478. {
  479. return refcount_inc_not_zero(&ptr->refcount);
  480. }
  481. void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  482. void (*func)(struct rcu_head *head))
  483. {
  484. if (!refcount_dec_and_test(&ptr->refcount))
  485. return;
  486. call_rcu(&ptr->rcu, func);
  487. }
  488. /**
  489. * ipcperms - check ipc permissions
  490. * @ns: ipc namespace
  491. * @ipcp: ipc permission set
  492. * @flag: desired permission set
  493. *
  494. * Check user, group, other permissions for access
  495. * to ipc resources. return 0 if allowed
  496. *
  497. * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
  498. */
  499. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  500. {
  501. kuid_t euid = current_euid();
  502. int requested_mode, granted_mode;
  503. audit_ipc_obj(ipcp);
  504. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  505. granted_mode = ipcp->mode;
  506. if (uid_eq(euid, ipcp->cuid) ||
  507. uid_eq(euid, ipcp->uid))
  508. granted_mode >>= 6;
  509. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  510. granted_mode >>= 3;
  511. /* is there some bit set in requested_mode but not in granted_mode? */
  512. if ((requested_mode & ~granted_mode & 0007) &&
  513. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  514. return -1;
  515. return security_ipc_permission(ipcp, flag);
  516. }
  517. /*
  518. * Functions to convert between the kern_ipc_perm structure and the
  519. * old/new ipc_perm structures
  520. */
  521. /**
  522. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  523. * @in: kernel permissions
  524. * @out: new style ipc permissions
  525. *
  526. * Turn the kernel object @in into a set of permissions descriptions
  527. * for returning to userspace (@out).
  528. */
  529. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
  530. {
  531. out->key = in->key;
  532. out->uid = from_kuid_munged(current_user_ns(), in->uid);
  533. out->gid = from_kgid_munged(current_user_ns(), in->gid);
  534. out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
  535. out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
  536. out->mode = in->mode;
  537. out->seq = in->seq;
  538. }
  539. /**
  540. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  541. * @in: new style ipc permissions
  542. * @out: old style ipc permissions
  543. *
  544. * Turn the new style permissions object @in into a compatibility
  545. * object and store it into the @out pointer.
  546. */
  547. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
  548. {
  549. out->key = in->key;
  550. SET_UID(out->uid, in->uid);
  551. SET_GID(out->gid, in->gid);
  552. SET_UID(out->cuid, in->cuid);
  553. SET_GID(out->cgid, in->cgid);
  554. out->mode = in->mode;
  555. out->seq = in->seq;
  556. }
  557. /**
  558. * ipc_obtain_object_idr
  559. * @ids: ipc identifier set
  560. * @id: ipc id to look for
  561. *
  562. * Look for an id in the ipc ids idr and return associated ipc object.
  563. *
  564. * Call inside the RCU critical section.
  565. * The ipc object is *not* locked on exit.
  566. */
  567. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
  568. {
  569. struct kern_ipc_perm *out;
  570. int idx = ipcid_to_idx(id);
  571. out = idr_find(&ids->ipcs_idr, idx);
  572. if (!out)
  573. return ERR_PTR(-EINVAL);
  574. return out;
  575. }
  576. /**
  577. * ipc_obtain_object_check
  578. * @ids: ipc identifier set
  579. * @id: ipc id to look for
  580. *
  581. * Similar to ipc_obtain_object_idr() but also checks the ipc object
  582. * sequence number.
  583. *
  584. * Call inside the RCU critical section.
  585. * The ipc object is *not* locked on exit.
  586. */
  587. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  588. {
  589. struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
  590. if (IS_ERR(out))
  591. goto out;
  592. if (ipc_checkid(out, id))
  593. return ERR_PTR(-EINVAL);
  594. out:
  595. return out;
  596. }
  597. /**
  598. * ipcget - Common sys_*get() code
  599. * @ns: namespace
  600. * @ids: ipc identifier set
  601. * @ops: operations to be called on ipc object creation, permission checks
  602. * and further checks
  603. * @params: the parameters needed by the previous operations.
  604. *
  605. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  606. */
  607. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  608. const struct ipc_ops *ops, struct ipc_params *params)
  609. {
  610. if (params->key == IPC_PRIVATE)
  611. return ipcget_new(ns, ids, ops, params);
  612. else
  613. return ipcget_public(ns, ids, ops, params);
  614. }
  615. /**
  616. * ipc_update_perm - update the permissions of an ipc object
  617. * @in: the permission given as input.
  618. * @out: the permission of the ipc to set.
  619. */
  620. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  621. {
  622. kuid_t uid = make_kuid(current_user_ns(), in->uid);
  623. kgid_t gid = make_kgid(current_user_ns(), in->gid);
  624. if (!uid_valid(uid) || !gid_valid(gid))
  625. return -EINVAL;
  626. out->uid = uid;
  627. out->gid = gid;
  628. out->mode = (out->mode & ~S_IRWXUGO)
  629. | (in->mode & S_IRWXUGO);
  630. return 0;
  631. }
  632. /**
  633. * ipcctl_obtain_check - retrieve an ipc object and check permissions
  634. * @ns: ipc namespace
  635. * @ids: the table of ids where to look for the ipc
  636. * @id: the id of the ipc to retrieve
  637. * @cmd: the cmd to check
  638. * @perm: the permission to set
  639. * @extra_perm: one extra permission parameter used by msq
  640. *
  641. * This function does some common audit and permissions check for some IPC_XXX
  642. * cmd and is called from semctl_down, shmctl_down and msgctl_down.
  643. *
  644. * It:
  645. * - retrieves the ipc object with the given id in the given table.
  646. * - performs some audit and permission check, depending on the given cmd
  647. * - returns a pointer to the ipc object or otherwise, the corresponding
  648. * error.
  649. *
  650. * Call holding the both the rwsem and the rcu read lock.
  651. */
  652. struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
  653. struct ipc_ids *ids, int id, int cmd,
  654. struct ipc64_perm *perm, int extra_perm)
  655. {
  656. kuid_t euid;
  657. int err = -EPERM;
  658. struct kern_ipc_perm *ipcp;
  659. ipcp = ipc_obtain_object_check(ids, id);
  660. if (IS_ERR(ipcp)) {
  661. err = PTR_ERR(ipcp);
  662. goto err;
  663. }
  664. audit_ipc_obj(ipcp);
  665. if (cmd == IPC_SET)
  666. audit_ipc_set_perm(extra_perm, perm->uid,
  667. perm->gid, perm->mode);
  668. euid = current_euid();
  669. if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
  670. ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  671. return ipcp; /* successful lookup */
  672. err:
  673. return ERR_PTR(err);
  674. }
  675. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  676. /**
  677. * ipc_parse_version - ipc call version
  678. * @cmd: pointer to command
  679. *
  680. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  681. * The @cmd value is turned from an encoding command and version into
  682. * just the command code.
  683. */
  684. int ipc_parse_version(int *cmd)
  685. {
  686. if (*cmd & IPC_64) {
  687. *cmd ^= IPC_64;
  688. return IPC_64;
  689. } else {
  690. return IPC_OLD;
  691. }
  692. }
  693. #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
  694. #ifdef CONFIG_PROC_FS
  695. struct ipc_proc_iter {
  696. struct ipc_namespace *ns;
  697. struct pid_namespace *pid_ns;
  698. struct ipc_proc_iface *iface;
  699. };
  700. struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s)
  701. {
  702. struct ipc_proc_iter *iter = s->private;
  703. return iter->pid_ns;
  704. }
  705. /**
  706. * sysvipc_find_ipc - Find and lock the ipc structure based on seq pos
  707. * @ids: ipc identifier set
  708. * @pos: expected position
  709. *
  710. * The function finds an ipc structure, based on the sequence file
  711. * position @pos. If there is no ipc structure at position @pos, then
  712. * the successor is selected.
  713. * If a structure is found, then it is locked (both rcu_read_lock() and
  714. * ipc_lock_object()) and @pos is set to the position needed to locate
  715. * the found ipc structure.
  716. * If nothing is found (i.e. EOF), @pos is not modified.
  717. *
  718. * The function returns the found ipc structure, or NULL at EOF.
  719. */
  720. static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t *pos)
  721. {
  722. int tmpidx;
  723. struct kern_ipc_perm *ipc;
  724. /* convert from position to idr index -> "-1" */
  725. tmpidx = *pos - 1;
  726. ipc = idr_get_next(&ids->ipcs_idr, &tmpidx);
  727. if (ipc != NULL) {
  728. rcu_read_lock();
  729. ipc_lock_object(ipc);
  730. /* convert from idr index to position -> "+1" */
  731. *pos = tmpidx + 1;
  732. }
  733. return ipc;
  734. }
  735. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  736. {
  737. struct ipc_proc_iter *iter = s->private;
  738. struct ipc_proc_iface *iface = iter->iface;
  739. struct kern_ipc_perm *ipc = it;
  740. /* If we had an ipc id locked before, unlock it */
  741. if (ipc && ipc != SEQ_START_TOKEN)
  742. ipc_unlock(ipc);
  743. /* Next -> search for *pos+1 */
  744. (*pos)++;
  745. return sysvipc_find_ipc(&iter->ns->ids[iface->ids], pos);
  746. }
  747. /*
  748. * File positions: pos 0 -> header, pos n -> ipc idx = n - 1.
  749. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  750. */
  751. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  752. {
  753. struct ipc_proc_iter *iter = s->private;
  754. struct ipc_proc_iface *iface = iter->iface;
  755. struct ipc_ids *ids;
  756. ids = &iter->ns->ids[iface->ids];
  757. /*
  758. * Take the lock - this will be released by the corresponding
  759. * call to stop().
  760. */
  761. down_read(&ids->rwsem);
  762. /* pos < 0 is invalid */
  763. if (*pos < 0)
  764. return NULL;
  765. /* pos == 0 means header */
  766. if (*pos == 0)
  767. return SEQ_START_TOKEN;
  768. /* Otherwise return the correct ipc structure */
  769. return sysvipc_find_ipc(ids, pos);
  770. }
  771. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  772. {
  773. struct kern_ipc_perm *ipc = it;
  774. struct ipc_proc_iter *iter = s->private;
  775. struct ipc_proc_iface *iface = iter->iface;
  776. struct ipc_ids *ids;
  777. /* If we had a locked structure, release it */
  778. if (ipc && ipc != SEQ_START_TOKEN)
  779. ipc_unlock(ipc);
  780. ids = &iter->ns->ids[iface->ids];
  781. /* Release the lock we took in start() */
  782. up_read(&ids->rwsem);
  783. }
  784. static int sysvipc_proc_show(struct seq_file *s, void *it)
  785. {
  786. struct ipc_proc_iter *iter = s->private;
  787. struct ipc_proc_iface *iface = iter->iface;
  788. if (it == SEQ_START_TOKEN) {
  789. seq_puts(s, iface->header);
  790. return 0;
  791. }
  792. return iface->show(s, it);
  793. }
  794. static const struct seq_operations sysvipc_proc_seqops = {
  795. .start = sysvipc_proc_start,
  796. .stop = sysvipc_proc_stop,
  797. .next = sysvipc_proc_next,
  798. .show = sysvipc_proc_show,
  799. };
  800. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  801. {
  802. struct ipc_proc_iter *iter;
  803. iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
  804. if (!iter)
  805. return -ENOMEM;
  806. iter->iface = pde_data(inode);
  807. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  808. iter->pid_ns = get_pid_ns(task_active_pid_ns(current));
  809. return 0;
  810. }
  811. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  812. {
  813. struct seq_file *seq = file->private_data;
  814. struct ipc_proc_iter *iter = seq->private;
  815. put_ipc_ns(iter->ns);
  816. put_pid_ns(iter->pid_ns);
  817. return seq_release_private(inode, file);
  818. }
  819. static const struct proc_ops sysvipc_proc_ops = {
  820. .proc_flags = PROC_ENTRY_PERMANENT,
  821. .proc_open = sysvipc_proc_open,
  822. .proc_read = seq_read,
  823. .proc_lseek = seq_lseek,
  824. .proc_release = sysvipc_proc_release,
  825. };
  826. #endif /* CONFIG_PROC_FS */