1 /* $OpenBSD: t_mkdir.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2 /* $NetBSD: t_mkdir.c,v 1.2 2011/10/15 07:38:31 jruoho Exp $ */
3
4 /*-
5 * Copyright (c) 2008, 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe and Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "macros.h"
34
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37
38 #include "atf-c.h"
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 ATF_TC(mkdir_err);
ATF_TC_HEAD(mkdir_err,tc)49 ATF_TC_HEAD(mkdir_err, tc)
50 {
51 atf_tc_set_md_var(tc, "descr", "Checks errors from mkdir(2)");
52 }
53
ATF_TC_BODY(mkdir_err,tc)54 ATF_TC_BODY(mkdir_err, tc)
55 {
56 char buf[PATH_MAX + 1];
57 int fd;
58
59 (void)memset(buf, 'x', sizeof(buf));
60
61 fd = open("/etc", O_RDONLY);
62
63 if (fd >= 0) {
64
65 (void)close(fd);
66
67 errno = 0;
68 ATF_REQUIRE_ERRNO(EEXIST, mkdir("/etc", 0500) == -1);
69 }
70
71 errno = 0;
72 ATF_REQUIRE_ERRNO(EFAULT, mkdir((void *)-1, 0500) == -1);
73
74 errno = 0;
75 ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkdir(buf, 0500) == -1);
76
77 errno = 0;
78 ATF_REQUIRE_ERRNO(ENOENT, mkdir("/a/b/c/d/e/f/g/h/i/j/k", 0500) == -1);
79 }
80
81 ATF_TC_WITH_CLEANUP(mkdir_perm);
ATF_TC_HEAD(mkdir_perm,tc)82 ATF_TC_HEAD(mkdir_perm, tc)
83 {
84 atf_tc_set_md_var(tc, "descr", "Checks permissions with mkdir(2)");
85 atf_tc_set_md_var(tc, "require.user", "unprivileged");
86 }
87
ATF_TC_BODY(mkdir_perm,tc)88 ATF_TC_BODY(mkdir_perm, tc)
89 {
90 errno = 0;
91 ATF_REQUIRE_ERRNO(EACCES, mkdir("/usr/__nonexistent__", 0500) == -1);
92 }
93
ATF_TC_CLEANUP(mkdir_perm,tc)94 ATF_TC_CLEANUP(mkdir_perm, tc)
95 {
96 (void)rmdir("/usr/__nonexistent__");
97 }
98
99 ATF_TC_WITH_CLEANUP(mkdir_mode);
ATF_TC_HEAD(mkdir_mode,tc)100 ATF_TC_HEAD(mkdir_mode, tc)
101 {
102 atf_tc_set_md_var(tc, "descr", "Test that UIDs and GIDs are right "
103 "for a directory created with mkdir(2)");
104 atf_tc_set_md_var(tc, "require.user", "root");
105 }
106
ATF_TC_BODY(mkdir_mode,tc)107 ATF_TC_BODY(mkdir_mode, tc)
108 {
109 static const char *path = "/tmp/mkdir";
110 struct stat st_a, st_b;
111 struct passwd *pw;
112 pid_t pid;
113 int sta;
114
115 (void)memset(&st_a, 0, sizeof(struct stat));
116 (void)memset(&st_b, 0, sizeof(struct stat));
117
118 pw = getpwnam("nobody");
119
120 ATF_REQUIRE(pw != NULL);
121 ATF_REQUIRE(stat("/tmp", &st_a) == 0);
122
123 pid = fork();
124 ATF_REQUIRE(pid >= 0);
125
126 if (pid == 0) {
127
128 if (setuid(pw->pw_uid) != 0)
129 _exit(EXIT_FAILURE);
130
131 if (mkdir(path, 0500) != 0)
132 _exit(EXIT_FAILURE);
133
134 _exit(EXIT_SUCCESS);
135 }
136
137 (void)sleep(1);
138 (void)wait(&sta);
139
140 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
141 atf_tc_fail("failed to create '%s'", path);
142
143 ATF_REQUIRE(stat(path, &st_b) == 0);
144 ATF_REQUIRE(rmdir(path) == 0);
145
146 /*
147 * The directory's owner ID should be set to the
148 * effective UID, whereas the group ID should be
149 * set to that of the parent directory.
150 */
151 if (st_b.st_uid != pw->pw_uid)
152 atf_tc_fail("invalid UID for '%s'", path);
153
154 if (st_b.st_gid != st_a.st_gid)
155 atf_tc_fail("GID did not follow the parent directory");
156 }
157
ATF_TC_CLEANUP(mkdir_mode,tc)158 ATF_TC_CLEANUP(mkdir_mode, tc)
159 {
160 (void)rmdir("/tmp/mkdir");
161 }
162
163 ATF_TC(mkdir_trail);
ATF_TC_HEAD(mkdir_trail,tc)164 ATF_TC_HEAD(mkdir_trail, tc)
165 {
166 atf_tc_set_md_var(tc, "descr", "Checks mkdir(2) for trailing slashes");
167 }
168
ATF_TC_BODY(mkdir_trail,tc)169 ATF_TC_BODY(mkdir_trail, tc)
170 {
171 const char *tests[] = {
172 /*
173 * IEEE 1003.1 second ed. 2.2.2.78:
174 *
175 * If the pathname refers to a directory, it may also have
176 * one or more trailing slashes. Multiple successive slashes
177 * are considered to be the same as one slash.
178 */
179 "dir1/",
180 "dir2//",
181
182 NULL,
183 };
184
185 const char **test;
186
187 for (test = &tests[0]; *test != NULL; ++test) {
188
189 (void)printf("Checking \"%s\"\n", *test);
190 (void)rmdir(*test);
191
192 ATF_REQUIRE(mkdir(*test, 0777) == 0);
193 ATF_REQUIRE(rename(*test, "foo") == 0);
194 ATF_REQUIRE(rename("foo/", *test) == 0);
195 ATF_REQUIRE(rmdir(*test) == 0);
196 }
197 }
198
ATF_TP_ADD_TCS(tp)199 ATF_TP_ADD_TCS(tp)
200 {
201
202 ATF_TP_ADD_TC(tp, mkdir_err);
203 ATF_TP_ADD_TC(tp, mkdir_perm);
204 ATF_TP_ADD_TC(tp, mkdir_mode);
205 ATF_TP_ADD_TC(tp, mkdir_trail);
206
207 return atf_no_error();
208 }
209