util.c 22 KB

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