util.c 23 KB

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