util.c 22 KB

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