shm.c 42 KB

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