util.c 22 KB

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