shm.c 26 KB

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