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