shm.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. err = -ENOMEM;
  775. file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
  776. if (!file)
  777. goto out_free;
  778. file->private_data = sfd;
  779. file->f_mapping = shp->shm_file->f_mapping;
  780. sfd->id = shp->shm_perm.id;
  781. sfd->ns = get_ipc_ns(ns);
  782. sfd->file = shp->shm_file;
  783. sfd->vm_ops = NULL;
  784. down_write(&current->mm->mmap_sem);
  785. if (addr && !(shmflg & SHM_REMAP)) {
  786. err = -EINVAL;
  787. if (find_vma_intersection(current->mm, addr, addr + size))
  788. goto invalid;
  789. /*
  790. * If shm segment goes below stack, make sure there is some
  791. * space left for the stack to grow (at least 4 pages).
  792. */
  793. if (addr < current->mm->start_stack &&
  794. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  795. goto invalid;
  796. }
  797. user_addr = do_mmap (file, addr, size, prot, flags, 0);
  798. *raddr = user_addr;
  799. err = 0;
  800. if (IS_ERR_VALUE(user_addr))
  801. err = (long)user_addr;
  802. invalid:
  803. up_write(&current->mm->mmap_sem);
  804. fput(file);
  805. out_nattch:
  806. down_write(&shm_ids(ns).rw_mutex);
  807. shp = shm_lock_down(ns, shmid);
  808. BUG_ON(IS_ERR(shp));
  809. shp->shm_nattch--;
  810. if(shp->shm_nattch == 0 &&
  811. shp->shm_perm.mode & SHM_DEST)
  812. shm_destroy(ns, shp);
  813. else
  814. shm_unlock(shp);
  815. up_write(&shm_ids(ns).rw_mutex);
  816. out:
  817. return err;
  818. out_unlock:
  819. shm_unlock(shp);
  820. goto out;
  821. out_free:
  822. kfree(sfd);
  823. out_put_dentry:
  824. dput(path.dentry);
  825. goto out_nattch;
  826. }
  827. asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
  828. {
  829. unsigned long ret;
  830. long err;
  831. err = do_shmat(shmid, shmaddr, shmflg, &ret);
  832. if (err)
  833. return err;
  834. force_successful_syscall_return();
  835. return (long)ret;
  836. }
  837. /*
  838. * detach and kill segment if marked destroyed.
  839. * The work is done in shm_close.
  840. */
  841. asmlinkage long sys_shmdt(char __user *shmaddr)
  842. {
  843. struct mm_struct *mm = current->mm;
  844. struct vm_area_struct *vma, *next;
  845. unsigned long addr = (unsigned long)shmaddr;
  846. loff_t size = 0;
  847. int retval = -EINVAL;
  848. if (addr & ~PAGE_MASK)
  849. return retval;
  850. down_write(&mm->mmap_sem);
  851. /*
  852. * This function tries to be smart and unmap shm segments that
  853. * were modified by partial mlock or munmap calls:
  854. * - It first determines the size of the shm segment that should be
  855. * unmapped: It searches for a vma that is backed by shm and that
  856. * started at address shmaddr. It records it's size and then unmaps
  857. * it.
  858. * - Then it unmaps all shm vmas that started at shmaddr and that
  859. * are within the initially determined size.
  860. * Errors from do_munmap are ignored: the function only fails if
  861. * it's called with invalid parameters or if it's called to unmap
  862. * a part of a vma. Both calls in this function are for full vmas,
  863. * the parameters are directly copied from the vma itself and always
  864. * valid - therefore do_munmap cannot fail. (famous last words?)
  865. */
  866. /*
  867. * If it had been mremap()'d, the starting address would not
  868. * match the usual checks anyway. So assume all vma's are
  869. * above the starting address given.
  870. */
  871. vma = find_vma(mm, addr);
  872. while (vma) {
  873. next = vma->vm_next;
  874. /*
  875. * Check if the starting address would match, i.e. it's
  876. * a fragment created by mprotect() and/or munmap(), or it
  877. * otherwise it starts at this address with no hassles.
  878. */
  879. if ((vma->vm_ops == &shm_vm_ops) &&
  880. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  881. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  882. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  883. /*
  884. * We discovered the size of the shm segment, so
  885. * break out of here and fall through to the next
  886. * loop that uses the size information to stop
  887. * searching for matching vma's.
  888. */
  889. retval = 0;
  890. vma = next;
  891. break;
  892. }
  893. vma = next;
  894. }
  895. /*
  896. * We need look no further than the maximum address a fragment
  897. * could possibly have landed at. Also cast things to loff_t to
  898. * prevent overflows and make comparisions vs. equal-width types.
  899. */
  900. size = PAGE_ALIGN(size);
  901. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  902. next = vma->vm_next;
  903. /* finding a matching vma now does not alter retval */
  904. if ((vma->vm_ops == &shm_vm_ops) &&
  905. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  906. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  907. vma = next;
  908. }
  909. up_write(&mm->mmap_sem);
  910. return retval;
  911. }
  912. #ifdef CONFIG_PROC_FS
  913. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  914. {
  915. struct shmid_kernel *shp = it;
  916. char *format;
  917. #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
  918. #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
  919. if (sizeof(size_t) <= sizeof(int))
  920. format = SMALL_STRING;
  921. else
  922. format = BIG_STRING;
  923. return seq_printf(s, format,
  924. shp->shm_perm.key,
  925. shp->shm_perm.id,
  926. shp->shm_perm.mode,
  927. shp->shm_segsz,
  928. shp->shm_cprid,
  929. shp->shm_lprid,
  930. shp->shm_nattch,
  931. shp->shm_perm.uid,
  932. shp->shm_perm.gid,
  933. shp->shm_perm.cuid,
  934. shp->shm_perm.cgid,
  935. shp->shm_atim,
  936. shp->shm_dtim,
  937. shp->shm_ctim);
  938. }
  939. #endif