shm.c 25 KB

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