shm.c 21 KB

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