shm.c 26 KB

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