shm.c 26 KB

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