shm.c 21 KB

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