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
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/hugetlb.h>
27 #include <linux/shm.h>
28 #include <linux/init.h>
29 #include <linux/file.h>
30 #include <linux/mman.h>
31 #include <linux/shmem_fs.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/audit.h>
35 #include <linux/capability.h>
36 #include <linux/ptrace.h>
37 #include <linux/seq_file.h>
38 #include <linux/rwsem.h>
39 #include <linux/nsproxy.h>
40 #include <linux/mount.h>
41 #include <linux/ipc_namespace.h>
42
43 #include <asm/uaccess.h>
44
45 #include "util.h"
46
47 struct shm_file_data {
48 int id;
49 struct ipc_namespace *ns;
50 struct file *file;
51 const struct vm_operations_struct *vm_ops;
52 };
53
54 #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
55
56 static const struct file_operations shm_file_operations;
57 static const struct vm_operations_struct shm_vm_ops;
58
59 #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
60
61 #define shm_unlock(shp) \
62 ipc_unlock(&(shp)->shm_perm)
63
64 static int newseg(struct ipc_namespace *, struct ipc_params *);
65 static void shm_open(struct vm_area_struct *vma);
66 static void shm_close(struct vm_area_struct *vma);
67 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
68 #ifdef CONFIG_PROC_FS
69 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
70 #endif
71
shm_init_ns(struct ipc_namespace * ns)72 void shm_init_ns(struct ipc_namespace *ns)
73 {
74 ns->shm_ctlmax = SHMMAX;
75 ns->shm_ctlall = SHMALL;
76 ns->shm_ctlmni = SHMMNI;
77 ns->shm_rmid_forced = 0;
78 ns->shm_tot = 0;
79 ipc_init_ids(&shm_ids(ns));
80 }
81
82 /*
83 * Called with shm_ids.rw_mutex (writer) and the shp structure locked.
84 * Only shm_ids.rw_mutex remains locked on exit.
85 */
do_shm_rmid(struct ipc_namespace * ns,struct kern_ipc_perm * ipcp)86 static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
87 {
88 struct shmid_kernel *shp;
89 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
90
91 if (shp->shm_nattch){
92 shp->shm_perm.mode |= SHM_DEST;
93 /* Do not find it any more */
94 shp->shm_perm.key = IPC_PRIVATE;
95 shm_unlock(shp);
96 } else
97 shm_destroy(ns, shp);
98 }
99
100 #ifdef CONFIG_IPC_NS
shm_exit_ns(struct ipc_namespace * ns)101 void shm_exit_ns(struct ipc_namespace *ns)
102 {
103 free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
104 idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
105 }
106 #endif
107
ipc_ns_init(void)108 static int __init ipc_ns_init(void)
109 {
110 shm_init_ns(&init_ipc_ns);
111 return 0;
112 }
113
114 pure_initcall(ipc_ns_init);
115
shm_init(void)116 void __init shm_init (void)
117 {
118 ipc_init_proc_interface("sysvipc/shm",
119 #if BITS_PER_LONG <= 32
120 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
121 #else
122 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
123 #endif
124 IPC_SHM_IDS, sysvipc_shm_proc_show);
125 }
126
127 /*
128 * shm_lock_(check_) routines are called in the paths where the rw_mutex
129 * is not necessarily held.
130 */
shm_lock(struct ipc_namespace * ns,int id)131 static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
132 {
133 struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
134
135 if (IS_ERR(ipcp))
136 return (struct shmid_kernel *)ipcp;
137
138 return container_of(ipcp, struct shmid_kernel, shm_perm);
139 }
140
shm_lock_by_ptr(struct shmid_kernel * ipcp)141 static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
142 {
143 rcu_read_lock();
144 spin_lock(&ipcp->shm_perm.lock);
145 }
146
shm_lock_check(struct ipc_namespace * ns,int id)147 static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
148 int id)
149 {
150 struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
151
152 if (IS_ERR(ipcp))
153 return (struct shmid_kernel *)ipcp;
154
155 return container_of(ipcp, struct shmid_kernel, shm_perm);
156 }
157
shm_rcu_free(struct rcu_head * head)158 static void shm_rcu_free(struct rcu_head *head)
159 {
160 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
161 struct shmid_kernel *shp = ipc_rcu_to_struct(p);
162
163 security_shm_free(shp);
164 ipc_rcu_free(head);
165 }
166
shm_rmid(struct ipc_namespace * ns,struct shmid_kernel * s)167 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
168 {
169 ipc_rmid(&shm_ids(ns), &s->shm_perm);
170 }
171
172
173 /* This is called by fork, once for every shm attach. */
shm_open(struct vm_area_struct * vma)174 static void shm_open(struct vm_area_struct *vma)
175 {
176 struct file *file = vma->vm_file;
177 struct shm_file_data *sfd = shm_file_data(file);
178 struct shmid_kernel *shp;
179
180 shp = shm_lock(sfd->ns, sfd->id);
181 BUG_ON(IS_ERR(shp));
182 shp->shm_atim = get_seconds();
183 shp->shm_lprid = task_tgid_vnr(current);
184 shp->shm_nattch++;
185 shm_unlock(shp);
186 }
187
188 /*
189 * shm_destroy - free the struct shmid_kernel
190 *
191 * @ns: namespace
192 * @shp: struct to free
193 *
194 * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
195 * but returns with shp unlocked and freed.
196 */
shm_destroy(struct ipc_namespace * ns,struct shmid_kernel * shp)197 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
198 {
199 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
200 shm_rmid(ns, shp);
201 shm_unlock(shp);
202 if (!is_file_hugepages(shp->shm_file))
203 shmem_lock(shp->shm_file, 0, shp->mlock_user);
204 else if (shp->mlock_user)
205 user_shm_unlock(file_inode(shp->shm_file)->i_size,
206 shp->mlock_user);
207 fput (shp->shm_file);
208 ipc_rcu_putref(shp, shm_rcu_free);
209 }
210
211 /*
212 * shm_may_destroy - identifies whether shm segment should be destroyed now
213 *
214 * Returns true if and only if there are no active users of the segment and
215 * one of the following is true:
216 *
217 * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
218 *
219 * 2) sysctl kernel.shm_rmid_forced is set to 1.
220 */
shm_may_destroy(struct ipc_namespace * ns,struct shmid_kernel * shp)221 static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
222 {
223 return (shp->shm_nattch == 0) &&
224 (ns->shm_rmid_forced ||
225 (shp->shm_perm.mode & SHM_DEST));
226 }
227
228 /*
229 * remove the attach descriptor vma.
230 * free memory for segment if it is marked destroyed.
231 * The descriptor has already been removed from the current->mm->mmap list
232 * and will later be kfree()d.
233 */
shm_close(struct vm_area_struct * vma)234 static void shm_close(struct vm_area_struct *vma)
235 {
236 struct file * file = vma->vm_file;
237 struct shm_file_data *sfd = shm_file_data(file);
238 struct shmid_kernel *shp;
239 struct ipc_namespace *ns = sfd->ns;
240
241 down_write(&shm_ids(ns).rw_mutex);
242 /* remove from the list of attaches of the shm segment */
243 shp = shm_lock(ns, sfd->id);
244 BUG_ON(IS_ERR(shp));
245 shp->shm_lprid = task_tgid_vnr(current);
246 shp->shm_dtim = get_seconds();
247 shp->shm_nattch--;
248 if (shm_may_destroy(ns, shp))
249 shm_destroy(ns, shp);
250 else
251 shm_unlock(shp);
252 up_write(&shm_ids(ns).rw_mutex);
253 }
254
255 /* Called with ns->shm_ids(ns).rw_mutex locked */
shm_try_destroy_current(int id,void * p,void * data)256 static int shm_try_destroy_current(int id, void *p, void *data)
257 {
258 struct ipc_namespace *ns = data;
259 struct kern_ipc_perm *ipcp = p;
260 struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
261
262 if (shp->shm_creator != current)
263 return 0;
264
265 /*
266 * Mark it as orphaned to destroy the segment when
267 * kernel.shm_rmid_forced is changed.
268 * It is noop if the following shm_may_destroy() returns true.
269 */
270 shp->shm_creator = NULL;
271
272 /*
273 * Don't even try to destroy it. If shm_rmid_forced=0 and IPC_RMID
274 * is not set, it shouldn't be deleted here.
275 */
276 if (!ns->shm_rmid_forced)
277 return 0;
278
279 if (shm_may_destroy(ns, shp)) {
280 shm_lock_by_ptr(shp);
281 shm_destroy(ns, shp);
282 }
283 return 0;
284 }
285
286 /* Called with ns->shm_ids(ns).rw_mutex locked */
shm_try_destroy_orphaned(int id,void * p,void * data)287 static int shm_try_destroy_orphaned(int id, void *p, void *data)
288 {
289 struct ipc_namespace *ns = data;
290 struct kern_ipc_perm *ipcp = p;
291 struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
292
293 /*
294 * We want to destroy segments without users and with already
295 * exit'ed originating process.
296 *
297 * As shp->* are changed under rw_mutex, it's safe to skip shp locking.
298 */
299 if (shp->shm_creator != NULL)
300 return 0;
301
302 if (shm_may_destroy(ns, shp)) {
303 shm_lock_by_ptr(shp);
304 shm_destroy(ns, shp);
305 }
306 return 0;
307 }
308
shm_destroy_orphaned(struct ipc_namespace * ns)309 void shm_destroy_orphaned(struct ipc_namespace *ns)
310 {
311 down_write(&shm_ids(ns).rw_mutex);
312 if (shm_ids(ns).in_use)
313 idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
314 up_write(&shm_ids(ns).rw_mutex);
315 }
316
317
exit_shm(struct task_struct * task)318 void exit_shm(struct task_struct *task)
319 {
320 struct ipc_namespace *ns = task->nsproxy->ipc_ns;
321
322 if (shm_ids(ns).in_use == 0)
323 return;
324
325 /* Destroy all already created segments, but not mapped yet */
326 down_write(&shm_ids(ns).rw_mutex);
327 if (shm_ids(ns).in_use)
328 idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_current, ns);
329 up_write(&shm_ids(ns).rw_mutex);
330 }
331
shm_fault(struct vm_area_struct * vma,struct vm_fault * vmf)332 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
333 {
334 struct file *file = vma->vm_file;
335 struct shm_file_data *sfd = shm_file_data(file);
336
337 return sfd->vm_ops->fault(vma, vmf);
338 }
339
340 #ifdef CONFIG_NUMA
shm_set_policy(struct vm_area_struct * vma,struct mempolicy * new)341 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
342 {
343 struct file *file = vma->vm_file;
344 struct shm_file_data *sfd = shm_file_data(file);
345 int err = 0;
346 if (sfd->vm_ops->set_policy)
347 err = sfd->vm_ops->set_policy(vma, new);
348 return err;
349 }
350
shm_get_policy(struct vm_area_struct * vma,unsigned long addr)351 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
352 unsigned long addr)
353 {
354 struct file *file = vma->vm_file;
355 struct shm_file_data *sfd = shm_file_data(file);
356 struct mempolicy *pol = NULL;
357
358 if (sfd->vm_ops->get_policy)
359 pol = sfd->vm_ops->get_policy(vma, addr);
360 else if (vma->vm_policy)
361 pol = vma->vm_policy;
362
363 return pol;
364 }
365 #endif
366
shm_mmap(struct file * file,struct vm_area_struct * vma)367 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
368 {
369 struct shm_file_data *sfd = shm_file_data(file);
370 int ret;
371
372 ret = sfd->file->f_op->mmap(sfd->file, vma);
373 if (ret != 0)
374 return ret;
375 sfd->vm_ops = vma->vm_ops;
376 #ifdef CONFIG_MMU
377 BUG_ON(!sfd->vm_ops->fault);
378 #endif
379 vma->vm_ops = &shm_vm_ops;
380 shm_open(vma);
381
382 return ret;
383 }
384
shm_release(struct inode * ino,struct file * file)385 static int shm_release(struct inode *ino, struct file *file)
386 {
387 struct shm_file_data *sfd = shm_file_data(file);
388
389 put_ipc_ns(sfd->ns);
390 shm_file_data(file) = NULL;
391 kfree(sfd);
392 return 0;
393 }
394
shm_fsync(struct file * file,loff_t start,loff_t end,int datasync)395 static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
396 {
397 struct shm_file_data *sfd = shm_file_data(file);
398
399 if (!sfd->file->f_op->fsync)
400 return -EINVAL;
401 return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
402 }
403
shm_fallocate(struct file * file,int mode,loff_t offset,loff_t len)404 static long shm_fallocate(struct file *file, int mode, loff_t offset,
405 loff_t len)
406 {
407 struct shm_file_data *sfd = shm_file_data(file);
408
409 if (!sfd->file->f_op->fallocate)
410 return -EOPNOTSUPP;
411 return sfd->file->f_op->fallocate(file, mode, offset, len);
412 }
413
shm_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)414 static unsigned long shm_get_unmapped_area(struct file *file,
415 unsigned long addr, unsigned long len, unsigned long pgoff,
416 unsigned long flags)
417 {
418 struct shm_file_data *sfd = shm_file_data(file);
419 return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
420 pgoff, flags);
421 }
422
423 static const struct file_operations shm_file_operations = {
424 .mmap = shm_mmap,
425 .fsync = shm_fsync,
426 .release = shm_release,
427 #ifndef CONFIG_MMU
428 .get_unmapped_area = shm_get_unmapped_area,
429 #endif
430 .llseek = noop_llseek,
431 .fallocate = shm_fallocate,
432 };
433
434 static const struct file_operations shm_file_operations_huge = {
435 .mmap = shm_mmap,
436 .fsync = shm_fsync,
437 .release = shm_release,
438 .get_unmapped_area = shm_get_unmapped_area,
439 .llseek = noop_llseek,
440 .fallocate = shm_fallocate,
441 };
442
is_file_shm_hugepages(struct file * file)443 int is_file_shm_hugepages(struct file *file)
444 {
445 return file->f_op == &shm_file_operations_huge;
446 }
447
448 static const struct vm_operations_struct shm_vm_ops = {
449 .open = shm_open, /* callback for a new vm-area open */
450 .close = shm_close, /* callback for when the vm-area is released */
451 .fault = shm_fault,
452 #if defined(CONFIG_NUMA)
453 .set_policy = shm_set_policy,
454 .get_policy = shm_get_policy,
455 #endif
456 };
457
458 /**
459 * newseg - Create a new shared memory segment
460 * @ns: namespace
461 * @params: ptr to the structure that contains key, size and shmflg
462 *
463 * Called with shm_ids.rw_mutex held as a writer.
464 */
465
newseg(struct ipc_namespace * ns,struct ipc_params * params)466 static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
467 {
468 key_t key = params->key;
469 int shmflg = params->flg;
470 size_t size = params->u.size;
471 int error;
472 struct shmid_kernel *shp;
473 size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
474 struct file * file;
475 char name[13];
476 int id;
477 vm_flags_t acctflag = 0;
478
479 if (size < SHMMIN || size > ns->shm_ctlmax)
480 return -EINVAL;
481
482 if (ns->shm_tot + numpages > ns->shm_ctlall)
483 return -ENOSPC;
484
485 shp = ipc_rcu_alloc(sizeof(*shp));
486 if (!shp)
487 return -ENOMEM;
488
489 shp->shm_perm.key = key;
490 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
491 shp->mlock_user = NULL;
492
493 shp->shm_perm.security = NULL;
494 error = security_shm_alloc(shp);
495 if (error) {
496 ipc_rcu_putref(shp, ipc_rcu_free);
497 return error;
498 }
499
500 sprintf (name, "SYSV%08x", key);
501 if (shmflg & SHM_HUGETLB) {
502 struct hstate *hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT)
503 & SHM_HUGE_MASK);
504 size_t hugesize;
505
506 if (!hs) {
507 error = -EINVAL;
508 goto no_file;
509 }
510 hugesize = ALIGN(size, huge_page_size(hs));
511
512 /* hugetlb_file_setup applies strict accounting */
513 if (shmflg & SHM_NORESERVE)
514 acctflag = VM_NORESERVE;
515 file = hugetlb_file_setup(name, hugesize, acctflag,
516 &shp->mlock_user, HUGETLB_SHMFS_INODE,
517 (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
518 } else {
519 /*
520 * Do not allow no accounting for OVERCOMMIT_NEVER, even
521 * if it's asked for.
522 */
523 if ((shmflg & SHM_NORESERVE) &&
524 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
525 acctflag = VM_NORESERVE;
526 file = shmem_file_setup(name, size, acctflag);
527 }
528 error = PTR_ERR(file);
529 if (IS_ERR(file))
530 goto no_file;
531
532 shp->shm_cprid = task_tgid_vnr(current);
533 shp->shm_lprid = 0;
534 shp->shm_atim = shp->shm_dtim = 0;
535 shp->shm_ctim = get_seconds();
536 shp->shm_segsz = size;
537 shp->shm_nattch = 0;
538 shp->shm_file = file;
539 shp->shm_creator = current;
540
541 id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
542 if (id < 0) {
543 error = id;
544 goto no_id;
545 }
546
547 /*
548 * shmid gets reported as "inode#" in /proc/pid/maps.
549 * proc-ps tools use this. Changing this will break them.
550 */
551 file_inode(file)->i_ino = shp->shm_perm.id;
552
553 ns->shm_tot += numpages;
554 error = shp->shm_perm.id;
555 shm_unlock(shp);
556 return error;
557
558 no_id:
559 if (is_file_hugepages(file) && shp->mlock_user)
560 user_shm_unlock(size, shp->mlock_user);
561 fput(file);
562 no_file:
563 ipc_rcu_putref(shp, shm_rcu_free);
564 return error;
565 }
566
567 /*
568 * Called with shm_ids.rw_mutex and ipcp locked.
569 */
shm_security(struct kern_ipc_perm * ipcp,int shmflg)570 static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
571 {
572 struct shmid_kernel *shp;
573
574 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
575 return security_shm_associate(shp, shmflg);
576 }
577
578 /*
579 * Called with shm_ids.rw_mutex and ipcp locked.
580 */
shm_more_checks(struct kern_ipc_perm * ipcp,struct ipc_params * params)581 static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
582 struct ipc_params *params)
583 {
584 struct shmid_kernel *shp;
585
586 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
587 if (shp->shm_segsz < params->u.size)
588 return -EINVAL;
589
590 return 0;
591 }
592
SYSCALL_DEFINE3(shmget,key_t,key,size_t,size,int,shmflg)593 SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
594 {
595 struct ipc_namespace *ns;
596 struct ipc_ops shm_ops;
597 struct ipc_params shm_params;
598
599 ns = current->nsproxy->ipc_ns;
600
601 shm_ops.getnew = newseg;
602 shm_ops.associate = shm_security;
603 shm_ops.more_checks = shm_more_checks;
604
605 shm_params.key = key;
606 shm_params.flg = shmflg;
607 shm_params.u.size = size;
608
609 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
610 }
611
copy_shmid_to_user(void __user * buf,struct shmid64_ds * in,int version)612 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
613 {
614 switch(version) {
615 case IPC_64:
616 return copy_to_user(buf, in, sizeof(*in));
617 case IPC_OLD:
618 {
619 struct shmid_ds out;
620
621 memset(&out, 0, sizeof(out));
622 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
623 out.shm_segsz = in->shm_segsz;
624 out.shm_atime = in->shm_atime;
625 out.shm_dtime = in->shm_dtime;
626 out.shm_ctime = in->shm_ctime;
627 out.shm_cpid = in->shm_cpid;
628 out.shm_lpid = in->shm_lpid;
629 out.shm_nattch = in->shm_nattch;
630
631 return copy_to_user(buf, &out, sizeof(out));
632 }
633 default:
634 return -EINVAL;
635 }
636 }
637
638 static inline unsigned long
copy_shmid_from_user(struct shmid64_ds * out,void __user * buf,int version)639 copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
640 {
641 switch(version) {
642 case IPC_64:
643 if (copy_from_user(out, buf, sizeof(*out)))
644 return -EFAULT;
645 return 0;
646 case IPC_OLD:
647 {
648 struct shmid_ds tbuf_old;
649
650 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
651 return -EFAULT;
652
653 out->shm_perm.uid = tbuf_old.shm_perm.uid;
654 out->shm_perm.gid = tbuf_old.shm_perm.gid;
655 out->shm_perm.mode = tbuf_old.shm_perm.mode;
656
657 return 0;
658 }
659 default:
660 return -EINVAL;
661 }
662 }
663
copy_shminfo_to_user(void __user * buf,struct shminfo64 * in,int version)664 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
665 {
666 switch(version) {
667 case IPC_64:
668 return copy_to_user(buf, in, sizeof(*in));
669 case IPC_OLD:
670 {
671 struct shminfo out;
672
673 if(in->shmmax > INT_MAX)
674 out.shmmax = INT_MAX;
675 else
676 out.shmmax = (int)in->shmmax;
677
678 out.shmmin = in->shmmin;
679 out.shmmni = in->shmmni;
680 out.shmseg = in->shmseg;
681 out.shmall = in->shmall;
682
683 return copy_to_user(buf, &out, sizeof(out));
684 }
685 default:
686 return -EINVAL;
687 }
688 }
689
690 /*
691 * Calculate and add used RSS and swap pages of a shm.
692 * Called with shm_ids.rw_mutex held as a reader
693 */
shm_add_rss_swap(struct shmid_kernel * shp,unsigned long * rss_add,unsigned long * swp_add)694 static void shm_add_rss_swap(struct shmid_kernel *shp,
695 unsigned long *rss_add, unsigned long *swp_add)
696 {
697 struct inode *inode;
698
699 inode = file_inode(shp->shm_file);
700
701 if (is_file_hugepages(shp->shm_file)) {
702 struct address_space *mapping = inode->i_mapping;
703 struct hstate *h = hstate_file(shp->shm_file);
704 *rss_add += pages_per_huge_page(h) * mapping->nrpages;
705 } else {
706 #ifdef CONFIG_SHMEM
707 struct shmem_inode_info *info = SHMEM_I(inode);
708 spin_lock(&info->lock);
709 *rss_add += inode->i_mapping->nrpages;
710 *swp_add += info->swapped;
711 spin_unlock(&info->lock);
712 #else
713 *rss_add += inode->i_mapping->nrpages;
714 #endif
715 }
716 }
717
718 /*
719 * Called with shm_ids.rw_mutex held as a reader
720 */
shm_get_stat(struct ipc_namespace * ns,unsigned long * rss,unsigned long * swp)721 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
722 unsigned long *swp)
723 {
724 int next_id;
725 int total, in_use;
726
727 *rss = 0;
728 *swp = 0;
729
730 in_use = shm_ids(ns).in_use;
731
732 for (total = 0, next_id = 0; total < in_use; next_id++) {
733 struct kern_ipc_perm *ipc;
734 struct shmid_kernel *shp;
735
736 ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
737 if (ipc == NULL)
738 continue;
739 shp = container_of(ipc, struct shmid_kernel, shm_perm);
740
741 shm_add_rss_swap(shp, rss, swp);
742
743 total++;
744 }
745 }
746
747 /*
748 * This function handles some shmctl commands which require the rw_mutex
749 * to be held in write mode.
750 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
751 */
shmctl_down(struct ipc_namespace * ns,int shmid,int cmd,struct shmid_ds __user * buf,int version)752 static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
753 struct shmid_ds __user *buf, int version)
754 {
755 struct kern_ipc_perm *ipcp;
756 struct shmid64_ds shmid64;
757 struct shmid_kernel *shp;
758 int err;
759
760 if (cmd == IPC_SET) {
761 if (copy_shmid_from_user(&shmid64, buf, version))
762 return -EFAULT;
763 }
764
765 ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
766 &shmid64.shm_perm, 0);
767 if (IS_ERR(ipcp))
768 return PTR_ERR(ipcp);
769
770 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
771
772 err = security_shm_shmctl(shp, cmd);
773 if (err)
774 goto out_unlock;
775 switch (cmd) {
776 case IPC_RMID:
777 do_shm_rmid(ns, ipcp);
778 goto out_up;
779 case IPC_SET:
780 err = ipc_update_perm(&shmid64.shm_perm, ipcp);
781 if (err)
782 goto out_unlock;
783 shp->shm_ctim = get_seconds();
784 break;
785 default:
786 err = -EINVAL;
787 }
788 out_unlock:
789 shm_unlock(shp);
790 out_up:
791 up_write(&shm_ids(ns).rw_mutex);
792 return err;
793 }
794
SYSCALL_DEFINE3(shmctl,int,shmid,int,cmd,struct shmid_ds __user *,buf)795 SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
796 {
797 struct shmid_kernel *shp;
798 int err, version;
799 struct ipc_namespace *ns;
800
801 if (cmd < 0 || shmid < 0) {
802 err = -EINVAL;
803 goto out;
804 }
805
806 version = ipc_parse_version(&cmd);
807 ns = current->nsproxy->ipc_ns;
808
809 switch (cmd) { /* replace with proc interface ? */
810 case IPC_INFO:
811 {
812 struct shminfo64 shminfo;
813
814 err = security_shm_shmctl(NULL, cmd);
815 if (err)
816 return err;
817
818 memset(&shminfo, 0, sizeof(shminfo));
819 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
820 shminfo.shmmax = ns->shm_ctlmax;
821 shminfo.shmall = ns->shm_ctlall;
822
823 shminfo.shmmin = SHMMIN;
824 if(copy_shminfo_to_user (buf, &shminfo, version))
825 return -EFAULT;
826
827 down_read(&shm_ids(ns).rw_mutex);
828 err = ipc_get_maxid(&shm_ids(ns));
829 up_read(&shm_ids(ns).rw_mutex);
830
831 if(err<0)
832 err = 0;
833 goto out;
834 }
835 case SHM_INFO:
836 {
837 struct shm_info shm_info;
838
839 err = security_shm_shmctl(NULL, cmd);
840 if (err)
841 return err;
842
843 memset(&shm_info, 0, sizeof(shm_info));
844 down_read(&shm_ids(ns).rw_mutex);
845 shm_info.used_ids = shm_ids(ns).in_use;
846 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
847 shm_info.shm_tot = ns->shm_tot;
848 shm_info.swap_attempts = 0;
849 shm_info.swap_successes = 0;
850 err = ipc_get_maxid(&shm_ids(ns));
851 up_read(&shm_ids(ns).rw_mutex);
852 if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
853 err = -EFAULT;
854 goto out;
855 }
856
857 err = err < 0 ? 0 : err;
858 goto out;
859 }
860 case SHM_STAT:
861 case IPC_STAT:
862 {
863 struct shmid64_ds tbuf;
864 int result;
865
866 if (cmd == SHM_STAT) {
867 shp = shm_lock(ns, shmid);
868 if (IS_ERR(shp)) {
869 err = PTR_ERR(shp);
870 goto out;
871 }
872 result = shp->shm_perm.id;
873 } else {
874 shp = shm_lock_check(ns, shmid);
875 if (IS_ERR(shp)) {
876 err = PTR_ERR(shp);
877 goto out;
878 }
879 result = 0;
880 }
881 err = -EACCES;
882 if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
883 goto out_unlock;
884 err = security_shm_shmctl(shp, cmd);
885 if (err)
886 goto out_unlock;
887 memset(&tbuf, 0, sizeof(tbuf));
888 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
889 tbuf.shm_segsz = shp->shm_segsz;
890 tbuf.shm_atime = shp->shm_atim;
891 tbuf.shm_dtime = shp->shm_dtim;
892 tbuf.shm_ctime = shp->shm_ctim;
893 tbuf.shm_cpid = shp->shm_cprid;
894 tbuf.shm_lpid = shp->shm_lprid;
895 tbuf.shm_nattch = shp->shm_nattch;
896 shm_unlock(shp);
897 if(copy_shmid_to_user (buf, &tbuf, version))
898 err = -EFAULT;
899 else
900 err = result;
901 goto out;
902 }
903 case SHM_LOCK:
904 case SHM_UNLOCK:
905 {
906 struct file *shm_file;
907
908 shp = shm_lock_check(ns, shmid);
909 if (IS_ERR(shp)) {
910 err = PTR_ERR(shp);
911 goto out;
912 }
913
914 audit_ipc_obj(&(shp->shm_perm));
915
916 if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
917 kuid_t euid = current_euid();
918 err = -EPERM;
919 if (!uid_eq(euid, shp->shm_perm.uid) &&
920 !uid_eq(euid, shp->shm_perm.cuid))
921 goto out_unlock;
922 if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
923 goto out_unlock;
924 }
925
926 err = security_shm_shmctl(shp, cmd);
927 if (err)
928 goto out_unlock;
929
930 shm_file = shp->shm_file;
931 if (is_file_hugepages(shm_file))
932 goto out_unlock;
933
934 if (cmd == SHM_LOCK) {
935 struct user_struct *user = current_user();
936 err = shmem_lock(shm_file, 1, user);
937 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
938 shp->shm_perm.mode |= SHM_LOCKED;
939 shp->mlock_user = user;
940 }
941 goto out_unlock;
942 }
943
944 /* SHM_UNLOCK */
945 if (!(shp->shm_perm.mode & SHM_LOCKED))
946 goto out_unlock;
947 shmem_lock(shm_file, 0, shp->mlock_user);
948 shp->shm_perm.mode &= ~SHM_LOCKED;
949 shp->mlock_user = NULL;
950 get_file(shm_file);
951 shm_unlock(shp);
952 shmem_unlock_mapping(shm_file->f_mapping);
953 fput(shm_file);
954 goto out;
955 }
956 case IPC_RMID:
957 case IPC_SET:
958 err = shmctl_down(ns, shmid, cmd, buf, version);
959 return err;
960 default:
961 return -EINVAL;
962 }
963
964 out_unlock:
965 shm_unlock(shp);
966 out:
967 return err;
968 }
969
970 /*
971 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
972 *
973 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
974 * "raddr" thing points to kernel space, and there has to be a wrapper around
975 * this.
976 */
do_shmat(int shmid,char __user * shmaddr,int shmflg,ulong * raddr,unsigned long shmlba)977 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
978 unsigned long shmlba)
979 {
980 struct shmid_kernel *shp;
981 unsigned long addr;
982 unsigned long size;
983 struct file * file;
984 int err;
985 unsigned long flags;
986 unsigned long prot;
987 int acc_mode;
988 struct ipc_namespace *ns;
989 struct shm_file_data *sfd;
990 struct path path;
991 fmode_t f_mode;
992 unsigned long populate = 0;
993
994 err = -EINVAL;
995 if (shmid < 0)
996 goto out;
997 else if ((addr = (ulong)shmaddr)) {
998 if (addr & (shmlba - 1)) {
999 if (shmflg & SHM_RND)
1000 addr &= ~(shmlba - 1); /* round down */
1001 else
1002 #ifndef __ARCH_FORCE_SHMLBA
1003 if (addr & ~PAGE_MASK)
1004 #endif
1005 goto out;
1006 }
1007 flags = MAP_SHARED | MAP_FIXED;
1008 } else {
1009 if ((shmflg & SHM_REMAP))
1010 goto out;
1011
1012 flags = MAP_SHARED;
1013 }
1014
1015 if (shmflg & SHM_RDONLY) {
1016 prot = PROT_READ;
1017 acc_mode = S_IRUGO;
1018 f_mode = FMODE_READ;
1019 } else {
1020 prot = PROT_READ | PROT_WRITE;
1021 acc_mode = S_IRUGO | S_IWUGO;
1022 f_mode = FMODE_READ | FMODE_WRITE;
1023 }
1024 if (shmflg & SHM_EXEC) {
1025 prot |= PROT_EXEC;
1026 acc_mode |= S_IXUGO;
1027 }
1028
1029 /*
1030 * We cannot rely on the fs check since SYSV IPC does have an
1031 * additional creator id...
1032 */
1033 ns = current->nsproxy->ipc_ns;
1034 shp = shm_lock_check(ns, shmid);
1035 if (IS_ERR(shp)) {
1036 err = PTR_ERR(shp);
1037 goto out;
1038 }
1039
1040 err = -EACCES;
1041 if (ipcperms(ns, &shp->shm_perm, acc_mode))
1042 goto out_unlock;
1043
1044 err = security_shm_shmat(shp, shmaddr, shmflg);
1045 if (err)
1046 goto out_unlock;
1047
1048 path = shp->shm_file->f_path;
1049 path_get(&path);
1050 shp->shm_nattch++;
1051 size = i_size_read(path.dentry->d_inode);
1052 shm_unlock(shp);
1053
1054 err = -ENOMEM;
1055 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
1056 if (!sfd)
1057 goto out_put_dentry;
1058
1059 file = alloc_file(&path, f_mode,
1060 is_file_hugepages(shp->shm_file) ?
1061 &shm_file_operations_huge :
1062 &shm_file_operations);
1063 err = PTR_ERR(file);
1064 if (IS_ERR(file))
1065 goto out_free;
1066
1067 file->private_data = sfd;
1068 file->f_mapping = shp->shm_file->f_mapping;
1069 sfd->id = shp->shm_perm.id;
1070 sfd->ns = get_ipc_ns(ns);
1071 sfd->file = shp->shm_file;
1072 sfd->vm_ops = NULL;
1073
1074 err = security_mmap_file(file, prot, flags);
1075 if (err)
1076 goto out_fput;
1077
1078 down_write(¤t->mm->mmap_sem);
1079 if (addr && !(shmflg & SHM_REMAP)) {
1080 err = -EINVAL;
1081 if (find_vma_intersection(current->mm, addr, addr + size))
1082 goto invalid;
1083 /*
1084 * If shm segment goes below stack, make sure there is some
1085 * space left for the stack to grow (at least 4 pages).
1086 */
1087 if (addr < current->mm->start_stack &&
1088 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
1089 goto invalid;
1090 }
1091
1092 addr = do_mmap_pgoff(file, addr, size, prot, flags, 0, &populate);
1093 *raddr = addr;
1094 err = 0;
1095 if (IS_ERR_VALUE(addr))
1096 err = (long)addr;
1097 invalid:
1098 up_write(¤t->mm->mmap_sem);
1099 if (populate)
1100 mm_populate(addr, populate);
1101
1102 out_fput:
1103 fput(file);
1104
1105 out_nattch:
1106 down_write(&shm_ids(ns).rw_mutex);
1107 shp = shm_lock(ns, shmid);
1108 BUG_ON(IS_ERR(shp));
1109 shp->shm_nattch--;
1110 if (shm_may_destroy(ns, shp))
1111 shm_destroy(ns, shp);
1112 else
1113 shm_unlock(shp);
1114 up_write(&shm_ids(ns).rw_mutex);
1115
1116 out:
1117 return err;
1118
1119 out_unlock:
1120 shm_unlock(shp);
1121 goto out;
1122
1123 out_free:
1124 kfree(sfd);
1125 out_put_dentry:
1126 path_put(&path);
1127 goto out_nattch;
1128 }
1129
SYSCALL_DEFINE3(shmat,int,shmid,char __user *,shmaddr,int,shmflg)1130 SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
1131 {
1132 unsigned long ret;
1133 long err;
1134
1135 err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
1136 if (err)
1137 return err;
1138 force_successful_syscall_return();
1139 return (long)ret;
1140 }
1141
1142 /*
1143 * detach and kill segment if marked destroyed.
1144 * The work is done in shm_close.
1145 */
SYSCALL_DEFINE1(shmdt,char __user *,shmaddr)1146 SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1147 {
1148 struct mm_struct *mm = current->mm;
1149 struct vm_area_struct *vma;
1150 unsigned long addr = (unsigned long)shmaddr;
1151 int retval = -EINVAL;
1152 #ifdef CONFIG_MMU
1153 loff_t size = 0;
1154 struct vm_area_struct *next;
1155 #endif
1156
1157 if (addr & ~PAGE_MASK)
1158 return retval;
1159
1160 down_write(&mm->mmap_sem);
1161
1162 /*
1163 * This function tries to be smart and unmap shm segments that
1164 * were modified by partial mlock or munmap calls:
1165 * - It first determines the size of the shm segment that should be
1166 * unmapped: It searches for a vma that is backed by shm and that
1167 * started at address shmaddr. It records it's size and then unmaps
1168 * it.
1169 * - Then it unmaps all shm vmas that started at shmaddr and that
1170 * are within the initially determined size.
1171 * Errors from do_munmap are ignored: the function only fails if
1172 * it's called with invalid parameters or if it's called to unmap
1173 * a part of a vma. Both calls in this function are for full vmas,
1174 * the parameters are directly copied from the vma itself and always
1175 * valid - therefore do_munmap cannot fail. (famous last words?)
1176 */
1177 /*
1178 * If it had been mremap()'d, the starting address would not
1179 * match the usual checks anyway. So assume all vma's are
1180 * above the starting address given.
1181 */
1182 vma = find_vma(mm, addr);
1183
1184 #ifdef CONFIG_MMU
1185 while (vma) {
1186 next = vma->vm_next;
1187
1188 /*
1189 * Check if the starting address would match, i.e. it's
1190 * a fragment created by mprotect() and/or munmap(), or it
1191 * otherwise it starts at this address with no hassles.
1192 */
1193 if ((vma->vm_ops == &shm_vm_ops) &&
1194 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1195
1196
1197 size = file_inode(vma->vm_file)->i_size;
1198 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1199 /*
1200 * We discovered the size of the shm segment, so
1201 * break out of here and fall through to the next
1202 * loop that uses the size information to stop
1203 * searching for matching vma's.
1204 */
1205 retval = 0;
1206 vma = next;
1207 break;
1208 }
1209 vma = next;
1210 }
1211
1212 /*
1213 * We need look no further than the maximum address a fragment
1214 * could possibly have landed at. Also cast things to loff_t to
1215 * prevent overflows and make comparisons vs. equal-width types.
1216 */
1217 size = PAGE_ALIGN(size);
1218 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1219 next = vma->vm_next;
1220
1221 /* finding a matching vma now does not alter retval */
1222 if ((vma->vm_ops == &shm_vm_ops) &&
1223 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1224
1225 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1226 vma = next;
1227 }
1228
1229 #else /* CONFIG_MMU */
1230 /* under NOMMU conditions, the exact address to be destroyed must be
1231 * given */
1232 retval = -EINVAL;
1233 if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
1234 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1235 retval = 0;
1236 }
1237
1238 #endif
1239
1240 up_write(&mm->mmap_sem);
1241 return retval;
1242 }
1243
1244 #ifdef CONFIG_PROC_FS
sysvipc_shm_proc_show(struct seq_file * s,void * it)1245 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1246 {
1247 struct user_namespace *user_ns = seq_user_ns(s);
1248 struct shmid_kernel *shp = it;
1249 unsigned long rss = 0, swp = 0;
1250
1251 shm_add_rss_swap(shp, &rss, &swp);
1252
1253 #if BITS_PER_LONG <= 32
1254 #define SIZE_SPEC "%10lu"
1255 #else
1256 #define SIZE_SPEC "%21lu"
1257 #endif
1258
1259 return seq_printf(s,
1260 "%10d %10d %4o " SIZE_SPEC " %5u %5u "
1261 "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
1262 SIZE_SPEC " " SIZE_SPEC "\n",
1263 shp->shm_perm.key,
1264 shp->shm_perm.id,
1265 shp->shm_perm.mode,
1266 shp->shm_segsz,
1267 shp->shm_cprid,
1268 shp->shm_lprid,
1269 shp->shm_nattch,
1270 from_kuid_munged(user_ns, shp->shm_perm.uid),
1271 from_kgid_munged(user_ns, shp->shm_perm.gid),
1272 from_kuid_munged(user_ns, shp->shm_perm.cuid),
1273 from_kgid_munged(user_ns, shp->shm_perm.cgid),
1274 shp->shm_atim,
1275 shp->shm_dtim,
1276 shp->shm_ctim,
1277 rss * PAGE_SIZE,
1278 swp * PAGE_SIZE);
1279 }
1280 #endif
1281