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. {
  110. int seq_limit = INT_MAX/SEQ_MULTIPLIER;
  111. if (seq_limit > USHRT_MAX)
  112. ids->seq_max = USHRT_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. * @ns: IPC namespace
  273. * @ipcp: ipc permission set
  274. * @ops: the actual security routine to call
  275. * @params: its parameters
  276. *
  277. * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
  278. * when the key is not IPC_PRIVATE and that key already exists in the
  279. * ids IDR.
  280. *
  281. * On success, the IPC id is returned.
  282. *
  283. * It is called with ipc_ids.rw_mutex and ipcp->lock held.
  284. */
  285. static int ipc_check_perms(struct ipc_namespace *ns,
  286. struct kern_ipc_perm *ipcp,
  287. struct ipc_ops *ops,
  288. struct ipc_params *params)
  289. {
  290. int err;
  291. if (ipcperms(ns, ipcp, params->flg))
  292. err = -EACCES;
  293. else {
  294. err = ops->associate(ipcp, params->flg);
  295. if (!err)
  296. err = ipcp->id;
  297. }
  298. return err;
  299. }
  300. /**
  301. * ipcget_public - get an ipc object or create a new one
  302. * @ns: namespace
  303. * @ids: IPC identifer set
  304. * @ops: the actual creation routine to call
  305. * @params: its parameters
  306. *
  307. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  308. * when the key is not IPC_PRIVATE.
  309. * It adds a new entry if the key is not found and does some permission
  310. * / security checkings if the key is found.
  311. *
  312. * On success, the ipc id is returned.
  313. */
  314. static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
  315. struct ipc_ops *ops, struct ipc_params *params)
  316. {
  317. struct kern_ipc_perm *ipcp;
  318. int flg = params->flg;
  319. int err;
  320. retry:
  321. err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
  322. /*
  323. * Take the lock as a writer since we are potentially going to add
  324. * a new entry + read locks are not "upgradable"
  325. */
  326. down_write(&ids->rw_mutex);
  327. ipcp = ipc_findkey(ids, params->key);
  328. if (ipcp == NULL) {
  329. /* key not used */
  330. if (!(flg & IPC_CREAT))
  331. err = -ENOENT;
  332. else if (!err)
  333. err = -ENOMEM;
  334. else
  335. err = ops->getnew(ns, params);
  336. } else {
  337. /* ipc object has been locked by ipc_findkey() */
  338. if (flg & IPC_CREAT && flg & IPC_EXCL)
  339. err = -EEXIST;
  340. else {
  341. err = 0;
  342. if (ops->more_checks)
  343. err = ops->more_checks(ipcp, params);
  344. if (!err)
  345. /*
  346. * ipc_check_perms returns the IPC id on
  347. * success
  348. */
  349. err = ipc_check_perms(ns, ipcp, ops, params);
  350. }
  351. ipc_unlock(ipcp);
  352. }
  353. up_write(&ids->rw_mutex);
  354. if (err == -EAGAIN)
  355. goto retry;
  356. return err;
  357. }
  358. /**
  359. * ipc_rmid - remove an IPC identifier
  360. * @ids: IPC identifier set
  361. * @ipcp: ipc perm structure containing the identifier to remove
  362. *
  363. * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
  364. * before this function is called, and remain locked on the exit.
  365. */
  366. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  367. {
  368. int lid = ipcid_to_idx(ipcp->id);
  369. idr_remove(&ids->ipcs_idr, lid);
  370. ids->in_use--;
  371. ipcp->deleted = 1;
  372. return;
  373. }
  374. /**
  375. * ipc_alloc - allocate ipc space
  376. * @size: size desired
  377. *
  378. * Allocate memory from the appropriate pools and return a pointer to it.
  379. * NULL is returned if the allocation fails
  380. */
  381. void* ipc_alloc(int size)
  382. {
  383. void* out;
  384. if(size > PAGE_SIZE)
  385. out = vmalloc(size);
  386. else
  387. out = kmalloc(size, GFP_KERNEL);
  388. return out;
  389. }
  390. /**
  391. * ipc_free - free ipc space
  392. * @ptr: pointer returned by ipc_alloc
  393. * @size: size of block
  394. *
  395. * Free a block created with ipc_alloc(). The caller must know the size
  396. * used in the allocation call.
  397. */
  398. void ipc_free(void* ptr, int size)
  399. {
  400. if(size > PAGE_SIZE)
  401. vfree(ptr);
  402. else
  403. kfree(ptr);
  404. }
  405. /*
  406. * rcu allocations:
  407. * There are three headers that are prepended to the actual allocation:
  408. * - during use: ipc_rcu_hdr.
  409. * - during the rcu grace period: ipc_rcu_grace.
  410. * - [only if vmalloc]: ipc_rcu_sched.
  411. * Their lifetime doesn't overlap, thus the headers share the same memory.
  412. * Unlike a normal union, they are right-aligned, thus some container_of
  413. * forward/backward casting is necessary:
  414. */
  415. struct ipc_rcu_hdr
  416. {
  417. int refcount;
  418. int is_vmalloc;
  419. void *data[0];
  420. };
  421. struct ipc_rcu_grace
  422. {
  423. struct rcu_head rcu;
  424. /* "void *" makes sure alignment of following data is sane. */
  425. void *data[0];
  426. };
  427. struct ipc_rcu_sched
  428. {
  429. struct work_struct work;
  430. /* "void *" makes sure alignment of following data is sane. */
  431. void *data[0];
  432. };
  433. #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
  434. sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
  435. #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
  436. sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
  437. static inline int rcu_use_vmalloc(int size)
  438. {
  439. /* Too big for a single page? */
  440. if (HDRLEN_KMALLOC + size > PAGE_SIZE)
  441. return 1;
  442. return 0;
  443. }
  444. /**
  445. * ipc_rcu_alloc - allocate ipc and rcu space
  446. * @size: size desired
  447. *
  448. * Allocate memory for the rcu header structure + the object.
  449. * Returns the pointer to the object.
  450. * NULL is returned if the allocation fails.
  451. */
  452. void* ipc_rcu_alloc(int size)
  453. {
  454. void* out;
  455. /*
  456. * We prepend the allocation with the rcu struct, and
  457. * workqueue if necessary (for vmalloc).
  458. */
  459. if (rcu_use_vmalloc(size)) {
  460. out = vmalloc(HDRLEN_VMALLOC + size);
  461. if (out) {
  462. out += HDRLEN_VMALLOC;
  463. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
  464. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  465. }
  466. } else {
  467. out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
  468. if (out) {
  469. out += HDRLEN_KMALLOC;
  470. container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
  471. container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
  472. }
  473. }
  474. return out;
  475. }
  476. void ipc_rcu_getref(void *ptr)
  477. {
  478. container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
  479. }
  480. static void ipc_do_vfree(struct work_struct *work)
  481. {
  482. vfree(container_of(work, struct ipc_rcu_sched, work));
  483. }
  484. /**
  485. * ipc_schedule_free - free ipc + rcu space
  486. * @head: RCU callback structure for queued work
  487. *
  488. * Since RCU callback function is called in bh,
  489. * we need to defer the vfree to schedule_work().
  490. */
  491. static void ipc_schedule_free(struct rcu_head *head)
  492. {
  493. struct ipc_rcu_grace *grace;
  494. struct ipc_rcu_sched *sched;
  495. grace = container_of(head, struct ipc_rcu_grace, rcu);
  496. sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
  497. data[0]);
  498. INIT_WORK(&sched->work, ipc_do_vfree);
  499. schedule_work(&sched->work);
  500. }
  501. /**
  502. * ipc_immediate_free - free ipc + rcu space
  503. * @head: RCU callback structure that contains pointer to be freed
  504. *
  505. * Free from the RCU callback context.
  506. */
  507. static void ipc_immediate_free(struct rcu_head *head)
  508. {
  509. struct ipc_rcu_grace *free =
  510. container_of(head, struct ipc_rcu_grace, rcu);
  511. kfree(free);
  512. }
  513. void ipc_rcu_putref(void *ptr)
  514. {
  515. if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
  516. return;
  517. if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
  518. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  519. ipc_schedule_free);
  520. } else {
  521. call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
  522. ipc_immediate_free);
  523. }
  524. }
  525. /**
  526. * ipcperms - check IPC permissions
  527. * @ns: IPC namespace
  528. * @ipcp: IPC permission set
  529. * @flag: desired permission set.
  530. *
  531. * Check user, group, other permissions for access
  532. * to ipc resources. return 0 if allowed
  533. *
  534. * @flag will most probably be 0 or S_...UGO from <linux/stat.h>
  535. */
  536. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  537. {
  538. uid_t euid = current_euid();
  539. int requested_mode, granted_mode;
  540. audit_ipc_obj(ipcp);
  541. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  542. granted_mode = ipcp->mode;
  543. if (euid == ipcp->cuid ||
  544. euid == ipcp->uid)
  545. granted_mode >>= 6;
  546. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  547. granted_mode >>= 3;
  548. /* is there some bit set in requested_mode but not in granted_mode? */
  549. if ((requested_mode & ~granted_mode & 0007) &&
  550. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  551. return -1;
  552. return security_ipc_permission(ipcp, flag);
  553. }
  554. /*
  555. * Functions to convert between the kern_ipc_perm structure and the
  556. * old/new ipc_perm structures
  557. */
  558. /**
  559. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  560. * @in: kernel permissions
  561. * @out: new style IPC permissions
  562. *
  563. * Turn the kernel object @in into a set of permissions descriptions
  564. * for returning to userspace (@out).
  565. */
  566. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  567. {
  568. out->key = in->key;
  569. out->uid = in->uid;
  570. out->gid = in->gid;
  571. out->cuid = in->cuid;
  572. out->cgid = in->cgid;
  573. out->mode = in->mode;
  574. out->seq = in->seq;
  575. }
  576. /**
  577. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  578. * @in: new style IPC permissions
  579. * @out: old style IPC permissions
  580. *
  581. * Turn the new style permissions object @in into a compatibility
  582. * object and store it into the @out pointer.
  583. */
  584. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  585. {
  586. out->key = in->key;
  587. SET_UID(out->uid, in->uid);
  588. SET_GID(out->gid, in->gid);
  589. SET_UID(out->cuid, in->cuid);
  590. SET_GID(out->cgid, in->cgid);
  591. out->mode = in->mode;
  592. out->seq = in->seq;
  593. }
  594. /**
  595. * ipc_lock - Lock an ipc structure without rw_mutex held
  596. * @ids: IPC identifier set
  597. * @id: ipc id to look for
  598. *
  599. * Look for an id in the ipc ids idr and lock the associated ipc object.
  600. *
  601. * The ipc object is locked on exit.
  602. */
  603. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  604. {
  605. struct kern_ipc_perm *out;
  606. int lid = ipcid_to_idx(id);
  607. rcu_read_lock();
  608. out = idr_find(&ids->ipcs_idr, lid);
  609. if (out == NULL) {
  610. rcu_read_unlock();
  611. return ERR_PTR(-EINVAL);
  612. }
  613. spin_lock(&out->lock);
  614. /* ipc_rmid() may have already freed the ID while ipc_lock
  615. * was spinning: here verify that the structure is still valid
  616. */
  617. if (out->deleted) {
  618. spin_unlock(&out->lock);
  619. rcu_read_unlock();
  620. return ERR_PTR(-EINVAL);
  621. }
  622. return out;
  623. }
  624. struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
  625. {
  626. struct kern_ipc_perm *out;
  627. out = ipc_lock(ids, id);
  628. if (IS_ERR(out))
  629. return out;
  630. if (ipc_checkid(out, id)) {
  631. ipc_unlock(out);
  632. return ERR_PTR(-EIDRM);
  633. }
  634. return out;
  635. }
  636. /**
  637. * ipcget - Common sys_*get() code
  638. * @ns : namsepace
  639. * @ids : IPC identifier set
  640. * @ops : operations to be called on ipc object creation, permission checks
  641. * and further checks
  642. * @params : the parameters needed by the previous operations.
  643. *
  644. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  645. */
  646. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  647. struct ipc_ops *ops, struct ipc_params *params)
  648. {
  649. if (params->key == IPC_PRIVATE)
  650. return ipcget_new(ns, ids, ops, params);
  651. else
  652. return ipcget_public(ns, ids, ops, params);
  653. }
  654. /**
  655. * ipc_update_perm - update the permissions of an IPC.
  656. * @in: the permission given as input.
  657. * @out: the permission of the ipc to set.
  658. */
  659. void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  660. {
  661. out->uid = in->uid;
  662. out->gid = in->gid;
  663. out->mode = (out->mode & ~S_IRWXUGO)
  664. | (in->mode & S_IRWXUGO);
  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. uid_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 (euid == ipcp->cuid || 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 __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 /* __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 */