shm.c 26 KB

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