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