util.c 23 KB

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