xref: /freebsd/tools/regression/priv/priv_vfs_clearsugid.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 
31 /*
32  * There are three cases in which the file system will clear the setuid or
33  * setgid bits on a file when running unprivileged:
34  *
35  * - When the file is chown()'d and either of the uid or the gid is changed.
36  *   (currently, only changing the file gid applies, as privilege is required
37  *   to change the uid).
38  *
39  * - The file is written to successfully.
40  *
41  * - An extended attribute of the file is written to successfully.
42  *
43  * In each case, check that the flags are cleared if unprivileged, and that
44  * they aren't cleared if privileged.
45  *
46  * We can't use expect() as we're looking for side-effects rather than
47  * success/failure of the system call.
48  */
49 
50 #include <sys/types.h>
51 #include <sys/extattr.h>
52 #include <sys/stat.h>
53 
54 #include <err.h>
55 #include <fcntl.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "main.h"
61 
62 static char fpath[1024];
63 static int fpath_initialized;
64 
65 /*
66  * If running as root, check that SUID is still set; otherwise, check that it
67  * is not.
68  */
69 static void
70 confirm_sugid(char *test_case, int asroot, int injail)
71 {
72 	struct stat sb;
73 
74 	if (stat(fpath, &sb) < 0) {
75 		warn("%s stat(%s)", test_case, fpath);
76 		return;
77 	}
78 	if (asroot) {
79 		if (!(sb.st_mode & S_ISUID))
80 			warnx("%s(root, %s): !SUID", test_case, injail ?
81 			    "jail" : "!jail");
82 	} else {
83 		if (sb.st_mode & S_ISUID)
84 			warnx("%s(!root, %s): SUID", test_case, injail ?
85 			    "jail" : "!jail");
86 	}
87 }
88 
89 int
90 priv_vfs_clearsugid_setup(int asroot, int injail, struct test *test)
91 {
92 
93 	setup_file("priv_vfs_clearsugid_setup: fpath", fpath, UID_OWNER,
94 	    GID_OTHER, 0600 | S_ISUID);
95 	fpath_initialized = 1;
96 	return (0);
97 }
98 
99 void
100 priv_vfs_clearsugid_chgrp(int asroot, int injail, struct test *test)
101 {
102 
103 	if (chown(fpath, -1, asroot ? GID_WHEEL : GID_OWNER) < 0)
104 		err(-1, "priv_vfs_clearsugid_chgrp(%s, %s): chrgrp",
105 		    asroot ? "root" : "!root", injail ? "jail" : "!jail");
106 	confirm_sugid("priv_vfs_clearsugid_chgrp", asroot, injail);
107 }
108 
109 #define	EA_NAMESPACE	EXTATTR_NAMESPACE_USER
110 #define	EA_NAME		"clearsugid"
111 #define	EA_DATA		"test"
112 #define	EA_SIZE		(strlen(EA_DATA))
113 
114 void
115 priv_vfs_clearsugid_extattr(int asroot, int injail, struct test *test)
116 {
117 
118 	if (extattr_set_file(fpath, EA_NAMESPACE, EA_NAME, EA_DATA, EA_SIZE)
119 	    < 0)
120 		err(-1,
121 		    "priv_vfs_clearsugid_extattr(%s, %s): extattr_set_file",
122 		    asroot ? "root" : "!root", injail ? "jail" : "!jail");
123 	confirm_sugid("priv_vfs_clearsugid_extattr", asroot, injail);
124 }
125 
126 void
127 priv_vfs_clearsugid_write(int asroot, int injail, struct test *test)
128 {
129 	int fd;
130 
131 	fd = open(fpath, O_RDWR);
132 	if (fd < 0)
133 		err(-1, "priv_vfs_clearsugid_write(%s, %s): open",
134 		    asroot ? "root" : "!root", injail ? "jail" : "!jail");
135 	if (write(fd, EA_DATA, EA_SIZE) < 0)
136 		err(-1, "priv_vfs_clearsugid_write(%s, %s): write",
137 		    asroot ? "root" : "!root", injail ? "jail" : "!jail");
138 	(void)close(fd);
139 	confirm_sugid("priv_vfs_clearsugid_write", asroot, injail);
140 }
141 
142 void
143 priv_vfs_clearsugid_cleanup(int asroot, int injail, struct test *test)
144 {
145 
146 	if (fpath_initialized) {
147 		(void)unlink(fpath);
148 		fpath_initialized = 0;
149 	}
150 }
151