util.c 23 KB

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