shm.c 25 KB

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