shm.c 28 KB

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