shm.c 26 KB

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