util.c 22 KB

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