1 /* $NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by 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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mknod.c,v 1.2 2012/03/18 07:00:52 jruoho Exp $");
33 
34 #include <sys/stat.h>
35 
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 static char	 path[] = "node";
46 
47 ATF_TC_WITH_CLEANUP(mknod_err);
48 ATF_TC_HEAD(mknod_err, tc)
49 {
50 	atf_tc_set_md_var(tc, "descr",
51 	    "Test error conditions of mknod(2) (PR kern/45111)");
52 	atf_tc_set_md_var(tc, "require.user", "root");
53 }
54 
55 ATF_TC_BODY(mknod_err, tc)
56 {
57 	char buf[PATH_MAX + 1];
58 
59 	(void)memset(buf, 'x', sizeof(buf));
60 
61 #ifndef __FreeBSD__
62 	/*
63 	 * As of FreeBSD 6.0 device nodes may be created in regular file systems but
64 	 * such nodes cannot be used to access devices. As a result an invalid dev
65 	 * argument is unchecked.
66 	 */
67 	errno = 0;
68 	ATF_REQUIRE_ERRNO(EINVAL, mknod(path, S_IFCHR, -1) == -1);
69 #endif
70 
71 	errno = 0;
72 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, mknod(buf, S_IFCHR, 0) == -1);
73 
74 	errno = 0;
75 	ATF_REQUIRE_ERRNO(EFAULT, mknod((char *)-1, S_IFCHR, 0) == -1);
76 
77 	errno = 0;
78 	ATF_REQUIRE_ERRNO(ENOENT, mknod("/a/b/c/d/e/f/g", S_IFCHR, 0) == -1);
79 }
80 
81 ATF_TC_CLEANUP(mknod_err, tc)
82 {
83 	(void)unlink(path);
84 }
85 
86 ATF_TC_WITH_CLEANUP(mknod_exist);
87 ATF_TC_HEAD(mknod_exist, tc)
88 {
89 	atf_tc_set_md_var(tc, "descr", "Test EEXIST from mknod(2)");
90 	atf_tc_set_md_var(tc, "require.user", "root");
91 }
92 
93 ATF_TC_BODY(mknod_exist, tc)
94 {
95 	int fd;
96 
97 	fd = open("/etc/passwd", O_RDONLY);
98 
99 	if (fd >= 0) {
100 
101 		(void)close(fd);
102 
103 		errno = 0;
104 		ATF_REQUIRE_ERRNO(EEXIST,
105 		    mknod("/etc/passwd", S_IFCHR, 0) == -1);
106 	}
107 
108 	ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
109 
110 	errno = 0;
111 	ATF_REQUIRE_ERRNO(EEXIST, mknod(path, S_IFCHR, 0) == -1);
112 
113 	ATF_REQUIRE(unlink(path) == 0);
114 }
115 
116 ATF_TC_CLEANUP(mknod_exist, tc)
117 {
118 	(void)unlink(path);
119 }
120 
121 ATF_TC_WITH_CLEANUP(mknod_perm);
122 ATF_TC_HEAD(mknod_perm, tc)
123 {
124 	atf_tc_set_md_var(tc, "descr", "Test permissions of mknod(2)");
125 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
126 }
127 
128 ATF_TC_BODY(mknod_perm, tc)
129 {
130 
131 	errno = 0;
132 	ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFCHR, 0) == -1);
133 
134 	errno = 0;
135 	ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFBLK, 0) == -1);
136 }
137 
138 ATF_TC_CLEANUP(mknod_perm, tc)
139 {
140 	(void)unlink(path);
141 }
142 
143 ATF_TC_WITH_CLEANUP(mknod_stat);
144 ATF_TC_HEAD(mknod_stat, tc)
145 {
146 	atf_tc_set_md_var(tc, "descr", "A basic test of mknod(2)");
147 	atf_tc_set_md_var(tc, "require.user", "root");
148 }
149 
150 ATF_TC_BODY(mknod_stat, tc)
151 {
152 	struct stat st;
153 
154 	(void)memset(&st, 0, sizeof(struct stat));
155 
156 	ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
157 	ATF_REQUIRE(stat(path, &st) == 0);
158 
159 	if (S_ISCHR(st.st_mode) == 0)
160 		atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFCHR)");
161 
162 	ATF_REQUIRE(unlink(path) == 0);
163 
164 	(void)memset(&st, 0, sizeof(struct stat));
165 
166 	ATF_REQUIRE(mknod(path, S_IFBLK, 0) == 0);
167 	ATF_REQUIRE(stat(path, &st) == 0);
168 
169 	if (S_ISBLK(st.st_mode) == 0)
170 		atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFBLK)");
171 
172 	ATF_REQUIRE(unlink(path) == 0);
173 
174 	(void)memset(&st, 0, sizeof(struct stat));
175 
176 #ifdef __FreeBSD__
177 	atf_tc_expect_fail("mknod does not allow S_IFREG");
178 #endif
179 	ATF_REQUIRE(mknod(path, S_IFREG, 0) == 0);
180 	ATF_REQUIRE(stat(path, &st) == 0);
181 
182 	if (S_ISREG(st.st_mode) == 0)
183 		atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFREG)");
184 
185 	ATF_REQUIRE(unlink(path) == 0);
186 }
187 
188 ATF_TC_CLEANUP(mknod_stat, tc)
189 {
190 	(void)unlink(path);
191 }
192 
193 ATF_TP_ADD_TCS(tp)
194 {
195 
196 	ATF_TP_ADD_TC(tp, mknod_err);
197 	ATF_TP_ADD_TC(tp, mknod_exist);
198 	ATF_TP_ADD_TC(tp, mknod_perm);
199 	ATF_TP_ADD_TC(tp, mknod_stat);
200 
201 	return atf_no_error();
202 }
203