xref: /freebsd/tools/regression/priv/priv_vfs_generation.c (revision bfe691b2f75de2224c7ceb304ebcdef2b42d4179)
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 a generation number isn't returned by stat() when not running
34  * with privilege.  In order to differentiate between a generation of 0 and
35  * a generation not being returned, we have to create a temporary file known
36  * to have a non-0 generation.  We try up to 10 times, and then give up,
37  * which is non-ideal, but better than not testing for a problem.
38  */
39 
40 #include <sys/stat.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "main.h"
48 
49 /*
50  * Can't use setup_file() since the resulting file needs to have specific
51  * properties.
52  */
53 void
54 priv_vfs_generation(void)
55 {
56 	char fpath[1024] = "/tmp/priv.XXXXXXXXXX";
57 	struct stat sb;
58 	int fd, i;
59 
60 	assert_root();
61 
62 	/*
63 	 * Create a file with a non-0 generation number.  Try ten times,
64 	 * which gives a high chance of succeeds, fail otherwise.  Not ideal,
65 	 * since we can't distinguish the file having a generation of 0 from
66 	 * not being able to query it for access control reasons.  The perils
67 	 * of an API that changes behavior based on lack of privilege rather
68 	 * than failing...
69 	 */
70 	for (i = 0; i < 10; i++) {
71 		fd = mkstemp(fpath);
72 		if (fd < 0)
73 			err(-1, "mkstemp");
74 		if (fstat(fd, &sb) < 0) {
75 			warn("fstat(%s)", fpath);
76 			close(fd);
77 			goto out;
78 		}
79 		if (sb.st_gen != 0)
80 			break;
81 		close(fd);
82 		(void)unlink(fpath);
83 		strcpy(fpath, "/tmp/generation.XXXXXXXXXX");
84 		fd = -1;
85 	}
86 	if (fd == -1)
87 		errx(-1,
88 		    "could not create file with non-0 generation as root");
89 	close(fd);
90 
91 	/*
92 	 * We've already tested that fstat() works, but try stat() to be
93 	 * consistent between privileged and unprivileged tests.
94 	 */
95 	if (stat(fpath, &sb) < 0) {
96 		warn("stat(%s) as root", fpath);
97 		goto out;
98 	}
99 
100 	set_euid(UID_OTHER);
101 
102 	if (stat(fpath, &sb) < 0) {
103 		warn("stat(%s) as !root", fpath);
104 		goto out;
105 	}
106 
107 	if (sb.st_gen != 0)
108 		warn("stat(%s) returned generation as !root", fpath);
109 
110 out:
111 	(void)seteuid(UID_ROOT);
112 	(void)unlink(fpath);
113 }
114