uipc_shm.c revision 228533
1175164Sjhb/*- 2225344Srwatson * Copyright (c) 2006, 2011 Robert N. M. Watson 3175164Sjhb * All rights reserved. 4175164Sjhb * 5175164Sjhb * Redistribution and use in source and binary forms, with or without 6175164Sjhb * modification, are permitted provided that the following conditions 7175164Sjhb * are met: 8175164Sjhb * 1. Redistributions of source code must retain the above copyright 9175164Sjhb * notice, this list of conditions and the following disclaimer. 10175164Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11175164Sjhb * notice, this list of conditions and the following disclaimer in the 12175164Sjhb * documentation and/or other materials provided with the distribution. 13175164Sjhb * 14175164Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15175164Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16175164Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17175164Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18175164Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19175164Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20175164Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21175164Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22175164Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23175164Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24175164Sjhb * SUCH DAMAGE. 25175164Sjhb */ 26175164Sjhb 27175164Sjhb/* 28175164Sjhb * Support for shared swap-backed anonymous memory objects via 29175164Sjhb * shm_open(2) and shm_unlink(2). While most of the implementation is 30175164Sjhb * here, vm_mmap.c contains mapping logic changes. 31175164Sjhb * 32175164Sjhb * TODO: 33175164Sjhb * 34225344Srwatson * (1) Need to export data to a userland tool via a sysctl. Should ipcs(1) 35175164Sjhb * and ipcrm(1) be expanded or should new tools to manage both POSIX 36175164Sjhb * kernel semaphores and POSIX shared memory be written? 37175164Sjhb * 38225344Srwatson * (2) Add support for this file type to fstat(1). 39175164Sjhb * 40225344Srwatson * (3) Resource limits? Does this need its own resource limits or are the 41175164Sjhb * existing limits in mmap(2) sufficient? 42175164Sjhb * 43225344Srwatson * (4) Partial page truncation. vnode_pager_setsize() will zero any parts 44175164Sjhb * of a partially mapped page as a result of ftruncate(2)/truncate(2). 45175164Sjhb * We can do the same (with the same pmap evil), but do we need to 46175164Sjhb * worry about the bits on disk if the page is swapped out or will the 47175164Sjhb * swapper zero the parts of a page that are invalid if the page is 48175164Sjhb * swapped back in for us? 49175164Sjhb */ 50175164Sjhb 51175164Sjhb#include <sys/cdefs.h> 52175164Sjhb__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 228533 2011-12-15 15:17:19Z jhb $"); 53175164Sjhb 54223692Sjonathan#include "opt_capsicum.h" 55223692Sjonathan 56175164Sjhb#include <sys/param.h> 57223692Sjonathan#include <sys/capability.h> 58175164Sjhb#include <sys/fcntl.h> 59175164Sjhb#include <sys/file.h> 60175164Sjhb#include <sys/filedesc.h> 61175164Sjhb#include <sys/fnv_hash.h> 62175164Sjhb#include <sys/kernel.h> 63175164Sjhb#include <sys/lock.h> 64175164Sjhb#include <sys/malloc.h> 65175164Sjhb#include <sys/mman.h> 66175164Sjhb#include <sys/mutex.h> 67224914Skib#include <sys/priv.h> 68175164Sjhb#include <sys/proc.h> 69175164Sjhb#include <sys/refcount.h> 70175164Sjhb#include <sys/resourcevar.h> 71175164Sjhb#include <sys/stat.h> 72175164Sjhb#include <sys/sysctl.h> 73175164Sjhb#include <sys/sysproto.h> 74175164Sjhb#include <sys/systm.h> 75175164Sjhb#include <sys/sx.h> 76175164Sjhb#include <sys/time.h> 77175164Sjhb#include <sys/vnode.h> 78175164Sjhb 79175164Sjhb#include <security/mac/mac_framework.h> 80175164Sjhb 81175164Sjhb#include <vm/vm.h> 82175164Sjhb#include <vm/vm_param.h> 83175164Sjhb#include <vm/pmap.h> 84228533Sjhb#include <vm/vm_extern.h> 85175164Sjhb#include <vm/vm_map.h> 86228509Sjhb#include <vm/vm_kern.h> 87175164Sjhb#include <vm/vm_object.h> 88175164Sjhb#include <vm/vm_page.h> 89175164Sjhb#include <vm/vm_pager.h> 90175164Sjhb#include <vm/swap_pager.h> 91175164Sjhb 92175164Sjhbstruct shm_mapping { 93175164Sjhb char *sm_path; 94175164Sjhb Fnv32_t sm_fnv; 95175164Sjhb struct shmfd *sm_shmfd; 96175164Sjhb LIST_ENTRY(shm_mapping) sm_link; 97175164Sjhb}; 98175164Sjhb 99175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor"); 100175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary; 101175164Sjhbstatic struct sx shm_dict_lock; 102175164Sjhbstatic struct mtx shm_timestamp_lock; 103175164Sjhbstatic u_long shm_hash; 104175164Sjhb 105175164Sjhb#define SHM_HASH(fnv) (&shm_dictionary[(fnv) & shm_hash]) 106175164Sjhb 107175164Sjhbstatic int shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags); 108175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode); 109175164Sjhbstatic void shm_dict_init(void *arg); 110175164Sjhbstatic void shm_drop(struct shmfd *shmfd); 111175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd); 112175164Sjhbstatic void shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd); 113175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv); 114175164Sjhbstatic int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred); 115194766Skibstatic int shm_dotruncate(struct shmfd *shmfd, off_t length); 116175164Sjhb 117175164Sjhbstatic fo_rdwr_t shm_read; 118175164Sjhbstatic fo_rdwr_t shm_write; 119175164Sjhbstatic fo_truncate_t shm_truncate; 120175164Sjhbstatic fo_ioctl_t shm_ioctl; 121175164Sjhbstatic fo_poll_t shm_poll; 122175164Sjhbstatic fo_kqfilter_t shm_kqfilter; 123175164Sjhbstatic fo_stat_t shm_stat; 124175164Sjhbstatic fo_close_t shm_close; 125224914Skibstatic fo_chmod_t shm_chmod; 126224914Skibstatic fo_chown_t shm_chown; 127175164Sjhb 128175164Sjhb/* File descriptor operations. */ 129175164Sjhbstatic struct fileops shm_ops = { 130175164Sjhb .fo_read = shm_read, 131175164Sjhb .fo_write = shm_write, 132175164Sjhb .fo_truncate = shm_truncate, 133175164Sjhb .fo_ioctl = shm_ioctl, 134175164Sjhb .fo_poll = shm_poll, 135175164Sjhb .fo_kqfilter = shm_kqfilter, 136175164Sjhb .fo_stat = shm_stat, 137175164Sjhb .fo_close = shm_close, 138224914Skib .fo_chmod = shm_chmod, 139224914Skib .fo_chown = shm_chown, 140175164Sjhb .fo_flags = DFLAG_PASSABLE 141175164Sjhb}; 142175164Sjhb 143175164SjhbFEATURE(posix_shm, "POSIX shared memory"); 144175164Sjhb 145175164Sjhbstatic int 146175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 147175164Sjhb int flags, struct thread *td) 148175164Sjhb{ 149175164Sjhb 150175164Sjhb return (EOPNOTSUPP); 151175164Sjhb} 152175164Sjhb 153175164Sjhbstatic int 154175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 155175164Sjhb int flags, struct thread *td) 156175164Sjhb{ 157175164Sjhb 158175164Sjhb return (EOPNOTSUPP); 159175164Sjhb} 160175164Sjhb 161175164Sjhbstatic int 162175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred, 163175164Sjhb struct thread *td) 164175164Sjhb{ 165175164Sjhb struct shmfd *shmfd; 166175164Sjhb#ifdef MAC 167175164Sjhb int error; 168175164Sjhb#endif 169175164Sjhb 170175164Sjhb shmfd = fp->f_data; 171175164Sjhb#ifdef MAC 172175164Sjhb error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd); 173175164Sjhb if (error) 174175164Sjhb return (error); 175175164Sjhb#endif 176194766Skib return (shm_dotruncate(shmfd, length)); 177175164Sjhb} 178175164Sjhb 179175164Sjhbstatic int 180175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data, 181175164Sjhb struct ucred *active_cred, struct thread *td) 182175164Sjhb{ 183175164Sjhb 184175164Sjhb return (EOPNOTSUPP); 185175164Sjhb} 186175164Sjhb 187175164Sjhbstatic int 188175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred, 189175164Sjhb struct thread *td) 190175164Sjhb{ 191175164Sjhb 192175164Sjhb return (EOPNOTSUPP); 193175164Sjhb} 194175164Sjhb 195175164Sjhbstatic int 196175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn) 197175164Sjhb{ 198175164Sjhb 199175164Sjhb return (EOPNOTSUPP); 200175164Sjhb} 201175164Sjhb 202175164Sjhbstatic int 203175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, 204175164Sjhb struct thread *td) 205175164Sjhb{ 206175164Sjhb struct shmfd *shmfd; 207175164Sjhb#ifdef MAC 208175164Sjhb int error; 209175164Sjhb#endif 210175164Sjhb 211175164Sjhb shmfd = fp->f_data; 212175164Sjhb 213175164Sjhb#ifdef MAC 214175164Sjhb error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd); 215175164Sjhb if (error) 216175164Sjhb return (error); 217175164Sjhb#endif 218175164Sjhb 219175164Sjhb /* 220175164Sjhb * Attempt to return sanish values for fstat() on a memory file 221175164Sjhb * descriptor. 222175164Sjhb */ 223175164Sjhb bzero(sb, sizeof(*sb)); 224175164Sjhb sb->st_blksize = PAGE_SIZE; 225175164Sjhb sb->st_size = shmfd->shm_size; 226175164Sjhb sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize; 227224914Skib mtx_lock(&shm_timestamp_lock); 228205792Sed sb->st_atim = shmfd->shm_atime; 229205792Sed sb->st_ctim = shmfd->shm_ctime; 230205792Sed sb->st_mtim = shmfd->shm_mtime; 231224914Skib sb->st_birthtim = shmfd->shm_birthtime; 232224914Skib sb->st_mode = S_IFREG | shmfd->shm_mode; /* XXX */ 233175164Sjhb sb->st_uid = shmfd->shm_uid; 234175164Sjhb sb->st_gid = shmfd->shm_gid; 235224914Skib mtx_unlock(&shm_timestamp_lock); 236175164Sjhb 237175164Sjhb return (0); 238175164Sjhb} 239175164Sjhb 240175164Sjhbstatic int 241175164Sjhbshm_close(struct file *fp, struct thread *td) 242175164Sjhb{ 243175164Sjhb struct shmfd *shmfd; 244175164Sjhb 245175164Sjhb shmfd = fp->f_data; 246175164Sjhb fp->f_data = NULL; 247175164Sjhb shm_drop(shmfd); 248175164Sjhb 249175164Sjhb return (0); 250175164Sjhb} 251175164Sjhb 252194766Skibstatic int 253175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length) 254175164Sjhb{ 255175164Sjhb vm_object_t object; 256175164Sjhb vm_page_t m; 257175164Sjhb vm_pindex_t nobjsize; 258194766Skib vm_ooffset_t delta; 259175164Sjhb 260175164Sjhb object = shmfd->shm_object; 261175164Sjhb VM_OBJECT_LOCK(object); 262175164Sjhb if (length == shmfd->shm_size) { 263175164Sjhb VM_OBJECT_UNLOCK(object); 264194766Skib return (0); 265175164Sjhb } 266175164Sjhb nobjsize = OFF_TO_IDX(length + PAGE_MASK); 267175164Sjhb 268175164Sjhb /* Are we shrinking? If so, trim the end. */ 269175164Sjhb if (length < shmfd->shm_size) { 270228509Sjhb /* 271228509Sjhb * Disallow any requests to shrink the size if this 272228509Sjhb * object is mapped into the kernel. 273228509Sjhb */ 274228509Sjhb if (shmfd->shm_kmappings > 0) { 275228509Sjhb VM_OBJECT_UNLOCK(object); 276228509Sjhb return (EBUSY); 277228509Sjhb } 278194766Skib delta = ptoa(object->size - nobjsize); 279194766Skib 280175164Sjhb /* Toss in memory pages. */ 281175164Sjhb if (nobjsize < object->size) 282175164Sjhb vm_object_page_remove(object, nobjsize, object->size, 283223677Salc 0); 284175164Sjhb 285175164Sjhb /* Toss pages from swap. */ 286175164Sjhb if (object->type == OBJT_SWAP) 287194766Skib swap_pager_freespace(object, nobjsize, delta); 288175164Sjhb 289194766Skib /* Free the swap accounted for shm */ 290216128Strasz swap_release_by_cred(delta, object->cred); 291194766Skib object->charge -= delta; 292194766Skib 293175164Sjhb /* 294175164Sjhb * If the last page is partially mapped, then zero out 295175164Sjhb * the garbage at the end of the page. See comments 296193303Salc * in vnode_pager_setsize() for more details. 297175164Sjhb * 298175164Sjhb * XXXJHB: This handles in memory pages, but what about 299175164Sjhb * a page swapped out to disk? 300175164Sjhb */ 301175164Sjhb if ((length & PAGE_MASK) && 302175164Sjhb (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL && 303175164Sjhb m->valid != 0) { 304175164Sjhb int base = (int)length & PAGE_MASK; 305175164Sjhb int size = PAGE_SIZE - base; 306175164Sjhb 307175164Sjhb pmap_zero_page_area(m, base, size); 308193303Salc 309193303Salc /* 310193303Salc * Update the valid bits to reflect the blocks that 311193303Salc * have been zeroed. Some of these valid bits may 312193303Salc * have already been set. 313193303Salc */ 314228156Skib vm_page_set_valid_range(m, base, size); 315193303Salc 316193303Salc /* 317193303Salc * Round "base" to the next block boundary so that the 318193303Salc * dirty bit for a partially zeroed block is not 319193303Salc * cleared. 320193303Salc */ 321193303Salc base = roundup2(base, DEV_BSIZE); 322193303Salc 323193303Salc vm_page_clear_dirty(m, base, PAGE_SIZE - base); 324176075Salc } else if ((length & PAGE_MASK) && 325176075Salc __predict_false(object->cache != NULL)) { 326176075Salc vm_page_cache_free(object, OFF_TO_IDX(length), 327176075Salc nobjsize); 328175164Sjhb } 329194766Skib } else { 330194766Skib 331194766Skib /* Attempt to reserve the swap */ 332194766Skib delta = ptoa(nobjsize - object->size); 333216128Strasz if (!swap_reserve_by_cred(delta, object->cred)) { 334194766Skib VM_OBJECT_UNLOCK(object); 335194766Skib return (ENOMEM); 336194766Skib } 337194766Skib object->charge += delta; 338175164Sjhb } 339175164Sjhb shmfd->shm_size = length; 340175164Sjhb mtx_lock(&shm_timestamp_lock); 341175164Sjhb vfs_timestamp(&shmfd->shm_ctime); 342175164Sjhb shmfd->shm_mtime = shmfd->shm_ctime; 343175164Sjhb mtx_unlock(&shm_timestamp_lock); 344175164Sjhb object->size = nobjsize; 345175164Sjhb VM_OBJECT_UNLOCK(object); 346194766Skib return (0); 347175164Sjhb} 348175164Sjhb 349175164Sjhb/* 350175164Sjhb * shmfd object management including creation and reference counting 351175164Sjhb * routines. 352175164Sjhb */ 353175164Sjhbstatic struct shmfd * 354175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode) 355175164Sjhb{ 356175164Sjhb struct shmfd *shmfd; 357175164Sjhb 358175164Sjhb shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO); 359175164Sjhb shmfd->shm_size = 0; 360175164Sjhb shmfd->shm_uid = ucred->cr_uid; 361175164Sjhb shmfd->shm_gid = ucred->cr_gid; 362175164Sjhb shmfd->shm_mode = mode; 363175164Sjhb shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL, 364194766Skib shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred); 365175164Sjhb KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate")); 366178181Salc VM_OBJECT_LOCK(shmfd->shm_object); 367178181Salc vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING); 368178181Salc vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT); 369178181Salc VM_OBJECT_UNLOCK(shmfd->shm_object); 370175164Sjhb vfs_timestamp(&shmfd->shm_birthtime); 371175164Sjhb shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime = 372175164Sjhb shmfd->shm_birthtime; 373175164Sjhb refcount_init(&shmfd->shm_refs, 1); 374175164Sjhb#ifdef MAC 375175164Sjhb mac_posixshm_init(shmfd); 376175164Sjhb mac_posixshm_create(ucred, shmfd); 377175164Sjhb#endif 378175164Sjhb 379175164Sjhb return (shmfd); 380175164Sjhb} 381175164Sjhb 382175164Sjhbstatic struct shmfd * 383175164Sjhbshm_hold(struct shmfd *shmfd) 384175164Sjhb{ 385175164Sjhb 386175164Sjhb refcount_acquire(&shmfd->shm_refs); 387175164Sjhb return (shmfd); 388175164Sjhb} 389175164Sjhb 390175164Sjhbstatic void 391175164Sjhbshm_drop(struct shmfd *shmfd) 392175164Sjhb{ 393175164Sjhb 394175164Sjhb if (refcount_release(&shmfd->shm_refs)) { 395175164Sjhb#ifdef MAC 396175164Sjhb mac_posixshm_destroy(shmfd); 397175164Sjhb#endif 398175164Sjhb vm_object_deallocate(shmfd->shm_object); 399175164Sjhb free(shmfd, M_SHMFD); 400175164Sjhb } 401175164Sjhb} 402175164Sjhb 403175164Sjhb/* 404175164Sjhb * Determine if the credentials have sufficient permissions for a 405175164Sjhb * specified combination of FREAD and FWRITE. 406175164Sjhb */ 407175164Sjhbstatic int 408175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags) 409175164Sjhb{ 410184413Strasz accmode_t accmode; 411224914Skib int error; 412175164Sjhb 413184413Strasz accmode = 0; 414175164Sjhb if (flags & FREAD) 415184413Strasz accmode |= VREAD; 416175164Sjhb if (flags & FWRITE) 417184413Strasz accmode |= VWRITE; 418224914Skib mtx_lock(&shm_timestamp_lock); 419224914Skib error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid, 420224914Skib accmode, ucred, NULL); 421224914Skib mtx_unlock(&shm_timestamp_lock); 422224914Skib return (error); 423175164Sjhb} 424175164Sjhb 425175164Sjhb/* 426175164Sjhb * Dictionary management. We maintain an in-kernel dictionary to map 427175164Sjhb * paths to shmfd objects. We use the FNV hash on the path to store 428175164Sjhb * the mappings in a hash table. 429175164Sjhb */ 430175164Sjhbstatic void 431175164Sjhbshm_dict_init(void *arg) 432175164Sjhb{ 433175164Sjhb 434175164Sjhb mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF); 435175164Sjhb sx_init(&shm_dict_lock, "shm dictionary"); 436175164Sjhb shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash); 437175164Sjhb} 438175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL); 439175164Sjhb 440175164Sjhbstatic struct shmfd * 441175164Sjhbshm_lookup(char *path, Fnv32_t fnv) 442175164Sjhb{ 443175164Sjhb struct shm_mapping *map; 444175164Sjhb 445175164Sjhb LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 446175164Sjhb if (map->sm_fnv != fnv) 447175164Sjhb continue; 448175164Sjhb if (strcmp(map->sm_path, path) == 0) 449175164Sjhb return (map->sm_shmfd); 450175164Sjhb } 451175164Sjhb 452175164Sjhb return (NULL); 453175164Sjhb} 454175164Sjhb 455175164Sjhbstatic void 456175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd) 457175164Sjhb{ 458175164Sjhb struct shm_mapping *map; 459175164Sjhb 460175164Sjhb map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK); 461175164Sjhb map->sm_path = path; 462175164Sjhb map->sm_fnv = fnv; 463175164Sjhb map->sm_shmfd = shm_hold(shmfd); 464175164Sjhb LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link); 465175164Sjhb} 466175164Sjhb 467175164Sjhbstatic int 468175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) 469175164Sjhb{ 470175164Sjhb struct shm_mapping *map; 471175164Sjhb int error; 472175164Sjhb 473175164Sjhb LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { 474175164Sjhb if (map->sm_fnv != fnv) 475175164Sjhb continue; 476175164Sjhb if (strcmp(map->sm_path, path) == 0) { 477175164Sjhb#ifdef MAC 478175164Sjhb error = mac_posixshm_check_unlink(ucred, map->sm_shmfd); 479175164Sjhb if (error) 480175164Sjhb return (error); 481175164Sjhb#endif 482175164Sjhb error = shm_access(map->sm_shmfd, ucred, 483175164Sjhb FREAD | FWRITE); 484175164Sjhb if (error) 485175164Sjhb return (error); 486175164Sjhb LIST_REMOVE(map, sm_link); 487175164Sjhb shm_drop(map->sm_shmfd); 488175164Sjhb free(map->sm_path, M_SHMFD); 489175164Sjhb free(map, M_SHMFD); 490175164Sjhb return (0); 491175164Sjhb } 492175164Sjhb } 493175164Sjhb 494175164Sjhb return (ENOENT); 495175164Sjhb} 496175164Sjhb 497175164Sjhb/* System calls. */ 498175164Sjhbint 499225617Skmacysys_shm_open(struct thread *td, struct shm_open_args *uap) 500175164Sjhb{ 501175164Sjhb struct filedesc *fdp; 502175164Sjhb struct shmfd *shmfd; 503175164Sjhb struct file *fp; 504175164Sjhb char *path; 505175164Sjhb Fnv32_t fnv; 506175164Sjhb mode_t cmode; 507175164Sjhb int fd, error; 508175164Sjhb 509223692Sjonathan#ifdef CAPABILITY_MODE 510223692Sjonathan /* 511223692Sjonathan * shm_open(2) is only allowed for anonymous objects. 512223692Sjonathan */ 513223692Sjonathan if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON)) 514223692Sjonathan return (ECAPMODE); 515223692Sjonathan#endif 516223692Sjonathan 517175164Sjhb if ((uap->flags & O_ACCMODE) != O_RDONLY && 518175164Sjhb (uap->flags & O_ACCMODE) != O_RDWR) 519175164Sjhb return (EINVAL); 520175164Sjhb 521175164Sjhb if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0) 522175164Sjhb return (EINVAL); 523175164Sjhb 524175164Sjhb fdp = td->td_proc->p_fd; 525175164Sjhb cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS; 526175164Sjhb 527220245Skib error = falloc(td, &fp, &fd, 0); 528175164Sjhb if (error) 529175164Sjhb return (error); 530175164Sjhb 531175164Sjhb /* A SHM_ANON path pointer creates an anonymous object. */ 532175164Sjhb if (uap->path == SHM_ANON) { 533175164Sjhb /* A read-only anonymous object is pointless. */ 534175164Sjhb if ((uap->flags & O_ACCMODE) == O_RDONLY) { 535175164Sjhb fdclose(fdp, fp, fd, td); 536175164Sjhb fdrop(fp, td); 537175164Sjhb return (EINVAL); 538175164Sjhb } 539175164Sjhb shmfd = shm_alloc(td->td_ucred, cmode); 540175164Sjhb } else { 541175164Sjhb path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); 542175164Sjhb error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 543175164Sjhb 544175164Sjhb /* Require paths to start with a '/' character. */ 545175164Sjhb if (error == 0 && path[0] != '/') 546175164Sjhb error = EINVAL; 547175164Sjhb if (error) { 548175164Sjhb fdclose(fdp, fp, fd, td); 549175164Sjhb fdrop(fp, td); 550175164Sjhb free(path, M_SHMFD); 551175164Sjhb return (error); 552175164Sjhb } 553175164Sjhb 554175164Sjhb fnv = fnv_32_str(path, FNV1_32_INIT); 555175164Sjhb sx_xlock(&shm_dict_lock); 556175164Sjhb shmfd = shm_lookup(path, fnv); 557175164Sjhb if (shmfd == NULL) { 558175164Sjhb /* Object does not yet exist, create it if requested. */ 559175164Sjhb if (uap->flags & O_CREAT) { 560225344Srwatson#ifdef MAC 561225344Srwatson error = mac_posixshm_check_create(td->td_ucred, 562225344Srwatson path); 563225344Srwatson if (error == 0) { 564225344Srwatson#endif 565225344Srwatson shmfd = shm_alloc(td->td_ucred, cmode); 566225344Srwatson shm_insert(path, fnv, shmfd); 567225344Srwatson#ifdef MAC 568225344Srwatson } 569225344Srwatson#endif 570175164Sjhb } else { 571175164Sjhb free(path, M_SHMFD); 572175164Sjhb error = ENOENT; 573175164Sjhb } 574175164Sjhb } else { 575175164Sjhb /* 576175164Sjhb * Object already exists, obtain a new 577175164Sjhb * reference if requested and permitted. 578175164Sjhb */ 579175164Sjhb free(path, M_SHMFD); 580175164Sjhb if ((uap->flags & (O_CREAT | O_EXCL)) == 581175164Sjhb (O_CREAT | O_EXCL)) 582175164Sjhb error = EEXIST; 583175164Sjhb else { 584175164Sjhb#ifdef MAC 585175164Sjhb error = mac_posixshm_check_open(td->td_ucred, 586225344Srwatson shmfd, FFLAGS(uap->flags & O_ACCMODE)); 587175164Sjhb if (error == 0) 588175164Sjhb#endif 589175164Sjhb error = shm_access(shmfd, td->td_ucred, 590175164Sjhb FFLAGS(uap->flags & O_ACCMODE)); 591175164Sjhb } 592175164Sjhb 593175164Sjhb /* 594175164Sjhb * Truncate the file back to zero length if 595175164Sjhb * O_TRUNC was specified and the object was 596175164Sjhb * opened with read/write. 597175164Sjhb */ 598175164Sjhb if (error == 0 && 599175164Sjhb (uap->flags & (O_ACCMODE | O_TRUNC)) == 600175164Sjhb (O_RDWR | O_TRUNC)) { 601175164Sjhb#ifdef MAC 602175164Sjhb error = mac_posixshm_check_truncate( 603175164Sjhb td->td_ucred, fp->f_cred, shmfd); 604175164Sjhb if (error == 0) 605175164Sjhb#endif 606175164Sjhb shm_dotruncate(shmfd, 0); 607175164Sjhb } 608175164Sjhb if (error == 0) 609175164Sjhb shm_hold(shmfd); 610175164Sjhb } 611175164Sjhb sx_xunlock(&shm_dict_lock); 612175164Sjhb 613175164Sjhb if (error) { 614175164Sjhb fdclose(fdp, fp, fd, td); 615175164Sjhb fdrop(fp, td); 616175164Sjhb return (error); 617175164Sjhb } 618175164Sjhb } 619175164Sjhb 620175164Sjhb finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); 621175164Sjhb 622175164Sjhb FILEDESC_XLOCK(fdp); 623175164Sjhb if (fdp->fd_ofiles[fd] == fp) 624175164Sjhb fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 625175164Sjhb FILEDESC_XUNLOCK(fdp); 626175164Sjhb td->td_retval[0] = fd; 627175164Sjhb fdrop(fp, td); 628175164Sjhb 629175164Sjhb return (0); 630175164Sjhb} 631175164Sjhb 632175164Sjhbint 633225617Skmacysys_shm_unlink(struct thread *td, struct shm_unlink_args *uap) 634175164Sjhb{ 635175164Sjhb char *path; 636175164Sjhb Fnv32_t fnv; 637175164Sjhb int error; 638175164Sjhb 639175164Sjhb path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 640175164Sjhb error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 641175164Sjhb if (error) { 642175164Sjhb free(path, M_TEMP); 643175164Sjhb return (error); 644175164Sjhb } 645175164Sjhb 646175164Sjhb fnv = fnv_32_str(path, FNV1_32_INIT); 647175164Sjhb sx_xlock(&shm_dict_lock); 648175164Sjhb error = shm_remove(path, fnv, td->td_ucred); 649175164Sjhb sx_xunlock(&shm_dict_lock); 650175164Sjhb free(path, M_TEMP); 651175164Sjhb 652175164Sjhb return (error); 653175164Sjhb} 654175164Sjhb 655175164Sjhb/* 656175164Sjhb * mmap() helper to validate mmap() requests against shm object state 657175164Sjhb * and give mmap() the vm_object to use for the mapping. 658175164Sjhb */ 659175164Sjhbint 660175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff, 661175164Sjhb vm_object_t *obj) 662175164Sjhb{ 663175164Sjhb 664175164Sjhb /* 665175164Sjhb * XXXRW: This validation is probably insufficient, and subject to 666175164Sjhb * sign errors. It should be fixed. 667175164Sjhb */ 668185533Skan if (foff >= shmfd->shm_size || 669185533Skan foff + objsize > round_page(shmfd->shm_size)) 670175164Sjhb return (EINVAL); 671175164Sjhb 672175164Sjhb mtx_lock(&shm_timestamp_lock); 673175164Sjhb vfs_timestamp(&shmfd->shm_atime); 674175164Sjhb mtx_unlock(&shm_timestamp_lock); 675175164Sjhb vm_object_reference(shmfd->shm_object); 676175164Sjhb *obj = shmfd->shm_object; 677175164Sjhb return (0); 678175164Sjhb} 679224914Skib 680224914Skibstatic int 681224914Skibshm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 682224914Skib struct thread *td) 683224914Skib{ 684224914Skib struct shmfd *shmfd; 685224914Skib int error; 686224914Skib 687224914Skib error = 0; 688224914Skib shmfd = fp->f_data; 689224914Skib mtx_lock(&shm_timestamp_lock); 690224914Skib /* 691224914Skib * SUSv4 says that x bits of permission need not be affected. 692224914Skib * Be consistent with our shm_open there. 693224914Skib */ 694224914Skib#ifdef MAC 695224914Skib error = mac_posixshm_check_setmode(active_cred, shmfd, mode); 696224914Skib if (error != 0) 697224914Skib goto out; 698224914Skib#endif 699224914Skib error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, 700224914Skib shmfd->shm_gid, VADMIN, active_cred, NULL); 701224914Skib if (error != 0) 702224914Skib goto out; 703224914Skib shmfd->shm_mode = mode & ACCESSPERMS; 704224914Skibout: 705224914Skib mtx_unlock(&shm_timestamp_lock); 706224914Skib return (error); 707224914Skib} 708224914Skib 709224914Skibstatic int 710224914Skibshm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 711224914Skib struct thread *td) 712224914Skib{ 713224914Skib struct shmfd *shmfd; 714224914Skib int error; 715224914Skib 716224935Skib error = 0; 717224914Skib shmfd = fp->f_data; 718224914Skib mtx_lock(&shm_timestamp_lock); 719224914Skib#ifdef MAC 720224914Skib error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid); 721224914Skib if (error != 0) 722224914Skib goto out; 723224914Skib#endif 724224914Skib if (uid == (uid_t)-1) 725224914Skib uid = shmfd->shm_uid; 726224914Skib if (gid == (gid_t)-1) 727224914Skib gid = shmfd->shm_gid; 728224914Skib if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) || 729224914Skib (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) && 730224914Skib (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0))) 731224914Skib goto out; 732224914Skib shmfd->shm_uid = uid; 733224914Skib shmfd->shm_gid = gid; 734224914Skibout: 735224914Skib mtx_unlock(&shm_timestamp_lock); 736224914Skib return (error); 737224914Skib} 738228509Sjhb 739228509Sjhb/* 740228509Sjhb * Helper routines to allow the backing object of a shared memory file 741228509Sjhb * descriptor to be mapped in the kernel. 742228509Sjhb */ 743228509Sjhbint 744228509Sjhbshm_map(struct file *fp, size_t size, off_t offset, void **memp) 745228509Sjhb{ 746228509Sjhb struct shmfd *shmfd; 747228509Sjhb vm_offset_t kva, ofs; 748228509Sjhb vm_object_t obj; 749228509Sjhb int rv; 750228509Sjhb 751228509Sjhb if (fp->f_type != DTYPE_SHM) 752228509Sjhb return (EINVAL); 753228509Sjhb shmfd = fp->f_data; 754228509Sjhb obj = shmfd->shm_object; 755228509Sjhb VM_OBJECT_LOCK(obj); 756228509Sjhb /* 757228509Sjhb * XXXRW: This validation is probably insufficient, and subject to 758228509Sjhb * sign errors. It should be fixed. 759228509Sjhb */ 760228509Sjhb if (offset >= shmfd->shm_size || 761228509Sjhb offset + size > round_page(shmfd->shm_size)) { 762228509Sjhb VM_OBJECT_UNLOCK(obj); 763228509Sjhb return (EINVAL); 764228509Sjhb } 765228509Sjhb 766228509Sjhb shmfd->shm_kmappings++; 767228509Sjhb vm_object_reference_locked(obj); 768228509Sjhb VM_OBJECT_UNLOCK(obj); 769228509Sjhb 770228509Sjhb /* Map the object into the kernel_map and wire it. */ 771228509Sjhb kva = vm_map_min(kernel_map); 772228509Sjhb ofs = offset & PAGE_MASK; 773228509Sjhb offset = trunc_page(offset); 774228509Sjhb size = round_page(size + ofs); 775228509Sjhb rv = vm_map_find(kernel_map, obj, offset, &kva, size, 776228509Sjhb VMFS_ALIGNED_SPACE, VM_PROT_READ | VM_PROT_WRITE, 777228509Sjhb VM_PROT_READ | VM_PROT_WRITE, 0); 778228509Sjhb if (rv == KERN_SUCCESS) { 779228509Sjhb rv = vm_map_wire(kernel_map, kva, kva + size, 780228509Sjhb VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); 781228509Sjhb if (rv == KERN_SUCCESS) { 782228509Sjhb *memp = (void *)(kva + ofs); 783228509Sjhb return (0); 784228509Sjhb } 785228509Sjhb vm_map_remove(kernel_map, kva, kva + size); 786228509Sjhb } else 787228509Sjhb vm_object_deallocate(obj); 788228509Sjhb 789228509Sjhb /* On failure, drop our mapping reference. */ 790228509Sjhb VM_OBJECT_LOCK(obj); 791228509Sjhb shmfd->shm_kmappings--; 792228509Sjhb VM_OBJECT_UNLOCK(obj); 793228509Sjhb 794228533Sjhb return (vm_mmap_to_errno(rv)); 795228509Sjhb} 796228509Sjhb 797228509Sjhb/* 798228509Sjhb * We require the caller to unmap the entire entry. This allows us to 799228509Sjhb * safely decrement shm_kmappings when a mapping is removed. 800228509Sjhb */ 801228509Sjhbint 802228509Sjhbshm_unmap(struct file *fp, void *mem, size_t size) 803228509Sjhb{ 804228509Sjhb struct shmfd *shmfd; 805228509Sjhb vm_map_entry_t entry; 806228509Sjhb vm_offset_t kva, ofs; 807228509Sjhb vm_object_t obj; 808228509Sjhb vm_pindex_t pindex; 809228509Sjhb vm_prot_t prot; 810228509Sjhb boolean_t wired; 811228509Sjhb vm_map_t map; 812228509Sjhb int rv; 813228509Sjhb 814228509Sjhb if (fp->f_type != DTYPE_SHM) 815228509Sjhb return (EINVAL); 816228509Sjhb shmfd = fp->f_data; 817228509Sjhb kva = (vm_offset_t)mem; 818228509Sjhb ofs = kva & PAGE_MASK; 819228509Sjhb kva = trunc_page(kva); 820228509Sjhb size = round_page(size + ofs); 821228509Sjhb map = kernel_map; 822228509Sjhb rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry, 823228509Sjhb &obj, &pindex, &prot, &wired); 824228509Sjhb if (rv != KERN_SUCCESS) 825228509Sjhb return (EINVAL); 826228509Sjhb if (entry->start != kva || entry->end != kva + size) { 827228509Sjhb vm_map_lookup_done(map, entry); 828228509Sjhb return (EINVAL); 829228509Sjhb } 830228509Sjhb vm_map_lookup_done(map, entry); 831228509Sjhb if (obj != shmfd->shm_object) 832228509Sjhb return (EINVAL); 833228509Sjhb vm_map_remove(map, kva, kva + size); 834228509Sjhb VM_OBJECT_LOCK(obj); 835228509Sjhb KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped")); 836228509Sjhb shmfd->shm_kmappings--; 837228509Sjhb VM_OBJECT_UNLOCK(obj); 838228509Sjhb return (0); 839228509Sjhb} 840