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