shm.c 21 KB

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