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