util.c 23 KB

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