shm.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/ipc/shm.c
  4. * Copyright (C) 1992, 1993 Krishna Balasubramanian
  5. * Many improvements/fixes by Bruno Haible.
  6. * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
  7. * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
  8. *
  9. * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  10. * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
  11. * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
  12. * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
  13. * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
  14. * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  15. * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  16. *
  17. * support for audit of ipc object properties and permission changes
  18. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  19. *
  20. * namespaces support
  21. * OpenVZ, SWsoft Inc.
  22. * Pavel Emelianov <xemul@openvz.org>
  23. *
  24. * Better ipc lock (kern_ipc_perm.lock) handling
  25. * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
  26. */
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/hugetlb.h>
  30. #include <linux/shm.h>
  31. #include <linux/init.h>
  32. #include <linux/file.h>
  33. #include <linux/mman.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/security.h>
  36. #include <linux/syscalls.h>
  37. #include <linux/audit.h>
  38. #include <linux/capability.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/seq_file.h>
  41. #include <linux/rwsem.h>
  42. #include <linux/nsproxy.h>
  43. #include <linux/mount.h>
  44. #include <linux/ipc_namespace.h>
  45. #include <linux/uaccess.h>
  46. #include "util.h"
  47. struct shm_file_data {
  48. int id;
  49. struct ipc_namespace *ns;
  50. struct file *file;
  51. const struct vm_operations_struct *vm_ops;
  52. };
  53. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  54. static const struct file_operations shm_file_operations;
  55. static const struct vm_operations_struct shm_vm_ops;
  56. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  57. #define shm_unlock(shp) \
  58. ipc_unlock(&(shp)->shm_perm)
  59. static int newseg(struct ipc_namespace *, struct ipc_params *);
  60. static void shm_open(struct vm_area_struct *vma);
  61. static void shm_close(struct vm_area_struct *vma);
  62. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
  63. #ifdef CONFIG_PROC_FS
  64. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  65. #endif
  66. int shm_init_ns(struct ipc_namespace *ns)
  67. {
  68. ns->shm_ctlmax = SHMMAX;
  69. ns->shm_ctlall = SHMALL;
  70. ns->shm_ctlmni = SHMMNI;
  71. ns->shm_rmid_forced = 0;
  72. ns->shm_tot = 0;
  73. return ipc_init_ids(&shm_ids(ns));
  74. }
  75. /*
  76. * Called with shm_ids.rwsem (writer) and the shp structure locked.
  77. * Only shm_ids.rwsem remains locked on exit.
  78. */
  79. static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  80. {
  81. struct shmid_kernel *shp;
  82. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  83. if (shp->shm_nattch) {
  84. shp->shm_perm.mode |= SHM_DEST;
  85. /* Do not find it any more */
  86. ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
  87. shm_unlock(shp);
  88. } else
  89. shm_destroy(ns, shp);
  90. }
  91. #ifdef CONFIG_IPC_NS
  92. void shm_exit_ns(struct ipc_namespace *ns)
  93. {
  94. free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
  95. idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
  96. rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
  97. }
  98. #endif
  99. static int __init ipc_ns_init(void)
  100. {
  101. const int err = shm_init_ns(&init_ipc_ns);
  102. WARN(err, "ipc: sysv shm_init_ns failed: %d\n", err);
  103. return err;
  104. }
  105. pure_initcall(ipc_ns_init);
  106. void __init shm_init(void)
  107. {
  108. ipc_init_proc_interface("sysvipc/shm",
  109. #if BITS_PER_LONG <= 32
  110. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  111. #else
  112. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  113. #endif
  114. IPC_SHM_IDS, sysvipc_shm_proc_show);
  115. }
  116. static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
  117. {
  118. struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
  119. if (IS_ERR(ipcp))
  120. return ERR_CAST(ipcp);
  121. return container_of(ipcp, struct shmid_kernel, shm_perm);
  122. }
  123. static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
  124. {
  125. struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
  126. if (IS_ERR(ipcp))
  127. return ERR_CAST(ipcp);
  128. return container_of(ipcp, struct shmid_kernel, shm_perm);
  129. }
  130. /*
  131. * shm_lock_(check_) routines are called in the paths where the rwsem
  132. * is not necessarily held.
  133. */
  134. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  135. {
  136. struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
  137. /*
  138. * Callers of shm_lock() must validate the status of the returned ipc
  139. * object pointer (as returned by ipc_lock()), and error out as
  140. * appropriate.
  141. */
  142. if (IS_ERR(ipcp))
  143. return (void *)ipcp;
  144. return container_of(ipcp, struct shmid_kernel, shm_perm);
  145. }
  146. static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
  147. {
  148. rcu_read_lock();
  149. ipc_lock_object(&ipcp->shm_perm);
  150. }
  151. static void shm_rcu_free(struct rcu_head *head)
  152. {
  153. struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
  154. rcu);
  155. struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
  156. shm_perm);
  157. security_shm_free(shp);
  158. kvfree(shp);
  159. }
  160. static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
  161. {
  162. list_del(&s->shm_clist);
  163. ipc_rmid(&shm_ids(ns), &s->shm_perm);
  164. }
  165. static int __shm_open(struct vm_area_struct *vma)
  166. {
  167. struct file *file = vma->vm_file;
  168. struct shm_file_data *sfd = shm_file_data(file);
  169. struct shmid_kernel *shp;
  170. shp = shm_lock(sfd->ns, sfd->id);
  171. if (IS_ERR(shp))
  172. return PTR_ERR(shp);
  173. shp->shm_atim = ktime_get_real_seconds();
  174. shp->shm_lprid = task_tgid_vnr(current);
  175. shp->shm_nattch++;
  176. shm_unlock(shp);
  177. return 0;
  178. }
  179. /* This is called by fork, once for every shm attach. */
  180. static void shm_open(struct vm_area_struct *vma)
  181. {
  182. int err = __shm_open(vma);
  183. /*
  184. * We raced in the idr lookup or with shm_destroy().
  185. * Either way, the ID is busted.
  186. */
  187. WARN_ON_ONCE(err);
  188. }
  189. /*
  190. * shm_destroy - free the struct shmid_kernel
  191. *
  192. * @ns: namespace
  193. * @shp: struct to free
  194. *
  195. * It has to be called with shp and shm_ids.rwsem (writer) locked,
  196. * but returns with shp unlocked and freed.
  197. */
  198. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  199. {
  200. struct file *shm_file;
  201. shm_file = shp->shm_file;
  202. shp->shm_file = NULL;
  203. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  204. shm_rmid(ns, shp);
  205. shm_unlock(shp);
  206. if (!is_file_hugepages(shm_file))
  207. shmem_lock(shm_file, 0, shp->mlock_user);
  208. else if (shp->mlock_user)
  209. user_shm_unlock(i_size_read(file_inode(shm_file)),
  210. shp->mlock_user);
  211. fput(shm_file);
  212. ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
  213. }
  214. /*
  215. * shm_may_destroy - identifies whether shm segment should be destroyed now
  216. *
  217. * Returns true if and only if there are no active users of the segment and
  218. * one of the following is true:
  219. *
  220. * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
  221. *
  222. * 2) sysctl kernel.shm_rmid_forced is set to 1.
  223. */
  224. static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  225. {
  226. return (shp->shm_nattch == 0) &&
  227. (ns->shm_rmid_forced ||
  228. (shp->shm_perm.mode & SHM_DEST));
  229. }
  230. /*
  231. * remove the attach descriptor vma.
  232. * free memory for segment if it is marked destroyed.
  233. * The descriptor has already been removed from the current->mm->mmap list
  234. * and will later be kfree()d.
  235. */
  236. static void shm_close(struct vm_area_struct *vma)
  237. {
  238. struct file *file = vma->vm_file;
  239. struct shm_file_data *sfd = shm_file_data(file);
  240. struct shmid_kernel *shp;
  241. struct ipc_namespace *ns = sfd->ns;
  242. down_write(&shm_ids(ns).rwsem);
  243. /* remove from the list of attaches of the shm segment */
  244. shp = shm_lock(ns, sfd->id);
  245. /*
  246. * We raced in the idr lookup or with shm_destroy().
  247. * Either way, the ID is busted.
  248. */
  249. if (WARN_ON_ONCE(IS_ERR(shp)))
  250. goto done; /* no-op */
  251. shp->shm_lprid = task_tgid_vnr(current);
  252. shp->shm_dtim = ktime_get_real_seconds();
  253. shp->shm_nattch--;
  254. if (shm_may_destroy(ns, shp))
  255. shm_destroy(ns, shp);
  256. else
  257. shm_unlock(shp);
  258. done:
  259. up_write(&shm_ids(ns).rwsem);
  260. }
  261. /* Called with ns->shm_ids(ns).rwsem locked */
  262. static int shm_try_destroy_orphaned(int id, void *p, void *data)
  263. {
  264. struct ipc_namespace *ns = data;
  265. struct kern_ipc_perm *ipcp = p;
  266. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  267. /*
  268. * We want to destroy segments without users and with already
  269. * exit'ed originating process.
  270. *
  271. * As shp->* are changed under rwsem, it's safe to skip shp locking.
  272. */
  273. if (shp->shm_creator != NULL)
  274. return 0;
  275. if (shm_may_destroy(ns, shp)) {
  276. shm_lock_by_ptr(shp);
  277. shm_destroy(ns, shp);
  278. }
  279. return 0;
  280. }
  281. void shm_destroy_orphaned(struct ipc_namespace *ns)
  282. {
  283. down_write(&shm_ids(ns).rwsem);
  284. if (shm_ids(ns).in_use)
  285. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
  286. up_write(&shm_ids(ns).rwsem);
  287. }
  288. /* Locking assumes this will only be called with task == current */
  289. void exit_shm(struct task_struct *task)
  290. {
  291. struct ipc_namespace *ns = task->nsproxy->ipc_ns;
  292. struct shmid_kernel *shp, *n;
  293. if (list_empty(&task->sysvshm.shm_clist))
  294. return;
  295. /*
  296. * If kernel.shm_rmid_forced is not set then only keep track of
  297. * which shmids are orphaned, so that a later set of the sysctl
  298. * can clean them up.
  299. */
  300. if (!ns->shm_rmid_forced) {
  301. down_read(&shm_ids(ns).rwsem);
  302. list_for_each_entry(shp, &task->sysvshm.shm_clist, shm_clist)
  303. shp->shm_creator = NULL;
  304. /*
  305. * Only under read lock but we are only called on current
  306. * so no entry on the list will be shared.
  307. */
  308. list_del(&task->sysvshm.shm_clist);
  309. up_read(&shm_ids(ns).rwsem);
  310. return;
  311. }
  312. /*
  313. * Destroy all already created segments, that were not yet mapped,
  314. * and mark any mapped as orphan to cover the sysctl toggling.
  315. * Destroy is skipped if shm_may_destroy() returns false.
  316. */
  317. down_write(&shm_ids(ns).rwsem);
  318. list_for_each_entry_safe(shp, n, &task->sysvshm.shm_clist, shm_clist) {
  319. shp->shm_creator = NULL;
  320. if (shm_may_destroy(ns, shp)) {
  321. shm_lock_by_ptr(shp);
  322. shm_destroy(ns, shp);
  323. }
  324. }
  325. /* Remove the list head from any segments still attached. */
  326. list_del(&task->sysvshm.shm_clist);
  327. up_write(&shm_ids(ns).rwsem);
  328. }
  329. static int shm_fault(struct vm_fault *vmf)
  330. {
  331. struct file *file = vmf->vma->vm_file;
  332. struct shm_file_data *sfd = shm_file_data(file);
  333. return sfd->vm_ops->fault(vmf);
  334. }
  335. #ifdef CONFIG_NUMA
  336. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  337. {
  338. struct file *file = vma->vm_file;
  339. struct shm_file_data *sfd = shm_file_data(file);
  340. int err = 0;
  341. if (sfd->vm_ops->set_policy)
  342. err = sfd->vm_ops->set_policy(vma, new);
  343. return err;
  344. }
  345. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  346. unsigned long addr)
  347. {
  348. struct file *file = vma->vm_file;
  349. struct shm_file_data *sfd = shm_file_data(file);
  350. struct mempolicy *pol = NULL;
  351. if (sfd->vm_ops->get_policy)
  352. pol = sfd->vm_ops->get_policy(vma, addr);
  353. else if (vma->vm_policy)
  354. pol = vma->vm_policy;
  355. return pol;
  356. }
  357. #endif
  358. static int shm_mmap(struct file *file, struct vm_area_struct *vma)
  359. {
  360. struct shm_file_data *sfd = shm_file_data(file);
  361. int ret;
  362. /*
  363. * In case of remap_file_pages() emulation, the file can represent
  364. * removed IPC ID: propogate shm_lock() error to caller.
  365. */
  366. ret = __shm_open(vma);
  367. if (ret)
  368. return ret;
  369. ret = call_mmap(sfd->file, vma);
  370. if (ret) {
  371. shm_close(vma);
  372. return ret;
  373. }
  374. sfd->vm_ops = vma->vm_ops;
  375. #ifdef CONFIG_MMU
  376. WARN_ON(!sfd->vm_ops->fault);
  377. #endif
  378. vma->vm_ops = &shm_vm_ops;
  379. return 0;
  380. }
  381. static int shm_release(struct inode *ino, struct file *file)
  382. {
  383. struct shm_file_data *sfd = shm_file_data(file);
  384. put_ipc_ns(sfd->ns);
  385. shm_file_data(file) = NULL;
  386. kfree(sfd);
  387. return 0;
  388. }
  389. static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  390. {
  391. struct shm_file_data *sfd = shm_file_data(file);
  392. if (!sfd->file->f_op->fsync)
  393. return -EINVAL;
  394. return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
  395. }
  396. static long shm_fallocate(struct file *file, int mode, loff_t offset,
  397. loff_t len)
  398. {
  399. struct shm_file_data *sfd = shm_file_data(file);
  400. if (!sfd->file->f_op->fallocate)
  401. return -EOPNOTSUPP;
  402. return sfd->file->f_op->fallocate(file, mode, offset, len);
  403. }
  404. static unsigned long shm_get_unmapped_area(struct file *file,
  405. unsigned long addr, unsigned long len, unsigned long pgoff,
  406. unsigned long flags)
  407. {
  408. struct shm_file_data *sfd = shm_file_data(file);
  409. return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
  410. pgoff, flags);
  411. }
  412. static const struct file_operations shm_file_operations = {
  413. .mmap = shm_mmap,
  414. .fsync = shm_fsync,
  415. .release = shm_release,
  416. .get_unmapped_area = shm_get_unmapped_area,
  417. .llseek = noop_llseek,
  418. .fallocate = shm_fallocate,
  419. };
  420. /*
  421. * shm_file_operations_huge is now identical to shm_file_operations,
  422. * but we keep it distinct for the sake of is_file_shm_hugepages().
  423. */
  424. static const struct file_operations shm_file_operations_huge = {
  425. .mmap = shm_mmap,
  426. .fsync = shm_fsync,
  427. .release = shm_release,
  428. .get_unmapped_area = shm_get_unmapped_area,
  429. .llseek = noop_llseek,
  430. .fallocate = shm_fallocate,
  431. };
  432. bool is_file_shm_hugepages(struct file *file)
  433. {
  434. return file->f_op == &shm_file_operations_huge;
  435. }
  436. static const struct vm_operations_struct shm_vm_ops = {
  437. .open = shm_open, /* callback for a new vm-area open */
  438. .close = shm_close, /* callback for when the vm-area is released */
  439. .fault = shm_fault,
  440. #if defined(CONFIG_NUMA)
  441. .set_policy = shm_set_policy,
  442. .get_policy = shm_get_policy,
  443. #endif
  444. };
  445. /**
  446. * newseg - Create a new shared memory segment
  447. * @ns: namespace
  448. * @params: ptr to the structure that contains key, size and shmflg
  449. *
  450. * Called with shm_ids.rwsem held as a writer.
  451. */
  452. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  453. {
  454. key_t key = params->key;
  455. int shmflg = params->flg;
  456. size_t size = params->u.size;
  457. int error;
  458. struct shmid_kernel *shp;
  459. size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  460. struct file *file;
  461. char name[13];
  462. vm_flags_t acctflag = 0;
  463. if (size < SHMMIN || size > ns->shm_ctlmax)
  464. return -EINVAL;
  465. if (numpages << PAGE_SHIFT < size)
  466. return -ENOSPC;
  467. if (ns->shm_tot + numpages < ns->shm_tot ||
  468. ns->shm_tot + numpages > ns->shm_ctlall)
  469. return -ENOSPC;
  470. shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
  471. if (unlikely(!shp))
  472. return -ENOMEM;
  473. shp->shm_perm.key = key;
  474. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  475. shp->mlock_user = NULL;
  476. shp->shm_perm.security = NULL;
  477. error = security_shm_alloc(shp);
  478. if (error) {
  479. kvfree(shp);
  480. return error;
  481. }
  482. sprintf(name, "SYSV%08x", key);
  483. if (shmflg & SHM_HUGETLB) {
  484. struct hstate *hs;
  485. size_t hugesize;
  486. hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
  487. if (!hs) {
  488. error = -EINVAL;
  489. goto no_file;
  490. }
  491. hugesize = ALIGN(size, huge_page_size(hs));
  492. /* hugetlb_file_setup applies strict accounting */
  493. if (shmflg & SHM_NORESERVE)
  494. acctflag = VM_NORESERVE;
  495. file = hugetlb_file_setup(name, hugesize, acctflag,
  496. &shp->mlock_user, HUGETLB_SHMFS_INODE,
  497. (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
  498. } else {
  499. /*
  500. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  501. * if it's asked for.
  502. */
  503. if ((shmflg & SHM_NORESERVE) &&
  504. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  505. acctflag = VM_NORESERVE;
  506. file = shmem_kernel_file_setup(name, size, acctflag);
  507. }
  508. error = PTR_ERR(file);
  509. if (IS_ERR(file))
  510. goto no_file;
  511. shp->shm_cprid = task_tgid_vnr(current);
  512. shp->shm_lprid = 0;
  513. shp->shm_atim = shp->shm_dtim = 0;
  514. shp->shm_ctim = ktime_get_real_seconds();
  515. shp->shm_segsz = size;
  516. shp->shm_nattch = 0;
  517. shp->shm_file = file;
  518. shp->shm_creator = current;
  519. /* ipc_addid() locks shp upon success. */
  520. error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  521. if (error < 0)
  522. goto no_id;
  523. list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
  524. /*
  525. * shmid gets reported as "inode#" in /proc/pid/maps.
  526. * proc-ps tools use this. Changing this will break them.
  527. */
  528. file_inode(file)->i_ino = shp->shm_perm.id;
  529. ns->shm_tot += numpages;
  530. error = shp->shm_perm.id;
  531. ipc_unlock_object(&shp->shm_perm);
  532. rcu_read_unlock();
  533. return error;
  534. no_id:
  535. if (is_file_hugepages(file) && shp->mlock_user)
  536. user_shm_unlock(size, shp->mlock_user);
  537. fput(file);
  538. no_file:
  539. call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
  540. return error;
  541. }
  542. /*
  543. * Called with shm_ids.rwsem and ipcp locked.
  544. */
  545. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  546. {
  547. struct shmid_kernel *shp;
  548. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  549. return security_shm_associate(shp, shmflg);
  550. }
  551. /*
  552. * Called with shm_ids.rwsem and ipcp locked.
  553. */
  554. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  555. struct ipc_params *params)
  556. {
  557. struct shmid_kernel *shp;
  558. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  559. if (shp->shm_segsz < params->u.size)
  560. return -EINVAL;
  561. return 0;
  562. }
  563. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  564. {
  565. struct ipc_namespace *ns;
  566. static const struct ipc_ops shm_ops = {
  567. .getnew = newseg,
  568. .associate = shm_security,
  569. .more_checks = shm_more_checks,
  570. };
  571. struct ipc_params shm_params;
  572. ns = current->nsproxy->ipc_ns;
  573. shm_params.key = key;
  574. shm_params.flg = shmflg;
  575. shm_params.u.size = size;
  576. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  577. }
  578. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  579. {
  580. switch (version) {
  581. case IPC_64:
  582. return copy_to_user(buf, in, sizeof(*in));
  583. case IPC_OLD:
  584. {
  585. struct shmid_ds out;
  586. memset(&out, 0, sizeof(out));
  587. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  588. out.shm_segsz = in->shm_segsz;
  589. out.shm_atime = in->shm_atime;
  590. out.shm_dtime = in->shm_dtime;
  591. out.shm_ctime = in->shm_ctime;
  592. out.shm_cpid = in->shm_cpid;
  593. out.shm_lpid = in->shm_lpid;
  594. out.shm_nattch = in->shm_nattch;
  595. return copy_to_user(buf, &out, sizeof(out));
  596. }
  597. default:
  598. return -EINVAL;
  599. }
  600. }
  601. static inline unsigned long
  602. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  603. {
  604. switch (version) {
  605. case IPC_64:
  606. if (copy_from_user(out, buf, sizeof(*out)))
  607. return -EFAULT;
  608. return 0;
  609. case IPC_OLD:
  610. {
  611. struct shmid_ds tbuf_old;
  612. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  613. return -EFAULT;
  614. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  615. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  616. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  617. return 0;
  618. }
  619. default:
  620. return -EINVAL;
  621. }
  622. }
  623. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  624. {
  625. switch (version) {
  626. case IPC_64:
  627. return copy_to_user(buf, in, sizeof(*in));
  628. case IPC_OLD:
  629. {
  630. struct shminfo out;
  631. if (in->shmmax > INT_MAX)
  632. out.shmmax = INT_MAX;
  633. else
  634. out.shmmax = (int)in->shmmax;
  635. out.shmmin = in->shmmin;
  636. out.shmmni = in->shmmni;
  637. out.shmseg = in->shmseg;
  638. out.shmall = in->shmall;
  639. return copy_to_user(buf, &out, sizeof(out));
  640. }
  641. default:
  642. return -EINVAL;
  643. }
  644. }
  645. /*
  646. * Calculate and add used RSS and swap pages of a shm.
  647. * Called with shm_ids.rwsem held as a reader
  648. */
  649. static void shm_add_rss_swap(struct shmid_kernel *shp,
  650. unsigned long *rss_add, unsigned long *swp_add)
  651. {
  652. struct inode *inode;
  653. inode = file_inode(shp->shm_file);
  654. if (is_file_hugepages(shp->shm_file)) {
  655. struct address_space *mapping = inode->i_mapping;
  656. struct hstate *h = hstate_file(shp->shm_file);
  657. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  658. } else {
  659. #ifdef CONFIG_SHMEM
  660. struct shmem_inode_info *info = SHMEM_I(inode);
  661. spin_lock_irq(&info->lock);
  662. *rss_add += inode->i_mapping->nrpages;
  663. *swp_add += info->swapped;
  664. spin_unlock_irq(&info->lock);
  665. #else
  666. *rss_add += inode->i_mapping->nrpages;
  667. #endif
  668. }
  669. }
  670. /*
  671. * Called with shm_ids.rwsem held as a reader
  672. */
  673. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  674. unsigned long *swp)
  675. {
  676. int next_id;
  677. int total, in_use;
  678. *rss = 0;
  679. *swp = 0;
  680. in_use = shm_ids(ns).in_use;
  681. for (total = 0, next_id = 0; total < in_use; next_id++) {
  682. struct kern_ipc_perm *ipc;
  683. struct shmid_kernel *shp;
  684. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  685. if (ipc == NULL)
  686. continue;
  687. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  688. shm_add_rss_swap(shp, rss, swp);
  689. total++;
  690. }
  691. }
  692. /*
  693. * This function handles some shmctl commands which require the rwsem
  694. * to be held in write mode.
  695. * NOTE: no locks must be held, the rwsem is taken inside this function.
  696. */
  697. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  698. struct shmid64_ds *shmid64)
  699. {
  700. struct kern_ipc_perm *ipcp;
  701. struct shmid_kernel *shp;
  702. int err;
  703. down_write(&shm_ids(ns).rwsem);
  704. rcu_read_lock();
  705. ipcp = ipcctl_pre_down_nolock(ns, &shm_ids(ns), shmid, cmd,
  706. &shmid64->shm_perm, 0);
  707. if (IS_ERR(ipcp)) {
  708. err = PTR_ERR(ipcp);
  709. goto out_unlock1;
  710. }
  711. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  712. err = security_shm_shmctl(shp, cmd);
  713. if (err)
  714. goto out_unlock1;
  715. switch (cmd) {
  716. case IPC_RMID:
  717. ipc_lock_object(&shp->shm_perm);
  718. /* do_shm_rmid unlocks the ipc object and rcu */
  719. do_shm_rmid(ns, ipcp);
  720. goto out_up;
  721. case IPC_SET:
  722. ipc_lock_object(&shp->shm_perm);
  723. err = ipc_update_perm(&shmid64->shm_perm, ipcp);
  724. if (err)
  725. goto out_unlock0;
  726. shp->shm_ctim = ktime_get_real_seconds();
  727. break;
  728. default:
  729. err = -EINVAL;
  730. goto out_unlock1;
  731. }
  732. out_unlock0:
  733. ipc_unlock_object(&shp->shm_perm);
  734. out_unlock1:
  735. rcu_read_unlock();
  736. out_up:
  737. up_write(&shm_ids(ns).rwsem);
  738. return err;
  739. }
  740. static int shmctl_ipc_info(struct ipc_namespace *ns,
  741. struct shminfo64 *shminfo)
  742. {
  743. int err = security_shm_shmctl(NULL, IPC_INFO);
  744. if (!err) {
  745. memset(shminfo, 0, sizeof(*shminfo));
  746. shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni;
  747. shminfo->shmmax = ns->shm_ctlmax;
  748. shminfo->shmall = ns->shm_ctlall;
  749. shminfo->shmmin = SHMMIN;
  750. down_read(&shm_ids(ns).rwsem);
  751. err = ipc_get_maxid(&shm_ids(ns));
  752. up_read(&shm_ids(ns).rwsem);
  753. if (err < 0)
  754. err = 0;
  755. }
  756. return err;
  757. }
  758. static int shmctl_shm_info(struct ipc_namespace *ns,
  759. struct shm_info *shm_info)
  760. {
  761. int err = security_shm_shmctl(NULL, SHM_INFO);
  762. if (!err) {
  763. memset(shm_info, 0, sizeof(*shm_info));
  764. down_read(&shm_ids(ns).rwsem);
  765. shm_info->used_ids = shm_ids(ns).in_use;
  766. shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp);
  767. shm_info->shm_tot = ns->shm_tot;
  768. shm_info->swap_attempts = 0;
  769. shm_info->swap_successes = 0;
  770. err = ipc_get_maxid(&shm_ids(ns));
  771. up_read(&shm_ids(ns).rwsem);
  772. if (err < 0)
  773. err = 0;
  774. }
  775. return err;
  776. }
  777. static int shmctl_stat(struct ipc_namespace *ns, int shmid,
  778. int cmd, struct shmid64_ds *tbuf)
  779. {
  780. struct shmid_kernel *shp;
  781. int id = 0;
  782. int err;
  783. memset(tbuf, 0, sizeof(*tbuf));
  784. rcu_read_lock();
  785. if (cmd == SHM_STAT) {
  786. shp = shm_obtain_object(ns, shmid);
  787. if (IS_ERR(shp)) {
  788. err = PTR_ERR(shp);
  789. goto out_unlock;
  790. }
  791. id = shp->shm_perm.id;
  792. } else {
  793. shp = shm_obtain_object_check(ns, shmid);
  794. if (IS_ERR(shp)) {
  795. err = PTR_ERR(shp);
  796. goto out_unlock;
  797. }
  798. }
  799. err = -EACCES;
  800. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  801. goto out_unlock;
  802. err = security_shm_shmctl(shp, cmd);
  803. if (err)
  804. goto out_unlock;
  805. ipc_lock_object(&shp->shm_perm);
  806. if (!ipc_valid_object(&shp->shm_perm)) {
  807. ipc_unlock_object(&shp->shm_perm);
  808. err = -EIDRM;
  809. goto out_unlock;
  810. }
  811. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm);
  812. tbuf->shm_segsz = shp->shm_segsz;
  813. tbuf->shm_atime = shp->shm_atim;
  814. tbuf->shm_dtime = shp->shm_dtim;
  815. tbuf->shm_ctime = shp->shm_ctim;
  816. tbuf->shm_cpid = shp->shm_cprid;
  817. tbuf->shm_lpid = shp->shm_lprid;
  818. tbuf->shm_nattch = shp->shm_nattch;
  819. ipc_unlock_object(&shp->shm_perm);
  820. rcu_read_unlock();
  821. return id;
  822. out_unlock:
  823. rcu_read_unlock();
  824. return err;
  825. }
  826. static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd)
  827. {
  828. struct shmid_kernel *shp;
  829. struct file *shm_file;
  830. int err;
  831. rcu_read_lock();
  832. shp = shm_obtain_object_check(ns, shmid);
  833. if (IS_ERR(shp)) {
  834. err = PTR_ERR(shp);
  835. goto out_unlock1;
  836. }
  837. audit_ipc_obj(&(shp->shm_perm));
  838. err = security_shm_shmctl(shp, cmd);
  839. if (err)
  840. goto out_unlock1;
  841. ipc_lock_object(&shp->shm_perm);
  842. /* check if shm_destroy() is tearing down shp */
  843. if (!ipc_valid_object(&shp->shm_perm)) {
  844. err = -EIDRM;
  845. goto out_unlock0;
  846. }
  847. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  848. kuid_t euid = current_euid();
  849. if (!uid_eq(euid, shp->shm_perm.uid) &&
  850. !uid_eq(euid, shp->shm_perm.cuid)) {
  851. err = -EPERM;
  852. goto out_unlock0;
  853. }
  854. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
  855. err = -EPERM;
  856. goto out_unlock0;
  857. }
  858. }
  859. shm_file = shp->shm_file;
  860. if (is_file_hugepages(shm_file))
  861. goto out_unlock0;
  862. if (cmd == SHM_LOCK) {
  863. struct user_struct *user = current_user();
  864. err = shmem_lock(shm_file, 1, user);
  865. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
  866. shp->shm_perm.mode |= SHM_LOCKED;
  867. shp->mlock_user = user;
  868. }
  869. goto out_unlock0;
  870. }
  871. /* SHM_UNLOCK */
  872. if (!(shp->shm_perm.mode & SHM_LOCKED))
  873. goto out_unlock0;
  874. shmem_lock(shm_file, 0, shp->mlock_user);
  875. shp->shm_perm.mode &= ~SHM_LOCKED;
  876. shp->mlock_user = NULL;
  877. get_file(shm_file);
  878. ipc_unlock_object(&shp->shm_perm);
  879. rcu_read_unlock();
  880. shmem_unlock_mapping(shm_file->f_mapping);
  881. fput(shm_file);
  882. return err;
  883. out_unlock0:
  884. ipc_unlock_object(&shp->shm_perm);
  885. out_unlock1:
  886. rcu_read_unlock();
  887. return err;
  888. }
  889. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  890. {
  891. int err, version;
  892. struct ipc_namespace *ns;
  893. struct shmid64_ds sem64;
  894. if (cmd < 0 || shmid < 0)
  895. return -EINVAL;
  896. version = ipc_parse_version(&cmd);
  897. ns = current->nsproxy->ipc_ns;
  898. switch (cmd) {
  899. case IPC_INFO: {
  900. struct shminfo64 shminfo;
  901. err = shmctl_ipc_info(ns, &shminfo);
  902. if (err < 0)
  903. return err;
  904. if (copy_shminfo_to_user(buf, &shminfo, version))
  905. err = -EFAULT;
  906. return err;
  907. }
  908. case SHM_INFO: {
  909. struct shm_info shm_info;
  910. err = shmctl_shm_info(ns, &shm_info);
  911. if (err < 0)
  912. return err;
  913. if (copy_to_user(buf, &shm_info, sizeof(shm_info)))
  914. err = -EFAULT;
  915. return err;
  916. }
  917. case SHM_STAT:
  918. case IPC_STAT: {
  919. err = shmctl_stat(ns, shmid, cmd, &sem64);
  920. if (err < 0)
  921. return err;
  922. if (copy_shmid_to_user(buf, &sem64, version))
  923. err = -EFAULT;
  924. return err;
  925. }
  926. case IPC_SET:
  927. if (copy_shmid_from_user(&sem64, buf, version))
  928. return -EFAULT;
  929. /* fallthru */
  930. case IPC_RMID:
  931. return shmctl_down(ns, shmid, cmd, &sem64);
  932. case SHM_LOCK:
  933. case SHM_UNLOCK:
  934. return shmctl_do_lock(ns, shmid, cmd);
  935. default:
  936. return -EINVAL;
  937. }
  938. }
  939. #ifdef CONFIG_COMPAT
  940. struct compat_shmid_ds {
  941. struct compat_ipc_perm shm_perm;
  942. int shm_segsz;
  943. compat_time_t shm_atime;
  944. compat_time_t shm_dtime;
  945. compat_time_t shm_ctime;
  946. compat_ipc_pid_t shm_cpid;
  947. compat_ipc_pid_t shm_lpid;
  948. unsigned short shm_nattch;
  949. unsigned short shm_unused;
  950. compat_uptr_t shm_unused2;
  951. compat_uptr_t shm_unused3;
  952. };
  953. struct compat_shminfo64 {
  954. compat_ulong_t shmmax;
  955. compat_ulong_t shmmin;
  956. compat_ulong_t shmmni;
  957. compat_ulong_t shmseg;
  958. compat_ulong_t shmall;
  959. compat_ulong_t __unused1;
  960. compat_ulong_t __unused2;
  961. compat_ulong_t __unused3;
  962. compat_ulong_t __unused4;
  963. };
  964. struct compat_shm_info {
  965. compat_int_t used_ids;
  966. compat_ulong_t shm_tot, shm_rss, shm_swp;
  967. compat_ulong_t swap_attempts, swap_successes;
  968. };
  969. static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
  970. int version)
  971. {
  972. if (in->shmmax > INT_MAX)
  973. in->shmmax = INT_MAX;
  974. if (version == IPC_64) {
  975. struct compat_shminfo64 info;
  976. memset(&info, 0, sizeof(info));
  977. info.shmmax = in->shmmax;
  978. info.shmmin = in->shmmin;
  979. info.shmmni = in->shmmni;
  980. info.shmseg = in->shmseg;
  981. info.shmall = in->shmall;
  982. return copy_to_user(buf, &info, sizeof(info));
  983. } else {
  984. struct shminfo info;
  985. memset(&info, 0, sizeof(info));
  986. info.shmmax = in->shmmax;
  987. info.shmmin = in->shmmin;
  988. info.shmmni = in->shmmni;
  989. info.shmseg = in->shmseg;
  990. info.shmall = in->shmall;
  991. return copy_to_user(buf, &info, sizeof(info));
  992. }
  993. }
  994. static int put_compat_shm_info(struct shm_info *ip,
  995. struct compat_shm_info __user *uip)
  996. {
  997. struct compat_shm_info info;
  998. memset(&info, 0, sizeof(info));
  999. info.used_ids = ip->used_ids;
  1000. info.shm_tot = ip->shm_tot;
  1001. info.shm_rss = ip->shm_rss;
  1002. info.shm_swp = ip->shm_swp;
  1003. info.swap_attempts = ip->swap_attempts;
  1004. info.swap_successes = ip->swap_successes;
  1005. return copy_to_user(uip, &info, sizeof(info));
  1006. }
  1007. static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in,
  1008. int version)
  1009. {
  1010. if (version == IPC_64) {
  1011. struct compat_shmid64_ds v;
  1012. memset(&v, 0, sizeof(v));
  1013. to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm);
  1014. v.shm_atime = in->shm_atime;
  1015. v.shm_dtime = in->shm_dtime;
  1016. v.shm_ctime = in->shm_ctime;
  1017. v.shm_segsz = in->shm_segsz;
  1018. v.shm_nattch = in->shm_nattch;
  1019. v.shm_cpid = in->shm_cpid;
  1020. v.shm_lpid = in->shm_lpid;
  1021. return copy_to_user(buf, &v, sizeof(v));
  1022. } else {
  1023. struct compat_shmid_ds v;
  1024. memset(&v, 0, sizeof(v));
  1025. to_compat_ipc_perm(&v.shm_perm, &in->shm_perm);
  1026. v.shm_perm.key = in->shm_perm.key;
  1027. v.shm_atime = in->shm_atime;
  1028. v.shm_dtime = in->shm_dtime;
  1029. v.shm_ctime = in->shm_ctime;
  1030. v.shm_segsz = in->shm_segsz;
  1031. v.shm_nattch = in->shm_nattch;
  1032. v.shm_cpid = in->shm_cpid;
  1033. v.shm_lpid = in->shm_lpid;
  1034. return copy_to_user(buf, &v, sizeof(v));
  1035. }
  1036. }
  1037. static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf,
  1038. int version)
  1039. {
  1040. memset(out, 0, sizeof(*out));
  1041. if (version == IPC_64) {
  1042. struct compat_shmid64_ds __user *p = buf;
  1043. return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm);
  1044. } else {
  1045. struct compat_shmid_ds __user *p = buf;
  1046. return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm);
  1047. }
  1048. }
  1049. COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr)
  1050. {
  1051. struct ipc_namespace *ns;
  1052. struct shmid64_ds sem64;
  1053. int version = compat_ipc_parse_version(&cmd);
  1054. int err;
  1055. ns = current->nsproxy->ipc_ns;
  1056. if (cmd < 0 || shmid < 0)
  1057. return -EINVAL;
  1058. switch (cmd) {
  1059. case IPC_INFO: {
  1060. struct shminfo64 shminfo;
  1061. err = shmctl_ipc_info(ns, &shminfo);
  1062. if (err < 0)
  1063. return err;
  1064. if (copy_compat_shminfo_to_user(uptr, &shminfo, version))
  1065. err = -EFAULT;
  1066. return err;
  1067. }
  1068. case SHM_INFO: {
  1069. struct shm_info shm_info;
  1070. err = shmctl_shm_info(ns, &shm_info);
  1071. if (err < 0)
  1072. return err;
  1073. if (put_compat_shm_info(&shm_info, uptr))
  1074. err = -EFAULT;
  1075. return err;
  1076. }
  1077. case IPC_STAT:
  1078. case SHM_STAT:
  1079. err = shmctl_stat(ns, shmid, cmd, &sem64);
  1080. if (err < 0)
  1081. return err;
  1082. if (copy_compat_shmid_to_user(uptr, &sem64, version))
  1083. err = -EFAULT;
  1084. return err;
  1085. case IPC_SET:
  1086. if (copy_compat_shmid_from_user(&sem64, uptr, version))
  1087. return -EFAULT;
  1088. /* fallthru */
  1089. case IPC_RMID:
  1090. return shmctl_down(ns, shmid, cmd, &sem64);
  1091. case SHM_LOCK:
  1092. case SHM_UNLOCK:
  1093. return shmctl_do_lock(ns, shmid, cmd);
  1094. break;
  1095. default:
  1096. return -EINVAL;
  1097. }
  1098. return err;
  1099. }
  1100. #endif
  1101. /*
  1102. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  1103. *
  1104. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  1105. * "raddr" thing points to kernel space, and there has to be a wrapper around
  1106. * this.
  1107. */
  1108. long do_shmat(int shmid, char __user *shmaddr, int shmflg,
  1109. ulong *raddr, unsigned long shmlba)
  1110. {
  1111. struct shmid_kernel *shp;
  1112. unsigned long addr = (unsigned long)shmaddr;
  1113. unsigned long size;
  1114. struct file *file;
  1115. int err;
  1116. unsigned long flags = MAP_SHARED;
  1117. unsigned long prot;
  1118. int acc_mode;
  1119. struct ipc_namespace *ns;
  1120. struct shm_file_data *sfd;
  1121. struct path path;
  1122. fmode_t f_mode;
  1123. unsigned long populate = 0;
  1124. err = -EINVAL;
  1125. if (shmid < 0)
  1126. goto out;
  1127. if (addr) {
  1128. if (addr & (shmlba - 1)) {
  1129. /*
  1130. * Round down to the nearest multiple of shmlba.
  1131. * For sane do_mmap_pgoff() parameters, avoid
  1132. * round downs that trigger nil-page and MAP_FIXED.
  1133. */
  1134. if ((shmflg & SHM_RND) && addr >= shmlba)
  1135. addr &= ~(shmlba - 1);
  1136. else
  1137. #ifndef __ARCH_FORCE_SHMLBA
  1138. if (addr & ~PAGE_MASK)
  1139. #endif
  1140. goto out;
  1141. }
  1142. flags |= MAP_FIXED;
  1143. } else if ((shmflg & SHM_REMAP))
  1144. goto out;
  1145. if (shmflg & SHM_RDONLY) {
  1146. prot = PROT_READ;
  1147. acc_mode = S_IRUGO;
  1148. f_mode = FMODE_READ;
  1149. } else {
  1150. prot = PROT_READ | PROT_WRITE;
  1151. acc_mode = S_IRUGO | S_IWUGO;
  1152. f_mode = FMODE_READ | FMODE_WRITE;
  1153. }
  1154. if (shmflg & SHM_EXEC) {
  1155. prot |= PROT_EXEC;
  1156. acc_mode |= S_IXUGO;
  1157. }
  1158. /*
  1159. * We cannot rely on the fs check since SYSV IPC does have an
  1160. * additional creator id...
  1161. */
  1162. ns = current->nsproxy->ipc_ns;
  1163. rcu_read_lock();
  1164. shp = shm_obtain_object_check(ns, shmid);
  1165. if (IS_ERR(shp)) {
  1166. err = PTR_ERR(shp);
  1167. goto out_unlock;
  1168. }
  1169. err = -EACCES;
  1170. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  1171. goto out_unlock;
  1172. err = security_shm_shmat(shp, shmaddr, shmflg);
  1173. if (err)
  1174. goto out_unlock;
  1175. ipc_lock_object(&shp->shm_perm);
  1176. /* check if shm_destroy() is tearing down shp */
  1177. if (!ipc_valid_object(&shp->shm_perm)) {
  1178. ipc_unlock_object(&shp->shm_perm);
  1179. err = -EIDRM;
  1180. goto out_unlock;
  1181. }
  1182. path = shp->shm_file->f_path;
  1183. path_get(&path);
  1184. shp->shm_nattch++;
  1185. size = i_size_read(d_inode(path.dentry));
  1186. ipc_unlock_object(&shp->shm_perm);
  1187. rcu_read_unlock();
  1188. err = -ENOMEM;
  1189. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  1190. if (!sfd) {
  1191. path_put(&path);
  1192. goto out_nattch;
  1193. }
  1194. file = alloc_file(&path, f_mode,
  1195. is_file_hugepages(shp->shm_file) ?
  1196. &shm_file_operations_huge :
  1197. &shm_file_operations);
  1198. err = PTR_ERR(file);
  1199. if (IS_ERR(file)) {
  1200. kfree(sfd);
  1201. path_put(&path);
  1202. goto out_nattch;
  1203. }
  1204. file->private_data = sfd;
  1205. file->f_mapping = shp->shm_file->f_mapping;
  1206. sfd->id = shp->shm_perm.id;
  1207. sfd->ns = get_ipc_ns(ns);
  1208. sfd->file = shp->shm_file;
  1209. sfd->vm_ops = NULL;
  1210. err = security_mmap_file(file, prot, flags);
  1211. if (err)
  1212. goto out_fput;
  1213. if (down_write_killable(&current->mm->mmap_sem)) {
  1214. err = -EINTR;
  1215. goto out_fput;
  1216. }
  1217. if (addr && !(shmflg & SHM_REMAP)) {
  1218. err = -EINVAL;
  1219. if (addr + size < addr)
  1220. goto invalid;
  1221. if (find_vma_intersection(current->mm, addr, addr + size))
  1222. goto invalid;
  1223. }
  1224. addr = do_mmap_pgoff(file, addr, size, prot, flags, 0, &populate, NULL);
  1225. *raddr = addr;
  1226. err = 0;
  1227. if (IS_ERR_VALUE(addr))
  1228. err = (long)addr;
  1229. invalid:
  1230. up_write(&current->mm->mmap_sem);
  1231. if (populate)
  1232. mm_populate(addr, populate);
  1233. out_fput:
  1234. fput(file);
  1235. out_nattch:
  1236. down_write(&shm_ids(ns).rwsem);
  1237. shp = shm_lock(ns, shmid);
  1238. shp->shm_nattch--;
  1239. if (shm_may_destroy(ns, shp))
  1240. shm_destroy(ns, shp);
  1241. else
  1242. shm_unlock(shp);
  1243. up_write(&shm_ids(ns).rwsem);
  1244. return err;
  1245. out_unlock:
  1246. rcu_read_unlock();
  1247. out:
  1248. return err;
  1249. }
  1250. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  1251. {
  1252. unsigned long ret;
  1253. long err;
  1254. err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
  1255. if (err)
  1256. return err;
  1257. force_successful_syscall_return();
  1258. return (long)ret;
  1259. }
  1260. #ifdef CONFIG_COMPAT
  1261. #ifndef COMPAT_SHMLBA
  1262. #define COMPAT_SHMLBA SHMLBA
  1263. #endif
  1264. COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg)
  1265. {
  1266. unsigned long ret;
  1267. long err;
  1268. err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
  1269. if (err)
  1270. return err;
  1271. force_successful_syscall_return();
  1272. return (long)ret;
  1273. }
  1274. #endif
  1275. /*
  1276. * detach and kill segment if marked destroyed.
  1277. * The work is done in shm_close.
  1278. */
  1279. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  1280. {
  1281. struct mm_struct *mm = current->mm;
  1282. struct vm_area_struct *vma;
  1283. unsigned long addr = (unsigned long)shmaddr;
  1284. int retval = -EINVAL;
  1285. #ifdef CONFIG_MMU
  1286. loff_t size = 0;
  1287. struct file *file;
  1288. struct vm_area_struct *next;
  1289. #endif
  1290. if (addr & ~PAGE_MASK)
  1291. return retval;
  1292. if (down_write_killable(&mm->mmap_sem))
  1293. return -EINTR;
  1294. /*
  1295. * This function tries to be smart and unmap shm segments that
  1296. * were modified by partial mlock or munmap calls:
  1297. * - It first determines the size of the shm segment that should be
  1298. * unmapped: It searches for a vma that is backed by shm and that
  1299. * started at address shmaddr. It records it's size and then unmaps
  1300. * it.
  1301. * - Then it unmaps all shm vmas that started at shmaddr and that
  1302. * are within the initially determined size and that are from the
  1303. * same shm segment from which we determined the size.
  1304. * Errors from do_munmap are ignored: the function only fails if
  1305. * it's called with invalid parameters or if it's called to unmap
  1306. * a part of a vma. Both calls in this function are for full vmas,
  1307. * the parameters are directly copied from the vma itself and always
  1308. * valid - therefore do_munmap cannot fail. (famous last words?)
  1309. */
  1310. /*
  1311. * If it had been mremap()'d, the starting address would not
  1312. * match the usual checks anyway. So assume all vma's are
  1313. * above the starting address given.
  1314. */
  1315. vma = find_vma(mm, addr);
  1316. #ifdef CONFIG_MMU
  1317. while (vma) {
  1318. next = vma->vm_next;
  1319. /*
  1320. * Check if the starting address would match, i.e. it's
  1321. * a fragment created by mprotect() and/or munmap(), or it
  1322. * otherwise it starts at this address with no hassles.
  1323. */
  1324. if ((vma->vm_ops == &shm_vm_ops) &&
  1325. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  1326. /*
  1327. * Record the file of the shm segment being
  1328. * unmapped. With mremap(), someone could place
  1329. * page from another segment but with equal offsets
  1330. * in the range we are unmapping.
  1331. */
  1332. file = vma->vm_file;
  1333. size = i_size_read(file_inode(vma->vm_file));
  1334. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
  1335. /*
  1336. * We discovered the size of the shm segment, so
  1337. * break out of here and fall through to the next
  1338. * loop that uses the size information to stop
  1339. * searching for matching vma's.
  1340. */
  1341. retval = 0;
  1342. vma = next;
  1343. break;
  1344. }
  1345. vma = next;
  1346. }
  1347. /*
  1348. * We need look no further than the maximum address a fragment
  1349. * could possibly have landed at. Also cast things to loff_t to
  1350. * prevent overflows and make comparisons vs. equal-width types.
  1351. */
  1352. size = PAGE_ALIGN(size);
  1353. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1354. next = vma->vm_next;
  1355. /* finding a matching vma now does not alter retval */
  1356. if ((vma->vm_ops == &shm_vm_ops) &&
  1357. ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
  1358. (vma->vm_file == file))
  1359. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
  1360. vma = next;
  1361. }
  1362. #else /* CONFIG_MMU */
  1363. /* under NOMMU conditions, the exact address to be destroyed must be
  1364. * given
  1365. */
  1366. if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1367. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
  1368. retval = 0;
  1369. }
  1370. #endif
  1371. up_write(&mm->mmap_sem);
  1372. return retval;
  1373. }
  1374. #ifdef CONFIG_PROC_FS
  1375. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1376. {
  1377. struct user_namespace *user_ns = seq_user_ns(s);
  1378. struct kern_ipc_perm *ipcp = it;
  1379. struct shmid_kernel *shp;
  1380. unsigned long rss = 0, swp = 0;
  1381. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  1382. shm_add_rss_swap(shp, &rss, &swp);
  1383. #if BITS_PER_LONG <= 32
  1384. #define SIZE_SPEC "%10lu"
  1385. #else
  1386. #define SIZE_SPEC "%21lu"
  1387. #endif
  1388. seq_printf(s,
  1389. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1390. "%5lu %5u %5u %5u %5u %10llu %10llu %10llu "
  1391. SIZE_SPEC " " SIZE_SPEC "\n",
  1392. shp->shm_perm.key,
  1393. shp->shm_perm.id,
  1394. shp->shm_perm.mode,
  1395. shp->shm_segsz,
  1396. shp->shm_cprid,
  1397. shp->shm_lprid,
  1398. shp->shm_nattch,
  1399. from_kuid_munged(user_ns, shp->shm_perm.uid),
  1400. from_kgid_munged(user_ns, shp->shm_perm.gid),
  1401. from_kuid_munged(user_ns, shp->shm_perm.cuid),
  1402. from_kgid_munged(user_ns, shp->shm_perm.cgid),
  1403. shp->shm_atim,
  1404. shp->shm_dtim,
  1405. shp->shm_ctim,
  1406. rss * PAGE_SIZE,
  1407. swp * PAGE_SIZE);
  1408. return 0;
  1409. }
  1410. #endif