shm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * linux/ipc/shm.c
  3. * Copyright (C) 1992, 1993 Krishna Balasubramanian
  4. * Many improvements/fixes by Bruno Haible.
  5. * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
  6. * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
  7. *
  8. * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  9. * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
  10. * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
  11. * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
  12. * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
  13. * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  14. * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  15. *
  16. * support for audit of ipc object properties and permission changes
  17. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  18. *
  19. * namespaces support
  20. * OpenVZ, SWsoft Inc.
  21. * Pavel Emelianov <xemul@openvz.org>
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/shm.h>
  27. #include <linux/init.h>
  28. #include <linux/file.h>
  29. #include <linux/mman.h>
  30. #include <linux/shmem_fs.h>
  31. #include <linux/security.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/audit.h>
  34. #include <linux/capability.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/rwsem.h>
  38. #include <linux/nsproxy.h>
  39. #include <linux/mount.h>
  40. #include <linux/ipc_namespace.h>
  41. #include <asm/uaccess.h>
  42. #include "util.h"
  43. struct shm_file_data {
  44. int id;
  45. struct ipc_namespace *ns;
  46. struct file *file;
  47. const struct vm_operations_struct *vm_ops;
  48. };
  49. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  50. static const struct file_operations shm_file_operations;
  51. static const struct vm_operations_struct shm_vm_ops;
  52. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  53. #define shm_unlock(shp) \
  54. ipc_unlock(&(shp)->shm_perm)
  55. static int newseg(struct ipc_namespace *, struct ipc_params *);
  56. static void shm_open(struct vm_area_struct *vma);
  57. static void shm_close(struct vm_area_struct *vma);
  58. static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
  59. #ifdef CONFIG_PROC_FS
  60. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  61. #endif
  62. void shm_init_ns(struct ipc_namespace *ns)
  63. {
  64. ns->shm_ctlmax = SHMMAX;
  65. ns->shm_ctlall = SHMALL;
  66. ns->shm_ctlmni = SHMMNI;
  67. ns->shm_rmid_forced = 0;
  68. ns->shm_tot = 0;
  69. ipc_init_ids(&shm_ids(ns));
  70. }
  71. /*
  72. * Called with shm_ids.rw_mutex (writer) and the shp structure locked.
  73. * Only shm_ids.rw_mutex remains locked on exit.
  74. */
  75. static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  76. {
  77. struct shmid_kernel *shp;
  78. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  79. if (shp->shm_nattch){
  80. shp->shm_perm.mode |= SHM_DEST;
  81. /* Do not find it any more */
  82. shp->shm_perm.key = IPC_PRIVATE;
  83. shm_unlock(shp);
  84. } else
  85. shm_destroy(ns, shp);
  86. }
  87. #ifdef CONFIG_IPC_NS
  88. void shm_exit_ns(struct ipc_namespace *ns)
  89. {
  90. free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
  91. idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
  92. }
  93. #endif
  94. static int __init ipc_ns_init(void)
  95. {
  96. shm_init_ns(&init_ipc_ns);
  97. return 0;
  98. }
  99. pure_initcall(ipc_ns_init);
  100. void __init shm_init (void)
  101. {
  102. ipc_init_proc_interface("sysvipc/shm",
  103. #if BITS_PER_LONG <= 32
  104. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  105. #else
  106. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  107. #endif
  108. IPC_SHM_IDS, sysvipc_shm_proc_show);
  109. }
  110. /*
  111. * shm_lock_(check_) routines are called in the paths where the rw_mutex
  112. * is not necessarily held.
  113. */
  114. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  115. {
  116. struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
  117. if (IS_ERR(ipcp))
  118. return (struct shmid_kernel *)ipcp;
  119. return container_of(ipcp, struct shmid_kernel, shm_perm);
  120. }
  121. static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
  122. {
  123. rcu_read_lock();
  124. spin_lock(&ipcp->shm_perm.lock);
  125. }
  126. static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
  127. int id)
  128. {
  129. struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
  130. if (IS_ERR(ipcp))
  131. return (struct shmid_kernel *)ipcp;
  132. return container_of(ipcp, struct shmid_kernel, shm_perm);
  133. }
  134. static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
  135. {
  136. ipc_rmid(&shm_ids(ns), &s->shm_perm);
  137. }
  138. /* This is called by fork, once for every shm attach. */
  139. static void shm_open(struct vm_area_struct *vma)
  140. {
  141. struct file *file = vma->vm_file;
  142. struct shm_file_data *sfd = shm_file_data(file);
  143. struct shmid_kernel *shp;
  144. shp = shm_lock(sfd->ns, sfd->id);
  145. BUG_ON(IS_ERR(shp));
  146. shp->shm_atim = get_seconds();
  147. shp->shm_lprid = task_tgid_vnr(current);
  148. shp->shm_nattch++;
  149. shm_unlock(shp);
  150. }
  151. /*
  152. * shm_destroy - free the struct shmid_kernel
  153. *
  154. * @ns: namespace
  155. * @shp: struct to free
  156. *
  157. * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
  158. * but returns with shp unlocked and freed.
  159. */
  160. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  161. {
  162. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  163. shm_rmid(ns, shp);
  164. shm_unlock(shp);
  165. if (!is_file_hugepages(shp->shm_file))
  166. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  167. else if (shp->mlock_user)
  168. user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
  169. shp->mlock_user);
  170. fput (shp->shm_file);
  171. security_shm_free(shp);
  172. ipc_rcu_putref(shp);
  173. }
  174. /*
  175. * shm_may_destroy - identifies whether shm segment should be destroyed now
  176. *
  177. * Returns true if and only if there are no active users of the segment and
  178. * one of the following is true:
  179. *
  180. * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
  181. *
  182. * 2) sysctl kernel.shm_rmid_forced is set to 1.
  183. */
  184. static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  185. {
  186. return (shp->shm_nattch == 0) &&
  187. (ns->shm_rmid_forced ||
  188. (shp->shm_perm.mode & SHM_DEST));
  189. }
  190. /*
  191. * remove the attach descriptor vma.
  192. * free memory for segment if it is marked destroyed.
  193. * The descriptor has already been removed from the current->mm->mmap list
  194. * and will later be kfree()d.
  195. */
  196. static void shm_close(struct vm_area_struct *vma)
  197. {
  198. struct file * file = vma->vm_file;
  199. struct shm_file_data *sfd = shm_file_data(file);
  200. struct shmid_kernel *shp;
  201. struct ipc_namespace *ns = sfd->ns;
  202. down_write(&shm_ids(ns).rw_mutex);
  203. /* remove from the list of attaches of the shm segment */
  204. shp = shm_lock(ns, sfd->id);
  205. BUG_ON(IS_ERR(shp));
  206. shp->shm_lprid = task_tgid_vnr(current);
  207. shp->shm_dtim = get_seconds();
  208. shp->shm_nattch--;
  209. if (shm_may_destroy(ns, shp))
  210. shm_destroy(ns, shp);
  211. else
  212. shm_unlock(shp);
  213. up_write(&shm_ids(ns).rw_mutex);
  214. }
  215. /* Called with ns->shm_ids(ns).rw_mutex locked */
  216. static int shm_try_destroy_current(int id, void *p, void *data)
  217. {
  218. struct ipc_namespace *ns = data;
  219. struct kern_ipc_perm *ipcp = p;
  220. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  221. if (shp->shm_creator != current)
  222. return 0;
  223. /*
  224. * Mark it as orphaned to destroy the segment when
  225. * kernel.shm_rmid_forced is changed.
  226. * It is noop if the following shm_may_destroy() returns true.
  227. */
  228. shp->shm_creator = NULL;
  229. /*
  230. * Don't even try to destroy it. If shm_rmid_forced=0 and IPC_RMID
  231. * is not set, it shouldn't be deleted here.
  232. */
  233. if (!ns->shm_rmid_forced)
  234. return 0;
  235. if (shm_may_destroy(ns, shp)) {
  236. shm_lock_by_ptr(shp);
  237. shm_destroy(ns, shp);
  238. }
  239. return 0;
  240. }
  241. /* Called with ns->shm_ids(ns).rw_mutex locked */
  242. static int shm_try_destroy_orphaned(int id, void *p, void *data)
  243. {
  244. struct ipc_namespace *ns = data;
  245. struct kern_ipc_perm *ipcp = p;
  246. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  247. /*
  248. * We want to destroy segments without users and with already
  249. * exit'ed originating process.
  250. *
  251. * As shp->* are changed under rw_mutex, it's safe to skip shp locking.
  252. */
  253. if (shp->shm_creator != NULL)
  254. return 0;
  255. if (shm_may_destroy(ns, shp)) {
  256. shm_lock_by_ptr(shp);
  257. shm_destroy(ns, shp);
  258. }
  259. return 0;
  260. }
  261. void shm_destroy_orphaned(struct ipc_namespace *ns)
  262. {
  263. down_write(&shm_ids(ns).rw_mutex);
  264. if (shm_ids(ns).in_use)
  265. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
  266. up_write(&shm_ids(ns).rw_mutex);
  267. }
  268. void exit_shm(struct task_struct *task)
  269. {
  270. struct ipc_namespace *ns = task->nsproxy->ipc_ns;
  271. if (shm_ids(ns).in_use == 0)
  272. return;
  273. /* Destroy all already created segments, but not mapped yet */
  274. down_write(&shm_ids(ns).rw_mutex);
  275. if (shm_ids(ns).in_use)
  276. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_current, ns);
  277. up_write(&shm_ids(ns).rw_mutex);
  278. }
  279. static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  280. {
  281. struct file *file = vma->vm_file;
  282. struct shm_file_data *sfd = shm_file_data(file);
  283. return sfd->vm_ops->fault(vma, vmf);
  284. }
  285. #ifdef CONFIG_NUMA
  286. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  287. {
  288. struct file *file = vma->vm_file;
  289. struct shm_file_data *sfd = shm_file_data(file);
  290. int err = 0;
  291. if (sfd->vm_ops->set_policy)
  292. err = sfd->vm_ops->set_policy(vma, new);
  293. return err;
  294. }
  295. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  296. unsigned long addr)
  297. {
  298. struct file *file = vma->vm_file;
  299. struct shm_file_data *sfd = shm_file_data(file);
  300. struct mempolicy *pol = NULL;
  301. if (sfd->vm_ops->get_policy)
  302. pol = sfd->vm_ops->get_policy(vma, addr);
  303. else if (vma->vm_policy)
  304. pol = vma->vm_policy;
  305. return pol;
  306. }
  307. #endif
  308. static int shm_mmap(struct file * file, struct vm_area_struct * vma)
  309. {
  310. struct shm_file_data *sfd = shm_file_data(file);
  311. int ret;
  312. ret = sfd->file->f_op->mmap(sfd->file, vma);
  313. if (ret != 0)
  314. return ret;
  315. sfd->vm_ops = vma->vm_ops;
  316. #ifdef CONFIG_MMU
  317. BUG_ON(!sfd->vm_ops->fault);
  318. #endif
  319. vma->vm_ops = &shm_vm_ops;
  320. shm_open(vma);
  321. return ret;
  322. }
  323. static int shm_release(struct inode *ino, struct file *file)
  324. {
  325. struct shm_file_data *sfd = shm_file_data(file);
  326. put_ipc_ns(sfd->ns);
  327. shm_file_data(file) = NULL;
  328. kfree(sfd);
  329. return 0;
  330. }
  331. static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  332. {
  333. struct shm_file_data *sfd = shm_file_data(file);
  334. if (!sfd->file->f_op->fsync)
  335. return -EINVAL;
  336. return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
  337. }
  338. static long shm_fallocate(struct file *file, int mode, loff_t offset,
  339. loff_t len)
  340. {
  341. struct shm_file_data *sfd = shm_file_data(file);
  342. if (!sfd->file->f_op->fallocate)
  343. return -EOPNOTSUPP;
  344. return sfd->file->f_op->fallocate(file, mode, offset, len);
  345. }
  346. static unsigned long shm_get_unmapped_area(struct file *file,
  347. unsigned long addr, unsigned long len, unsigned long pgoff,
  348. unsigned long flags)
  349. {
  350. struct shm_file_data *sfd = shm_file_data(file);
  351. return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
  352. pgoff, flags);
  353. }
  354. static const struct file_operations shm_file_operations = {
  355. .mmap = shm_mmap,
  356. .fsync = shm_fsync,
  357. .release = shm_release,
  358. #ifndef CONFIG_MMU
  359. .get_unmapped_area = shm_get_unmapped_area,
  360. #endif
  361. .llseek = noop_llseek,
  362. .fallocate = shm_fallocate,
  363. };
  364. static const struct file_operations shm_file_operations_huge = {
  365. .mmap = shm_mmap,
  366. .fsync = shm_fsync,
  367. .release = shm_release,
  368. .get_unmapped_area = shm_get_unmapped_area,
  369. .llseek = noop_llseek,
  370. .fallocate = shm_fallocate,
  371. };
  372. int is_file_shm_hugepages(struct file *file)
  373. {
  374. return file->f_op == &shm_file_operations_huge;
  375. }
  376. static const struct vm_operations_struct shm_vm_ops = {
  377. .open = shm_open, /* callback for a new vm-area open */
  378. .close = shm_close, /* callback for when the vm-area is released */
  379. .fault = shm_fault,
  380. #if defined(CONFIG_NUMA)
  381. .set_policy = shm_set_policy,
  382. .get_policy = shm_get_policy,
  383. #endif
  384. };
  385. /**
  386. * newseg - Create a new shared memory segment
  387. * @ns: namespace
  388. * @params: ptr to the structure that contains key, size and shmflg
  389. *
  390. * Called with shm_ids.rw_mutex held as a writer.
  391. */
  392. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  393. {
  394. key_t key = params->key;
  395. int shmflg = params->flg;
  396. size_t size = params->u.size;
  397. int error;
  398. struct shmid_kernel *shp;
  399. size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  400. struct file * file;
  401. char name[13];
  402. int id;
  403. vm_flags_t acctflag = 0;
  404. if (size < SHMMIN || size > ns->shm_ctlmax)
  405. return -EINVAL;
  406. if (ns->shm_tot + numpages > ns->shm_ctlall)
  407. return -ENOSPC;
  408. shp = ipc_rcu_alloc(sizeof(*shp));
  409. if (!shp)
  410. return -ENOMEM;
  411. shp->shm_perm.key = key;
  412. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  413. shp->mlock_user = NULL;
  414. shp->shm_perm.security = NULL;
  415. error = security_shm_alloc(shp);
  416. if (error) {
  417. ipc_rcu_putref(shp);
  418. return error;
  419. }
  420. sprintf (name, "SYSV%08x", key);
  421. if (shmflg & SHM_HUGETLB) {
  422. size_t hugesize = ALIGN(size, huge_page_size(&default_hstate));
  423. /* hugetlb_file_setup applies strict accounting */
  424. if (shmflg & SHM_NORESERVE)
  425. acctflag = VM_NORESERVE;
  426. file = hugetlb_file_setup(name, hugesize, acctflag,
  427. &shp->mlock_user, HUGETLB_SHMFS_INODE);
  428. } else {
  429. /*
  430. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  431. * if it's asked for.
  432. */
  433. if ((shmflg & SHM_NORESERVE) &&
  434. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  435. acctflag = VM_NORESERVE;
  436. file = shmem_file_setup(name, size, acctflag);
  437. }
  438. error = PTR_ERR(file);
  439. if (IS_ERR(file))
  440. goto no_file;
  441. shp->shm_cprid = task_tgid_vnr(current);
  442. shp->shm_lprid = 0;
  443. shp->shm_atim = shp->shm_dtim = 0;
  444. shp->shm_ctim = get_seconds();
  445. shp->shm_segsz = size;
  446. shp->shm_nattch = 0;
  447. shp->shm_file = file;
  448. shp->shm_creator = current;
  449. id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  450. if (id < 0) {
  451. error = id;
  452. goto no_id;
  453. }
  454. /*
  455. * shmid gets reported as "inode#" in /proc/pid/maps.
  456. * proc-ps tools use this. Changing this will break them.
  457. */
  458. file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
  459. ns->shm_tot += numpages;
  460. error = shp->shm_perm.id;
  461. shm_unlock(shp);
  462. return error;
  463. no_id:
  464. if (is_file_hugepages(file) && shp->mlock_user)
  465. user_shm_unlock(size, shp->mlock_user);
  466. fput(file);
  467. no_file:
  468. security_shm_free(shp);
  469. ipc_rcu_putref(shp);
  470. return error;
  471. }
  472. /*
  473. * Called with shm_ids.rw_mutex and ipcp locked.
  474. */
  475. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  476. {
  477. struct shmid_kernel *shp;
  478. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  479. return security_shm_associate(shp, shmflg);
  480. }
  481. /*
  482. * Called with shm_ids.rw_mutex and ipcp locked.
  483. */
  484. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  485. struct ipc_params *params)
  486. {
  487. struct shmid_kernel *shp;
  488. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  489. if (shp->shm_segsz < params->u.size)
  490. return -EINVAL;
  491. return 0;
  492. }
  493. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  494. {
  495. struct ipc_namespace *ns;
  496. struct ipc_ops shm_ops;
  497. struct ipc_params shm_params;
  498. ns = current->nsproxy->ipc_ns;
  499. shm_ops.getnew = newseg;
  500. shm_ops.associate = shm_security;
  501. shm_ops.more_checks = shm_more_checks;
  502. shm_params.key = key;
  503. shm_params.flg = shmflg;
  504. shm_params.u.size = size;
  505. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  506. }
  507. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  508. {
  509. switch(version) {
  510. case IPC_64:
  511. return copy_to_user(buf, in, sizeof(*in));
  512. case IPC_OLD:
  513. {
  514. struct shmid_ds out;
  515. memset(&out, 0, sizeof(out));
  516. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  517. out.shm_segsz = in->shm_segsz;
  518. out.shm_atime = in->shm_atime;
  519. out.shm_dtime = in->shm_dtime;
  520. out.shm_ctime = in->shm_ctime;
  521. out.shm_cpid = in->shm_cpid;
  522. out.shm_lpid = in->shm_lpid;
  523. out.shm_nattch = in->shm_nattch;
  524. return copy_to_user(buf, &out, sizeof(out));
  525. }
  526. default:
  527. return -EINVAL;
  528. }
  529. }
  530. static inline unsigned long
  531. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  532. {
  533. switch(version) {
  534. case IPC_64:
  535. if (copy_from_user(out, buf, sizeof(*out)))
  536. return -EFAULT;
  537. return 0;
  538. case IPC_OLD:
  539. {
  540. struct shmid_ds tbuf_old;
  541. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  542. return -EFAULT;
  543. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  544. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  545. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  546. return 0;
  547. }
  548. default:
  549. return -EINVAL;
  550. }
  551. }
  552. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  553. {
  554. switch(version) {
  555. case IPC_64:
  556. return copy_to_user(buf, in, sizeof(*in));
  557. case IPC_OLD:
  558. {
  559. struct shminfo out;
  560. if(in->shmmax > INT_MAX)
  561. out.shmmax = INT_MAX;
  562. else
  563. out.shmmax = (int)in->shmmax;
  564. out.shmmin = in->shmmin;
  565. out.shmmni = in->shmmni;
  566. out.shmseg = in->shmseg;
  567. out.shmall = in->shmall;
  568. return copy_to_user(buf, &out, sizeof(out));
  569. }
  570. default:
  571. return -EINVAL;
  572. }
  573. }
  574. /*
  575. * Calculate and add used RSS and swap pages of a shm.
  576. * Called with shm_ids.rw_mutex held as a reader
  577. */
  578. static void shm_add_rss_swap(struct shmid_kernel *shp,
  579. unsigned long *rss_add, unsigned long *swp_add)
  580. {
  581. struct inode *inode;
  582. inode = shp->shm_file->f_path.dentry->d_inode;
  583. if (is_file_hugepages(shp->shm_file)) {
  584. struct address_space *mapping = inode->i_mapping;
  585. struct hstate *h = hstate_file(shp->shm_file);
  586. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  587. } else {
  588. #ifdef CONFIG_SHMEM
  589. struct shmem_inode_info *info = SHMEM_I(inode);
  590. spin_lock(&info->lock);
  591. *rss_add += inode->i_mapping->nrpages;
  592. *swp_add += info->swapped;
  593. spin_unlock(&info->lock);
  594. #else
  595. *rss_add += inode->i_mapping->nrpages;
  596. #endif
  597. }
  598. }
  599. /*
  600. * Called with shm_ids.rw_mutex held as a reader
  601. */
  602. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  603. unsigned long *swp)
  604. {
  605. int next_id;
  606. int total, in_use;
  607. *rss = 0;
  608. *swp = 0;
  609. in_use = shm_ids(ns).in_use;
  610. for (total = 0, next_id = 0; total < in_use; next_id++) {
  611. struct kern_ipc_perm *ipc;
  612. struct shmid_kernel *shp;
  613. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  614. if (ipc == NULL)
  615. continue;
  616. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  617. shm_add_rss_swap(shp, rss, swp);
  618. total++;
  619. }
  620. }
  621. /*
  622. * This function handles some shmctl commands which require the rw_mutex
  623. * to be held in write mode.
  624. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  625. */
  626. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  627. struct shmid_ds __user *buf, int version)
  628. {
  629. struct kern_ipc_perm *ipcp;
  630. struct shmid64_ds shmid64;
  631. struct shmid_kernel *shp;
  632. int err;
  633. if (cmd == IPC_SET) {
  634. if (copy_shmid_from_user(&shmid64, buf, version))
  635. return -EFAULT;
  636. }
  637. ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
  638. &shmid64.shm_perm, 0);
  639. if (IS_ERR(ipcp))
  640. return PTR_ERR(ipcp);
  641. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  642. err = security_shm_shmctl(shp, cmd);
  643. if (err)
  644. goto out_unlock;
  645. switch (cmd) {
  646. case IPC_RMID:
  647. do_shm_rmid(ns, ipcp);
  648. goto out_up;
  649. case IPC_SET:
  650. err = ipc_update_perm(&shmid64.shm_perm, ipcp);
  651. if (err)
  652. goto out_unlock;
  653. shp->shm_ctim = get_seconds();
  654. break;
  655. default:
  656. err = -EINVAL;
  657. }
  658. out_unlock:
  659. shm_unlock(shp);
  660. out_up:
  661. up_write(&shm_ids(ns).rw_mutex);
  662. return err;
  663. }
  664. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  665. {
  666. struct shmid_kernel *shp;
  667. int err, version;
  668. struct ipc_namespace *ns;
  669. if (cmd < 0 || shmid < 0) {
  670. err = -EINVAL;
  671. goto out;
  672. }
  673. version = ipc_parse_version(&cmd);
  674. ns = current->nsproxy->ipc_ns;
  675. switch (cmd) { /* replace with proc interface ? */
  676. case IPC_INFO:
  677. {
  678. struct shminfo64 shminfo;
  679. err = security_shm_shmctl(NULL, cmd);
  680. if (err)
  681. return err;
  682. memset(&shminfo, 0, sizeof(shminfo));
  683. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  684. shminfo.shmmax = ns->shm_ctlmax;
  685. shminfo.shmall = ns->shm_ctlall;
  686. shminfo.shmmin = SHMMIN;
  687. if(copy_shminfo_to_user (buf, &shminfo, version))
  688. return -EFAULT;
  689. down_read(&shm_ids(ns).rw_mutex);
  690. err = ipc_get_maxid(&shm_ids(ns));
  691. up_read(&shm_ids(ns).rw_mutex);
  692. if(err<0)
  693. err = 0;
  694. goto out;
  695. }
  696. case SHM_INFO:
  697. {
  698. struct shm_info shm_info;
  699. err = security_shm_shmctl(NULL, cmd);
  700. if (err)
  701. return err;
  702. memset(&shm_info, 0, sizeof(shm_info));
  703. down_read(&shm_ids(ns).rw_mutex);
  704. shm_info.used_ids = shm_ids(ns).in_use;
  705. shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
  706. shm_info.shm_tot = ns->shm_tot;
  707. shm_info.swap_attempts = 0;
  708. shm_info.swap_successes = 0;
  709. err = ipc_get_maxid(&shm_ids(ns));
  710. up_read(&shm_ids(ns).rw_mutex);
  711. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  712. err = -EFAULT;
  713. goto out;
  714. }
  715. err = err < 0 ? 0 : err;
  716. goto out;
  717. }
  718. case SHM_STAT:
  719. case IPC_STAT:
  720. {
  721. struct shmid64_ds tbuf;
  722. int result;
  723. if (cmd == SHM_STAT) {
  724. shp = shm_lock(ns, shmid);
  725. if (IS_ERR(shp)) {
  726. err = PTR_ERR(shp);
  727. goto out;
  728. }
  729. result = shp->shm_perm.id;
  730. } else {
  731. shp = shm_lock_check(ns, shmid);
  732. if (IS_ERR(shp)) {
  733. err = PTR_ERR(shp);
  734. goto out;
  735. }
  736. result = 0;
  737. }
  738. err = -EACCES;
  739. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  740. goto out_unlock;
  741. err = security_shm_shmctl(shp, cmd);
  742. if (err)
  743. goto out_unlock;
  744. memset(&tbuf, 0, sizeof(tbuf));
  745. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  746. tbuf.shm_segsz = shp->shm_segsz;
  747. tbuf.shm_atime = shp->shm_atim;
  748. tbuf.shm_dtime = shp->shm_dtim;
  749. tbuf.shm_ctime = shp->shm_ctim;
  750. tbuf.shm_cpid = shp->shm_cprid;
  751. tbuf.shm_lpid = shp->shm_lprid;
  752. tbuf.shm_nattch = shp->shm_nattch;
  753. shm_unlock(shp);
  754. if(copy_shmid_to_user (buf, &tbuf, version))
  755. err = -EFAULT;
  756. else
  757. err = result;
  758. goto out;
  759. }
  760. case SHM_LOCK:
  761. case SHM_UNLOCK:
  762. {
  763. struct file *shm_file;
  764. shp = shm_lock_check(ns, shmid);
  765. if (IS_ERR(shp)) {
  766. err = PTR_ERR(shp);
  767. goto out;
  768. }
  769. audit_ipc_obj(&(shp->shm_perm));
  770. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  771. kuid_t euid = current_euid();
  772. err = -EPERM;
  773. if (!uid_eq(euid, shp->shm_perm.uid) &&
  774. !uid_eq(euid, shp->shm_perm.cuid))
  775. goto out_unlock;
  776. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
  777. goto out_unlock;
  778. }
  779. err = security_shm_shmctl(shp, cmd);
  780. if (err)
  781. goto out_unlock;
  782. shm_file = shp->shm_file;
  783. if (is_file_hugepages(shm_file))
  784. goto out_unlock;
  785. if (cmd == SHM_LOCK) {
  786. struct user_struct *user = current_user();
  787. err = shmem_lock(shm_file, 1, user);
  788. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
  789. shp->shm_perm.mode |= SHM_LOCKED;
  790. shp->mlock_user = user;
  791. }
  792. goto out_unlock;
  793. }
  794. /* SHM_UNLOCK */
  795. if (!(shp->shm_perm.mode & SHM_LOCKED))
  796. goto out_unlock;
  797. shmem_lock(shm_file, 0, shp->mlock_user);
  798. shp->shm_perm.mode &= ~SHM_LOCKED;
  799. shp->mlock_user = NULL;
  800. get_file(shm_file);
  801. shm_unlock(shp);
  802. shmem_unlock_mapping(shm_file->f_mapping);
  803. fput(shm_file);
  804. goto out;
  805. }
  806. case IPC_RMID:
  807. case IPC_SET:
  808. err = shmctl_down(ns, shmid, cmd, buf, version);
  809. return err;
  810. default:
  811. return -EINVAL;
  812. }
  813. out_unlock:
  814. shm_unlock(shp);
  815. out:
  816. return err;
  817. }
  818. /*
  819. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  820. *
  821. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  822. * "raddr" thing points to kernel space, and there has to be a wrapper around
  823. * this.
  824. */
  825. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
  826. unsigned long shmlba)
  827. {
  828. struct shmid_kernel *shp;
  829. unsigned long addr;
  830. unsigned long size;
  831. struct file * file;
  832. int err;
  833. unsigned long flags;
  834. unsigned long prot;
  835. int acc_mode;
  836. unsigned long user_addr;
  837. struct ipc_namespace *ns;
  838. struct shm_file_data *sfd;
  839. struct path path;
  840. fmode_t f_mode;
  841. err = -EINVAL;
  842. if (shmid < 0)
  843. goto out;
  844. else if ((addr = (ulong)shmaddr)) {
  845. if (addr & (shmlba - 1)) {
  846. /*
  847. * Round down to the nearest multiple of shmlba.
  848. * For sane do_mmap_pgoff() parameters, avoid
  849. * round downs that trigger nil-page and MAP_FIXED.
  850. */
  851. if ((shmflg & SHM_RND) && addr >= shmlba)
  852. addr &= ~(shmlba - 1);
  853. else
  854. #ifndef __ARCH_FORCE_SHMLBA
  855. if (addr & ~PAGE_MASK)
  856. #endif
  857. goto out;
  858. }
  859. flags = MAP_SHARED | MAP_FIXED;
  860. } else {
  861. if ((shmflg & SHM_REMAP))
  862. goto out;
  863. flags = MAP_SHARED;
  864. }
  865. if (shmflg & SHM_RDONLY) {
  866. prot = PROT_READ;
  867. acc_mode = S_IRUGO;
  868. f_mode = FMODE_READ;
  869. } else {
  870. prot = PROT_READ | PROT_WRITE;
  871. acc_mode = S_IRUGO | S_IWUGO;
  872. f_mode = FMODE_READ | FMODE_WRITE;
  873. }
  874. if (shmflg & SHM_EXEC) {
  875. prot |= PROT_EXEC;
  876. acc_mode |= S_IXUGO;
  877. }
  878. /*
  879. * We cannot rely on the fs check since SYSV IPC does have an
  880. * additional creator id...
  881. */
  882. ns = current->nsproxy->ipc_ns;
  883. shp = shm_lock_check(ns, shmid);
  884. if (IS_ERR(shp)) {
  885. err = PTR_ERR(shp);
  886. goto out;
  887. }
  888. err = -EACCES;
  889. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  890. goto out_unlock;
  891. err = security_shm_shmat(shp, shmaddr, shmflg);
  892. if (err)
  893. goto out_unlock;
  894. path = shp->shm_file->f_path;
  895. path_get(&path);
  896. shp->shm_nattch++;
  897. size = i_size_read(path.dentry->d_inode);
  898. shm_unlock(shp);
  899. err = -ENOMEM;
  900. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  901. if (!sfd)
  902. goto out_put_dentry;
  903. file = alloc_file(&path, f_mode,
  904. is_file_hugepages(shp->shm_file) ?
  905. &shm_file_operations_huge :
  906. &shm_file_operations);
  907. if (!file)
  908. goto out_free;
  909. file->private_data = sfd;
  910. file->f_mapping = shp->shm_file->f_mapping;
  911. sfd->id = shp->shm_perm.id;
  912. sfd->ns = get_ipc_ns(ns);
  913. sfd->file = shp->shm_file;
  914. sfd->vm_ops = NULL;
  915. err = security_mmap_file(file, prot, flags);
  916. if (err)
  917. goto out_fput;
  918. down_write(&current->mm->mmap_sem);
  919. if (addr && !(shmflg & SHM_REMAP)) {
  920. err = -EINVAL;
  921. if (find_vma_intersection(current->mm, addr, addr + size))
  922. goto invalid;
  923. /*
  924. * If shm segment goes below stack, make sure there is some
  925. * space left for the stack to grow (at least 4 pages).
  926. */
  927. if (addr < current->mm->start_stack &&
  928. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  929. goto invalid;
  930. }
  931. user_addr = do_mmap (file, addr, size, prot, flags, 0);
  932. *raddr = user_addr;
  933. err = 0;
  934. if (IS_ERR_VALUE(user_addr))
  935. err = (long)user_addr;
  936. invalid:
  937. up_write(&current->mm->mmap_sem);
  938. out_fput:
  939. fput(file);
  940. out_nattch:
  941. down_write(&shm_ids(ns).rw_mutex);
  942. shp = shm_lock(ns, shmid);
  943. BUG_ON(IS_ERR(shp));
  944. shp->shm_nattch--;
  945. if (shm_may_destroy(ns, shp))
  946. shm_destroy(ns, shp);
  947. else
  948. shm_unlock(shp);
  949. up_write(&shm_ids(ns).rw_mutex);
  950. out:
  951. return err;
  952. out_unlock:
  953. shm_unlock(shp);
  954. goto out;
  955. out_free:
  956. kfree(sfd);
  957. out_put_dentry:
  958. path_put(&path);
  959. goto out_nattch;
  960. }
  961. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  962. {
  963. unsigned long ret;
  964. long err;
  965. err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
  966. if (err)
  967. return err;
  968. force_successful_syscall_return();
  969. return (long)ret;
  970. }
  971. /*
  972. * detach and kill segment if marked destroyed.
  973. * The work is done in shm_close.
  974. */
  975. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  976. {
  977. struct mm_struct *mm = current->mm;
  978. struct vm_area_struct *vma;
  979. unsigned long addr = (unsigned long)shmaddr;
  980. int retval = -EINVAL;
  981. #ifdef CONFIG_MMU
  982. loff_t size = 0;
  983. struct vm_area_struct *next;
  984. #endif
  985. if (addr & ~PAGE_MASK)
  986. return retval;
  987. down_write(&mm->mmap_sem);
  988. /*
  989. * This function tries to be smart and unmap shm segments that
  990. * were modified by partial mlock or munmap calls:
  991. * - It first determines the size of the shm segment that should be
  992. * unmapped: It searches for a vma that is backed by shm and that
  993. * started at address shmaddr. It records it's size and then unmaps
  994. * it.
  995. * - Then it unmaps all shm vmas that started at shmaddr and that
  996. * are within the initially determined size.
  997. * Errors from do_munmap are ignored: the function only fails if
  998. * it's called with invalid parameters or if it's called to unmap
  999. * a part of a vma. Both calls in this function are for full vmas,
  1000. * the parameters are directly copied from the vma itself and always
  1001. * valid - therefore do_munmap cannot fail. (famous last words?)
  1002. */
  1003. /*
  1004. * If it had been mremap()'d, the starting address would not
  1005. * match the usual checks anyway. So assume all vma's are
  1006. * above the starting address given.
  1007. */
  1008. vma = find_vma(mm, addr);
  1009. #ifdef CONFIG_MMU
  1010. while (vma) {
  1011. next = vma->vm_next;
  1012. /*
  1013. * Check if the starting address would match, i.e. it's
  1014. * a fragment created by mprotect() and/or munmap(), or it
  1015. * otherwise it starts at this address with no hassles.
  1016. */
  1017. if ((vma->vm_ops == &shm_vm_ops) &&
  1018. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  1019. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  1020. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1021. /*
  1022. * We discovered the size of the shm segment, so
  1023. * break out of here and fall through to the next
  1024. * loop that uses the size information to stop
  1025. * searching for matching vma's.
  1026. */
  1027. retval = 0;
  1028. vma = next;
  1029. break;
  1030. }
  1031. vma = next;
  1032. }
  1033. /*
  1034. * We need look no further than the maximum address a fragment
  1035. * could possibly have landed at. Also cast things to loff_t to
  1036. * prevent overflows and make comparisons vs. equal-width types.
  1037. */
  1038. size = PAGE_ALIGN(size);
  1039. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1040. next = vma->vm_next;
  1041. /* finding a matching vma now does not alter retval */
  1042. if ((vma->vm_ops == &shm_vm_ops) &&
  1043. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  1044. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1045. vma = next;
  1046. }
  1047. #else /* CONFIG_MMU */
  1048. /* under NOMMU conditions, the exact address to be destroyed must be
  1049. * given */
  1050. retval = -EINVAL;
  1051. if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1052. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1053. retval = 0;
  1054. }
  1055. #endif
  1056. up_write(&mm->mmap_sem);
  1057. return retval;
  1058. }
  1059. #ifdef CONFIG_PROC_FS
  1060. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1061. {
  1062. struct user_namespace *user_ns = seq_user_ns(s);
  1063. struct shmid_kernel *shp = it;
  1064. unsigned long rss = 0, swp = 0;
  1065. shm_add_rss_swap(shp, &rss, &swp);
  1066. #if BITS_PER_LONG <= 32
  1067. #define SIZE_SPEC "%10lu"
  1068. #else
  1069. #define SIZE_SPEC "%21lu"
  1070. #endif
  1071. return seq_printf(s,
  1072. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1073. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
  1074. SIZE_SPEC " " SIZE_SPEC "\n",
  1075. shp->shm_perm.key,
  1076. shp->shm_perm.id,
  1077. shp->shm_perm.mode,
  1078. shp->shm_segsz,
  1079. shp->shm_cprid,
  1080. shp->shm_lprid,
  1081. shp->shm_nattch,
  1082. from_kuid_munged(user_ns, shp->shm_perm.uid),
  1083. from_kgid_munged(user_ns, shp->shm_perm.gid),
  1084. from_kuid_munged(user_ns, shp->shm_perm.cuid),
  1085. from_kgid_munged(user_ns, shp->shm_perm.cgid),
  1086. shp->shm_atim,
  1087. shp->shm_dtim,
  1088. shp->shm_ctim,
  1089. rss * PAGE_SIZE,
  1090. swp * PAGE_SIZE);
  1091. }
  1092. #endif