shm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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. int 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. /* hugetlb_file_setup applies strict accounting */
  423. if (shmflg & SHM_NORESERVE)
  424. acctflag = VM_NORESERVE;
  425. file = hugetlb_file_setup(name, 0, size, acctflag,
  426. &shp->mlock_user, HUGETLB_SHMFS_INODE);
  427. } else {
  428. /*
  429. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  430. * if it's asked for.
  431. */
  432. if ((shmflg & SHM_NORESERVE) &&
  433. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  434. acctflag = VM_NORESERVE;
  435. file = shmem_file_setup(name, size, acctflag);
  436. }
  437. error = PTR_ERR(file);
  438. if (IS_ERR(file))
  439. goto no_file;
  440. id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  441. if (id < 0) {
  442. error = id;
  443. goto no_id;
  444. }
  445. shp->shm_cprid = task_tgid_vnr(current);
  446. shp->shm_lprid = 0;
  447. shp->shm_atim = shp->shm_dtim = 0;
  448. shp->shm_ctim = get_seconds();
  449. shp->shm_segsz = size;
  450. shp->shm_nattch = 0;
  451. shp->shm_file = file;
  452. shp->shm_creator = current;
  453. /*
  454. * shmid gets reported as "inode#" in /proc/pid/maps.
  455. * proc-ps tools use this. Changing this will break them.
  456. */
  457. file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
  458. ns->shm_tot += numpages;
  459. error = shp->shm_perm.id;
  460. shm_unlock(shp);
  461. return error;
  462. no_id:
  463. if (is_file_hugepages(file) && shp->mlock_user)
  464. user_shm_unlock(size, shp->mlock_user);
  465. fput(file);
  466. no_file:
  467. security_shm_free(shp);
  468. ipc_rcu_putref(shp);
  469. return error;
  470. }
  471. /*
  472. * Called with shm_ids.rw_mutex and ipcp locked.
  473. */
  474. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  475. {
  476. struct shmid_kernel *shp;
  477. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  478. return security_shm_associate(shp, shmflg);
  479. }
  480. /*
  481. * Called with shm_ids.rw_mutex and ipcp locked.
  482. */
  483. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  484. struct ipc_params *params)
  485. {
  486. struct shmid_kernel *shp;
  487. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  488. if (shp->shm_segsz < params->u.size)
  489. return -EINVAL;
  490. return 0;
  491. }
  492. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  493. {
  494. struct ipc_namespace *ns;
  495. struct ipc_ops shm_ops;
  496. struct ipc_params shm_params;
  497. ns = current->nsproxy->ipc_ns;
  498. shm_ops.getnew = newseg;
  499. shm_ops.associate = shm_security;
  500. shm_ops.more_checks = shm_more_checks;
  501. shm_params.key = key;
  502. shm_params.flg = shmflg;
  503. shm_params.u.size = size;
  504. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  505. }
  506. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  507. {
  508. switch(version) {
  509. case IPC_64:
  510. return copy_to_user(buf, in, sizeof(*in));
  511. case IPC_OLD:
  512. {
  513. struct shmid_ds out;
  514. memset(&out, 0, sizeof(out));
  515. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  516. out.shm_segsz = in->shm_segsz;
  517. out.shm_atime = in->shm_atime;
  518. out.shm_dtime = in->shm_dtime;
  519. out.shm_ctime = in->shm_ctime;
  520. out.shm_cpid = in->shm_cpid;
  521. out.shm_lpid = in->shm_lpid;
  522. out.shm_nattch = in->shm_nattch;
  523. return copy_to_user(buf, &out, sizeof(out));
  524. }
  525. default:
  526. return -EINVAL;
  527. }
  528. }
  529. static inline unsigned long
  530. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  531. {
  532. switch(version) {
  533. case IPC_64:
  534. if (copy_from_user(out, buf, sizeof(*out)))
  535. return -EFAULT;
  536. return 0;
  537. case IPC_OLD:
  538. {
  539. struct shmid_ds tbuf_old;
  540. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  541. return -EFAULT;
  542. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  543. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  544. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  545. return 0;
  546. }
  547. default:
  548. return -EINVAL;
  549. }
  550. }
  551. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  552. {
  553. switch(version) {
  554. case IPC_64:
  555. return copy_to_user(buf, in, sizeof(*in));
  556. case IPC_OLD:
  557. {
  558. struct shminfo out;
  559. if(in->shmmax > INT_MAX)
  560. out.shmmax = INT_MAX;
  561. else
  562. out.shmmax = (int)in->shmmax;
  563. out.shmmin = in->shmmin;
  564. out.shmmni = in->shmmni;
  565. out.shmseg = in->shmseg;
  566. out.shmall = in->shmall;
  567. return copy_to_user(buf, &out, sizeof(out));
  568. }
  569. default:
  570. return -EINVAL;
  571. }
  572. }
  573. /*
  574. * Calculate and add used RSS and swap pages of a shm.
  575. * Called with shm_ids.rw_mutex held as a reader
  576. */
  577. static void shm_add_rss_swap(struct shmid_kernel *shp,
  578. unsigned long *rss_add, unsigned long *swp_add)
  579. {
  580. struct inode *inode;
  581. inode = shp->shm_file->f_path.dentry->d_inode;
  582. if (is_file_hugepages(shp->shm_file)) {
  583. struct address_space *mapping = inode->i_mapping;
  584. struct hstate *h = hstate_file(shp->shm_file);
  585. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  586. } else {
  587. #ifdef CONFIG_SHMEM
  588. struct shmem_inode_info *info = SHMEM_I(inode);
  589. spin_lock(&info->lock);
  590. *rss_add += inode->i_mapping->nrpages;
  591. *swp_add += info->swapped;
  592. spin_unlock(&info->lock);
  593. #else
  594. *rss_add += inode->i_mapping->nrpages;
  595. #endif
  596. }
  597. }
  598. /*
  599. * Called with shm_ids.rw_mutex held as a reader
  600. */
  601. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  602. unsigned long *swp)
  603. {
  604. int next_id;
  605. int total, in_use;
  606. *rss = 0;
  607. *swp = 0;
  608. in_use = shm_ids(ns).in_use;
  609. for (total = 0, next_id = 0; total < in_use; next_id++) {
  610. struct kern_ipc_perm *ipc;
  611. struct shmid_kernel *shp;
  612. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  613. if (ipc == NULL)
  614. continue;
  615. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  616. shm_add_rss_swap(shp, rss, swp);
  617. total++;
  618. }
  619. }
  620. /*
  621. * This function handles some shmctl commands which require the rw_mutex
  622. * to be held in write mode.
  623. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  624. */
  625. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  626. struct shmid_ds __user *buf, int version)
  627. {
  628. struct kern_ipc_perm *ipcp;
  629. struct shmid64_ds shmid64;
  630. struct shmid_kernel *shp;
  631. int err;
  632. if (cmd == IPC_SET) {
  633. if (copy_shmid_from_user(&shmid64, buf, version))
  634. return -EFAULT;
  635. }
  636. ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
  637. &shmid64.shm_perm, 0);
  638. if (IS_ERR(ipcp))
  639. return PTR_ERR(ipcp);
  640. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  641. err = security_shm_shmctl(shp, cmd);
  642. if (err)
  643. goto out_unlock;
  644. switch (cmd) {
  645. case IPC_RMID:
  646. do_shm_rmid(ns, ipcp);
  647. goto out_up;
  648. case IPC_SET:
  649. ipc_update_perm(&shmid64.shm_perm, ipcp);
  650. shp->shm_ctim = get_seconds();
  651. break;
  652. default:
  653. err = -EINVAL;
  654. }
  655. out_unlock:
  656. shm_unlock(shp);
  657. out_up:
  658. up_write(&shm_ids(ns).rw_mutex);
  659. return err;
  660. }
  661. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  662. {
  663. struct shmid_kernel *shp;
  664. int err, version;
  665. struct ipc_namespace *ns;
  666. if (cmd < 0 || shmid < 0) {
  667. err = -EINVAL;
  668. goto out;
  669. }
  670. version = ipc_parse_version(&cmd);
  671. ns = current->nsproxy->ipc_ns;
  672. switch (cmd) { /* replace with proc interface ? */
  673. case IPC_INFO:
  674. {
  675. struct shminfo64 shminfo;
  676. err = security_shm_shmctl(NULL, cmd);
  677. if (err)
  678. return err;
  679. memset(&shminfo, 0, sizeof(shminfo));
  680. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  681. shminfo.shmmax = ns->shm_ctlmax;
  682. shminfo.shmall = ns->shm_ctlall;
  683. shminfo.shmmin = SHMMIN;
  684. if(copy_shminfo_to_user (buf, &shminfo, version))
  685. return -EFAULT;
  686. down_read(&shm_ids(ns).rw_mutex);
  687. err = ipc_get_maxid(&shm_ids(ns));
  688. up_read(&shm_ids(ns).rw_mutex);
  689. if(err<0)
  690. err = 0;
  691. goto out;
  692. }
  693. case SHM_INFO:
  694. {
  695. struct shm_info shm_info;
  696. err = security_shm_shmctl(NULL, cmd);
  697. if (err)
  698. return err;
  699. memset(&shm_info, 0, sizeof(shm_info));
  700. down_read(&shm_ids(ns).rw_mutex);
  701. shm_info.used_ids = shm_ids(ns).in_use;
  702. shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
  703. shm_info.shm_tot = ns->shm_tot;
  704. shm_info.swap_attempts = 0;
  705. shm_info.swap_successes = 0;
  706. err = ipc_get_maxid(&shm_ids(ns));
  707. up_read(&shm_ids(ns).rw_mutex);
  708. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  709. err = -EFAULT;
  710. goto out;
  711. }
  712. err = err < 0 ? 0 : err;
  713. goto out;
  714. }
  715. case SHM_STAT:
  716. case IPC_STAT:
  717. {
  718. struct shmid64_ds tbuf;
  719. int result;
  720. if (cmd == SHM_STAT) {
  721. shp = shm_lock(ns, shmid);
  722. if (IS_ERR(shp)) {
  723. err = PTR_ERR(shp);
  724. goto out;
  725. }
  726. result = shp->shm_perm.id;
  727. } else {
  728. shp = shm_lock_check(ns, shmid);
  729. if (IS_ERR(shp)) {
  730. err = PTR_ERR(shp);
  731. goto out;
  732. }
  733. result = 0;
  734. }
  735. err = -EACCES;
  736. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  737. goto out_unlock;
  738. err = security_shm_shmctl(shp, cmd);
  739. if (err)
  740. goto out_unlock;
  741. memset(&tbuf, 0, sizeof(tbuf));
  742. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  743. tbuf.shm_segsz = shp->shm_segsz;
  744. tbuf.shm_atime = shp->shm_atim;
  745. tbuf.shm_dtime = shp->shm_dtim;
  746. tbuf.shm_ctime = shp->shm_ctim;
  747. tbuf.shm_cpid = shp->shm_cprid;
  748. tbuf.shm_lpid = shp->shm_lprid;
  749. tbuf.shm_nattch = shp->shm_nattch;
  750. shm_unlock(shp);
  751. if(copy_shmid_to_user (buf, &tbuf, version))
  752. err = -EFAULT;
  753. else
  754. err = result;
  755. goto out;
  756. }
  757. case SHM_LOCK:
  758. case SHM_UNLOCK:
  759. {
  760. struct file *shm_file;
  761. shp = shm_lock_check(ns, shmid);
  762. if (IS_ERR(shp)) {
  763. err = PTR_ERR(shp);
  764. goto out;
  765. }
  766. audit_ipc_obj(&(shp->shm_perm));
  767. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  768. uid_t euid = current_euid();
  769. err = -EPERM;
  770. if (euid != shp->shm_perm.uid &&
  771. euid != shp->shm_perm.cuid)
  772. goto out_unlock;
  773. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
  774. goto out_unlock;
  775. }
  776. err = security_shm_shmctl(shp, cmd);
  777. if (err)
  778. goto out_unlock;
  779. shm_file = shp->shm_file;
  780. if (is_file_hugepages(shm_file))
  781. goto out_unlock;
  782. if (cmd == SHM_LOCK) {
  783. struct user_struct *user = current_user();
  784. err = shmem_lock(shm_file, 1, user);
  785. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
  786. shp->shm_perm.mode |= SHM_LOCKED;
  787. shp->mlock_user = user;
  788. }
  789. goto out_unlock;
  790. }
  791. /* SHM_UNLOCK */
  792. if (!(shp->shm_perm.mode & SHM_LOCKED))
  793. goto out_unlock;
  794. shmem_lock(shm_file, 0, shp->mlock_user);
  795. shp->shm_perm.mode &= ~SHM_LOCKED;
  796. shp->mlock_user = NULL;
  797. get_file(shm_file);
  798. shm_unlock(shp);
  799. shmem_unlock_mapping(shm_file->f_mapping);
  800. fput(shm_file);
  801. goto out;
  802. }
  803. case IPC_RMID:
  804. case IPC_SET:
  805. err = shmctl_down(ns, shmid, cmd, buf, version);
  806. return err;
  807. default:
  808. return -EINVAL;
  809. }
  810. out_unlock:
  811. shm_unlock(shp);
  812. out:
  813. return err;
  814. }
  815. /*
  816. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  817. *
  818. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  819. * "raddr" thing points to kernel space, and there has to be a wrapper around
  820. * this.
  821. */
  822. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
  823. unsigned long shmlba)
  824. {
  825. struct shmid_kernel *shp;
  826. unsigned long addr;
  827. unsigned long size;
  828. struct file * file;
  829. int err;
  830. unsigned long flags;
  831. unsigned long prot;
  832. int acc_mode;
  833. unsigned long user_addr;
  834. struct ipc_namespace *ns;
  835. struct shm_file_data *sfd;
  836. struct path path;
  837. fmode_t f_mode;
  838. err = -EINVAL;
  839. if (shmid < 0)
  840. goto out;
  841. else if ((addr = (ulong)shmaddr)) {
  842. if (addr & (shmlba - 1)) {
  843. if (shmflg & SHM_RND)
  844. addr &= ~(shmlba - 1); /* round down */
  845. else
  846. #ifndef __ARCH_FORCE_SHMLBA
  847. if (addr & ~PAGE_MASK)
  848. #endif
  849. goto out;
  850. }
  851. flags = MAP_SHARED | MAP_FIXED;
  852. } else {
  853. if ((shmflg & SHM_REMAP))
  854. goto out;
  855. flags = MAP_SHARED;
  856. }
  857. if (shmflg & SHM_RDONLY) {
  858. prot = PROT_READ;
  859. acc_mode = S_IRUGO;
  860. f_mode = FMODE_READ;
  861. } else {
  862. prot = PROT_READ | PROT_WRITE;
  863. acc_mode = S_IRUGO | S_IWUGO;
  864. f_mode = FMODE_READ | FMODE_WRITE;
  865. }
  866. if (shmflg & SHM_EXEC) {
  867. prot |= PROT_EXEC;
  868. acc_mode |= S_IXUGO;
  869. }
  870. /*
  871. * We cannot rely on the fs check since SYSV IPC does have an
  872. * additional creator id...
  873. */
  874. ns = current->nsproxy->ipc_ns;
  875. shp = shm_lock_check(ns, shmid);
  876. if (IS_ERR(shp)) {
  877. err = PTR_ERR(shp);
  878. goto out;
  879. }
  880. err = -EACCES;
  881. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  882. goto out_unlock;
  883. err = security_shm_shmat(shp, shmaddr, shmflg);
  884. if (err)
  885. goto out_unlock;
  886. path = shp->shm_file->f_path;
  887. path_get(&path);
  888. shp->shm_nattch++;
  889. size = i_size_read(path.dentry->d_inode);
  890. shm_unlock(shp);
  891. err = -ENOMEM;
  892. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  893. if (!sfd)
  894. goto out_put_dentry;
  895. file = alloc_file(&path, f_mode,
  896. is_file_hugepages(shp->shm_file) ?
  897. &shm_file_operations_huge :
  898. &shm_file_operations);
  899. if (!file)
  900. goto out_free;
  901. file->private_data = sfd;
  902. file->f_mapping = shp->shm_file->f_mapping;
  903. sfd->id = shp->shm_perm.id;
  904. sfd->ns = get_ipc_ns(ns);
  905. sfd->file = shp->shm_file;
  906. sfd->vm_ops = NULL;
  907. err = security_mmap_file(file, prot, flags);
  908. if (err)
  909. goto out_fput;
  910. down_write(&current->mm->mmap_sem);
  911. if (addr && !(shmflg & SHM_REMAP)) {
  912. err = -EINVAL;
  913. if (find_vma_intersection(current->mm, addr, addr + size))
  914. goto invalid;
  915. /*
  916. * If shm segment goes below stack, make sure there is some
  917. * space left for the stack to grow (at least 4 pages).
  918. */
  919. if (addr < current->mm->start_stack &&
  920. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  921. goto invalid;
  922. }
  923. user_addr = do_mmap_pgoff(file, addr, size, prot, flags, 0);
  924. *raddr = user_addr;
  925. err = 0;
  926. if (IS_ERR_VALUE(user_addr))
  927. err = (long)user_addr;
  928. invalid:
  929. up_write(&current->mm->mmap_sem);
  930. out_fput:
  931. fput(file);
  932. out_nattch:
  933. down_write(&shm_ids(ns).rw_mutex);
  934. shp = shm_lock(ns, shmid);
  935. BUG_ON(IS_ERR(shp));
  936. shp->shm_nattch--;
  937. if (shm_may_destroy(ns, shp))
  938. shm_destroy(ns, shp);
  939. else
  940. shm_unlock(shp);
  941. up_write(&shm_ids(ns).rw_mutex);
  942. out:
  943. return err;
  944. out_unlock:
  945. shm_unlock(shp);
  946. goto out;
  947. out_free:
  948. kfree(sfd);
  949. out_put_dentry:
  950. path_put(&path);
  951. goto out_nattch;
  952. }
  953. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  954. {
  955. unsigned long ret;
  956. long err;
  957. err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
  958. if (err)
  959. return err;
  960. force_successful_syscall_return();
  961. return (long)ret;
  962. }
  963. /*
  964. * detach and kill segment if marked destroyed.
  965. * The work is done in shm_close.
  966. */
  967. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  968. {
  969. struct mm_struct *mm = current->mm;
  970. struct vm_area_struct *vma;
  971. unsigned long addr = (unsigned long)shmaddr;
  972. int retval = -EINVAL;
  973. #ifdef CONFIG_MMU
  974. loff_t size = 0;
  975. struct vm_area_struct *next;
  976. #endif
  977. if (addr & ~PAGE_MASK)
  978. return retval;
  979. down_write(&mm->mmap_sem);
  980. /*
  981. * This function tries to be smart and unmap shm segments that
  982. * were modified by partial mlock or munmap calls:
  983. * - It first determines the size of the shm segment that should be
  984. * unmapped: It searches for a vma that is backed by shm and that
  985. * started at address shmaddr. It records it's size and then unmaps
  986. * it.
  987. * - Then it unmaps all shm vmas that started at shmaddr and that
  988. * are within the initially determined size.
  989. * Errors from do_munmap are ignored: the function only fails if
  990. * it's called with invalid parameters or if it's called to unmap
  991. * a part of a vma. Both calls in this function are for full vmas,
  992. * the parameters are directly copied from the vma itself and always
  993. * valid - therefore do_munmap cannot fail. (famous last words?)
  994. */
  995. /*
  996. * If it had been mremap()'d, the starting address would not
  997. * match the usual checks anyway. So assume all vma's are
  998. * above the starting address given.
  999. */
  1000. vma = find_vma(mm, addr);
  1001. #ifdef CONFIG_MMU
  1002. while (vma) {
  1003. next = vma->vm_next;
  1004. /*
  1005. * Check if the starting address would match, i.e. it's
  1006. * a fragment created by mprotect() and/or munmap(), or it
  1007. * otherwise it starts at this address with no hassles.
  1008. */
  1009. if ((vma->vm_ops == &shm_vm_ops) &&
  1010. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  1011. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  1012. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1013. /*
  1014. * We discovered the size of the shm segment, so
  1015. * break out of here and fall through to the next
  1016. * loop that uses the size information to stop
  1017. * searching for matching vma's.
  1018. */
  1019. retval = 0;
  1020. vma = next;
  1021. break;
  1022. }
  1023. vma = next;
  1024. }
  1025. /*
  1026. * We need look no further than the maximum address a fragment
  1027. * could possibly have landed at. Also cast things to loff_t to
  1028. * prevent overflows and make comparisons vs. equal-width types.
  1029. */
  1030. size = PAGE_ALIGN(size);
  1031. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1032. next = vma->vm_next;
  1033. /* finding a matching vma now does not alter retval */
  1034. if ((vma->vm_ops == &shm_vm_ops) &&
  1035. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  1036. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1037. vma = next;
  1038. }
  1039. #else /* CONFIG_MMU */
  1040. /* under NOMMU conditions, the exact address to be destroyed must be
  1041. * given */
  1042. retval = -EINVAL;
  1043. if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1044. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1045. retval = 0;
  1046. }
  1047. #endif
  1048. up_write(&mm->mmap_sem);
  1049. return retval;
  1050. }
  1051. #ifdef CONFIG_PROC_FS
  1052. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1053. {
  1054. struct shmid_kernel *shp = it;
  1055. unsigned long rss = 0, swp = 0;
  1056. shm_add_rss_swap(shp, &rss, &swp);
  1057. #if BITS_PER_LONG <= 32
  1058. #define SIZE_SPEC "%10lu"
  1059. #else
  1060. #define SIZE_SPEC "%21lu"
  1061. #endif
  1062. return seq_printf(s,
  1063. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1064. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
  1065. SIZE_SPEC " " SIZE_SPEC "\n",
  1066. shp->shm_perm.key,
  1067. shp->shm_perm.id,
  1068. shp->shm_perm.mode,
  1069. shp->shm_segsz,
  1070. shp->shm_cprid,
  1071. shp->shm_lprid,
  1072. shp->shm_nattch,
  1073. shp->shm_perm.uid,
  1074. shp->shm_perm.gid,
  1075. shp->shm_perm.cuid,
  1076. shp->shm_perm.cgid,
  1077. shp->shm_atim,
  1078. shp->shm_dtim,
  1079. shp->shm_ctim,
  1080. rss * PAGE_SIZE,
  1081. swp * PAGE_SIZE);
  1082. }
  1083. #endif