uipc_shm.c revision 229821
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 */
43175164Sjhb
44175164Sjhb#include <sys/cdefs.h>
45175164Sjhb__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 229821 2012-01-08 20:09:26Z alc $");
46175164Sjhb
47223692Sjonathan#include "opt_capsicum.h"
48223692Sjonathan
49175164Sjhb#include <sys/param.h>
50223692Sjonathan#include <sys/capability.h>
51175164Sjhb#include <sys/fcntl.h>
52175164Sjhb#include <sys/file.h>
53175164Sjhb#include <sys/filedesc.h>
54175164Sjhb#include <sys/fnv_hash.h>
55175164Sjhb#include <sys/kernel.h>
56175164Sjhb#include <sys/lock.h>
57175164Sjhb#include <sys/malloc.h>
58175164Sjhb#include <sys/mman.h>
59175164Sjhb#include <sys/mutex.h>
60224914Skib#include <sys/priv.h>
61175164Sjhb#include <sys/proc.h>
62175164Sjhb#include <sys/refcount.h>
63175164Sjhb#include <sys/resourcevar.h>
64175164Sjhb#include <sys/stat.h>
65175164Sjhb#include <sys/sysctl.h>
66175164Sjhb#include <sys/sysproto.h>
67175164Sjhb#include <sys/systm.h>
68175164Sjhb#include <sys/sx.h>
69175164Sjhb#include <sys/time.h>
70175164Sjhb#include <sys/vnode.h>
71175164Sjhb
72175164Sjhb#include <security/mac/mac_framework.h>
73175164Sjhb
74175164Sjhb#include <vm/vm.h>
75175164Sjhb#include <vm/vm_param.h>
76175164Sjhb#include <vm/pmap.h>
77228533Sjhb#include <vm/vm_extern.h>
78175164Sjhb#include <vm/vm_map.h>
79228509Sjhb#include <vm/vm_kern.h>
80175164Sjhb#include <vm/vm_object.h>
81175164Sjhb#include <vm/vm_page.h>
82229821Salc#include <vm/vm_pageout.h>
83175164Sjhb#include <vm/vm_pager.h>
84175164Sjhb#include <vm/swap_pager.h>
85175164Sjhb
86175164Sjhbstruct shm_mapping {
87175164Sjhb	char		*sm_path;
88175164Sjhb	Fnv32_t		sm_fnv;
89175164Sjhb	struct shmfd	*sm_shmfd;
90175164Sjhb	LIST_ENTRY(shm_mapping) sm_link;
91175164Sjhb};
92175164Sjhb
93175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
94175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary;
95175164Sjhbstatic struct sx shm_dict_lock;
96175164Sjhbstatic struct mtx shm_timestamp_lock;
97175164Sjhbstatic u_long shm_hash;
98175164Sjhb
99175164Sjhb#define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
100175164Sjhb
101175164Sjhbstatic int	shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
102175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
103175164Sjhbstatic void	shm_dict_init(void *arg);
104175164Sjhbstatic void	shm_drop(struct shmfd *shmfd);
105175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd);
106175164Sjhbstatic void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
107175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
108175164Sjhbstatic int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
109194766Skibstatic int	shm_dotruncate(struct shmfd *shmfd, off_t length);
110175164Sjhb
111175164Sjhbstatic fo_rdwr_t	shm_read;
112175164Sjhbstatic fo_rdwr_t	shm_write;
113175164Sjhbstatic fo_truncate_t	shm_truncate;
114175164Sjhbstatic fo_ioctl_t	shm_ioctl;
115175164Sjhbstatic fo_poll_t	shm_poll;
116175164Sjhbstatic fo_kqfilter_t	shm_kqfilter;
117175164Sjhbstatic fo_stat_t	shm_stat;
118175164Sjhbstatic fo_close_t	shm_close;
119224914Skibstatic fo_chmod_t	shm_chmod;
120224914Skibstatic fo_chown_t	shm_chown;
121175164Sjhb
122175164Sjhb/* File descriptor operations. */
123175164Sjhbstatic struct fileops shm_ops = {
124175164Sjhb	.fo_read = shm_read,
125175164Sjhb	.fo_write = shm_write,
126175164Sjhb	.fo_truncate = shm_truncate,
127175164Sjhb	.fo_ioctl = shm_ioctl,
128175164Sjhb	.fo_poll = shm_poll,
129175164Sjhb	.fo_kqfilter = shm_kqfilter,
130175164Sjhb	.fo_stat = shm_stat,
131175164Sjhb	.fo_close = shm_close,
132224914Skib	.fo_chmod = shm_chmod,
133224914Skib	.fo_chown = shm_chown,
134175164Sjhb	.fo_flags = DFLAG_PASSABLE
135175164Sjhb};
136175164Sjhb
137175164SjhbFEATURE(posix_shm, "POSIX shared memory");
138175164Sjhb
139175164Sjhbstatic int
140175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
141175164Sjhb    int flags, struct thread *td)
142175164Sjhb{
143175164Sjhb
144175164Sjhb	return (EOPNOTSUPP);
145175164Sjhb}
146175164Sjhb
147175164Sjhbstatic int
148175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
149175164Sjhb    int flags, struct thread *td)
150175164Sjhb{
151175164Sjhb
152175164Sjhb	return (EOPNOTSUPP);
153175164Sjhb}
154175164Sjhb
155175164Sjhbstatic int
156175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
157175164Sjhb    struct thread *td)
158175164Sjhb{
159175164Sjhb	struct shmfd *shmfd;
160175164Sjhb#ifdef MAC
161175164Sjhb	int error;
162175164Sjhb#endif
163175164Sjhb
164175164Sjhb	shmfd = fp->f_data;
165175164Sjhb#ifdef MAC
166175164Sjhb	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
167175164Sjhb	if (error)
168175164Sjhb		return (error);
169175164Sjhb#endif
170194766Skib	return (shm_dotruncate(shmfd, length));
171175164Sjhb}
172175164Sjhb
173175164Sjhbstatic int
174175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data,
175175164Sjhb    struct ucred *active_cred, struct thread *td)
176175164Sjhb{
177175164Sjhb
178175164Sjhb	return (EOPNOTSUPP);
179175164Sjhb}
180175164Sjhb
181175164Sjhbstatic int
182175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred,
183175164Sjhb    struct thread *td)
184175164Sjhb{
185175164Sjhb
186175164Sjhb	return (EOPNOTSUPP);
187175164Sjhb}
188175164Sjhb
189175164Sjhbstatic int
190175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn)
191175164Sjhb{
192175164Sjhb
193175164Sjhb	return (EOPNOTSUPP);
194175164Sjhb}
195175164Sjhb
196175164Sjhbstatic int
197175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
198175164Sjhb    struct thread *td)
199175164Sjhb{
200175164Sjhb	struct shmfd *shmfd;
201175164Sjhb#ifdef MAC
202175164Sjhb	int error;
203175164Sjhb#endif
204175164Sjhb
205175164Sjhb	shmfd = fp->f_data;
206175164Sjhb
207175164Sjhb#ifdef MAC
208175164Sjhb	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
209175164Sjhb	if (error)
210175164Sjhb		return (error);
211175164Sjhb#endif
212175164Sjhb
213175164Sjhb	/*
214175164Sjhb	 * Attempt to return sanish values for fstat() on a memory file
215175164Sjhb	 * descriptor.
216175164Sjhb	 */
217175164Sjhb	bzero(sb, sizeof(*sb));
218175164Sjhb	sb->st_blksize = PAGE_SIZE;
219175164Sjhb	sb->st_size = shmfd->shm_size;
220175164Sjhb	sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
221224914Skib	mtx_lock(&shm_timestamp_lock);
222205792Sed	sb->st_atim = shmfd->shm_atime;
223205792Sed	sb->st_ctim = shmfd->shm_ctime;
224205792Sed	sb->st_mtim = shmfd->shm_mtime;
225224914Skib	sb->st_birthtim = shmfd->shm_birthtime;
226224914Skib	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
227175164Sjhb	sb->st_uid = shmfd->shm_uid;
228175164Sjhb	sb->st_gid = shmfd->shm_gid;
229224914Skib	mtx_unlock(&shm_timestamp_lock);
230175164Sjhb
231175164Sjhb	return (0);
232175164Sjhb}
233175164Sjhb
234175164Sjhbstatic int
235175164Sjhbshm_close(struct file *fp, struct thread *td)
236175164Sjhb{
237175164Sjhb	struct shmfd *shmfd;
238175164Sjhb
239175164Sjhb	shmfd = fp->f_data;
240175164Sjhb	fp->f_data = NULL;
241175164Sjhb	shm_drop(shmfd);
242175164Sjhb
243175164Sjhb	return (0);
244175164Sjhb}
245175164Sjhb
246194766Skibstatic int
247175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length)
248175164Sjhb{
249175164Sjhb	vm_object_t object;
250229821Salc	vm_page_t m, ma[1];
251229821Salc	vm_pindex_t idx, nobjsize;
252194766Skib	vm_ooffset_t delta;
253229821Salc	int base, rv;
254175164Sjhb
255175164Sjhb	object = shmfd->shm_object;
256175164Sjhb	VM_OBJECT_LOCK(object);
257175164Sjhb	if (length == shmfd->shm_size) {
258175164Sjhb		VM_OBJECT_UNLOCK(object);
259194766Skib		return (0);
260175164Sjhb	}
261175164Sjhb	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
262175164Sjhb
263175164Sjhb	/* Are we shrinking?  If so, trim the end. */
264175164Sjhb	if (length < shmfd->shm_size) {
265228509Sjhb		/*
266228509Sjhb		 * Disallow any requests to shrink the size if this
267228509Sjhb		 * object is mapped into the kernel.
268228509Sjhb		 */
269228509Sjhb		if (shmfd->shm_kmappings > 0) {
270228509Sjhb			VM_OBJECT_UNLOCK(object);
271228509Sjhb			return (EBUSY);
272228509Sjhb		}
273229821Salc
274229821Salc		/*
275229821Salc		 * Zero the truncated part of the last page.
276229821Salc		 */
277229821Salc		base = length & PAGE_MASK;
278229821Salc		if (base != 0) {
279229821Salc			idx = OFF_TO_IDX(length);
280229821Salcretry:
281229821Salc			m = vm_page_lookup(object, idx);
282229821Salc			if (m != NULL) {
283229821Salc				if ((m->oflags & VPO_BUSY) != 0 ||
284229821Salc				    m->busy != 0) {
285229821Salc					vm_page_sleep(m, "shmtrc");
286229821Salc					goto retry;
287229821Salc				}
288229821Salc			} else if (vm_pager_has_page(object, idx, NULL, NULL)) {
289229821Salc				m = vm_page_alloc(object, idx, VM_ALLOC_NORMAL);
290229821Salc				if (m == NULL) {
291229821Salc					VM_OBJECT_UNLOCK(object);
292229821Salc					VM_WAIT;
293229821Salc					VM_OBJECT_LOCK(object);
294229821Salc					goto retry;
295229821Salc				} else if (m->valid != VM_PAGE_BITS_ALL) {
296229821Salc					ma[0] = m;
297229821Salc					rv = vm_pager_get_pages(object, ma, 1,
298229821Salc					    0);
299229821Salc					m = vm_page_lookup(object, idx);
300229821Salc				} else
301229821Salc					/* A cached page was reactivated. */
302229821Salc					rv = VM_PAGER_OK;
303229821Salc				vm_page_lock(m);
304229821Salc				if (rv == VM_PAGER_OK) {
305229821Salc					vm_page_deactivate(m);
306229821Salc					vm_page_unlock(m);
307229821Salc					vm_page_wakeup(m);
308229821Salc				} else {
309229821Salc					vm_page_free(m);
310229821Salc					vm_page_unlock(m);
311229821Salc					VM_OBJECT_UNLOCK(object);
312229821Salc					return (EIO);
313229821Salc				}
314229821Salc			}
315229821Salc			if (m != NULL) {
316229821Salc				pmap_zero_page_area(m, base, PAGE_SIZE - base);
317229821Salc				KASSERT(m->valid == VM_PAGE_BITS_ALL,
318229821Salc				    ("shm_dotruncate: page %p is invalid", m));
319229821Salc				vm_page_dirty(m);
320229821Salc				vm_pager_page_unswapped(m);
321229821Salc			}
322229821Salc		}
323194766Skib		delta = ptoa(object->size - nobjsize);
324194766Skib
325175164Sjhb		/* Toss in memory pages. */
326175164Sjhb		if (nobjsize < object->size)
327175164Sjhb			vm_object_page_remove(object, nobjsize, object->size,
328223677Salc			    0);
329175164Sjhb
330175164Sjhb		/* Toss pages from swap. */
331175164Sjhb		if (object->type == OBJT_SWAP)
332194766Skib			swap_pager_freespace(object, nobjsize, delta);
333175164Sjhb
334194766Skib		/* Free the swap accounted for shm */
335216128Strasz		swap_release_by_cred(delta, object->cred);
336194766Skib		object->charge -= delta;
337194766Skib	} else {
338194766Skib		/* Attempt to reserve the swap */
339194766Skib		delta = ptoa(nobjsize - object->size);
340216128Strasz		if (!swap_reserve_by_cred(delta, object->cred)) {
341194766Skib			VM_OBJECT_UNLOCK(object);
342194766Skib			return (ENOMEM);
343194766Skib		}
344194766Skib		object->charge += delta;
345175164Sjhb	}
346175164Sjhb	shmfd->shm_size = length;
347175164Sjhb	mtx_lock(&shm_timestamp_lock);
348175164Sjhb	vfs_timestamp(&shmfd->shm_ctime);
349175164Sjhb	shmfd->shm_mtime = shmfd->shm_ctime;
350175164Sjhb	mtx_unlock(&shm_timestamp_lock);
351175164Sjhb	object->size = nobjsize;
352175164Sjhb	VM_OBJECT_UNLOCK(object);
353194766Skib	return (0);
354175164Sjhb}
355175164Sjhb
356175164Sjhb/*
357175164Sjhb * shmfd object management including creation and reference counting
358175164Sjhb * routines.
359175164Sjhb */
360175164Sjhbstatic struct shmfd *
361175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode)
362175164Sjhb{
363175164Sjhb	struct shmfd *shmfd;
364175164Sjhb
365175164Sjhb	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
366175164Sjhb	shmfd->shm_size = 0;
367175164Sjhb	shmfd->shm_uid = ucred->cr_uid;
368175164Sjhb	shmfd->shm_gid = ucred->cr_gid;
369175164Sjhb	shmfd->shm_mode = mode;
370175164Sjhb	shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
371194766Skib	    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
372175164Sjhb	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
373178181Salc	VM_OBJECT_LOCK(shmfd->shm_object);
374178181Salc	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
375178181Salc	vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
376178181Salc	VM_OBJECT_UNLOCK(shmfd->shm_object);
377175164Sjhb	vfs_timestamp(&shmfd->shm_birthtime);
378175164Sjhb	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
379175164Sjhb	    shmfd->shm_birthtime;
380175164Sjhb	refcount_init(&shmfd->shm_refs, 1);
381175164Sjhb#ifdef MAC
382175164Sjhb	mac_posixshm_init(shmfd);
383175164Sjhb	mac_posixshm_create(ucred, shmfd);
384175164Sjhb#endif
385175164Sjhb
386175164Sjhb	return (shmfd);
387175164Sjhb}
388175164Sjhb
389175164Sjhbstatic struct shmfd *
390175164Sjhbshm_hold(struct shmfd *shmfd)
391175164Sjhb{
392175164Sjhb
393175164Sjhb	refcount_acquire(&shmfd->shm_refs);
394175164Sjhb	return (shmfd);
395175164Sjhb}
396175164Sjhb
397175164Sjhbstatic void
398175164Sjhbshm_drop(struct shmfd *shmfd)
399175164Sjhb{
400175164Sjhb
401175164Sjhb	if (refcount_release(&shmfd->shm_refs)) {
402175164Sjhb#ifdef MAC
403175164Sjhb		mac_posixshm_destroy(shmfd);
404175164Sjhb#endif
405175164Sjhb		vm_object_deallocate(shmfd->shm_object);
406175164Sjhb		free(shmfd, M_SHMFD);
407175164Sjhb	}
408175164Sjhb}
409175164Sjhb
410175164Sjhb/*
411175164Sjhb * Determine if the credentials have sufficient permissions for a
412175164Sjhb * specified combination of FREAD and FWRITE.
413175164Sjhb */
414175164Sjhbstatic int
415175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
416175164Sjhb{
417184413Strasz	accmode_t accmode;
418224914Skib	int error;
419175164Sjhb
420184413Strasz	accmode = 0;
421175164Sjhb	if (flags & FREAD)
422184413Strasz		accmode |= VREAD;
423175164Sjhb	if (flags & FWRITE)
424184413Strasz		accmode |= VWRITE;
425224914Skib	mtx_lock(&shm_timestamp_lock);
426224914Skib	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
427224914Skib	    accmode, ucred, NULL);
428224914Skib	mtx_unlock(&shm_timestamp_lock);
429224914Skib	return (error);
430175164Sjhb}
431175164Sjhb
432175164Sjhb/*
433175164Sjhb * Dictionary management.  We maintain an in-kernel dictionary to map
434175164Sjhb * paths to shmfd objects.  We use the FNV hash on the path to store
435175164Sjhb * the mappings in a hash table.
436175164Sjhb */
437175164Sjhbstatic void
438175164Sjhbshm_dict_init(void *arg)
439175164Sjhb{
440175164Sjhb
441175164Sjhb	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
442175164Sjhb	sx_init(&shm_dict_lock, "shm dictionary");
443175164Sjhb	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
444175164Sjhb}
445175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
446175164Sjhb
447175164Sjhbstatic struct shmfd *
448175164Sjhbshm_lookup(char *path, Fnv32_t fnv)
449175164Sjhb{
450175164Sjhb	struct shm_mapping *map;
451175164Sjhb
452175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
453175164Sjhb		if (map->sm_fnv != fnv)
454175164Sjhb			continue;
455175164Sjhb		if (strcmp(map->sm_path, path) == 0)
456175164Sjhb			return (map->sm_shmfd);
457175164Sjhb	}
458175164Sjhb
459175164Sjhb	return (NULL);
460175164Sjhb}
461175164Sjhb
462175164Sjhbstatic void
463175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
464175164Sjhb{
465175164Sjhb	struct shm_mapping *map;
466175164Sjhb
467175164Sjhb	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
468175164Sjhb	map->sm_path = path;
469175164Sjhb	map->sm_fnv = fnv;
470175164Sjhb	map->sm_shmfd = shm_hold(shmfd);
471175164Sjhb	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
472175164Sjhb}
473175164Sjhb
474175164Sjhbstatic int
475175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
476175164Sjhb{
477175164Sjhb	struct shm_mapping *map;
478175164Sjhb	int error;
479175164Sjhb
480175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
481175164Sjhb		if (map->sm_fnv != fnv)
482175164Sjhb			continue;
483175164Sjhb		if (strcmp(map->sm_path, path) == 0) {
484175164Sjhb#ifdef MAC
485175164Sjhb			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
486175164Sjhb			if (error)
487175164Sjhb				return (error);
488175164Sjhb#endif
489175164Sjhb			error = shm_access(map->sm_shmfd, ucred,
490175164Sjhb			    FREAD | FWRITE);
491175164Sjhb			if (error)
492175164Sjhb				return (error);
493175164Sjhb			LIST_REMOVE(map, sm_link);
494175164Sjhb			shm_drop(map->sm_shmfd);
495175164Sjhb			free(map->sm_path, M_SHMFD);
496175164Sjhb			free(map, M_SHMFD);
497175164Sjhb			return (0);
498175164Sjhb		}
499175164Sjhb	}
500175164Sjhb
501175164Sjhb	return (ENOENT);
502175164Sjhb}
503175164Sjhb
504175164Sjhb/* System calls. */
505175164Sjhbint
506225617Skmacysys_shm_open(struct thread *td, struct shm_open_args *uap)
507175164Sjhb{
508175164Sjhb	struct filedesc *fdp;
509175164Sjhb	struct shmfd *shmfd;
510175164Sjhb	struct file *fp;
511175164Sjhb	char *path;
512175164Sjhb	Fnv32_t fnv;
513175164Sjhb	mode_t cmode;
514175164Sjhb	int fd, error;
515175164Sjhb
516223692Sjonathan#ifdef CAPABILITY_MODE
517223692Sjonathan	/*
518223692Sjonathan	 * shm_open(2) is only allowed for anonymous objects.
519223692Sjonathan	 */
520223692Sjonathan	if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON))
521223692Sjonathan		return (ECAPMODE);
522223692Sjonathan#endif
523223692Sjonathan
524175164Sjhb	if ((uap->flags & O_ACCMODE) != O_RDONLY &&
525175164Sjhb	    (uap->flags & O_ACCMODE) != O_RDWR)
526175164Sjhb		return (EINVAL);
527175164Sjhb
528175164Sjhb	if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
529175164Sjhb		return (EINVAL);
530175164Sjhb
531175164Sjhb	fdp = td->td_proc->p_fd;
532175164Sjhb	cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
533175164Sjhb
534220245Skib	error = falloc(td, &fp, &fd, 0);
535175164Sjhb	if (error)
536175164Sjhb		return (error);
537175164Sjhb
538175164Sjhb	/* A SHM_ANON path pointer creates an anonymous object. */
539175164Sjhb	if (uap->path == SHM_ANON) {
540175164Sjhb		/* A read-only anonymous object is pointless. */
541175164Sjhb		if ((uap->flags & O_ACCMODE) == O_RDONLY) {
542175164Sjhb			fdclose(fdp, fp, fd, td);
543175164Sjhb			fdrop(fp, td);
544175164Sjhb			return (EINVAL);
545175164Sjhb		}
546175164Sjhb		shmfd = shm_alloc(td->td_ucred, cmode);
547175164Sjhb	} else {
548175164Sjhb		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
549175164Sjhb		error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
550175164Sjhb
551175164Sjhb		/* Require paths to start with a '/' character. */
552175164Sjhb		if (error == 0 && path[0] != '/')
553175164Sjhb			error = EINVAL;
554175164Sjhb		if (error) {
555175164Sjhb			fdclose(fdp, fp, fd, td);
556175164Sjhb			fdrop(fp, td);
557175164Sjhb			free(path, M_SHMFD);
558175164Sjhb			return (error);
559175164Sjhb		}
560175164Sjhb
561175164Sjhb		fnv = fnv_32_str(path, FNV1_32_INIT);
562175164Sjhb		sx_xlock(&shm_dict_lock);
563175164Sjhb		shmfd = shm_lookup(path, fnv);
564175164Sjhb		if (shmfd == NULL) {
565175164Sjhb			/* Object does not yet exist, create it if requested. */
566175164Sjhb			if (uap->flags & O_CREAT) {
567225344Srwatson#ifdef MAC
568225344Srwatson				error = mac_posixshm_check_create(td->td_ucred,
569225344Srwatson				    path);
570225344Srwatson				if (error == 0) {
571225344Srwatson#endif
572225344Srwatson					shmfd = shm_alloc(td->td_ucred, cmode);
573225344Srwatson					shm_insert(path, fnv, shmfd);
574225344Srwatson#ifdef MAC
575225344Srwatson				}
576225344Srwatson#endif
577175164Sjhb			} else {
578175164Sjhb				free(path, M_SHMFD);
579175164Sjhb				error = ENOENT;
580175164Sjhb			}
581175164Sjhb		} else {
582175164Sjhb			/*
583175164Sjhb			 * Object already exists, obtain a new
584175164Sjhb			 * reference if requested and permitted.
585175164Sjhb			 */
586175164Sjhb			free(path, M_SHMFD);
587175164Sjhb			if ((uap->flags & (O_CREAT | O_EXCL)) ==
588175164Sjhb			    (O_CREAT | O_EXCL))
589175164Sjhb				error = EEXIST;
590175164Sjhb			else {
591175164Sjhb#ifdef MAC
592175164Sjhb				error = mac_posixshm_check_open(td->td_ucred,
593225344Srwatson				    shmfd, FFLAGS(uap->flags & O_ACCMODE));
594175164Sjhb				if (error == 0)
595175164Sjhb#endif
596175164Sjhb				error = shm_access(shmfd, td->td_ucred,
597175164Sjhb				    FFLAGS(uap->flags & O_ACCMODE));
598175164Sjhb			}
599175164Sjhb
600175164Sjhb			/*
601175164Sjhb			 * Truncate the file back to zero length if
602175164Sjhb			 * O_TRUNC was specified and the object was
603175164Sjhb			 * opened with read/write.
604175164Sjhb			 */
605175164Sjhb			if (error == 0 &&
606175164Sjhb			    (uap->flags & (O_ACCMODE | O_TRUNC)) ==
607175164Sjhb			    (O_RDWR | O_TRUNC)) {
608175164Sjhb#ifdef MAC
609175164Sjhb				error = mac_posixshm_check_truncate(
610175164Sjhb					td->td_ucred, fp->f_cred, shmfd);
611175164Sjhb				if (error == 0)
612175164Sjhb#endif
613175164Sjhb					shm_dotruncate(shmfd, 0);
614175164Sjhb			}
615175164Sjhb			if (error == 0)
616175164Sjhb				shm_hold(shmfd);
617175164Sjhb		}
618175164Sjhb		sx_xunlock(&shm_dict_lock);
619175164Sjhb
620175164Sjhb		if (error) {
621175164Sjhb			fdclose(fdp, fp, fd, td);
622175164Sjhb			fdrop(fp, td);
623175164Sjhb			return (error);
624175164Sjhb		}
625175164Sjhb	}
626175164Sjhb
627175164Sjhb	finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
628175164Sjhb
629175164Sjhb	FILEDESC_XLOCK(fdp);
630175164Sjhb	if (fdp->fd_ofiles[fd] == fp)
631175164Sjhb		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
632175164Sjhb	FILEDESC_XUNLOCK(fdp);
633175164Sjhb	td->td_retval[0] = fd;
634175164Sjhb	fdrop(fp, td);
635175164Sjhb
636175164Sjhb	return (0);
637175164Sjhb}
638175164Sjhb
639175164Sjhbint
640225617Skmacysys_shm_unlink(struct thread *td, struct shm_unlink_args *uap)
641175164Sjhb{
642175164Sjhb	char *path;
643175164Sjhb	Fnv32_t fnv;
644175164Sjhb	int error;
645175164Sjhb
646175164Sjhb	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
647175164Sjhb	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
648175164Sjhb	if (error) {
649175164Sjhb		free(path, M_TEMP);
650175164Sjhb		return (error);
651175164Sjhb	}
652175164Sjhb
653175164Sjhb	fnv = fnv_32_str(path, FNV1_32_INIT);
654175164Sjhb	sx_xlock(&shm_dict_lock);
655175164Sjhb	error = shm_remove(path, fnv, td->td_ucred);
656175164Sjhb	sx_xunlock(&shm_dict_lock);
657175164Sjhb	free(path, M_TEMP);
658175164Sjhb
659175164Sjhb	return (error);
660175164Sjhb}
661175164Sjhb
662175164Sjhb/*
663175164Sjhb * mmap() helper to validate mmap() requests against shm object state
664175164Sjhb * and give mmap() the vm_object to use for the mapping.
665175164Sjhb */
666175164Sjhbint
667175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
668175164Sjhb    vm_object_t *obj)
669175164Sjhb{
670175164Sjhb
671175164Sjhb	/*
672175164Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
673175164Sjhb	 * sign errors.  It should be fixed.
674175164Sjhb	 */
675185533Skan	if (foff >= shmfd->shm_size ||
676185533Skan	    foff + objsize > round_page(shmfd->shm_size))
677175164Sjhb		return (EINVAL);
678175164Sjhb
679175164Sjhb	mtx_lock(&shm_timestamp_lock);
680175164Sjhb	vfs_timestamp(&shmfd->shm_atime);
681175164Sjhb	mtx_unlock(&shm_timestamp_lock);
682175164Sjhb	vm_object_reference(shmfd->shm_object);
683175164Sjhb	*obj = shmfd->shm_object;
684175164Sjhb	return (0);
685175164Sjhb}
686224914Skib
687224914Skibstatic int
688224914Skibshm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
689224914Skib    struct thread *td)
690224914Skib{
691224914Skib	struct shmfd *shmfd;
692224914Skib	int error;
693224914Skib
694224914Skib	error = 0;
695224914Skib	shmfd = fp->f_data;
696224914Skib	mtx_lock(&shm_timestamp_lock);
697224914Skib	/*
698224914Skib	 * SUSv4 says that x bits of permission need not be affected.
699224914Skib	 * Be consistent with our shm_open there.
700224914Skib	 */
701224914Skib#ifdef MAC
702224914Skib	error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
703224914Skib	if (error != 0)
704224914Skib		goto out;
705224914Skib#endif
706224914Skib	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid,
707224914Skib	    shmfd->shm_gid, VADMIN, active_cred, NULL);
708224914Skib	if (error != 0)
709224914Skib		goto out;
710224914Skib	shmfd->shm_mode = mode & ACCESSPERMS;
711224914Skibout:
712224914Skib	mtx_unlock(&shm_timestamp_lock);
713224914Skib	return (error);
714224914Skib}
715224914Skib
716224914Skibstatic int
717224914Skibshm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
718224914Skib    struct thread *td)
719224914Skib{
720224914Skib	struct shmfd *shmfd;
721224914Skib	int error;
722224914Skib
723224935Skib	error = 0;
724224914Skib	shmfd = fp->f_data;
725224914Skib	mtx_lock(&shm_timestamp_lock);
726224914Skib#ifdef MAC
727224914Skib	error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
728224914Skib	if (error != 0)
729224914Skib		goto out;
730224914Skib#endif
731224914Skib	if (uid == (uid_t)-1)
732224914Skib		uid = shmfd->shm_uid;
733224914Skib	if (gid == (gid_t)-1)
734224914Skib                 gid = shmfd->shm_gid;
735224914Skib	if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
736224914Skib	    (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
737224914Skib	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
738224914Skib		goto out;
739224914Skib	shmfd->shm_uid = uid;
740224914Skib	shmfd->shm_gid = gid;
741224914Skibout:
742224914Skib	mtx_unlock(&shm_timestamp_lock);
743224914Skib	return (error);
744224914Skib}
745228509Sjhb
746228509Sjhb/*
747228509Sjhb * Helper routines to allow the backing object of a shared memory file
748228509Sjhb * descriptor to be mapped in the kernel.
749228509Sjhb */
750228509Sjhbint
751228509Sjhbshm_map(struct file *fp, size_t size, off_t offset, void **memp)
752228509Sjhb{
753228509Sjhb	struct shmfd *shmfd;
754228509Sjhb	vm_offset_t kva, ofs;
755228509Sjhb	vm_object_t obj;
756228509Sjhb	int rv;
757228509Sjhb
758228509Sjhb	if (fp->f_type != DTYPE_SHM)
759228509Sjhb		return (EINVAL);
760228509Sjhb	shmfd = fp->f_data;
761228509Sjhb	obj = shmfd->shm_object;
762228509Sjhb	VM_OBJECT_LOCK(obj);
763228509Sjhb	/*
764228509Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
765228509Sjhb	 * sign errors.  It should be fixed.
766228509Sjhb	 */
767228509Sjhb	if (offset >= shmfd->shm_size ||
768228509Sjhb	    offset + size > round_page(shmfd->shm_size)) {
769228509Sjhb		VM_OBJECT_UNLOCK(obj);
770228509Sjhb		return (EINVAL);
771228509Sjhb	}
772228509Sjhb
773228509Sjhb	shmfd->shm_kmappings++;
774228509Sjhb	vm_object_reference_locked(obj);
775228509Sjhb	VM_OBJECT_UNLOCK(obj);
776228509Sjhb
777228509Sjhb	/* Map the object into the kernel_map and wire it. */
778228509Sjhb	kva = vm_map_min(kernel_map);
779228509Sjhb	ofs = offset & PAGE_MASK;
780228509Sjhb	offset = trunc_page(offset);
781228509Sjhb	size = round_page(size + ofs);
782228509Sjhb	rv = vm_map_find(kernel_map, obj, offset, &kva, size,
783228509Sjhb	    VMFS_ALIGNED_SPACE, VM_PROT_READ | VM_PROT_WRITE,
784228509Sjhb	    VM_PROT_READ | VM_PROT_WRITE, 0);
785228509Sjhb	if (rv == KERN_SUCCESS) {
786228509Sjhb		rv = vm_map_wire(kernel_map, kva, kva + size,
787228509Sjhb		    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
788228509Sjhb		if (rv == KERN_SUCCESS) {
789228509Sjhb			*memp = (void *)(kva + ofs);
790228509Sjhb			return (0);
791228509Sjhb		}
792228509Sjhb		vm_map_remove(kernel_map, kva, kva + size);
793228509Sjhb	} else
794228509Sjhb		vm_object_deallocate(obj);
795228509Sjhb
796228509Sjhb	/* On failure, drop our mapping reference. */
797228509Sjhb	VM_OBJECT_LOCK(obj);
798228509Sjhb	shmfd->shm_kmappings--;
799228509Sjhb	VM_OBJECT_UNLOCK(obj);
800228509Sjhb
801228533Sjhb	return (vm_mmap_to_errno(rv));
802228509Sjhb}
803228509Sjhb
804228509Sjhb/*
805228509Sjhb * We require the caller to unmap the entire entry.  This allows us to
806228509Sjhb * safely decrement shm_kmappings when a mapping is removed.
807228509Sjhb */
808228509Sjhbint
809228509Sjhbshm_unmap(struct file *fp, void *mem, size_t size)
810228509Sjhb{
811228509Sjhb	struct shmfd *shmfd;
812228509Sjhb	vm_map_entry_t entry;
813228509Sjhb	vm_offset_t kva, ofs;
814228509Sjhb	vm_object_t obj;
815228509Sjhb	vm_pindex_t pindex;
816228509Sjhb	vm_prot_t prot;
817228509Sjhb	boolean_t wired;
818228509Sjhb	vm_map_t map;
819228509Sjhb	int rv;
820228509Sjhb
821228509Sjhb	if (fp->f_type != DTYPE_SHM)
822228509Sjhb		return (EINVAL);
823228509Sjhb	shmfd = fp->f_data;
824228509Sjhb	kva = (vm_offset_t)mem;
825228509Sjhb	ofs = kva & PAGE_MASK;
826228509Sjhb	kva = trunc_page(kva);
827228509Sjhb	size = round_page(size + ofs);
828228509Sjhb	map = kernel_map;
829228509Sjhb	rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry,
830228509Sjhb	    &obj, &pindex, &prot, &wired);
831228509Sjhb	if (rv != KERN_SUCCESS)
832228509Sjhb		return (EINVAL);
833228509Sjhb	if (entry->start != kva || entry->end != kva + size) {
834228509Sjhb		vm_map_lookup_done(map, entry);
835228509Sjhb		return (EINVAL);
836228509Sjhb	}
837228509Sjhb	vm_map_lookup_done(map, entry);
838228509Sjhb	if (obj != shmfd->shm_object)
839228509Sjhb		return (EINVAL);
840228509Sjhb	vm_map_remove(map, kva, kva + size);
841228509Sjhb	VM_OBJECT_LOCK(obj);
842228509Sjhb	KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped"));
843228509Sjhb	shmfd->shm_kmappings--;
844228509Sjhb	VM_OBJECT_UNLOCK(obj);
845228509Sjhb	return (0);
846228509Sjhb}
847