1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2006, 2011, 2016-2017 Robert N. M. Watson 5 * Copyright 2020 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * Portions of this software were developed by BAE Systems, the University of 9 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL 10 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent 11 * Computing (TC) research program. 12 * 13 * Portions of this software were developed by Konstantin Belousov 14 * under sponsorship from the FreeBSD Foundation. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 /* 39 * Support for shared swap-backed anonymous memory objects via 40 * shm_open(2), shm_rename(2), and shm_unlink(2). 41 * While most of the implementation is here, vm_mmap.c contains 42 * mapping logic changes. 43 * 44 * posixshmcontrol(1) allows users to inspect the state of the memory 45 * objects. Per-uid swap resource limit controls total amount of 46 * memory that user can consume for anonymous objects, including 47 * shared. 48 */ 49 50 #include <sys/cdefs.h> 51 #include "opt_capsicum.h" 52 #include "opt_ktrace.h" 53 54 #include <sys/param.h> 55 #include <sys/capsicum.h> 56 #include <sys/conf.h> 57 #include <sys/fcntl.h> 58 #include <sys/file.h> 59 #include <sys/filedesc.h> 60 #include <sys/filio.h> 61 #include <sys/fnv_hash.h> 62 #include <sys/kernel.h> 63 #include <sys/limits.h> 64 #include <sys/uio.h> 65 #include <sys/signal.h> 66 #include <sys/jail.h> 67 #include <sys/ktrace.h> 68 #include <sys/lock.h> 69 #include <sys/malloc.h> 70 #include <sys/mman.h> 71 #include <sys/mutex.h> 72 #include <sys/priv.h> 73 #include <sys/proc.h> 74 #include <sys/refcount.h> 75 #include <sys/resourcevar.h> 76 #include <sys/rwlock.h> 77 #include <sys/sbuf.h> 78 #include <sys/stat.h> 79 #include <sys/syscallsubr.h> 80 #include <sys/sysctl.h> 81 #include <sys/sysproto.h> 82 #include <sys/systm.h> 83 #include <sys/sx.h> 84 #include <sys/time.h> 85 #include <sys/vmmeter.h> 86 #include <sys/vnode.h> 87 #include <sys/unistd.h> 88 #include <sys/user.h> 89 90 #include <security/audit/audit.h> 91 #include <security/mac/mac_framework.h> 92 93 #include <vm/vm.h> 94 #include <vm/vm_param.h> 95 #include <vm/pmap.h> 96 #include <vm/vm_extern.h> 97 #include <vm/vm_map.h> 98 #include <vm/vm_kern.h> 99 #include <vm/vm_object.h> 100 #include <vm/vm_page.h> 101 #include <vm/vm_pageout.h> 102 #include <vm/vm_pager.h> 103 #include <vm/vm_radix.h> 104 #include <vm/swap_pager.h> 105 106 struct shm_mapping { 107 char *sm_path; 108 Fnv32_t sm_fnv; 109 struct shmfd *sm_shmfd; 110 LIST_ENTRY(shm_mapping) sm_link; 111 }; 112 113 static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor"); 114 static LIST_HEAD(, shm_mapping) *shm_dictionary; 115 static struct sx shm_dict_lock; 116 static struct mtx shm_timestamp_lock; 117 static u_long shm_hash; 118 static struct unrhdr64 shm_ino_unr; 119 static dev_t shm_dev_ino; 120 121 #define SHM_HASH(fnv) (&shm_dictionary[(fnv) & shm_hash]) 122 123 static void shm_init(void *arg); 124 static void shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd); 125 static struct shmfd *shm_lookup(char *path, Fnv32_t fnv); 126 static int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred); 127 static void shm_doremove(struct shm_mapping *map); 128 static int shm_dotruncate_cookie(struct shmfd *shmfd, off_t length, 129 void *rl_cookie); 130 static int shm_dotruncate_locked(struct shmfd *shmfd, off_t length, 131 void *rl_cookie); 132 static int shm_copyin_path(struct thread *td, const char *userpath_in, 133 char **path_out); 134 static int shm_deallocate(struct shmfd *shmfd, off_t *offset, 135 off_t *length, int flags); 136 137 static fo_rdwr_t shm_read; 138 static fo_rdwr_t shm_write; 139 static fo_truncate_t shm_truncate; 140 static fo_ioctl_t shm_ioctl; 141 static fo_stat_t shm_stat; 142 static fo_close_t shm_close; 143 static fo_chmod_t shm_chmod; 144 static fo_chown_t shm_chown; 145 static fo_seek_t shm_seek; 146 static fo_fill_kinfo_t shm_fill_kinfo; 147 static fo_mmap_t shm_mmap; 148 static fo_get_seals_t shm_get_seals; 149 static fo_add_seals_t shm_add_seals; 150 static fo_fallocate_t shm_fallocate; 151 static fo_fspacectl_t shm_fspacectl; 152 153 /* File descriptor operations. */ 154 const struct fileops shm_ops = { 155 .fo_read = shm_read, 156 .fo_write = shm_write, 157 .fo_truncate = shm_truncate, 158 .fo_ioctl = shm_ioctl, 159 .fo_poll = invfo_poll, 160 .fo_kqfilter = invfo_kqfilter, 161 .fo_stat = shm_stat, 162 .fo_close = shm_close, 163 .fo_chmod = shm_chmod, 164 .fo_chown = shm_chown, 165 .fo_sendfile = vn_sendfile, 166 .fo_seek = shm_seek, 167 .fo_fill_kinfo = shm_fill_kinfo, 168 .fo_mmap = shm_mmap, 169 .fo_get_seals = shm_get_seals, 170 .fo_add_seals = shm_add_seals, 171 .fo_fallocate = shm_fallocate, 172 .fo_fspacectl = shm_fspacectl, 173 .fo_cmp = file_kcmp_generic, 174 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE, 175 }; 176 177 FEATURE(posix_shm, "POSIX shared memory"); 178 179 static SYSCTL_NODE(_vm, OID_AUTO, largepages, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 180 ""); 181 182 static int largepage_reclaim_tries = 1; 183 SYSCTL_INT(_vm_largepages, OID_AUTO, reclaim_tries, 184 CTLFLAG_RWTUN, &largepage_reclaim_tries, 0, 185 "Number of contig reclaims before giving up for default alloc policy"); 186 187 #define shm_rangelock_unlock(shmfd, cookie) \ 188 rangelock_unlock(&(shmfd)->shm_rl, (cookie)) 189 #define shm_rangelock_rlock(shmfd, start, end) \ 190 rangelock_rlock(&(shmfd)->shm_rl, (start), (end)) 191 #define shm_rangelock_tryrlock(shmfd, start, end) \ 192 rangelock_tryrlock(&(shmfd)->shm_rl, (start), (end)) 193 #define shm_rangelock_wlock(shmfd, start, end) \ 194 rangelock_wlock(&(shmfd)->shm_rl, (start), (end)) 195 196 static int 197 uiomove_object_page(vm_object_t obj, size_t len, struct uio *uio) 198 { 199 struct pctrie_iter pages; 200 vm_page_t m; 201 vm_pindex_t idx; 202 size_t tlen; 203 int error, offset, rv; 204 205 idx = OFF_TO_IDX(uio->uio_offset); 206 offset = uio->uio_offset & PAGE_MASK; 207 tlen = MIN(PAGE_SIZE - offset, len); 208 209 rv = vm_page_grab_valid_unlocked(&m, obj, idx, 210 VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY | VM_ALLOC_NOCREAT); 211 if (rv == VM_PAGER_OK) 212 goto found; 213 214 /* 215 * Read I/O without either a corresponding resident page or swap 216 * page: use zero_region. This is intended to avoid instantiating 217 * pages on read from a sparse region. 218 */ 219 vm_page_iter_init(&pages, obj); 220 VM_OBJECT_WLOCK(obj); 221 m = vm_radix_iter_lookup(&pages, idx); 222 if (uio->uio_rw == UIO_READ && m == NULL && 223 !vm_pager_has_page(obj, idx, NULL, NULL)) { 224 VM_OBJECT_WUNLOCK(obj); 225 return (uiomove(__DECONST(void *, zero_region), tlen, uio)); 226 } 227 228 /* 229 * Although the tmpfs vnode lock is held here, it is 230 * nonetheless safe to sleep waiting for a free page. The 231 * pageout daemon does not need to acquire the tmpfs vnode 232 * lock to page out tobj's pages because tobj is a OBJT_SWAP 233 * type object. 234 */ 235 rv = vm_page_grab_valid_iter(&m, obj, idx, 236 VM_ALLOC_NORMAL | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY, &pages); 237 if (rv != VM_PAGER_OK) { 238 VM_OBJECT_WUNLOCK(obj); 239 if (bootverbose) { 240 printf("uiomove_object: vm_obj %p idx %jd " 241 "pager error %d\n", obj, idx, rv); 242 } 243 return (rv == VM_PAGER_AGAIN ? ENOSPC : EIO); 244 } 245 VM_OBJECT_WUNLOCK(obj); 246 247 found: 248 error = uiomove_fromphys(&m, offset, tlen, uio); 249 if (uio->uio_rw == UIO_WRITE && error == 0) 250 vm_page_set_dirty(m); 251 vm_page_activate(m); 252 vm_page_sunbusy(m); 253 254 return (error); 255 } 256 257 int 258 uiomove_object(vm_object_t obj, off_t obj_size, struct uio *uio) 259 { 260 ssize_t resid; 261 size_t len; 262 int error; 263 264 error = 0; 265 while ((resid = uio->uio_resid) > 0) { 266 if (obj_size <= uio->uio_offset) 267 break; 268 len = MIN(obj_size - uio->uio_offset, resid); 269 if (len == 0) 270 break; 271 error = uiomove_object_page(obj, len, uio); 272 if (error != 0 || resid == uio->uio_resid) 273 break; 274 } 275 return (error); 276 } 277 278 static u_long count_largepages[MAXPAGESIZES]; 279 280 static int 281 shm_largepage_phys_populate(vm_object_t object, vm_pindex_t pidx, 282 int fault_type, vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last) 283 { 284 vm_page_t m __diagused; 285 int psind; 286 287 psind = object->un_pager.phys.data_val; 288 if (psind == 0 || pidx >= object->size) 289 return (VM_PAGER_FAIL); 290 *first = rounddown2(pidx, pagesizes[psind] / PAGE_SIZE); 291 292 /* 293 * We only busy the first page in the superpage run. It is 294 * useless to busy whole run since we only remove full 295 * superpage, and it takes too long to busy e.g. 512 * 512 == 296 * 262144 pages constituing 1G amd64 superage. 297 */ 298 m = vm_page_grab(object, *first, VM_ALLOC_NORMAL | VM_ALLOC_NOCREAT); 299 MPASS(m != NULL); 300 301 *last = *first + atop(pagesizes[psind]) - 1; 302 return (VM_PAGER_OK); 303 } 304 305 static boolean_t 306 shm_largepage_phys_haspage(vm_object_t object, vm_pindex_t pindex, 307 int *before, int *after) 308 { 309 int psind; 310 311 psind = object->un_pager.phys.data_val; 312 if (psind == 0 || pindex >= object->size) 313 return (FALSE); 314 if (before != NULL) { 315 *before = pindex - rounddown2(pindex, pagesizes[psind] / 316 PAGE_SIZE); 317 } 318 if (after != NULL) { 319 *after = roundup2(pindex, pagesizes[psind] / PAGE_SIZE) - 320 pindex; 321 } 322 return (TRUE); 323 } 324 325 static void 326 shm_largepage_phys_ctor(vm_object_t object, vm_prot_t prot, 327 vm_ooffset_t foff, struct ucred *cred) 328 { 329 } 330 331 static void 332 shm_largepage_phys_dtor(vm_object_t object) 333 { 334 int psind; 335 336 psind = object->un_pager.phys.data_val; 337 if (psind != 0) { 338 atomic_subtract_long(&count_largepages[psind], 339 object->size / (pagesizes[psind] / PAGE_SIZE)); 340 vm_wire_sub(object->size); 341 } else { 342 KASSERT(object->size == 0, 343 ("largepage phys obj %p not initialized bit size %#jx > 0", 344 object, (uintmax_t)object->size)); 345 } 346 } 347 348 static const struct phys_pager_ops shm_largepage_phys_ops = { 349 .phys_pg_populate = shm_largepage_phys_populate, 350 .phys_pg_haspage = shm_largepage_phys_haspage, 351 .phys_pg_ctor = shm_largepage_phys_ctor, 352 .phys_pg_dtor = shm_largepage_phys_dtor, 353 }; 354 355 bool 356 shm_largepage(struct shmfd *shmfd) 357 { 358 return (shmfd->shm_object->type == OBJT_PHYS); 359 } 360 361 static void 362 shm_pager_freespace(vm_object_t obj, vm_pindex_t start, vm_size_t size) 363 { 364 struct shmfd *shm; 365 vm_size_t c; 366 367 swap_pager_freespace(obj, start, size, &c); 368 if (c == 0) 369 return; 370 371 shm = obj->un_pager.swp.swp_priv; 372 if (shm == NULL) 373 return; 374 KASSERT(shm->shm_pages >= c, 375 ("shm %p pages %jd free %jd", shm, 376 (uintmax_t)shm->shm_pages, (uintmax_t)c)); 377 shm->shm_pages -= c; 378 } 379 380 static void 381 shm_page_inserted(vm_object_t obj, vm_page_t m) 382 { 383 struct shmfd *shm; 384 385 shm = obj->un_pager.swp.swp_priv; 386 if (shm == NULL) 387 return; 388 if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) 389 shm->shm_pages += 1; 390 } 391 392 static void 393 shm_page_removed(vm_object_t obj, vm_page_t m) 394 { 395 struct shmfd *shm; 396 397 shm = obj->un_pager.swp.swp_priv; 398 if (shm == NULL) 399 return; 400 if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) { 401 KASSERT(shm->shm_pages >= 1, 402 ("shm %p pages %jd free 1", shm, 403 (uintmax_t)shm->shm_pages)); 404 shm->shm_pages -= 1; 405 } 406 } 407 408 static struct pagerops shm_swap_pager_ops = { 409 .pgo_kvme_type = KVME_TYPE_SWAP, 410 .pgo_freespace = shm_pager_freespace, 411 .pgo_page_inserted = shm_page_inserted, 412 .pgo_page_removed = shm_page_removed, 413 }; 414 static int shmfd_pager_type = -1; 415 416 static int 417 shm_seek(struct file *fp, off_t offset, int whence, struct thread *td) 418 { 419 struct shmfd *shmfd; 420 off_t foffset; 421 int error; 422 423 shmfd = fp->f_data; 424 foffset = foffset_lock(fp, 0); 425 error = 0; 426 switch (whence) { 427 case L_INCR: 428 if (foffset < 0 || 429 (offset > 0 && foffset > OFF_MAX - offset)) { 430 error = EOVERFLOW; 431 break; 432 } 433 offset += foffset; 434 break; 435 case L_XTND: 436 if (offset > 0 && shmfd->shm_size > OFF_MAX - offset) { 437 error = EOVERFLOW; 438 break; 439 } 440 offset += shmfd->shm_size; 441 break; 442 case L_SET: 443 break; 444 default: 445 error = EINVAL; 446 } 447 if (error == 0) { 448 if (offset < 0 || offset > shmfd->shm_size) 449 error = EINVAL; 450 else 451 td->td_uretoff.tdu_off = offset; 452 } 453 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0); 454 return (error); 455 } 456 457 static int 458 shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 459 int flags, struct thread *td) 460 { 461 struct shmfd *shmfd; 462 void *rl_cookie; 463 int error; 464 465 shmfd = fp->f_data; 466 #ifdef MAC 467 error = mac_posixshm_check_read(active_cred, fp->f_cred, shmfd); 468 if (error) 469 return (error); 470 #endif 471 foffset_lock_uio(fp, uio, flags); 472 rl_cookie = shm_rangelock_rlock(shmfd, uio->uio_offset, 473 uio->uio_offset + uio->uio_resid); 474 error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio); 475 shm_rangelock_unlock(shmfd, rl_cookie); 476 foffset_unlock_uio(fp, uio, flags); 477 return (error); 478 } 479 480 static int 481 shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 482 int flags, struct thread *td) 483 { 484 struct shmfd *shmfd; 485 void *rl_cookie; 486 int error; 487 off_t newsize; 488 489 KASSERT((flags & FOF_OFFSET) == 0 || uio->uio_offset >= 0, 490 ("%s: negative offset", __func__)); 491 492 shmfd = fp->f_data; 493 #ifdef MAC 494 error = mac_posixshm_check_write(active_cred, fp->f_cred, shmfd); 495 if (error) 496 return (error); 497 #endif 498 if (shm_largepage(shmfd) && shmfd->shm_lp_psind == 0) 499 return (EINVAL); 500 foffset_lock_uio(fp, uio, flags); 501 if (uio->uio_resid > OFF_MAX - uio->uio_offset) { 502 /* 503 * Overflow is only an error if we're supposed to expand on 504 * write. Otherwise, we'll just truncate the write to the 505 * size of the file, which can only grow up to OFF_MAX. 506 */ 507 if ((shmfd->shm_flags & SHM_GROW_ON_WRITE) != 0) { 508 foffset_unlock_uio(fp, uio, flags); 509 return (EFBIG); 510 } 511 512 newsize = atomic_load_64(&shmfd->shm_size); 513 } else { 514 newsize = uio->uio_offset + uio->uio_resid; 515 } 516 if ((flags & FOF_OFFSET) == 0) 517 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX); 518 else 519 rl_cookie = shm_rangelock_wlock(shmfd, uio->uio_offset, 520 MAX(newsize, uio->uio_offset)); 521 if ((shmfd->shm_seals & F_SEAL_WRITE) != 0) { 522 error = EPERM; 523 } else { 524 error = 0; 525 if ((shmfd->shm_flags & SHM_GROW_ON_WRITE) != 0 && 526 newsize > shmfd->shm_size) { 527 error = shm_dotruncate_cookie(shmfd, newsize, 528 rl_cookie); 529 } 530 if (error == 0) 531 error = uiomove_object(shmfd->shm_object, 532 shmfd->shm_size, uio); 533 } 534 shm_rangelock_unlock(shmfd, rl_cookie); 535 foffset_unlock_uio(fp, uio, flags); 536 return (error); 537 } 538 539 static int 540 shm_truncate(struct file *fp, off_t length, struct ucred *active_cred, 541 struct thread *td) 542 { 543 struct shmfd *shmfd; 544 #ifdef MAC 545 int error; 546 #endif 547 548 shmfd = fp->f_data; 549 #ifdef MAC 550 error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd); 551 if (error) 552 return (error); 553 #endif 554 return (shm_dotruncate(shmfd, length)); 555 } 556 557 int 558 shm_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, 559 struct thread *td) 560 { 561 struct shmfd *shmfd; 562 struct shm_largepage_conf *conf; 563 void *rl_cookie; 564 565 shmfd = fp->f_data; 566 switch (com) { 567 case FIONBIO: 568 case FIOASYNC: 569 /* 570 * Allow fcntl(fd, F_SETFL, O_NONBLOCK) to work, 571 * just like it would on an unlinked regular file 572 */ 573 return (0); 574 case FIOSSHMLPGCNF: 575 if (!shm_largepage(shmfd)) 576 return (ENOTTY); 577 conf = data; 578 if (shmfd->shm_lp_psind != 0 && 579 conf->psind != shmfd->shm_lp_psind) 580 return (EINVAL); 581 if (conf->psind <= 0 || conf->psind >= MAXPAGESIZES || 582 pagesizes[conf->psind] == 0) 583 return (EINVAL); 584 if (conf->alloc_policy != SHM_LARGEPAGE_ALLOC_DEFAULT && 585 conf->alloc_policy != SHM_LARGEPAGE_ALLOC_NOWAIT && 586 conf->alloc_policy != SHM_LARGEPAGE_ALLOC_HARD) 587 return (EINVAL); 588 589 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX); 590 shmfd->shm_lp_psind = conf->psind; 591 shmfd->shm_lp_alloc_policy = conf->alloc_policy; 592 shmfd->shm_object->un_pager.phys.data_val = conf->psind; 593 shm_rangelock_unlock(shmfd, rl_cookie); 594 return (0); 595 case FIOGSHMLPGCNF: 596 if (!shm_largepage(shmfd)) 597 return (ENOTTY); 598 conf = data; 599 rl_cookie = shm_rangelock_rlock(shmfd, 0, OFF_MAX); 600 conf->psind = shmfd->shm_lp_psind; 601 conf->alloc_policy = shmfd->shm_lp_alloc_policy; 602 shm_rangelock_unlock(shmfd, rl_cookie); 603 return (0); 604 default: 605 return (ENOTTY); 606 } 607 } 608 609 static int 610 shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred) 611 { 612 struct shmfd *shmfd; 613 #ifdef MAC 614 int error; 615 #endif 616 617 shmfd = fp->f_data; 618 619 #ifdef MAC 620 error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd); 621 if (error) 622 return (error); 623 #endif 624 625 /* 626 * Attempt to return sanish values for fstat() on a memory file 627 * descriptor. 628 */ 629 bzero(sb, sizeof(*sb)); 630 sb->st_blksize = PAGE_SIZE; 631 sb->st_size = shmfd->shm_size; 632 mtx_lock(&shm_timestamp_lock); 633 sb->st_atim = shmfd->shm_atime; 634 sb->st_ctim = shmfd->shm_ctime; 635 sb->st_mtim = shmfd->shm_mtime; 636 sb->st_birthtim = shmfd->shm_birthtime; 637 sb->st_mode = S_IFREG | shmfd->shm_mode; /* XXX */ 638 sb->st_uid = shmfd->shm_uid; 639 sb->st_gid = shmfd->shm_gid; 640 mtx_unlock(&shm_timestamp_lock); 641 sb->st_dev = shm_dev_ino; 642 sb->st_ino = shmfd->shm_ino; 643 sb->st_nlink = shmfd->shm_object->ref_count; 644 if (shm_largepage(shmfd)) { 645 sb->st_blocks = shmfd->shm_object->size / 646 (pagesizes[shmfd->shm_lp_psind] >> PAGE_SHIFT); 647 } else { 648 sb->st_blocks = shmfd->shm_pages; 649 } 650 651 return (0); 652 } 653 654 static int 655 shm_close(struct file *fp, struct thread *td) 656 { 657 struct shmfd *shmfd; 658 659 shmfd = fp->f_data; 660 fp->f_data = NULL; 661 shm_drop(shmfd); 662 663 return (0); 664 } 665 666 static int 667 shm_copyin_path(struct thread *td, const char *userpath_in, char **path_out) { 668 int error; 669 char *path; 670 const char *pr_path; 671 size_t pr_pathlen; 672 673 path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); 674 pr_path = td->td_ucred->cr_prison->pr_path; 675 676 /* Construct a full pathname for jailed callers. */ 677 pr_pathlen = strcmp(pr_path, "/") == 678 0 ? 0 : strlcpy(path, pr_path, MAXPATHLEN); 679 error = copyinstr(userpath_in, path + pr_pathlen, 680 MAXPATHLEN - pr_pathlen, NULL); 681 if (error != 0) 682 goto out; 683 684 #ifdef KTRACE 685 if (KTRPOINT(curthread, KTR_NAMEI)) 686 ktrnamei(path); 687 #endif 688 689 /* Require paths to start with a '/' character. */ 690 if (path[pr_pathlen] != '/') { 691 error = EINVAL; 692 goto out; 693 } 694 695 *path_out = path; 696 697 out: 698 if (error != 0) 699 free(path, M_SHMFD); 700 701 return (error); 702 } 703 704 static int 705 shm_partial_page_invalidate(vm_object_t object, vm_pindex_t idx, int base, 706 int end) 707 { 708 int error; 709 710 error = vm_page_grab_zero_partial(object, idx, base, end); 711 if (error == EIO) 712 VM_OBJECT_WUNLOCK(object); 713 return (error); 714 } 715 716 static int 717 shm_dotruncate_locked(struct shmfd *shmfd, off_t length, void *rl_cookie) 718 { 719 vm_object_t object; 720 vm_pindex_t nobjsize; 721 vm_ooffset_t delta; 722 int base, error; 723 724 KASSERT(length >= 0, ("shm_dotruncate: length < 0")); 725 object = shmfd->shm_object; 726 VM_OBJECT_ASSERT_WLOCKED(object); 727 rangelock_cookie_assert(rl_cookie, RA_WLOCKED); 728 if (length == shmfd->shm_size) 729 return (0); 730 nobjsize = OFF_TO_IDX(length + PAGE_MASK); 731 732 /* Are we shrinking? If so, trim the end. */ 733 if (length < shmfd->shm_size) { 734 if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0) 735 return (EPERM); 736 737 /* 738 * Disallow any requests to shrink the size if this 739 * object is mapped into the kernel. 740 */ 741 if (shmfd->shm_kmappings > 0) 742 return (EBUSY); 743 744 /* 745 * Zero the truncated part of the last page. 746 */ 747 base = length & PAGE_MASK; 748 if (base != 0) { 749 error = shm_partial_page_invalidate(object, 750 OFF_TO_IDX(length), base, PAGE_SIZE); 751 if (error) 752 return (error); 753 } 754 delta = IDX_TO_OFF(object->size - nobjsize); 755 756 if (nobjsize < object->size) 757 vm_object_page_remove(object, nobjsize, object->size, 758 0); 759 760 /* Free the swap accounted for shm */ 761 swap_release_by_cred(delta, object->cred); 762 object->charge -= delta; 763 } else { 764 if ((shmfd->shm_seals & F_SEAL_GROW) != 0) 765 return (EPERM); 766 767 /* Try to reserve additional swap space. */ 768 delta = IDX_TO_OFF(nobjsize - object->size); 769 if (!swap_reserve_by_cred(delta, object->cred)) 770 return (ENOMEM); 771 object->charge += delta; 772 } 773 shmfd->shm_size = length; 774 mtx_lock(&shm_timestamp_lock); 775 vfs_timestamp(&shmfd->shm_ctime); 776 shmfd->shm_mtime = shmfd->shm_ctime; 777 mtx_unlock(&shm_timestamp_lock); 778 object->size = nobjsize; 779 return (0); 780 } 781 782 static int 783 shm_dotruncate_largepage(struct shmfd *shmfd, off_t length, void *rl_cookie) 784 { 785 vm_object_t object; 786 vm_page_t m; 787 vm_pindex_t newobjsz; 788 vm_pindex_t oldobjsz __unused; 789 int aflags, error, i, psind, try; 790 791 KASSERT(length >= 0, ("shm_dotruncate: length < 0")); 792 object = shmfd->shm_object; 793 VM_OBJECT_ASSERT_WLOCKED(object); 794 rangelock_cookie_assert(rl_cookie, RA_WLOCKED); 795 796 oldobjsz = object->size; 797 newobjsz = OFF_TO_IDX(length); 798 if (length == shmfd->shm_size) 799 return (0); 800 psind = shmfd->shm_lp_psind; 801 if (psind == 0 && length != 0) 802 return (EINVAL); 803 if ((length & (pagesizes[psind] - 1)) != 0) 804 return (EINVAL); 805 806 if (length < shmfd->shm_size) { 807 if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0) 808 return (EPERM); 809 if (shmfd->shm_kmappings > 0) 810 return (EBUSY); 811 return (ENOTSUP); /* Pages are unmanaged. */ 812 #if 0 813 vm_object_page_remove(object, newobjsz, oldobjsz, 0); 814 object->size = newobjsz; 815 shmfd->shm_size = length; 816 return (0); 817 #endif 818 } 819 820 if ((shmfd->shm_seals & F_SEAL_GROW) != 0) 821 return (EPERM); 822 823 aflags = VM_ALLOC_NORMAL | VM_ALLOC_ZERO; 824 if (shmfd->shm_lp_alloc_policy == SHM_LARGEPAGE_ALLOC_NOWAIT) 825 aflags |= VM_ALLOC_WAITFAIL; 826 try = 0; 827 828 /* 829 * Extend shmfd and object, keeping all already fully 830 * allocated large pages intact even on error, because dropped 831 * object lock might allowed mapping of them. 832 */ 833 while (object->size < newobjsz) { 834 m = vm_page_alloc_contig(object, object->size, aflags, 835 pagesizes[psind] / PAGE_SIZE, 0, ~0, 836 pagesizes[psind], 0, 837 VM_MEMATTR_DEFAULT); 838 if (m == NULL) { 839 VM_OBJECT_WUNLOCK(object); 840 if (shmfd->shm_lp_alloc_policy == 841 SHM_LARGEPAGE_ALLOC_NOWAIT || 842 (shmfd->shm_lp_alloc_policy == 843 SHM_LARGEPAGE_ALLOC_DEFAULT && 844 try >= largepage_reclaim_tries)) { 845 VM_OBJECT_WLOCK(object); 846 return (ENOMEM); 847 } 848 error = vm_page_reclaim_contig(aflags, 849 pagesizes[psind] / PAGE_SIZE, 0, ~0, 850 pagesizes[psind], 0); 851 if (error == ENOMEM) 852 error = vm_wait_intr(object); 853 if (error != 0) { 854 VM_OBJECT_WLOCK(object); 855 return (error); 856 } 857 try++; 858 VM_OBJECT_WLOCK(object); 859 continue; 860 } 861 try = 0; 862 for (i = 0; i < pagesizes[psind] / PAGE_SIZE; i++) { 863 if ((m[i].flags & PG_ZERO) == 0) 864 pmap_zero_page(&m[i]); 865 vm_page_valid(&m[i]); 866 vm_page_xunbusy(&m[i]); 867 } 868 object->size += OFF_TO_IDX(pagesizes[psind]); 869 shmfd->shm_size += pagesizes[psind]; 870 atomic_add_long(&count_largepages[psind], 1); 871 vm_wire_add(atop(pagesizes[psind])); 872 } 873 return (0); 874 } 875 876 static int 877 shm_dotruncate_cookie(struct shmfd *shmfd, off_t length, void *rl_cookie) 878 { 879 int error; 880 881 VM_OBJECT_WLOCK(shmfd->shm_object); 882 error = shm_largepage(shmfd) ? shm_dotruncate_largepage(shmfd, 883 length, rl_cookie) : shm_dotruncate_locked(shmfd, length, 884 rl_cookie); 885 VM_OBJECT_WUNLOCK(shmfd->shm_object); 886 return (error); 887 } 888 889 int 890 shm_dotruncate(struct shmfd *shmfd, off_t length) 891 { 892 void *rl_cookie; 893 int error; 894 895 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX); 896 error = shm_dotruncate_cookie(shmfd, length, rl_cookie); 897 shm_rangelock_unlock(shmfd, rl_cookie); 898 return (error); 899 } 900 901 /* 902 * shmfd object management including creation and reference counting 903 * routines. 904 */ 905 struct shmfd * 906 shm_alloc(struct ucred *ucred, mode_t mode, bool largepage) 907 { 908 struct shmfd *shmfd; 909 vm_object_t obj; 910 911 if (largepage) { 912 obj = phys_pager_allocate(NULL, &shm_largepage_phys_ops, 913 NULL, 0, VM_PROT_DEFAULT, 0, ucred); 914 } else { 915 obj = vm_pager_allocate(shmfd_pager_type, NULL, 0, 916 VM_PROT_DEFAULT, 0, ucred); 917 } 918 if (obj == NULL) { 919 /* 920 * swap reservation limits can cause object allocation 921 * to fail. 922 */ 923 return (NULL); 924 } 925 926 shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO); 927 shmfd->shm_uid = ucred->cr_uid; 928 shmfd->shm_gid = ucred->cr_gid; 929 shmfd->shm_mode = mode; 930 if (largepage) { 931 obj->un_pager.phys.phys_priv = shmfd; 932 shmfd->shm_lp_alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT; 933 } else { 934 obj->un_pager.swp.swp_priv = shmfd; 935 } 936 937 VM_OBJECT_WLOCK(obj); 938 vm_object_set_flag(obj, OBJ_POSIXSHM); 939 VM_OBJECT_WUNLOCK(obj); 940 shmfd->shm_object = obj; 941 vfs_timestamp(&shmfd->shm_birthtime); 942 shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime = 943 shmfd->shm_birthtime; 944 shmfd->shm_ino = alloc_unr64(&shm_ino_unr); 945 refcount_init(&shmfd->shm_refs, 1); 946 mtx_init(&shmfd->shm_mtx, "shmrl", NULL, MTX_DEF); 947 rangelock_init(&shmfd->shm_rl); 948 #ifdef MAC 949 mac_posixshm_init(shmfd); 950 mac_posixshm_create(ucred, shmfd); 951 #endif 952 953 return (shmfd); 954 } 955 956 struct shmfd * 957 shm_hold(struct shmfd *shmfd) 958 { 959 960 refcount_acquire(&shmfd->shm_refs); 961 return (shmfd); 962 } 963 964 void 965 shm_drop(struct shmfd *shmfd) 966 { 967 vm_object_t obj; 968 969 if (refcount_release(&shmfd->shm_refs)) { 970 #ifdef MAC 971 mac_posixshm_destroy(shmfd); 972 #endif 973 rangelock_destroy(&shmfd->shm_rl); 974 mtx_destroy(&shmfd->shm_mtx); 975 obj = shmfd->shm_object; 976 VM_OBJECT_WLOCK(obj); 977 if (shm_largepage(shmfd)) 978 obj->un_pager.phys.phys_priv = NULL; 979 else 980 obj->un_pager.swp.swp_priv = NULL; 981 VM_OBJECT_WUNLOCK(obj); 982 vm_object_deallocate(obj); 983 free(shmfd, M_SHMFD); 984 } 985 } 986 987 /* 988 * Determine if the credentials have sufficient permissions for a 989 * specified combination of FREAD and FWRITE. 990 */ 991 int 992 shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags) 993 { 994 accmode_t accmode; 995 int error; 996 997 accmode = 0; 998 if (flags & FREAD) 999 accmode |= VREAD; 1000 if (flags & FWRITE) 1001 accmode |= VWRITE; 1002 mtx_lock(&shm_timestamp_lock); 1003 error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid, 1004 accmode, ucred); 1005 mtx_unlock(&shm_timestamp_lock); 1006 return (error); 1007 } 1008 1009 static void 1010 shm_init(void *arg) 1011 { 1012 char name[32]; 1013 int i; 1014 1015 mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF); 1016 sx_init(&shm_dict_lock, "shm dictionary"); 1017 shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash); 1018 new_unrhdr64(&shm_ino_unr, 1); 1019 shm_dev_ino = devfs_alloc_cdp_inode(); 1020 KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized")); 1021 shmfd_pager_type = vm_pager_alloc_dyn_type(&shm_swap_pager_ops, 1022 OBJT_SWAP); 1023 MPASS(shmfd_pager_type != -1); 1024 1025 for (i = 1; i < MAXPAGESIZES; i++) { 1026 if (pagesizes[i] == 0) 1027 break; 1028 #define M (1024 * 1024) 1029 #define G (1024 * M) 1030 if (pagesizes[i] >= G) 1031 snprintf(name, sizeof(name), "%luG", pagesizes[i] / G); 1032 else if (pagesizes[i] >= M) 1033 snprintf(name, sizeof(name), "%luM", pagesizes[i] / M); 1034 else 1035 snprintf(name, sizeof(name), "%lu", pagesizes[i]); 1036 #undef G 1037 #undef M 1038 SYSCTL_ADD_ULONG(NULL, SYSCTL_STATIC_CHILDREN(_vm_largepages), 1039 OID_AUTO, name, CTLFLAG_RD, &count_largepages[i], 1040 "number of non-transient largepages allocated"); 1041 } 1042 } 1043 SYSINIT(shm_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_init, NULL); 1044 1045 /* 1046 * Remove all shared memory objects that belong to a prison. 1047 */ 1048 void 1049 shm_remove_prison(struct prison *pr) 1050 { 1051 struct shm_mapping *shmm, *tshmm; 1052 u_long i; 1053 1054 sx_xlock(&shm_dict_lock); 1055 for (i = 0; i < shm_hash + 1; i++) { 1056 LIST_FOREACH_SAFE(shmm, &shm_dictionary[i], sm_link, tshmm) { 1057 if (shmm->sm_shmfd->shm_object->cred && 1058 shmm->sm_shmfd->shm_object->cred->cr_prison == pr) 1059 shm_doremove(shmm); 1060 } 1061 } 1062 sx_xunlock(&shm_dict_lock); 1063 } 1064 1065 /* 1066 * Dictionary management. We maintain an in-kernel dictionary to map 1067 * paths to shmfd objects. We use the FNV hash on the path to store 1068 * the mappings in a hash table. 1069 */ 1070 static struct shmfd * 1071 shm_lookup(char *path, Fnv32_t fnv) 1072 { 1073 struct shm_mapping *map; 1074 1075 LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 1076 if (map->sm_fnv != fnv) 1077 continue; 1078 if (strcmp(map->sm_path, path) == 0) 1079 return (map->sm_shmfd); 1080 } 1081 1082 return (NULL); 1083 } 1084 1085 static void 1086 shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd) 1087 { 1088 struct shm_mapping *map; 1089 1090 map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK); 1091 map->sm_path = path; 1092 map->sm_fnv = fnv; 1093 map->sm_shmfd = shm_hold(shmfd); 1094 shmfd->shm_path = path; 1095 LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link); 1096 } 1097 1098 static int 1099 shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) 1100 { 1101 struct shm_mapping *map; 1102 int error; 1103 1104 LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 1105 if (map->sm_fnv != fnv) 1106 continue; 1107 if (strcmp(map->sm_path, path) == 0) { 1108 #ifdef MAC 1109 error = mac_posixshm_check_unlink(ucred, map->sm_shmfd); 1110 if (error) 1111 return (error); 1112 #endif 1113 error = shm_access(map->sm_shmfd, ucred, 1114 FREAD | FWRITE); 1115 if (error) 1116 return (error); 1117 shm_doremove(map); 1118 return (0); 1119 } 1120 } 1121 1122 return (ENOENT); 1123 } 1124 1125 static void 1126 shm_doremove(struct shm_mapping *map) 1127 { 1128 map->sm_shmfd->shm_path = NULL; 1129 LIST_REMOVE(map, sm_link); 1130 shm_drop(map->sm_shmfd); 1131 free(map->sm_path, M_SHMFD); 1132 free(map, M_SHMFD); 1133 } 1134 1135 int 1136 kern_shm_open2(struct thread *td, const char *userpath, int flags, mode_t mode, 1137 int shmflags, struct filecaps *fcaps, const char *name __unused, 1138 struct shmfd *shmfd) 1139 { 1140 struct pwddesc *pdp; 1141 struct file *fp; 1142 char *path; 1143 void *rl_cookie; 1144 Fnv32_t fnv; 1145 mode_t cmode; 1146 int error, fd, initial_seals; 1147 bool largepage; 1148 1149 if ((shmflags & ~(SHM_ALLOW_SEALING | SHM_GROW_ON_WRITE | 1150 SHM_LARGEPAGE)) != 0) 1151 return (EINVAL); 1152 1153 initial_seals = F_SEAL_SEAL; 1154 if ((shmflags & SHM_ALLOW_SEALING) != 0) 1155 initial_seals &= ~F_SEAL_SEAL; 1156 1157 AUDIT_ARG_FFLAGS(flags); 1158 AUDIT_ARG_MODE(mode); 1159 1160 if ((flags & O_ACCMODE) != O_RDONLY && (flags & O_ACCMODE) != O_RDWR) 1161 return (EINVAL); 1162 1163 if ((flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC)) != 0) 1164 return (EINVAL); 1165 1166 largepage = (shmflags & SHM_LARGEPAGE) != 0; 1167 if (largepage && !PMAP_HAS_LARGEPAGES) 1168 return (ENOTTY); 1169 1170 /* 1171 * Currently only F_SEAL_SEAL may be set when creating or opening shmfd. 1172 * If the decision is made later to allow additional seals, care must be 1173 * taken below to ensure that the seals are properly set if the shmfd 1174 * already existed -- this currently assumes that only F_SEAL_SEAL can 1175 * be set and doesn't take further precautions to ensure the validity of 1176 * the seals being added with respect to current mappings. 1177 */ 1178 if ((initial_seals & ~F_SEAL_SEAL) != 0) 1179 return (EINVAL); 1180 1181 if (userpath != SHM_ANON) { 1182 error = shm_copyin_path(td, userpath, &path); 1183 if (error != 0) 1184 return (error); 1185 1186 #ifdef CAPABILITY_MODE 1187 /* 1188 * shm_open(2) is only allowed for anonymous objects. 1189 */ 1190 if (CAP_TRACING(td)) 1191 ktrcapfail(CAPFAIL_NAMEI, path); 1192 if (IN_CAPABILITY_MODE(td)) { 1193 error = ECAPMODE; 1194 goto outnofp; 1195 } 1196 #endif 1197 1198 AUDIT_ARG_UPATH1_CANON(path); 1199 } else { 1200 path = NULL; 1201 } 1202 1203 pdp = td->td_proc->p_pd; 1204 cmode = (mode & ~pdp->pd_cmask) & ACCESSPERMS; 1205 1206 /* 1207 * shm_open(2) created shm should always have O_CLOEXEC set, as mandated 1208 * by POSIX. We allow it to be unset here so that an in-kernel 1209 * interface may be written as a thin layer around shm, optionally not 1210 * setting CLOEXEC. For shm_open(2), O_CLOEXEC is set unconditionally 1211 * in sys_shm_open() to keep this implementation compliant. 1212 */ 1213 error = falloc_caps(td, &fp, &fd, flags & O_CLOEXEC, fcaps); 1214 if (error != 0) 1215 goto outnofp; 1216 1217 /* 1218 * A SHM_ANON path pointer creates an anonymous object. We allow other 1219 * parts of the kernel to pre-populate a shmfd and then materialize an 1220 * fd for it here as a means to pass data back up to userland. This 1221 * doesn't really make sense for named shm objects, but it makes plenty 1222 * of sense for anonymous objects. 1223 */ 1224 if (userpath == SHM_ANON) { 1225 if (shmfd != NULL) { 1226 shm_hold(shmfd); 1227 } else { 1228 /* 1229 * A read-only anonymous object is pointless, unless it 1230 * was pre-populated by the kernel with the expectation 1231 * that a shmfd would later be created for userland to 1232 * access it through. 1233 */ 1234 if ((flags & O_ACCMODE) == O_RDONLY) { 1235 error = EINVAL; 1236 goto out; 1237 } 1238 shmfd = shm_alloc(td->td_ucred, cmode, largepage); 1239 if (shmfd == NULL) { 1240 error = ENOMEM; 1241 goto out; 1242 } 1243 1244 shmfd->shm_seals = initial_seals; 1245 shmfd->shm_flags = shmflags; 1246 } 1247 } else { 1248 fnv = fnv_32_str(path, FNV1_32_INIT); 1249 sx_xlock(&shm_dict_lock); 1250 1251 MPASS(shmfd == NULL); 1252 shmfd = shm_lookup(path, fnv); 1253 if (shmfd == NULL) { 1254 /* Object does not yet exist, create it if requested. */ 1255 if (flags & O_CREAT) { 1256 #ifdef MAC 1257 error = mac_posixshm_check_create(td->td_ucred, 1258 path); 1259 if (error == 0) { 1260 #endif 1261 shmfd = shm_alloc(td->td_ucred, cmode, 1262 largepage); 1263 if (shmfd == NULL) { 1264 error = ENOMEM; 1265 } else { 1266 shmfd->shm_seals = 1267 initial_seals; 1268 shmfd->shm_flags = shmflags; 1269 shm_insert(path, fnv, shmfd); 1270 path = NULL; 1271 } 1272 #ifdef MAC 1273 } 1274 #endif 1275 } else { 1276 error = ENOENT; 1277 } 1278 } else { 1279 /* 1280 * Object already exists, obtain a new reference if 1281 * requested and permitted. 1282 */ 1283 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX); 1284 1285 /* 1286 * kern_shm_open() likely shouldn't ever error out on 1287 * trying to set a seal that already exists, unlike 1288 * F_ADD_SEALS. This would break terribly as 1289 * shm_open(2) actually sets F_SEAL_SEAL to maintain 1290 * historical behavior where the underlying file could 1291 * not be sealed. 1292 */ 1293 initial_seals &= ~shmfd->shm_seals; 1294 1295 /* 1296 * initial_seals can't set additional seals if we've 1297 * already been set F_SEAL_SEAL. If F_SEAL_SEAL is set, 1298 * then we've already removed that one from 1299 * initial_seals. This is currently redundant as we 1300 * only allow setting F_SEAL_SEAL at creation time, but 1301 * it's cheap to check and decreases the effort required 1302 * to allow additional seals. 1303 */ 1304 if ((shmfd->shm_seals & F_SEAL_SEAL) != 0 && 1305 initial_seals != 0) 1306 error = EPERM; 1307 else if ((flags & (O_CREAT | O_EXCL)) == 1308 (O_CREAT | O_EXCL)) 1309 error = EEXIST; 1310 else if (shmflags != 0 && shmflags != shmfd->shm_flags) 1311 error = EINVAL; 1312 else { 1313 #ifdef MAC 1314 error = mac_posixshm_check_open(td->td_ucred, 1315 shmfd, FFLAGS(flags & O_ACCMODE)); 1316 if (error == 0) 1317 #endif 1318 error = shm_access(shmfd, td->td_ucred, 1319 FFLAGS(flags & O_ACCMODE)); 1320 } 1321 1322 /* 1323 * Truncate the file back to zero length if 1324 * O_TRUNC was specified and the object was 1325 * opened with read/write. 1326 */ 1327 if (error == 0 && 1328 (flags & (O_ACCMODE | O_TRUNC)) == 1329 (O_RDWR | O_TRUNC)) { 1330 VM_OBJECT_WLOCK(shmfd->shm_object); 1331 #ifdef MAC 1332 error = mac_posixshm_check_truncate( 1333 td->td_ucred, fp->f_cred, shmfd); 1334 if (error == 0) 1335 #endif 1336 error = shm_dotruncate_locked(shmfd, 0, 1337 rl_cookie); 1338 VM_OBJECT_WUNLOCK(shmfd->shm_object); 1339 } 1340 if (error == 0) { 1341 /* 1342 * Currently we only allow F_SEAL_SEAL to be 1343 * set initially. As noted above, this would 1344 * need to be reworked should that change. 1345 */ 1346 shmfd->shm_seals |= initial_seals; 1347 shm_hold(shmfd); 1348 } 1349 shm_rangelock_unlock(shmfd, rl_cookie); 1350 } 1351 sx_xunlock(&shm_dict_lock); 1352 1353 if (error != 0) 1354 goto out; 1355 } 1356 1357 finit(fp, FFLAGS(flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); 1358 1359 td->td_retval[0] = fd; 1360 fdrop(fp, td); 1361 free(path, M_SHMFD); 1362 1363 return (0); 1364 1365 out: 1366 fdclose(td, fp, fd); 1367 fdrop(fp, td); 1368 outnofp: 1369 free(path, M_SHMFD); 1370 1371 return (error); 1372 } 1373 1374 /* System calls. */ 1375 #ifdef COMPAT_FREEBSD12 1376 int 1377 freebsd12_shm_open(struct thread *td, struct freebsd12_shm_open_args *uap) 1378 { 1379 1380 return (kern_shm_open(td, uap->path, uap->flags | O_CLOEXEC, 1381 uap->mode, NULL)); 1382 } 1383 #endif 1384 1385 int 1386 sys_shm_unlink(struct thread *td, struct shm_unlink_args *uap) 1387 { 1388 char *path; 1389 Fnv32_t fnv; 1390 int error; 1391 1392 error = shm_copyin_path(td, uap->path, &path); 1393 if (error != 0) 1394 return (error); 1395 1396 AUDIT_ARG_UPATH1_CANON(path); 1397 fnv = fnv_32_str(path, FNV1_32_INIT); 1398 sx_xlock(&shm_dict_lock); 1399 error = shm_remove(path, fnv, td->td_ucred); 1400 sx_xunlock(&shm_dict_lock); 1401 free(path, M_SHMFD); 1402 1403 return (error); 1404 } 1405 1406 int 1407 sys_shm_rename(struct thread *td, struct shm_rename_args *uap) 1408 { 1409 char *path_from = NULL, *path_to = NULL; 1410 Fnv32_t fnv_from, fnv_to; 1411 struct shmfd *fd_from; 1412 struct shmfd *fd_to; 1413 int error; 1414 int flags; 1415 1416 flags = uap->flags; 1417 AUDIT_ARG_FFLAGS(flags); 1418 1419 /* 1420 * Make sure the user passed only valid flags. 1421 * If you add a new flag, please add a new term here. 1422 */ 1423 if ((flags & ~( 1424 SHM_RENAME_NOREPLACE | 1425 SHM_RENAME_EXCHANGE 1426 )) != 0) { 1427 error = EINVAL; 1428 goto out; 1429 } 1430 1431 /* 1432 * EXCHANGE and NOREPLACE don't quite make sense together. Let's 1433 * force the user to choose one or the other. 1434 */ 1435 if ((flags & SHM_RENAME_NOREPLACE) != 0 && 1436 (flags & SHM_RENAME_EXCHANGE) != 0) { 1437 error = EINVAL; 1438 goto out; 1439 } 1440 1441 /* Renaming to or from anonymous makes no sense */ 1442 if (uap->path_from == SHM_ANON || uap->path_to == SHM_ANON) { 1443 error = EINVAL; 1444 goto out; 1445 } 1446 1447 error = shm_copyin_path(td, uap->path_from, &path_from); 1448 if (error != 0) 1449 goto out; 1450 1451 error = shm_copyin_path(td, uap->path_to, &path_to); 1452 if (error != 0) 1453 goto out; 1454 1455 AUDIT_ARG_UPATH1_CANON(path_from); 1456 AUDIT_ARG_UPATH2_CANON(path_to); 1457 1458 /* Rename with from/to equal is a no-op */ 1459 if (strcmp(path_from, path_to) == 0) 1460 goto out; 1461 1462 fnv_from = fnv_32_str(path_from, FNV1_32_INIT); 1463 fnv_to = fnv_32_str(path_to, FNV1_32_INIT); 1464 1465 sx_xlock(&shm_dict_lock); 1466 1467 fd_from = shm_lookup(path_from, fnv_from); 1468 if (fd_from == NULL) { 1469 error = ENOENT; 1470 goto out_locked; 1471 } 1472 1473 fd_to = shm_lookup(path_to, fnv_to); 1474 if ((flags & SHM_RENAME_NOREPLACE) != 0 && fd_to != NULL) { 1475 error = EEXIST; 1476 goto out_locked; 1477 } 1478 1479 /* 1480 * Unconditionally prevents shm_remove from invalidating the 'from' 1481 * shm's state. 1482 */ 1483 shm_hold(fd_from); 1484 error = shm_remove(path_from, fnv_from, td->td_ucred); 1485 1486 /* 1487 * One of my assumptions failed if ENOENT (e.g. locking didn't 1488 * protect us) 1489 */ 1490 KASSERT(error != ENOENT, ("Our shm disappeared during shm_rename: %s", 1491 path_from)); 1492 if (error != 0) { 1493 shm_drop(fd_from); 1494 goto out_locked; 1495 } 1496 1497 /* 1498 * If we are exchanging, we need to ensure the shm_remove below 1499 * doesn't invalidate the dest shm's state. 1500 */ 1501 if ((flags & SHM_RENAME_EXCHANGE) != 0 && fd_to != NULL) 1502 shm_hold(fd_to); 1503 1504 /* 1505 * NOTE: if path_to is not already in the hash, c'est la vie; 1506 * it simply means we have nothing already at path_to to unlink. 1507 * That is the ENOENT case. 1508 * 1509 * If we somehow don't have access to unlink this guy, but 1510 * did for the shm at path_from, then relink the shm to path_from 1511 * and abort with EACCES. 1512 * 1513 * All other errors: that is weird; let's relink and abort the 1514 * operation. 1515 */ 1516 error = shm_remove(path_to, fnv_to, td->td_ucred); 1517 if (error != 0 && error != ENOENT) { 1518 shm_insert(path_from, fnv_from, fd_from); 1519 shm_drop(fd_from); 1520 /* Don't free path_from now, since the hash references it */ 1521 path_from = NULL; 1522 goto out_locked; 1523 } 1524 1525 error = 0; 1526 1527 shm_insert(path_to, fnv_to, fd_from); 1528 1529 /* Don't free path_to now, since the hash references it */ 1530 path_to = NULL; 1531 1532 /* We kept a ref when we removed, and incremented again in insert */ 1533 shm_drop(fd_from); 1534 KASSERT(fd_from->shm_refs > 0, ("Expected >0 refs; got: %d\n", 1535 fd_from->shm_refs)); 1536 1537 if ((flags & SHM_RENAME_EXCHANGE) != 0 && fd_to != NULL) { 1538 shm_insert(path_from, fnv_from, fd_to); 1539 path_from = NULL; 1540 shm_drop(fd_to); 1541 KASSERT(fd_to->shm_refs > 0, ("Expected >0 refs; got: %d\n", 1542 fd_to->shm_refs)); 1543 } 1544 1545 out_locked: 1546 sx_xunlock(&shm_dict_lock); 1547 1548 out: 1549 free(path_from, M_SHMFD); 1550 free(path_to, M_SHMFD); 1551 return (error); 1552 } 1553 1554 static int 1555 shm_mmap_large(struct shmfd *shmfd, vm_map_t map, vm_offset_t *addr, 1556 vm_size_t size, vm_prot_t prot, vm_prot_t max_prot, int flags, 1557 vm_ooffset_t foff, struct thread *td) 1558 { 1559 struct vmspace *vms; 1560 vm_map_entry_t next_entry, prev_entry; 1561 vm_offset_t align, mask, maxaddr; 1562 int docow, error, rv, try; 1563 bool curmap; 1564 1565 if (shmfd->shm_lp_psind == 0) 1566 return (EINVAL); 1567 1568 /* MAP_PRIVATE is disabled */ 1569 if ((flags & ~(MAP_SHARED | MAP_FIXED | MAP_EXCL | 1570 MAP_NOCORE | MAP_32BIT | MAP_ALIGNMENT_MASK)) != 0) 1571 return (EINVAL); 1572 1573 vms = td->td_proc->p_vmspace; 1574 curmap = map == &vms->vm_map; 1575 if (curmap) { 1576 error = kern_mmap_racct_check(td, map, size); 1577 if (error != 0) 1578 return (error); 1579 } 1580 1581 docow = shmfd->shm_lp_psind << MAP_SPLIT_BOUNDARY_SHIFT; 1582 docow |= MAP_INHERIT_SHARE; 1583 if ((flags & MAP_NOCORE) != 0) 1584 docow |= MAP_DISABLE_COREDUMP; 1585 1586 mask = pagesizes[shmfd->shm_lp_psind] - 1; 1587 if ((foff & mask) != 0) 1588 return (EINVAL); 1589 maxaddr = vm_map_max(map); 1590 if ((flags & MAP_32BIT) != 0 && maxaddr > MAP_32BIT_MAX_ADDR) 1591 maxaddr = MAP_32BIT_MAX_ADDR; 1592 if (size == 0 || (size & mask) != 0 || 1593 (*addr != 0 && ((*addr & mask) != 0 || 1594 *addr + size < *addr || *addr + size > maxaddr))) 1595 return (EINVAL); 1596 1597 align = flags & MAP_ALIGNMENT_MASK; 1598 if (align == 0) { 1599 align = pagesizes[shmfd->shm_lp_psind]; 1600 } else if (align == MAP_ALIGNED_SUPER) { 1601 /* 1602 * MAP_ALIGNED_SUPER is only supported on superpage sizes, 1603 * i.e., [1, VM_NRESERVLEVEL]. shmfd->shm_lp_psind < 1 is 1604 * handled above. 1605 */ 1606 if ( 1607 #if VM_NRESERVLEVEL > 0 1608 shmfd->shm_lp_psind > VM_NRESERVLEVEL 1609 #else 1610 shmfd->shm_lp_psind > 1 1611 #endif 1612 ) 1613 return (EINVAL); 1614 align = pagesizes[shmfd->shm_lp_psind]; 1615 } else { 1616 align >>= MAP_ALIGNMENT_SHIFT; 1617 align = 1ULL << align; 1618 /* Also handles overflow. */ 1619 if (align < pagesizes[shmfd->shm_lp_psind]) 1620 return (EINVAL); 1621 } 1622 1623 vm_map_lock(map); 1624 if ((flags & MAP_FIXED) == 0) { 1625 try = 1; 1626 if (curmap && (*addr == 0 || 1627 (*addr >= round_page((vm_offset_t)vms->vm_taddr) && 1628 *addr < round_page((vm_offset_t)vms->vm_daddr + 1629 lim_max(td, RLIMIT_DATA))))) { 1630 *addr = roundup2((vm_offset_t)vms->vm_daddr + 1631 lim_max(td, RLIMIT_DATA), 1632 pagesizes[shmfd->shm_lp_psind]); 1633 } 1634 again: 1635 rv = vm_map_find_aligned(map, addr, size, maxaddr, align); 1636 if (rv != KERN_SUCCESS) { 1637 if (try == 1) { 1638 try = 2; 1639 *addr = vm_map_min(map); 1640 if ((*addr & mask) != 0) 1641 *addr = (*addr + mask) & mask; 1642 goto again; 1643 } 1644 goto fail1; 1645 } 1646 } else if ((flags & MAP_EXCL) == 0) { 1647 rv = vm_map_delete(map, *addr, *addr + size); 1648 if (rv != KERN_SUCCESS) 1649 goto fail1; 1650 } else { 1651 error = ENOSPC; 1652 if (vm_map_lookup_entry(map, *addr, &prev_entry)) 1653 goto fail; 1654 next_entry = vm_map_entry_succ(prev_entry); 1655 if (next_entry->start < *addr + size) 1656 goto fail; 1657 } 1658 1659 rv = vm_map_insert(map, shmfd->shm_object, foff, *addr, *addr + size, 1660 prot, max_prot, docow); 1661 fail1: 1662 error = vm_mmap_to_errno(rv); 1663 fail: 1664 vm_map_unlock(map); 1665 return (error); 1666 } 1667 1668 static int 1669 shm_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t objsize, 1670 vm_prot_t prot, vm_prot_t max_maxprot, int flags, 1671 vm_ooffset_t foff, struct thread *td) 1672 { 1673 struct shmfd *shmfd; 1674 vm_prot_t maxprot; 1675 int error; 1676 bool writecnt; 1677 void *rl_cookie; 1678 1679 shmfd = fp->f_data; 1680 maxprot = VM_PROT_NONE; 1681 1682 rl_cookie = shm_rangelock_rlock(shmfd, 0, objsize); 1683 /* FREAD should always be set. */ 1684 if ((fp->f_flag & FREAD) != 0) 1685 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ; 1686 1687 /* 1688 * If FWRITE's set, we can allow VM_PROT_WRITE unless it's a shared 1689 * mapping with a write seal applied. Private mappings are always 1690 * writeable. 1691 */ 1692 if ((flags & MAP_SHARED) == 0) { 1693 if ((max_maxprot & VM_PROT_WRITE) != 0) 1694 maxprot |= VM_PROT_WRITE; 1695 writecnt = false; 1696 } else { 1697 if ((fp->f_flag & FWRITE) != 0 && 1698 (shmfd->shm_seals & F_SEAL_WRITE) == 0) 1699 maxprot |= VM_PROT_WRITE; 1700 1701 /* 1702 * Any mappings from a writable descriptor may be upgraded to 1703 * VM_PROT_WRITE with mprotect(2), unless a write-seal was 1704 * applied between the open and subsequent mmap(2). We want to 1705 * reject application of a write seal as long as any such 1706 * mapping exists so that the seal cannot be trivially bypassed. 1707 */ 1708 writecnt = (maxprot & VM_PROT_WRITE) != 0; 1709 if (!writecnt && (prot & VM_PROT_WRITE) != 0) { 1710 error = EACCES; 1711 goto out; 1712 } 1713 } 1714 maxprot &= max_maxprot; 1715 1716 /* See comment in vn_mmap(). */ 1717 if ( 1718 #ifdef _LP64 1719 objsize > OFF_MAX || 1720 #endif 1721 foff > OFF_MAX - objsize) { 1722 error = EINVAL; 1723 goto out; 1724 } 1725 1726 #ifdef MAC 1727 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, flags); 1728 if (error != 0) 1729 goto out; 1730 #endif 1731 1732 mtx_lock(&shm_timestamp_lock); 1733 vfs_timestamp(&shmfd->shm_atime); 1734 mtx_unlock(&shm_timestamp_lock); 1735 vm_object_reference(shmfd->shm_object); 1736 1737 if (shm_largepage(shmfd)) { 1738 writecnt = false; 1739 error = shm_mmap_large(shmfd, map, addr, objsize, prot, 1740 maxprot, flags, foff, td); 1741 } else { 1742 if (writecnt) { 1743 vm_pager_update_writecount(shmfd->shm_object, 0, 1744 objsize); 1745 } 1746 error = vm_mmap_object(map, addr, objsize, prot, maxprot, flags, 1747 shmfd->shm_object, foff, writecnt, td); 1748 } 1749 if (error != 0) { 1750 if (writecnt) 1751 vm_pager_release_writecount(shmfd->shm_object, 0, 1752 objsize); 1753 vm_object_deallocate(shmfd->shm_object); 1754 } 1755 out: 1756 shm_rangelock_unlock(shmfd, rl_cookie); 1757 return (error); 1758 } 1759 1760 static int 1761 shm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 1762 struct thread *td) 1763 { 1764 struct shmfd *shmfd; 1765 int error; 1766 1767 error = 0; 1768 shmfd = fp->f_data; 1769 mtx_lock(&shm_timestamp_lock); 1770 /* 1771 * SUSv4 says that x bits of permission need not be affected. 1772 * Be consistent with our shm_open there. 1773 */ 1774 #ifdef MAC 1775 error = mac_posixshm_check_setmode(active_cred, shmfd, mode); 1776 if (error != 0) 1777 goto out; 1778 #endif 1779 error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid, 1780 VADMIN, active_cred); 1781 if (error != 0) 1782 goto out; 1783 shmfd->shm_mode = mode & ACCESSPERMS; 1784 out: 1785 mtx_unlock(&shm_timestamp_lock); 1786 return (error); 1787 } 1788 1789 static int 1790 shm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 1791 struct thread *td) 1792 { 1793 struct shmfd *shmfd; 1794 int error; 1795 1796 error = 0; 1797 shmfd = fp->f_data; 1798 mtx_lock(&shm_timestamp_lock); 1799 #ifdef MAC 1800 error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid); 1801 if (error != 0) 1802 goto out; 1803 #endif 1804 if (uid == (uid_t)-1) 1805 uid = shmfd->shm_uid; 1806 if (gid == (gid_t)-1) 1807 gid = shmfd->shm_gid; 1808 if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) || 1809 (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) && 1810 (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN))) 1811 goto out; 1812 shmfd->shm_uid = uid; 1813 shmfd->shm_gid = gid; 1814 out: 1815 mtx_unlock(&shm_timestamp_lock); 1816 return (error); 1817 } 1818 1819 /* 1820 * Helper routines to allow the backing object of a shared memory file 1821 * descriptor to be mapped in the kernel. 1822 */ 1823 int 1824 shm_map(struct file *fp, size_t size, off_t offset, void **memp) 1825 { 1826 struct shmfd *shmfd; 1827 vm_offset_t kva, ofs; 1828 vm_object_t obj; 1829 int rv; 1830 1831 if (fp->f_type != DTYPE_SHM) 1832 return (EINVAL); 1833 shmfd = fp->f_data; 1834 obj = shmfd->shm_object; 1835 VM_OBJECT_WLOCK(obj); 1836 /* 1837 * XXXRW: This validation is probably insufficient, and subject to 1838 * sign errors. It should be fixed. 1839 */ 1840 if (offset >= shmfd->shm_size || 1841 offset + size > round_page(shmfd->shm_size)) { 1842 VM_OBJECT_WUNLOCK(obj); 1843 return (EINVAL); 1844 } 1845 1846 shmfd->shm_kmappings++; 1847 vm_object_reference_locked(obj); 1848 VM_OBJECT_WUNLOCK(obj); 1849 1850 /* Map the object into the kernel_map and wire it. */ 1851 kva = vm_map_min(kernel_map); 1852 ofs = offset & PAGE_MASK; 1853 offset = trunc_page(offset); 1854 size = round_page(size + ofs); 1855 rv = vm_map_find(kernel_map, obj, offset, &kva, size, 0, 1856 VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE, 1857 VM_PROT_READ | VM_PROT_WRITE, 0); 1858 if (rv == KERN_SUCCESS) { 1859 rv = vm_map_wire(kernel_map, kva, kva + size, 1860 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 1861 if (rv == KERN_SUCCESS) { 1862 *memp = (void *)(kva + ofs); 1863 return (0); 1864 } 1865 vm_map_remove(kernel_map, kva, kva + size); 1866 } else 1867 vm_object_deallocate(obj); 1868 1869 /* On failure, drop our mapping reference. */ 1870 VM_OBJECT_WLOCK(obj); 1871 shmfd->shm_kmappings--; 1872 VM_OBJECT_WUNLOCK(obj); 1873 1874 return (vm_mmap_to_errno(rv)); 1875 } 1876 1877 /* 1878 * We require the caller to unmap the entire entry. This allows us to 1879 * safely decrement shm_kmappings when a mapping is removed. 1880 */ 1881 int 1882 shm_unmap(struct file *fp, void *mem, size_t size) 1883 { 1884 struct shmfd *shmfd; 1885 vm_map_entry_t entry; 1886 vm_offset_t kva, ofs; 1887 vm_object_t obj; 1888 vm_pindex_t pindex; 1889 vm_prot_t prot; 1890 boolean_t wired; 1891 vm_map_t map; 1892 int rv; 1893 1894 if (fp->f_type != DTYPE_SHM) 1895 return (EINVAL); 1896 shmfd = fp->f_data; 1897 kva = (vm_offset_t)mem; 1898 ofs = kva & PAGE_MASK; 1899 kva = trunc_page(kva); 1900 size = round_page(size + ofs); 1901 map = kernel_map; 1902 rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry, 1903 &obj, &pindex, &prot, &wired); 1904 if (rv != KERN_SUCCESS) 1905 return (EINVAL); 1906 if (entry->start != kva || entry->end != kva + size) { 1907 vm_map_lookup_done(map, entry); 1908 return (EINVAL); 1909 } 1910 vm_map_lookup_done(map, entry); 1911 if (obj != shmfd->shm_object) 1912 return (EINVAL); 1913 vm_map_remove(map, kva, kva + size); 1914 VM_OBJECT_WLOCK(obj); 1915 KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped")); 1916 shmfd->shm_kmappings--; 1917 VM_OBJECT_WUNLOCK(obj); 1918 return (0); 1919 } 1920 1921 static int 1922 shm_fill_kinfo_locked(struct shmfd *shmfd, struct kinfo_file *kif, bool list) 1923 { 1924 const char *path, *pr_path; 1925 size_t pr_pathlen; 1926 bool visible; 1927 1928 sx_assert(&shm_dict_lock, SA_LOCKED); 1929 kif->kf_type = KF_TYPE_SHM; 1930 kif->kf_un.kf_file.kf_file_mode = S_IFREG | shmfd->shm_mode; 1931 kif->kf_un.kf_file.kf_file_size = shmfd->shm_size; 1932 if (shmfd->shm_path != NULL) { 1933 path = shmfd->shm_path; 1934 pr_path = curthread->td_ucred->cr_prison->pr_path; 1935 if (strcmp(pr_path, "/") != 0) { 1936 /* Return the jail-rooted pathname. */ 1937 pr_pathlen = strlen(pr_path); 1938 visible = strncmp(path, pr_path, pr_pathlen) == 0 && 1939 path[pr_pathlen] == '/'; 1940 if (list && !visible) 1941 return (EPERM); 1942 if (visible) 1943 path += pr_pathlen; 1944 } 1945 strlcpy(kif->kf_path, path, sizeof(kif->kf_path)); 1946 } 1947 return (0); 1948 } 1949 1950 static int 1951 shm_fill_kinfo(struct file *fp, struct kinfo_file *kif, 1952 struct filedesc *fdp __unused) 1953 { 1954 int res; 1955 1956 sx_slock(&shm_dict_lock); 1957 res = shm_fill_kinfo_locked(fp->f_data, kif, false); 1958 sx_sunlock(&shm_dict_lock); 1959 return (res); 1960 } 1961 1962 static int 1963 shm_add_seals(struct file *fp, int seals) 1964 { 1965 struct shmfd *shmfd; 1966 void *rl_cookie; 1967 vm_ooffset_t writemappings; 1968 int error, nseals; 1969 1970 error = 0; 1971 shmfd = fp->f_data; 1972 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX); 1973 1974 /* Even already-set seals should result in EPERM. */ 1975 if ((shmfd->shm_seals & F_SEAL_SEAL) != 0) { 1976 error = EPERM; 1977 goto out; 1978 } 1979 nseals = seals & ~shmfd->shm_seals; 1980 if ((nseals & F_SEAL_WRITE) != 0) { 1981 if (shm_largepage(shmfd)) { 1982 error = ENOTSUP; 1983 goto out; 1984 } 1985 1986 /* 1987 * The rangelock above prevents writable mappings from being 1988 * added after we've started applying seals. The RLOCK here 1989 * is to avoid torn reads on ILP32 arches as unmapping/reducing 1990 * writemappings will be done without a rangelock. 1991 */ 1992 VM_OBJECT_RLOCK(shmfd->shm_object); 1993 writemappings = shmfd->shm_object->un_pager.swp.writemappings; 1994 VM_OBJECT_RUNLOCK(shmfd->shm_object); 1995 /* kmappings are also writable */ 1996 if (writemappings > 0) { 1997 error = EBUSY; 1998 goto out; 1999 } 2000 } 2001 shmfd->shm_seals |= nseals; 2002 out: 2003 shm_rangelock_unlock(shmfd, rl_cookie); 2004 return (error); 2005 } 2006 2007 static int 2008 shm_get_seals(struct file *fp, int *seals) 2009 { 2010 struct shmfd *shmfd; 2011 2012 shmfd = fp->f_data; 2013 *seals = shmfd->shm_seals; 2014 return (0); 2015 } 2016 2017 static int 2018 shm_deallocate(struct shmfd *shmfd, off_t *offset, off_t *length, int flags) 2019 { 2020 vm_object_t object; 2021 vm_pindex_t pistart, pi, piend; 2022 vm_ooffset_t off, len; 2023 int startofs, endofs, end; 2024 int error; 2025 2026 off = *offset; 2027 len = *length; 2028 KASSERT(off + len <= (vm_ooffset_t)OFF_MAX, ("off + len overflows")); 2029 if (off + len > shmfd->shm_size) 2030 len = shmfd->shm_size - off; 2031 object = shmfd->shm_object; 2032 startofs = off & PAGE_MASK; 2033 endofs = (off + len) & PAGE_MASK; 2034 pistart = OFF_TO_IDX(off); 2035 piend = OFF_TO_IDX(off + len); 2036 pi = OFF_TO_IDX(off + PAGE_MASK); 2037 error = 0; 2038 2039 /* Handle the case when offset is on or beyond shm size. */ 2040 if ((off_t)len <= 0) { 2041 *length = 0; 2042 return (0); 2043 } 2044 2045 VM_OBJECT_WLOCK(object); 2046 2047 if (startofs != 0) { 2048 end = pistart != piend ? PAGE_SIZE : endofs; 2049 error = shm_partial_page_invalidate(object, pistart, startofs, 2050 end); 2051 if (error) 2052 goto out; 2053 off += end - startofs; 2054 len -= end - startofs; 2055 } 2056 2057 if (pi < piend) { 2058 vm_object_page_remove(object, pi, piend, 0); 2059 off += IDX_TO_OFF(piend - pi); 2060 len -= IDX_TO_OFF(piend - pi); 2061 } 2062 2063 if (endofs != 0 && pistart != piend) { 2064 error = shm_partial_page_invalidate(object, piend, 0, endofs); 2065 if (error) 2066 goto out; 2067 off += endofs; 2068 len -= endofs; 2069 } 2070 2071 out: 2072 VM_OBJECT_WUNLOCK(shmfd->shm_object); 2073 *offset = off; 2074 *length = len; 2075 return (error); 2076 } 2077 2078 static int 2079 shm_fspacectl(struct file *fp, int cmd, off_t *offset, off_t *length, int flags, 2080 struct ucred *active_cred, struct thread *td) 2081 { 2082 void *rl_cookie; 2083 struct shmfd *shmfd; 2084 off_t off, len; 2085 int error; 2086 2087 KASSERT(cmd == SPACECTL_DEALLOC, ("shm_fspacectl: Invalid cmd")); 2088 KASSERT((flags & ~SPACECTL_F_SUPPORTED) == 0, 2089 ("shm_fspacectl: non-zero flags")); 2090 KASSERT(*offset >= 0 && *length > 0 && *length <= OFF_MAX - *offset, 2091 ("shm_fspacectl: offset/length overflow or underflow")); 2092 error = EINVAL; 2093 shmfd = fp->f_data; 2094 off = *offset; 2095 len = *length; 2096 2097 rl_cookie = shm_rangelock_wlock(shmfd, off, off + len); 2098 switch (cmd) { 2099 case SPACECTL_DEALLOC: 2100 if ((shmfd->shm_seals & F_SEAL_WRITE) != 0) { 2101 error = EPERM; 2102 break; 2103 } 2104 error = shm_deallocate(shmfd, &off, &len, flags); 2105 *offset = off; 2106 *length = len; 2107 break; 2108 default: 2109 __assert_unreachable(); 2110 } 2111 shm_rangelock_unlock(shmfd, rl_cookie); 2112 return (error); 2113 } 2114 2115 2116 static int 2117 shm_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td) 2118 { 2119 void *rl_cookie; 2120 struct shmfd *shmfd; 2121 size_t size; 2122 int error; 2123 2124 /* This assumes that the caller already checked for overflow. */ 2125 error = 0; 2126 shmfd = fp->f_data; 2127 size = offset + len; 2128 2129 /* 2130 * Just grab the rangelock for the range that we may be attempting to 2131 * grow, rather than blocking read/write for regions we won't be 2132 * touching while this (potential) resize is in progress. Other 2133 * attempts to resize the shmfd will have to take a write lock from 0 to 2134 * OFF_MAX, so this being potentially beyond the current usable range of 2135 * the shmfd is not necessarily a concern. If other mechanisms are 2136 * added to grow a shmfd, this may need to be re-evaluated. 2137 */ 2138 rl_cookie = shm_rangelock_wlock(shmfd, offset, size); 2139 if (size > shmfd->shm_size) 2140 error = shm_dotruncate_cookie(shmfd, size, rl_cookie); 2141 shm_rangelock_unlock(shmfd, rl_cookie); 2142 /* Translate to posix_fallocate(2) return value as needed. */ 2143 if (error == ENOMEM) 2144 error = ENOSPC; 2145 return (error); 2146 } 2147 2148 static int 2149 sysctl_posix_shm_list(SYSCTL_HANDLER_ARGS) 2150 { 2151 struct shm_mapping *shmm; 2152 struct sbuf sb; 2153 struct kinfo_file kif; 2154 u_long i; 2155 int error, error2; 2156 2157 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_file) * 5, req); 2158 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 2159 error = 0; 2160 sx_slock(&shm_dict_lock); 2161 for (i = 0; i < shm_hash + 1; i++) { 2162 LIST_FOREACH(shmm, &shm_dictionary[i], sm_link) { 2163 error = shm_fill_kinfo_locked(shmm->sm_shmfd, 2164 &kif, true); 2165 if (error == EPERM) { 2166 error = 0; 2167 continue; 2168 } 2169 if (error != 0) 2170 break; 2171 pack_kinfo(&kif); 2172 error = sbuf_bcat(&sb, &kif, kif.kf_structsize) == 0 ? 2173 0 : ENOMEM; 2174 if (error != 0) 2175 break; 2176 } 2177 } 2178 sx_sunlock(&shm_dict_lock); 2179 error2 = sbuf_finish(&sb); 2180 sbuf_delete(&sb); 2181 return (error != 0 ? error : error2); 2182 } 2183 2184 SYSCTL_PROC(_kern_ipc, OID_AUTO, posix_shm_list, 2185 CTLFLAG_RD | CTLFLAG_PRISON | CTLFLAG_MPSAFE | CTLTYPE_OPAQUE, 2186 NULL, 0, sysctl_posix_shm_list, "", 2187 "POSIX SHM list"); 2188 2189 int 2190 kern_shm_open(struct thread *td, const char *path, int flags, mode_t mode, 2191 struct filecaps *caps) 2192 { 2193 2194 return (kern_shm_open2(td, path, flags, mode, 0, caps, NULL, NULL)); 2195 } 2196 2197 /* 2198 * This version of the shm_open() interface leaves CLOEXEC behavior up to the 2199 * caller, and libc will enforce it for the traditional shm_open() call. This 2200 * allows other consumers, like memfd_create(), to opt-in for CLOEXEC. This 2201 * interface also includes a 'name' argument that is currently unused, but could 2202 * potentially be exported later via some interface for debugging purposes. 2203 * From the kernel's perspective, it is optional. Individual consumers like 2204 * memfd_create() may require it in order to be compatible with other systems 2205 * implementing the same function. 2206 */ 2207 int 2208 sys_shm_open2(struct thread *td, struct shm_open2_args *uap) 2209 { 2210 2211 return (kern_shm_open2(td, uap->path, uap->flags, uap->mode, 2212 uap->shmflags, NULL, uap->name, NULL)); 2213 } 2214 2215 int 2216 shm_get_path(struct vm_object *obj, char *path, size_t sz) 2217 { 2218 struct shmfd *shmfd; 2219 int error; 2220 2221 error = 0; 2222 shmfd = NULL; 2223 sx_slock(&shm_dict_lock); 2224 VM_OBJECT_RLOCK(obj); 2225 if ((obj->flags & OBJ_POSIXSHM) == 0) { 2226 error = EINVAL; 2227 } else { 2228 if (obj->type == shmfd_pager_type) 2229 shmfd = obj->un_pager.swp.swp_priv; 2230 else if (obj->type == OBJT_PHYS) 2231 shmfd = obj->un_pager.phys.phys_priv; 2232 if (shmfd == NULL) { 2233 error = ENXIO; 2234 } else { 2235 strlcpy(path, shmfd->shm_path == NULL ? "anon" : 2236 shmfd->shm_path, sz); 2237 } 2238 } 2239 if (error != 0) 2240 path[0] = '\0'; 2241 VM_OBJECT_RUNLOCK(obj); 2242 sx_sunlock(&shm_dict_lock); 2243 return (error); 2244 } 2245