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