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