shm.c 39 KB

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