shm.c 21 KB

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