util.c 22 KB

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