uipc_shm.c revision 228509
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 228509 2011-12-14 22:22:19Z jhb $");
53175164Sjhb
54223692Sjonathan#include "opt_capsicum.h"
55223692Sjonathan
56175164Sjhb#include <sys/param.h>
57223692Sjonathan#include <sys/capability.h>
58175164Sjhb#include <sys/fcntl.h>
59175164Sjhb#include <sys/file.h>
60175164Sjhb#include <sys/filedesc.h>
61175164Sjhb#include <sys/fnv_hash.h>
62175164Sjhb#include <sys/kernel.h>
63175164Sjhb#include <sys/lock.h>
64175164Sjhb#include <sys/malloc.h>
65175164Sjhb#include <sys/mman.h>
66175164Sjhb#include <sys/mutex.h>
67224914Skib#include <sys/priv.h>
68175164Sjhb#include <sys/proc.h>
69175164Sjhb#include <sys/refcount.h>
70175164Sjhb#include <sys/resourcevar.h>
71175164Sjhb#include <sys/stat.h>
72175164Sjhb#include <sys/sysctl.h>
73175164Sjhb#include <sys/sysproto.h>
74175164Sjhb#include <sys/systm.h>
75175164Sjhb#include <sys/sx.h>
76175164Sjhb#include <sys/time.h>
77175164Sjhb#include <sys/vnode.h>
78175164Sjhb
79175164Sjhb#include <security/mac/mac_framework.h>
80175164Sjhb
81175164Sjhb#include <vm/vm.h>
82175164Sjhb#include <vm/vm_param.h>
83175164Sjhb#include <vm/pmap.h>
84175164Sjhb#include <vm/vm_map.h>
85228509Sjhb#include <vm/vm_kern.h>
86175164Sjhb#include <vm/vm_object.h>
87175164Sjhb#include <vm/vm_page.h>
88175164Sjhb#include <vm/vm_pager.h>
89175164Sjhb#include <vm/swap_pager.h>
90175164Sjhb
91175164Sjhbstruct shm_mapping {
92175164Sjhb	char		*sm_path;
93175164Sjhb	Fnv32_t		sm_fnv;
94175164Sjhb	struct shmfd	*sm_shmfd;
95175164Sjhb	LIST_ENTRY(shm_mapping) sm_link;
96175164Sjhb};
97175164Sjhb
98175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
99175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary;
100175164Sjhbstatic struct sx shm_dict_lock;
101175164Sjhbstatic struct mtx shm_timestamp_lock;
102175164Sjhbstatic u_long shm_hash;
103175164Sjhb
104175164Sjhb#define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
105175164Sjhb
106175164Sjhbstatic int	shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
107175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
108175164Sjhbstatic void	shm_dict_init(void *arg);
109175164Sjhbstatic void	shm_drop(struct shmfd *shmfd);
110175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd);
111175164Sjhbstatic void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
112175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
113175164Sjhbstatic int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
114194766Skibstatic int	shm_dotruncate(struct shmfd *shmfd, off_t length);
115175164Sjhb
116175164Sjhbstatic fo_rdwr_t	shm_read;
117175164Sjhbstatic fo_rdwr_t	shm_write;
118175164Sjhbstatic fo_truncate_t	shm_truncate;
119175164Sjhbstatic fo_ioctl_t	shm_ioctl;
120175164Sjhbstatic fo_poll_t	shm_poll;
121175164Sjhbstatic fo_kqfilter_t	shm_kqfilter;
122175164Sjhbstatic fo_stat_t	shm_stat;
123175164Sjhbstatic fo_close_t	shm_close;
124224914Skibstatic fo_chmod_t	shm_chmod;
125224914Skibstatic fo_chown_t	shm_chown;
126175164Sjhb
127175164Sjhb/* File descriptor operations. */
128175164Sjhbstatic struct fileops shm_ops = {
129175164Sjhb	.fo_read = shm_read,
130175164Sjhb	.fo_write = shm_write,
131175164Sjhb	.fo_truncate = shm_truncate,
132175164Sjhb	.fo_ioctl = shm_ioctl,
133175164Sjhb	.fo_poll = shm_poll,
134175164Sjhb	.fo_kqfilter = shm_kqfilter,
135175164Sjhb	.fo_stat = shm_stat,
136175164Sjhb	.fo_close = shm_close,
137224914Skib	.fo_chmod = shm_chmod,
138224914Skib	.fo_chown = shm_chown,
139175164Sjhb	.fo_flags = DFLAG_PASSABLE
140175164Sjhb};
141175164Sjhb
142175164SjhbFEATURE(posix_shm, "POSIX shared memory");
143175164Sjhb
144175164Sjhbstatic int
145175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
146175164Sjhb    int flags, struct thread *td)
147175164Sjhb{
148175164Sjhb
149175164Sjhb	return (EOPNOTSUPP);
150175164Sjhb}
151175164Sjhb
152175164Sjhbstatic int
153175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
154175164Sjhb    int flags, struct thread *td)
155175164Sjhb{
156175164Sjhb
157175164Sjhb	return (EOPNOTSUPP);
158175164Sjhb}
159175164Sjhb
160175164Sjhbstatic int
161175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
162175164Sjhb    struct thread *td)
163175164Sjhb{
164175164Sjhb	struct shmfd *shmfd;
165175164Sjhb#ifdef MAC
166175164Sjhb	int error;
167175164Sjhb#endif
168175164Sjhb
169175164Sjhb	shmfd = fp->f_data;
170175164Sjhb#ifdef MAC
171175164Sjhb	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
172175164Sjhb	if (error)
173175164Sjhb		return (error);
174175164Sjhb#endif
175194766Skib	return (shm_dotruncate(shmfd, length));
176175164Sjhb}
177175164Sjhb
178175164Sjhbstatic int
179175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data,
180175164Sjhb    struct ucred *active_cred, struct thread *td)
181175164Sjhb{
182175164Sjhb
183175164Sjhb	return (EOPNOTSUPP);
184175164Sjhb}
185175164Sjhb
186175164Sjhbstatic int
187175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred,
188175164Sjhb    struct thread *td)
189175164Sjhb{
190175164Sjhb
191175164Sjhb	return (EOPNOTSUPP);
192175164Sjhb}
193175164Sjhb
194175164Sjhbstatic int
195175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn)
196175164Sjhb{
197175164Sjhb
198175164Sjhb	return (EOPNOTSUPP);
199175164Sjhb}
200175164Sjhb
201175164Sjhbstatic int
202175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
203175164Sjhb    struct thread *td)
204175164Sjhb{
205175164Sjhb	struct shmfd *shmfd;
206175164Sjhb#ifdef MAC
207175164Sjhb	int error;
208175164Sjhb#endif
209175164Sjhb
210175164Sjhb	shmfd = fp->f_data;
211175164Sjhb
212175164Sjhb#ifdef MAC
213175164Sjhb	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
214175164Sjhb	if (error)
215175164Sjhb		return (error);
216175164Sjhb#endif
217175164Sjhb
218175164Sjhb	/*
219175164Sjhb	 * Attempt to return sanish values for fstat() on a memory file
220175164Sjhb	 * descriptor.
221175164Sjhb	 */
222175164Sjhb	bzero(sb, sizeof(*sb));
223175164Sjhb	sb->st_blksize = PAGE_SIZE;
224175164Sjhb	sb->st_size = shmfd->shm_size;
225175164Sjhb	sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
226224914Skib	mtx_lock(&shm_timestamp_lock);
227205792Sed	sb->st_atim = shmfd->shm_atime;
228205792Sed	sb->st_ctim = shmfd->shm_ctime;
229205792Sed	sb->st_mtim = shmfd->shm_mtime;
230224914Skib	sb->st_birthtim = shmfd->shm_birthtime;
231224914Skib	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
232175164Sjhb	sb->st_uid = shmfd->shm_uid;
233175164Sjhb	sb->st_gid = shmfd->shm_gid;
234224914Skib	mtx_unlock(&shm_timestamp_lock);
235175164Sjhb
236175164Sjhb	return (0);
237175164Sjhb}
238175164Sjhb
239175164Sjhbstatic int
240175164Sjhbshm_close(struct file *fp, struct thread *td)
241175164Sjhb{
242175164Sjhb	struct shmfd *shmfd;
243175164Sjhb
244175164Sjhb	shmfd = fp->f_data;
245175164Sjhb	fp->f_data = NULL;
246175164Sjhb	shm_drop(shmfd);
247175164Sjhb
248175164Sjhb	return (0);
249175164Sjhb}
250175164Sjhb
251194766Skibstatic int
252175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length)
253175164Sjhb{
254175164Sjhb	vm_object_t object;
255175164Sjhb	vm_page_t m;
256175164Sjhb	vm_pindex_t nobjsize;
257194766Skib	vm_ooffset_t delta;
258175164Sjhb
259175164Sjhb	object = shmfd->shm_object;
260175164Sjhb	VM_OBJECT_LOCK(object);
261175164Sjhb	if (length == shmfd->shm_size) {
262175164Sjhb		VM_OBJECT_UNLOCK(object);
263194766Skib		return (0);
264175164Sjhb	}
265175164Sjhb	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
266175164Sjhb
267175164Sjhb	/* Are we shrinking?  If so, trim the end. */
268175164Sjhb	if (length < shmfd->shm_size) {
269228509Sjhb		/*
270228509Sjhb		 * Disallow any requests to shrink the size if this
271228509Sjhb		 * object is mapped into the kernel.
272228509Sjhb		 */
273228509Sjhb		if (shmfd->shm_kmappings > 0) {
274228509Sjhb			VM_OBJECT_UNLOCK(object);
275228509Sjhb			return (EBUSY);
276228509Sjhb		}
277194766Skib		delta = ptoa(object->size - nobjsize);
278194766Skib
279175164Sjhb		/* Toss in memory pages. */
280175164Sjhb		if (nobjsize < object->size)
281175164Sjhb			vm_object_page_remove(object, nobjsize, object->size,
282223677Salc			    0);
283175164Sjhb
284175164Sjhb		/* Toss pages from swap. */
285175164Sjhb		if (object->type == OBJT_SWAP)
286194766Skib			swap_pager_freespace(object, nobjsize, delta);
287175164Sjhb
288194766Skib		/* Free the swap accounted for shm */
289216128Strasz		swap_release_by_cred(delta, object->cred);
290194766Skib		object->charge -= delta;
291194766Skib
292175164Sjhb		/*
293175164Sjhb		 * If the last page is partially mapped, then zero out
294175164Sjhb		 * the garbage at the end of the page.  See comments
295193303Salc		 * in vnode_pager_setsize() for more details.
296175164Sjhb		 *
297175164Sjhb		 * XXXJHB: This handles in memory pages, but what about
298175164Sjhb		 * a page swapped out to disk?
299175164Sjhb		 */
300175164Sjhb		if ((length & PAGE_MASK) &&
301175164Sjhb		    (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
302175164Sjhb		    m->valid != 0) {
303175164Sjhb			int base = (int)length & PAGE_MASK;
304175164Sjhb			int size = PAGE_SIZE - base;
305175164Sjhb
306175164Sjhb			pmap_zero_page_area(m, base, size);
307193303Salc
308193303Salc			/*
309193303Salc			 * Update the valid bits to reflect the blocks that
310193303Salc			 * have been zeroed.  Some of these valid bits may
311193303Salc			 * have already been set.
312193303Salc			 */
313228156Skib			vm_page_set_valid_range(m, base, size);
314193303Salc
315193303Salc			/*
316193303Salc			 * Round "base" to the next block boundary so that the
317193303Salc			 * dirty bit for a partially zeroed block is not
318193303Salc			 * cleared.
319193303Salc			 */
320193303Salc			base = roundup2(base, DEV_BSIZE);
321193303Salc
322193303Salc			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
323176075Salc		} else if ((length & PAGE_MASK) &&
324176075Salc		    __predict_false(object->cache != NULL)) {
325176075Salc			vm_page_cache_free(object, OFF_TO_IDX(length),
326176075Salc			    nobjsize);
327175164Sjhb		}
328194766Skib	} else {
329194766Skib
330194766Skib		/* Attempt to reserve the swap */
331194766Skib		delta = ptoa(nobjsize - object->size);
332216128Strasz		if (!swap_reserve_by_cred(delta, object->cred)) {
333194766Skib			VM_OBJECT_UNLOCK(object);
334194766Skib			return (ENOMEM);
335194766Skib		}
336194766Skib		object->charge += delta;
337175164Sjhb	}
338175164Sjhb	shmfd->shm_size = length;
339175164Sjhb	mtx_lock(&shm_timestamp_lock);
340175164Sjhb	vfs_timestamp(&shmfd->shm_ctime);
341175164Sjhb	shmfd->shm_mtime = shmfd->shm_ctime;
342175164Sjhb	mtx_unlock(&shm_timestamp_lock);
343175164Sjhb	object->size = nobjsize;
344175164Sjhb	VM_OBJECT_UNLOCK(object);
345194766Skib	return (0);
346175164Sjhb}
347175164Sjhb
348175164Sjhb/*
349175164Sjhb * shmfd object management including creation and reference counting
350175164Sjhb * routines.
351175164Sjhb */
352175164Sjhbstatic struct shmfd *
353175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode)
354175164Sjhb{
355175164Sjhb	struct shmfd *shmfd;
356175164Sjhb
357175164Sjhb	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
358175164Sjhb	shmfd->shm_size = 0;
359175164Sjhb	shmfd->shm_uid = ucred->cr_uid;
360175164Sjhb	shmfd->shm_gid = ucred->cr_gid;
361175164Sjhb	shmfd->shm_mode = mode;
362175164Sjhb	shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
363194766Skib	    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
364175164Sjhb	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
365178181Salc	VM_OBJECT_LOCK(shmfd->shm_object);
366178181Salc	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
367178181Salc	vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
368178181Salc	VM_OBJECT_UNLOCK(shmfd->shm_object);
369175164Sjhb	vfs_timestamp(&shmfd->shm_birthtime);
370175164Sjhb	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
371175164Sjhb	    shmfd->shm_birthtime;
372175164Sjhb	refcount_init(&shmfd->shm_refs, 1);
373175164Sjhb#ifdef MAC
374175164Sjhb	mac_posixshm_init(shmfd);
375175164Sjhb	mac_posixshm_create(ucred, shmfd);
376175164Sjhb#endif
377175164Sjhb
378175164Sjhb	return (shmfd);
379175164Sjhb}
380175164Sjhb
381175164Sjhbstatic struct shmfd *
382175164Sjhbshm_hold(struct shmfd *shmfd)
383175164Sjhb{
384175164Sjhb
385175164Sjhb	refcount_acquire(&shmfd->shm_refs);
386175164Sjhb	return (shmfd);
387175164Sjhb}
388175164Sjhb
389175164Sjhbstatic void
390175164Sjhbshm_drop(struct shmfd *shmfd)
391175164Sjhb{
392175164Sjhb
393175164Sjhb	if (refcount_release(&shmfd->shm_refs)) {
394175164Sjhb#ifdef MAC
395175164Sjhb		mac_posixshm_destroy(shmfd);
396175164Sjhb#endif
397175164Sjhb		vm_object_deallocate(shmfd->shm_object);
398175164Sjhb		free(shmfd, M_SHMFD);
399175164Sjhb	}
400175164Sjhb}
401175164Sjhb
402175164Sjhb/*
403175164Sjhb * Determine if the credentials have sufficient permissions for a
404175164Sjhb * specified combination of FREAD and FWRITE.
405175164Sjhb */
406175164Sjhbstatic int
407175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
408175164Sjhb{
409184413Strasz	accmode_t accmode;
410224914Skib	int error;
411175164Sjhb
412184413Strasz	accmode = 0;
413175164Sjhb	if (flags & FREAD)
414184413Strasz		accmode |= VREAD;
415175164Sjhb	if (flags & FWRITE)
416184413Strasz		accmode |= VWRITE;
417224914Skib	mtx_lock(&shm_timestamp_lock);
418224914Skib	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
419224914Skib	    accmode, ucred, NULL);
420224914Skib	mtx_unlock(&shm_timestamp_lock);
421224914Skib	return (error);
422175164Sjhb}
423175164Sjhb
424175164Sjhb/*
425175164Sjhb * Dictionary management.  We maintain an in-kernel dictionary to map
426175164Sjhb * paths to shmfd objects.  We use the FNV hash on the path to store
427175164Sjhb * the mappings in a hash table.
428175164Sjhb */
429175164Sjhbstatic void
430175164Sjhbshm_dict_init(void *arg)
431175164Sjhb{
432175164Sjhb
433175164Sjhb	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
434175164Sjhb	sx_init(&shm_dict_lock, "shm dictionary");
435175164Sjhb	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
436175164Sjhb}
437175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
438175164Sjhb
439175164Sjhbstatic struct shmfd *
440175164Sjhbshm_lookup(char *path, Fnv32_t fnv)
441175164Sjhb{
442175164Sjhb	struct shm_mapping *map;
443175164Sjhb
444175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
445175164Sjhb		if (map->sm_fnv != fnv)
446175164Sjhb			continue;
447175164Sjhb		if (strcmp(map->sm_path, path) == 0)
448175164Sjhb			return (map->sm_shmfd);
449175164Sjhb	}
450175164Sjhb
451175164Sjhb	return (NULL);
452175164Sjhb}
453175164Sjhb
454175164Sjhbstatic void
455175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
456175164Sjhb{
457175164Sjhb	struct shm_mapping *map;
458175164Sjhb
459175164Sjhb	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
460175164Sjhb	map->sm_path = path;
461175164Sjhb	map->sm_fnv = fnv;
462175164Sjhb	map->sm_shmfd = shm_hold(shmfd);
463175164Sjhb	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
464175164Sjhb}
465175164Sjhb
466175164Sjhbstatic int
467175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
468175164Sjhb{
469175164Sjhb	struct shm_mapping *map;
470175164Sjhb	int error;
471175164Sjhb
472175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
473175164Sjhb		if (map->sm_fnv != fnv)
474175164Sjhb			continue;
475175164Sjhb		if (strcmp(map->sm_path, path) == 0) {
476175164Sjhb#ifdef MAC
477175164Sjhb			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
478175164Sjhb			if (error)
479175164Sjhb				return (error);
480175164Sjhb#endif
481175164Sjhb			error = shm_access(map->sm_shmfd, ucred,
482175164Sjhb			    FREAD | FWRITE);
483175164Sjhb			if (error)
484175164Sjhb				return (error);
485175164Sjhb			LIST_REMOVE(map, sm_link);
486175164Sjhb			shm_drop(map->sm_shmfd);
487175164Sjhb			free(map->sm_path, M_SHMFD);
488175164Sjhb			free(map, M_SHMFD);
489175164Sjhb			return (0);
490175164Sjhb		}
491175164Sjhb	}
492175164Sjhb
493175164Sjhb	return (ENOENT);
494175164Sjhb}
495175164Sjhb
496175164Sjhb/* System calls. */
497175164Sjhbint
498225617Skmacysys_shm_open(struct thread *td, struct shm_open_args *uap)
499175164Sjhb{
500175164Sjhb	struct filedesc *fdp;
501175164Sjhb	struct shmfd *shmfd;
502175164Sjhb	struct file *fp;
503175164Sjhb	char *path;
504175164Sjhb	Fnv32_t fnv;
505175164Sjhb	mode_t cmode;
506175164Sjhb	int fd, error;
507175164Sjhb
508223692Sjonathan#ifdef CAPABILITY_MODE
509223692Sjonathan	/*
510223692Sjonathan	 * shm_open(2) is only allowed for anonymous objects.
511223692Sjonathan	 */
512223692Sjonathan	if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON))
513223692Sjonathan		return (ECAPMODE);
514223692Sjonathan#endif
515223692Sjonathan
516175164Sjhb	if ((uap->flags & O_ACCMODE) != O_RDONLY &&
517175164Sjhb	    (uap->flags & O_ACCMODE) != O_RDWR)
518175164Sjhb		return (EINVAL);
519175164Sjhb
520175164Sjhb	if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
521175164Sjhb		return (EINVAL);
522175164Sjhb
523175164Sjhb	fdp = td->td_proc->p_fd;
524175164Sjhb	cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
525175164Sjhb
526220245Skib	error = falloc(td, &fp, &fd, 0);
527175164Sjhb	if (error)
528175164Sjhb		return (error);
529175164Sjhb
530175164Sjhb	/* A SHM_ANON path pointer creates an anonymous object. */
531175164Sjhb	if (uap->path == SHM_ANON) {
532175164Sjhb		/* A read-only anonymous object is pointless. */
533175164Sjhb		if ((uap->flags & O_ACCMODE) == O_RDONLY) {
534175164Sjhb			fdclose(fdp, fp, fd, td);
535175164Sjhb			fdrop(fp, td);
536175164Sjhb			return (EINVAL);
537175164Sjhb		}
538175164Sjhb		shmfd = shm_alloc(td->td_ucred, cmode);
539175164Sjhb	} else {
540175164Sjhb		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
541175164Sjhb		error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
542175164Sjhb
543175164Sjhb		/* Require paths to start with a '/' character. */
544175164Sjhb		if (error == 0 && path[0] != '/')
545175164Sjhb			error = EINVAL;
546175164Sjhb		if (error) {
547175164Sjhb			fdclose(fdp, fp, fd, td);
548175164Sjhb			fdrop(fp, td);
549175164Sjhb			free(path, M_SHMFD);
550175164Sjhb			return (error);
551175164Sjhb		}
552175164Sjhb
553175164Sjhb		fnv = fnv_32_str(path, FNV1_32_INIT);
554175164Sjhb		sx_xlock(&shm_dict_lock);
555175164Sjhb		shmfd = shm_lookup(path, fnv);
556175164Sjhb		if (shmfd == NULL) {
557175164Sjhb			/* Object does not yet exist, create it if requested. */
558175164Sjhb			if (uap->flags & O_CREAT) {
559225344Srwatson#ifdef MAC
560225344Srwatson				error = mac_posixshm_check_create(td->td_ucred,
561225344Srwatson				    path);
562225344Srwatson				if (error == 0) {
563225344Srwatson#endif
564225344Srwatson					shmfd = shm_alloc(td->td_ucred, cmode);
565225344Srwatson					shm_insert(path, fnv, shmfd);
566225344Srwatson#ifdef MAC
567225344Srwatson				}
568225344Srwatson#endif
569175164Sjhb			} else {
570175164Sjhb				free(path, M_SHMFD);
571175164Sjhb				error = ENOENT;
572175164Sjhb			}
573175164Sjhb		} else {
574175164Sjhb			/*
575175164Sjhb			 * Object already exists, obtain a new
576175164Sjhb			 * reference if requested and permitted.
577175164Sjhb			 */
578175164Sjhb			free(path, M_SHMFD);
579175164Sjhb			if ((uap->flags & (O_CREAT | O_EXCL)) ==
580175164Sjhb			    (O_CREAT | O_EXCL))
581175164Sjhb				error = EEXIST;
582175164Sjhb			else {
583175164Sjhb#ifdef MAC
584175164Sjhb				error = mac_posixshm_check_open(td->td_ucred,
585225344Srwatson				    shmfd, FFLAGS(uap->flags & O_ACCMODE));
586175164Sjhb				if (error == 0)
587175164Sjhb#endif
588175164Sjhb				error = shm_access(shmfd, td->td_ucred,
589175164Sjhb				    FFLAGS(uap->flags & O_ACCMODE));
590175164Sjhb			}
591175164Sjhb
592175164Sjhb			/*
593175164Sjhb			 * Truncate the file back to zero length if
594175164Sjhb			 * O_TRUNC was specified and the object was
595175164Sjhb			 * opened with read/write.
596175164Sjhb			 */
597175164Sjhb			if (error == 0 &&
598175164Sjhb			    (uap->flags & (O_ACCMODE | O_TRUNC)) ==
599175164Sjhb			    (O_RDWR | O_TRUNC)) {
600175164Sjhb#ifdef MAC
601175164Sjhb				error = mac_posixshm_check_truncate(
602175164Sjhb					td->td_ucred, fp->f_cred, shmfd);
603175164Sjhb				if (error == 0)
604175164Sjhb#endif
605175164Sjhb					shm_dotruncate(shmfd, 0);
606175164Sjhb			}
607175164Sjhb			if (error == 0)
608175164Sjhb				shm_hold(shmfd);
609175164Sjhb		}
610175164Sjhb		sx_xunlock(&shm_dict_lock);
611175164Sjhb
612175164Sjhb		if (error) {
613175164Sjhb			fdclose(fdp, fp, fd, td);
614175164Sjhb			fdrop(fp, td);
615175164Sjhb			return (error);
616175164Sjhb		}
617175164Sjhb	}
618175164Sjhb
619175164Sjhb	finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
620175164Sjhb
621175164Sjhb	FILEDESC_XLOCK(fdp);
622175164Sjhb	if (fdp->fd_ofiles[fd] == fp)
623175164Sjhb		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
624175164Sjhb	FILEDESC_XUNLOCK(fdp);
625175164Sjhb	td->td_retval[0] = fd;
626175164Sjhb	fdrop(fp, td);
627175164Sjhb
628175164Sjhb	return (0);
629175164Sjhb}
630175164Sjhb
631175164Sjhbint
632225617Skmacysys_shm_unlink(struct thread *td, struct shm_unlink_args *uap)
633175164Sjhb{
634175164Sjhb	char *path;
635175164Sjhb	Fnv32_t fnv;
636175164Sjhb	int error;
637175164Sjhb
638175164Sjhb	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
639175164Sjhb	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
640175164Sjhb	if (error) {
641175164Sjhb		free(path, M_TEMP);
642175164Sjhb		return (error);
643175164Sjhb	}
644175164Sjhb
645175164Sjhb	fnv = fnv_32_str(path, FNV1_32_INIT);
646175164Sjhb	sx_xlock(&shm_dict_lock);
647175164Sjhb	error = shm_remove(path, fnv, td->td_ucred);
648175164Sjhb	sx_xunlock(&shm_dict_lock);
649175164Sjhb	free(path, M_TEMP);
650175164Sjhb
651175164Sjhb	return (error);
652175164Sjhb}
653175164Sjhb
654175164Sjhb/*
655175164Sjhb * mmap() helper to validate mmap() requests against shm object state
656175164Sjhb * and give mmap() the vm_object to use for the mapping.
657175164Sjhb */
658175164Sjhbint
659175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
660175164Sjhb    vm_object_t *obj)
661175164Sjhb{
662175164Sjhb
663175164Sjhb	/*
664175164Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
665175164Sjhb	 * sign errors.  It should be fixed.
666175164Sjhb	 */
667185533Skan	if (foff >= shmfd->shm_size ||
668185533Skan	    foff + objsize > round_page(shmfd->shm_size))
669175164Sjhb		return (EINVAL);
670175164Sjhb
671175164Sjhb	mtx_lock(&shm_timestamp_lock);
672175164Sjhb	vfs_timestamp(&shmfd->shm_atime);
673175164Sjhb	mtx_unlock(&shm_timestamp_lock);
674175164Sjhb	vm_object_reference(shmfd->shm_object);
675175164Sjhb	*obj = shmfd->shm_object;
676175164Sjhb	return (0);
677175164Sjhb}
678224914Skib
679224914Skibstatic int
680224914Skibshm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
681224914Skib    struct thread *td)
682224914Skib{
683224914Skib	struct shmfd *shmfd;
684224914Skib	int error;
685224914Skib
686224914Skib	error = 0;
687224914Skib	shmfd = fp->f_data;
688224914Skib	mtx_lock(&shm_timestamp_lock);
689224914Skib	/*
690224914Skib	 * SUSv4 says that x bits of permission need not be affected.
691224914Skib	 * Be consistent with our shm_open there.
692224914Skib	 */
693224914Skib#ifdef MAC
694224914Skib	error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
695224914Skib	if (error != 0)
696224914Skib		goto out;
697224914Skib#endif
698224914Skib	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid,
699224914Skib	    shmfd->shm_gid, VADMIN, active_cred, NULL);
700224914Skib	if (error != 0)
701224914Skib		goto out;
702224914Skib	shmfd->shm_mode = mode & ACCESSPERMS;
703224914Skibout:
704224914Skib	mtx_unlock(&shm_timestamp_lock);
705224914Skib	return (error);
706224914Skib}
707224914Skib
708224914Skibstatic int
709224914Skibshm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
710224914Skib    struct thread *td)
711224914Skib{
712224914Skib	struct shmfd *shmfd;
713224914Skib	int error;
714224914Skib
715224935Skib	error = 0;
716224914Skib	shmfd = fp->f_data;
717224914Skib	mtx_lock(&shm_timestamp_lock);
718224914Skib#ifdef MAC
719224914Skib	error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
720224914Skib	if (error != 0)
721224914Skib		goto out;
722224914Skib#endif
723224914Skib	if (uid == (uid_t)-1)
724224914Skib		uid = shmfd->shm_uid;
725224914Skib	if (gid == (gid_t)-1)
726224914Skib                 gid = shmfd->shm_gid;
727224914Skib	if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
728224914Skib	    (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
729224914Skib	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
730224914Skib		goto out;
731224914Skib	shmfd->shm_uid = uid;
732224914Skib	shmfd->shm_gid = gid;
733224914Skibout:
734224914Skib	mtx_unlock(&shm_timestamp_lock);
735224914Skib	return (error);
736224914Skib}
737228509Sjhb
738228509Sjhb/*
739228509Sjhb * Helper routines to allow the backing object of a shared memory file
740228509Sjhb * descriptor to be mapped in the kernel.
741228509Sjhb */
742228509Sjhbint
743228509Sjhbshm_map(struct file *fp, size_t size, off_t offset, void **memp)
744228509Sjhb{
745228509Sjhb	struct shmfd *shmfd;
746228509Sjhb	vm_offset_t kva, ofs;
747228509Sjhb	vm_object_t obj;
748228509Sjhb	int rv;
749228509Sjhb
750228509Sjhb	if (fp->f_type != DTYPE_SHM)
751228509Sjhb		return (EINVAL);
752228509Sjhb	shmfd = fp->f_data;
753228509Sjhb	obj = shmfd->shm_object;
754228509Sjhb	VM_OBJECT_LOCK(obj);
755228509Sjhb	/*
756228509Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
757228509Sjhb	 * sign errors.  It should be fixed.
758228509Sjhb	 */
759228509Sjhb	if (offset >= shmfd->shm_size ||
760228509Sjhb	    offset + size > round_page(shmfd->shm_size)) {
761228509Sjhb		VM_OBJECT_UNLOCK(obj);
762228509Sjhb		return (EINVAL);
763228509Sjhb	}
764228509Sjhb
765228509Sjhb	shmfd->shm_kmappings++;
766228509Sjhb	vm_object_reference_locked(obj);
767228509Sjhb	VM_OBJECT_UNLOCK(obj);
768228509Sjhb
769228509Sjhb	/* Map the object into the kernel_map and wire it. */
770228509Sjhb	kva = vm_map_min(kernel_map);
771228509Sjhb	ofs = offset & PAGE_MASK;
772228509Sjhb	offset = trunc_page(offset);
773228509Sjhb	size = round_page(size + ofs);
774228509Sjhb	rv = vm_map_find(kernel_map, obj, offset, &kva, size,
775228509Sjhb	    VMFS_ALIGNED_SPACE, VM_PROT_READ | VM_PROT_WRITE,
776228509Sjhb	    VM_PROT_READ | VM_PROT_WRITE, 0);
777228509Sjhb	if (rv == KERN_SUCCESS) {
778228509Sjhb		rv = vm_map_wire(kernel_map, kva, kva + size,
779228509Sjhb		    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
780228509Sjhb		if (rv == KERN_SUCCESS) {
781228509Sjhb			*memp = (void *)(kva + ofs);
782228509Sjhb			return (0);
783228509Sjhb		}
784228509Sjhb		vm_map_remove(kernel_map, kva, kva + size);
785228509Sjhb	} else
786228509Sjhb		vm_object_deallocate(obj);
787228509Sjhb
788228509Sjhb	/* On failure, drop our mapping reference. */
789228509Sjhb	VM_OBJECT_LOCK(obj);
790228509Sjhb	shmfd->shm_kmappings--;
791228509Sjhb	VM_OBJECT_UNLOCK(obj);
792228509Sjhb
793228509Sjhb	switch (rv) {
794228509Sjhb	case KERN_INVALID_ADDRESS:
795228509Sjhb	case KERN_NO_SPACE:
796228509Sjhb		return (ENOMEM);
797228509Sjhb	case KERN_PROTECTION_FAILURE:
798228509Sjhb		return (EACCES);
799228509Sjhb	default:
800228509Sjhb		return (EINVAL);
801228509Sjhb	}
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