shm.c 22 KB

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