xref: /freebsd/tools/regression/priv/priv_vfs_stickyfile.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * Copyright (c) 2007 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed by Robert N. M. Watson for the TrustedBSD
7  * Project under contract to nCircle Network Security, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 /*
34  * Check that privilege is required to set the sticky bit on a file but not a
35  * directory.
36  */
37 
38 #include <sys/stat.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <unistd.h>
43 
44 #include "main.h"
45 
46 char fpath[1024];
47 int fpath_initialized;
48 
49 char dpath[1024];
50 int dpath_initialized;
51 
52 int
53 priv_vfs_stickyfile_dir_fowner_setup(int asroot, int injail,
54     struct test *test)
55 {
56 
57 	setup_dir("priv_vfs_stickyfile_fowner_setup: dpath", dpath,
58 	    UID_OWNER, GID_OWNER, 0700);
59 	dpath_initialized = 1;
60 	return (0);
61 }
62 
63 int
64 priv_vfs_stickyfile_dir_fother_setup(int asroot, int injail,
65     struct test *test)
66 {
67 
68 	setup_dir("priv_vfs_stickyfile_fother_setup: dpath", dpath,
69 	    UID_OTHER, GID_OTHER, 0700);
70 	dpath_initialized = 1;
71 	return (0);
72 }
73 
74 int
75 priv_vfs_stickyfile_file_fowner_setup(int asroot, int injail,
76     struct test *test)
77 {
78 
79 	setup_file("priv_vfs_stickyfile_fowner_setup: fpath", fpath,
80 	    UID_OWNER, GID_OWNER, 0600);
81 	fpath_initialized = 1;
82 	return (0);
83 }
84 
85 int
86 priv_vfs_stickyfile_file_fother_setup(int asroot, int injail,
87     struct test *test)
88 {
89 
90 	setup_file("priv_vfs_stickyfile_fother_setup: fpath", fpath,
91 	    UID_OTHER, GID_OTHER, 0600);
92 	fpath_initialized = 1;
93 	return (0);
94 }
95 
96 void
97 priv_vfs_stickyfile_dir_fowner(int asroot, int injail, struct test *test)
98 {
99 	int error;
100 
101 	error = chmod(dpath, 0700 | S_ISTXT);
102 	if (asroot && injail)
103 		expect("priv_vfs_stickyfile_dir_fowner(root, jail)", error,
104 		    0, 0);
105 	if (asroot && !injail)
106 		expect("priv_vfs_stickyfile_dir_fowner(root, !jail)", error,
107 		    0, 0);
108 	if (!asroot && injail)
109 		expect("priv_vfs_stickyfile_dir_fowner(!root, jail)", error,
110 		    0, 0);
111 	if (!asroot && !injail)
112 		expect("priv_vfs_stickyfile_dir_fowner(!root, !jail)", error,
113 		    0, 0);
114 }
115 
116 void
117 priv_vfs_stickyfile_dir_fother(int asroot, int injail, struct test *test)
118 {
119 	int error;
120 
121 	error = chmod(dpath, 0700 | S_ISTXT);
122 	if (asroot && injail)
123 		expect("priv_vfs_stickyfile_dir_fother(root, jail)", error,
124 		    0, 0);
125 	if (asroot && !injail)
126 		expect("priv_vfs_stickyfile_dir_fother(root, !jail)", error,
127 		    0, 0);
128 	if (!asroot && injail)
129 		expect("priv_vfs_stickyfile_dir_fother(!root, jail)", error,
130 		    -1, EPERM);
131 	if (!asroot && !injail)
132 		expect("priv_vfs_stickyfile_dir_fother(!root, !jail)", error,
133 		    -1, EPERM);
134 }
135 
136 void
137 priv_vfs_stickyfile_file_fowner(int asroot, int injail, struct test *test)
138 {
139 	int error;
140 
141 	error = chmod(fpath, 0600 | S_ISTXT);
142 	if (asroot && injail)
143 		expect("priv_vfs_stickyfile_file_fowner(root, jail)", error,
144 		    0, 0);
145 	if (asroot && !injail)
146 		expect("priv_vfs_stickyfile_file_fowner(root, !jail)", error,
147 		    0, 0);
148 	if (!asroot && injail)
149 		expect("priv_vfs_stickyfile_file_fowner(!root, jail)", error,
150 		    -1, EFTYPE);
151 	if (!asroot && !injail)
152 		expect("priv_vfs_stickyfile_file_fowner(!root, !jail)", error,
153 		    -1, EFTYPE);
154 }
155 
156 void
157 priv_vfs_stickyfile_file_fother(int asroot, int injail, struct test *test)
158 {
159 	int error;
160 
161 	error = chmod(fpath, 0600 | S_ISTXT);
162 	if (asroot && injail)
163 		expect("priv_vfs_stickyfile_file_fother(root, jail)", error,
164 		    0, 0);
165 	if (asroot && !injail)
166 		expect("priv_vfs_stickyfile_file_fother(root, !jail)", error,
167 		    0, 0);
168 	if (!asroot && injail)
169 		expect("priv_vfs_stickyfile_file_fother(!root, jail)", error,
170 		    -1, EPERM);
171 	if (!asroot && !injail)
172 		expect("priv_vfs_stickyfile_file_fother(!root, !jail)", error,
173 		    -1, EPERM);
174 }
175 
176 void
177 priv_vfs_stickyfile_dir_cleanup(int asroot, int injail, struct test *test)
178 {
179 
180 	if (dpath_initialized) {
181 		(void)rmdir(dpath);
182 		dpath_initialized = 0;
183 	}
184 }
185 
186 void
187 priv_vfs_stickyfile_file_cleanup(int asroot, int injail, struct test *test)
188 {
189 
190 	if (fpath_initialized) {
191 		(void)unlink(fpath);
192 		fpath_initialized = 0;
193 	}
194 }
195