shm.c 24 KB

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