xref: /freebsd/sys/kern/uipc_shm.c (revision a9ac5e142408d3228694aea197d9f77e643ab015)
18e38aeffSJohn Baldwin /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
415bcf785SRobert Watson  * Copyright (c) 2006, 2011, 2016-2017 Robert N. M. Watson
58e38aeffSJohn Baldwin  * All rights reserved.
68e38aeffSJohn Baldwin  *
715bcf785SRobert Watson  * Portions of this software were developed by BAE Systems, the University of
815bcf785SRobert Watson  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
915bcf785SRobert Watson  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
1015bcf785SRobert Watson  * Computing (TC) research program.
1115bcf785SRobert Watson  *
128e38aeffSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
138e38aeffSJohn Baldwin  * modification, are permitted provided that the following conditions
148e38aeffSJohn Baldwin  * are met:
158e38aeffSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
168e38aeffSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
178e38aeffSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
188e38aeffSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
198e38aeffSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
208e38aeffSJohn Baldwin  *
218e38aeffSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
228e38aeffSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238e38aeffSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248e38aeffSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
258e38aeffSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268e38aeffSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278e38aeffSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288e38aeffSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298e38aeffSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308e38aeffSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318e38aeffSJohn Baldwin  * SUCH DAMAGE.
328e38aeffSJohn Baldwin  */
338e38aeffSJohn Baldwin 
348e38aeffSJohn Baldwin /*
358e38aeffSJohn Baldwin  * Support for shared swap-backed anonymous memory objects via
368e38aeffSJohn Baldwin  * shm_open(2) and shm_unlink(2).  While most of the implementation is
378e38aeffSJohn Baldwin  * here, vm_mmap.c contains mapping logic changes.
388e38aeffSJohn Baldwin  *
395c066cd2SKonstantin Belousov  * posixshmcontrol(1) allows users to inspect the state of the memory
405c066cd2SKonstantin Belousov  * objects.  Per-uid swap resource limit controls total amount of
415c066cd2SKonstantin Belousov  * memory that user can consume for anonymous objects, including
425c066cd2SKonstantin Belousov  * shared.
438e38aeffSJohn Baldwin  */
448e38aeffSJohn Baldwin 
458e38aeffSJohn Baldwin #include <sys/cdefs.h>
468e38aeffSJohn Baldwin __FBSDID("$FreeBSD$");
478e38aeffSJohn Baldwin 
4812bc222eSJonathan Anderson #include "opt_capsicum.h"
49551a7895SRui Paulo #include "opt_ktrace.h"
5012bc222eSJonathan Anderson 
518e38aeffSJohn Baldwin #include <sys/param.h>
524a144410SRobert Watson #include <sys/capsicum.h>
53610a2b3cSJohn Baldwin #include <sys/conf.h>
548e38aeffSJohn Baldwin #include <sys/fcntl.h>
558e38aeffSJohn Baldwin #include <sys/file.h>
568e38aeffSJohn Baldwin #include <sys/filedesc.h>
572b64ab22SMark Johnston #include <sys/filio.h>
588e38aeffSJohn Baldwin #include <sys/fnv_hash.h>
598e38aeffSJohn Baldwin #include <sys/kernel.h>
6091898857SMark Johnston #include <sys/limits.h>
61551a7895SRui Paulo #include <sys/uio.h>
62551a7895SRui Paulo #include <sys/signal.h>
63cc7b259aSJamie Gritton #include <sys/jail.h>
64551a7895SRui Paulo #include <sys/ktrace.h>
658e38aeffSJohn Baldwin #include <sys/lock.h>
668e38aeffSJohn Baldwin #include <sys/malloc.h>
678e38aeffSJohn Baldwin #include <sys/mman.h>
688e38aeffSJohn Baldwin #include <sys/mutex.h>
699c00bb91SKonstantin Belousov #include <sys/priv.h>
708e38aeffSJohn Baldwin #include <sys/proc.h>
718e38aeffSJohn Baldwin #include <sys/refcount.h>
728e38aeffSJohn Baldwin #include <sys/resourcevar.h>
7389f6b863SAttilio Rao #include <sys/rwlock.h>
7456d0e33eSKonstantin Belousov #include <sys/sbuf.h>
758e38aeffSJohn Baldwin #include <sys/stat.h>
767ee1b208SEd Schouten #include <sys/syscallsubr.h>
778e38aeffSJohn Baldwin #include <sys/sysctl.h>
788e38aeffSJohn Baldwin #include <sys/sysproto.h>
798e38aeffSJohn Baldwin #include <sys/systm.h>
808e38aeffSJohn Baldwin #include <sys/sx.h>
818e38aeffSJohn Baldwin #include <sys/time.h>
828e38aeffSJohn Baldwin #include <sys/vnode.h>
83940cb0e2SKonstantin Belousov #include <sys/unistd.h>
849696feebSJohn Baldwin #include <sys/user.h>
858e38aeffSJohn Baldwin 
8615bcf785SRobert Watson #include <security/audit/audit.h>
878e38aeffSJohn Baldwin #include <security/mac/mac_framework.h>
888e38aeffSJohn Baldwin 
898e38aeffSJohn Baldwin #include <vm/vm.h>
908e38aeffSJohn Baldwin #include <vm/vm_param.h>
918e38aeffSJohn Baldwin #include <vm/pmap.h>
92338e7cf2SJohn Baldwin #include <vm/vm_extern.h>
938e38aeffSJohn Baldwin #include <vm/vm_map.h>
94fb680e16SJohn Baldwin #include <vm/vm_kern.h>
958e38aeffSJohn Baldwin #include <vm/vm_object.h>
968e38aeffSJohn Baldwin #include <vm/vm_page.h>
972971897dSAlan Cox #include <vm/vm_pageout.h>
988e38aeffSJohn Baldwin #include <vm/vm_pager.h>
998e38aeffSJohn Baldwin #include <vm/swap_pager.h>
1008e38aeffSJohn Baldwin 
1018e38aeffSJohn Baldwin struct shm_mapping {
1028e38aeffSJohn Baldwin 	char		*sm_path;
1038e38aeffSJohn Baldwin 	Fnv32_t		sm_fnv;
1048e38aeffSJohn Baldwin 	struct shmfd	*sm_shmfd;
1058e38aeffSJohn Baldwin 	LIST_ENTRY(shm_mapping) sm_link;
1068e38aeffSJohn Baldwin };
1078e38aeffSJohn Baldwin 
1088e38aeffSJohn Baldwin static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
1098e38aeffSJohn Baldwin static LIST_HEAD(, shm_mapping) *shm_dictionary;
1108e38aeffSJohn Baldwin static struct sx shm_dict_lock;
1118e38aeffSJohn Baldwin static struct mtx shm_timestamp_lock;
1128e38aeffSJohn Baldwin static u_long shm_hash;
1137883ce1fSMateusz Guzik static struct unrhdr64 shm_ino_unr;
114610a2b3cSJohn Baldwin static dev_t shm_dev_ino;
1158e38aeffSJohn Baldwin 
1168e38aeffSJohn Baldwin #define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
1178e38aeffSJohn Baldwin 
1185be725d7SAndreas Tobler static void	shm_init(void *arg);
1198e38aeffSJohn Baldwin static void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
1208e38aeffSJohn Baldwin static struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
1218e38aeffSJohn Baldwin static int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
122af755d3eSKyle Evans static int	shm_dotruncate_locked(struct shmfd *shmfd, off_t length,
123af755d3eSKyle Evans     void *rl_cookie);
1248e38aeffSJohn Baldwin 
1258e38aeffSJohn Baldwin static fo_rdwr_t	shm_read;
1268e38aeffSJohn Baldwin static fo_rdwr_t	shm_write;
1278e38aeffSJohn Baldwin static fo_truncate_t	shm_truncate;
1282b64ab22SMark Johnston static fo_ioctl_t	shm_ioctl;
1298e38aeffSJohn Baldwin static fo_stat_t	shm_stat;
1308e38aeffSJohn Baldwin static fo_close_t	shm_close;
1319c00bb91SKonstantin Belousov static fo_chmod_t	shm_chmod;
1329c00bb91SKonstantin Belousov static fo_chown_t	shm_chown;
133940cb0e2SKonstantin Belousov static fo_seek_t	shm_seek;
1349696feebSJohn Baldwin static fo_fill_kinfo_t	shm_fill_kinfo;
1357077c426SJohn Baldwin static fo_mmap_t	shm_mmap;
136af755d3eSKyle Evans static fo_get_seals_t	shm_get_seals;
137af755d3eSKyle Evans static fo_add_seals_t	shm_add_seals;
1388e38aeffSJohn Baldwin 
1398e38aeffSJohn Baldwin /* File descriptor operations. */
1401bdbd705SKonstantin Belousov struct fileops shm_ops = {
1418e38aeffSJohn Baldwin 	.fo_read = shm_read,
1428e38aeffSJohn Baldwin 	.fo_write = shm_write,
1438e38aeffSJohn Baldwin 	.fo_truncate = shm_truncate,
1442b64ab22SMark Johnston 	.fo_ioctl = shm_ioctl,
1452d69d0dcSJohn Baldwin 	.fo_poll = invfo_poll,
1462d69d0dcSJohn Baldwin 	.fo_kqfilter = invfo_kqfilter,
1478e38aeffSJohn Baldwin 	.fo_stat = shm_stat,
1488e38aeffSJohn Baldwin 	.fo_close = shm_close,
1499c00bb91SKonstantin Belousov 	.fo_chmod = shm_chmod,
1509c00bb91SKonstantin Belousov 	.fo_chown = shm_chown,
151227aaa86SKonstantin Belousov 	.fo_sendfile = vn_sendfile,
152940cb0e2SKonstantin Belousov 	.fo_seek = shm_seek,
1539696feebSJohn Baldwin 	.fo_fill_kinfo = shm_fill_kinfo,
1547077c426SJohn Baldwin 	.fo_mmap = shm_mmap,
155af755d3eSKyle Evans 	.fo_get_seals = shm_get_seals,
156af755d3eSKyle Evans 	.fo_add_seals = shm_add_seals,
157940cb0e2SKonstantin Belousov 	.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1588e38aeffSJohn Baldwin };
1598e38aeffSJohn Baldwin 
1608e38aeffSJohn Baldwin FEATURE(posix_shm, "POSIX shared memory");
1618e38aeffSJohn Baldwin 
1628e38aeffSJohn Baldwin static int
16341cf41fdSKonstantin Belousov uiomove_object_page(vm_object_t obj, size_t len, struct uio *uio)
16441cf41fdSKonstantin Belousov {
16541cf41fdSKonstantin Belousov 	vm_page_t m;
16641cf41fdSKonstantin Belousov 	vm_pindex_t idx;
16741cf41fdSKonstantin Belousov 	size_t tlen;
16841cf41fdSKonstantin Belousov 	int error, offset, rv;
16941cf41fdSKonstantin Belousov 
17041cf41fdSKonstantin Belousov 	idx = OFF_TO_IDX(uio->uio_offset);
17141cf41fdSKonstantin Belousov 	offset = uio->uio_offset & PAGE_MASK;
17241cf41fdSKonstantin Belousov 	tlen = MIN(PAGE_SIZE - offset, len);
17341cf41fdSKonstantin Belousov 
17441cf41fdSKonstantin Belousov 	VM_OBJECT_WLOCK(obj);
17541cf41fdSKonstantin Belousov 
17641cf41fdSKonstantin Belousov 	/*
1776311d7aaSWill Andrews 	 * Read I/O without either a corresponding resident page or swap
1786311d7aaSWill Andrews 	 * page: use zero_region.  This is intended to avoid instantiating
1796311d7aaSWill Andrews 	 * pages on read from a sparse region.
1806311d7aaSWill Andrews 	 */
1816311d7aaSWill Andrews 	if (uio->uio_rw == UIO_READ && vm_page_lookup(obj, idx) == NULL &&
1826311d7aaSWill Andrews 	    !vm_pager_has_page(obj, idx, NULL, NULL)) {
1836311d7aaSWill Andrews 		VM_OBJECT_WUNLOCK(obj);
184b9062c93SKonstantin Belousov 		return (uiomove(__DECONST(void *, zero_region), tlen, uio));
1856311d7aaSWill Andrews 	}
1866311d7aaSWill Andrews 
1876311d7aaSWill Andrews 	/*
18841cf41fdSKonstantin Belousov 	 * Parallel reads of the page content from disk are prevented
18941cf41fdSKonstantin Belousov 	 * by exclusive busy.
19041cf41fdSKonstantin Belousov 	 *
19141cf41fdSKonstantin Belousov 	 * Although the tmpfs vnode lock is held here, it is
19241cf41fdSKonstantin Belousov 	 * nonetheless safe to sleep waiting for a free page.  The
19341cf41fdSKonstantin Belousov 	 * pageout daemon does not need to acquire the tmpfs vnode
19441cf41fdSKonstantin Belousov 	 * lock to page out tobj's pages because tobj is a OBJT_SWAP
19541cf41fdSKonstantin Belousov 	 * type object.
19641cf41fdSKonstantin Belousov 	 */
197c7575748SJeff Roberson 	rv = vm_page_grab_valid(&m, obj, idx,
198c7575748SJeff Roberson 	    VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_NOBUSY);
19941cf41fdSKonstantin Belousov 	if (rv != VM_PAGER_OK) {
20041cf41fdSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
201c7575748SJeff Roberson 		printf("uiomove_object: vm_obj %p idx %jd pager error %d\n",
202c7575748SJeff Roberson 		    obj, idx, rv);
20341cf41fdSKonstantin Belousov 		return (EIO);
20441cf41fdSKonstantin Belousov 	}
20541cf41fdSKonstantin Belousov 	VM_OBJECT_WUNLOCK(obj);
20641cf41fdSKonstantin Belousov 	error = uiomove_fromphys(&m, offset, tlen, uio);
20741cf41fdSKonstantin Belousov 	if (uio->uio_rw == UIO_WRITE && error == 0) {
20841cf41fdSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
20941cf41fdSKonstantin Belousov 		vm_page_dirty(m);
2105d9b4508SKonstantin Belousov 		vm_pager_page_unswapped(m);
21141cf41fdSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
21241cf41fdSKonstantin Belousov 	}
213eeacb3b0SMark Johnston 	vm_page_unwire(m, PQ_ACTIVE);
21441cf41fdSKonstantin Belousov 
21541cf41fdSKonstantin Belousov 	return (error);
21641cf41fdSKonstantin Belousov }
21741cf41fdSKonstantin Belousov 
21841cf41fdSKonstantin Belousov int
21941cf41fdSKonstantin Belousov uiomove_object(vm_object_t obj, off_t obj_size, struct uio *uio)
22041cf41fdSKonstantin Belousov {
22141cf41fdSKonstantin Belousov 	ssize_t resid;
22241cf41fdSKonstantin Belousov 	size_t len;
22341cf41fdSKonstantin Belousov 	int error;
22441cf41fdSKonstantin Belousov 
22541cf41fdSKonstantin Belousov 	error = 0;
22641cf41fdSKonstantin Belousov 	while ((resid = uio->uio_resid) > 0) {
22741cf41fdSKonstantin Belousov 		if (obj_size <= uio->uio_offset)
22841cf41fdSKonstantin Belousov 			break;
22941cf41fdSKonstantin Belousov 		len = MIN(obj_size - uio->uio_offset, resid);
23041cf41fdSKonstantin Belousov 		if (len == 0)
23141cf41fdSKonstantin Belousov 			break;
23241cf41fdSKonstantin Belousov 		error = uiomove_object_page(obj, len, uio);
23341cf41fdSKonstantin Belousov 		if (error != 0 || resid == uio->uio_resid)
23441cf41fdSKonstantin Belousov 			break;
23541cf41fdSKonstantin Belousov 	}
23641cf41fdSKonstantin Belousov 	return (error);
23741cf41fdSKonstantin Belousov }
23841cf41fdSKonstantin Belousov 
23941cf41fdSKonstantin Belousov static int
240940cb0e2SKonstantin Belousov shm_seek(struct file *fp, off_t offset, int whence, struct thread *td)
241940cb0e2SKonstantin Belousov {
242940cb0e2SKonstantin Belousov 	struct shmfd *shmfd;
243940cb0e2SKonstantin Belousov 	off_t foffset;
244940cb0e2SKonstantin Belousov 	int error;
245940cb0e2SKonstantin Belousov 
246940cb0e2SKonstantin Belousov 	shmfd = fp->f_data;
247940cb0e2SKonstantin Belousov 	foffset = foffset_lock(fp, 0);
248940cb0e2SKonstantin Belousov 	error = 0;
249940cb0e2SKonstantin Belousov 	switch (whence) {
250940cb0e2SKonstantin Belousov 	case L_INCR:
251940cb0e2SKonstantin Belousov 		if (foffset < 0 ||
252940cb0e2SKonstantin Belousov 		    (offset > 0 && foffset > OFF_MAX - offset)) {
253940cb0e2SKonstantin Belousov 			error = EOVERFLOW;
254940cb0e2SKonstantin Belousov 			break;
255940cb0e2SKonstantin Belousov 		}
256940cb0e2SKonstantin Belousov 		offset += foffset;
257940cb0e2SKonstantin Belousov 		break;
258940cb0e2SKonstantin Belousov 	case L_XTND:
259940cb0e2SKonstantin Belousov 		if (offset > 0 && shmfd->shm_size > OFF_MAX - offset) {
260940cb0e2SKonstantin Belousov 			error = EOVERFLOW;
261940cb0e2SKonstantin Belousov 			break;
262940cb0e2SKonstantin Belousov 		}
263940cb0e2SKonstantin Belousov 		offset += shmfd->shm_size;
264940cb0e2SKonstantin Belousov 		break;
265940cb0e2SKonstantin Belousov 	case L_SET:
266940cb0e2SKonstantin Belousov 		break;
267940cb0e2SKonstantin Belousov 	default:
268940cb0e2SKonstantin Belousov 		error = EINVAL;
269940cb0e2SKonstantin Belousov 	}
270940cb0e2SKonstantin Belousov 	if (error == 0) {
271940cb0e2SKonstantin Belousov 		if (offset < 0 || offset > shmfd->shm_size)
272940cb0e2SKonstantin Belousov 			error = EINVAL;
273940cb0e2SKonstantin Belousov 		else
2746f2b769cSJohn-Mark Gurney 			td->td_uretoff.tdu_off = offset;
275940cb0e2SKonstantin Belousov 	}
276940cb0e2SKonstantin Belousov 	foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);
277940cb0e2SKonstantin Belousov 	return (error);
278940cb0e2SKonstantin Belousov }
279940cb0e2SKonstantin Belousov 
280940cb0e2SKonstantin Belousov static int
2818e38aeffSJohn Baldwin shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
2828e38aeffSJohn Baldwin     int flags, struct thread *td)
2838e38aeffSJohn Baldwin {
284940cb0e2SKonstantin Belousov 	struct shmfd *shmfd;
285940cb0e2SKonstantin Belousov 	void *rl_cookie;
286940cb0e2SKonstantin Belousov 	int error;
2878e38aeffSJohn Baldwin 
288940cb0e2SKonstantin Belousov 	shmfd = fp->f_data;
289940cb0e2SKonstantin Belousov #ifdef MAC
290940cb0e2SKonstantin Belousov 	error = mac_posixshm_check_read(active_cred, fp->f_cred, shmfd);
291940cb0e2SKonstantin Belousov 	if (error)
292940cb0e2SKonstantin Belousov 		return (error);
293940cb0e2SKonstantin Belousov #endif
2946ea906eeSJilles Tjoelker 	foffset_lock_uio(fp, uio, flags);
2956ea906eeSJilles Tjoelker 	rl_cookie = rangelock_rlock(&shmfd->shm_rl, uio->uio_offset,
2966ea906eeSJilles Tjoelker 	    uio->uio_offset + uio->uio_resid, &shmfd->shm_mtx);
297940cb0e2SKonstantin Belousov 	error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio);
298940cb0e2SKonstantin Belousov 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
299940cb0e2SKonstantin Belousov 	foffset_unlock_uio(fp, uio, flags);
300940cb0e2SKonstantin Belousov 	return (error);
3018e38aeffSJohn Baldwin }
3028e38aeffSJohn Baldwin 
3038e38aeffSJohn Baldwin static int
3048e38aeffSJohn Baldwin shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
3058e38aeffSJohn Baldwin     int flags, struct thread *td)
3068e38aeffSJohn Baldwin {
307940cb0e2SKonstantin Belousov 	struct shmfd *shmfd;
308940cb0e2SKonstantin Belousov 	void *rl_cookie;
309940cb0e2SKonstantin Belousov 	int error;
3108e38aeffSJohn Baldwin 
311940cb0e2SKonstantin Belousov 	shmfd = fp->f_data;
312940cb0e2SKonstantin Belousov #ifdef MAC
313940cb0e2SKonstantin Belousov 	error = mac_posixshm_check_write(active_cred, fp->f_cred, shmfd);
314940cb0e2SKonstantin Belousov 	if (error)
315940cb0e2SKonstantin Belousov 		return (error);
316940cb0e2SKonstantin Belousov #endif
317940cb0e2SKonstantin Belousov 	foffset_lock_uio(fp, uio, flags);
318940cb0e2SKonstantin Belousov 	if ((flags & FOF_OFFSET) == 0) {
319940cb0e2SKonstantin Belousov 		rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
320940cb0e2SKonstantin Belousov 		    &shmfd->shm_mtx);
321940cb0e2SKonstantin Belousov 	} else {
322940cb0e2SKonstantin Belousov 		rl_cookie = rangelock_wlock(&shmfd->shm_rl, uio->uio_offset,
323940cb0e2SKonstantin Belousov 		    uio->uio_offset + uio->uio_resid, &shmfd->shm_mtx);
324940cb0e2SKonstantin Belousov 	}
325af755d3eSKyle Evans 	if ((shmfd->shm_seals & F_SEAL_WRITE) != 0)
326af755d3eSKyle Evans 		error = EPERM;
327af755d3eSKyle Evans 	else
328940cb0e2SKonstantin Belousov 		error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio);
329940cb0e2SKonstantin Belousov 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
330940cb0e2SKonstantin Belousov 	foffset_unlock_uio(fp, uio, flags);
331940cb0e2SKonstantin Belousov 	return (error);
3328e38aeffSJohn Baldwin }
3338e38aeffSJohn Baldwin 
3348e38aeffSJohn Baldwin static int
3358e38aeffSJohn Baldwin shm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3368e38aeffSJohn Baldwin     struct thread *td)
3378e38aeffSJohn Baldwin {
3388e38aeffSJohn Baldwin 	struct shmfd *shmfd;
3398e38aeffSJohn Baldwin #ifdef MAC
3408e38aeffSJohn Baldwin 	int error;
3418e38aeffSJohn Baldwin #endif
3428e38aeffSJohn Baldwin 
3438e38aeffSJohn Baldwin 	shmfd = fp->f_data;
3448e38aeffSJohn Baldwin #ifdef MAC
3458e38aeffSJohn Baldwin 	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
3468e38aeffSJohn Baldwin 	if (error)
3478e38aeffSJohn Baldwin 		return (error);
3488e38aeffSJohn Baldwin #endif
3493364c323SKonstantin Belousov 	return (shm_dotruncate(shmfd, length));
3508e38aeffSJohn Baldwin }
3518e38aeffSJohn Baldwin 
3522b64ab22SMark Johnston int
3532b64ab22SMark Johnston shm_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3542b64ab22SMark Johnston     struct thread *td)
3552b64ab22SMark Johnston {
3562b64ab22SMark Johnston 
3572b64ab22SMark Johnston 	switch (com) {
3582b64ab22SMark Johnston 	case FIONBIO:
3592b64ab22SMark Johnston 	case FIOASYNC:
3602b64ab22SMark Johnston 		/*
3612b64ab22SMark Johnston 		 * Allow fcntl(fd, F_SETFL, O_NONBLOCK) to work,
3622b64ab22SMark Johnston 		 * just like it would on an unlinked regular file
3632b64ab22SMark Johnston 		 */
3642b64ab22SMark Johnston 		return (0);
3652b64ab22SMark Johnston 	default:
3662b64ab22SMark Johnston 		return (ENOTTY);
3672b64ab22SMark Johnston 	}
3682b64ab22SMark Johnston }
3692b64ab22SMark Johnston 
3708e38aeffSJohn Baldwin static int
3718e38aeffSJohn Baldwin shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3728e38aeffSJohn Baldwin     struct thread *td)
3738e38aeffSJohn Baldwin {
3748e38aeffSJohn Baldwin 	struct shmfd *shmfd;
3758e38aeffSJohn Baldwin #ifdef MAC
3768e38aeffSJohn Baldwin 	int error;
3778e38aeffSJohn Baldwin #endif
3788e38aeffSJohn Baldwin 
3798e38aeffSJohn Baldwin 	shmfd = fp->f_data;
3808e38aeffSJohn Baldwin 
3818e38aeffSJohn Baldwin #ifdef MAC
3828e38aeffSJohn Baldwin 	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
3838e38aeffSJohn Baldwin 	if (error)
3848e38aeffSJohn Baldwin 		return (error);
3858e38aeffSJohn Baldwin #endif
3868e38aeffSJohn Baldwin 
3878e38aeffSJohn Baldwin 	/*
3888e38aeffSJohn Baldwin 	 * Attempt to return sanish values for fstat() on a memory file
3898e38aeffSJohn Baldwin 	 * descriptor.
3908e38aeffSJohn Baldwin 	 */
3918e38aeffSJohn Baldwin 	bzero(sb, sizeof(*sb));
3928e38aeffSJohn Baldwin 	sb->st_blksize = PAGE_SIZE;
3938e38aeffSJohn Baldwin 	sb->st_size = shmfd->shm_size;
39455e0987aSPedro F. Giffuni 	sb->st_blocks = howmany(sb->st_size, sb->st_blksize);
3959c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
396510ea843SEd Schouten 	sb->st_atim = shmfd->shm_atime;
397510ea843SEd Schouten 	sb->st_ctim = shmfd->shm_ctime;
398510ea843SEd Schouten 	sb->st_mtim = shmfd->shm_mtime;
399510ea843SEd Schouten 	sb->st_birthtim = shmfd->shm_birthtime;
4009c00bb91SKonstantin Belousov 	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
4018e38aeffSJohn Baldwin 	sb->st_uid = shmfd->shm_uid;
4028e38aeffSJohn Baldwin 	sb->st_gid = shmfd->shm_gid;
4039c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
404610a2b3cSJohn Baldwin 	sb->st_dev = shm_dev_ino;
405610a2b3cSJohn Baldwin 	sb->st_ino = shmfd->shm_ino;
406e4b77548SKonstantin Belousov 	sb->st_nlink = shmfd->shm_object->ref_count;
4078e38aeffSJohn Baldwin 
4088e38aeffSJohn Baldwin 	return (0);
4098e38aeffSJohn Baldwin }
4108e38aeffSJohn Baldwin 
4118e38aeffSJohn Baldwin static int
4128e38aeffSJohn Baldwin shm_close(struct file *fp, struct thread *td)
4138e38aeffSJohn Baldwin {
4148e38aeffSJohn Baldwin 	struct shmfd *shmfd;
4158e38aeffSJohn Baldwin 
4168e38aeffSJohn Baldwin 	shmfd = fp->f_data;
4178e38aeffSJohn Baldwin 	fp->f_data = NULL;
4188e38aeffSJohn Baldwin 	shm_drop(shmfd);
4198e38aeffSJohn Baldwin 
4208e38aeffSJohn Baldwin 	return (0);
4218e38aeffSJohn Baldwin }
4228e38aeffSJohn Baldwin 
423af755d3eSKyle Evans static int
424af755d3eSKyle Evans shm_dotruncate_locked(struct shmfd *shmfd, off_t length, void *rl_cookie)
4258e38aeffSJohn Baldwin {
4268e38aeffSJohn Baldwin 	vm_object_t object;
427093c7f39SGleb Smirnoff 	vm_page_t m;
4282971897dSAlan Cox 	vm_pindex_t idx, nobjsize;
4293364c323SKonstantin Belousov 	vm_ooffset_t delta;
4302971897dSAlan Cox 	int base, rv;
4318e38aeffSJohn Baldwin 
4322a016de1SAlan Cox 	KASSERT(length >= 0, ("shm_dotruncate: length < 0"));
4338e38aeffSJohn Baldwin 	object = shmfd->shm_object;
434af755d3eSKyle Evans 	VM_OBJECT_ASSERT_WLOCKED(object);
435af755d3eSKyle Evans 	rangelock_cookie_assert(rl_cookie, RA_WLOCKED);
436af755d3eSKyle Evans 	if (length == shmfd->shm_size)
4373364c323SKonstantin Belousov 		return (0);
4388e38aeffSJohn Baldwin 	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
4398e38aeffSJohn Baldwin 
4408e38aeffSJohn Baldwin 	/* Are we shrinking?  If so, trim the end. */
4418e38aeffSJohn Baldwin 	if (length < shmfd->shm_size) {
442af755d3eSKyle Evans 		if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0)
443af755d3eSKyle Evans 			return (EPERM);
444af755d3eSKyle Evans 
445fb680e16SJohn Baldwin 		/*
446fb680e16SJohn Baldwin 		 * Disallow any requests to shrink the size if this
447fb680e16SJohn Baldwin 		 * object is mapped into the kernel.
448fb680e16SJohn Baldwin 		 */
449af755d3eSKyle Evans 		if (shmfd->shm_kmappings > 0)
450fb680e16SJohn Baldwin 			return (EBUSY);
4512971897dSAlan Cox 
4522971897dSAlan Cox 		/*
4532971897dSAlan Cox 		 * Zero the truncated part of the last page.
4542971897dSAlan Cox 		 */
4552971897dSAlan Cox 		base = length & PAGE_MASK;
4562971897dSAlan Cox 		if (base != 0) {
4572971897dSAlan Cox 			idx = OFF_TO_IDX(length);
4582971897dSAlan Cox retry:
4592971897dSAlan Cox 			m = vm_page_lookup(object, idx);
4602971897dSAlan Cox 			if (m != NULL) {
461c7aebda8SAttilio Rao 				if (vm_page_sleep_if_busy(m, "shmtrc"))
4622971897dSAlan Cox 					goto retry;
4632971897dSAlan Cox 			} else if (vm_pager_has_page(object, idx, NULL, NULL)) {
4648d6fbbb8SJeff Roberson 				m = vm_page_alloc(object, idx,
4658d6fbbb8SJeff Roberson 				    VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL);
4668d6fbbb8SJeff Roberson 				if (m == NULL)
4672971897dSAlan Cox 					goto retry;
4687667839aSAlan Cox 				rv = vm_pager_get_pages(object, &m, 1, NULL,
4697667839aSAlan Cox 				    NULL);
4702971897dSAlan Cox 				if (rv == VM_PAGER_OK) {
4712d612d2dSAlan Cox 					/*
4722d612d2dSAlan Cox 					 * Since the page was not resident,
4732d612d2dSAlan Cox 					 * and therefore not recently
4742d612d2dSAlan Cox 					 * accessed, immediately enqueue it
4752d612d2dSAlan Cox 					 * for asynchronous laundering.  The
4762d612d2dSAlan Cox 					 * current operation is not regarded
4772d612d2dSAlan Cox 					 * as an access.
4782d612d2dSAlan Cox 					 */
4792d612d2dSAlan Cox 					vm_page_launder(m);
480c7aebda8SAttilio Rao 					vm_page_xunbusy(m);
4812971897dSAlan Cox 				} else {
4822971897dSAlan Cox 					vm_page_free(m);
48389f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
4842971897dSAlan Cox 					return (EIO);
4852971897dSAlan Cox 				}
4862971897dSAlan Cox 			}
4872971897dSAlan Cox 			if (m != NULL) {
4882971897dSAlan Cox 				pmap_zero_page_area(m, base, PAGE_SIZE - base);
4892971897dSAlan Cox 				KASSERT(m->valid == VM_PAGE_BITS_ALL,
4902971897dSAlan Cox 				    ("shm_dotruncate: page %p is invalid", m));
4912971897dSAlan Cox 				vm_page_dirty(m);
4922971897dSAlan Cox 				vm_pager_page_unswapped(m);
4932971897dSAlan Cox 			}
4942971897dSAlan Cox 		}
4952a016de1SAlan Cox 		delta = IDX_TO_OFF(object->size - nobjsize);
4963364c323SKonstantin Belousov 
4978e38aeffSJohn Baldwin 		/* Toss in memory pages. */
4988e38aeffSJohn Baldwin 		if (nobjsize < object->size)
4998e38aeffSJohn Baldwin 			vm_object_page_remove(object, nobjsize, object->size,
5006bbee8e2SAlan Cox 			    0);
5018e38aeffSJohn Baldwin 
5028e38aeffSJohn Baldwin 		/* Toss pages from swap. */
5038e38aeffSJohn Baldwin 		if (object->type == OBJT_SWAP)
5043364c323SKonstantin Belousov 			swap_pager_freespace(object, nobjsize, delta);
5053364c323SKonstantin Belousov 
5063364c323SKonstantin Belousov 		/* Free the swap accounted for shm */
507ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(delta, object->cred);
5083364c323SKonstantin Belousov 		object->charge -= delta;
5093364c323SKonstantin Belousov 	} else {
510af755d3eSKyle Evans 		if ((shmfd->shm_seals & F_SEAL_GROW) != 0)
511af755d3eSKyle Evans 			return (EPERM);
512af755d3eSKyle Evans 
5132a016de1SAlan Cox 		/* Try to reserve additional swap space. */
5142a016de1SAlan Cox 		delta = IDX_TO_OFF(nobjsize - object->size);
515af755d3eSKyle Evans 		if (!swap_reserve_by_cred(delta, object->cred))
5163364c323SKonstantin Belousov 			return (ENOMEM);
5173364c323SKonstantin Belousov 		object->charge += delta;
5188e38aeffSJohn Baldwin 	}
5198e38aeffSJohn Baldwin 	shmfd->shm_size = length;
5208e38aeffSJohn Baldwin 	mtx_lock(&shm_timestamp_lock);
5218e38aeffSJohn Baldwin 	vfs_timestamp(&shmfd->shm_ctime);
5228e38aeffSJohn Baldwin 	shmfd->shm_mtime = shmfd->shm_ctime;
5238e38aeffSJohn Baldwin 	mtx_unlock(&shm_timestamp_lock);
5248e38aeffSJohn Baldwin 	object->size = nobjsize;
5253364c323SKonstantin Belousov 	return (0);
5268e38aeffSJohn Baldwin }
5278e38aeffSJohn Baldwin 
528af755d3eSKyle Evans int
529af755d3eSKyle Evans shm_dotruncate(struct shmfd *shmfd, off_t length)
530af755d3eSKyle Evans {
531af755d3eSKyle Evans 	void *rl_cookie;
532af755d3eSKyle Evans 	int error;
533af755d3eSKyle Evans 
534af755d3eSKyle Evans 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
535af755d3eSKyle Evans 	    &shmfd->shm_mtx);
536af755d3eSKyle Evans 	VM_OBJECT_WLOCK(shmfd->shm_object);
537af755d3eSKyle Evans 	error = shm_dotruncate_locked(shmfd, length, rl_cookie);
538af755d3eSKyle Evans 	VM_OBJECT_WUNLOCK(shmfd->shm_object);
539af755d3eSKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
540af755d3eSKyle Evans 	return (error);
541af755d3eSKyle Evans }
542af755d3eSKyle Evans 
5438e38aeffSJohn Baldwin /*
5448e38aeffSJohn Baldwin  * shmfd object management including creation and reference counting
5458e38aeffSJohn Baldwin  * routines.
5468e38aeffSJohn Baldwin  */
5471bdbd705SKonstantin Belousov struct shmfd *
5488e38aeffSJohn Baldwin shm_alloc(struct ucred *ucred, mode_t mode)
5498e38aeffSJohn Baldwin {
5508e38aeffSJohn Baldwin 	struct shmfd *shmfd;
5518e38aeffSJohn Baldwin 
5528e38aeffSJohn Baldwin 	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
5538e38aeffSJohn Baldwin 	shmfd->shm_size = 0;
5548e38aeffSJohn Baldwin 	shmfd->shm_uid = ucred->cr_uid;
5558e38aeffSJohn Baldwin 	shmfd->shm_gid = ucred->cr_gid;
5568e38aeffSJohn Baldwin 	shmfd->shm_mode = mode;
55732287ea7SKyle Evans 	shmfd->shm_object = vm_pager_allocate(OBJT_SWAP, NULL,
5583364c323SKonstantin Belousov 	    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
5598e38aeffSJohn Baldwin 	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
560f4c6aea3SAlan Cox 	shmfd->shm_object->pg_color = 0;
56189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(shmfd->shm_object);
562e384d8a8SAlan Cox 	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
563f4c6aea3SAlan Cox 	vm_object_set_flag(shmfd->shm_object, OBJ_COLORED | OBJ_NOSPLIT);
56489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(shmfd->shm_object);
5658e38aeffSJohn Baldwin 	vfs_timestamp(&shmfd->shm_birthtime);
5668e38aeffSJohn Baldwin 	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
5678e38aeffSJohn Baldwin 	    shmfd->shm_birthtime;
5687883ce1fSMateusz Guzik 	shmfd->shm_ino = alloc_unr64(&shm_ino_unr);
5698e38aeffSJohn Baldwin 	refcount_init(&shmfd->shm_refs, 1);
570940cb0e2SKonstantin Belousov 	mtx_init(&shmfd->shm_mtx, "shmrl", NULL, MTX_DEF);
571940cb0e2SKonstantin Belousov 	rangelock_init(&shmfd->shm_rl);
5728e38aeffSJohn Baldwin #ifdef MAC
5738e38aeffSJohn Baldwin 	mac_posixshm_init(shmfd);
5748e38aeffSJohn Baldwin 	mac_posixshm_create(ucred, shmfd);
5758e38aeffSJohn Baldwin #endif
5768e38aeffSJohn Baldwin 
5778e38aeffSJohn Baldwin 	return (shmfd);
5788e38aeffSJohn Baldwin }
5798e38aeffSJohn Baldwin 
5801bdbd705SKonstantin Belousov struct shmfd *
5818e38aeffSJohn Baldwin shm_hold(struct shmfd *shmfd)
5828e38aeffSJohn Baldwin {
5838e38aeffSJohn Baldwin 
5848e38aeffSJohn Baldwin 	refcount_acquire(&shmfd->shm_refs);
5858e38aeffSJohn Baldwin 	return (shmfd);
5868e38aeffSJohn Baldwin }
5878e38aeffSJohn Baldwin 
5881bdbd705SKonstantin Belousov void
5898e38aeffSJohn Baldwin shm_drop(struct shmfd *shmfd)
5908e38aeffSJohn Baldwin {
5918e38aeffSJohn Baldwin 
5928e38aeffSJohn Baldwin 	if (refcount_release(&shmfd->shm_refs)) {
5938e38aeffSJohn Baldwin #ifdef MAC
5948e38aeffSJohn Baldwin 		mac_posixshm_destroy(shmfd);
5958e38aeffSJohn Baldwin #endif
596940cb0e2SKonstantin Belousov 		rangelock_destroy(&shmfd->shm_rl);
597940cb0e2SKonstantin Belousov 		mtx_destroy(&shmfd->shm_mtx);
5988e38aeffSJohn Baldwin 		vm_object_deallocate(shmfd->shm_object);
5998e38aeffSJohn Baldwin 		free(shmfd, M_SHMFD);
6008e38aeffSJohn Baldwin 	}
6018e38aeffSJohn Baldwin }
6028e38aeffSJohn Baldwin 
6038e38aeffSJohn Baldwin /*
6048e38aeffSJohn Baldwin  * Determine if the credentials have sufficient permissions for a
6058e38aeffSJohn Baldwin  * specified combination of FREAD and FWRITE.
6068e38aeffSJohn Baldwin  */
6071bdbd705SKonstantin Belousov int
6088e38aeffSJohn Baldwin shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
6098e38aeffSJohn Baldwin {
61015bc6b2bSEdward Tomasz Napierala 	accmode_t accmode;
6119c00bb91SKonstantin Belousov 	int error;
6128e38aeffSJohn Baldwin 
61315bc6b2bSEdward Tomasz Napierala 	accmode = 0;
6148e38aeffSJohn Baldwin 	if (flags & FREAD)
61515bc6b2bSEdward Tomasz Napierala 		accmode |= VREAD;
6168e38aeffSJohn Baldwin 	if (flags & FWRITE)
61715bc6b2bSEdward Tomasz Napierala 		accmode |= VWRITE;
6189c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
6199c00bb91SKonstantin Belousov 	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
6209c00bb91SKonstantin Belousov 	    accmode, ucred, NULL);
6219c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
6229c00bb91SKonstantin Belousov 	return (error);
6238e38aeffSJohn Baldwin }
6248e38aeffSJohn Baldwin 
6258e38aeffSJohn Baldwin /*
6268e38aeffSJohn Baldwin  * Dictionary management.  We maintain an in-kernel dictionary to map
6278e38aeffSJohn Baldwin  * paths to shmfd objects.  We use the FNV hash on the path to store
6288e38aeffSJohn Baldwin  * the mappings in a hash table.
6298e38aeffSJohn Baldwin  */
6308e38aeffSJohn Baldwin static void
631610a2b3cSJohn Baldwin shm_init(void *arg)
6328e38aeffSJohn Baldwin {
6338e38aeffSJohn Baldwin 
6348e38aeffSJohn Baldwin 	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
6358e38aeffSJohn Baldwin 	sx_init(&shm_dict_lock, "shm dictionary");
6368e38aeffSJohn Baldwin 	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
6377883ce1fSMateusz Guzik 	new_unrhdr64(&shm_ino_unr, 1);
638610a2b3cSJohn Baldwin 	shm_dev_ino = devfs_alloc_cdp_inode();
639610a2b3cSJohn Baldwin 	KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized"));
6408e38aeffSJohn Baldwin }
641610a2b3cSJohn Baldwin SYSINIT(shm_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_init, NULL);
6428e38aeffSJohn Baldwin 
6438e38aeffSJohn Baldwin static struct shmfd *
6448e38aeffSJohn Baldwin shm_lookup(char *path, Fnv32_t fnv)
6458e38aeffSJohn Baldwin {
6468e38aeffSJohn Baldwin 	struct shm_mapping *map;
6478e38aeffSJohn Baldwin 
6488e38aeffSJohn Baldwin 	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
6498e38aeffSJohn Baldwin 		if (map->sm_fnv != fnv)
6508e38aeffSJohn Baldwin 			continue;
6518e38aeffSJohn Baldwin 		if (strcmp(map->sm_path, path) == 0)
6528e38aeffSJohn Baldwin 			return (map->sm_shmfd);
6538e38aeffSJohn Baldwin 	}
6548e38aeffSJohn Baldwin 
6558e38aeffSJohn Baldwin 	return (NULL);
6568e38aeffSJohn Baldwin }
6578e38aeffSJohn Baldwin 
6588e38aeffSJohn Baldwin static void
6598e38aeffSJohn Baldwin shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
6608e38aeffSJohn Baldwin {
6618e38aeffSJohn Baldwin 	struct shm_mapping *map;
6628e38aeffSJohn Baldwin 
6638e38aeffSJohn Baldwin 	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
6648e38aeffSJohn Baldwin 	map->sm_path = path;
6658e38aeffSJohn Baldwin 	map->sm_fnv = fnv;
6668e38aeffSJohn Baldwin 	map->sm_shmfd = shm_hold(shmfd);
667e506e182SJohn Baldwin 	shmfd->shm_path = path;
6688e38aeffSJohn Baldwin 	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
6698e38aeffSJohn Baldwin }
6708e38aeffSJohn Baldwin 
6718e38aeffSJohn Baldwin static int
6728e38aeffSJohn Baldwin shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
6738e38aeffSJohn Baldwin {
6748e38aeffSJohn Baldwin 	struct shm_mapping *map;
6758e38aeffSJohn Baldwin 	int error;
6768e38aeffSJohn Baldwin 
6778e38aeffSJohn Baldwin 	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
6788e38aeffSJohn Baldwin 		if (map->sm_fnv != fnv)
6798e38aeffSJohn Baldwin 			continue;
6808e38aeffSJohn Baldwin 		if (strcmp(map->sm_path, path) == 0) {
6818e38aeffSJohn Baldwin #ifdef MAC
6828e38aeffSJohn Baldwin 			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
6838e38aeffSJohn Baldwin 			if (error)
6848e38aeffSJohn Baldwin 				return (error);
6858e38aeffSJohn Baldwin #endif
6868e38aeffSJohn Baldwin 			error = shm_access(map->sm_shmfd, ucred,
6878e38aeffSJohn Baldwin 			    FREAD | FWRITE);
6888e38aeffSJohn Baldwin 			if (error)
6898e38aeffSJohn Baldwin 				return (error);
690e506e182SJohn Baldwin 			map->sm_shmfd->shm_path = NULL;
6918e38aeffSJohn Baldwin 			LIST_REMOVE(map, sm_link);
6928e38aeffSJohn Baldwin 			shm_drop(map->sm_shmfd);
6938e38aeffSJohn Baldwin 			free(map->sm_path, M_SHMFD);
6948e38aeffSJohn Baldwin 			free(map, M_SHMFD);
6958e38aeffSJohn Baldwin 			return (0);
6968e38aeffSJohn Baldwin 		}
6978e38aeffSJohn Baldwin 	}
6988e38aeffSJohn Baldwin 
6998e38aeffSJohn Baldwin 	return (ENOENT);
7008e38aeffSJohn Baldwin }
7018e38aeffSJohn Baldwin 
7028e38aeffSJohn Baldwin int
7037ee1b208SEd Schouten kern_shm_open(struct thread *td, const char *userpath, int flags, mode_t mode,
7040cd95859SKyle Evans     struct filecaps *fcaps, int initial_seals)
7058e38aeffSJohn Baldwin {
7068e38aeffSJohn Baldwin 	struct filedesc *fdp;
7078e38aeffSJohn Baldwin 	struct shmfd *shmfd;
7088e38aeffSJohn Baldwin 	struct file *fp;
7098e38aeffSJohn Baldwin 	char *path;
710cc7b259aSJamie Gritton 	const char *pr_path;
7110cd95859SKyle Evans 	void *rl_cookie;
712cc7b259aSJamie Gritton 	size_t pr_pathlen;
7138e38aeffSJohn Baldwin 	Fnv32_t fnv;
7148e38aeffSJohn Baldwin 	mode_t cmode;
7158e38aeffSJohn Baldwin 	int fd, error;
7168e38aeffSJohn Baldwin 
71712bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
71812bc222eSJonathan Anderson 	/*
71912bc222eSJonathan Anderson 	 * shm_open(2) is only allowed for anonymous objects.
72012bc222eSJonathan Anderson 	 */
7217ee1b208SEd Schouten 	if (IN_CAPABILITY_MODE(td) && (userpath != SHM_ANON))
72212bc222eSJonathan Anderson 		return (ECAPMODE);
72312bc222eSJonathan Anderson #endif
72412bc222eSJonathan Anderson 
72515bcf785SRobert Watson 	AUDIT_ARG_FFLAGS(flags);
72615bcf785SRobert Watson 	AUDIT_ARG_MODE(mode);
72715bcf785SRobert Watson 
7287ee1b208SEd Schouten 	if ((flags & O_ACCMODE) != O_RDONLY && (flags & O_ACCMODE) != O_RDWR)
7298e38aeffSJohn Baldwin 		return (EINVAL);
7308e38aeffSJohn Baldwin 
7317ee1b208SEd Schouten 	if ((flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC)) != 0)
7328e38aeffSJohn Baldwin 		return (EINVAL);
7338e38aeffSJohn Baldwin 
7340cd95859SKyle Evans 	/*
7350cd95859SKyle Evans 	 * Currently only F_SEAL_SEAL may be set when creating or opening shmfd.
7360cd95859SKyle Evans 	 * If the decision is made later to allow additional seals, care must be
7370cd95859SKyle Evans 	 * taken below to ensure that the seals are properly set if the shmfd
7380cd95859SKyle Evans 	 * already existed -- this currently assumes that only F_SEAL_SEAL can
7390cd95859SKyle Evans 	 * be set and doesn't take further precautions to ensure the validity of
7400cd95859SKyle Evans 	 * the seals being added with respect to current mappings.
7410cd95859SKyle Evans 	 */
7420cd95859SKyle Evans 	if ((initial_seals & ~F_SEAL_SEAL) != 0)
7430cd95859SKyle Evans 		return (EINVAL);
7440cd95859SKyle Evans 
7458e38aeffSJohn Baldwin 	fdp = td->td_proc->p_fd;
7467ee1b208SEd Schouten 	cmode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
7478e38aeffSJohn Baldwin 
748b5a7ac99SKyle Evans 	/*
749b5a7ac99SKyle Evans 	 * shm_open(2) created shm should always have O_CLOEXEC set, as mandated
750b5a7ac99SKyle Evans 	 * by POSIX.  We allow it to be unset here so that an in-kernel
751b5a7ac99SKyle Evans 	 * interface may be written as a thin layer around shm, optionally not
752b5a7ac99SKyle Evans 	 * setting CLOEXEC.  For shm_open(2), O_CLOEXEC is set unconditionally
753b5a7ac99SKyle Evans 	 * in sys_shm_open() to keep this implementation compliant.
754b5a7ac99SKyle Evans 	 */
755b5a7ac99SKyle Evans 	error = falloc_caps(td, &fp, &fd, flags & O_CLOEXEC, fcaps);
7568e38aeffSJohn Baldwin 	if (error)
7578e38aeffSJohn Baldwin 		return (error);
7588e38aeffSJohn Baldwin 
7598e38aeffSJohn Baldwin 	/* A SHM_ANON path pointer creates an anonymous object. */
7607ee1b208SEd Schouten 	if (userpath == SHM_ANON) {
7618e38aeffSJohn Baldwin 		/* A read-only anonymous object is pointless. */
7627ee1b208SEd Schouten 		if ((flags & O_ACCMODE) == O_RDONLY) {
76390f54cbfSMateusz Guzik 			fdclose(td, fp, fd);
7648e38aeffSJohn Baldwin 			fdrop(fp, td);
7658e38aeffSJohn Baldwin 			return (EINVAL);
7668e38aeffSJohn Baldwin 		}
7678e38aeffSJohn Baldwin 		shmfd = shm_alloc(td->td_ucred, cmode);
7680cd95859SKyle Evans 		shmfd->shm_seals = initial_seals;
7698e38aeffSJohn Baldwin 	} else {
7708e38aeffSJohn Baldwin 		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
771cc7b259aSJamie Gritton 		pr_path = td->td_ucred->cr_prison->pr_path;
77244c16975SJamie Gritton 
77344c16975SJamie Gritton 		/* Construct a full pathname for jailed callers. */
774cc7b259aSJamie Gritton 		pr_pathlen = strcmp(pr_path, "/") == 0 ? 0
775cc7b259aSJamie Gritton 		    : strlcpy(path, pr_path, MAXPATHLEN);
776cc7b259aSJamie Gritton 		error = copyinstr(userpath, path + pr_pathlen,
777cc7b259aSJamie Gritton 		    MAXPATHLEN - pr_pathlen, NULL);
778551a7895SRui Paulo #ifdef KTRACE
779551a7895SRui Paulo 		if (error == 0 && KTRPOINT(curthread, KTR_NAMEI))
780551a7895SRui Paulo 			ktrnamei(path);
781551a7895SRui Paulo #endif
7828e38aeffSJohn Baldwin 		/* Require paths to start with a '/' character. */
783cc7b259aSJamie Gritton 		if (error == 0 && path[pr_pathlen] != '/')
7848e38aeffSJohn Baldwin 			error = EINVAL;
7858e38aeffSJohn Baldwin 		if (error) {
78690f54cbfSMateusz Guzik 			fdclose(td, fp, fd);
7878e38aeffSJohn Baldwin 			fdrop(fp, td);
7888e38aeffSJohn Baldwin 			free(path, M_SHMFD);
7898e38aeffSJohn Baldwin 			return (error);
7908e38aeffSJohn Baldwin 		}
7918e38aeffSJohn Baldwin 
79215bcf785SRobert Watson 		AUDIT_ARG_UPATH1_CANON(path);
7938e38aeffSJohn Baldwin 		fnv = fnv_32_str(path, FNV1_32_INIT);
7948e38aeffSJohn Baldwin 		sx_xlock(&shm_dict_lock);
7958e38aeffSJohn Baldwin 		shmfd = shm_lookup(path, fnv);
7968e38aeffSJohn Baldwin 		if (shmfd == NULL) {
7978e38aeffSJohn Baldwin 			/* Object does not yet exist, create it if requested. */
7987ee1b208SEd Schouten 			if (flags & O_CREAT) {
7999b6dd12eSRobert Watson #ifdef MAC
8009b6dd12eSRobert Watson 				error = mac_posixshm_check_create(td->td_ucred,
8019b6dd12eSRobert Watson 				    path);
8029b6dd12eSRobert Watson 				if (error == 0) {
8039b6dd12eSRobert Watson #endif
8048e38aeffSJohn Baldwin 					shmfd = shm_alloc(td->td_ucred, cmode);
8050cd95859SKyle Evans 					shmfd->shm_seals = initial_seals;
8068e38aeffSJohn Baldwin 					shm_insert(path, fnv, shmfd);
8079b6dd12eSRobert Watson #ifdef MAC
8089b6dd12eSRobert Watson 				}
8099b6dd12eSRobert Watson #endif
8108e38aeffSJohn Baldwin 			} else {
8118e38aeffSJohn Baldwin 				free(path, M_SHMFD);
8128e38aeffSJohn Baldwin 				error = ENOENT;
8138e38aeffSJohn Baldwin 			}
8148e38aeffSJohn Baldwin 		} else {
8150cd95859SKyle Evans 			rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
8160cd95859SKyle Evans 			    &shmfd->shm_mtx);
8170cd95859SKyle Evans 
8180cd95859SKyle Evans 			/*
8190cd95859SKyle Evans 			 * kern_shm_open() likely shouldn't ever error out on
8200cd95859SKyle Evans 			 * trying to set a seal that already exists, unlike
8210cd95859SKyle Evans 			 * F_ADD_SEALS.  This would break terribly as
8220cd95859SKyle Evans 			 * shm_open(2) actually sets F_SEAL_SEAL to maintain
8230cd95859SKyle Evans 			 * historical behavior where the underlying file could
8240cd95859SKyle Evans 			 * not be sealed.
8250cd95859SKyle Evans 			 */
8260cd95859SKyle Evans 			initial_seals &= ~shmfd->shm_seals;
8270cd95859SKyle Evans 
8288e38aeffSJohn Baldwin 			/*
8298e38aeffSJohn Baldwin 			 * Object already exists, obtain a new
8308e38aeffSJohn Baldwin 			 * reference if requested and permitted.
8318e38aeffSJohn Baldwin 			 */
8328e38aeffSJohn Baldwin 			free(path, M_SHMFD);
8330cd95859SKyle Evans 
8340cd95859SKyle Evans 			/*
8350cd95859SKyle Evans 			 * initial_seals can't set additional seals if we've
8360cd95859SKyle Evans 			 * already been set F_SEAL_SEAL.  If F_SEAL_SEAL is set,
8370cd95859SKyle Evans 			 * then we've already removed that one from
8380cd95859SKyle Evans 			 * initial_seals.  This is currently redundant as we
8390cd95859SKyle Evans 			 * only allow setting F_SEAL_SEAL at creation time, but
8400cd95859SKyle Evans 			 * it's cheap to check and decreases the effort required
8410cd95859SKyle Evans 			 * to allow additional seals.
8420cd95859SKyle Evans 			 */
8430cd95859SKyle Evans 			if ((shmfd->shm_seals & F_SEAL_SEAL) != 0 &&
8440cd95859SKyle Evans 			    initial_seals != 0)
8450cd95859SKyle Evans 				error = EPERM;
8460cd95859SKyle Evans 			else if ((flags & (O_CREAT | O_EXCL)) ==
8470cd95859SKyle Evans 			    (O_CREAT | O_EXCL))
8488e38aeffSJohn Baldwin 				error = EEXIST;
8498e38aeffSJohn Baldwin 			else {
8508e38aeffSJohn Baldwin #ifdef MAC
8518e38aeffSJohn Baldwin 				error = mac_posixshm_check_open(td->td_ucred,
8527ee1b208SEd Schouten 				    shmfd, FFLAGS(flags & O_ACCMODE));
8538e38aeffSJohn Baldwin 				if (error == 0)
8548e38aeffSJohn Baldwin #endif
8558e38aeffSJohn Baldwin 				error = shm_access(shmfd, td->td_ucred,
8567ee1b208SEd Schouten 				    FFLAGS(flags & O_ACCMODE));
8578e38aeffSJohn Baldwin 			}
8588e38aeffSJohn Baldwin 
8598e38aeffSJohn Baldwin 			/*
8608e38aeffSJohn Baldwin 			 * Truncate the file back to zero length if
8618e38aeffSJohn Baldwin 			 * O_TRUNC was specified and the object was
8628e38aeffSJohn Baldwin 			 * opened with read/write.
8638e38aeffSJohn Baldwin 			 */
8648e38aeffSJohn Baldwin 			if (error == 0 &&
8657ee1b208SEd Schouten 			    (flags & (O_ACCMODE | O_TRUNC)) ==
8668e38aeffSJohn Baldwin 			    (O_RDWR | O_TRUNC)) {
8670cd95859SKyle Evans 				VM_OBJECT_WLOCK(shmfd->shm_object);
8688e38aeffSJohn Baldwin #ifdef MAC
8698e38aeffSJohn Baldwin 				error = mac_posixshm_check_truncate(
8708e38aeffSJohn Baldwin 					td->td_ucred, fp->f_cred, shmfd);
8718e38aeffSJohn Baldwin 				if (error == 0)
8728e38aeffSJohn Baldwin #endif
8730cd95859SKyle Evans 					error = shm_dotruncate_locked(shmfd, 0,
8740cd95859SKyle Evans 					    rl_cookie);
8750cd95859SKyle Evans 				VM_OBJECT_WUNLOCK(shmfd->shm_object);
8768e38aeffSJohn Baldwin 			}
8770cd95859SKyle Evans 			if (error == 0) {
8780cd95859SKyle Evans 				/*
8790cd95859SKyle Evans 				 * Currently we only allow F_SEAL_SEAL to be
8800cd95859SKyle Evans 				 * set initially.  As noted above, this would
8810cd95859SKyle Evans 				 * need to be reworked should that change.
8820cd95859SKyle Evans 				 */
8830cd95859SKyle Evans 				shmfd->shm_seals |= initial_seals;
8848e38aeffSJohn Baldwin 				shm_hold(shmfd);
8858e38aeffSJohn Baldwin 			}
8860cd95859SKyle Evans 			rangelock_unlock(&shmfd->shm_rl, rl_cookie,
8870cd95859SKyle Evans 			    &shmfd->shm_mtx);
8880cd95859SKyle Evans 		}
8898e38aeffSJohn Baldwin 		sx_xunlock(&shm_dict_lock);
8908e38aeffSJohn Baldwin 
8918e38aeffSJohn Baldwin 		if (error) {
89290f54cbfSMateusz Guzik 			fdclose(td, fp, fd);
8938e38aeffSJohn Baldwin 			fdrop(fp, td);
8948e38aeffSJohn Baldwin 			return (error);
8958e38aeffSJohn Baldwin 		}
8968e38aeffSJohn Baldwin 	}
8978e38aeffSJohn Baldwin 
8987ee1b208SEd Schouten 	finit(fp, FFLAGS(flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
8998e38aeffSJohn Baldwin 
9008e38aeffSJohn Baldwin 	td->td_retval[0] = fd;
9018e38aeffSJohn Baldwin 	fdrop(fp, td);
9028e38aeffSJohn Baldwin 
9038e38aeffSJohn Baldwin 	return (0);
9048e38aeffSJohn Baldwin }
9058e38aeffSJohn Baldwin 
9067ee1b208SEd Schouten /* System calls. */
907*a9ac5e14SKyle Evans #ifdef COMPAT_FREEBSD12
9087ee1b208SEd Schouten int
909*a9ac5e14SKyle Evans freebsd12_shm_open(struct thread *td, struct freebsd12_shm_open_args *uap)
9107ee1b208SEd Schouten {
9117ee1b208SEd Schouten 
912b5a7ac99SKyle Evans 	return (kern_shm_open(td, uap->path, uap->flags | O_CLOEXEC, uap->mode,
9130cd95859SKyle Evans 	    NULL, F_SEAL_SEAL));
9147ee1b208SEd Schouten }
915*a9ac5e14SKyle Evans #endif
9167ee1b208SEd Schouten 
9178e38aeffSJohn Baldwin int
9188451d0ddSKip Macy sys_shm_unlink(struct thread *td, struct shm_unlink_args *uap)
9198e38aeffSJohn Baldwin {
9208e38aeffSJohn Baldwin 	char *path;
921cc7b259aSJamie Gritton 	const char *pr_path;
922cc7b259aSJamie Gritton 	size_t pr_pathlen;
9238e38aeffSJohn Baldwin 	Fnv32_t fnv;
9248e38aeffSJohn Baldwin 	int error;
9258e38aeffSJohn Baldwin 
9268e38aeffSJohn Baldwin 	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
927cc7b259aSJamie Gritton 	pr_path = td->td_ucred->cr_prison->pr_path;
928cc7b259aSJamie Gritton 	pr_pathlen = strcmp(pr_path, "/") == 0 ? 0
929cc7b259aSJamie Gritton 	    : strlcpy(path, pr_path, MAXPATHLEN);
930cc7b259aSJamie Gritton 	error = copyinstr(uap->path, path + pr_pathlen, MAXPATHLEN - pr_pathlen,
931cc7b259aSJamie Gritton 	    NULL);
9328e38aeffSJohn Baldwin 	if (error) {
9338e38aeffSJohn Baldwin 		free(path, M_TEMP);
9348e38aeffSJohn Baldwin 		return (error);
9358e38aeffSJohn Baldwin 	}
936551a7895SRui Paulo #ifdef KTRACE
937551a7895SRui Paulo 	if (KTRPOINT(curthread, KTR_NAMEI))
938551a7895SRui Paulo 		ktrnamei(path);
939551a7895SRui Paulo #endif
94015bcf785SRobert Watson 	AUDIT_ARG_UPATH1_CANON(path);
9418e38aeffSJohn Baldwin 	fnv = fnv_32_str(path, FNV1_32_INIT);
9428e38aeffSJohn Baldwin 	sx_xlock(&shm_dict_lock);
9438e38aeffSJohn Baldwin 	error = shm_remove(path, fnv, td->td_ucred);
9448e38aeffSJohn Baldwin 	sx_xunlock(&shm_dict_lock);
9458e38aeffSJohn Baldwin 	free(path, M_TEMP);
9468e38aeffSJohn Baldwin 
9478e38aeffSJohn Baldwin 	return (error);
9488e38aeffSJohn Baldwin }
9498e38aeffSJohn Baldwin 
9508e38aeffSJohn Baldwin int
9517077c426SJohn Baldwin shm_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t objsize,
9527077c426SJohn Baldwin     vm_prot_t prot, vm_prot_t cap_maxprot, int flags,
9537077c426SJohn Baldwin     vm_ooffset_t foff, struct thread *td)
9548e38aeffSJohn Baldwin {
9557077c426SJohn Baldwin 	struct shmfd *shmfd;
9567077c426SJohn Baldwin 	vm_prot_t maxprot;
9577077c426SJohn Baldwin 	int error;
958dca52ab4SKyle Evans 	bool writecnt;
959af755d3eSKyle Evans 	void *rl_cookie;
9607077c426SJohn Baldwin 
9617077c426SJohn Baldwin 	shmfd = fp->f_data;
9627077c426SJohn Baldwin 	maxprot = VM_PROT_NONE;
9637077c426SJohn Baldwin 
964af755d3eSKyle Evans 	rl_cookie = rangelock_rlock(&shmfd->shm_rl, 0, objsize,
965af755d3eSKyle Evans 	    &shmfd->shm_mtx);
9667077c426SJohn Baldwin 	/* FREAD should always be set. */
9677077c426SJohn Baldwin 	if ((fp->f_flag & FREAD) != 0)
9687077c426SJohn Baldwin 		maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
9697077c426SJohn Baldwin 	if ((fp->f_flag & FWRITE) != 0)
9707077c426SJohn Baldwin 		maxprot |= VM_PROT_WRITE;
9717077c426SJohn Baldwin 
972dca52ab4SKyle Evans 	writecnt = (flags & MAP_SHARED) != 0 && (prot & VM_PROT_WRITE) != 0;
973dca52ab4SKyle Evans 
974af755d3eSKyle Evans 	if (writecnt && (shmfd->shm_seals & F_SEAL_WRITE) != 0) {
975af755d3eSKyle Evans 		error = EPERM;
976af755d3eSKyle Evans 		goto out;
977af755d3eSKyle Evans 	}
978af755d3eSKyle Evans 
9797077c426SJohn Baldwin 	/* Don't permit shared writable mappings on read-only descriptors. */
980af755d3eSKyle Evans 	if (writecnt && (maxprot & VM_PROT_WRITE) == 0) {
981af755d3eSKyle Evans 		error = EACCES;
982af755d3eSKyle Evans 		goto out;
983af755d3eSKyle Evans 	}
9847077c426SJohn Baldwin 	maxprot &= cap_maxprot;
9857077c426SJohn Baldwin 
986987ff181SKonstantin Belousov 	/* See comment in vn_mmap(). */
987987ff181SKonstantin Belousov 	if (
988987ff181SKonstantin Belousov #ifdef _LP64
989987ff181SKonstantin Belousov 	    objsize > OFF_MAX ||
990987ff181SKonstantin Belousov #endif
991af755d3eSKyle Evans 	    foff < 0 || foff > OFF_MAX - objsize) {
992af755d3eSKyle Evans 		error = EINVAL;
993af755d3eSKyle Evans 		goto out;
994af755d3eSKyle Evans 	}
995987ff181SKonstantin Belousov 
9967077c426SJohn Baldwin #ifdef MAC
9977077c426SJohn Baldwin 	error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, flags);
9987077c426SJohn Baldwin 	if (error != 0)
999af755d3eSKyle Evans 		goto out;
10007077c426SJohn Baldwin #endif
10018e38aeffSJohn Baldwin 
10028e38aeffSJohn Baldwin 	mtx_lock(&shm_timestamp_lock);
10038e38aeffSJohn Baldwin 	vfs_timestamp(&shmfd->shm_atime);
10048e38aeffSJohn Baldwin 	mtx_unlock(&shm_timestamp_lock);
10058e38aeffSJohn Baldwin 	vm_object_reference(shmfd->shm_object);
10067077c426SJohn Baldwin 
1007dca52ab4SKyle Evans 	if (writecnt)
1008dca52ab4SKyle Evans 		vm_pager_update_writecount(shmfd->shm_object, 0, objsize);
10097077c426SJohn Baldwin 	error = vm_mmap_object(map, addr, objsize, prot, maxprot, flags,
1010dca52ab4SKyle Evans 	    shmfd->shm_object, foff, writecnt, td);
1011dca52ab4SKyle Evans 	if (error != 0) {
1012dca52ab4SKyle Evans 		if (writecnt)
1013dca52ab4SKyle Evans 			vm_pager_release_writecount(shmfd->shm_object, 0,
1014dca52ab4SKyle Evans 			    objsize);
10157077c426SJohn Baldwin 		vm_object_deallocate(shmfd->shm_object);
1016dca52ab4SKyle Evans 	}
1017af755d3eSKyle Evans out:
1018af755d3eSKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
101934d3e89fSKonstantin Belousov 	return (error);
10208e38aeffSJohn Baldwin }
10219c00bb91SKonstantin Belousov 
10229c00bb91SKonstantin Belousov static int
10239c00bb91SKonstantin Belousov shm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
10249c00bb91SKonstantin Belousov     struct thread *td)
10259c00bb91SKonstantin Belousov {
10269c00bb91SKonstantin Belousov 	struct shmfd *shmfd;
10279c00bb91SKonstantin Belousov 	int error;
10289c00bb91SKonstantin Belousov 
10299c00bb91SKonstantin Belousov 	error = 0;
10309c00bb91SKonstantin Belousov 	shmfd = fp->f_data;
10319c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
10329c00bb91SKonstantin Belousov 	/*
10339c00bb91SKonstantin Belousov 	 * SUSv4 says that x bits of permission need not be affected.
10349c00bb91SKonstantin Belousov 	 * Be consistent with our shm_open there.
10359c00bb91SKonstantin Belousov 	 */
10369c00bb91SKonstantin Belousov #ifdef MAC
10379c00bb91SKonstantin Belousov 	error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
10389c00bb91SKonstantin Belousov 	if (error != 0)
10399c00bb91SKonstantin Belousov 		goto out;
10409c00bb91SKonstantin Belousov #endif
10419c00bb91SKonstantin Belousov 	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid,
10429c00bb91SKonstantin Belousov 	    shmfd->shm_gid, VADMIN, active_cred, NULL);
10439c00bb91SKonstantin Belousov 	if (error != 0)
10449c00bb91SKonstantin Belousov 		goto out;
10459c00bb91SKonstantin Belousov 	shmfd->shm_mode = mode & ACCESSPERMS;
10469c00bb91SKonstantin Belousov out:
10479c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
10489c00bb91SKonstantin Belousov 	return (error);
10499c00bb91SKonstantin Belousov }
10509c00bb91SKonstantin Belousov 
10519c00bb91SKonstantin Belousov static int
10529c00bb91SKonstantin Belousov shm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
10539c00bb91SKonstantin Belousov     struct thread *td)
10549c00bb91SKonstantin Belousov {
10559c00bb91SKonstantin Belousov 	struct shmfd *shmfd;
10569c00bb91SKonstantin Belousov 	int error;
10579c00bb91SKonstantin Belousov 
105868889ed6SKonstantin Belousov 	error = 0;
10599c00bb91SKonstantin Belousov 	shmfd = fp->f_data;
10609c00bb91SKonstantin Belousov 	mtx_lock(&shm_timestamp_lock);
10619c00bb91SKonstantin Belousov #ifdef MAC
10629c00bb91SKonstantin Belousov 	error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
10639c00bb91SKonstantin Belousov 	if (error != 0)
10649c00bb91SKonstantin Belousov 		goto out;
10659c00bb91SKonstantin Belousov #endif
10669c00bb91SKonstantin Belousov 	if (uid == (uid_t)-1)
10679c00bb91SKonstantin Belousov 		uid = shmfd->shm_uid;
10689c00bb91SKonstantin Belousov 	if (gid == (gid_t)-1)
10699c00bb91SKonstantin Belousov                  gid = shmfd->shm_gid;
10709c00bb91SKonstantin Belousov 	if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
10719c00bb91SKonstantin Belousov 	    (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
1072cc426dd3SMateusz Guzik 	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN)))
10739c00bb91SKonstantin Belousov 		goto out;
10749c00bb91SKonstantin Belousov 	shmfd->shm_uid = uid;
10759c00bb91SKonstantin Belousov 	shmfd->shm_gid = gid;
10769c00bb91SKonstantin Belousov out:
10779c00bb91SKonstantin Belousov 	mtx_unlock(&shm_timestamp_lock);
10789c00bb91SKonstantin Belousov 	return (error);
10799c00bb91SKonstantin Belousov }
1080fb680e16SJohn Baldwin 
1081fb680e16SJohn Baldwin /*
1082fb680e16SJohn Baldwin  * Helper routines to allow the backing object of a shared memory file
1083fb680e16SJohn Baldwin  * descriptor to be mapped in the kernel.
1084fb680e16SJohn Baldwin  */
1085fb680e16SJohn Baldwin int
1086fb680e16SJohn Baldwin shm_map(struct file *fp, size_t size, off_t offset, void **memp)
1087fb680e16SJohn Baldwin {
1088fb680e16SJohn Baldwin 	struct shmfd *shmfd;
1089fb680e16SJohn Baldwin 	vm_offset_t kva, ofs;
1090fb680e16SJohn Baldwin 	vm_object_t obj;
1091fb680e16SJohn Baldwin 	int rv;
1092fb680e16SJohn Baldwin 
1093fb680e16SJohn Baldwin 	if (fp->f_type != DTYPE_SHM)
1094fb680e16SJohn Baldwin 		return (EINVAL);
1095fb680e16SJohn Baldwin 	shmfd = fp->f_data;
1096fb680e16SJohn Baldwin 	obj = shmfd->shm_object;
109789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1098fb680e16SJohn Baldwin 	/*
1099fb680e16SJohn Baldwin 	 * XXXRW: This validation is probably insufficient, and subject to
1100fb680e16SJohn Baldwin 	 * sign errors.  It should be fixed.
1101fb680e16SJohn Baldwin 	 */
1102fb680e16SJohn Baldwin 	if (offset >= shmfd->shm_size ||
1103fb680e16SJohn Baldwin 	    offset + size > round_page(shmfd->shm_size)) {
110489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
1105fb680e16SJohn Baldwin 		return (EINVAL);
1106fb680e16SJohn Baldwin 	}
1107fb680e16SJohn Baldwin 
1108fb680e16SJohn Baldwin 	shmfd->shm_kmappings++;
1109fb680e16SJohn Baldwin 	vm_object_reference_locked(obj);
111089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1111fb680e16SJohn Baldwin 
1112fb680e16SJohn Baldwin 	/* Map the object into the kernel_map and wire it. */
1113fb680e16SJohn Baldwin 	kva = vm_map_min(kernel_map);
1114fb680e16SJohn Baldwin 	ofs = offset & PAGE_MASK;
1115fb680e16SJohn Baldwin 	offset = trunc_page(offset);
1116fb680e16SJohn Baldwin 	size = round_page(size + ofs);
1117edb572a3SJohn Baldwin 	rv = vm_map_find(kernel_map, obj, offset, &kva, size, 0,
11185e3a17c0SJohn Baldwin 	    VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE,
1119fb680e16SJohn Baldwin 	    VM_PROT_READ | VM_PROT_WRITE, 0);
1120fb680e16SJohn Baldwin 	if (rv == KERN_SUCCESS) {
1121fb680e16SJohn Baldwin 		rv = vm_map_wire(kernel_map, kva, kva + size,
1122fb680e16SJohn Baldwin 		    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
1123fb680e16SJohn Baldwin 		if (rv == KERN_SUCCESS) {
1124fb680e16SJohn Baldwin 			*memp = (void *)(kva + ofs);
1125fb680e16SJohn Baldwin 			return (0);
1126fb680e16SJohn Baldwin 		}
1127fb680e16SJohn Baldwin 		vm_map_remove(kernel_map, kva, kva + size);
1128fb680e16SJohn Baldwin 	} else
1129fb680e16SJohn Baldwin 		vm_object_deallocate(obj);
1130fb680e16SJohn Baldwin 
1131fb680e16SJohn Baldwin 	/* On failure, drop our mapping reference. */
113289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1133fb680e16SJohn Baldwin 	shmfd->shm_kmappings--;
113489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1135fb680e16SJohn Baldwin 
1136338e7cf2SJohn Baldwin 	return (vm_mmap_to_errno(rv));
1137fb680e16SJohn Baldwin }
1138fb680e16SJohn Baldwin 
1139fb680e16SJohn Baldwin /*
1140fb680e16SJohn Baldwin  * We require the caller to unmap the entire entry.  This allows us to
1141fb680e16SJohn Baldwin  * safely decrement shm_kmappings when a mapping is removed.
1142fb680e16SJohn Baldwin  */
1143fb680e16SJohn Baldwin int
1144fb680e16SJohn Baldwin shm_unmap(struct file *fp, void *mem, size_t size)
1145fb680e16SJohn Baldwin {
1146fb680e16SJohn Baldwin 	struct shmfd *shmfd;
1147fb680e16SJohn Baldwin 	vm_map_entry_t entry;
1148fb680e16SJohn Baldwin 	vm_offset_t kva, ofs;
1149fb680e16SJohn Baldwin 	vm_object_t obj;
1150fb680e16SJohn Baldwin 	vm_pindex_t pindex;
1151fb680e16SJohn Baldwin 	vm_prot_t prot;
1152fb680e16SJohn Baldwin 	boolean_t wired;
1153fb680e16SJohn Baldwin 	vm_map_t map;
1154fb680e16SJohn Baldwin 	int rv;
1155fb680e16SJohn Baldwin 
1156fb680e16SJohn Baldwin 	if (fp->f_type != DTYPE_SHM)
1157fb680e16SJohn Baldwin 		return (EINVAL);
1158fb680e16SJohn Baldwin 	shmfd = fp->f_data;
1159fb680e16SJohn Baldwin 	kva = (vm_offset_t)mem;
1160fb680e16SJohn Baldwin 	ofs = kva & PAGE_MASK;
1161fb680e16SJohn Baldwin 	kva = trunc_page(kva);
1162fb680e16SJohn Baldwin 	size = round_page(size + ofs);
1163fb680e16SJohn Baldwin 	map = kernel_map;
1164fb680e16SJohn Baldwin 	rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry,
1165fb680e16SJohn Baldwin 	    &obj, &pindex, &prot, &wired);
1166fb680e16SJohn Baldwin 	if (rv != KERN_SUCCESS)
1167fb680e16SJohn Baldwin 		return (EINVAL);
1168fb680e16SJohn Baldwin 	if (entry->start != kva || entry->end != kva + size) {
1169fb680e16SJohn Baldwin 		vm_map_lookup_done(map, entry);
1170fb680e16SJohn Baldwin 		return (EINVAL);
1171fb680e16SJohn Baldwin 	}
1172fb680e16SJohn Baldwin 	vm_map_lookup_done(map, entry);
1173fb680e16SJohn Baldwin 	if (obj != shmfd->shm_object)
1174fb680e16SJohn Baldwin 		return (EINVAL);
1175fb680e16SJohn Baldwin 	vm_map_remove(map, kva, kva + size);
117689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(obj);
1177fb680e16SJohn Baldwin 	KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped"));
1178fb680e16SJohn Baldwin 	shmfd->shm_kmappings--;
117989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(obj);
1180fb680e16SJohn Baldwin 	return (0);
1181fb680e16SJohn Baldwin }
1182e506e182SJohn Baldwin 
11839696feebSJohn Baldwin static int
118456d0e33eSKonstantin Belousov shm_fill_kinfo_locked(struct shmfd *shmfd, struct kinfo_file *kif, bool list)
1185e506e182SJohn Baldwin {
1186cc7b259aSJamie Gritton 	const char *path, *pr_path;
1187cc7b259aSJamie Gritton 	size_t pr_pathlen;
118856d0e33eSKonstantin Belousov 	bool visible;
1189e506e182SJohn Baldwin 
119056d0e33eSKonstantin Belousov 	sx_assert(&shm_dict_lock, SA_LOCKED);
11919696feebSJohn Baldwin 	kif->kf_type = KF_TYPE_SHM;
119256d0e33eSKonstantin Belousov 	kif->kf_un.kf_file.kf_file_mode = S_IFREG | shmfd->shm_mode;
11939696feebSJohn Baldwin 	kif->kf_un.kf_file.kf_file_size = shmfd->shm_size;
11949696feebSJohn Baldwin 	if (shmfd->shm_path != NULL) {
119544c16975SJamie Gritton 		if (shmfd->shm_path != NULL) {
1196cc7b259aSJamie Gritton 			path = shmfd->shm_path;
1197cc7b259aSJamie Gritton 			pr_path = curthread->td_ucred->cr_prison->pr_path;
119844c16975SJamie Gritton 			if (strcmp(pr_path, "/") != 0) {
119944c16975SJamie Gritton 				/* Return the jail-rooted pathname. */
1200cc7b259aSJamie Gritton 				pr_pathlen = strlen(pr_path);
120156d0e33eSKonstantin Belousov 				visible = strncmp(path, pr_path, pr_pathlen)
120256d0e33eSKonstantin Belousov 				    == 0 && path[pr_pathlen] == '/';
120356d0e33eSKonstantin Belousov 				if (list && !visible)
120456d0e33eSKonstantin Belousov 					return (EPERM);
120556d0e33eSKonstantin Belousov 				if (visible)
1206cc7b259aSJamie Gritton 					path += pr_pathlen;
1207cc7b259aSJamie Gritton 			}
1208cc7b259aSJamie Gritton 			strlcpy(kif->kf_path, path, sizeof(kif->kf_path));
1209cc7b259aSJamie Gritton 		}
1210e506e182SJohn Baldwin 	}
12119696feebSJohn Baldwin 	return (0);
12129696feebSJohn Baldwin }
121356d0e33eSKonstantin Belousov 
121456d0e33eSKonstantin Belousov static int
121556d0e33eSKonstantin Belousov shm_fill_kinfo(struct file *fp, struct kinfo_file *kif,
121656d0e33eSKonstantin Belousov     struct filedesc *fdp __unused)
121756d0e33eSKonstantin Belousov {
121856d0e33eSKonstantin Belousov 	int res;
121956d0e33eSKonstantin Belousov 
122056d0e33eSKonstantin Belousov 	sx_slock(&shm_dict_lock);
122156d0e33eSKonstantin Belousov 	res = shm_fill_kinfo_locked(fp->f_data, kif, false);
122256d0e33eSKonstantin Belousov 	sx_sunlock(&shm_dict_lock);
122356d0e33eSKonstantin Belousov 	return (res);
122456d0e33eSKonstantin Belousov }
122556d0e33eSKonstantin Belousov 
122656d0e33eSKonstantin Belousov static int
1227af755d3eSKyle Evans shm_add_seals(struct file *fp, int seals)
1228af755d3eSKyle Evans {
1229af755d3eSKyle Evans 	struct shmfd *shmfd;
1230af755d3eSKyle Evans 	void *rl_cookie;
1231af755d3eSKyle Evans 	vm_ooffset_t writemappings;
1232af755d3eSKyle Evans 	int error, nseals;
1233af755d3eSKyle Evans 
1234af755d3eSKyle Evans 	error = 0;
1235af755d3eSKyle Evans 	shmfd = fp->f_data;
1236af755d3eSKyle Evans 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
1237af755d3eSKyle Evans 	    &shmfd->shm_mtx);
1238af755d3eSKyle Evans 
1239af755d3eSKyle Evans 	/* Even already-set seals should result in EPERM. */
1240af755d3eSKyle Evans 	if ((shmfd->shm_seals & F_SEAL_SEAL) != 0) {
1241af755d3eSKyle Evans 		error = EPERM;
1242af755d3eSKyle Evans 		goto out;
1243af755d3eSKyle Evans 	}
1244af755d3eSKyle Evans 	nseals = seals & ~shmfd->shm_seals;
1245af755d3eSKyle Evans 	if ((nseals & F_SEAL_WRITE) != 0) {
1246af755d3eSKyle Evans 		/*
1247af755d3eSKyle Evans 		 * The rangelock above prevents writable mappings from being
1248af755d3eSKyle Evans 		 * added after we've started applying seals.  The RLOCK here
1249af755d3eSKyle Evans 		 * is to avoid torn reads on ILP32 arches as unmapping/reducing
1250af755d3eSKyle Evans 		 * writemappings will be done without a rangelock.
1251af755d3eSKyle Evans 		 */
1252af755d3eSKyle Evans 		VM_OBJECT_RLOCK(shmfd->shm_object);
1253af755d3eSKyle Evans 		writemappings = shmfd->shm_object->un_pager.swp.writemappings;
1254af755d3eSKyle Evans 		VM_OBJECT_RUNLOCK(shmfd->shm_object);
1255af755d3eSKyle Evans 		/* kmappings are also writable */
1256af755d3eSKyle Evans 		if (writemappings > 0) {
1257af755d3eSKyle Evans 			error = EBUSY;
1258af755d3eSKyle Evans 			goto out;
1259af755d3eSKyle Evans 		}
1260af755d3eSKyle Evans 	}
1261af755d3eSKyle Evans 	shmfd->shm_seals |= nseals;
1262af755d3eSKyle Evans out:
1263af755d3eSKyle Evans 	rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
1264af755d3eSKyle Evans 	return (error);
1265af755d3eSKyle Evans }
1266af755d3eSKyle Evans 
1267af755d3eSKyle Evans static int
1268af755d3eSKyle Evans shm_get_seals(struct file *fp, int *seals)
1269af755d3eSKyle Evans {
1270af755d3eSKyle Evans 	struct shmfd *shmfd;
1271af755d3eSKyle Evans 
1272af755d3eSKyle Evans 	shmfd = fp->f_data;
1273af755d3eSKyle Evans 	*seals = shmfd->shm_seals;
1274af755d3eSKyle Evans 	return (0);
1275af755d3eSKyle Evans }
1276af755d3eSKyle Evans 
1277af755d3eSKyle Evans static int
127856d0e33eSKonstantin Belousov sysctl_posix_shm_list(SYSCTL_HANDLER_ARGS)
127956d0e33eSKonstantin Belousov {
128056d0e33eSKonstantin Belousov 	struct shm_mapping *shmm;
128156d0e33eSKonstantin Belousov 	struct sbuf sb;
128256d0e33eSKonstantin Belousov 	struct kinfo_file kif;
128356d0e33eSKonstantin Belousov 	u_long i;
128456d0e33eSKonstantin Belousov 	ssize_t curlen;
128556d0e33eSKonstantin Belousov 	int error, error2;
128656d0e33eSKonstantin Belousov 
128756d0e33eSKonstantin Belousov 	sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_file) * 5, req);
128856d0e33eSKonstantin Belousov 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
128956d0e33eSKonstantin Belousov 	curlen = 0;
129056d0e33eSKonstantin Belousov 	error = 0;
129156d0e33eSKonstantin Belousov 	sx_slock(&shm_dict_lock);
129256d0e33eSKonstantin Belousov 	for (i = 0; i < shm_hash + 1; i++) {
129356d0e33eSKonstantin Belousov 		LIST_FOREACH(shmm, &shm_dictionary[i], sm_link) {
129456d0e33eSKonstantin Belousov 			error = shm_fill_kinfo_locked(shmm->sm_shmfd,
129556d0e33eSKonstantin Belousov 			    &kif, true);
129656d0e33eSKonstantin Belousov 			if (error == EPERM)
129756d0e33eSKonstantin Belousov 				continue;
129856d0e33eSKonstantin Belousov 			if (error != 0)
129956d0e33eSKonstantin Belousov 				break;
130056d0e33eSKonstantin Belousov 			pack_kinfo(&kif);
130156d0e33eSKonstantin Belousov 			if (req->oldptr != NULL &&
130256d0e33eSKonstantin Belousov 			    kif.kf_structsize + curlen > req->oldlen)
130356d0e33eSKonstantin Belousov 				break;
130456d0e33eSKonstantin Belousov 			error = sbuf_bcat(&sb, &kif, kif.kf_structsize) == 0 ?
130556d0e33eSKonstantin Belousov 			    0 : ENOMEM;
130656d0e33eSKonstantin Belousov 			if (error != 0)
130756d0e33eSKonstantin Belousov 				break;
130856d0e33eSKonstantin Belousov 			curlen += kif.kf_structsize;
130956d0e33eSKonstantin Belousov 		}
131056d0e33eSKonstantin Belousov 	}
131156d0e33eSKonstantin Belousov 	sx_sunlock(&shm_dict_lock);
131256d0e33eSKonstantin Belousov 	error2 = sbuf_finish(&sb);
131356d0e33eSKonstantin Belousov 	sbuf_delete(&sb);
131456d0e33eSKonstantin Belousov 	return (error != 0 ? error : error2);
131556d0e33eSKonstantin Belousov }
131656d0e33eSKonstantin Belousov 
131756d0e33eSKonstantin Belousov SYSCTL_PROC(_kern_ipc, OID_AUTO, posix_shm_list,
131856d0e33eSKonstantin Belousov     CTLFLAG_RD | CTLFLAG_MPSAFE | CTLTYPE_OPAQUE,
131956d0e33eSKonstantin Belousov     NULL, 0, sysctl_posix_shm_list, "",
132056d0e33eSKonstantin Belousov     "POSIX SHM list");
132120f70576SKyle Evans 
132220f70576SKyle Evans int
132320f70576SKyle Evans kern_shm_open2(struct thread *td, const char *path, int flags, mode_t mode,
132420f70576SKyle Evans     int shmflags, const char *name __unused)
132520f70576SKyle Evans {
132620f70576SKyle Evans 	int initial_seals;
132720f70576SKyle Evans 
132820f70576SKyle Evans 	if ((shmflags & ~SHM_ALLOW_SEALING) != 0)
132920f70576SKyle Evans 		return (EINVAL);
133020f70576SKyle Evans 
133120f70576SKyle Evans 	initial_seals = F_SEAL_SEAL;
133220f70576SKyle Evans 	if ((shmflags & SHM_ALLOW_SEALING) != 0)
133320f70576SKyle Evans 		initial_seals &= ~F_SEAL_SEAL;
133420f70576SKyle Evans 	return (kern_shm_open(td, path, flags, 0, NULL, initial_seals));
133520f70576SKyle Evans }
133620f70576SKyle Evans 
133720f70576SKyle Evans /*
133820f70576SKyle Evans  * This version of the shm_open() interface leaves CLOEXEC behavior up to the
133920f70576SKyle Evans  * caller, and libc will enforce it for the traditional shm_open() call.  This
134020f70576SKyle Evans  * allows other consumers, like memfd_create(), to opt-in for CLOEXEC.  This
134120f70576SKyle Evans  * interface also includes a 'name' argument that is currently unused, but could
134220f70576SKyle Evans  * potentially be exported later via some interface for debugging purposes.
134320f70576SKyle Evans  * From the kernel's perspective, it is optional.  Individual consumers like
134420f70576SKyle Evans  * memfd_create() may require it in order to be compatible with other systems
134520f70576SKyle Evans  * implementing the same function.
134620f70576SKyle Evans  */
134720f70576SKyle Evans int
134820f70576SKyle Evans sys_shm_open2(struct thread *td, struct shm_open2_args *uap)
134920f70576SKyle Evans {
135020f70576SKyle Evans 
135120f70576SKyle Evans 	return (kern_shm_open2(td, uap->path, uap->flags, uap->mode,
135220f70576SKyle Evans 	    uap->shmflags, uap->name));
135320f70576SKyle Evans }
1354