shm.c 26 KB

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