util.c 24 KB

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