shm.c 26 KB

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