util.c 22 KB

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