133965Sjdp/*-
289857Sobrien * Copyright (c) 2006 nCircle Network Security, Inc.
389857Sobrien * Copyright (c) 2007 Robert N. M. Watson
433965Sjdp * All rights reserved.
533965Sjdp *
633965Sjdp * This software was developed by Robert N. M. Watson for the TrustedBSD
733965Sjdp * Project under contract to nCircle Network Security, Inc.
833965Sjdp *
933965Sjdp * Redistribution and use in source and binary forms, with or without
1033965Sjdp * modification, are permitted provided that the following conditions
1133965Sjdp * are met:
1233965Sjdp * 1. Redistributions of source code must retain the above copyright
1333965Sjdp *    notice, this list of conditions and the following disclaimer.
1433965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1533965Sjdp *    notice, this list of conditions and the following disclaimer in the
1633965Sjdp *    documentation and/or other materials provided with the distribution.
1733965Sjdp *
1833965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1933965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2033965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2133965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
2233965Sjdp * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2333965Sjdp * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2433965Sjdp * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2533965Sjdp * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2633965Sjdp * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2733965Sjdp * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2833965Sjdp * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2933965Sjdp *
3033965Sjdp * $FreeBSD$
3133965Sjdp */
3233965Sjdp
3389857Sobrien/*
3489857Sobrien * Confirm that calls to fhopen() require non-jailed priilege.  We create a
3589857Sobrien * temporary file and grab the file handle using getfh() before starting.
3689857Sobrien */
3789857Sobrien
3889857Sobrien#include <sys/param.h>
3989857Sobrien#include <sys/mount.h>
4033965Sjdp
4133965Sjdp#include <err.h>
4233965Sjdp#include <errno.h>
4389857Sobrien#include <fcntl.h>
4433965Sjdp#include <unistd.h>
4533965Sjdp
4633965Sjdp#include "main.h"
4733965Sjdp
4833965Sjdpstatic char fpath[1024];
4933965Sjdpstatic int fpath_initialized;
5033965Sjdpstatic fhandle_t fh;
5133965Sjdp
5233965Sjdpint
5333965Sjdppriv_vfs_fhopen_setup(int asroot, int injail, struct test *test)
5433965Sjdp{
5533965Sjdp
5689857Sobrien	setup_file("private_vfs_fhopen_setup: fpath", fpath, UID_ROOT,
5733965Sjdp	    GID_WHEEL, 0644);
5833965Sjdp	fpath_initialized = 1;
5933965Sjdp	if (getfh(fpath, &fh) < 0) {
6033965Sjdp		warn("priv_vfs_fhopen_setup: getfh(%s)", fpath);
6133965Sjdp		return (-1);
6233965Sjdp	}
6333965Sjdp	return (0);
6433965Sjdp}
6533965Sjdp
6633965Sjdpvoid
6733965Sjdppriv_vfs_fhopen(int asroot, int injail, struct test *test)
6833965Sjdp{
6933965Sjdp	int errno_saved, error, fd;
7033965Sjdp
7133965Sjdp	fd = fhopen(&fh, O_RDONLY);
7233965Sjdp	if (fd >= 0) {
7333965Sjdp		error = 0;
7433965Sjdp		errno_saved = errno;
7533965Sjdp		close(fd);
7633965Sjdp		errno = errno_saved;
7733965Sjdp	} else
7833965Sjdp		error = -1;
7933965Sjdp	if (asroot && injail)
8033965Sjdp		expect("priv_vfs_fhopen(asroot, injail)", error, -1, EPERM);
8133965Sjdp	if (asroot && !injail)
8233965Sjdp		expect("priv_vfs_fhopen(asroot, !injail)", error, 0, 0);
8333965Sjdp	if (!asroot && injail)
8433965Sjdp		expect("priv_vfs_fhopen(!asroot, injail)", error, -1, EPERM);
8589857Sobrien	if (!asroot && !injail)
8633965Sjdp		expect("priv_vfs_fhopen(!asroot, !injail)", error, -1, EPERM);
8789857Sobrien}
8889857Sobrien
8933965Sjdpvoid
9033965Sjdppriv_vfs_fhopen_cleanup(int asroot, int injail, struct test *test)
9133965Sjdp{
9233965Sjdp
9333965Sjdp	if (fpath_initialized) {
9433965Sjdp		(void)unlink(fpath);
9533965Sjdp		fpath_initialized = 0;
9689857Sobrien	}
9733965Sjdp}
9833965Sjdp