util.c 23 KB

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