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 that privilege is required to set the sgid bit on a file with a group
34 * that isn't in the process credential. The file uid owner is set to the
35 * uid being tested with, as we are not interested in testing privileges
36 * associated with file ownership.
48 static const gid_t gidset_without[] = {GID_WHEEL};
49 static const gid_t gidset_with[] = {GID_WHEEL, GID_OWNER};
59 setup_file(fpath, UID_ROOT, GID_OWNER, 0644);
61 if (setgroups(1, gidset_without) < 0) {
62 warn("setgroups(1, {%d})", gidset_without[0]);
66 fd = open(fpath, O_RDWR);
68 warn("open(%s, O_RDWR)", fpath);
73 * With privilege, set mode on file.
75 if (fchmod(fd, 0600 | S_ISGID) < 0) {
76 warn("fchmod(%s, 0600 | S_ISGID) as root", fpath);
81 * Reset mode and chown file before dropping privilege.
83 if (fchmod(fd, 0600) < 0) {
84 warn("fchmod(%s, 0600) as root", fpath);
88 if (fchown(fd, UID_OWNER, GID_OWNER) < 0) {
89 warn("fchown(%s, %d, %d) as root", fpath, UID_OWNER,
100 * Without privilege, set mode on file.
102 error = fchmod(fd, 0600 | S_ISGID);
104 warnx("fchmod(%s, 0600 | S_ISGID) succeeded as !root",
108 if (errno != EPERM) {
109 warn("fchmod(%s, 0600 | S_ISGID) wrong errno %d as !root",
115 * Turn privilege back on so that we confirm privilege isn't required
116 * if we are a group member of the file's group.
120 if (setgroups(2, gidset_with) < 0) {
121 warn("setgroups(2, {%d, %d})", gidset_with[0],
126 if (seteuid(UID_OWNER) < 0) {
127 warn("seteuid(%d) pass 2", UID_OWNER);
132 * Without privilege, set mode on file (this time with right gid).
134 if (fchmod(fd, 0600 | S_ISGID) < 0) {
135 warnx("fchmod(%s, 0600 | S_ISGID) pass 2 as !root", fpath);