blob: e7922a546d7c5da6b615375cf4904a3424c0e893 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/ipc/shm.c
4 * Copyright (C) 1992, 1993 Krishna Balasubramanian
5 * Many improvements/fixes by Bruno Haible.
6 * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
7 * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
8 *
9 * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
10 * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
11 * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
12 * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
13 * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
14 * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
15 * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
16 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
19 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
23 *
24 * Better ipc lock (kern_ipc_perm.lock) handling
25 * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
26 */
27
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/hugetlb.h>
31#include <linux/shm.h>
32#include <linux/init.h>
33#include <linux/file.h>
34#include <linux/mman.h>
35#include <linux/shmem_fs.h>
36#include <linux/security.h>
37#include <linux/syscalls.h>
38#include <linux/audit.h>
39#include <linux/capability.h>
40#include <linux/ptrace.h>
41#include <linux/seq_file.h>
42#include <linux/rwsem.h>
43#include <linux/nsproxy.h>
44#include <linux/mount.h>
45#include <linux/ipc_namespace.h>
46#include <linux/rhashtable.h>
47
48#include <linux/uaccess.h>
49
50#include "util.h"
51
52struct shmid_kernel /* private to the kernel */
53{
54 struct kern_ipc_perm shm_perm;
55 struct file *shm_file;
56 unsigned long shm_nattch;
57 unsigned long shm_segsz;
58 time64_t shm_atim;
59 time64_t shm_dtim;
60 time64_t shm_ctim;
61 struct pid *shm_cprid;
62 struct pid *shm_lprid;
63 struct user_struct *mlock_user;
64
65 /* The task created the shm object. NULL if the task is dead. */
66 struct task_struct *shm_creator;
67 struct list_head shm_clist; /* list by creator */
68} __randomize_layout;
69
70/* shm_mode upper byte flags */
71#define SHM_DEST 01000 /* segment will be destroyed on last detach */
72#define SHM_LOCKED 02000 /* segment will not be swapped */
73
74struct shm_file_data {
75 int id;
76 struct ipc_namespace *ns;
77 struct file *file;
78 const struct vm_operations_struct *vm_ops;
79};
80
81#define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
82
83static const struct file_operations shm_file_operations;
84static const struct vm_operations_struct shm_vm_ops;
85
86#define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
87
88#define shm_unlock(shp) \
89 ipc_unlock(&(shp)->shm_perm)
90
91static int newseg(struct ipc_namespace *, struct ipc_params *);
92static void shm_open(struct vm_area_struct *vma);
93static void shm_close(struct vm_area_struct *vma);
94static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
95#ifdef CONFIG_PROC_FS
96static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
97#endif
98
99void shm_init_ns(struct ipc_namespace *ns)
100{
101 ns->shm_ctlmax = SHMMAX;
102 ns->shm_ctlall = SHMALL;
103 ns->shm_ctlmni = SHMMNI;
104 ns->shm_rmid_forced = 0;
105 ns->shm_tot = 0;
106 ipc_init_ids(&shm_ids(ns));
107}
108
109/*
110 * Called with shm_ids.rwsem (writer) and the shp structure locked.
111 * Only shm_ids.rwsem remains locked on exit.
112 */
113static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
114{
115 struct shmid_kernel *shp;
116
117 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
118
119 if (shp->shm_nattch) {
120 shp->shm_perm.mode |= SHM_DEST;
121 /* Do not find it any more */
122 ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
123 shm_unlock(shp);
124 } else
125 shm_destroy(ns, shp);
126}
127
128#ifdef CONFIG_IPC_NS
129void shm_exit_ns(struct ipc_namespace *ns)
130{
131 free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
132 idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
133 rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
134}
135#endif
136
137static int __init ipc_ns_init(void)
138{
139 shm_init_ns(&init_ipc_ns);
140 return 0;
141}
142
143pure_initcall(ipc_ns_init);
144
145void __init shm_init(void)
146{
147 if (IS_ENABLED(CONFIG_PROC_STRIPPED))
148 return;
149 ipc_init_proc_interface("sysvipc/shm",
150#if BITS_PER_LONG <= 32
151 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
152#else
153 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
154#endif
155 IPC_SHM_IDS, sysvipc_shm_proc_show);
156}
157
158static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
159{
160 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
161
162 if (IS_ERR(ipcp))
163 return ERR_CAST(ipcp);
164
165 return container_of(ipcp, struct shmid_kernel, shm_perm);
166}
167
168static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
169{
170 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
171
172 if (IS_ERR(ipcp))
173 return ERR_CAST(ipcp);
174
175 return container_of(ipcp, struct shmid_kernel, shm_perm);
176}
177
178/*
179 * shm_lock_(check_) routines are called in the paths where the rwsem
180 * is not necessarily held.
181 */
182static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
183{
184 struct kern_ipc_perm *ipcp;
185
186 rcu_read_lock();
187 ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
188 if (IS_ERR(ipcp))
189 goto err;
190
191 ipc_lock_object(ipcp);
192 /*
193 * ipc_rmid() may have already freed the ID while ipc_lock_object()
194 * was spinning: here verify that the structure is still valid.
195 * Upon races with RMID, return -EIDRM, thus indicating that
196 * the ID points to a removed identifier.
197 */
198 if (ipc_valid_object(ipcp)) {
199 /* return a locked ipc object upon success */
200 return container_of(ipcp, struct shmid_kernel, shm_perm);
201 }
202
203 ipc_unlock_object(ipcp);
204 ipcp = ERR_PTR(-EIDRM);
205err:
206 rcu_read_unlock();
207 /*
208 * Callers of shm_lock() must validate the status of the returned ipc
209 * object pointer and error out as appropriate.
210 */
211 return ERR_CAST(ipcp);
212}
213
214static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
215{
216 rcu_read_lock();
217 ipc_lock_object(&ipcp->shm_perm);
218}
219
220static void shm_rcu_free(struct rcu_head *head)
221{
222 struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
223 rcu);
224 struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
225 shm_perm);
226 security_shm_free(&shp->shm_perm);
227 kvfree(shp);
228}
229
230static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
231{
232 list_del(&s->shm_clist);
233 ipc_rmid(&shm_ids(ns), &s->shm_perm);
234}
235
236
237static int __shm_open(struct vm_area_struct *vma)
238{
239 struct file *file = vma->vm_file;
240 struct shm_file_data *sfd = shm_file_data(file);
241 struct shmid_kernel *shp;
242
243 shp = shm_lock(sfd->ns, sfd->id);
244
245 if (IS_ERR(shp))
246 return PTR_ERR(shp);
247
248 if (shp->shm_file != sfd->file) {
249 /* ID was reused */
250 shm_unlock(shp);
251 return -EINVAL;
252 }
253
254 shp->shm_atim = ktime_get_real_seconds();
255 ipc_update_pid(&shp->shm_lprid, task_tgid(current));
256 shp->shm_nattch++;
257 shm_unlock(shp);
258 return 0;
259}
260
261/* This is called by fork, once for every shm attach. */
262static void shm_open(struct vm_area_struct *vma)
263{
264 int err = __shm_open(vma);
265 /*
266 * We raced in the idr lookup or with shm_destroy().
267 * Either way, the ID is busted.
268 */
269 WARN_ON_ONCE(err);
270}
271
272/*
273 * shm_destroy - free the struct shmid_kernel
274 *
275 * @ns: namespace
276 * @shp: struct to free
277 *
278 * It has to be called with shp and shm_ids.rwsem (writer) locked,
279 * but returns with shp unlocked and freed.
280 */
281static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
282{
283 struct file *shm_file;
284
285 shm_file = shp->shm_file;
286 shp->shm_file = NULL;
287 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
288 shm_rmid(ns, shp);
289 shm_unlock(shp);
290 if (!is_file_hugepages(shm_file))
291 shmem_lock(shm_file, 0, shp->mlock_user);
292 else if (shp->mlock_user)
293 user_shm_unlock(i_size_read(file_inode(shm_file)),
294 shp->mlock_user);
295 fput(shm_file);
296 ipc_update_pid(&shp->shm_cprid, NULL);
297 ipc_update_pid(&shp->shm_lprid, NULL);
298 ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
299}
300
301/*
302 * shm_may_destroy - identifies whether shm segment should be destroyed now
303 *
304 * Returns true if and only if there are no active users of the segment and
305 * one of the following is true:
306 *
307 * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
308 *
309 * 2) sysctl kernel.shm_rmid_forced is set to 1.
310 */
311static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
312{
313 return (shp->shm_nattch == 0) &&
314 (ns->shm_rmid_forced ||
315 (shp->shm_perm.mode & SHM_DEST));
316}
317
318/*
319 * remove the attach descriptor vma.
320 * free memory for segment if it is marked destroyed.
321 * The descriptor has already been removed from the current->mm->mmap list
322 * and will later be kfree()d.
323 */
324static void shm_close(struct vm_area_struct *vma)
325{
326 struct file *file = vma->vm_file;
327 struct shm_file_data *sfd = shm_file_data(file);
328 struct shmid_kernel *shp;
329 struct ipc_namespace *ns = sfd->ns;
330
331 down_write(&shm_ids(ns).rwsem);
332 /* remove from the list of attaches of the shm segment */
333 shp = shm_lock(ns, sfd->id);
334
335 /*
336 * We raced in the idr lookup or with shm_destroy().
337 * Either way, the ID is busted.
338 */
339 if (WARN_ON_ONCE(IS_ERR(shp)))
340 goto done; /* no-op */
341
342 ipc_update_pid(&shp->shm_lprid, task_tgid(current));
343 shp->shm_dtim = ktime_get_real_seconds();
344 shp->shm_nattch--;
345 if (shm_may_destroy(ns, shp))
346 shm_destroy(ns, shp);
347 else
348 shm_unlock(shp);
349done:
350 up_write(&shm_ids(ns).rwsem);
351}
352
353/* Called with ns->shm_ids(ns).rwsem locked */
354static int shm_try_destroy_orphaned(int id, void *p, void *data)
355{
356 struct ipc_namespace *ns = data;
357 struct kern_ipc_perm *ipcp = p;
358 struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
359
360 /*
361 * We want to destroy segments without users and with already
362 * exit'ed originating process.
363 *
364 * As shp->* are changed under rwsem, it's safe to skip shp locking.
365 */
366 if (shp->shm_creator != NULL)
367 return 0;
368
369 if (shm_may_destroy(ns, shp)) {
370 shm_lock_by_ptr(shp);
371 shm_destroy(ns, shp);
372 }
373 return 0;
374}
375
376void shm_destroy_orphaned(struct ipc_namespace *ns)
377{
378 down_write(&shm_ids(ns).rwsem);
379 if (shm_ids(ns).in_use)
380 idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
381 up_write(&shm_ids(ns).rwsem);
382}
383
384/* Locking assumes this will only be called with task == current */
385void exit_shm(struct task_struct *task)
386{
387 struct ipc_namespace *ns = task->nsproxy->ipc_ns;
388 struct shmid_kernel *shp, *n;
389
390 if (list_empty(&task->sysvshm.shm_clist))
391 return;
392
393 /*
394 * If kernel.shm_rmid_forced is not set then only keep track of
395 * which shmids are orphaned, so that a later set of the sysctl
396 * can clean them up.
397 */
398 if (!ns->shm_rmid_forced) {
399 down_read(&shm_ids(ns).rwsem);
400 list_for_each_entry(shp, &task->sysvshm.shm_clist, shm_clist)
401 shp->shm_creator = NULL;
402 /*
403 * Only under read lock but we are only called on current
404 * so no entry on the list will be shared.
405 */
406 list_del(&task->sysvshm.shm_clist);
407 up_read(&shm_ids(ns).rwsem);
408 return;
409 }
410
411 /*
412 * Destroy all already created segments, that were not yet mapped,
413 * and mark any mapped as orphan to cover the sysctl toggling.
414 * Destroy is skipped if shm_may_destroy() returns false.
415 */
416 down_write(&shm_ids(ns).rwsem);
417 list_for_each_entry_safe(shp, n, &task->sysvshm.shm_clist, shm_clist) {
418 shp->shm_creator = NULL;
419
420 if (shm_may_destroy(ns, shp)) {
421 shm_lock_by_ptr(shp);
422 shm_destroy(ns, shp);
423 }
424 }
425
426 /* Remove the list head from any segments still attached. */
427 list_del(&task->sysvshm.shm_clist);
428 up_write(&shm_ids(ns).rwsem);
429}
430
431static vm_fault_t shm_fault(struct vm_fault *vmf)
432{
433 struct file *file = vmf->vma->vm_file;
434 struct shm_file_data *sfd = shm_file_data(file);
435
436 return sfd->vm_ops->fault(vmf);
437}
438
439static int shm_split(struct vm_area_struct *vma, unsigned long addr)
440{
441 struct file *file = vma->vm_file;
442 struct shm_file_data *sfd = shm_file_data(file);
443
444 if (sfd->vm_ops->split)
445 return sfd->vm_ops->split(vma, addr);
446
447 return 0;
448}
449
450static unsigned long shm_pagesize(struct vm_area_struct *vma)
451{
452 struct file *file = vma->vm_file;
453 struct shm_file_data *sfd = shm_file_data(file);
454
455 if (sfd->vm_ops->pagesize)
456 return sfd->vm_ops->pagesize(vma);
457
458 return PAGE_SIZE;
459}
460
461#ifdef CONFIG_NUMA
462static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
463{
464 struct file *file = vma->vm_file;
465 struct shm_file_data *sfd = shm_file_data(file);
466 int err = 0;
467
468 if (sfd->vm_ops->set_policy)
469 err = sfd->vm_ops->set_policy(vma, new);
470 return err;
471}
472
473static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
474 unsigned long addr)
475{
476 struct file *file = vma->vm_file;
477 struct shm_file_data *sfd = shm_file_data(file);
478 struct mempolicy *pol = NULL;
479
480 if (sfd->vm_ops->get_policy)
481 pol = sfd->vm_ops->get_policy(vma, addr);
482 else if (vma->vm_policy)
483 pol = vma->vm_policy;
484
485 return pol;
486}
487#endif
488
489static int shm_mmap(struct file *file, struct vm_area_struct *vma)
490{
491 struct shm_file_data *sfd = shm_file_data(file);
492 int ret;
493
494 /*
495 * In case of remap_file_pages() emulation, the file can represent an
496 * IPC ID that was removed, and possibly even reused by another shm
497 * segment already. Propagate this case as an error to caller.
498 */
499 ret = __shm_open(vma);
500 if (ret)
501 return ret;
502
503 ret = call_mmap(sfd->file, vma);
504 if (ret) {
505 shm_close(vma);
506 return ret;
507 }
508 sfd->vm_ops = vma->vm_ops;
509#ifdef CONFIG_MMU
510 WARN_ON(!sfd->vm_ops->fault);
511#endif
512 vma->vm_ops = &shm_vm_ops;
513 return 0;
514}
515
516static int shm_release(struct inode *ino, struct file *file)
517{
518 struct shm_file_data *sfd = shm_file_data(file);
519
520 put_ipc_ns(sfd->ns);
521 fput(sfd->file);
522 shm_file_data(file) = NULL;
523 kfree(sfd);
524 return 0;
525}
526
527static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
528{
529 struct shm_file_data *sfd = shm_file_data(file);
530
531 if (!sfd->file->f_op->fsync)
532 return -EINVAL;
533 return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
534}
535
536static long shm_fallocate(struct file *file, int mode, loff_t offset,
537 loff_t len)
538{
539 struct shm_file_data *sfd = shm_file_data(file);
540
541 if (!sfd->file->f_op->fallocate)
542 return -EOPNOTSUPP;
543 return sfd->file->f_op->fallocate(file, mode, offset, len);
544}
545
546static unsigned long shm_get_unmapped_area(struct file *file,
547 unsigned long addr, unsigned long len, unsigned long pgoff,
548 unsigned long flags)
549{
550 struct shm_file_data *sfd = shm_file_data(file);
551
552 return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
553 pgoff, flags);
554}
555
556static const struct file_operations shm_file_operations = {
557 .mmap = shm_mmap,
558 .fsync = shm_fsync,
559 .release = shm_release,
560 .get_unmapped_area = shm_get_unmapped_area,
561 .llseek = noop_llseek,
562 .fallocate = shm_fallocate,
563};
564
565/*
566 * shm_file_operations_huge is now identical to shm_file_operations,
567 * but we keep it distinct for the sake of is_file_shm_hugepages().
568 */
569static const struct file_operations shm_file_operations_huge = {
570 .mmap = shm_mmap,
571 .fsync = shm_fsync,
572 .release = shm_release,
573 .get_unmapped_area = shm_get_unmapped_area,
574 .llseek = noop_llseek,
575 .fallocate = shm_fallocate,
576};
577
578bool is_file_shm_hugepages(struct file *file)
579{
580 return file->f_op == &shm_file_operations_huge;
581}
582
583static const struct vm_operations_struct shm_vm_ops = {
584 .open = shm_open, /* callback for a new vm-area open */
585 .close = shm_close, /* callback for when the vm-area is released */
586 .fault = shm_fault,
587 .split = shm_split,
588 .pagesize = shm_pagesize,
589#if defined(CONFIG_NUMA)
590 .set_policy = shm_set_policy,
591 .get_policy = shm_get_policy,
592#endif
593};
594
595/**
596 * newseg - Create a new shared memory segment
597 * @ns: namespace
598 * @params: ptr to the structure that contains key, size and shmflg
599 *
600 * Called with shm_ids.rwsem held as a writer.
601 */
602static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
603{
604 key_t key = params->key;
605 int shmflg = params->flg;
606 size_t size = params->u.size;
607 int error;
608 struct shmid_kernel *shp;
609 size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
610 struct file *file;
611 char name[13];
612 vm_flags_t acctflag = 0;
613
614 if (size < SHMMIN || size > ns->shm_ctlmax)
615 return -EINVAL;
616
617 if (numpages << PAGE_SHIFT < size)
618 return -ENOSPC;
619
620 if (ns->shm_tot + numpages < ns->shm_tot ||
621 ns->shm_tot + numpages > ns->shm_ctlall)
622 return -ENOSPC;
623
624 shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
625 if (unlikely(!shp))
626 return -ENOMEM;
627
628 shp->shm_perm.key = key;
629 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
630 shp->mlock_user = NULL;
631
632 shp->shm_perm.security = NULL;
633 error = security_shm_alloc(&shp->shm_perm);
634 if (error) {
635 kvfree(shp);
636 return error;
637 }
638
639 sprintf(name, "SYSV%08x", key);
640 if (shmflg & SHM_HUGETLB) {
641 struct hstate *hs;
642 size_t hugesize;
643
644 hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
645 if (!hs) {
646 error = -EINVAL;
647 goto no_file;
648 }
649 hugesize = ALIGN(size, huge_page_size(hs));
650
651 /* hugetlb_file_setup applies strict accounting */
652 if (shmflg & SHM_NORESERVE)
653 acctflag = VM_NORESERVE;
654 file = hugetlb_file_setup(name, hugesize, acctflag,
655 &shp->mlock_user, HUGETLB_SHMFS_INODE,
656 (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
657 } else {
658 /*
659 * Do not allow no accounting for OVERCOMMIT_NEVER, even
660 * if it's asked for.
661 */
662 if ((shmflg & SHM_NORESERVE) &&
663 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
664 acctflag = VM_NORESERVE;
665 file = shmem_kernel_file_setup(name, size, acctflag);
666 }
667 error = PTR_ERR(file);
668 if (IS_ERR(file))
669 goto no_file;
670
671 shp->shm_cprid = get_pid(task_tgid(current));
672 shp->shm_lprid = NULL;
673 shp->shm_atim = shp->shm_dtim = 0;
674 shp->shm_ctim = ktime_get_real_seconds();
675 shp->shm_segsz = size;
676 shp->shm_nattch = 0;
677 shp->shm_file = file;
678 shp->shm_creator = current;
679
680 /* ipc_addid() locks shp upon success. */
681 error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
682 if (error < 0)
683 goto no_id;
684
685 list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
686
687 /*
688 * shmid gets reported as "inode#" in /proc/pid/maps.
689 * proc-ps tools use this. Changing this will break them.
690 */
691 file_inode(file)->i_ino = shp->shm_perm.id;
692
693 ns->shm_tot += numpages;
694 error = shp->shm_perm.id;
695
696 ipc_unlock_object(&shp->shm_perm);
697 rcu_read_unlock();
698 return error;
699
700no_id:
701 ipc_update_pid(&shp->shm_cprid, NULL);
702 ipc_update_pid(&shp->shm_lprid, NULL);
703 if (is_file_hugepages(file) && shp->mlock_user)
704 user_shm_unlock(size, shp->mlock_user);
705 fput(file);
706 ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
707 return error;
708no_file:
709 call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
710 return error;
711}
712
713/*
714 * Called with shm_ids.rwsem and ipcp locked.
715 */
716static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
717 struct ipc_params *params)
718{
719 struct shmid_kernel *shp;
720
721 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
722 if (shp->shm_segsz < params->u.size)
723 return -EINVAL;
724
725 return 0;
726}
727
728long ksys_shmget(key_t key, size_t size, int shmflg)
729{
730 struct ipc_namespace *ns;
731 static const struct ipc_ops shm_ops = {
732 .getnew = newseg,
733 .associate = security_shm_associate,
734 .more_checks = shm_more_checks,
735 };
736 struct ipc_params shm_params;
737
738 ns = current->nsproxy->ipc_ns;
739
740 shm_params.key = key;
741 shm_params.flg = shmflg;
742 shm_params.u.size = size;
743
744 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
745}
746
747SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
748{
749 return ksys_shmget(key, size, shmflg);
750}
751
752static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
753{
754 switch (version) {
755 case IPC_64:
756 return copy_to_user(buf, in, sizeof(*in));
757 case IPC_OLD:
758 {
759 struct shmid_ds out;
760
761 memset(&out, 0, sizeof(out));
762 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
763 out.shm_segsz = in->shm_segsz;
764 out.shm_atime = in->shm_atime;
765 out.shm_dtime = in->shm_dtime;
766 out.shm_ctime = in->shm_ctime;
767 out.shm_cpid = in->shm_cpid;
768 out.shm_lpid = in->shm_lpid;
769 out.shm_nattch = in->shm_nattch;
770
771 return copy_to_user(buf, &out, sizeof(out));
772 }
773 default:
774 return -EINVAL;
775 }
776}
777
778static inline unsigned long
779copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
780{
781 switch (version) {
782 case IPC_64:
783 if (copy_from_user(out, buf, sizeof(*out)))
784 return -EFAULT;
785 return 0;
786 case IPC_OLD:
787 {
788 struct shmid_ds tbuf_old;
789
790 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
791 return -EFAULT;
792
793 out->shm_perm.uid = tbuf_old.shm_perm.uid;
794 out->shm_perm.gid = tbuf_old.shm_perm.gid;
795 out->shm_perm.mode = tbuf_old.shm_perm.mode;
796
797 return 0;
798 }
799 default:
800 return -EINVAL;
801 }
802}
803
804static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
805{
806 switch (version) {
807 case IPC_64:
808 return copy_to_user(buf, in, sizeof(*in));
809 case IPC_OLD:
810 {
811 struct shminfo out;
812
813 if (in->shmmax > INT_MAX)
814 out.shmmax = INT_MAX;
815 else
816 out.shmmax = (int)in->shmmax;
817
818 out.shmmin = in->shmmin;
819 out.shmmni = in->shmmni;
820 out.shmseg = in->shmseg;
821 out.shmall = in->shmall;
822
823 return copy_to_user(buf, &out, sizeof(out));
824 }
825 default:
826 return -EINVAL;
827 }
828}
829
830/*
831 * Calculate and add used RSS and swap pages of a shm.
832 * Called with shm_ids.rwsem held as a reader
833 */
834static void shm_add_rss_swap(struct shmid_kernel *shp,
835 unsigned long *rss_add, unsigned long *swp_add)
836{
837 struct inode *inode;
838
839 inode = file_inode(shp->shm_file);
840
841 if (is_file_hugepages(shp->shm_file)) {
842 struct address_space *mapping = inode->i_mapping;
843 struct hstate *h = hstate_file(shp->shm_file);
844 *rss_add += pages_per_huge_page(h) * mapping->nrpages;
845 } else {
846#ifdef CONFIG_SHMEM
847 struct shmem_inode_info *info = SHMEM_I(inode);
848
849 spin_lock_irq(&info->lock);
850 *rss_add += inode->i_mapping->nrpages;
851 *swp_add += info->swapped;
852 spin_unlock_irq(&info->lock);
853#else
854 *rss_add += inode->i_mapping->nrpages;
855#endif
856 }
857}
858
859/*
860 * Called with shm_ids.rwsem held as a reader
861 */
862static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
863 unsigned long *swp)
864{
865 int next_id;
866 int total, in_use;
867
868 *rss = 0;
869 *swp = 0;
870
871 in_use = shm_ids(ns).in_use;
872
873 for (total = 0, next_id = 0; total < in_use; next_id++) {
874 struct kern_ipc_perm *ipc;
875 struct shmid_kernel *shp;
876
877 ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
878 if (ipc == NULL)
879 continue;
880 shp = container_of(ipc, struct shmid_kernel, shm_perm);
881
882 shm_add_rss_swap(shp, rss, swp);
883
884 total++;
885 }
886}
887
888/*
889 * This function handles some shmctl commands which require the rwsem
890 * to be held in write mode.
891 * NOTE: no locks must be held, the rwsem is taken inside this function.
892 */
893static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
894 struct shmid64_ds *shmid64)
895{
896 struct kern_ipc_perm *ipcp;
897 struct shmid_kernel *shp;
898 int err;
899
900 down_write(&shm_ids(ns).rwsem);
901 rcu_read_lock();
902
903 ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd,
904 &shmid64->shm_perm, 0);
905 if (IS_ERR(ipcp)) {
906 err = PTR_ERR(ipcp);
907 goto out_unlock1;
908 }
909
910 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
911
912 err = security_shm_shmctl(&shp->shm_perm, cmd);
913 if (err)
914 goto out_unlock1;
915
916 switch (cmd) {
917 case IPC_RMID:
918 ipc_lock_object(&shp->shm_perm);
919 /* do_shm_rmid unlocks the ipc object and rcu */
920 do_shm_rmid(ns, ipcp);
921 goto out_up;
922 case IPC_SET:
923 ipc_lock_object(&shp->shm_perm);
924 err = ipc_update_perm(&shmid64->shm_perm, ipcp);
925 if (err)
926 goto out_unlock0;
927 shp->shm_ctim = ktime_get_real_seconds();
928 break;
929 default:
930 err = -EINVAL;
931 goto out_unlock1;
932 }
933
934out_unlock0:
935 ipc_unlock_object(&shp->shm_perm);
936out_unlock1:
937 rcu_read_unlock();
938out_up:
939 up_write(&shm_ids(ns).rwsem);
940 return err;
941}
942
943static int shmctl_ipc_info(struct ipc_namespace *ns,
944 struct shminfo64 *shminfo)
945{
946 int err = security_shm_shmctl(NULL, IPC_INFO);
947 if (!err) {
948 memset(shminfo, 0, sizeof(*shminfo));
949 shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni;
950 shminfo->shmmax = ns->shm_ctlmax;
951 shminfo->shmall = ns->shm_ctlall;
952 shminfo->shmmin = SHMMIN;
953 down_read(&shm_ids(ns).rwsem);
954 err = ipc_get_maxidx(&shm_ids(ns));
955 up_read(&shm_ids(ns).rwsem);
956 if (err < 0)
957 err = 0;
958 }
959 return err;
960}
961
962static int shmctl_shm_info(struct ipc_namespace *ns,
963 struct shm_info *shm_info)
964{
965 int err = security_shm_shmctl(NULL, SHM_INFO);
966 if (!err) {
967 memset(shm_info, 0, sizeof(*shm_info));
968 down_read(&shm_ids(ns).rwsem);
969 shm_info->used_ids = shm_ids(ns).in_use;
970 shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp);
971 shm_info->shm_tot = ns->shm_tot;
972 shm_info->swap_attempts = 0;
973 shm_info->swap_successes = 0;
974 err = ipc_get_maxidx(&shm_ids(ns));
975 up_read(&shm_ids(ns).rwsem);
976 if (err < 0)
977 err = 0;
978 }
979 return err;
980}
981
982static int shmctl_stat(struct ipc_namespace *ns, int shmid,
983 int cmd, struct shmid64_ds *tbuf)
984{
985 struct shmid_kernel *shp;
986 int err;
987
988 memset(tbuf, 0, sizeof(*tbuf));
989
990 rcu_read_lock();
991 if (cmd == SHM_STAT || cmd == SHM_STAT_ANY) {
992 shp = shm_obtain_object(ns, shmid);
993 if (IS_ERR(shp)) {
994 err = PTR_ERR(shp);
995 goto out_unlock;
996 }
997 } else { /* IPC_STAT */
998 shp = shm_obtain_object_check(ns, shmid);
999 if (IS_ERR(shp)) {
1000 err = PTR_ERR(shp);
1001 goto out_unlock;
1002 }
1003 }
1004
1005 /*
1006 * Semantically SHM_STAT_ANY ought to be identical to
1007 * that functionality provided by the /proc/sysvipc/
1008 * interface. As such, only audit these calls and
1009 * do not do traditional S_IRUGO permission checks on
1010 * the ipc object.
1011 */
1012 if (cmd == SHM_STAT_ANY)
1013 audit_ipc_obj(&shp->shm_perm);
1014 else {
1015 err = -EACCES;
1016 if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
1017 goto out_unlock;
1018 }
1019
1020 err = security_shm_shmctl(&shp->shm_perm, cmd);
1021 if (err)
1022 goto out_unlock;
1023
1024 ipc_lock_object(&shp->shm_perm);
1025
1026 if (!ipc_valid_object(&shp->shm_perm)) {
1027 ipc_unlock_object(&shp->shm_perm);
1028 err = -EIDRM;
1029 goto out_unlock;
1030 }
1031
1032 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm);
1033 tbuf->shm_segsz = shp->shm_segsz;
1034 tbuf->shm_atime = shp->shm_atim;
1035 tbuf->shm_dtime = shp->shm_dtim;
1036 tbuf->shm_ctime = shp->shm_ctim;
1037#ifndef CONFIG_64BIT
1038 tbuf->shm_atime_high = shp->shm_atim >> 32;
1039 tbuf->shm_dtime_high = shp->shm_dtim >> 32;
1040 tbuf->shm_ctime_high = shp->shm_ctim >> 32;
1041#endif
1042 tbuf->shm_cpid = pid_vnr(shp->shm_cprid);
1043 tbuf->shm_lpid = pid_vnr(shp->shm_lprid);
1044 tbuf->shm_nattch = shp->shm_nattch;
1045
1046 if (cmd == IPC_STAT) {
1047 /*
1048 * As defined in SUS:
1049 * Return 0 on success
1050 */
1051 err = 0;
1052 } else {
1053 /*
1054 * SHM_STAT and SHM_STAT_ANY (both Linux specific)
1055 * Return the full id, including the sequence number
1056 */
1057 err = shp->shm_perm.id;
1058 }
1059
1060 ipc_unlock_object(&shp->shm_perm);
1061out_unlock:
1062 rcu_read_unlock();
1063 return err;
1064}
1065
1066static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd)
1067{
1068 struct shmid_kernel *shp;
1069 struct file *shm_file;
1070 int err;
1071
1072 rcu_read_lock();
1073 shp = shm_obtain_object_check(ns, shmid);
1074 if (IS_ERR(shp)) {
1075 err = PTR_ERR(shp);
1076 goto out_unlock1;
1077 }
1078
1079 audit_ipc_obj(&(shp->shm_perm));
1080 err = security_shm_shmctl(&shp->shm_perm, cmd);
1081 if (err)
1082 goto out_unlock1;
1083
1084 ipc_lock_object(&shp->shm_perm);
1085
1086 /* check if shm_destroy() is tearing down shp */
1087 if (!ipc_valid_object(&shp->shm_perm)) {
1088 err = -EIDRM;
1089 goto out_unlock0;
1090 }
1091
1092 if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
1093 kuid_t euid = current_euid();
1094
1095 if (!uid_eq(euid, shp->shm_perm.uid) &&
1096 !uid_eq(euid, shp->shm_perm.cuid)) {
1097 err = -EPERM;
1098 goto out_unlock0;
1099 }
1100 if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
1101 err = -EPERM;
1102 goto out_unlock0;
1103 }
1104 }
1105
1106 shm_file = shp->shm_file;
1107 if (is_file_hugepages(shm_file))
1108 goto out_unlock0;
1109
1110 if (cmd == SHM_LOCK) {
1111 struct user_struct *user = current_user();
1112
1113 err = shmem_lock(shm_file, 1, user);
1114 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
1115 shp->shm_perm.mode |= SHM_LOCKED;
1116 shp->mlock_user = user;
1117 }
1118 goto out_unlock0;
1119 }
1120
1121 /* SHM_UNLOCK */
1122 if (!(shp->shm_perm.mode & SHM_LOCKED))
1123 goto out_unlock0;
1124 shmem_lock(shm_file, 0, shp->mlock_user);
1125 shp->shm_perm.mode &= ~SHM_LOCKED;
1126 shp->mlock_user = NULL;
1127 get_file(shm_file);
1128 ipc_unlock_object(&shp->shm_perm);
1129 rcu_read_unlock();
1130 shmem_unlock_mapping(shm_file->f_mapping);
1131
1132 fput(shm_file);
1133 return err;
1134
1135out_unlock0:
1136 ipc_unlock_object(&shp->shm_perm);
1137out_unlock1:
1138 rcu_read_unlock();
1139 return err;
1140}
1141
1142long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf)
1143{
1144 int err, version;
1145 struct ipc_namespace *ns;
1146 struct shmid64_ds sem64;
1147
1148 if (cmd < 0 || shmid < 0)
1149 return -EINVAL;
1150
1151 version = ipc_parse_version(&cmd);
1152 ns = current->nsproxy->ipc_ns;
1153
1154 switch (cmd) {
1155 case IPC_INFO: {
1156 struct shminfo64 shminfo;
1157 err = shmctl_ipc_info(ns, &shminfo);
1158 if (err < 0)
1159 return err;
1160 if (copy_shminfo_to_user(buf, &shminfo, version))
1161 err = -EFAULT;
1162 return err;
1163 }
1164 case SHM_INFO: {
1165 struct shm_info shm_info;
1166 err = shmctl_shm_info(ns, &shm_info);
1167 if (err < 0)
1168 return err;
1169 if (copy_to_user(buf, &shm_info, sizeof(shm_info)))
1170 err = -EFAULT;
1171 return err;
1172 }
1173 case SHM_STAT:
1174 case SHM_STAT_ANY:
1175 case IPC_STAT: {
1176 err = shmctl_stat(ns, shmid, cmd, &sem64);
1177 if (err < 0)
1178 return err;
1179 if (copy_shmid_to_user(buf, &sem64, version))
1180 err = -EFAULT;
1181 return err;
1182 }
1183 case IPC_SET:
1184 if (copy_shmid_from_user(&sem64, buf, version))
1185 return -EFAULT;
1186 /* fallthru */
1187 case IPC_RMID:
1188 return shmctl_down(ns, shmid, cmd, &sem64);
1189 case SHM_LOCK:
1190 case SHM_UNLOCK:
1191 return shmctl_do_lock(ns, shmid, cmd);
1192 default:
1193 return -EINVAL;
1194 }
1195}
1196
1197SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
1198{
1199 return ksys_shmctl(shmid, cmd, buf);
1200}
1201
1202#ifdef CONFIG_COMPAT
1203
1204struct compat_shmid_ds {
1205 struct compat_ipc_perm shm_perm;
1206 int shm_segsz;
1207 compat_time_t shm_atime;
1208 compat_time_t shm_dtime;
1209 compat_time_t shm_ctime;
1210 compat_ipc_pid_t shm_cpid;
1211 compat_ipc_pid_t shm_lpid;
1212 unsigned short shm_nattch;
1213 unsigned short shm_unused;
1214 compat_uptr_t shm_unused2;
1215 compat_uptr_t shm_unused3;
1216};
1217
1218struct compat_shminfo64 {
1219 compat_ulong_t shmmax;
1220 compat_ulong_t shmmin;
1221 compat_ulong_t shmmni;
1222 compat_ulong_t shmseg;
1223 compat_ulong_t shmall;
1224 compat_ulong_t __unused1;
1225 compat_ulong_t __unused2;
1226 compat_ulong_t __unused3;
1227 compat_ulong_t __unused4;
1228};
1229
1230struct compat_shm_info {
1231 compat_int_t used_ids;
1232 compat_ulong_t shm_tot, shm_rss, shm_swp;
1233 compat_ulong_t swap_attempts, swap_successes;
1234};
1235
1236static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
1237 int version)
1238{
1239 if (in->shmmax > INT_MAX)
1240 in->shmmax = INT_MAX;
1241 if (version == IPC_64) {
1242 struct compat_shminfo64 info;
1243 memset(&info, 0, sizeof(info));
1244 info.shmmax = in->shmmax;
1245 info.shmmin = in->shmmin;
1246 info.shmmni = in->shmmni;
1247 info.shmseg = in->shmseg;
1248 info.shmall = in->shmall;
1249 return copy_to_user(buf, &info, sizeof(info));
1250 } else {
1251 struct shminfo info;
1252 memset(&info, 0, sizeof(info));
1253 info.shmmax = in->shmmax;
1254 info.shmmin = in->shmmin;
1255 info.shmmni = in->shmmni;
1256 info.shmseg = in->shmseg;
1257 info.shmall = in->shmall;
1258 return copy_to_user(buf, &info, sizeof(info));
1259 }
1260}
1261
1262static int put_compat_shm_info(struct shm_info *ip,
1263 struct compat_shm_info __user *uip)
1264{
1265 struct compat_shm_info info;
1266
1267 memset(&info, 0, sizeof(info));
1268 info.used_ids = ip->used_ids;
1269 info.shm_tot = ip->shm_tot;
1270 info.shm_rss = ip->shm_rss;
1271 info.shm_swp = ip->shm_swp;
1272 info.swap_attempts = ip->swap_attempts;
1273 info.swap_successes = ip->swap_successes;
1274 return copy_to_user(uip, &info, sizeof(info));
1275}
1276
1277static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in,
1278 int version)
1279{
1280 if (version == IPC_64) {
1281 struct compat_shmid64_ds v;
1282 memset(&v, 0, sizeof(v));
1283 to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm);
1284 v.shm_atime = lower_32_bits(in->shm_atime);
1285 v.shm_atime_high = upper_32_bits(in->shm_atime);
1286 v.shm_dtime = lower_32_bits(in->shm_dtime);
1287 v.shm_dtime_high = upper_32_bits(in->shm_dtime);
1288 v.shm_ctime = lower_32_bits(in->shm_ctime);
1289 v.shm_ctime_high = upper_32_bits(in->shm_ctime);
1290 v.shm_segsz = in->shm_segsz;
1291 v.shm_nattch = in->shm_nattch;
1292 v.shm_cpid = in->shm_cpid;
1293 v.shm_lpid = in->shm_lpid;
1294 return copy_to_user(buf, &v, sizeof(v));
1295 } else {
1296 struct compat_shmid_ds v;
1297 memset(&v, 0, sizeof(v));
1298 to_compat_ipc_perm(&v.shm_perm, &in->shm_perm);
1299 v.shm_perm.key = in->shm_perm.key;
1300 v.shm_atime = in->shm_atime;
1301 v.shm_dtime = in->shm_dtime;
1302 v.shm_ctime = in->shm_ctime;
1303 v.shm_segsz = in->shm_segsz;
1304 v.shm_nattch = in->shm_nattch;
1305 v.shm_cpid = in->shm_cpid;
1306 v.shm_lpid = in->shm_lpid;
1307 return copy_to_user(buf, &v, sizeof(v));
1308 }
1309}
1310
1311static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf,
1312 int version)
1313{
1314 memset(out, 0, sizeof(*out));
1315 if (version == IPC_64) {
1316 struct compat_shmid64_ds __user *p = buf;
1317 return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm);
1318 } else {
1319 struct compat_shmid_ds __user *p = buf;
1320 return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm);
1321 }
1322}
1323
1324long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr)
1325{
1326 struct ipc_namespace *ns;
1327 struct shmid64_ds sem64;
1328 int version = compat_ipc_parse_version(&cmd);
1329 int err;
1330
1331 ns = current->nsproxy->ipc_ns;
1332
1333 if (cmd < 0 || shmid < 0)
1334 return -EINVAL;
1335
1336 switch (cmd) {
1337 case IPC_INFO: {
1338 struct shminfo64 shminfo;
1339 err = shmctl_ipc_info(ns, &shminfo);
1340 if (err < 0)
1341 return err;
1342 if (copy_compat_shminfo_to_user(uptr, &shminfo, version))
1343 err = -EFAULT;
1344 return err;
1345 }
1346 case SHM_INFO: {
1347 struct shm_info shm_info;
1348 err = shmctl_shm_info(ns, &shm_info);
1349 if (err < 0)
1350 return err;
1351 if (put_compat_shm_info(&shm_info, uptr))
1352 err = -EFAULT;
1353 return err;
1354 }
1355 case IPC_STAT:
1356 case SHM_STAT_ANY:
1357 case SHM_STAT:
1358 err = shmctl_stat(ns, shmid, cmd, &sem64);
1359 if (err < 0)
1360 return err;
1361 if (copy_compat_shmid_to_user(uptr, &sem64, version))
1362 err = -EFAULT;
1363 return err;
1364
1365 case IPC_SET:
1366 if (copy_compat_shmid_from_user(&sem64, uptr, version))
1367 return -EFAULT;
1368 /* fallthru */
1369 case IPC_RMID:
1370 return shmctl_down(ns, shmid, cmd, &sem64);
1371 case SHM_LOCK:
1372 case SHM_UNLOCK:
1373 return shmctl_do_lock(ns, shmid, cmd);
1374 break;
1375 default:
1376 return -EINVAL;
1377 }
1378 return err;
1379}
1380
1381COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr)
1382{
1383 return compat_ksys_shmctl(shmid, cmd, uptr);
1384}
1385#endif
1386
1387/*
1388 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
1389 *
1390 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
1391 * "raddr" thing points to kernel space, and there has to be a wrapper around
1392 * this.
1393 */
1394long do_shmat(int shmid, char __user *shmaddr, int shmflg,
1395 ulong *raddr, unsigned long shmlba)
1396{
1397 struct shmid_kernel *shp;
1398 unsigned long addr = (unsigned long)shmaddr;
1399 unsigned long size;
1400 struct file *file, *base;
1401 int err;
1402 unsigned long flags = MAP_SHARED;
1403 unsigned long prot;
1404 int acc_mode;
1405 struct ipc_namespace *ns;
1406 struct shm_file_data *sfd;
1407 int f_flags;
1408 unsigned long populate = 0;
1409
1410 err = -EINVAL;
1411 if (shmid < 0)
1412 goto out;
1413
1414 if (addr) {
1415 if (addr & (shmlba - 1)) {
1416 if (shmflg & SHM_RND) {
1417 addr &= ~(shmlba - 1); /* round down */
1418
1419 /*
1420 * Ensure that the round-down is non-nil
1421 * when remapping. This can happen for
1422 * cases when addr < shmlba.
1423 */
1424 if (!addr && (shmflg & SHM_REMAP))
1425 goto out;
1426 } else
1427#ifndef __ARCH_FORCE_SHMLBA
1428 if (addr & ~PAGE_MASK)
1429#endif
1430 goto out;
1431 }
1432
1433 flags |= MAP_FIXED;
1434 } else if ((shmflg & SHM_REMAP))
1435 goto out;
1436
1437 if (shmflg & SHM_RDONLY) {
1438 prot = PROT_READ;
1439 acc_mode = S_IRUGO;
1440 f_flags = O_RDONLY;
1441 } else {
1442 prot = PROT_READ | PROT_WRITE;
1443 acc_mode = S_IRUGO | S_IWUGO;
1444 f_flags = O_RDWR;
1445 }
1446 if (shmflg & SHM_EXEC) {
1447 prot |= PROT_EXEC;
1448 acc_mode |= S_IXUGO;
1449 }
1450
1451 /*
1452 * We cannot rely on the fs check since SYSV IPC does have an
1453 * additional creator id...
1454 */
1455 ns = current->nsproxy->ipc_ns;
1456 rcu_read_lock();
1457 shp = shm_obtain_object_check(ns, shmid);
1458 if (IS_ERR(shp)) {
1459 err = PTR_ERR(shp);
1460 goto out_unlock;
1461 }
1462
1463 err = -EACCES;
1464 if (ipcperms(ns, &shp->shm_perm, acc_mode))
1465 goto out_unlock;
1466
1467 err = security_shm_shmat(&shp->shm_perm, shmaddr, shmflg);
1468 if (err)
1469 goto out_unlock;
1470
1471 ipc_lock_object(&shp->shm_perm);
1472
1473 /* check if shm_destroy() is tearing down shp */
1474 if (!ipc_valid_object(&shp->shm_perm)) {
1475 ipc_unlock_object(&shp->shm_perm);
1476 err = -EIDRM;
1477 goto out_unlock;
1478 }
1479
1480 /*
1481 * We need to take a reference to the real shm file to prevent the
1482 * pointer from becoming stale in cases where the lifetime of the outer
1483 * file extends beyond that of the shm segment. It's not usually
1484 * possible, but it can happen during remap_file_pages() emulation as
1485 * that unmaps the memory, then does ->mmap() via file reference only.
1486 * We'll deny the ->mmap() if the shm segment was since removed, but to
1487 * detect shm ID reuse we need to compare the file pointers.
1488 */
1489 base = get_file(shp->shm_file);
1490 shp->shm_nattch++;
1491 size = i_size_read(file_inode(base));
1492 ipc_unlock_object(&shp->shm_perm);
1493 rcu_read_unlock();
1494
1495 err = -ENOMEM;
1496 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
1497 if (!sfd) {
1498 fput(base);
1499 goto out_nattch;
1500 }
1501
1502 file = alloc_file_clone(base, f_flags,
1503 is_file_hugepages(base) ?
1504 &shm_file_operations_huge :
1505 &shm_file_operations);
1506 err = PTR_ERR(file);
1507 if (IS_ERR(file)) {
1508 kfree(sfd);
1509 fput(base);
1510 goto out_nattch;
1511 }
1512
1513 sfd->id = shp->shm_perm.id;
1514 sfd->ns = get_ipc_ns(ns);
1515 sfd->file = base;
1516 sfd->vm_ops = NULL;
1517 file->private_data = sfd;
1518
1519 err = security_mmap_file(file, prot, flags);
1520 if (err)
1521 goto out_fput;
1522
1523 if (down_write_killable(&current->mm->mmap_sem)) {
1524 err = -EINTR;
1525 goto out_fput;
1526 }
1527
1528 if (addr && !(shmflg & SHM_REMAP)) {
1529 err = -EINVAL;
1530 if (addr + size < addr)
1531 goto invalid;
1532
1533 if (find_vma_intersection(current->mm, addr, addr + size))
1534 goto invalid;
1535 }
1536
1537 addr = do_mmap_pgoff(file, addr, size, prot, flags, 0, &populate, NULL);
1538 *raddr = addr;
1539 err = 0;
1540 if (IS_ERR_VALUE(addr))
1541 err = (long)addr;
1542invalid:
1543 up_write(&current->mm->mmap_sem);
1544 if (populate)
1545 mm_populate(addr, populate);
1546
1547out_fput:
1548 fput(file);
1549
1550out_nattch:
1551 down_write(&shm_ids(ns).rwsem);
1552 shp = shm_lock(ns, shmid);
1553 shp->shm_nattch--;
1554 if (shm_may_destroy(ns, shp))
1555 shm_destroy(ns, shp);
1556 else
1557 shm_unlock(shp);
1558 up_write(&shm_ids(ns).rwsem);
1559 return err;
1560
1561out_unlock:
1562 rcu_read_unlock();
1563out:
1564 return err;
1565}
1566
1567SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
1568{
1569 unsigned long ret;
1570 long err;
1571
1572 err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
1573 if (err)
1574 return err;
1575 force_successful_syscall_return();
1576 return (long)ret;
1577}
1578
1579#ifdef CONFIG_COMPAT
1580
1581#ifndef COMPAT_SHMLBA
1582#define COMPAT_SHMLBA SHMLBA
1583#endif
1584
1585COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg)
1586{
1587 unsigned long ret;
1588 long err;
1589
1590 err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
1591 if (err)
1592 return err;
1593 force_successful_syscall_return();
1594 return (long)ret;
1595}
1596#endif
1597
1598/*
1599 * detach and kill segment if marked destroyed.
1600 * The work is done in shm_close.
1601 */
1602long ksys_shmdt(char __user *shmaddr)
1603{
1604 struct mm_struct *mm = current->mm;
1605 struct vm_area_struct *vma;
1606 unsigned long addr = (unsigned long)shmaddr;
1607 int retval = -EINVAL;
1608#ifdef CONFIG_MMU
1609 loff_t size = 0;
1610 struct file *file;
1611 struct vm_area_struct *next;
1612#endif
1613
1614 if (addr & ~PAGE_MASK)
1615 return retval;
1616
1617 if (down_write_killable(&mm->mmap_sem))
1618 return -EINTR;
1619
1620 /*
1621 * This function tries to be smart and unmap shm segments that
1622 * were modified by partial mlock or munmap calls:
1623 * - It first determines the size of the shm segment that should be
1624 * unmapped: It searches for a vma that is backed by shm and that
1625 * started at address shmaddr. It records it's size and then unmaps
1626 * it.
1627 * - Then it unmaps all shm vmas that started at shmaddr and that
1628 * are within the initially determined size and that are from the
1629 * same shm segment from which we determined the size.
1630 * Errors from do_munmap are ignored: the function only fails if
1631 * it's called with invalid parameters or if it's called to unmap
1632 * a part of a vma. Both calls in this function are for full vmas,
1633 * the parameters are directly copied from the vma itself and always
1634 * valid - therefore do_munmap cannot fail. (famous last words?)
1635 */
1636 /*
1637 * If it had been mremap()'d, the starting address would not
1638 * match the usual checks anyway. So assume all vma's are
1639 * above the starting address given.
1640 */
1641 vma = find_vma(mm, addr);
1642
1643#ifdef CONFIG_MMU
1644 while (vma) {
1645 next = vma->vm_next;
1646
1647 /*
1648 * Check if the starting address would match, i.e. it's
1649 * a fragment created by mprotect() and/or munmap(), or it
1650 * otherwise it starts at this address with no hassles.
1651 */
1652 if ((vma->vm_ops == &shm_vm_ops) &&
1653 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1654
1655 /*
1656 * Record the file of the shm segment being
1657 * unmapped. With mremap(), someone could place
1658 * page from another segment but with equal offsets
1659 * in the range we are unmapping.
1660 */
1661 file = vma->vm_file;
1662 size = i_size_read(file_inode(vma->vm_file));
1663 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1664 /*
1665 * We discovered the size of the shm segment, so
1666 * break out of here and fall through to the next
1667 * loop that uses the size information to stop
1668 * searching for matching vma's.
1669 */
1670 retval = 0;
1671 vma = next;
1672 break;
1673 }
1674 vma = next;
1675 }
1676
1677 /*
1678 * We need look no further than the maximum address a fragment
1679 * could possibly have landed at. Also cast things to loff_t to
1680 * prevent overflows and make comparisons vs. equal-width types.
1681 */
1682 size = PAGE_ALIGN(size);
1683 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1684 next = vma->vm_next;
1685
1686 /* finding a matching vma now does not alter retval */
1687 if ((vma->vm_ops == &shm_vm_ops) &&
1688 ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
1689 (vma->vm_file == file))
1690 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1691 vma = next;
1692 }
1693
1694#else /* CONFIG_MMU */
1695 /* under NOMMU conditions, the exact address to be destroyed must be
1696 * given
1697 */
1698 if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
1699 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1700 retval = 0;
1701 }
1702
1703#endif
1704
1705 up_write(&mm->mmap_sem);
1706 return retval;
1707}
1708
1709SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1710{
1711 return ksys_shmdt(shmaddr);
1712}
1713
1714#ifdef CONFIG_PROC_FS
1715static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1716{
1717 struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
1718 struct user_namespace *user_ns = seq_user_ns(s);
1719 struct kern_ipc_perm *ipcp = it;
1720 struct shmid_kernel *shp;
1721 unsigned long rss = 0, swp = 0;
1722
1723 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
1724 shm_add_rss_swap(shp, &rss, &swp);
1725
1726#if BITS_PER_LONG <= 32
1727#define SIZE_SPEC "%10lu"
1728#else
1729#define SIZE_SPEC "%21lu"
1730#endif
1731
1732 seq_printf(s,
1733 "%10d %10d %4o " SIZE_SPEC " %5u %5u "
1734 "%5lu %5u %5u %5u %5u %10llu %10llu %10llu "
1735 SIZE_SPEC " " SIZE_SPEC "\n",
1736 shp->shm_perm.key,
1737 shp->shm_perm.id,
1738 shp->shm_perm.mode,
1739 shp->shm_segsz,
1740 pid_nr_ns(shp->shm_cprid, pid_ns),
1741 pid_nr_ns(shp->shm_lprid, pid_ns),
1742 shp->shm_nattch,
1743 from_kuid_munged(user_ns, shp->shm_perm.uid),
1744 from_kgid_munged(user_ns, shp->shm_perm.gid),
1745 from_kuid_munged(user_ns, shp->shm_perm.cuid),
1746 from_kgid_munged(user_ns, shp->shm_perm.cgid),
1747 shp->shm_atim,
1748 shp->shm_dtim,
1749 shp->shm_ctim,
1750 rss * PAGE_SIZE,
1751 swp * PAGE_SIZE);
1752
1753 return 0;
1754}
1755#endif