util.c 23 KB

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