util.c 23 KB

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