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);
ATF_TC_HEAD(mknod_err,tc)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 
ATF_TC_BODY(mknod_err,tc)55 ATF_TC_BODY(mknod_err, tc)
56 {
57           char buf[PATH_MAX + 1];
58 
59           (void)memset(buf, 'x', sizeof(buf));
60 
61           errno = 0;
62           ATF_REQUIRE_ERRNO(EINVAL, mknod(path, S_IFCHR, -1) == -1);
63 
64           errno = 0;
65           ATF_REQUIRE_ERRNO(ENAMETOOLONG, mknod(buf, S_IFCHR, 0) == -1);
66 
67           errno = 0;
68           ATF_REQUIRE_ERRNO(EFAULT, mknod((char *)-1, S_IFCHR, 0) == -1);
69 
70           errno = 0;
71           ATF_REQUIRE_ERRNO(ENOENT, mknod("/a/b/c/d/e/f/g", S_IFCHR, 0) == -1);
72 }
73 
ATF_TC_CLEANUP(mknod_err,tc)74 ATF_TC_CLEANUP(mknod_err, tc)
75 {
76           (void)unlink(path);
77 }
78 
79 ATF_TC_WITH_CLEANUP(mknod_exist);
ATF_TC_HEAD(mknod_exist,tc)80 ATF_TC_HEAD(mknod_exist, tc)
81 {
82           atf_tc_set_md_var(tc, "descr", "Test EEXIST from mknod(2)");
83           atf_tc_set_md_var(tc, "require.user", "root");
84 }
85 
ATF_TC_BODY(mknod_exist,tc)86 ATF_TC_BODY(mknod_exist, tc)
87 {
88           int fd;
89 
90           fd = open("/etc/passwd", O_RDONLY);
91 
92           if (fd >= 0) {
93 
94                     (void)close(fd);
95 
96                     errno = 0;
97                     ATF_REQUIRE_ERRNO(EEXIST,
98                         mknod("/etc/passwd", S_IFCHR, 0) == -1);
99           }
100 
101           ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
102 
103           errno = 0;
104           ATF_REQUIRE_ERRNO(EEXIST, mknod(path, S_IFCHR, 0) == -1);
105 
106           ATF_REQUIRE(unlink(path) == 0);
107 }
108 
ATF_TC_CLEANUP(mknod_exist,tc)109 ATF_TC_CLEANUP(mknod_exist, tc)
110 {
111           (void)unlink(path);
112 }
113 
114 ATF_TC_WITH_CLEANUP(mknod_perm);
ATF_TC_HEAD(mknod_perm,tc)115 ATF_TC_HEAD(mknod_perm, tc)
116 {
117           atf_tc_set_md_var(tc, "descr", "Test permissions of mknod(2)");
118           atf_tc_set_md_var(tc, "require.user", "unprivileged");
119 }
120 
ATF_TC_BODY(mknod_perm,tc)121 ATF_TC_BODY(mknod_perm, tc)
122 {
123 
124           errno = 0;
125           ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFCHR, 0) == -1);
126 
127           errno = 0;
128           ATF_REQUIRE_ERRNO(EPERM, mknod(path, S_IFBLK, 0) == -1);
129 }
130 
ATF_TC_CLEANUP(mknod_perm,tc)131 ATF_TC_CLEANUP(mknod_perm, tc)
132 {
133           (void)unlink(path);
134 }
135 
136 ATF_TC_WITH_CLEANUP(mknod_stat);
ATF_TC_HEAD(mknod_stat,tc)137 ATF_TC_HEAD(mknod_stat, tc)
138 {
139           atf_tc_set_md_var(tc, "descr", "A basic test of mknod(2)");
140           atf_tc_set_md_var(tc, "require.user", "root");
141 }
142 
ATF_TC_BODY(mknod_stat,tc)143 ATF_TC_BODY(mknod_stat, tc)
144 {
145           struct stat st;
146 
147           (void)memset(&st, 0, sizeof(struct stat));
148 
149           ATF_REQUIRE(mknod(path, S_IFCHR, 0) == 0);
150           ATF_REQUIRE(stat(path, &st) == 0);
151 
152           if (S_ISCHR(st.st_mode) == 0)
153                     atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFCHR)");
154 
155           ATF_REQUIRE(unlink(path) == 0);
156 
157           (void)memset(&st, 0, sizeof(struct stat));
158 
159           ATF_REQUIRE(mknod(path, S_IFBLK, 0) == 0);
160           ATF_REQUIRE(stat(path, &st) == 0);
161 
162           if (S_ISBLK(st.st_mode) == 0)
163                     atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFBLK)");
164 
165           ATF_REQUIRE(unlink(path) == 0);
166 
167           (void)memset(&st, 0, sizeof(struct stat));
168 
169           ATF_REQUIRE(mknod(path, S_IFREG, 0) == 0);
170           ATF_REQUIRE(stat(path, &st) == 0);
171 
172           if (S_ISREG(st.st_mode) == 0)
173                     atf_tc_fail_nonfatal("invalid mode from mknod(2) (S_IFREG)");
174 
175           ATF_REQUIRE(unlink(path) == 0);
176 }
177 
ATF_TC_CLEANUP(mknod_stat,tc)178 ATF_TC_CLEANUP(mknod_stat, tc)
179 {
180           (void)unlink(path);
181 }
182 
ATF_TP_ADD_TCS(tp)183 ATF_TP_ADD_TCS(tp)
184 {
185 
186           ATF_TP_ADD_TC(tp, mknod_err);
187           ATF_TP_ADD_TC(tp, mknod_exist);
188           ATF_TP_ADD_TC(tp, mknod_perm);
189           ATF_TP_ADD_TC(tp, mknod_stat);
190 
191           return atf_no_error();
192 }
193