util.c 22 KB

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