19fa5f6b4SRobert Watson /*-
29fa5f6b4SRobert Watson * Copyright (c) 2006 nCircle Network Security, Inc.
3d903306aSRobert Watson * Copyright (c) 2007 Robert N. M. Watson
49fa5f6b4SRobert Watson * All rights reserved.
59fa5f6b4SRobert Watson *
69fa5f6b4SRobert Watson * This software was developed by Robert N. M. Watson for the TrustedBSD
79fa5f6b4SRobert Watson * Project under contract to nCircle Network Security, Inc.
89fa5f6b4SRobert Watson *
99fa5f6b4SRobert Watson * Redistribution and use in source and binary forms, with or without
109fa5f6b4SRobert Watson * modification, are permitted provided that the following conditions
119fa5f6b4SRobert Watson * are met:
129fa5f6b4SRobert Watson * 1. Redistributions of source code must retain the above copyright
139fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer.
149fa5f6b4SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
159fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer in the
169fa5f6b4SRobert Watson * documentation and/or other materials provided with the distribution.
179fa5f6b4SRobert Watson *
189fa5f6b4SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199fa5f6b4SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209fa5f6b4SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219fa5f6b4SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
229fa5f6b4SRobert Watson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239fa5f6b4SRobert Watson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
249fa5f6b4SRobert Watson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
259fa5f6b4SRobert Watson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
269fa5f6b4SRobert Watson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
279fa5f6b4SRobert Watson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
289fa5f6b4SRobert Watson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299fa5f6b4SRobert Watson */
309fa5f6b4SRobert Watson
319fa5f6b4SRobert Watson /*
329fa5f6b4SRobert Watson * This is a joint test of both the read and write privileges with respect to
339fa5f6b4SRobert Watson * discretionary file system access control (permissions). Only permissions,
349fa5f6b4SRobert Watson * not ACL semantics, and only privilege-related checks are performed.
359fa5f6b4SRobert Watson */
369fa5f6b4SRobert Watson
379fa5f6b4SRobert Watson #include <sys/types.h>
389fa5f6b4SRobert Watson #include <sys/stat.h>
399fa5f6b4SRobert Watson
409fa5f6b4SRobert Watson #include <err.h>
419fa5f6b4SRobert Watson #include <errno.h>
429fa5f6b4SRobert Watson #include <fcntl.h>
439fa5f6b4SRobert Watson #include <stdlib.h>
449fa5f6b4SRobert Watson #include <string.h>
459fa5f6b4SRobert Watson #include <unistd.h>
469fa5f6b4SRobert Watson
479fa5f6b4SRobert Watson #include "main.h"
489fa5f6b4SRobert Watson
49d903306aSRobert Watson static char fpath_none[1024];
50d903306aSRobert Watson static char fpath_read[1024];
51d903306aSRobert Watson static char fpath_write[1024];
52d903306aSRobert Watson static char fpath_readwrite[1024];
539fa5f6b4SRobert Watson
54d903306aSRobert Watson static int fpath_none_initialized;
55d903306aSRobert Watson static int fpath_read_initialized;
56d903306aSRobert Watson static int fpath_write_initialized;
57d903306aSRobert Watson static int fpath_readwrite_initialized;
589fa5f6b4SRobert Watson
599fa5f6b4SRobert Watson static void
try_io(const char * label,const char * fpathp,int asroot,int injail,int flags,int expected_error,int expected_errno)60d903306aSRobert Watson try_io(const char *label, const char *fpathp, int asroot, int injail,
61d903306aSRobert Watson int flags, int expected_error, int expected_errno)
629fa5f6b4SRobert Watson {
63d903306aSRobert Watson int fd;
649fa5f6b4SRobert Watson
65d903306aSRobert Watson fd = open(fpathp, flags);
66d903306aSRobert Watson if (fd < 0) {
67d903306aSRobert Watson if (expected_error != -1)
68d903306aSRobert Watson warnx("%s(%s, %s): expected (%d, %d) got (-1, %d)",
69d903306aSRobert Watson label, asroot ? "root" : "!root", injail ? "jail"
70d903306aSRobert Watson : "!jail", expected_error, expected_errno, errno);
719fa5f6b4SRobert Watson } else {
72d903306aSRobert Watson if (expected_error == -1)
73d903306aSRobert Watson warnx("%s(%s, %s): expected (%d, %d) got 0", label,
74d903306aSRobert Watson asroot ? "root" : "!root", injail ? "jail" :
75d903306aSRobert Watson "!jail", expected_error, expected_errno);
76d903306aSRobert Watson (void)close(fd);
779fa5f6b4SRobert Watson }
789fa5f6b4SRobert Watson }
799fa5f6b4SRobert Watson
80d903306aSRobert Watson int
priv_vfs_readwrite_fowner_setup(int asroot,int injail,struct test * test)81d903306aSRobert Watson priv_vfs_readwrite_fowner_setup(int asroot, int injail, struct test *test)
829fa5f6b4SRobert Watson {
839fa5f6b4SRobert Watson
84d903306aSRobert Watson setup_file("priv_vfs_readwrite_fowner_setup: fpath_none", fpath_none,
85d903306aSRobert Watson asroot ? UID_ROOT : UID_OWNER, GID_OTHER, 0000);
86d903306aSRobert Watson fpath_none_initialized = 1;
87d903306aSRobert Watson setup_file("priv_vfs_readwrite_fowner_setup: fpath_read", fpath_read,
88d903306aSRobert Watson asroot ? UID_ROOT : UID_OWNER, GID_OTHER, 0400);
89d903306aSRobert Watson fpath_read_initialized = 1;
90d903306aSRobert Watson setup_file("priv_vfs_readwrite_fowner_setup: fpath_write",
91d903306aSRobert Watson fpath_write, asroot ? UID_ROOT : UID_OWNER, GID_OTHER, 0200);
92d903306aSRobert Watson fpath_write_initialized = 1;
93d903306aSRobert Watson setup_file("priv_vfs_readwrite_fowner_setup: fpath_readwrite",
94d903306aSRobert Watson fpath_readwrite, asroot ? UID_ROOT : UID_OWNER, GID_OTHER, 0600);
95d903306aSRobert Watson fpath_readwrite_initialized = 1;
96d903306aSRobert Watson return (0);
97d903306aSRobert Watson }
98d903306aSRobert Watson
99d903306aSRobert Watson int
priv_vfs_readwrite_fgroup_setup(int asroot,int injail,struct test * test)100d903306aSRobert Watson priv_vfs_readwrite_fgroup_setup(int asroot, int injail, struct test *test)
101d903306aSRobert Watson {
102d903306aSRobert Watson
103d903306aSRobert Watson setup_file("priv_vfs_readwrite_fgroup_setup: fpath_none", fpath_none,
104d903306aSRobert Watson UID_OTHER, asroot ? GID_WHEEL : GID_OWNER, 0000);
105d903306aSRobert Watson fpath_none_initialized = 1;
106d903306aSRobert Watson setup_file("priv_vfs_readwrite_fgroup_setup: fpath_read", fpath_read,
107d903306aSRobert Watson UID_OTHER, asroot ? GID_WHEEL : GID_OWNER, 0040);
108d903306aSRobert Watson fpath_read_initialized = 1;
109d903306aSRobert Watson setup_file("priv_vfs_readwrite_fgroup_setup: fpath_write",
110d903306aSRobert Watson fpath_write, UID_OTHER, asroot ? GID_WHEEL : GID_OWNER, 0020);
111d903306aSRobert Watson fpath_write_initialized = 1;
112d903306aSRobert Watson setup_file("priv_vfs_readwrite_fgroup_setup: fpath_readwrite",
113d903306aSRobert Watson fpath_readwrite, UID_OTHER, asroot ? GID_WHEEL : GID_OWNER,
114d903306aSRobert Watson 0060);
115d903306aSRobert Watson fpath_readwrite_initialized = 1;
116d903306aSRobert Watson return (0);
117d903306aSRobert Watson }
118d903306aSRobert Watson
119d903306aSRobert Watson int
priv_vfs_readwrite_fother_setup(int asroot,int injail,struct test * test)120d903306aSRobert Watson priv_vfs_readwrite_fother_setup(int asroot, int injail, struct test *test)
121d903306aSRobert Watson {
122d903306aSRobert Watson
123d903306aSRobert Watson setup_file("priv_vfs_readwrite_fother_setup: fpath_none", fpath_none,
124d903306aSRobert Watson UID_OTHER, GID_OTHER, 0000);
125d903306aSRobert Watson fpath_none_initialized = 1;
126d903306aSRobert Watson setup_file("priv_vfs_readwrite_fother_setup: fpath_read", fpath_read,
127d903306aSRobert Watson UID_OTHER, GID_OTHER, 0004);
128d903306aSRobert Watson fpath_read_initialized = 1;
129d903306aSRobert Watson setup_file("priv_vfs_readwrite_fother_setup: fpath_write",
130d903306aSRobert Watson fpath_write, UID_OTHER, GID_OTHER, 0002);
131d903306aSRobert Watson fpath_write_initialized = 1;
132d903306aSRobert Watson setup_file("priv_vfs_readwrite_fother_setup: fpath_readwrite",
133d903306aSRobert Watson fpath_readwrite, UID_OTHER, GID_OTHER, 0006);
134d903306aSRobert Watson fpath_readwrite_initialized = 1;
135d903306aSRobert Watson return (0);
1369fa5f6b4SRobert Watson }
1379fa5f6b4SRobert Watson
1389fa5f6b4SRobert Watson void
priv_vfs_readwrite_fowner(int asroot,int injail,struct test * test)139d903306aSRobert Watson priv_vfs_readwrite_fowner(int asroot, int injail, struct test *test)
1409fa5f6b4SRobert Watson {
1419fa5f6b4SRobert Watson
142d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(none, O_RDONLY)", fpath_none,
143d903306aSRobert Watson asroot, injail, O_RDONLY, asroot ? 0 : -1, EACCES);
144d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(none, O_WRONLY)", fpath_none,
145d903306aSRobert Watson asroot, injail, O_WRONLY, asroot ? 0 : -1, EACCES);
146d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(none, O_RDWR)", fpath_none,
147d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
1489fa5f6b4SRobert Watson
149d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(read, O_RDONLY)", fpath_read,
150d903306aSRobert Watson asroot, injail, O_RDONLY, 0, 0);
151d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(read, O_WRONLY)", fpath_read,
152d903306aSRobert Watson asroot, injail, O_WRONLY, asroot ? 0 : -1, EACCES);
153d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(read, O_RDWR)", fpath_read,
154d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
1559fa5f6b4SRobert Watson
156d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(write, O_RDONLY)", fpath_write,
157d903306aSRobert Watson asroot, injail, O_RDONLY, asroot ? 0 : -1, EACCES);
158d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(write, O_WRONLY)", fpath_write,
159d903306aSRobert Watson asroot, injail, O_WRONLY, 0, 0);
160d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(write, O_RDWR)", fpath_write,
161d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
1629fa5f6b4SRobert Watson
163d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(write, O_RDONLY)", fpath_readwrite,
164d903306aSRobert Watson asroot, injail, O_RDONLY, 0, 0);
165d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(write, O_WRONLY)", fpath_readwrite,
166d903306aSRobert Watson asroot, injail, O_WRONLY, 0, 0);
167d903306aSRobert Watson try_io("priv_vfs_readwrite_fowner(write, O_RDWR)", fpath_readwrite,
168d903306aSRobert Watson asroot, injail, O_RDWR, 0, 0);
1699fa5f6b4SRobert Watson }
1709fa5f6b4SRobert Watson
1719fa5f6b4SRobert Watson void
priv_vfs_readwrite_fgroup(int asroot,int injail,struct test * test)172d903306aSRobert Watson priv_vfs_readwrite_fgroup(int asroot, int injail, struct test *test)
1739fa5f6b4SRobert Watson {
1749fa5f6b4SRobert Watson
175d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(none, O_RDONLY)", fpath_none,
176d903306aSRobert Watson asroot, injail, O_RDONLY, asroot ? 0 : -1, EACCES);
177d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(none, O_WRONLY)", fpath_none,
178d903306aSRobert Watson asroot, injail, O_WRONLY, asroot ? 0 : -1, EACCES);
179d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(none, O_RDWR)", fpath_none,
180d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
1819fa5f6b4SRobert Watson
182d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(read, O_RDONLY)", fpath_read,
183d903306aSRobert Watson asroot, injail, O_RDONLY, 0, 0);
184d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(read, O_WRONLY)", fpath_read,
185d903306aSRobert Watson asroot, injail, O_WRONLY, asroot ? 0 : -1, EACCES);
186d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(read, O_RDWR)", fpath_read,
187d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
1889fa5f6b4SRobert Watson
189d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(write, O_RDONLY)", fpath_write,
190d903306aSRobert Watson asroot, injail, O_RDONLY, asroot ? 0 : -1, EACCES);
191d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(write, O_WRONLY)", fpath_write,
192d903306aSRobert Watson asroot, injail, O_WRONLY, 0, 0);
193d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(write, O_RDWR)", fpath_write,
194d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
1959fa5f6b4SRobert Watson
196d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(write, O_RDONLY)", fpath_readwrite,
197d903306aSRobert Watson asroot, injail, O_RDONLY, 0, 0);
198d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(write, O_WRONLY)", fpath_readwrite,
199d903306aSRobert Watson asroot, injail, O_WRONLY, 0, 0);
200d903306aSRobert Watson try_io("priv_vfs_readwrite_fgroup(write, O_RDWR)", fpath_readwrite,
201d903306aSRobert Watson asroot, injail, O_RDWR, 0, 0);
202d903306aSRobert Watson }
2039fa5f6b4SRobert Watson
204d903306aSRobert Watson void
priv_vfs_readwrite_fother(int asroot,int injail,struct test * test)205d903306aSRobert Watson priv_vfs_readwrite_fother(int asroot, int injail, struct test *test)
206d903306aSRobert Watson {
2079fa5f6b4SRobert Watson
208d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(none, O_RDONLY)", fpath_none,
209d903306aSRobert Watson asroot, injail, O_RDONLY, asroot ? 0 : -1, EACCES);
210d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(none, O_WRONLY)", fpath_none,
211d903306aSRobert Watson asroot, injail, O_WRONLY, asroot ? 0 : -1, EACCES);
212d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(none, O_RDWR)", fpath_none,
213d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
2149fa5f6b4SRobert Watson
215d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(read, O_RDONLY)", fpath_read,
216d903306aSRobert Watson asroot, injail, O_RDONLY, 0, 0);
217d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(read, O_WRONLY)", fpath_read,
218d903306aSRobert Watson asroot, injail, O_WRONLY, asroot ? 0 : -1, EACCES);
219d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(read, O_RDWR)", fpath_read,
220d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
2219fa5f6b4SRobert Watson
222d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(write, O_RDONLY)", fpath_write,
223d903306aSRobert Watson asroot, injail, O_RDONLY, asroot ? 0 : -1, EACCES);
224d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(write, O_WRONLY)", fpath_write,
225d903306aSRobert Watson asroot, injail, O_WRONLY, 0, 0);
226d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(write, O_RDWR)", fpath_write,
227d903306aSRobert Watson asroot, injail, O_RDWR, asroot ? 0 : -1, EACCES);
2289fa5f6b4SRobert Watson
229d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(write, O_RDONLY)", fpath_readwrite,
230d903306aSRobert Watson asroot, injail, O_RDONLY, 0, 0);
231d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(write, O_WRONLY)", fpath_readwrite,
232d903306aSRobert Watson asroot, injail, O_WRONLY, 0, 0);
233d903306aSRobert Watson try_io("priv_vfs_readwrite_fother(write, O_RDWR)", fpath_readwrite,
234d903306aSRobert Watson asroot, injail, O_RDWR, 0, 0);
235d903306aSRobert Watson }
2369fa5f6b4SRobert Watson
237d903306aSRobert Watson void
priv_vfs_readwrite_cleanup(int asroot,int injail,struct test * test)238d903306aSRobert Watson priv_vfs_readwrite_cleanup(int asroot, int injail, struct test *test)
239d903306aSRobert Watson {
2409fa5f6b4SRobert Watson
241d903306aSRobert Watson if (fpath_none_initialized) {
242d903306aSRobert Watson (void)unlink(fpath_none);
243d903306aSRobert Watson fpath_none_initialized = 0;
244d903306aSRobert Watson }
245d903306aSRobert Watson if (fpath_read_initialized) {
246d903306aSRobert Watson (void)unlink(fpath_read);
247d903306aSRobert Watson fpath_read_initialized = 0;
248d903306aSRobert Watson }
249d903306aSRobert Watson if (fpath_write_initialized) {
250d903306aSRobert Watson (void)unlink(fpath_write);
251d903306aSRobert Watson fpath_write_initialized = 0;
252d903306aSRobert Watson }
253d903306aSRobert Watson if (fpath_readwrite_initialized) {
254d903306aSRobert Watson (void)unlink(fpath_readwrite);
255d903306aSRobert Watson fpath_readwrite_initialized = 0;
256d903306aSRobert Watson }
2579fa5f6b4SRobert Watson }
258