shm.c 45 KB

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