util.c 23 KB

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