2 * Copyright (c) 2006 nCircle Network Security, Inc.
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
6 * Project under contract to nCircle Network Security, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 * Test privilege check on /dev/io. By default, the permissions also protect
34 * against non-superuser access, so this program will modify permissions on
35 * /dev/io to allow group access for the wheel group, and revert the change
36 * on exit. This is not good for run-time security, but is necessary to test
37 * the checks properly.
40 #include <sys/types.h>
50 #define NEW_PERMS 0660
51 #define DEV_IO "/dev/io"
52 #define EXPECTED_PERMS 0600
54 static mode_t saved_perms;
61 if (stat(DEV_IO, &sb) < 0)
62 err(-1, "save_perms: stat(%s)", DEV_IO);
64 saved_perms = sb.st_mode & ALLPERMS;
66 if (saved_perms != EXPECTED_PERMS)
67 err(-1, "save_perms: perms = 0%o; expected 0%o", saved_perms,
76 if (chmod(DEV_IO, NEW_PERMS) < 0)
77 err(-1, "set_perms: chmod(%s, 0%o)", DEV_IO, NEW_PERMS);
84 if (chmod(DEV_IO, saved_perms) < 0)
85 err(-1, "restore_perms: chmod(%s, 0%o)", DEV_IO, saved_perms);
89 try_open(const char *test_case, uid_t uid, int expected)
94 fd = open(DEV_IO, O_RDONLY);
97 warn("try_open: %s open(%s) errno %d", DEV_IO,
105 warn("try_open: %s open(%s) unexpected success", test_case,
110 if (errno == expected)
112 warn("try_open: %s open(%s) wrong errno %d, expected %d", DEV_IO,
113 test_case, errno, expected);
126 try_open("root:0600", UID_ROOT, 0);
127 try_open("other", UID_OTHER, EACCES);
131 try_open("root:0660", UID_ROOT, 0);
132 try_open("other", UID_OTHER, EPERM);