uipc_shm.c revision 225344
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 225344 2011-09-02 17:40:39Z rwatson $");
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>
84175164Sjhb#include <vm/vm_map.h>
85175164Sjhb#include <vm/vm_object.h>
86175164Sjhb#include <vm/vm_page.h>
87175164Sjhb#include <vm/vm_pager.h>
88175164Sjhb#include <vm/swap_pager.h>
89175164Sjhb
90175164Sjhbstruct shm_mapping {
91175164Sjhb	char		*sm_path;
92175164Sjhb	Fnv32_t		sm_fnv;
93175164Sjhb	struct shmfd	*sm_shmfd;
94175164Sjhb	LIST_ENTRY(shm_mapping) sm_link;
95175164Sjhb};
96175164Sjhb
97175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
98175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary;
99175164Sjhbstatic struct sx shm_dict_lock;
100175164Sjhbstatic struct mtx shm_timestamp_lock;
101175164Sjhbstatic u_long shm_hash;
102175164Sjhb
103175164Sjhb#define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
104175164Sjhb
105175164Sjhbstatic int	shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
106175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
107175164Sjhbstatic void	shm_dict_init(void *arg);
108175164Sjhbstatic void	shm_drop(struct shmfd *shmfd);
109175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd);
110175164Sjhbstatic void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
111175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
112175164Sjhbstatic int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
113194766Skibstatic int	shm_dotruncate(struct shmfd *shmfd, off_t length);
114175164Sjhb
115175164Sjhbstatic fo_rdwr_t	shm_read;
116175164Sjhbstatic fo_rdwr_t	shm_write;
117175164Sjhbstatic fo_truncate_t	shm_truncate;
118175164Sjhbstatic fo_ioctl_t	shm_ioctl;
119175164Sjhbstatic fo_poll_t	shm_poll;
120175164Sjhbstatic fo_kqfilter_t	shm_kqfilter;
121175164Sjhbstatic fo_stat_t	shm_stat;
122175164Sjhbstatic fo_close_t	shm_close;
123224914Skibstatic fo_chmod_t	shm_chmod;
124224914Skibstatic fo_chown_t	shm_chown;
125175164Sjhb
126175164Sjhb/* File descriptor operations. */
127175164Sjhbstatic struct fileops shm_ops = {
128175164Sjhb	.fo_read = shm_read,
129175164Sjhb	.fo_write = shm_write,
130175164Sjhb	.fo_truncate = shm_truncate,
131175164Sjhb	.fo_ioctl = shm_ioctl,
132175164Sjhb	.fo_poll = shm_poll,
133175164Sjhb	.fo_kqfilter = shm_kqfilter,
134175164Sjhb	.fo_stat = shm_stat,
135175164Sjhb	.fo_close = shm_close,
136224914Skib	.fo_chmod = shm_chmod,
137224914Skib	.fo_chown = shm_chown,
138175164Sjhb	.fo_flags = DFLAG_PASSABLE
139175164Sjhb};
140175164Sjhb
141175164SjhbFEATURE(posix_shm, "POSIX shared memory");
142175164Sjhb
143175164Sjhbstatic int
144175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
145175164Sjhb    int flags, struct thread *td)
146175164Sjhb{
147175164Sjhb
148175164Sjhb	return (EOPNOTSUPP);
149175164Sjhb}
150175164Sjhb
151175164Sjhbstatic int
152175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
153175164Sjhb    int flags, struct thread *td)
154175164Sjhb{
155175164Sjhb
156175164Sjhb	return (EOPNOTSUPP);
157175164Sjhb}
158175164Sjhb
159175164Sjhbstatic int
160175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
161175164Sjhb    struct thread *td)
162175164Sjhb{
163175164Sjhb	struct shmfd *shmfd;
164175164Sjhb#ifdef MAC
165175164Sjhb	int error;
166175164Sjhb#endif
167175164Sjhb
168175164Sjhb	shmfd = fp->f_data;
169175164Sjhb#ifdef MAC
170175164Sjhb	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
171175164Sjhb	if (error)
172175164Sjhb		return (error);
173175164Sjhb#endif
174194766Skib	return (shm_dotruncate(shmfd, length));
175175164Sjhb}
176175164Sjhb
177175164Sjhbstatic int
178175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data,
179175164Sjhb    struct ucred *active_cred, struct thread *td)
180175164Sjhb{
181175164Sjhb
182175164Sjhb	return (EOPNOTSUPP);
183175164Sjhb}
184175164Sjhb
185175164Sjhbstatic int
186175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred,
187175164Sjhb    struct thread *td)
188175164Sjhb{
189175164Sjhb
190175164Sjhb	return (EOPNOTSUPP);
191175164Sjhb}
192175164Sjhb
193175164Sjhbstatic int
194175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn)
195175164Sjhb{
196175164Sjhb
197175164Sjhb	return (EOPNOTSUPP);
198175164Sjhb}
199175164Sjhb
200175164Sjhbstatic int
201175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
202175164Sjhb    struct thread *td)
203175164Sjhb{
204175164Sjhb	struct shmfd *shmfd;
205175164Sjhb#ifdef MAC
206175164Sjhb	int error;
207175164Sjhb#endif
208175164Sjhb
209175164Sjhb	shmfd = fp->f_data;
210175164Sjhb
211175164Sjhb#ifdef MAC
212175164Sjhb	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
213175164Sjhb	if (error)
214175164Sjhb		return (error);
215175164Sjhb#endif
216175164Sjhb
217175164Sjhb	/*
218175164Sjhb	 * Attempt to return sanish values for fstat() on a memory file
219175164Sjhb	 * descriptor.
220175164Sjhb	 */
221175164Sjhb	bzero(sb, sizeof(*sb));
222175164Sjhb	sb->st_blksize = PAGE_SIZE;
223175164Sjhb	sb->st_size = shmfd->shm_size;
224175164Sjhb	sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
225224914Skib	mtx_lock(&shm_timestamp_lock);
226205792Sed	sb->st_atim = shmfd->shm_atime;
227205792Sed	sb->st_ctim = shmfd->shm_ctime;
228205792Sed	sb->st_mtim = shmfd->shm_mtime;
229224914Skib	sb->st_birthtim = shmfd->shm_birthtime;
230224914Skib	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
231175164Sjhb	sb->st_uid = shmfd->shm_uid;
232175164Sjhb	sb->st_gid = shmfd->shm_gid;
233224914Skib	mtx_unlock(&shm_timestamp_lock);
234175164Sjhb
235175164Sjhb	return (0);
236175164Sjhb}
237175164Sjhb
238175164Sjhbstatic int
239175164Sjhbshm_close(struct file *fp, struct thread *td)
240175164Sjhb{
241175164Sjhb	struct shmfd *shmfd;
242175164Sjhb
243175164Sjhb	shmfd = fp->f_data;
244175164Sjhb	fp->f_data = NULL;
245175164Sjhb	shm_drop(shmfd);
246175164Sjhb
247175164Sjhb	return (0);
248175164Sjhb}
249175164Sjhb
250194766Skibstatic int
251175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length)
252175164Sjhb{
253175164Sjhb	vm_object_t object;
254175164Sjhb	vm_page_t m;
255175164Sjhb	vm_pindex_t nobjsize;
256194766Skib	vm_ooffset_t delta;
257175164Sjhb
258175164Sjhb	object = shmfd->shm_object;
259175164Sjhb	VM_OBJECT_LOCK(object);
260175164Sjhb	if (length == shmfd->shm_size) {
261175164Sjhb		VM_OBJECT_UNLOCK(object);
262194766Skib		return (0);
263175164Sjhb	}
264175164Sjhb	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
265175164Sjhb
266175164Sjhb	/* Are we shrinking?  If so, trim the end. */
267175164Sjhb	if (length < shmfd->shm_size) {
268194766Skib		delta = ptoa(object->size - nobjsize);
269194766Skib
270175164Sjhb		/* Toss in memory pages. */
271175164Sjhb		if (nobjsize < object->size)
272175164Sjhb			vm_object_page_remove(object, nobjsize, object->size,
273223677Salc			    0);
274175164Sjhb
275175164Sjhb		/* Toss pages from swap. */
276175164Sjhb		if (object->type == OBJT_SWAP)
277194766Skib			swap_pager_freespace(object, nobjsize, delta);
278175164Sjhb
279194766Skib		/* Free the swap accounted for shm */
280216128Strasz		swap_release_by_cred(delta, object->cred);
281194766Skib		object->charge -= delta;
282194766Skib
283175164Sjhb		/*
284175164Sjhb		 * If the last page is partially mapped, then zero out
285175164Sjhb		 * the garbage at the end of the page.  See comments
286193303Salc		 * in vnode_pager_setsize() for more details.
287175164Sjhb		 *
288175164Sjhb		 * XXXJHB: This handles in memory pages, but what about
289175164Sjhb		 * a page swapped out to disk?
290175164Sjhb		 */
291175164Sjhb		if ((length & PAGE_MASK) &&
292175164Sjhb		    (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
293175164Sjhb		    m->valid != 0) {
294175164Sjhb			int base = (int)length & PAGE_MASK;
295175164Sjhb			int size = PAGE_SIZE - base;
296175164Sjhb
297175164Sjhb			pmap_zero_page_area(m, base, size);
298193303Salc
299193303Salc			/*
300193303Salc			 * Update the valid bits to reflect the blocks that
301193303Salc			 * have been zeroed.  Some of these valid bits may
302193303Salc			 * have already been set.
303193303Salc			 */
304193303Salc			vm_page_set_valid(m, base, size);
305193303Salc
306193303Salc			/*
307193303Salc			 * Round "base" to the next block boundary so that the
308193303Salc			 * dirty bit for a partially zeroed block is not
309193303Salc			 * cleared.
310193303Salc			 */
311193303Salc			base = roundup2(base, DEV_BSIZE);
312193303Salc
313193303Salc			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
314176075Salc		} else if ((length & PAGE_MASK) &&
315176075Salc		    __predict_false(object->cache != NULL)) {
316176075Salc			vm_page_cache_free(object, OFF_TO_IDX(length),
317176075Salc			    nobjsize);
318175164Sjhb		}
319194766Skib	} else {
320194766Skib
321194766Skib		/* Attempt to reserve the swap */
322194766Skib		delta = ptoa(nobjsize - object->size);
323216128Strasz		if (!swap_reserve_by_cred(delta, object->cred)) {
324194766Skib			VM_OBJECT_UNLOCK(object);
325194766Skib			return (ENOMEM);
326194766Skib		}
327194766Skib		object->charge += delta;
328175164Sjhb	}
329175164Sjhb	shmfd->shm_size = length;
330175164Sjhb	mtx_lock(&shm_timestamp_lock);
331175164Sjhb	vfs_timestamp(&shmfd->shm_ctime);
332175164Sjhb	shmfd->shm_mtime = shmfd->shm_ctime;
333175164Sjhb	mtx_unlock(&shm_timestamp_lock);
334175164Sjhb	object->size = nobjsize;
335175164Sjhb	VM_OBJECT_UNLOCK(object);
336194766Skib	return (0);
337175164Sjhb}
338175164Sjhb
339175164Sjhb/*
340175164Sjhb * shmfd object management including creation and reference counting
341175164Sjhb * routines.
342175164Sjhb */
343175164Sjhbstatic struct shmfd *
344175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode)
345175164Sjhb{
346175164Sjhb	struct shmfd *shmfd;
347175164Sjhb
348175164Sjhb	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
349175164Sjhb	shmfd->shm_size = 0;
350175164Sjhb	shmfd->shm_uid = ucred->cr_uid;
351175164Sjhb	shmfd->shm_gid = ucred->cr_gid;
352175164Sjhb	shmfd->shm_mode = mode;
353175164Sjhb	shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
354194766Skib	    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
355175164Sjhb	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
356178181Salc	VM_OBJECT_LOCK(shmfd->shm_object);
357178181Salc	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
358178181Salc	vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
359178181Salc	VM_OBJECT_UNLOCK(shmfd->shm_object);
360175164Sjhb	vfs_timestamp(&shmfd->shm_birthtime);
361175164Sjhb	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
362175164Sjhb	    shmfd->shm_birthtime;
363175164Sjhb	refcount_init(&shmfd->shm_refs, 1);
364175164Sjhb#ifdef MAC
365175164Sjhb	mac_posixshm_init(shmfd);
366175164Sjhb	mac_posixshm_create(ucred, shmfd);
367175164Sjhb#endif
368175164Sjhb
369175164Sjhb	return (shmfd);
370175164Sjhb}
371175164Sjhb
372175164Sjhbstatic struct shmfd *
373175164Sjhbshm_hold(struct shmfd *shmfd)
374175164Sjhb{
375175164Sjhb
376175164Sjhb	refcount_acquire(&shmfd->shm_refs);
377175164Sjhb	return (shmfd);
378175164Sjhb}
379175164Sjhb
380175164Sjhbstatic void
381175164Sjhbshm_drop(struct shmfd *shmfd)
382175164Sjhb{
383175164Sjhb
384175164Sjhb	if (refcount_release(&shmfd->shm_refs)) {
385175164Sjhb#ifdef MAC
386175164Sjhb		mac_posixshm_destroy(shmfd);
387175164Sjhb#endif
388175164Sjhb		vm_object_deallocate(shmfd->shm_object);
389175164Sjhb		free(shmfd, M_SHMFD);
390175164Sjhb	}
391175164Sjhb}
392175164Sjhb
393175164Sjhb/*
394175164Sjhb * Determine if the credentials have sufficient permissions for a
395175164Sjhb * specified combination of FREAD and FWRITE.
396175164Sjhb */
397175164Sjhbstatic int
398175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
399175164Sjhb{
400184413Strasz	accmode_t accmode;
401224914Skib	int error;
402175164Sjhb
403184413Strasz	accmode = 0;
404175164Sjhb	if (flags & FREAD)
405184413Strasz		accmode |= VREAD;
406175164Sjhb	if (flags & FWRITE)
407184413Strasz		accmode |= VWRITE;
408224914Skib	mtx_lock(&shm_timestamp_lock);
409224914Skib	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
410224914Skib	    accmode, ucred, NULL);
411224914Skib	mtx_unlock(&shm_timestamp_lock);
412224914Skib	return (error);
413175164Sjhb}
414175164Sjhb
415175164Sjhb/*
416175164Sjhb * Dictionary management.  We maintain an in-kernel dictionary to map
417175164Sjhb * paths to shmfd objects.  We use the FNV hash on the path to store
418175164Sjhb * the mappings in a hash table.
419175164Sjhb */
420175164Sjhbstatic void
421175164Sjhbshm_dict_init(void *arg)
422175164Sjhb{
423175164Sjhb
424175164Sjhb	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
425175164Sjhb	sx_init(&shm_dict_lock, "shm dictionary");
426175164Sjhb	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
427175164Sjhb}
428175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
429175164Sjhb
430175164Sjhbstatic struct shmfd *
431175164Sjhbshm_lookup(char *path, Fnv32_t fnv)
432175164Sjhb{
433175164Sjhb	struct shm_mapping *map;
434175164Sjhb
435175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
436175164Sjhb		if (map->sm_fnv != fnv)
437175164Sjhb			continue;
438175164Sjhb		if (strcmp(map->sm_path, path) == 0)
439175164Sjhb			return (map->sm_shmfd);
440175164Sjhb	}
441175164Sjhb
442175164Sjhb	return (NULL);
443175164Sjhb}
444175164Sjhb
445175164Sjhbstatic void
446175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
447175164Sjhb{
448175164Sjhb	struct shm_mapping *map;
449175164Sjhb
450175164Sjhb	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
451175164Sjhb	map->sm_path = path;
452175164Sjhb	map->sm_fnv = fnv;
453175164Sjhb	map->sm_shmfd = shm_hold(shmfd);
454175164Sjhb	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
455175164Sjhb}
456175164Sjhb
457175164Sjhbstatic int
458175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
459175164Sjhb{
460175164Sjhb	struct shm_mapping *map;
461175164Sjhb	int error;
462175164Sjhb
463175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
464175164Sjhb		if (map->sm_fnv != fnv)
465175164Sjhb			continue;
466175164Sjhb		if (strcmp(map->sm_path, path) == 0) {
467175164Sjhb#ifdef MAC
468175164Sjhb			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
469175164Sjhb			if (error)
470175164Sjhb				return (error);
471175164Sjhb#endif
472175164Sjhb			error = shm_access(map->sm_shmfd, ucred,
473175164Sjhb			    FREAD | FWRITE);
474175164Sjhb			if (error)
475175164Sjhb				return (error);
476175164Sjhb			LIST_REMOVE(map, sm_link);
477175164Sjhb			shm_drop(map->sm_shmfd);
478175164Sjhb			free(map->sm_path, M_SHMFD);
479175164Sjhb			free(map, M_SHMFD);
480175164Sjhb			return (0);
481175164Sjhb		}
482175164Sjhb	}
483175164Sjhb
484175164Sjhb	return (ENOENT);
485175164Sjhb}
486175164Sjhb
487175164Sjhb/* System calls. */
488175164Sjhbint
489175164Sjhbshm_open(struct thread *td, struct shm_open_args *uap)
490175164Sjhb{
491175164Sjhb	struct filedesc *fdp;
492175164Sjhb	struct shmfd *shmfd;
493175164Sjhb	struct file *fp;
494175164Sjhb	char *path;
495175164Sjhb	Fnv32_t fnv;
496175164Sjhb	mode_t cmode;
497175164Sjhb	int fd, error;
498175164Sjhb
499223692Sjonathan#ifdef CAPABILITY_MODE
500223692Sjonathan	/*
501223692Sjonathan	 * shm_open(2) is only allowed for anonymous objects.
502223692Sjonathan	 */
503223692Sjonathan	if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON))
504223692Sjonathan		return (ECAPMODE);
505223692Sjonathan#endif
506223692Sjonathan
507175164Sjhb	if ((uap->flags & O_ACCMODE) != O_RDONLY &&
508175164Sjhb	    (uap->flags & O_ACCMODE) != O_RDWR)
509175164Sjhb		return (EINVAL);
510175164Sjhb
511175164Sjhb	if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
512175164Sjhb		return (EINVAL);
513175164Sjhb
514175164Sjhb	fdp = td->td_proc->p_fd;
515175164Sjhb	cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
516175164Sjhb
517220245Skib	error = falloc(td, &fp, &fd, 0);
518175164Sjhb	if (error)
519175164Sjhb		return (error);
520175164Sjhb
521175164Sjhb	/* A SHM_ANON path pointer creates an anonymous object. */
522175164Sjhb	if (uap->path == SHM_ANON) {
523175164Sjhb		/* A read-only anonymous object is pointless. */
524175164Sjhb		if ((uap->flags & O_ACCMODE) == O_RDONLY) {
525175164Sjhb			fdclose(fdp, fp, fd, td);
526175164Sjhb			fdrop(fp, td);
527175164Sjhb			return (EINVAL);
528175164Sjhb		}
529175164Sjhb		shmfd = shm_alloc(td->td_ucred, cmode);
530175164Sjhb	} else {
531175164Sjhb		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
532175164Sjhb		error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
533175164Sjhb
534175164Sjhb		/* Require paths to start with a '/' character. */
535175164Sjhb		if (error == 0 && path[0] != '/')
536175164Sjhb			error = EINVAL;
537175164Sjhb		if (error) {
538175164Sjhb			fdclose(fdp, fp, fd, td);
539175164Sjhb			fdrop(fp, td);
540175164Sjhb			free(path, M_SHMFD);
541175164Sjhb			return (error);
542175164Sjhb		}
543175164Sjhb
544175164Sjhb		fnv = fnv_32_str(path, FNV1_32_INIT);
545175164Sjhb		sx_xlock(&shm_dict_lock);
546175164Sjhb		shmfd = shm_lookup(path, fnv);
547175164Sjhb		if (shmfd == NULL) {
548175164Sjhb			/* Object does not yet exist, create it if requested. */
549175164Sjhb			if (uap->flags & O_CREAT) {
550225344Srwatson#ifdef MAC
551225344Srwatson				error = mac_posixshm_check_create(td->td_ucred,
552225344Srwatson				    path);
553225344Srwatson				if (error == 0) {
554225344Srwatson#endif
555225344Srwatson					shmfd = shm_alloc(td->td_ucred, cmode);
556225344Srwatson					shm_insert(path, fnv, shmfd);
557225344Srwatson#ifdef MAC
558225344Srwatson				}
559225344Srwatson#endif
560175164Sjhb			} else {
561175164Sjhb				free(path, M_SHMFD);
562175164Sjhb				error = ENOENT;
563175164Sjhb			}
564175164Sjhb		} else {
565175164Sjhb			/*
566175164Sjhb			 * Object already exists, obtain a new
567175164Sjhb			 * reference if requested and permitted.
568175164Sjhb			 */
569175164Sjhb			free(path, M_SHMFD);
570175164Sjhb			if ((uap->flags & (O_CREAT | O_EXCL)) ==
571175164Sjhb			    (O_CREAT | O_EXCL))
572175164Sjhb				error = EEXIST;
573175164Sjhb			else {
574175164Sjhb#ifdef MAC
575175164Sjhb				error = mac_posixshm_check_open(td->td_ucred,
576225344Srwatson				    shmfd, FFLAGS(uap->flags & O_ACCMODE));
577175164Sjhb				if (error == 0)
578175164Sjhb#endif
579175164Sjhb				error = shm_access(shmfd, td->td_ucred,
580175164Sjhb				    FFLAGS(uap->flags & O_ACCMODE));
581175164Sjhb			}
582175164Sjhb
583175164Sjhb			/*
584175164Sjhb			 * Truncate the file back to zero length if
585175164Sjhb			 * O_TRUNC was specified and the object was
586175164Sjhb			 * opened with read/write.
587175164Sjhb			 */
588175164Sjhb			if (error == 0 &&
589175164Sjhb			    (uap->flags & (O_ACCMODE | O_TRUNC)) ==
590175164Sjhb			    (O_RDWR | O_TRUNC)) {
591175164Sjhb#ifdef MAC
592175164Sjhb				error = mac_posixshm_check_truncate(
593175164Sjhb					td->td_ucred, fp->f_cred, shmfd);
594175164Sjhb				if (error == 0)
595175164Sjhb#endif
596175164Sjhb					shm_dotruncate(shmfd, 0);
597175164Sjhb			}
598175164Sjhb			if (error == 0)
599175164Sjhb				shm_hold(shmfd);
600175164Sjhb		}
601175164Sjhb		sx_xunlock(&shm_dict_lock);
602175164Sjhb
603175164Sjhb		if (error) {
604175164Sjhb			fdclose(fdp, fp, fd, td);
605175164Sjhb			fdrop(fp, td);
606175164Sjhb			return (error);
607175164Sjhb		}
608175164Sjhb	}
609175164Sjhb
610175164Sjhb	finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
611175164Sjhb
612175164Sjhb	FILEDESC_XLOCK(fdp);
613175164Sjhb	if (fdp->fd_ofiles[fd] == fp)
614175164Sjhb		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
615175164Sjhb	FILEDESC_XUNLOCK(fdp);
616175164Sjhb	td->td_retval[0] = fd;
617175164Sjhb	fdrop(fp, td);
618175164Sjhb
619175164Sjhb	return (0);
620175164Sjhb}
621175164Sjhb
622175164Sjhbint
623175164Sjhbshm_unlink(struct thread *td, struct shm_unlink_args *uap)
624175164Sjhb{
625175164Sjhb	char *path;
626175164Sjhb	Fnv32_t fnv;
627175164Sjhb	int error;
628175164Sjhb
629175164Sjhb	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
630175164Sjhb	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
631175164Sjhb	if (error) {
632175164Sjhb		free(path, M_TEMP);
633175164Sjhb		return (error);
634175164Sjhb	}
635175164Sjhb
636175164Sjhb	fnv = fnv_32_str(path, FNV1_32_INIT);
637175164Sjhb	sx_xlock(&shm_dict_lock);
638175164Sjhb	error = shm_remove(path, fnv, td->td_ucred);
639175164Sjhb	sx_xunlock(&shm_dict_lock);
640175164Sjhb	free(path, M_TEMP);
641175164Sjhb
642175164Sjhb	return (error);
643175164Sjhb}
644175164Sjhb
645175164Sjhb/*
646175164Sjhb * mmap() helper to validate mmap() requests against shm object state
647175164Sjhb * and give mmap() the vm_object to use for the mapping.
648175164Sjhb */
649175164Sjhbint
650175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
651175164Sjhb    vm_object_t *obj)
652175164Sjhb{
653175164Sjhb
654175164Sjhb	/*
655175164Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
656175164Sjhb	 * sign errors.  It should be fixed.
657175164Sjhb	 */
658185533Skan	if (foff >= shmfd->shm_size ||
659185533Skan	    foff + objsize > round_page(shmfd->shm_size))
660175164Sjhb		return (EINVAL);
661175164Sjhb
662175164Sjhb	mtx_lock(&shm_timestamp_lock);
663175164Sjhb	vfs_timestamp(&shmfd->shm_atime);
664175164Sjhb	mtx_unlock(&shm_timestamp_lock);
665175164Sjhb	vm_object_reference(shmfd->shm_object);
666175164Sjhb	*obj = shmfd->shm_object;
667175164Sjhb	return (0);
668175164Sjhb}
669224914Skib
670224914Skibstatic int
671224914Skibshm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
672224914Skib    struct thread *td)
673224914Skib{
674224914Skib	struct shmfd *shmfd;
675224914Skib	int error;
676224914Skib
677224914Skib	error = 0;
678224914Skib	shmfd = fp->f_data;
679224914Skib	mtx_lock(&shm_timestamp_lock);
680224914Skib	/*
681224914Skib	 * SUSv4 says that x bits of permission need not be affected.
682224914Skib	 * Be consistent with our shm_open there.
683224914Skib	 */
684224914Skib#ifdef MAC
685224914Skib	error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
686224914Skib	if (error != 0)
687224914Skib		goto out;
688224914Skib#endif
689224914Skib	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid,
690224914Skib	    shmfd->shm_gid, VADMIN, active_cred, NULL);
691224914Skib	if (error != 0)
692224914Skib		goto out;
693224914Skib	shmfd->shm_mode = mode & ACCESSPERMS;
694224914Skibout:
695224914Skib	mtx_unlock(&shm_timestamp_lock);
696224914Skib	return (error);
697224914Skib}
698224914Skib
699224914Skibstatic int
700224914Skibshm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
701224914Skib    struct thread *td)
702224914Skib{
703224914Skib	struct shmfd *shmfd;
704224914Skib	int error;
705224914Skib
706224935Skib	error = 0;
707224914Skib	shmfd = fp->f_data;
708224914Skib	mtx_lock(&shm_timestamp_lock);
709224914Skib#ifdef MAC
710224914Skib	error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
711224914Skib	if (error != 0)
712224914Skib		goto out;
713224914Skib#endif
714224914Skib	if (uid == (uid_t)-1)
715224914Skib		uid = shmfd->shm_uid;
716224914Skib	if (gid == (gid_t)-1)
717224914Skib                 gid = shmfd->shm_gid;
718224914Skib	if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
719224914Skib	    (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
720224914Skib	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
721224914Skib		goto out;
722224914Skib	shmfd->shm_uid = uid;
723224914Skib	shmfd->shm_gid = gid;
724224914Skibout:
725224914Skib	mtx_unlock(&shm_timestamp_lock);
726224914Skib	return (error);
727224914Skib}
728