shm.c 45 KB

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