shm.c 30 KB

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