uipc_shm.c revision 225617
1/*-
2 * Copyright (c) 2006, 2011 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Support for shared swap-backed anonymous memory objects via
29 * shm_open(2) and shm_unlink(2).  While most of the implementation is
30 * here, vm_mmap.c contains mapping logic changes.
31 *
32 * TODO:
33 *
34 * (1) Need to export data to a userland tool via a sysctl.  Should ipcs(1)
35 *     and ipcrm(1) be expanded or should new tools to manage both POSIX
36 *     kernel semaphores and POSIX shared memory be written?
37 *
38 * (2) Add support for this file type to fstat(1).
39 *
40 * (3) Resource limits?  Does this need its own resource limits or are the
41 *     existing limits in mmap(2) sufficient?
42 *
43 * (4) Partial page truncation.  vnode_pager_setsize() will zero any parts
44 *     of a partially mapped page as a result of ftruncate(2)/truncate(2).
45 *     We can do the same (with the same pmap evil), but do we need to
46 *     worry about the bits on disk if the page is swapped out or will the
47 *     swapper zero the parts of a page that are invalid if the page is
48 *     swapped back in for us?
49 */
50
51#include <sys/cdefs.h>
52__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 225617 2011-09-16 13:58:51Z kmacy $");
53
54#include "opt_capsicum.h"
55
56#include <sys/param.h>
57#include <sys/capability.h>
58#include <sys/fcntl.h>
59#include <sys/file.h>
60#include <sys/filedesc.h>
61#include <sys/fnv_hash.h>
62#include <sys/kernel.h>
63#include <sys/lock.h>
64#include <sys/malloc.h>
65#include <sys/mman.h>
66#include <sys/mutex.h>
67#include <sys/priv.h>
68#include <sys/proc.h>
69#include <sys/refcount.h>
70#include <sys/resourcevar.h>
71#include <sys/stat.h>
72#include <sys/sysctl.h>
73#include <sys/sysproto.h>
74#include <sys/systm.h>
75#include <sys/sx.h>
76#include <sys/time.h>
77#include <sys/vnode.h>
78
79#include <security/mac/mac_framework.h>
80
81#include <vm/vm.h>
82#include <vm/vm_param.h>
83#include <vm/pmap.h>
84#include <vm/vm_map.h>
85#include <vm/vm_object.h>
86#include <vm/vm_page.h>
87#include <vm/vm_pager.h>
88#include <vm/swap_pager.h>
89
90struct shm_mapping {
91	char		*sm_path;
92	Fnv32_t		sm_fnv;
93	struct shmfd	*sm_shmfd;
94	LIST_ENTRY(shm_mapping) sm_link;
95};
96
97static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
98static LIST_HEAD(, shm_mapping) *shm_dictionary;
99static struct sx shm_dict_lock;
100static struct mtx shm_timestamp_lock;
101static u_long shm_hash;
102
103#define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
104
105static int	shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
106static struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
107static void	shm_dict_init(void *arg);
108static void	shm_drop(struct shmfd *shmfd);
109static struct shmfd *shm_hold(struct shmfd *shmfd);
110static void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
111static struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
112static int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
113static int	shm_dotruncate(struct shmfd *shmfd, off_t length);
114
115static fo_rdwr_t	shm_read;
116static fo_rdwr_t	shm_write;
117static fo_truncate_t	shm_truncate;
118static fo_ioctl_t	shm_ioctl;
119static fo_poll_t	shm_poll;
120static fo_kqfilter_t	shm_kqfilter;
121static fo_stat_t	shm_stat;
122static fo_close_t	shm_close;
123static fo_chmod_t	shm_chmod;
124static fo_chown_t	shm_chown;
125
126/* File descriptor operations. */
127static struct fileops shm_ops = {
128	.fo_read = shm_read,
129	.fo_write = shm_write,
130	.fo_truncate = shm_truncate,
131	.fo_ioctl = shm_ioctl,
132	.fo_poll = shm_poll,
133	.fo_kqfilter = shm_kqfilter,
134	.fo_stat = shm_stat,
135	.fo_close = shm_close,
136	.fo_chmod = shm_chmod,
137	.fo_chown = shm_chown,
138	.fo_flags = DFLAG_PASSABLE
139};
140
141FEATURE(posix_shm, "POSIX shared memory");
142
143static int
144shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
145    int flags, struct thread *td)
146{
147
148	return (EOPNOTSUPP);
149}
150
151static int
152shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
153    int flags, struct thread *td)
154{
155
156	return (EOPNOTSUPP);
157}
158
159static int
160shm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
161    struct thread *td)
162{
163	struct shmfd *shmfd;
164#ifdef MAC
165	int error;
166#endif
167
168	shmfd = fp->f_data;
169#ifdef MAC
170	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
171	if (error)
172		return (error);
173#endif
174	return (shm_dotruncate(shmfd, length));
175}
176
177static int
178shm_ioctl(struct file *fp, u_long com, void *data,
179    struct ucred *active_cred, struct thread *td)
180{
181
182	return (EOPNOTSUPP);
183}
184
185static int
186shm_poll(struct file *fp, int events, struct ucred *active_cred,
187    struct thread *td)
188{
189
190	return (EOPNOTSUPP);
191}
192
193static int
194shm_kqfilter(struct file *fp, struct knote *kn)
195{
196
197	return (EOPNOTSUPP);
198}
199
200static int
201shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
202    struct thread *td)
203{
204	struct shmfd *shmfd;
205#ifdef MAC
206	int error;
207#endif
208
209	shmfd = fp->f_data;
210
211#ifdef MAC
212	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
213	if (error)
214		return (error);
215#endif
216
217	/*
218	 * Attempt to return sanish values for fstat() on a memory file
219	 * descriptor.
220	 */
221	bzero(sb, sizeof(*sb));
222	sb->st_blksize = PAGE_SIZE;
223	sb->st_size = shmfd->shm_size;
224	sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
225	mtx_lock(&shm_timestamp_lock);
226	sb->st_atim = shmfd->shm_atime;
227	sb->st_ctim = shmfd->shm_ctime;
228	sb->st_mtim = shmfd->shm_mtime;
229	sb->st_birthtim = shmfd->shm_birthtime;
230	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
231	sb->st_uid = shmfd->shm_uid;
232	sb->st_gid = shmfd->shm_gid;
233	mtx_unlock(&shm_timestamp_lock);
234
235	return (0);
236}
237
238static int
239shm_close(struct file *fp, struct thread *td)
240{
241	struct shmfd *shmfd;
242
243	shmfd = fp->f_data;
244	fp->f_data = NULL;
245	shm_drop(shmfd);
246
247	return (0);
248}
249
250static int
251shm_dotruncate(struct shmfd *shmfd, off_t length)
252{
253	vm_object_t object;
254	vm_page_t m;
255	vm_pindex_t nobjsize;
256	vm_ooffset_t delta;
257
258	object = shmfd->shm_object;
259	VM_OBJECT_LOCK(object);
260	if (length == shmfd->shm_size) {
261		VM_OBJECT_UNLOCK(object);
262		return (0);
263	}
264	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
265
266	/* Are we shrinking?  If so, trim the end. */
267	if (length < shmfd->shm_size) {
268		delta = ptoa(object->size - nobjsize);
269
270		/* Toss in memory pages. */
271		if (nobjsize < object->size)
272			vm_object_page_remove(object, nobjsize, object->size,
273			    0);
274
275		/* Toss pages from swap. */
276		if (object->type == OBJT_SWAP)
277			swap_pager_freespace(object, nobjsize, delta);
278
279		/* Free the swap accounted for shm */
280		swap_release_by_cred(delta, object->cred);
281		object->charge -= delta;
282
283		/*
284		 * If the last page is partially mapped, then zero out
285		 * the garbage at the end of the page.  See comments
286		 * in vnode_pager_setsize() for more details.
287		 *
288		 * XXXJHB: This handles in memory pages, but what about
289		 * a page swapped out to disk?
290		 */
291		if ((length & PAGE_MASK) &&
292		    (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
293		    m->valid != 0) {
294			int base = (int)length & PAGE_MASK;
295			int size = PAGE_SIZE - base;
296
297			pmap_zero_page_area(m, base, size);
298
299			/*
300			 * Update the valid bits to reflect the blocks that
301			 * have been zeroed.  Some of these valid bits may
302			 * have already been set.
303			 */
304			vm_page_set_valid(m, base, size);
305
306			/*
307			 * Round "base" to the next block boundary so that the
308			 * dirty bit for a partially zeroed block is not
309			 * cleared.
310			 */
311			base = roundup2(base, DEV_BSIZE);
312
313			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
314		} else if ((length & PAGE_MASK) &&
315		    __predict_false(object->cache != NULL)) {
316			vm_page_cache_free(object, OFF_TO_IDX(length),
317			    nobjsize);
318		}
319	} else {
320
321		/* Attempt to reserve the swap */
322		delta = ptoa(nobjsize - object->size);
323		if (!swap_reserve_by_cred(delta, object->cred)) {
324			VM_OBJECT_UNLOCK(object);
325			return (ENOMEM);
326		}
327		object->charge += delta;
328	}
329	shmfd->shm_size = length;
330	mtx_lock(&shm_timestamp_lock);
331	vfs_timestamp(&shmfd->shm_ctime);
332	shmfd->shm_mtime = shmfd->shm_ctime;
333	mtx_unlock(&shm_timestamp_lock);
334	object->size = nobjsize;
335	VM_OBJECT_UNLOCK(object);
336	return (0);
337}
338
339/*
340 * shmfd object management including creation and reference counting
341 * routines.
342 */
343static struct shmfd *
344shm_alloc(struct ucred *ucred, mode_t mode)
345{
346	struct shmfd *shmfd;
347
348	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
349	shmfd->shm_size = 0;
350	shmfd->shm_uid = ucred->cr_uid;
351	shmfd->shm_gid = ucred->cr_gid;
352	shmfd->shm_mode = mode;
353	shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
354	    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
355	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
356	VM_OBJECT_LOCK(shmfd->shm_object);
357	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
358	vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
359	VM_OBJECT_UNLOCK(shmfd->shm_object);
360	vfs_timestamp(&shmfd->shm_birthtime);
361	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
362	    shmfd->shm_birthtime;
363	refcount_init(&shmfd->shm_refs, 1);
364#ifdef MAC
365	mac_posixshm_init(shmfd);
366	mac_posixshm_create(ucred, shmfd);
367#endif
368
369	return (shmfd);
370}
371
372static struct shmfd *
373shm_hold(struct shmfd *shmfd)
374{
375
376	refcount_acquire(&shmfd->shm_refs);
377	return (shmfd);
378}
379
380static void
381shm_drop(struct shmfd *shmfd)
382{
383
384	if (refcount_release(&shmfd->shm_refs)) {
385#ifdef MAC
386		mac_posixshm_destroy(shmfd);
387#endif
388		vm_object_deallocate(shmfd->shm_object);
389		free(shmfd, M_SHMFD);
390	}
391}
392
393/*
394 * Determine if the credentials have sufficient permissions for a
395 * specified combination of FREAD and FWRITE.
396 */
397static int
398shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
399{
400	accmode_t accmode;
401	int error;
402
403	accmode = 0;
404	if (flags & FREAD)
405		accmode |= VREAD;
406	if (flags & FWRITE)
407		accmode |= VWRITE;
408	mtx_lock(&shm_timestamp_lock);
409	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
410	    accmode, ucred, NULL);
411	mtx_unlock(&shm_timestamp_lock);
412	return (error);
413}
414
415/*
416 * Dictionary management.  We maintain an in-kernel dictionary to map
417 * paths to shmfd objects.  We use the FNV hash on the path to store
418 * the mappings in a hash table.
419 */
420static void
421shm_dict_init(void *arg)
422{
423
424	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
425	sx_init(&shm_dict_lock, "shm dictionary");
426	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
427}
428SYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
429
430static struct shmfd *
431shm_lookup(char *path, Fnv32_t fnv)
432{
433	struct shm_mapping *map;
434
435	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
436		if (map->sm_fnv != fnv)
437			continue;
438		if (strcmp(map->sm_path, path) == 0)
439			return (map->sm_shmfd);
440	}
441
442	return (NULL);
443}
444
445static void
446shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
447{
448	struct shm_mapping *map;
449
450	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
451	map->sm_path = path;
452	map->sm_fnv = fnv;
453	map->sm_shmfd = shm_hold(shmfd);
454	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
455}
456
457static int
458shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
459{
460	struct shm_mapping *map;
461	int error;
462
463	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
464		if (map->sm_fnv != fnv)
465			continue;
466		if (strcmp(map->sm_path, path) == 0) {
467#ifdef MAC
468			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
469			if (error)
470				return (error);
471#endif
472			error = shm_access(map->sm_shmfd, ucred,
473			    FREAD | FWRITE);
474			if (error)
475				return (error);
476			LIST_REMOVE(map, sm_link);
477			shm_drop(map->sm_shmfd);
478			free(map->sm_path, M_SHMFD);
479			free(map, M_SHMFD);
480			return (0);
481		}
482	}
483
484	return (ENOENT);
485}
486
487/* System calls. */
488int
489sys_shm_open(struct thread *td, struct shm_open_args *uap)
490{
491	struct filedesc *fdp;
492	struct shmfd *shmfd;
493	struct file *fp;
494	char *path;
495	Fnv32_t fnv;
496	mode_t cmode;
497	int fd, error;
498
499#ifdef CAPABILITY_MODE
500	/*
501	 * shm_open(2) is only allowed for anonymous objects.
502	 */
503	if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON))
504		return (ECAPMODE);
505#endif
506
507	if ((uap->flags & O_ACCMODE) != O_RDONLY &&
508	    (uap->flags & O_ACCMODE) != O_RDWR)
509		return (EINVAL);
510
511	if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
512		return (EINVAL);
513
514	fdp = td->td_proc->p_fd;
515	cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
516
517	error = falloc(td, &fp, &fd, 0);
518	if (error)
519		return (error);
520
521	/* A SHM_ANON path pointer creates an anonymous object. */
522	if (uap->path == SHM_ANON) {
523		/* A read-only anonymous object is pointless. */
524		if ((uap->flags & O_ACCMODE) == O_RDONLY) {
525			fdclose(fdp, fp, fd, td);
526			fdrop(fp, td);
527			return (EINVAL);
528		}
529		shmfd = shm_alloc(td->td_ucred, cmode);
530	} else {
531		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
532		error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
533
534		/* Require paths to start with a '/' character. */
535		if (error == 0 && path[0] != '/')
536			error = EINVAL;
537		if (error) {
538			fdclose(fdp, fp, fd, td);
539			fdrop(fp, td);
540			free(path, M_SHMFD);
541			return (error);
542		}
543
544		fnv = fnv_32_str(path, FNV1_32_INIT);
545		sx_xlock(&shm_dict_lock);
546		shmfd = shm_lookup(path, fnv);
547		if (shmfd == NULL) {
548			/* Object does not yet exist, create it if requested. */
549			if (uap->flags & O_CREAT) {
550#ifdef MAC
551				error = mac_posixshm_check_create(td->td_ucred,
552				    path);
553				if (error == 0) {
554#endif
555					shmfd = shm_alloc(td->td_ucred, cmode);
556					shm_insert(path, fnv, shmfd);
557#ifdef MAC
558				}
559#endif
560			} else {
561				free(path, M_SHMFD);
562				error = ENOENT;
563			}
564		} else {
565			/*
566			 * Object already exists, obtain a new
567			 * reference if requested and permitted.
568			 */
569			free(path, M_SHMFD);
570			if ((uap->flags & (O_CREAT | O_EXCL)) ==
571			    (O_CREAT | O_EXCL))
572				error = EEXIST;
573			else {
574#ifdef MAC
575				error = mac_posixshm_check_open(td->td_ucred,
576				    shmfd, FFLAGS(uap->flags & O_ACCMODE));
577				if (error == 0)
578#endif
579				error = shm_access(shmfd, td->td_ucred,
580				    FFLAGS(uap->flags & O_ACCMODE));
581			}
582
583			/*
584			 * Truncate the file back to zero length if
585			 * O_TRUNC was specified and the object was
586			 * opened with read/write.
587			 */
588			if (error == 0 &&
589			    (uap->flags & (O_ACCMODE | O_TRUNC)) ==
590			    (O_RDWR | O_TRUNC)) {
591#ifdef MAC
592				error = mac_posixshm_check_truncate(
593					td->td_ucred, fp->f_cred, shmfd);
594				if (error == 0)
595#endif
596					shm_dotruncate(shmfd, 0);
597			}
598			if (error == 0)
599				shm_hold(shmfd);
600		}
601		sx_xunlock(&shm_dict_lock);
602
603		if (error) {
604			fdclose(fdp, fp, fd, td);
605			fdrop(fp, td);
606			return (error);
607		}
608	}
609
610	finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
611
612	FILEDESC_XLOCK(fdp);
613	if (fdp->fd_ofiles[fd] == fp)
614		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
615	FILEDESC_XUNLOCK(fdp);
616	td->td_retval[0] = fd;
617	fdrop(fp, td);
618
619	return (0);
620}
621
622int
623sys_shm_unlink(struct thread *td, struct shm_unlink_args *uap)
624{
625	char *path;
626	Fnv32_t fnv;
627	int error;
628
629	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
630	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
631	if (error) {
632		free(path, M_TEMP);
633		return (error);
634	}
635
636	fnv = fnv_32_str(path, FNV1_32_INIT);
637	sx_xlock(&shm_dict_lock);
638	error = shm_remove(path, fnv, td->td_ucred);
639	sx_xunlock(&shm_dict_lock);
640	free(path, M_TEMP);
641
642	return (error);
643}
644
645/*
646 * mmap() helper to validate mmap() requests against shm object state
647 * and give mmap() the vm_object to use for the mapping.
648 */
649int
650shm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
651    vm_object_t *obj)
652{
653
654	/*
655	 * XXXRW: This validation is probably insufficient, and subject to
656	 * sign errors.  It should be fixed.
657	 */
658	if (foff >= shmfd->shm_size ||
659	    foff + objsize > round_page(shmfd->shm_size))
660		return (EINVAL);
661
662	mtx_lock(&shm_timestamp_lock);
663	vfs_timestamp(&shmfd->shm_atime);
664	mtx_unlock(&shm_timestamp_lock);
665	vm_object_reference(shmfd->shm_object);
666	*obj = shmfd->shm_object;
667	return (0);
668}
669
670static int
671shm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
672    struct thread *td)
673{
674	struct shmfd *shmfd;
675	int error;
676
677	error = 0;
678	shmfd = fp->f_data;
679	mtx_lock(&shm_timestamp_lock);
680	/*
681	 * SUSv4 says that x bits of permission need not be affected.
682	 * Be consistent with our shm_open there.
683	 */
684#ifdef MAC
685	error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
686	if (error != 0)
687		goto out;
688#endif
689	error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid,
690	    shmfd->shm_gid, VADMIN, active_cred, NULL);
691	if (error != 0)
692		goto out;
693	shmfd->shm_mode = mode & ACCESSPERMS;
694out:
695	mtx_unlock(&shm_timestamp_lock);
696	return (error);
697}
698
699static int
700shm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
701    struct thread *td)
702{
703	struct shmfd *shmfd;
704	int error;
705
706	error = 0;
707	shmfd = fp->f_data;
708	mtx_lock(&shm_timestamp_lock);
709#ifdef MAC
710	error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
711	if (error != 0)
712		goto out;
713#endif
714	if (uid == (uid_t)-1)
715		uid = shmfd->shm_uid;
716	if (gid == (gid_t)-1)
717                 gid = shmfd->shm_gid;
718	if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
719	    (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
720	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
721		goto out;
722	shmfd->shm_uid = uid;
723	shmfd->shm_gid = gid;
724out:
725	mtx_unlock(&shm_timestamp_lock);
726	return (error);
727}
728