shm.c 24 KB

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