shm.c 22 KB

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