xref: /freebsd/tools/regression/priv/priv_vfs_fhstat.c (revision 94942af266ac119ede0ca836f9aa5a5ac0582938)
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Robert N. M. Watson for the TrustedBSD
6  * Project under contract to nCircle Network Security, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * Confirm that calls to fhstat() require privilege, trying with, and
34  * without.  We create a temporary file and grab the file handle using
35  * getfh() before starting.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <unistd.h>
45 
46 #include "main.h"
47 
48 void
49 priv_vfs_fhstat(void)
50 {
51 	char fpath[1024];
52 	struct stat sb;
53 	fhandle_t fh;
54 	int error;
55 
56 	assert_root();
57 
58 	setup_file(fpath, UID_ROOT, GID_WHEEL, 0644);
59 
60 	if (getfh(fpath, &fh) < 0) {
61 		warn("getfh(%s)", fpath);
62 		goto out;
63 	}
64 
65 	/*
66 	 * First, try with privilege.
67 	 */
68 	if (fhstat(&fh, &sb) < 0) {
69 		warn("fhstat(%s) as root", fpath);
70 		goto out;
71 	}
72 
73 	/*
74 	 * Now, without privilege.
75 	 */
76 	set_euid(UID_OTHER);
77 	error = fhstat(&fh, &sb);
78 	if (error == 0) {
79 		warn("fhstat(%s) succeeded as !root", fpath);
80 		goto out;
81 	}
82 	if (errno != EPERM) {
83 		warn("fhstat(%s) wrong errno %d as !root", fpath, errno);
84 		goto out;
85 	}
86 out:
87 	seteuid(UID_ROOT);
88 	(void)unlink(fpath);
89 }
90