shm.c 28 KB

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