util.c 22 KB

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