shm.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 <linux/ima.h>
  42. #include <asm/uaccess.h>
  43. #include "util.h"
  44. struct shm_file_data {
  45. int id;
  46. struct ipc_namespace *ns;
  47. struct file *file;
  48. const struct vm_operations_struct *vm_ops;
  49. };
  50. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  51. static const struct file_operations shm_file_operations;
  52. static const struct vm_operations_struct shm_vm_ops;
  53. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  54. #define shm_unlock(shp) \
  55. ipc_unlock(&(shp)->shm_perm)
  56. static int newseg(struct ipc_namespace *, struct ipc_params *);
  57. static void shm_open(struct vm_area_struct *vma);
  58. static void shm_close(struct vm_area_struct *vma);
  59. static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
  60. #ifdef CONFIG_PROC_FS
  61. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  62. #endif
  63. void shm_init_ns(struct ipc_namespace *ns)
  64. {
  65. ns->shm_ctlmax = SHMMAX;
  66. ns->shm_ctlall = SHMALL;
  67. ns->shm_ctlmni = SHMMNI;
  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. }
  92. #endif
  93. void __init shm_init (void)
  94. {
  95. shm_init_ns(&init_ipc_ns);
  96. ipc_init_proc_interface("sysvipc/shm",
  97. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
  98. IPC_SHM_IDS, sysvipc_shm_proc_show);
  99. }
  100. /*
  101. * shm_lock_(check_) routines are called in the paths where the rw_mutex
  102. * is not necessarily held.
  103. */
  104. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  105. {
  106. struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
  107. if (IS_ERR(ipcp))
  108. return (struct shmid_kernel *)ipcp;
  109. return container_of(ipcp, struct shmid_kernel, shm_perm);
  110. }
  111. static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
  112. int id)
  113. {
  114. struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
  115. if (IS_ERR(ipcp))
  116. return (struct shmid_kernel *)ipcp;
  117. return container_of(ipcp, struct shmid_kernel, shm_perm);
  118. }
  119. static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
  120. {
  121. ipc_rmid(&shm_ids(ns), &s->shm_perm);
  122. }
  123. /* This is called by fork, once for every shm attach. */
  124. static void shm_open(struct vm_area_struct *vma)
  125. {
  126. struct file *file = vma->vm_file;
  127. struct shm_file_data *sfd = shm_file_data(file);
  128. struct shmid_kernel *shp;
  129. shp = shm_lock(sfd->ns, sfd->id);
  130. BUG_ON(IS_ERR(shp));
  131. shp->shm_atim = get_seconds();
  132. shp->shm_lprid = task_tgid_vnr(current);
  133. shp->shm_nattch++;
  134. shm_unlock(shp);
  135. }
  136. /*
  137. * shm_destroy - free the struct shmid_kernel
  138. *
  139. * @ns: namespace
  140. * @shp: struct to free
  141. *
  142. * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
  143. * but returns with shp unlocked and freed.
  144. */
  145. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  146. {
  147. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  148. shm_rmid(ns, shp);
  149. shm_unlock(shp);
  150. if (!is_file_hugepages(shp->shm_file))
  151. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  152. else if (shp->mlock_user)
  153. user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
  154. shp->mlock_user);
  155. fput (shp->shm_file);
  156. security_shm_free(shp);
  157. ipc_rcu_putref(shp);
  158. }
  159. /*
  160. * remove the attach descriptor vma.
  161. * free memory for segment if it is marked destroyed.
  162. * The descriptor has already been removed from the current->mm->mmap list
  163. * and will later be kfree()d.
  164. */
  165. static void shm_close(struct vm_area_struct *vma)
  166. {
  167. struct file * file = vma->vm_file;
  168. struct shm_file_data *sfd = shm_file_data(file);
  169. struct shmid_kernel *shp;
  170. struct ipc_namespace *ns = sfd->ns;
  171. down_write(&shm_ids(ns).rw_mutex);
  172. /* remove from the list of attaches of the shm segment */
  173. shp = shm_lock(ns, sfd->id);
  174. BUG_ON(IS_ERR(shp));
  175. shp->shm_lprid = task_tgid_vnr(current);
  176. shp->shm_dtim = get_seconds();
  177. shp->shm_nattch--;
  178. if(shp->shm_nattch == 0 &&
  179. shp->shm_perm.mode & SHM_DEST)
  180. shm_destroy(ns, shp);
  181. else
  182. shm_unlock(shp);
  183. up_write(&shm_ids(ns).rw_mutex);
  184. }
  185. static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  186. {
  187. struct file *file = vma->vm_file;
  188. struct shm_file_data *sfd = shm_file_data(file);
  189. return sfd->vm_ops->fault(vma, vmf);
  190. }
  191. #ifdef CONFIG_NUMA
  192. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  193. {
  194. struct file *file = vma->vm_file;
  195. struct shm_file_data *sfd = shm_file_data(file);
  196. int err = 0;
  197. if (sfd->vm_ops->set_policy)
  198. err = sfd->vm_ops->set_policy(vma, new);
  199. return err;
  200. }
  201. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  202. unsigned long addr)
  203. {
  204. struct file *file = vma->vm_file;
  205. struct shm_file_data *sfd = shm_file_data(file);
  206. struct mempolicy *pol = NULL;
  207. if (sfd->vm_ops->get_policy)
  208. pol = sfd->vm_ops->get_policy(vma, addr);
  209. else if (vma->vm_policy)
  210. pol = vma->vm_policy;
  211. return pol;
  212. }
  213. #endif
  214. static int shm_mmap(struct file * file, struct vm_area_struct * vma)
  215. {
  216. struct shm_file_data *sfd = shm_file_data(file);
  217. int ret;
  218. ret = sfd->file->f_op->mmap(sfd->file, vma);
  219. if (ret != 0)
  220. return ret;
  221. sfd->vm_ops = vma->vm_ops;
  222. #ifdef CONFIG_MMU
  223. BUG_ON(!sfd->vm_ops->fault);
  224. #endif
  225. vma->vm_ops = &shm_vm_ops;
  226. shm_open(vma);
  227. return ret;
  228. }
  229. static int shm_release(struct inode *ino, struct file *file)
  230. {
  231. struct shm_file_data *sfd = shm_file_data(file);
  232. put_ipc_ns(sfd->ns);
  233. shm_file_data(file) = NULL;
  234. kfree(sfd);
  235. return 0;
  236. }
  237. static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
  238. {
  239. int (*fsync) (struct file *, struct dentry *, int datasync);
  240. struct shm_file_data *sfd = shm_file_data(file);
  241. int ret = -EINVAL;
  242. fsync = sfd->file->f_op->fsync;
  243. if (fsync)
  244. ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
  245. return ret;
  246. }
  247. static unsigned long shm_get_unmapped_area(struct file *file,
  248. unsigned long addr, unsigned long len, unsigned long pgoff,
  249. unsigned long flags)
  250. {
  251. struct shm_file_data *sfd = shm_file_data(file);
  252. return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
  253. }
  254. int is_file_shm_hugepages(struct file *file)
  255. {
  256. int ret = 0;
  257. if (file->f_op == &shm_file_operations) {
  258. struct shm_file_data *sfd;
  259. sfd = shm_file_data(file);
  260. ret = is_file_hugepages(sfd->file);
  261. }
  262. return ret;
  263. }
  264. static const struct file_operations shm_file_operations = {
  265. .mmap = shm_mmap,
  266. .fsync = shm_fsync,
  267. .release = shm_release,
  268. .get_unmapped_area = shm_get_unmapped_area,
  269. };
  270. static const struct vm_operations_struct shm_vm_ops = {
  271. .open = shm_open, /* callback for a new vm-area open */
  272. .close = shm_close, /* callback for when the vm-area is released */
  273. .fault = shm_fault,
  274. #if defined(CONFIG_NUMA)
  275. .set_policy = shm_set_policy,
  276. .get_policy = shm_get_policy,
  277. #endif
  278. };
  279. /**
  280. * newseg - Create a new shared memory segment
  281. * @ns: namespace
  282. * @params: ptr to the structure that contains key, size and shmflg
  283. *
  284. * Called with shm_ids.rw_mutex held as a writer.
  285. */
  286. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  287. {
  288. key_t key = params->key;
  289. int shmflg = params->flg;
  290. size_t size = params->u.size;
  291. int error;
  292. struct shmid_kernel *shp;
  293. int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
  294. struct file * file;
  295. char name[13];
  296. int id;
  297. int acctflag = 0;
  298. if (size < SHMMIN || size > ns->shm_ctlmax)
  299. return -EINVAL;
  300. if (ns->shm_tot + numpages > ns->shm_ctlall)
  301. return -ENOSPC;
  302. shp = ipc_rcu_alloc(sizeof(*shp));
  303. if (!shp)
  304. return -ENOMEM;
  305. shp->shm_perm.key = key;
  306. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  307. shp->mlock_user = NULL;
  308. shp->shm_perm.security = NULL;
  309. error = security_shm_alloc(shp);
  310. if (error) {
  311. ipc_rcu_putref(shp);
  312. return error;
  313. }
  314. sprintf (name, "SYSV%08x", key);
  315. if (shmflg & SHM_HUGETLB) {
  316. /* hugetlb_file_setup applies strict accounting */
  317. if (shmflg & SHM_NORESERVE)
  318. acctflag = VM_NORESERVE;
  319. file = hugetlb_file_setup(name, size, acctflag,
  320. &shp->mlock_user, HUGETLB_SHMFS_INODE);
  321. } else {
  322. /*
  323. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  324. * if it's asked for.
  325. */
  326. if ((shmflg & SHM_NORESERVE) &&
  327. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  328. acctflag = VM_NORESERVE;
  329. file = shmem_file_setup(name, size, acctflag);
  330. }
  331. error = PTR_ERR(file);
  332. if (IS_ERR(file))
  333. goto no_file;
  334. id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  335. if (id < 0) {
  336. error = id;
  337. goto no_id;
  338. }
  339. shp->shm_cprid = task_tgid_vnr(current);
  340. shp->shm_lprid = 0;
  341. shp->shm_atim = shp->shm_dtim = 0;
  342. shp->shm_ctim = get_seconds();
  343. shp->shm_segsz = size;
  344. shp->shm_nattch = 0;
  345. shp->shm_file = file;
  346. /*
  347. * shmid gets reported as "inode#" in /proc/pid/maps.
  348. * proc-ps tools use this. Changing this will break them.
  349. */
  350. file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
  351. ns->shm_tot += numpages;
  352. error = shp->shm_perm.id;
  353. shm_unlock(shp);
  354. return error;
  355. no_id:
  356. if (is_file_hugepages(file) && shp->mlock_user)
  357. user_shm_unlock(size, shp->mlock_user);
  358. fput(file);
  359. no_file:
  360. security_shm_free(shp);
  361. ipc_rcu_putref(shp);
  362. return error;
  363. }
  364. /*
  365. * Called with shm_ids.rw_mutex and ipcp locked.
  366. */
  367. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  368. {
  369. struct shmid_kernel *shp;
  370. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  371. return security_shm_associate(shp, shmflg);
  372. }
  373. /*
  374. * Called with shm_ids.rw_mutex and ipcp locked.
  375. */
  376. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  377. struct ipc_params *params)
  378. {
  379. struct shmid_kernel *shp;
  380. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  381. if (shp->shm_segsz < params->u.size)
  382. return -EINVAL;
  383. return 0;
  384. }
  385. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  386. {
  387. struct ipc_namespace *ns;
  388. struct ipc_ops shm_ops;
  389. struct ipc_params shm_params;
  390. ns = current->nsproxy->ipc_ns;
  391. shm_ops.getnew = newseg;
  392. shm_ops.associate = shm_security;
  393. shm_ops.more_checks = shm_more_checks;
  394. shm_params.key = key;
  395. shm_params.flg = shmflg;
  396. shm_params.u.size = size;
  397. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  398. }
  399. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  400. {
  401. switch(version) {
  402. case IPC_64:
  403. return copy_to_user(buf, in, sizeof(*in));
  404. case IPC_OLD:
  405. {
  406. struct shmid_ds out;
  407. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  408. out.shm_segsz = in->shm_segsz;
  409. out.shm_atime = in->shm_atime;
  410. out.shm_dtime = in->shm_dtime;
  411. out.shm_ctime = in->shm_ctime;
  412. out.shm_cpid = in->shm_cpid;
  413. out.shm_lpid = in->shm_lpid;
  414. out.shm_nattch = in->shm_nattch;
  415. return copy_to_user(buf, &out, sizeof(out));
  416. }
  417. default:
  418. return -EINVAL;
  419. }
  420. }
  421. static inline unsigned long
  422. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  423. {
  424. switch(version) {
  425. case IPC_64:
  426. if (copy_from_user(out, buf, sizeof(*out)))
  427. return -EFAULT;
  428. return 0;
  429. case IPC_OLD:
  430. {
  431. struct shmid_ds tbuf_old;
  432. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  433. return -EFAULT;
  434. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  435. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  436. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  437. return 0;
  438. }
  439. default:
  440. return -EINVAL;
  441. }
  442. }
  443. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  444. {
  445. switch(version) {
  446. case IPC_64:
  447. return copy_to_user(buf, in, sizeof(*in));
  448. case IPC_OLD:
  449. {
  450. struct shminfo out;
  451. if(in->shmmax > INT_MAX)
  452. out.shmmax = INT_MAX;
  453. else
  454. out.shmmax = (int)in->shmmax;
  455. out.shmmin = in->shmmin;
  456. out.shmmni = in->shmmni;
  457. out.shmseg = in->shmseg;
  458. out.shmall = in->shmall;
  459. return copy_to_user(buf, &out, sizeof(out));
  460. }
  461. default:
  462. return -EINVAL;
  463. }
  464. }
  465. /*
  466. * Called with shm_ids.rw_mutex held as a reader
  467. */
  468. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  469. unsigned long *swp)
  470. {
  471. int next_id;
  472. int total, in_use;
  473. *rss = 0;
  474. *swp = 0;
  475. in_use = shm_ids(ns).in_use;
  476. for (total = 0, next_id = 0; total < in_use; next_id++) {
  477. struct kern_ipc_perm *ipc;
  478. struct shmid_kernel *shp;
  479. struct inode *inode;
  480. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  481. if (ipc == NULL)
  482. continue;
  483. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  484. inode = shp->shm_file->f_path.dentry->d_inode;
  485. if (is_file_hugepages(shp->shm_file)) {
  486. struct address_space *mapping = inode->i_mapping;
  487. struct hstate *h = hstate_file(shp->shm_file);
  488. *rss += pages_per_huge_page(h) * mapping->nrpages;
  489. } else {
  490. #ifdef CONFIG_SHMEM
  491. struct shmem_inode_info *info = SHMEM_I(inode);
  492. spin_lock(&info->lock);
  493. *rss += inode->i_mapping->nrpages;
  494. *swp += info->swapped;
  495. spin_unlock(&info->lock);
  496. #else
  497. *rss += inode->i_mapping->nrpages;
  498. #endif
  499. }
  500. total++;
  501. }
  502. }
  503. /*
  504. * This function handles some shmctl commands which require the rw_mutex
  505. * to be held in write mode.
  506. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  507. */
  508. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  509. struct shmid_ds __user *buf, int version)
  510. {
  511. struct kern_ipc_perm *ipcp;
  512. struct shmid64_ds shmid64;
  513. struct shmid_kernel *shp;
  514. int err;
  515. if (cmd == IPC_SET) {
  516. if (copy_shmid_from_user(&shmid64, buf, version))
  517. return -EFAULT;
  518. }
  519. ipcp = ipcctl_pre_down(&shm_ids(ns), shmid, cmd, &shmid64.shm_perm, 0);
  520. if (IS_ERR(ipcp))
  521. return PTR_ERR(ipcp);
  522. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  523. err = security_shm_shmctl(shp, cmd);
  524. if (err)
  525. goto out_unlock;
  526. switch (cmd) {
  527. case IPC_RMID:
  528. do_shm_rmid(ns, ipcp);
  529. goto out_up;
  530. case IPC_SET:
  531. ipc_update_perm(&shmid64.shm_perm, ipcp);
  532. shp->shm_ctim = get_seconds();
  533. break;
  534. default:
  535. err = -EINVAL;
  536. }
  537. out_unlock:
  538. shm_unlock(shp);
  539. out_up:
  540. up_write(&shm_ids(ns).rw_mutex);
  541. return err;
  542. }
  543. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  544. {
  545. struct shmid_kernel *shp;
  546. int err, version;
  547. struct ipc_namespace *ns;
  548. if (cmd < 0 || shmid < 0) {
  549. err = -EINVAL;
  550. goto out;
  551. }
  552. version = ipc_parse_version(&cmd);
  553. ns = current->nsproxy->ipc_ns;
  554. switch (cmd) { /* replace with proc interface ? */
  555. case IPC_INFO:
  556. {
  557. struct shminfo64 shminfo;
  558. err = security_shm_shmctl(NULL, cmd);
  559. if (err)
  560. return err;
  561. memset(&shminfo, 0, sizeof(shminfo));
  562. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  563. shminfo.shmmax = ns->shm_ctlmax;
  564. shminfo.shmall = ns->shm_ctlall;
  565. shminfo.shmmin = SHMMIN;
  566. if(copy_shminfo_to_user (buf, &shminfo, version))
  567. return -EFAULT;
  568. down_read(&shm_ids(ns).rw_mutex);
  569. err = ipc_get_maxid(&shm_ids(ns));
  570. up_read(&shm_ids(ns).rw_mutex);
  571. if(err<0)
  572. err = 0;
  573. goto out;
  574. }
  575. case SHM_INFO:
  576. {
  577. struct shm_info shm_info;
  578. err = security_shm_shmctl(NULL, cmd);
  579. if (err)
  580. return err;
  581. memset(&shm_info, 0, sizeof(shm_info));
  582. down_read(&shm_ids(ns).rw_mutex);
  583. shm_info.used_ids = shm_ids(ns).in_use;
  584. shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
  585. shm_info.shm_tot = ns->shm_tot;
  586. shm_info.swap_attempts = 0;
  587. shm_info.swap_successes = 0;
  588. err = ipc_get_maxid(&shm_ids(ns));
  589. up_read(&shm_ids(ns).rw_mutex);
  590. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  591. err = -EFAULT;
  592. goto out;
  593. }
  594. err = err < 0 ? 0 : err;
  595. goto out;
  596. }
  597. case SHM_STAT:
  598. case IPC_STAT:
  599. {
  600. struct shmid64_ds tbuf;
  601. int result;
  602. if (cmd == SHM_STAT) {
  603. shp = shm_lock(ns, shmid);
  604. if (IS_ERR(shp)) {
  605. err = PTR_ERR(shp);
  606. goto out;
  607. }
  608. result = shp->shm_perm.id;
  609. } else {
  610. shp = shm_lock_check(ns, shmid);
  611. if (IS_ERR(shp)) {
  612. err = PTR_ERR(shp);
  613. goto out;
  614. }
  615. result = 0;
  616. }
  617. err = -EACCES;
  618. if (ipcperms (&shp->shm_perm, S_IRUGO))
  619. goto out_unlock;
  620. err = security_shm_shmctl(shp, cmd);
  621. if (err)
  622. goto out_unlock;
  623. memset(&tbuf, 0, sizeof(tbuf));
  624. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  625. tbuf.shm_segsz = shp->shm_segsz;
  626. tbuf.shm_atime = shp->shm_atim;
  627. tbuf.shm_dtime = shp->shm_dtim;
  628. tbuf.shm_ctime = shp->shm_ctim;
  629. tbuf.shm_cpid = shp->shm_cprid;
  630. tbuf.shm_lpid = shp->shm_lprid;
  631. tbuf.shm_nattch = shp->shm_nattch;
  632. shm_unlock(shp);
  633. if(copy_shmid_to_user (buf, &tbuf, version))
  634. err = -EFAULT;
  635. else
  636. err = result;
  637. goto out;
  638. }
  639. case SHM_LOCK:
  640. case SHM_UNLOCK:
  641. {
  642. struct file *uninitialized_var(shm_file);
  643. lru_add_drain_all(); /* drain pagevecs to lru lists */
  644. shp = shm_lock_check(ns, shmid);
  645. if (IS_ERR(shp)) {
  646. err = PTR_ERR(shp);
  647. goto out;
  648. }
  649. audit_ipc_obj(&(shp->shm_perm));
  650. if (!capable(CAP_IPC_LOCK)) {
  651. uid_t euid = current_euid();
  652. err = -EPERM;
  653. if (euid != shp->shm_perm.uid &&
  654. euid != shp->shm_perm.cuid)
  655. goto out_unlock;
  656. if (cmd == SHM_LOCK &&
  657. !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
  658. goto out_unlock;
  659. }
  660. err = security_shm_shmctl(shp, cmd);
  661. if (err)
  662. goto out_unlock;
  663. if(cmd==SHM_LOCK) {
  664. struct user_struct *user = current_user();
  665. if (!is_file_hugepages(shp->shm_file)) {
  666. err = shmem_lock(shp->shm_file, 1, user);
  667. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
  668. shp->shm_perm.mode |= SHM_LOCKED;
  669. shp->mlock_user = user;
  670. }
  671. }
  672. } else if (!is_file_hugepages(shp->shm_file)) {
  673. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  674. shp->shm_perm.mode &= ~SHM_LOCKED;
  675. shp->mlock_user = NULL;
  676. }
  677. shm_unlock(shp);
  678. goto out;
  679. }
  680. case IPC_RMID:
  681. case IPC_SET:
  682. err = shmctl_down(ns, shmid, cmd, buf, version);
  683. return err;
  684. default:
  685. return -EINVAL;
  686. }
  687. out_unlock:
  688. shm_unlock(shp);
  689. out:
  690. return err;
  691. }
  692. /*
  693. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  694. *
  695. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  696. * "raddr" thing points to kernel space, and there has to be a wrapper around
  697. * this.
  698. */
  699. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
  700. {
  701. struct shmid_kernel *shp;
  702. unsigned long addr;
  703. unsigned long size;
  704. struct file * file;
  705. int err;
  706. unsigned long flags;
  707. unsigned long prot;
  708. int acc_mode;
  709. unsigned long user_addr;
  710. struct ipc_namespace *ns;
  711. struct shm_file_data *sfd;
  712. struct path path;
  713. fmode_t f_mode;
  714. err = -EINVAL;
  715. if (shmid < 0)
  716. goto out;
  717. else if ((addr = (ulong)shmaddr)) {
  718. if (addr & (SHMLBA-1)) {
  719. if (shmflg & SHM_RND)
  720. addr &= ~(SHMLBA-1); /* round down */
  721. else
  722. #ifndef __ARCH_FORCE_SHMLBA
  723. if (addr & ~PAGE_MASK)
  724. #endif
  725. goto out;
  726. }
  727. flags = MAP_SHARED | MAP_FIXED;
  728. } else {
  729. if ((shmflg & SHM_REMAP))
  730. goto out;
  731. flags = MAP_SHARED;
  732. }
  733. if (shmflg & SHM_RDONLY) {
  734. prot = PROT_READ;
  735. acc_mode = S_IRUGO;
  736. f_mode = FMODE_READ;
  737. } else {
  738. prot = PROT_READ | PROT_WRITE;
  739. acc_mode = S_IRUGO | S_IWUGO;
  740. f_mode = FMODE_READ | FMODE_WRITE;
  741. }
  742. if (shmflg & SHM_EXEC) {
  743. prot |= PROT_EXEC;
  744. acc_mode |= S_IXUGO;
  745. }
  746. /*
  747. * We cannot rely on the fs check since SYSV IPC does have an
  748. * additional creator id...
  749. */
  750. ns = current->nsproxy->ipc_ns;
  751. shp = shm_lock_check(ns, shmid);
  752. if (IS_ERR(shp)) {
  753. err = PTR_ERR(shp);
  754. goto out;
  755. }
  756. err = -EACCES;
  757. if (ipcperms(&shp->shm_perm, acc_mode))
  758. goto out_unlock;
  759. err = security_shm_shmat(shp, shmaddr, shmflg);
  760. if (err)
  761. goto out_unlock;
  762. path.dentry = dget(shp->shm_file->f_path.dentry);
  763. path.mnt = shp->shm_file->f_path.mnt;
  764. shp->shm_nattch++;
  765. size = i_size_read(path.dentry->d_inode);
  766. shm_unlock(shp);
  767. err = -ENOMEM;
  768. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  769. if (!sfd)
  770. goto out_put_dentry;
  771. file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
  772. if (!file)
  773. goto out_free;
  774. ima_counts_get(file);
  775. file->private_data = sfd;
  776. file->f_mapping = shp->shm_file->f_mapping;
  777. sfd->id = shp->shm_perm.id;
  778. sfd->ns = get_ipc_ns(ns);
  779. sfd->file = shp->shm_file;
  780. sfd->vm_ops = NULL;
  781. down_write(&current->mm->mmap_sem);
  782. if (addr && !(shmflg & SHM_REMAP)) {
  783. err = -EINVAL;
  784. if (find_vma_intersection(current->mm, addr, addr + size))
  785. goto invalid;
  786. /*
  787. * If shm segment goes below stack, make sure there is some
  788. * space left for the stack to grow (at least 4 pages).
  789. */
  790. if (addr < current->mm->start_stack &&
  791. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  792. goto invalid;
  793. }
  794. user_addr = do_mmap (file, addr, size, prot, flags, 0);
  795. *raddr = user_addr;
  796. err = 0;
  797. if (IS_ERR_VALUE(user_addr))
  798. err = (long)user_addr;
  799. invalid:
  800. up_write(&current->mm->mmap_sem);
  801. fput(file);
  802. out_nattch:
  803. down_write(&shm_ids(ns).rw_mutex);
  804. shp = shm_lock(ns, shmid);
  805. BUG_ON(IS_ERR(shp));
  806. shp->shm_nattch--;
  807. if(shp->shm_nattch == 0 &&
  808. shp->shm_perm.mode & SHM_DEST)
  809. shm_destroy(ns, shp);
  810. else
  811. shm_unlock(shp);
  812. up_write(&shm_ids(ns).rw_mutex);
  813. out:
  814. return err;
  815. out_unlock:
  816. shm_unlock(shp);
  817. goto out;
  818. out_free:
  819. kfree(sfd);
  820. out_put_dentry:
  821. dput(path.dentry);
  822. goto out_nattch;
  823. }
  824. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  825. {
  826. unsigned long ret;
  827. long err;
  828. err = do_shmat(shmid, shmaddr, shmflg, &ret);
  829. if (err)
  830. return err;
  831. force_successful_syscall_return();
  832. return (long)ret;
  833. }
  834. /*
  835. * detach and kill segment if marked destroyed.
  836. * The work is done in shm_close.
  837. */
  838. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  839. {
  840. struct mm_struct *mm = current->mm;
  841. struct vm_area_struct *vma;
  842. unsigned long addr = (unsigned long)shmaddr;
  843. int retval = -EINVAL;
  844. #ifdef CONFIG_MMU
  845. loff_t size = 0;
  846. struct vm_area_struct *next;
  847. #endif
  848. if (addr & ~PAGE_MASK)
  849. return retval;
  850. down_write(&mm->mmap_sem);
  851. /*
  852. * This function tries to be smart and unmap shm segments that
  853. * were modified by partial mlock or munmap calls:
  854. * - It first determines the size of the shm segment that should be
  855. * unmapped: It searches for a vma that is backed by shm and that
  856. * started at address shmaddr. It records it's size and then unmaps
  857. * it.
  858. * - Then it unmaps all shm vmas that started at shmaddr and that
  859. * are within the initially determined size.
  860. * Errors from do_munmap are ignored: the function only fails if
  861. * it's called with invalid parameters or if it's called to unmap
  862. * a part of a vma. Both calls in this function are for full vmas,
  863. * the parameters are directly copied from the vma itself and always
  864. * valid - therefore do_munmap cannot fail. (famous last words?)
  865. */
  866. /*
  867. * If it had been mremap()'d, the starting address would not
  868. * match the usual checks anyway. So assume all vma's are
  869. * above the starting address given.
  870. */
  871. vma = find_vma(mm, addr);
  872. #ifdef CONFIG_MMU
  873. while (vma) {
  874. next = vma->vm_next;
  875. /*
  876. * Check if the starting address would match, i.e. it's
  877. * a fragment created by mprotect() and/or munmap(), or it
  878. * otherwise it starts at this address with no hassles.
  879. */
  880. if ((vma->vm_ops == &shm_vm_ops) &&
  881. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  882. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  883. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  884. /*
  885. * We discovered the size of the shm segment, so
  886. * break out of here and fall through to the next
  887. * loop that uses the size information to stop
  888. * searching for matching vma's.
  889. */
  890. retval = 0;
  891. vma = next;
  892. break;
  893. }
  894. vma = next;
  895. }
  896. /*
  897. * We need look no further than the maximum address a fragment
  898. * could possibly have landed at. Also cast things to loff_t to
  899. * prevent overflows and make comparisions vs. equal-width types.
  900. */
  901. size = PAGE_ALIGN(size);
  902. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  903. next = vma->vm_next;
  904. /* finding a matching vma now does not alter retval */
  905. if ((vma->vm_ops == &shm_vm_ops) &&
  906. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  907. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  908. vma = next;
  909. }
  910. #else /* CONFIG_MMU */
  911. /* under NOMMU conditions, the exact address to be destroyed must be
  912. * given */
  913. retval = -EINVAL;
  914. if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  915. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  916. retval = 0;
  917. }
  918. #endif
  919. up_write(&mm->mmap_sem);
  920. return retval;
  921. }
  922. #ifdef CONFIG_PROC_FS
  923. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  924. {
  925. struct shmid_kernel *shp = it;
  926. #if BITS_PER_LONG <= 32
  927. #define SIZE_SPEC "%10lu"
  928. #else
  929. #define SIZE_SPEC "%21lu"
  930. #endif
  931. return seq_printf(s,
  932. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  933. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu\n",
  934. shp->shm_perm.key,
  935. shp->shm_perm.id,
  936. shp->shm_perm.mode,
  937. shp->shm_segsz,
  938. shp->shm_cprid,
  939. shp->shm_lprid,
  940. shp->shm_nattch,
  941. shp->shm_perm.uid,
  942. shp->shm_perm.gid,
  943. shp->shm_perm.cuid,
  944. shp->shm_perm.cgid,
  945. shp->shm_atim,
  946. shp->shm_dtim,
  947. shp->shm_ctim);
  948. }
  949. #endif