util.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 writer ipc_ids.rw_mutex held.
  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. } ____cacheline_aligned_in_smp;
  411. /**
  412. * ipc_rcu_alloc - allocate ipc and rcu space
  413. * @size: size desired
  414. *
  415. * Allocate memory for the rcu header structure + the object.
  416. * Returns the pointer to the object or NULL upon failure.
  417. */
  418. void *ipc_rcu_alloc(int size)
  419. {
  420. /*
  421. * We prepend the allocation with the rcu struct
  422. */
  423. struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size);
  424. if (unlikely(!out))
  425. return NULL;
  426. atomic_set(&out->refcount, 1);
  427. return out + 1;
  428. }
  429. int ipc_rcu_getref(void *ptr)
  430. {
  431. struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
  432. return atomic_inc_not_zero(&p->refcount);
  433. }
  434. /**
  435. * ipc_schedule_free - free ipc + rcu space
  436. * @head: RCU callback structure for queued work
  437. */
  438. static void ipc_schedule_free(struct rcu_head *head)
  439. {
  440. vfree(container_of(head, struct ipc_rcu, rcu));
  441. }
  442. void ipc_rcu_putref(void *ptr)
  443. {
  444. struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
  445. if (!atomic_dec_and_test(&p->refcount))
  446. return;
  447. if (is_vmalloc_addr(ptr)) {
  448. call_rcu(&p->rcu, ipc_schedule_free);
  449. } else {
  450. kfree_rcu(p, rcu);
  451. }
  452. }
  453. /**
  454. * ipcperms - check IPC permissions
  455. * @ns: IPC namespace
  456. * @ipcp: IPC permission set
  457. * @flag: desired permission set.
  458. *
  459. * Check user, group, other permissions for access
  460. * to ipc resources. return 0 if allowed
  461. *
  462. * @flag will most probably be 0 or S_...UGO from <linux/stat.h>
  463. */
  464. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  465. {
  466. kuid_t euid = current_euid();
  467. int requested_mode, granted_mode;
  468. audit_ipc_obj(ipcp);
  469. requested_mode = (flag >> 6) | (flag >> 3) | flag;
  470. granted_mode = ipcp->mode;
  471. if (uid_eq(euid, ipcp->cuid) ||
  472. uid_eq(euid, ipcp->uid))
  473. granted_mode >>= 6;
  474. else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  475. granted_mode >>= 3;
  476. /* is there some bit set in requested_mode but not in granted_mode? */
  477. if ((requested_mode & ~granted_mode & 0007) &&
  478. !ns_capable(ns->user_ns, CAP_IPC_OWNER))
  479. return -1;
  480. return security_ipc_permission(ipcp, flag);
  481. }
  482. /*
  483. * Functions to convert between the kern_ipc_perm structure and the
  484. * old/new ipc_perm structures
  485. */
  486. /**
  487. * kernel_to_ipc64_perm - convert kernel ipc permissions to user
  488. * @in: kernel permissions
  489. * @out: new style IPC permissions
  490. *
  491. * Turn the kernel object @in into a set of permissions descriptions
  492. * for returning to userspace (@out).
  493. */
  494. void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
  495. {
  496. out->key = in->key;
  497. out->uid = from_kuid_munged(current_user_ns(), in->uid);
  498. out->gid = from_kgid_munged(current_user_ns(), in->gid);
  499. out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
  500. out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
  501. out->mode = in->mode;
  502. out->seq = in->seq;
  503. }
  504. /**
  505. * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
  506. * @in: new style IPC permissions
  507. * @out: old style IPC permissions
  508. *
  509. * Turn the new style permissions object @in into a compatibility
  510. * object and store it into the @out pointer.
  511. */
  512. void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
  513. {
  514. out->key = in->key;
  515. SET_UID(out->uid, in->uid);
  516. SET_GID(out->gid, in->gid);
  517. SET_UID(out->cuid, in->cuid);
  518. SET_GID(out->cgid, in->cgid);
  519. out->mode = in->mode;
  520. out->seq = in->seq;
  521. }
  522. /**
  523. * ipc_obtain_object
  524. * @ids: ipc identifier set
  525. * @id: ipc id to look for
  526. *
  527. * Look for an id in the ipc ids idr and return associated ipc object.
  528. *
  529. * Call inside the RCU critical section.
  530. * The ipc object is *not* locked on exit.
  531. */
  532. struct kern_ipc_perm *ipc_obtain_object(struct ipc_ids *ids, int id)
  533. {
  534. struct kern_ipc_perm *out;
  535. int lid = ipcid_to_idx(id);
  536. out = idr_find(&ids->ipcs_idr, lid);
  537. if (!out)
  538. return ERR_PTR(-EINVAL);
  539. return out;
  540. }
  541. /**
  542. * ipc_lock - Lock an ipc structure without rw_mutex held
  543. * @ids: IPC identifier set
  544. * @id: ipc id to look for
  545. *
  546. * Look for an id in the ipc ids idr and lock the associated ipc object.
  547. *
  548. * The ipc object is locked on successful exit.
  549. */
  550. struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
  551. {
  552. struct kern_ipc_perm *out;
  553. rcu_read_lock();
  554. out = ipc_obtain_object(ids, id);
  555. if (IS_ERR(out))
  556. goto err1;
  557. spin_lock(&out->lock);
  558. /* ipc_rmid() may have already freed the ID while ipc_lock
  559. * was spinning: here verify that the structure is still valid
  560. */
  561. if (!out->deleted)
  562. return out;
  563. spin_unlock(&out->lock);
  564. out = ERR_PTR(-EINVAL);
  565. err1:
  566. rcu_read_unlock();
  567. return out;
  568. }
  569. /**
  570. * ipc_obtain_object_check
  571. * @ids: ipc identifier set
  572. * @id: ipc id to look for
  573. *
  574. * Similar to ipc_obtain_object() but also checks
  575. * the ipc object reference counter.
  576. *
  577. * Call inside the RCU critical section.
  578. * The ipc object is *not* locked on exit.
  579. */
  580. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  581. {
  582. struct kern_ipc_perm *out = ipc_obtain_object(ids, id);
  583. if (IS_ERR(out))
  584. goto out;
  585. if (ipc_checkid(out, id))
  586. return ERR_PTR(-EIDRM);
  587. out:
  588. return out;
  589. }
  590. struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
  591. {
  592. struct kern_ipc_perm *out;
  593. out = ipc_lock(ids, id);
  594. if (IS_ERR(out))
  595. return out;
  596. if (ipc_checkid(out, id)) {
  597. ipc_unlock(out);
  598. return ERR_PTR(-EIDRM);
  599. }
  600. return out;
  601. }
  602. /**
  603. * ipcget - Common sys_*get() code
  604. * @ns : namsepace
  605. * @ids : IPC identifier set
  606. * @ops : operations to be called on ipc object creation, permission checks
  607. * and further checks
  608. * @params : the parameters needed by the previous operations.
  609. *
  610. * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
  611. */
  612. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  613. struct ipc_ops *ops, struct ipc_params *params)
  614. {
  615. if (params->key == IPC_PRIVATE)
  616. return ipcget_new(ns, ids, ops, params);
  617. else
  618. return ipcget_public(ns, ids, ops, params);
  619. }
  620. /**
  621. * ipc_update_perm - update the permissions of an IPC.
  622. * @in: the permission given as input.
  623. * @out: the permission of the ipc to set.
  624. */
  625. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  626. {
  627. kuid_t uid = make_kuid(current_user_ns(), in->uid);
  628. kgid_t gid = make_kgid(current_user_ns(), in->gid);
  629. if (!uid_valid(uid) || !gid_valid(gid))
  630. return -EINVAL;
  631. out->uid = uid;
  632. out->gid = gid;
  633. out->mode = (out->mode & ~S_IRWXUGO)
  634. | (in->mode & S_IRWXUGO);
  635. return 0;
  636. }
  637. /**
  638. * ipcctl_pre_down - retrieve an ipc and check permissions for some IPC_XXX cmd
  639. * @ns: the ipc namespace
  640. * @ids: the table of ids where to look for the ipc
  641. * @id: the id of the ipc to retrieve
  642. * @cmd: the cmd to check
  643. * @perm: the permission to set
  644. * @extra_perm: one extra permission parameter used by msq
  645. *
  646. * This function does some common audit and permissions check for some IPC_XXX
  647. * cmd and is called from semctl_down, shmctl_down and msgctl_down.
  648. * It must be called without any lock held and
  649. * - retrieves the ipc with the given id in the given table.
  650. * - performs some audit and permission check, depending on the given cmd
  651. * - returns the ipc with the ipc lock held in case of success
  652. * or an err-code without any lock held otherwise.
  653. *
  654. * Call holding the both the rw_mutex and the rcu read lock.
  655. */
  656. struct kern_ipc_perm *ipcctl_pre_down(struct ipc_namespace *ns,
  657. struct ipc_ids *ids, int id, int cmd,
  658. struct ipc64_perm *perm, int extra_perm)
  659. {
  660. struct kern_ipc_perm *ipcp;
  661. ipcp = ipcctl_pre_down_nolock(ns, ids, id, cmd, perm, extra_perm);
  662. if (IS_ERR(ipcp))
  663. goto out;
  664. spin_lock(&ipcp->lock);
  665. out:
  666. return ipcp;
  667. }
  668. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  669. struct ipc_ids *ids, int id, int cmd,
  670. struct ipc64_perm *perm, int extra_perm)
  671. {
  672. kuid_t euid;
  673. int err = -EPERM;
  674. struct kern_ipc_perm *ipcp;
  675. ipcp = ipc_obtain_object_check(ids, id);
  676. if (IS_ERR(ipcp)) {
  677. err = PTR_ERR(ipcp);
  678. goto err;
  679. }
  680. audit_ipc_obj(ipcp);
  681. if (cmd == IPC_SET)
  682. audit_ipc_set_perm(extra_perm, perm->uid,
  683. perm->gid, perm->mode);
  684. euid = current_euid();
  685. if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
  686. ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  687. return ipcp; /* successful lookup */
  688. err:
  689. return ERR_PTR(err);
  690. }
  691. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  692. /**
  693. * ipc_parse_version - IPC call version
  694. * @cmd: pointer to command
  695. *
  696. * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
  697. * The @cmd value is turned from an encoding command and version into
  698. * just the command code.
  699. */
  700. int ipc_parse_version (int *cmd)
  701. {
  702. if (*cmd & IPC_64) {
  703. *cmd ^= IPC_64;
  704. return IPC_64;
  705. } else {
  706. return IPC_OLD;
  707. }
  708. }
  709. #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
  710. #ifdef CONFIG_PROC_FS
  711. struct ipc_proc_iter {
  712. struct ipc_namespace *ns;
  713. struct ipc_proc_iface *iface;
  714. };
  715. /*
  716. * This routine locks the ipc structure found at least at position pos.
  717. */
  718. static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  719. loff_t *new_pos)
  720. {
  721. struct kern_ipc_perm *ipc;
  722. int total, id;
  723. total = 0;
  724. for (id = 0; id < pos && total < ids->in_use; id++) {
  725. ipc = idr_find(&ids->ipcs_idr, id);
  726. if (ipc != NULL)
  727. total++;
  728. }
  729. if (total >= ids->in_use)
  730. return NULL;
  731. for ( ; pos < IPCMNI; pos++) {
  732. ipc = idr_find(&ids->ipcs_idr, pos);
  733. if (ipc != NULL) {
  734. *new_pos = pos + 1;
  735. ipc_lock_by_ptr(ipc);
  736. return ipc;
  737. }
  738. }
  739. /* Out of range - return NULL to terminate iteration */
  740. return NULL;
  741. }
  742. static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  743. {
  744. struct ipc_proc_iter *iter = s->private;
  745. struct ipc_proc_iface *iface = iter->iface;
  746. struct kern_ipc_perm *ipc = it;
  747. /* If we had an ipc id locked before, unlock it */
  748. if (ipc && ipc != SEQ_START_TOKEN)
  749. ipc_unlock(ipc);
  750. return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
  751. }
  752. /*
  753. * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
  754. * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
  755. */
  756. static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  757. {
  758. struct ipc_proc_iter *iter = s->private;
  759. struct ipc_proc_iface *iface = iter->iface;
  760. struct ipc_ids *ids;
  761. ids = &iter->ns->ids[iface->ids];
  762. /*
  763. * Take the lock - this will be released by the corresponding
  764. * call to stop().
  765. */
  766. down_read(&ids->rw_mutex);
  767. /* pos < 0 is invalid */
  768. if (*pos < 0)
  769. return NULL;
  770. /* pos == 0 means header */
  771. if (*pos == 0)
  772. return SEQ_START_TOKEN;
  773. /* Find the (pos-1)th ipc */
  774. return sysvipc_find_ipc(ids, *pos - 1, pos);
  775. }
  776. static void sysvipc_proc_stop(struct seq_file *s, void *it)
  777. {
  778. struct kern_ipc_perm *ipc = it;
  779. struct ipc_proc_iter *iter = s->private;
  780. struct ipc_proc_iface *iface = iter->iface;
  781. struct ipc_ids *ids;
  782. /* If we had a locked structure, release it */
  783. if (ipc && ipc != SEQ_START_TOKEN)
  784. ipc_unlock(ipc);
  785. ids = &iter->ns->ids[iface->ids];
  786. /* Release the lock we took in start() */
  787. up_read(&ids->rw_mutex);
  788. }
  789. static int sysvipc_proc_show(struct seq_file *s, void *it)
  790. {
  791. struct ipc_proc_iter *iter = s->private;
  792. struct ipc_proc_iface *iface = iter->iface;
  793. if (it == SEQ_START_TOKEN)
  794. return seq_puts(s, iface->header);
  795. return iface->show(s, it);
  796. }
  797. static const struct seq_operations sysvipc_proc_seqops = {
  798. .start = sysvipc_proc_start,
  799. .stop = sysvipc_proc_stop,
  800. .next = sysvipc_proc_next,
  801. .show = sysvipc_proc_show,
  802. };
  803. static int sysvipc_proc_open(struct inode *inode, struct file *file)
  804. {
  805. int ret;
  806. struct seq_file *seq;
  807. struct ipc_proc_iter *iter;
  808. ret = -ENOMEM;
  809. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  810. if (!iter)
  811. goto out;
  812. ret = seq_open(file, &sysvipc_proc_seqops);
  813. if (ret)
  814. goto out_kfree;
  815. seq = file->private_data;
  816. seq->private = iter;
  817. iter->iface = PDE_DATA(inode);
  818. iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
  819. out:
  820. return ret;
  821. out_kfree:
  822. kfree(iter);
  823. goto out;
  824. }
  825. static int sysvipc_proc_release(struct inode *inode, struct file *file)
  826. {
  827. struct seq_file *seq = file->private_data;
  828. struct ipc_proc_iter *iter = seq->private;
  829. put_ipc_ns(iter->ns);
  830. return seq_release_private(inode, file);
  831. }
  832. static const struct file_operations sysvipc_proc_fops = {
  833. .open = sysvipc_proc_open,
  834. .read = seq_read,
  835. .llseek = seq_lseek,
  836. .release = sysvipc_proc_release,
  837. };
  838. #endif /* CONFIG_PROC_FS */