util.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/notifier.h>
  26. #include <linux/capability.h>
  27. #include <linux/highuid.h>
  28. #include <linux/security.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/audit.h>
  34. #include <linux/nsproxy.h>
  35. #include <linux/rwsem.h>
  36. #include <linux/memory.h>
  37. #include <linux/ipc_namespace.h>
  38. #include <asm/unistd.h>
  39. #include "util.h"
  40. struct ipc_proc_iface {
  41. const char *path;
  42. const char *header;
  43. int ids;
  44. int (*show)(struct seq_file *, void *);
  45. };
  46. static void ipc_memory_notifier(struct work_struct *work)
  47. {
  48. ipcns_notify(IPCNS_MEMCHANGED);
  49. }
  50. static int ipc_memory_callback(struct notifier_block *self,
  51. unsigned long action, void *arg)
  52. {
  53. static DECLARE_WORK(ipc_memory_wq, ipc_memory_notifier);
  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. static struct notifier_block ipc_memory_nb = {
  78. .notifier_call = ipc_memory_callback,
  79. .priority = IPC_CALLBACK_PRI,
  80. };
  81. /**
  82. * ipc_init - initialise IPC subsystem
  83. *
  84. * The various system5 IPC resources (semaphores, messages and shared
  85. * memory) are initialised
  86. * A callback routine is registered into the memory hotplug notifier
  87. * chain: since msgmni scales to lowmem this callback routine will be
  88. * called upon successful memory add / remove to recompute msmgni.
  89. */
  90. static int __init ipc_init(void)
  91. {
  92. sem_init();
  93. msg_init();
  94. shm_init();
  95. register_hotmemory_notifier(&ipc_memory_nb);
  96. register_ipcns_notifier(&init_ipc_ns);
  97. return 0;
  98. }
  99. __initcall(ipc_init);
  100. /**
  101. * ipc_init_ids - initialise IPC identifiers
  102. * @ids: Identifier set
  103. *
  104. * Set up the sequence range to use for the ipc identifier range (limited
  105. * below IPCMNI) then initialise the ids idr.
  106. */
  107. void ipc_init_ids(struct ipc_ids *ids)
  108. {
  109. init_rwsem(&ids->rw_mutex);
  110. ids->in_use = 0;
  111. ids->seq = 0;
  112. ids->next_id = -1;
  113. {
  114. int seq_limit = INT_MAX/SEQ_MULTIPLIER;
  115. if (seq_limit > USHRT_MAX)
  116. ids->seq_max = USHRT_MAX;
  117. else
  118. ids->seq_max = seq_limit;
  119. }
  120. idr_init(&ids->ipcs_idr);
  121. }
  122. #ifdef CONFIG_PROC_FS
  123. static const struct file_operations sysvipc_proc_fops;
  124. /**
  125. * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
  126. * @path: Path in procfs
  127. * @header: Banner to be printed at the beginning of the file.
  128. * @ids: ipc id table to iterate.
  129. * @show: show routine.
  130. */
  131. void __init ipc_init_proc_interface(const char *path, const char *header,
  132. int ids, int (*show)(struct seq_file *, void *))
  133. {
  134. struct proc_dir_entry *pde;
  135. struct ipc_proc_iface *iface;
  136. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  137. if (!iface)
  138. return;
  139. iface->path = path;
  140. iface->header = header;
  141. iface->ids = ids;
  142. iface->show = show;
  143. pde = proc_create_data(path,
  144. S_IRUGO, /* world readable */
  145. NULL, /* parent dir */
  146. &sysvipc_proc_fops,
  147. iface);
  148. if (!pde) {
  149. kfree(iface);
  150. }
  151. }
  152. #endif
  153. /**
  154. * ipc_findkey - find a key in an ipc identifier set
  155. * @ids: Identifier set
  156. * @key: The key to find
  157. *
  158. * Requires ipc_ids.rw_mutex locked.
  159. * Returns the LOCKED pointer to the ipc structure if found or NULL
  160. * if not.
  161. * If key is found ipc points to the owning ipc structure
  162. */
  163. static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
  164. {
  165. struct kern_ipc_perm *ipc;
  166. int next_id;
  167. int total;
  168. for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
  169. ipc = idr_find(&ids->ipcs_idr, next_id);
  170. if (ipc == NULL)
  171. continue;
  172. if (ipc->key != key) {
  173. total++;
  174. continue;
  175. }
  176. ipc_lock_by_ptr(ipc);
  177. return ipc;
  178. }
  179. return NULL;
  180. }
  181. /**
  182. * ipc_get_maxid - get the last assigned id
  183. * @ids: IPC identifier set
  184. *
  185. * Called with ipc_ids.rw_mutex held.
  186. */
  187. int ipc_get_maxid(struct ipc_ids *ids)
  188. {
  189. struct kern_ipc_perm *ipc;
  190. int max_id = -1;
  191. int total, id;
  192. if (ids->in_use == 0)
  193. return -1;
  194. if (ids->in_use == IPCMNI)
  195. return IPCMNI - 1;
  196. /* Look for the last assigned id */
  197. total = 0;
  198. for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
  199. ipc = idr_find(&ids->ipcs_idr, id);
  200. if (ipc != NULL) {
  201. max_id = id;
  202. total++;
  203. }
  204. }
  205. return max_id;
  206. }
  207. /**
  208. * ipc_addid - add an IPC identifier
  209. * @ids: IPC identifier set
  210. * @new: new IPC permission set
  211. * @size: limit for the number of used ids
  212. *
  213. * Add an entry 'new' to the IPC ids idr. The permissions object is
  214. * initialised and the first free entry is set up and the id assigned
  215. * is returned. The 'new' entry is returned in a locked state on success.
  216. * On failure the entry is not locked and a negative err-code is returned.
  217. *
  218. * Called with ipc_ids.rw_mutex held as a writer.
  219. */
  220. int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
  221. {
  222. kuid_t euid;
  223. kgid_t egid;
  224. int id;
  225. int next_id = ids->next_id;
  226. if (size > IPCMNI)
  227. size = IPCMNI;
  228. if (ids->in_use >= size)
  229. return -ENOSPC;
  230. idr_preload(GFP_KERNEL);
  231. spin_lock_init(&new->lock);
  232. new->deleted = 0;
  233. rcu_read_lock();
  234. spin_lock(&new->lock);
  235. id = idr_alloc(&ids->ipcs_idr, new,
  236. (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
  237. GFP_NOWAIT);
  238. idr_preload_end();
  239. if (id < 0) {
  240. spin_unlock(&new->lock);
  241. rcu_read_unlock();
  242. return id;
  243. }
  244. ids->in_use++;
  245. current_euid_egid(&euid, &egid);
  246. new->cuid = new->uid = euid;
  247. new->gid = new->cgid = egid;
  248. if (next_id < 0) {
  249. new->seq = ids->seq++;
  250. if (ids->seq > ids->seq_max)
  251. ids->seq = 0;
  252. } else {
  253. new->seq = ipcid_to_seqx(next_id);
  254. ids->next_id = -1;
  255. }
  256. new->id = ipc_buildid(id, new->seq);
  257. return id;
  258. }
  259. /**
  260. * ipcget_new - create a new ipc object
  261. * @ns: namespace
  262. * @ids: IPC identifer set
  263. * @ops: the actual creation routine to call
  264. * @params: its parameters
  265. *
  266. * This routine is called by sys_msgget, sys_semget() and sys_shmget()
  267. * when the key is IPC_PRIVATE.
  268. */
  269. static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
  270. struct ipc_ops *ops, struct ipc_params *params)
  271. {
  272. int err;
  273. down_write(&ids->rw_mutex);
  274. err = ops->getnew(ns, params);
  275. up_write(&ids->rw_mutex);
  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. /*
  329. * Take the lock as a writer since we are potentially going to add
  330. * a new entry + read locks are not "upgradable"
  331. */
  332. down_write(&ids->rw_mutex);
  333. ipcp = ipc_findkey(ids, params->key);
  334. if (ipcp == NULL) {
  335. /* key not used */
  336. if (!(flg & IPC_CREAT))
  337. err = -ENOENT;
  338. else
  339. err = ops->getnew(ns, params);
  340. } else {
  341. /* ipc object has been locked by ipc_findkey() */
  342. if (flg & IPC_CREAT && flg & IPC_EXCL)
  343. err = -EEXIST;
  344. else {
  345. err = 0;
  346. if (ops->more_checks)
  347. err = ops->more_checks(ipcp, params);
  348. if (!err)
  349. /*
  350. * ipc_check_perms returns the IPC id on
  351. * success
  352. */
  353. err = ipc_check_perms(ns, ipcp, ops, params);
  354. }
  355. ipc_unlock(ipcp);
  356. }
  357. up_write(&ids->rw_mutex);
  358. return err;
  359. }
  360. /**
  361. * ipc_rmid - remove an IPC identifier
  362. * @ids: IPC identifier set
  363. * @ipcp: ipc perm structure containing the identifier to remove
  364. *
  365. * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
  366. * before this function is called, and remain locked on the exit.
  367. */
  368. void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  369. {
  370. int lid = ipcid_to_idx(ipcp->id);
  371. idr_remove(&ids->ipcs_idr, lid);
  372. ids->in_use--;
  373. ipcp->deleted = 1;
  374. return;
  375. }
  376. /**
  377. * ipc_alloc - allocate ipc space
  378. * @size: size desired
  379. *
  380. * Allocate memory from the appropriate pools and return a pointer to it.
  381. * NULL is returned if the allocation fails
  382. */
  383. void *ipc_alloc(int size)
  384. {
  385. void *out;
  386. if(size > PAGE_SIZE)
  387. out = vmalloc(size);
  388. else
  389. out = kmalloc(size, GFP_KERNEL);
  390. return out;
  391. }
  392. /**
  393. * ipc_free - free ipc space
  394. * @ptr: pointer returned by ipc_alloc
  395. * @size: size of block
  396. *
  397. * Free a block created with ipc_alloc(). The caller must know the size
  398. * used in the allocation call.
  399. */
  400. void ipc_free(void* ptr, int size)
  401. {
  402. if(size > PAGE_SIZE)
  403. vfree(ptr);
  404. else
  405. kfree(ptr);
  406. }
  407. struct ipc_rcu {
  408. struct rcu_head rcu;
  409. atomic_t refcount;
  410. /* "void *" makes sure alignment of following data is sane. */
  411. void *data[0];
  412. };
  413. /**
  414. * ipc_rcu_alloc - allocate ipc and rcu space
  415. * @size: size desired
  416. *
  417. * Allocate memory for the rcu header structure + the object.
  418. * Returns the pointer to the object or NULL upon failure.
  419. */
  420. void *ipc_rcu_alloc(int size)
  421. {
  422. /*
  423. * We prepend the allocation with the rcu struct
  424. */
  425. struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size);
  426. if (unlikely(!out))
  427. return NULL;
  428. atomic_set(&out->refcount, 1);
  429. return out->data;
  430. }
  431. int ipc_rcu_getref(void *ptr)
  432. {
  433. return atomic_inc_not_zero(&container_of(ptr, struct ipc_rcu, data)->refcount);
  434. }
  435. /**
  436. * ipc_schedule_free - free ipc + rcu space
  437. * @head: RCU callback structure for queued work
  438. */
  439. static void ipc_schedule_free(struct rcu_head *head)
  440. {
  441. vfree(container_of(head, struct ipc_rcu, rcu));
  442. }
  443. void ipc_rcu_putref(void *ptr)
  444. {
  445. struct ipc_rcu *p = container_of(ptr, struct ipc_rcu, data);
  446. if (!atomic_dec_and_test(&p->refcount))
  447. return;
  448. if (is_vmalloc_addr(ptr)) {
  449. call_rcu(&p->rcu, ipc_schedule_free);
  450. } else {
  451. kfree_rcu(p, rcu);
  452. }
  453. }
  454. /**
  455. * ipcperms - check IPC permissions
  456. * @ns: IPC namespace
  457. * @ipcp: IPC permission set
  458. * @flag: desired permission set.
  459. *
  460. * Check user, group, other permissions for access
  461. * to ipc resources. return 0 if allowed
  462. *
  463. * @flag will most probably be 0 or S_...UGO from <linux/stat.h>
  464. */
  465. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  466. {
  467. kuid_t euid = current_euid();
  468. int requested_mode, granted_mode;
  469. audit_ipc_obj(ipcp);
  470. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  471. granted_mode = ipcp->mode;
  472. if (uid_eq(euid, ipcp->cuid) ||
  473. uid_eq(euid, ipcp->uid))
  474. granted_mode >>= 6;
  475. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  476. granted_mode >>= 3;
  477. /* is there some bit set in requested_mode but not in granted_mode? */
  478. if ((requested_mode & ~granted_mode & 0007) &&
  479. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  480. return -1;
  481. return security_ipc_permission(ipcp, flag);
  482. }
  483. /*
  484. * Functions to convert between the kern_ipc_perm structure and the
  485. * old/new ipc_perm structures
  486. */
  487. /**
  488. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  489. * @in: kernel permissions
  490. * @out: new style IPC permissions
  491. *
  492. * Turn the kernel object @in into a set of permissions descriptions
  493. * for returning to userspace (@out).
  494. */
  495. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  496. {
  497. out->key = in->key;
  498. out->uid = from_kuid_munged(current_user_ns(), in->uid);
  499. out->gid = from_kgid_munged(current_user_ns(), in->gid);
  500. out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
  501. out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
  502. out->mode = in->mode;
  503. out->seq = in->seq;
  504. }
  505. /**
  506. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  507. * @in: new style IPC permissions
  508. * @out: old style IPC permissions
  509. *
  510. * Turn the new style permissions object @in into a compatibility
  511. * object and store it into the @out pointer.
  512. */
  513. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  514. {
  515. out->key = in->key;
  516. SET_UID(out->uid, in->uid);
  517. SET_GID(out->gid, in->gid);
  518. SET_UID(out->cuid, in->cuid);
  519. SET_GID(out->cgid, in->cgid);
  520. out->mode = in->mode;
  521. out->seq = in->seq;
  522. }
  523. /**
  524. * ipc_obtain_object
  525. * @ids: ipc identifier set
  526. * @id: ipc id to look for
  527. *
  528. * Look for an id in the ipc ids idr and return associated ipc object.
  529. *
  530. * Call inside the RCU critical section.
  531. * The ipc object is *not* locked on exit.
  532. */
  533. struct kern_ipc_perm *ipc_obtain_object(struct ipc_ids *ids, int id)
  534. {
  535. struct kern_ipc_perm *out;
  536. int lid = ipcid_to_idx(id);
  537. out = idr_find(&ids->ipcs_idr, lid);
  538. if (!out)
  539. return ERR_PTR(-EINVAL);
  540. return out;
  541. }
  542. /**
  543. * ipc_lock - Lock an ipc structure without rw_mutex held
  544. * @ids: IPC identifier set
  545. * @id: ipc id to look for
  546. *
  547. * Look for an id in the ipc ids idr and lock the associated ipc object.
  548. *
  549. * The ipc object is locked on successful exit.
  550. */
  551. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  552. {
  553. struct kern_ipc_perm *out;
  554. rcu_read_lock();
  555. out = ipc_obtain_object(ids, id);
  556. if (IS_ERR(out))
  557. goto err1;
  558. spin_lock(&out->lock);
  559. /* ipc_rmid() may have already freed the ID while ipc_lock
  560. * was spinning: here verify that the structure is still valid
  561. */
  562. if (!out->deleted)
  563. return out;
  564. spin_unlock(&out->lock);
  565. out = ERR_PTR(-EINVAL);
  566. err1:
  567. rcu_read_unlock();
  568. return out;
  569. }
  570. /**
  571. * ipc_obtain_object_check
  572. * @ids: ipc identifier set
  573. * @id: ipc id to look for
  574. *
  575. * Similar to ipc_obtain_object() but also checks
  576. * the ipc object reference counter.
  577. *
  578. * Call inside the RCU critical section.
  579. * The ipc object is *not* locked on exit.
  580. */
  581. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  582. {
  583. struct kern_ipc_perm *out = ipc_obtain_object(ids, id);
  584. if (IS_ERR(out))
  585. goto out;
  586. if (ipc_checkid(out, id))
  587. return ERR_PTR(-EIDRM);
  588. out:
  589. return out;
  590. }
  591. struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
  592. {
  593. struct kern_ipc_perm *out;
  594. out = ipc_lock(ids, id);
  595. if (IS_ERR(out))
  596. return out;
  597. if (ipc_checkid(out, id)) {
  598. ipc_unlock(out);
  599. return ERR_PTR(-EIDRM);
  600. }
  601. return out;
  602. }
  603. /**
  604. * ipcget - Common sys_*get() code
  605. * @ns : namsepace
  606. * @ids : IPC identifier set
  607. * @ops : operations to be called on ipc object creation, permission checks
  608. * and further checks
  609. * @params : the parameters needed by the previous operations.
  610. *
  611. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  612. */
  613. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  614. struct ipc_ops *ops, struct ipc_params *params)
  615. {
  616. if (params->key == IPC_PRIVATE)
  617. return ipcget_new(ns, ids, ops, params);
  618. else
  619. return ipcget_public(ns, ids, ops, params);
  620. }
  621. /**
  622. * ipc_update_perm - update the permissions of an IPC.
  623. * @in: the permission given as input.
  624. * @out: the permission of the ipc to set.
  625. */
  626. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  627. {
  628. kuid_t uid = make_kuid(current_user_ns(), in->uid);
  629. kgid_t gid = make_kgid(current_user_ns(), in->gid);
  630. if (!uid_valid(uid) || !gid_valid(gid))
  631. return -EINVAL;
  632. out->uid = uid;
  633. out->gid = gid;
  634. out->mode = (out->mode & ~S_IRWXUGO)
  635. | (in->mode & S_IRWXUGO);
  636. return 0;
  637. }
  638. /**
  639. * ipcctl_pre_down - retrieve an ipc and check permissions for some IPC_XXX cmd
  640. * @ns: the ipc namespace
  641. * @ids: the table of ids where to look for the ipc
  642. * @id: the id of the ipc to retrieve
  643. * @cmd: the cmd to check
  644. * @perm: the permission to set
  645. * @extra_perm: one extra permission parameter used by msq
  646. *
  647. * This function does some common audit and permissions check for some IPC_XXX
  648. * cmd and is called from semctl_down, shmctl_down and msgctl_down.
  649. * It must be called without any lock held and
  650. * - retrieves the ipc with the given id in the given table.
  651. * - performs some audit and permission check, depending on the given cmd
  652. * - returns the ipc with both ipc and rw_mutex locks held in case of success
  653. * or an err-code without any lock held otherwise.
  654. */
  655. struct kern_ipc_perm *ipcctl_pre_down(struct ipc_namespace *ns,
  656. struct ipc_ids *ids, int id, int cmd,
  657. struct ipc64_perm *perm, int extra_perm)
  658. {
  659. struct kern_ipc_perm *ipcp;
  660. ipcp = ipcctl_pre_down_nolock(ns, ids, id, cmd, perm, extra_perm);
  661. if (IS_ERR(ipcp))
  662. goto out;
  663. spin_lock(&ipcp->lock);
  664. out:
  665. return ipcp;
  666. }
  667. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  668. struct ipc_ids *ids, int id, int cmd,
  669. struct ipc64_perm *perm, int extra_perm)
  670. {
  671. kuid_t euid;
  672. int err = -EPERM;
  673. struct kern_ipc_perm *ipcp;
  674. down_write(&ids->rw_mutex);
  675. rcu_read_lock();
  676. ipcp = ipc_obtain_object_check(ids, id);
  677. if (IS_ERR(ipcp)) {
  678. err = PTR_ERR(ipcp);
  679. goto out_up;
  680. }
  681. audit_ipc_obj(ipcp);
  682. if (cmd == IPC_SET)
  683. audit_ipc_set_perm(extra_perm, perm->uid,
  684. perm->gid, perm->mode);
  685. euid = current_euid();
  686. if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
  687. ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  688. return ipcp;
  689. out_up:
  690. /*
  691. * Unsuccessful lookup, unlock and return
  692. * the corresponding error.
  693. */
  694. rcu_read_unlock();
  695. up_write(&ids->rw_mutex);
  696. return ERR_PTR(err);
  697. }
  698. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  699. /**
  700. * ipc_parse_version - IPC call version
  701. * @cmd: pointer to command
  702. *
  703. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  704. * The @cmd value is turned from an encoding command and version into
  705. * just the command code.
  706. */
  707. int ipc_parse_version (int *cmd)
  708. {
  709. if (*cmd & IPC_64) {
  710. *cmd ^= IPC_64;
  711. return IPC_64;
  712. } else {
  713. return IPC_OLD;
  714. }
  715. }
  716. #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
  717. #ifdef CONFIG_PROC_FS
  718. struct ipc_proc_iter {
  719. struct ipc_namespace *ns;
  720. struct ipc_proc_iface *iface;
  721. };
  722. /*
  723. * This routine locks the ipc structure found at least at position pos.
  724. */
  725. static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  726. loff_t *new_pos)
  727. {
  728. struct kern_ipc_perm *ipc;
  729. int total, id;
  730. total = 0;
  731. for (id = 0; id < pos && total < ids->in_use; id++) {
  732. ipc = idr_find(&ids->ipcs_idr, id);
  733. if (ipc != NULL)
  734. total++;
  735. }
  736. if (total >= ids->in_use)
  737. return NULL;
  738. for ( ; pos < IPCMNI; pos++) {
  739. ipc = idr_find(&ids->ipcs_idr, pos);
  740. if (ipc != NULL) {
  741. *new_pos = pos + 1;
  742. ipc_lock_by_ptr(ipc);
  743. return ipc;
  744. }
  745. }
  746. /* Out of range - return NULL to terminate iteration */
  747. return NULL;
  748. }
  749. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  750. {
  751. struct ipc_proc_iter *iter = s->private;
  752. struct ipc_proc_iface *iface = iter->iface;
  753. struct kern_ipc_perm *ipc = it;
  754. /* If we had an ipc id locked before, unlock it */
  755. if (ipc && ipc != SEQ_START_TOKEN)
  756. ipc_unlock(ipc);
  757. return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
  758. }
  759. /*
  760. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  761. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  762. */
  763. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  764. {
  765. struct ipc_proc_iter *iter = s->private;
  766. struct ipc_proc_iface *iface = iter->iface;
  767. struct ipc_ids *ids;
  768. ids = &iter->ns->ids[iface->ids];
  769. /*
  770. * Take the lock - this will be released by the corresponding
  771. * call to stop().
  772. */
  773. down_read(&ids->rw_mutex);
  774. /* pos < 0 is invalid */
  775. if (*pos < 0)
  776. return NULL;
  777. /* pos == 0 means header */
  778. if (*pos == 0)
  779. return SEQ_START_TOKEN;
  780. /* Find the (pos-1)th ipc */
  781. return sysvipc_find_ipc(ids, *pos - 1, pos);
  782. }
  783. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  784. {
  785. struct kern_ipc_perm *ipc = it;
  786. struct ipc_proc_iter *iter = s->private;
  787. struct ipc_proc_iface *iface = iter->iface;
  788. struct ipc_ids *ids;
  789. /* If we had a locked structure, release it */
  790. if (ipc && ipc != SEQ_START_TOKEN)
  791. ipc_unlock(ipc);
  792. ids = &iter->ns->ids[iface->ids];
  793. /* Release the lock we took in start() */
  794. up_read(&ids->rw_mutex);
  795. }
  796. static int sysvipc_proc_show(struct seq_file *s, void *it)
  797. {
  798. struct ipc_proc_iter *iter = s->private;
  799. struct ipc_proc_iface *iface = iter->iface;
  800. if (it == SEQ_START_TOKEN)
  801. return seq_puts(s, iface->header);
  802. return iface->show(s, it);
  803. }
  804. static const struct seq_operations sysvipc_proc_seqops = {
  805. .start = sysvipc_proc_start,
  806. .stop = sysvipc_proc_stop,
  807. .next = sysvipc_proc_next,
  808. .show = sysvipc_proc_show,
  809. };
  810. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  811. {
  812. int ret;
  813. struct seq_file *seq;
  814. struct ipc_proc_iter *iter;
  815. ret = -ENOMEM;
  816. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  817. if (!iter)
  818. goto out;
  819. ret = seq_open(file, &sysvipc_proc_seqops);
  820. if (ret)
  821. goto out_kfree;
  822. seq = file->private_data;
  823. seq->private = iter;
  824. iter->iface = PDE_DATA(inode);
  825. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  826. out:
  827. return ret;
  828. out_kfree:
  829. kfree(iter);
  830. goto out;
  831. }
  832. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  833. {
  834. struct seq_file *seq = file->private_data;
  835. struct ipc_proc_iter *iter = seq->private;
  836. put_ipc_ns(iter->ns);
  837. return seq_release_private(inode, file);
  838. }
  839. static const struct file_operations sysvipc_proc_fops = {
  840. .open = sysvipc_proc_open,
  841. .read = seq_read,
  842. .llseek = seq_lseek,
  843. .release = sysvipc_proc_release,
  844. };
  845. #endif /* CONFIG_PROC_FS */