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 * Test privilege check on /dev/io. By default, the permissions also protect
339fa5f6b4SRobert Watson * against non-superuser access, so this program will modify permissions on
34d903306aSRobert Watson * /dev/io to allow world access, and revert the change on exit. This is not
35d903306aSRobert Watson * good for run-time security, but is necessary to test the checks properly.
369fa5f6b4SRobert Watson */
379fa5f6b4SRobert Watson
389fa5f6b4SRobert Watson #include <sys/types.h>
399fa5f6b4SRobert Watson #include <sys/stat.h>
409fa5f6b4SRobert Watson
419fa5f6b4SRobert Watson #include <err.h>
429fa5f6b4SRobert Watson #include <errno.h>
439fa5f6b4SRobert Watson #include <fcntl.h>
449fa5f6b4SRobert Watson #include <unistd.h>
459fa5f6b4SRobert Watson
469fa5f6b4SRobert Watson #include "main.h"
479fa5f6b4SRobert Watson
48d903306aSRobert Watson #define NEW_PERMS 0666
499fa5f6b4SRobert Watson #define DEV_IO "/dev/io"
509fa5f6b4SRobert Watson #define EXPECTED_PERMS 0600
519fa5f6b4SRobert Watson
52d903306aSRobert Watson static int initialized;
539fa5f6b4SRobert Watson static mode_t saved_perms;
549fa5f6b4SRobert Watson
55d903306aSRobert Watson int
priv_io_setup(int asroot,int asjail,struct test * test)56d903306aSRobert Watson priv_io_setup(int asroot, int asjail, struct test *test)
579fa5f6b4SRobert Watson {
589fa5f6b4SRobert Watson struct stat sb;
599fa5f6b4SRobert Watson
60d903306aSRobert Watson if (stat(DEV_IO, &sb) < 0) {
61d903306aSRobert Watson warn("priv_io_setup: stat(%s)", DEV_IO);
62d903306aSRobert Watson return (-1);
63d903306aSRobert Watson }
649fa5f6b4SRobert Watson saved_perms = sb.st_mode & ALLPERMS;
65d903306aSRobert Watson if (saved_perms != EXPECTED_PERMS) {
66d903306aSRobert Watson warnx("priv_io_setup: perms = 0%o; expected 0%o",
67d903306aSRobert Watson saved_perms, EXPECTED_PERMS);
68d903306aSRobert Watson return (-1);
699fa5f6b4SRobert Watson }
70d903306aSRobert Watson if (chmod(DEV_IO, NEW_PERMS) < 0) {
71d903306aSRobert Watson warn("priv_io_setup: chmod(%s, 0%o)", DEV_IO, NEW_PERMS);
72d903306aSRobert Watson return (-1);
739fa5f6b4SRobert Watson }
74d903306aSRobert Watson initialized = 1;
75d903306aSRobert Watson return (0);
769fa5f6b4SRobert Watson }
779fa5f6b4SRobert Watson
789fa5f6b4SRobert Watson void
priv_io(int asroot,int injail,struct test * test)79d903306aSRobert Watson priv_io(int asroot, int injail, struct test *test)
80d903306aSRobert Watson {
81d903306aSRobert Watson int error, fd;
82d903306aSRobert Watson
83d903306aSRobert Watson fd = open(DEV_IO, O_RDONLY);
84d903306aSRobert Watson if (fd < 0)
85d903306aSRobert Watson error = -1;
86d903306aSRobert Watson else
87d903306aSRobert Watson error = 0;
88d903306aSRobert Watson if (asroot && injail)
89d903306aSRobert Watson expect("priv_io(asroot, injail)", error, -1, EPERM);
90d903306aSRobert Watson if (asroot && !injail)
91d903306aSRobert Watson expect("priv_io(asroot, !injail)", error, 0, 0);
92d903306aSRobert Watson if (!asroot && injail)
93d903306aSRobert Watson expect("priv_io(!asroot, injail)", error, -1, EPERM);
94d903306aSRobert Watson if (!asroot && !injail)
95d903306aSRobert Watson expect("priv_io(!asroot, !injail)", error, -1, EPERM);
96d903306aSRobert Watson if (fd != -1)
97d903306aSRobert Watson close(fd);
98d903306aSRobert Watson }
99d903306aSRobert Watson
100d903306aSRobert Watson void
priv_io_cleanup(int asroot,int asjail,struct test * test)101d903306aSRobert Watson priv_io_cleanup(int asroot, int asjail, struct test *test)
1029fa5f6b4SRobert Watson {
1039fa5f6b4SRobert Watson
104d903306aSRobert Watson if (!initialized)
105d903306aSRobert Watson return;
106d903306aSRobert Watson if (chmod(DEV_IO, saved_perms) < 0)
107d903306aSRobert Watson err(-1, "priv_io_cleanup: chmod(%s, 0%o)", DEV_IO,
108d903306aSRobert Watson saved_perms);
109d903306aSRobert Watson initialized = 0;
1109fa5f6b4SRobert Watson }
111