shm.c 25 KB

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