shm.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /*
  2. * linux/ipc/shm.c
  3. * Copyright (C) 1992, 1993 Krishna Balasubramanian
  4. * Many improvements/fixes by Bruno Haible.
  5. * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
  6. * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
  7. *
  8. * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  9. * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
  10. * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
  11. * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
  12. * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
  13. * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  14. * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  15. *
  16. * support for audit of ipc object properties and permission changes
  17. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  18. *
  19. * namespaces support
  20. * OpenVZ, SWsoft Inc.
  21. * Pavel Emelianov <xemul@openvz.org>
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/shm.h>
  27. #include <linux/init.h>
  28. #include <linux/file.h>
  29. #include <linux/mman.h>
  30. #include <linux/shmem_fs.h>
  31. #include <linux/security.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/audit.h>
  34. #include <linux/capability.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/rwsem.h>
  38. #include <linux/nsproxy.h>
  39. #include <linux/mount.h>
  40. #include <linux/ipc_namespace.h>
  41. #include <asm/uaccess.h>
  42. #include "util.h"
  43. struct shm_file_data {
  44. int id;
  45. struct ipc_namespace *ns;
  46. struct file *file;
  47. const struct vm_operations_struct *vm_ops;
  48. };
  49. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  50. static const struct file_operations shm_file_operations;
  51. static const struct vm_operations_struct shm_vm_ops;
  52. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  53. #define shm_unlock(shp) \
  54. ipc_unlock(&(shp)->shm_perm)
  55. static int newseg(struct ipc_namespace *, struct ipc_params *);
  56. static void shm_open(struct vm_area_struct *vma);
  57. static void shm_close(struct vm_area_struct *vma);
  58. static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
  59. #ifdef CONFIG_PROC_FS
  60. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  61. #endif
  62. void shm_init_ns(struct ipc_namespace *ns)
  63. {
  64. ns->shm_ctlmax = SHMMAX;
  65. ns->shm_ctlall = SHMALL;
  66. ns->shm_ctlmni = SHMMNI;
  67. ns->shm_rmid_forced = 0;
  68. ns->shm_tot = 0;
  69. ipc_init_ids(&shm_ids(ns));
  70. }
  71. /*
  72. * Called with shm_ids.rw_mutex (writer) and the shp structure locked.
  73. * Only shm_ids.rw_mutex remains locked on exit.
  74. */
  75. static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  76. {
  77. struct shmid_kernel *shp;
  78. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  79. if (shp->shm_nattch){
  80. shp->shm_perm.mode |= SHM_DEST;
  81. /* Do not find it any more */
  82. shp->shm_perm.key = IPC_PRIVATE;
  83. shm_unlock(shp);
  84. } else
  85. shm_destroy(ns, shp);
  86. }
  87. #ifdef CONFIG_IPC_NS
  88. void shm_exit_ns(struct ipc_namespace *ns)
  89. {
  90. free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
  91. idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
  92. }
  93. #endif
  94. static int __init ipc_ns_init(void)
  95. {
  96. shm_init_ns(&init_ipc_ns);
  97. return 0;
  98. }
  99. pure_initcall(ipc_ns_init);
  100. void __init shm_init (void)
  101. {
  102. ipc_init_proc_interface("sysvipc/shm",
  103. #if BITS_PER_LONG <= 32
  104. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  105. #else
  106. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  107. #endif
  108. IPC_SHM_IDS, sysvipc_shm_proc_show);
  109. }
  110. /*
  111. * shm_lock_(check_) routines are called in the paths where the rw_mutex
  112. * is not necessarily held.
  113. */
  114. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  115. {
  116. struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
  117. if (IS_ERR(ipcp))
  118. return (struct shmid_kernel *)ipcp;
  119. return container_of(ipcp, struct shmid_kernel, shm_perm);
  120. }
  121. static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
  122. {
  123. rcu_read_lock();
  124. spin_lock(&ipcp->shm_perm.lock);
  125. }
  126. static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
  127. int id)
  128. {
  129. struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
  130. if (IS_ERR(ipcp))
  131. return (struct shmid_kernel *)ipcp;
  132. return container_of(ipcp, struct shmid_kernel, shm_perm);
  133. }
  134. static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
  135. {
  136. ipc_rmid(&shm_ids(ns), &s->shm_perm);
  137. }
  138. /* This is called by fork, once for every shm attach. */
  139. static void shm_open(struct vm_area_struct *vma)
  140. {
  141. struct file *file = vma->vm_file;
  142. struct shm_file_data *sfd = shm_file_data(file);
  143. struct shmid_kernel *shp;
  144. shp = shm_lock(sfd->ns, sfd->id);
  145. BUG_ON(IS_ERR(shp));
  146. shp->shm_atim = get_seconds();
  147. shp->shm_lprid = task_tgid_vnr(current);
  148. shp->shm_nattch++;
  149. shm_unlock(shp);
  150. }
  151. /*
  152. * shm_destroy - free the struct shmid_kernel
  153. *
  154. * @ns: namespace
  155. * @shp: struct to free
  156. *
  157. * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
  158. * but returns with shp unlocked and freed.
  159. */
  160. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  161. {
  162. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  163. shm_rmid(ns, shp);
  164. shm_unlock(shp);
  165. if (!is_file_hugepages(shp->shm_file))
  166. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  167. else if (shp->mlock_user)
  168. user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
  169. shp->mlock_user);
  170. fput (shp->shm_file);
  171. security_shm_free(shp);
  172. ipc_rcu_putref(shp);
  173. }
  174. /*
  175. * shm_may_destroy - identifies whether shm segment should be destroyed now
  176. *
  177. * Returns true if and only if there are no active users of the segment and
  178. * one of the following is true:
  179. *
  180. * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
  181. *
  182. * 2) sysctl kernel.shm_rmid_forced is set to 1.
  183. */
  184. static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  185. {
  186. return (shp->shm_nattch == 0) &&
  187. (ns->shm_rmid_forced ||
  188. (shp->shm_perm.mode & SHM_DEST));
  189. }
  190. /*
  191. * remove the attach descriptor vma.
  192. * free memory for segment if it is marked destroyed.
  193. * The descriptor has already been removed from the current->mm->mmap list
  194. * and will later be kfree()d.
  195. */
  196. static void shm_close(struct vm_area_struct *vma)
  197. {
  198. struct file * file = vma->vm_file;
  199. struct shm_file_data *sfd = shm_file_data(file);
  200. struct shmid_kernel *shp;
  201. struct ipc_namespace *ns = sfd->ns;
  202. down_write(&shm_ids(ns).rw_mutex);
  203. /* remove from the list of attaches of the shm segment */
  204. shp = shm_lock(ns, sfd->id);
  205. BUG_ON(IS_ERR(shp));
  206. shp->shm_lprid = task_tgid_vnr(current);
  207. shp->shm_dtim = get_seconds();
  208. shp->shm_nattch--;
  209. if (shm_may_destroy(ns, shp))
  210. shm_destroy(ns, shp);
  211. else
  212. shm_unlock(shp);
  213. up_write(&shm_ids(ns).rw_mutex);
  214. }
  215. /* Called with ns->shm_ids(ns).rw_mutex locked */
  216. static int shm_try_destroy_current(int id, void *p, void *data)
  217. {
  218. struct ipc_namespace *ns = data;
  219. struct kern_ipc_perm *ipcp = p;
  220. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  221. if (shp->shm_creator != current)
  222. return 0;
  223. /*
  224. * Mark it as orphaned to destroy the segment when
  225. * kernel.shm_rmid_forced is changed.
  226. * It is noop if the following shm_may_destroy() returns true.
  227. */
  228. shp->shm_creator = NULL;
  229. /*
  230. * Don't even try to destroy it. If shm_rmid_forced=0 and IPC_RMID
  231. * is not set, it shouldn't be deleted here.
  232. */
  233. if (!ns->shm_rmid_forced)
  234. return 0;
  235. if (shm_may_destroy(ns, shp)) {
  236. shm_lock_by_ptr(shp);
  237. shm_destroy(ns, shp);
  238. }
  239. return 0;
  240. }
  241. /* Called with ns->shm_ids(ns).rw_mutex locked */
  242. static int shm_try_destroy_orphaned(int id, void *p, void *data)
  243. {
  244. struct ipc_namespace *ns = data;
  245. struct kern_ipc_perm *ipcp = p;
  246. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  247. /*
  248. * We want to destroy segments without users and with already
  249. * exit'ed originating process.
  250. *
  251. * As shp->* are changed under rw_mutex, it's safe to skip shp locking.
  252. */
  253. if (shp->shm_creator != NULL)
  254. return 0;
  255. if (shm_may_destroy(ns, shp)) {
  256. shm_lock_by_ptr(shp);
  257. shm_destroy(ns, shp);
  258. }
  259. return 0;
  260. }
  261. void shm_destroy_orphaned(struct ipc_namespace *ns)
  262. {
  263. down_write(&shm_ids(ns).rw_mutex);
  264. if (shm_ids(ns).in_use)
  265. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
  266. up_write(&shm_ids(ns).rw_mutex);
  267. }
  268. void exit_shm(struct task_struct *task)
  269. {
  270. struct ipc_namespace *ns = task->nsproxy->ipc_ns;
  271. if (shm_ids(ns).in_use == 0)
  272. return;
  273. /* Destroy all already created segments, but not mapped yet */
  274. down_write(&shm_ids(ns).rw_mutex);
  275. if (shm_ids(ns).in_use)
  276. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_current, ns);
  277. up_write(&shm_ids(ns).rw_mutex);
  278. }
  279. static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  280. {
  281. struct file *file = vma->vm_file;
  282. struct shm_file_data *sfd = shm_file_data(file);
  283. return sfd->vm_ops->fault(vma, vmf);
  284. }
  285. #ifdef CONFIG_NUMA
  286. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  287. {
  288. struct file *file = vma->vm_file;
  289. struct shm_file_data *sfd = shm_file_data(file);
  290. int err = 0;
  291. if (sfd->vm_ops->set_policy)
  292. err = sfd->vm_ops->set_policy(vma, new);
  293. return err;
  294. }
  295. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  296. unsigned long addr)
  297. {
  298. struct file *file = vma->vm_file;
  299. struct shm_file_data *sfd = shm_file_data(file);
  300. struct mempolicy *pol = NULL;
  301. if (sfd->vm_ops->get_policy)
  302. pol = sfd->vm_ops->get_policy(vma, addr);
  303. else if (vma->vm_policy)
  304. pol = vma->vm_policy;
  305. return pol;
  306. }
  307. #endif
  308. static int shm_mmap(struct file * file, struct vm_area_struct * vma)
  309. {
  310. struct shm_file_data *sfd = shm_file_data(file);
  311. int ret;
  312. ret = sfd->file->f_op->mmap(sfd->file, vma);
  313. if (ret != 0)
  314. return ret;
  315. sfd->vm_ops = vma->vm_ops;
  316. #ifdef CONFIG_MMU
  317. BUG_ON(!sfd->vm_ops->fault);
  318. #endif
  319. vma->vm_ops = &shm_vm_ops;
  320. shm_open(vma);
  321. return ret;
  322. }
  323. static int shm_release(struct inode *ino, struct file *file)
  324. {
  325. struct shm_file_data *sfd = shm_file_data(file);
  326. put_ipc_ns(sfd->ns);
  327. shm_file_data(file) = NULL;
  328. kfree(sfd);
  329. return 0;
  330. }
  331. static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  332. {
  333. struct shm_file_data *sfd = shm_file_data(file);
  334. if (!sfd->file->f_op->fsync)
  335. return -EINVAL;
  336. return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
  337. }
  338. static long shm_fallocate(struct file *file, int mode, loff_t offset,
  339. loff_t len)
  340. {
  341. struct shm_file_data *sfd = shm_file_data(file);
  342. if (!sfd->file->f_op->fallocate)
  343. return -EOPNOTSUPP;
  344. return sfd->file->f_op->fallocate(file, mode, offset, len);
  345. }
  346. static unsigned long shm_get_unmapped_area(struct file *file,
  347. unsigned long addr, unsigned long len, unsigned long pgoff,
  348. unsigned long flags)
  349. {
  350. struct shm_file_data *sfd = shm_file_data(file);
  351. return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
  352. pgoff, flags);
  353. }
  354. static const struct file_operations shm_file_operations = {
  355. .mmap = shm_mmap,
  356. .fsync = shm_fsync,
  357. .release = shm_release,
  358. #ifndef CONFIG_MMU
  359. .get_unmapped_area = shm_get_unmapped_area,
  360. #endif
  361. .llseek = noop_llseek,
  362. .fallocate = shm_fallocate,
  363. };
  364. static const struct file_operations shm_file_operations_huge = {
  365. .mmap = shm_mmap,
  366. .fsync = shm_fsync,
  367. .release = shm_release,
  368. .get_unmapped_area = shm_get_unmapped_area,
  369. .llseek = noop_llseek,
  370. .fallocate = shm_fallocate,
  371. };
  372. int is_file_shm_hugepages(struct file *file)
  373. {
  374. return file->f_op == &shm_file_operations_huge;
  375. }
  376. static const struct vm_operations_struct shm_vm_ops = {
  377. .open = shm_open, /* callback for a new vm-area open */
  378. .close = shm_close, /* callback for when the vm-area is released */
  379. .fault = shm_fault,
  380. #if defined(CONFIG_NUMA)
  381. .set_policy = shm_set_policy,
  382. .get_policy = shm_get_policy,
  383. #endif
  384. };
  385. /**
  386. * newseg - Create a new shared memory segment
  387. * @ns: namespace
  388. * @params: ptr to the structure that contains key, size and shmflg
  389. *
  390. * Called with shm_ids.rw_mutex held as a writer.
  391. */
  392. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  393. {
  394. key_t key = params->key;
  395. int shmflg = params->flg;
  396. size_t size = params->u.size;
  397. int error;
  398. struct shmid_kernel *shp;
  399. size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  400. struct file * file;
  401. char name[13];
  402. int id;
  403. vm_flags_t acctflag = 0;
  404. if (size < SHMMIN || size > ns->shm_ctlmax)
  405. return -EINVAL;
  406. if (ns->shm_tot + numpages > ns->shm_ctlall)
  407. return -ENOSPC;
  408. shp = ipc_rcu_alloc(sizeof(*shp));
  409. if (!shp)
  410. return -ENOMEM;
  411. shp->shm_perm.key = key;
  412. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  413. shp->mlock_user = NULL;
  414. shp->shm_perm.security = NULL;
  415. error = security_shm_alloc(shp);
  416. if (error) {
  417. ipc_rcu_putref(shp);
  418. return error;
  419. }
  420. sprintf (name, "SYSV%08x", key);
  421. if (shmflg & SHM_HUGETLB) {
  422. size_t hugesize = ALIGN(size, huge_page_size(&default_hstate));
  423. /* hugetlb_file_setup applies strict accounting */
  424. if (shmflg & SHM_NORESERVE)
  425. acctflag = VM_NORESERVE;
  426. file = hugetlb_file_setup(name, hugesize, acctflag,
  427. &shp->mlock_user, HUGETLB_SHMFS_INODE);
  428. } else {
  429. /*
  430. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  431. * if it's asked for.
  432. */
  433. if ((shmflg & SHM_NORESERVE) &&
  434. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  435. acctflag = VM_NORESERVE;
  436. file = shmem_file_setup(name, size, acctflag);
  437. }
  438. error = PTR_ERR(file);
  439. if (IS_ERR(file))
  440. goto no_file;
  441. shp->shm_cprid = task_tgid_vnr(current);
  442. shp->shm_lprid = 0;
  443. shp->shm_atim = shp->shm_dtim = 0;
  444. shp->shm_ctim = get_seconds();
  445. shp->shm_segsz = size;
  446. shp->shm_nattch = 0;
  447. shp->shm_file = file;
  448. shp->shm_creator = current;
  449. id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  450. if (id < 0) {
  451. error = id;
  452. goto no_id;
  453. }
  454. /*
  455. * shmid gets reported as "inode#" in /proc/pid/maps.
  456. * proc-ps tools use this. Changing this will break them.
  457. */
  458. file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
  459. ns->shm_tot += numpages;
  460. error = shp->shm_perm.id;
  461. shm_unlock(shp);
  462. return error;
  463. no_id:
  464. if (is_file_hugepages(file) && shp->mlock_user)
  465. user_shm_unlock(size, shp->mlock_user);
  466. fput(file);
  467. no_file:
  468. security_shm_free(shp);
  469. ipc_rcu_putref(shp);
  470. return error;
  471. }
  472. /*
  473. * Called with shm_ids.rw_mutex and ipcp locked.
  474. */
  475. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  476. {
  477. struct shmid_kernel *shp;
  478. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  479. return security_shm_associate(shp, shmflg);
  480. }
  481. /*
  482. * Called with shm_ids.rw_mutex and ipcp locked.
  483. */
  484. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  485. struct ipc_params *params)
  486. {
  487. struct shmid_kernel *shp;
  488. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  489. if (shp->shm_segsz < params->u.size)
  490. return -EINVAL;
  491. return 0;
  492. }
  493. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  494. {
  495. struct ipc_namespace *ns;
  496. struct ipc_ops shm_ops;
  497. struct ipc_params shm_params;
  498. ns = current->nsproxy->ipc_ns;
  499. shm_ops.getnew = newseg;
  500. shm_ops.associate = shm_security;
  501. shm_ops.more_checks = shm_more_checks;
  502. shm_params.key = key;
  503. shm_params.flg = shmflg;
  504. shm_params.u.size = size;
  505. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  506. }
  507. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  508. {
  509. switch(version) {
  510. case IPC_64:
  511. return copy_to_user(buf, in, sizeof(*in));
  512. case IPC_OLD:
  513. {
  514. struct shmid_ds out;
  515. memset(&out, 0, sizeof(out));
  516. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  517. out.shm_segsz = in->shm_segsz;
  518. out.shm_atime = in->shm_atime;
  519. out.shm_dtime = in->shm_dtime;
  520. out.shm_ctime = in->shm_ctime;
  521. out.shm_cpid = in->shm_cpid;
  522. out.shm_lpid = in->shm_lpid;
  523. out.shm_nattch = in->shm_nattch;
  524. return copy_to_user(buf, &out, sizeof(out));
  525. }
  526. default:
  527. return -EINVAL;
  528. }
  529. }
  530. static inline unsigned long
  531. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  532. {
  533. switch(version) {
  534. case IPC_64:
  535. if (copy_from_user(out, buf, sizeof(*out)))
  536. return -EFAULT;
  537. return 0;
  538. case IPC_OLD:
  539. {
  540. struct shmid_ds tbuf_old;
  541. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  542. return -EFAULT;
  543. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  544. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  545. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  546. return 0;
  547. }
  548. default:
  549. return -EINVAL;
  550. }
  551. }
  552. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  553. {
  554. switch(version) {
  555. case IPC_64:
  556. return copy_to_user(buf, in, sizeof(*in));
  557. case IPC_OLD:
  558. {
  559. struct shminfo out;
  560. if(in->shmmax > INT_MAX)
  561. out.shmmax = INT_MAX;
  562. else
  563. out.shmmax = (int)in->shmmax;
  564. out.shmmin = in->shmmin;
  565. out.shmmni = in->shmmni;
  566. out.shmseg = in->shmseg;
  567. out.shmall = in->shmall;
  568. return copy_to_user(buf, &out, sizeof(out));
  569. }
  570. default:
  571. return -EINVAL;
  572. }
  573. }
  574. /*
  575. * Calculate and add used RSS and swap pages of a shm.
  576. * Called with shm_ids.rw_mutex held as a reader
  577. */
  578. static void shm_add_rss_swap(struct shmid_kernel *shp,
  579. unsigned long *rss_add, unsigned long *swp_add)
  580. {
  581. struct inode *inode;
  582. inode = shp->shm_file->f_path.dentry->d_inode;
  583. if (is_file_hugepages(shp->shm_file)) {
  584. struct address_space *mapping = inode->i_mapping;
  585. struct hstate *h = hstate_file(shp->shm_file);
  586. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  587. } else {
  588. #ifdef CONFIG_SHMEM
  589. struct shmem_inode_info *info = SHMEM_I(inode);
  590. spin_lock(&info->lock);
  591. *rss_add += inode->i_mapping->nrpages;
  592. *swp_add += info->swapped;
  593. spin_unlock(&info->lock);
  594. #else
  595. *rss_add += inode->i_mapping->nrpages;
  596. #endif
  597. }
  598. }
  599. /*
  600. * Called with shm_ids.rw_mutex held as a reader
  601. */
  602. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  603. unsigned long *swp)
  604. {
  605. int next_id;
  606. int total, in_use;
  607. *rss = 0;
  608. *swp = 0;
  609. in_use = shm_ids(ns).in_use;
  610. for (total = 0, next_id = 0; total < in_use; next_id++) {
  611. struct kern_ipc_perm *ipc;
  612. struct shmid_kernel *shp;
  613. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  614. if (ipc == NULL)
  615. continue;
  616. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  617. shm_add_rss_swap(shp, rss, swp);
  618. total++;
  619. }
  620. }
  621. /*
  622. * This function handles some shmctl commands which require the rw_mutex
  623. * to be held in write mode.
  624. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  625. */
  626. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  627. struct shmid_ds __user *buf, int version)
  628. {
  629. struct kern_ipc_perm *ipcp;
  630. struct shmid64_ds shmid64;
  631. struct shmid_kernel *shp;
  632. int err;
  633. if (cmd == IPC_SET) {
  634. if (copy_shmid_from_user(&shmid64, buf, version))
  635. return -EFAULT;
  636. }
  637. ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
  638. &shmid64.shm_perm, 0);
  639. if (IS_ERR(ipcp))
  640. return PTR_ERR(ipcp);
  641. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  642. err = security_shm_shmctl(shp, cmd);
  643. if (err)
  644. goto out_unlock;
  645. switch (cmd) {
  646. case IPC_RMID:
  647. do_shm_rmid(ns, ipcp);
  648. goto out_up;
  649. case IPC_SET:
  650. err = ipc_update_perm(&shmid64.shm_perm, ipcp);
  651. if (err)
  652. goto out_unlock;
  653. shp->shm_ctim = get_seconds();
  654. break;
  655. default:
  656. err = -EINVAL;
  657. }
  658. out_unlock:
  659. shm_unlock(shp);
  660. out_up:
  661. up_write(&shm_ids(ns).rw_mutex);
  662. return err;
  663. }
  664. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  665. {
  666. struct shmid_kernel *shp;
  667. int err, version;
  668. struct ipc_namespace *ns;
  669. if (cmd < 0 || shmid < 0) {
  670. err = -EINVAL;
  671. goto out;
  672. }
  673. version = ipc_parse_version(&cmd);
  674. ns = current->nsproxy->ipc_ns;
  675. switch (cmd) { /* replace with proc interface ? */
  676. case IPC_INFO:
  677. {
  678. struct shminfo64 shminfo;
  679. err = security_shm_shmctl(NULL, cmd);
  680. if (err)
  681. return err;
  682. memset(&shminfo, 0, sizeof(shminfo));
  683. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  684. shminfo.shmmax = ns->shm_ctlmax;
  685. shminfo.shmall = ns->shm_ctlall;
  686. shminfo.shmmin = SHMMIN;
  687. if(copy_shminfo_to_user (buf, &shminfo, version))
  688. return -EFAULT;
  689. down_read(&shm_ids(ns).rw_mutex);
  690. err = ipc_get_maxid(&shm_ids(ns));
  691. up_read(&shm_ids(ns).rw_mutex);
  692. if(err<0)
  693. err = 0;
  694. goto out;
  695. }
  696. case SHM_INFO:
  697. {
  698. struct shm_info shm_info;
  699. err = security_shm_shmctl(NULL, cmd);
  700. if (err)
  701. return err;
  702. memset(&shm_info, 0, sizeof(shm_info));
  703. down_read(&shm_ids(ns).rw_mutex);
  704. shm_info.used_ids = shm_ids(ns).in_use;
  705. shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
  706. shm_info.shm_tot = ns->shm_tot;
  707. shm_info.swap_attempts = 0;
  708. shm_info.swap_successes = 0;
  709. err = ipc_get_maxid(&shm_ids(ns));
  710. up_read(&shm_ids(ns).rw_mutex);
  711. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  712. err = -EFAULT;
  713. goto out;
  714. }
  715. err = err < 0 ? 0 : err;
  716. goto out;
  717. }
  718. case SHM_STAT:
  719. case IPC_STAT:
  720. {
  721. struct shmid64_ds tbuf;
  722. int result;
  723. if (cmd == SHM_STAT) {
  724. shp = shm_lock(ns, shmid);
  725. if (IS_ERR(shp)) {
  726. err = PTR_ERR(shp);
  727. goto out;
  728. }
  729. result = shp->shm_perm.id;
  730. } else {
  731. shp = shm_lock_check(ns, shmid);
  732. if (IS_ERR(shp)) {
  733. err = PTR_ERR(shp);
  734. goto out;
  735. }
  736. result = 0;
  737. }
  738. err = -EACCES;
  739. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  740. goto out_unlock;
  741. err = security_shm_shmctl(shp, cmd);
  742. if (err)
  743. goto out_unlock;
  744. memset(&tbuf, 0, sizeof(tbuf));
  745. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  746. tbuf.shm_segsz = shp->shm_segsz;
  747. tbuf.shm_atime = shp->shm_atim;
  748. tbuf.shm_dtime = shp->shm_dtim;
  749. tbuf.shm_ctime = shp->shm_ctim;
  750. tbuf.shm_cpid = shp->shm_cprid;
  751. tbuf.shm_lpid = shp->shm_lprid;
  752. tbuf.shm_nattch = shp->shm_nattch;
  753. shm_unlock(shp);
  754. if(copy_shmid_to_user (buf, &tbuf, version))
  755. err = -EFAULT;
  756. else
  757. err = result;
  758. goto out;
  759. }
  760. case SHM_LOCK:
  761. case SHM_UNLOCK:
  762. {
  763. struct file *shm_file;
  764. shp = shm_lock_check(ns, shmid);
  765. if (IS_ERR(shp)) {
  766. err = PTR_ERR(shp);
  767. goto out;
  768. }
  769. audit_ipc_obj(&(shp->shm_perm));
  770. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  771. kuid_t euid = current_euid();
  772. err = -EPERM;
  773. if (!uid_eq(euid, shp->shm_perm.uid) &&
  774. !uid_eq(euid, shp->shm_perm.cuid))
  775. goto out_unlock;
  776. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
  777. goto out_unlock;
  778. }
  779. err = security_shm_shmctl(shp, cmd);
  780. if (err)
  781. goto out_unlock;
  782. shm_file = shp->shm_file;
  783. if (is_file_hugepages(shm_file))
  784. goto out_unlock;
  785. if (cmd == SHM_LOCK) {
  786. struct user_struct *user = current_user();
  787. err = shmem_lock(shm_file, 1, user);
  788. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
  789. shp->shm_perm.mode |= SHM_LOCKED;
  790. shp->mlock_user = user;
  791. }
  792. goto out_unlock;
  793. }
  794. /* SHM_UNLOCK */
  795. if (!(shp->shm_perm.mode & SHM_LOCKED))
  796. goto out_unlock;
  797. shmem_lock(shm_file, 0, shp->mlock_user);
  798. shp->shm_perm.mode &= ~SHM_LOCKED;
  799. shp->mlock_user = NULL;
  800. get_file(shm_file);
  801. shm_unlock(shp);
  802. shmem_unlock_mapping(shm_file->f_mapping);
  803. fput(shm_file);
  804. goto out;
  805. }
  806. case IPC_RMID:
  807. case IPC_SET:
  808. err = shmctl_down(ns, shmid, cmd, buf, version);
  809. return err;
  810. default:
  811. return -EINVAL;
  812. }
  813. out_unlock:
  814. shm_unlock(shp);
  815. out:
  816. return err;
  817. }
  818. /*
  819. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  820. *
  821. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  822. * "raddr" thing points to kernel space, and there has to be a wrapper around
  823. * this.
  824. */
  825. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
  826. unsigned long shmlba)
  827. {
  828. struct shmid_kernel *shp;
  829. unsigned long addr;
  830. unsigned long size;
  831. struct file * file;
  832. int err;
  833. unsigned long flags;
  834. unsigned long prot;
  835. int acc_mode;
  836. unsigned long user_addr;
  837. struct ipc_namespace *ns;
  838. struct shm_file_data *sfd;
  839. struct path path;
  840. fmode_t f_mode;
  841. err = -EINVAL;
  842. if (shmid < 0)
  843. goto out;
  844. else if ((addr = (ulong)shmaddr)) {
  845. if (addr & (shmlba - 1)) {
  846. if (shmflg & SHM_RND)
  847. addr &= ~(shmlba - 1); /* round down */
  848. else
  849. #ifndef __ARCH_FORCE_SHMLBA
  850. if (addr & ~PAGE_MASK)
  851. #endif
  852. goto out;
  853. }
  854. flags = MAP_SHARED | MAP_FIXED;
  855. } else {
  856. if ((shmflg & SHM_REMAP))
  857. goto out;
  858. flags = MAP_SHARED;
  859. }
  860. if (shmflg & SHM_RDONLY) {
  861. prot = PROT_READ;
  862. acc_mode = S_IRUGO;
  863. f_mode = FMODE_READ;
  864. } else {
  865. prot = PROT_READ | PROT_WRITE;
  866. acc_mode = S_IRUGO | S_IWUGO;
  867. f_mode = FMODE_READ | FMODE_WRITE;
  868. }
  869. if (shmflg & SHM_EXEC) {
  870. prot |= PROT_EXEC;
  871. acc_mode |= S_IXUGO;
  872. }
  873. /*
  874. * We cannot rely on the fs check since SYSV IPC does have an
  875. * additional creator id...
  876. */
  877. ns = current->nsproxy->ipc_ns;
  878. shp = shm_lock_check(ns, shmid);
  879. if (IS_ERR(shp)) {
  880. err = PTR_ERR(shp);
  881. goto out;
  882. }
  883. err = -EACCES;
  884. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  885. goto out_unlock;
  886. err = security_shm_shmat(shp, shmaddr, shmflg);
  887. if (err)
  888. goto out_unlock;
  889. path = shp->shm_file->f_path;
  890. path_get(&path);
  891. shp->shm_nattch++;
  892. size = i_size_read(path.dentry->d_inode);
  893. shm_unlock(shp);
  894. err = -ENOMEM;
  895. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  896. if (!sfd)
  897. goto out_put_dentry;
  898. file = alloc_file(&path, f_mode,
  899. is_file_hugepages(shp->shm_file) ?
  900. &shm_file_operations_huge :
  901. &shm_file_operations);
  902. if (!file)
  903. goto out_free;
  904. file->private_data = sfd;
  905. file->f_mapping = shp->shm_file->f_mapping;
  906. sfd->id = shp->shm_perm.id;
  907. sfd->ns = get_ipc_ns(ns);
  908. sfd->file = shp->shm_file;
  909. sfd->vm_ops = NULL;
  910. err = security_mmap_file(file, prot, flags);
  911. if (err)
  912. goto out_fput;
  913. down_write(&current->mm->mmap_sem);
  914. if (addr && !(shmflg & SHM_REMAP)) {
  915. err = -EINVAL;
  916. if (find_vma_intersection(current->mm, addr, addr + size))
  917. goto invalid;
  918. /*
  919. * If shm segment goes below stack, make sure there is some
  920. * space left for the stack to grow (at least 4 pages).
  921. */
  922. if (addr < current->mm->start_stack &&
  923. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  924. goto invalid;
  925. }
  926. user_addr = do_mmap (file, addr, size, prot, flags, 0);
  927. *raddr = user_addr;
  928. err = 0;
  929. if (IS_ERR_VALUE(user_addr))
  930. err = (long)user_addr;
  931. invalid:
  932. up_write(&current->mm->mmap_sem);
  933. out_fput:
  934. fput(file);
  935. out_nattch:
  936. down_write(&shm_ids(ns).rw_mutex);
  937. shp = shm_lock(ns, shmid);
  938. BUG_ON(IS_ERR(shp));
  939. shp->shm_nattch--;
  940. if (shm_may_destroy(ns, shp))
  941. shm_destroy(ns, shp);
  942. else
  943. shm_unlock(shp);
  944. up_write(&shm_ids(ns).rw_mutex);
  945. out:
  946. return err;
  947. out_unlock:
  948. shm_unlock(shp);
  949. goto out;
  950. out_free:
  951. kfree(sfd);
  952. out_put_dentry:
  953. path_put(&path);
  954. goto out_nattch;
  955. }
  956. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  957. {
  958. unsigned long ret;
  959. long err;
  960. err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
  961. if (err)
  962. return err;
  963. force_successful_syscall_return();
  964. return (long)ret;
  965. }
  966. /*
  967. * detach and kill segment if marked destroyed.
  968. * The work is done in shm_close.
  969. */
  970. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  971. {
  972. struct mm_struct *mm = current->mm;
  973. struct vm_area_struct *vma;
  974. unsigned long addr = (unsigned long)shmaddr;
  975. int retval = -EINVAL;
  976. #ifdef CONFIG_MMU
  977. loff_t size = 0;
  978. struct vm_area_struct *next;
  979. #endif
  980. if (addr & ~PAGE_MASK)
  981. return retval;
  982. down_write(&mm->mmap_sem);
  983. /*
  984. * This function tries to be smart and unmap shm segments that
  985. * were modified by partial mlock or munmap calls:
  986. * - It first determines the size of the shm segment that should be
  987. * unmapped: It searches for a vma that is backed by shm and that
  988. * started at address shmaddr. It records it's size and then unmaps
  989. * it.
  990. * - Then it unmaps all shm vmas that started at shmaddr and that
  991. * are within the initially determined size.
  992. * Errors from do_munmap are ignored: the function only fails if
  993. * it's called with invalid parameters or if it's called to unmap
  994. * a part of a vma. Both calls in this function are for full vmas,
  995. * the parameters are directly copied from the vma itself and always
  996. * valid - therefore do_munmap cannot fail. (famous last words?)
  997. */
  998. /*
  999. * If it had been mremap()'d, the starting address would not
  1000. * match the usual checks anyway. So assume all vma's are
  1001. * above the starting address given.
  1002. */
  1003. vma = find_vma(mm, addr);
  1004. #ifdef CONFIG_MMU
  1005. while (vma) {
  1006. next = vma->vm_next;
  1007. /*
  1008. * Check if the starting address would match, i.e. it's
  1009. * a fragment created by mprotect() and/or munmap(), or it
  1010. * otherwise it starts at this address with no hassles.
  1011. */
  1012. if ((vma->vm_ops == &shm_vm_ops) &&
  1013. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  1014. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  1015. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1016. /*
  1017. * We discovered the size of the shm segment, so
  1018. * break out of here and fall through to the next
  1019. * loop that uses the size information to stop
  1020. * searching for matching vma's.
  1021. */
  1022. retval = 0;
  1023. vma = next;
  1024. break;
  1025. }
  1026. vma = next;
  1027. }
  1028. /*
  1029. * We need look no further than the maximum address a fragment
  1030. * could possibly have landed at. Also cast things to loff_t to
  1031. * prevent overflows and make comparisons vs. equal-width types.
  1032. */
  1033. size = PAGE_ALIGN(size);
  1034. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1035. next = vma->vm_next;
  1036. /* finding a matching vma now does not alter retval */
  1037. if ((vma->vm_ops == &shm_vm_ops) &&
  1038. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  1039. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1040. vma = next;
  1041. }
  1042. #else /* CONFIG_MMU */
  1043. /* under NOMMU conditions, the exact address to be destroyed must be
  1044. * given */
  1045. retval = -EINVAL;
  1046. if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1047. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1048. retval = 0;
  1049. }
  1050. #endif
  1051. up_write(&mm->mmap_sem);
  1052. return retval;
  1053. }
  1054. #ifdef CONFIG_PROC_FS
  1055. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1056. {
  1057. struct user_namespace *user_ns = seq_user_ns(s);
  1058. struct shmid_kernel *shp = it;
  1059. unsigned long rss = 0, swp = 0;
  1060. shm_add_rss_swap(shp, &rss, &swp);
  1061. #if BITS_PER_LONG <= 32
  1062. #define SIZE_SPEC "%10lu"
  1063. #else
  1064. #define SIZE_SPEC "%21lu"
  1065. #endif
  1066. return seq_printf(s,
  1067. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1068. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
  1069. SIZE_SPEC " " SIZE_SPEC "\n",
  1070. shp->shm_perm.key,
  1071. shp->shm_perm.id,
  1072. shp->shm_perm.mode,
  1073. shp->shm_segsz,
  1074. shp->shm_cprid,
  1075. shp->shm_lprid,
  1076. shp->shm_nattch,
  1077. from_kuid_munged(user_ns, shp->shm_perm.uid),
  1078. from_kgid_munged(user_ns, shp->shm_perm.gid),
  1079. from_kuid_munged(user_ns, shp->shm_perm.cuid),
  1080. from_kgid_munged(user_ns, shp->shm_perm.cgid),
  1081. shp->shm_atim,
  1082. shp->shm_dtim,
  1083. shp->shm_ctim,
  1084. rss * PAGE_SIZE,
  1085. swp * PAGE_SIZE);
  1086. }
  1087. #endif