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