shm.c 25 KB

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