xref: /freebsd/tests/sys/fs/fusefs/mknod.cc (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <fcntl.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 }
38 
39 #include "mockfs.hh"
40 #include "utils.hh"
41 
42 using namespace testing;
43 
44 #ifndef VNOVAL
45 #define VNOVAL (-1)	/* Defined in sys/vnode.h */
46 #endif
47 
48 const char FULLPATH[] = "mountpoint/some_file.txt";
49 const char RELPATH[] = "some_file.txt";
50 
51 class Mknod: public FuseTest {
52 
53 mode_t m_oldmask;
54 const static mode_t c_umask = 022;
55 
56 public:
57 
58 virtual void SetUp() {
59 	m_oldmask = umask(c_umask);
60 	if (geteuid() != 0) {
61 		GTEST_SKIP() << "Only root may use most mknod(2) variations";
62 	}
63 	FuseTest::SetUp();
64 }
65 
66 virtual void TearDown() {
67 	FuseTest::TearDown();
68 	(void)umask(m_oldmask);
69 }
70 
71 /* Test an OK creation of a file with the given mode and device number */
72 void expect_mknod(mode_t mode, dev_t dev) {
73 	uint64_t ino = 42;
74 
75 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
76 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
77 
78 	EXPECT_CALL(*m_mock, process(
79 		ResultOf([=](auto in) {
80 			const char *name = (const char*)in.body.bytes +
81 				sizeof(fuse_mknod_in);
82 			return (in.header.opcode == FUSE_MKNOD &&
83 				in.body.mknod.mode == mode &&
84 				in.body.mknod.rdev == (uint32_t)dev &&
85 				in.body.mknod.umask == c_umask &&
86 				(0 == strcmp(RELPATH, name)));
87 		}, Eq(true)),
88 		_)
89 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
90 		SET_OUT_HEADER_LEN(out, entry);
91 		out.body.entry.attr.mode = mode;
92 		out.body.entry.nodeid = ino;
93 		out.body.entry.entry_valid = UINT64_MAX;
94 		out.body.entry.attr_valid = UINT64_MAX;
95 		out.body.entry.attr.rdev = dev;
96 	})));
97 }
98 
99 };
100 
101 class Mknod_7_11: public FuseTest {
102 public:
103 virtual void SetUp() {
104 	m_kernel_minor_version = 11;
105 	if (geteuid() != 0) {
106 		GTEST_SKIP() << "Only root may use most mknod(2) variations";
107 	}
108 	FuseTest::SetUp();
109 }
110 
111 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
112 {
113 	FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
114 }
115 
116 /* Test an OK creation of a file with the given mode and device number */
117 void expect_mknod(mode_t mode, dev_t dev) {
118 	uint64_t ino = 42;
119 
120 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
121 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
122 
123 	EXPECT_CALL(*m_mock, process(
124 		ResultOf([=](auto in) {
125 			const char *name = (const char*)in.body.bytes +
126 				FUSE_COMPAT_MKNOD_IN_SIZE;
127 			return (in.header.opcode == FUSE_MKNOD &&
128 				in.body.mknod.mode == mode &&
129 				in.body.mknod.rdev == (uint32_t)dev &&
130 				(0 == strcmp(RELPATH, name)));
131 		}, Eq(true)),
132 		_)
133 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
134 		SET_OUT_HEADER_LEN(out, entry);
135 		out.body.entry.attr.mode = mode;
136 		out.body.entry.nodeid = ino;
137 		out.body.entry.entry_valid = UINT64_MAX;
138 		out.body.entry.attr_valid = UINT64_MAX;
139 		out.body.entry.attr.rdev = dev;
140 	})));
141 }
142 
143 };
144 
145 /*
146  * mknod(2) should be able to create block devices on a FUSE filesystem.  Even
147  * though FreeBSD doesn't use block devices, this is useful when copying media
148  * from or preparing media for other operating systems.
149  */
150 TEST_F(Mknod, blk)
151 {
152 	mode_t mode = S_IFBLK | 0755;
153 	dev_t rdev = 0xfe00; /* /dev/vda's device number on Linux */
154 	expect_mknod(mode, rdev);
155 	EXPECT_EQ(0, mknod(FULLPATH, mode, rdev)) << strerror(errno);
156 }
157 
158 TEST_F(Mknod, chr)
159 {
160 	mode_t mode = S_IFCHR | 0755;
161 	dev_t rdev = 54;			/* /dev/fuse's device number */
162 	expect_mknod(mode, rdev);
163 	EXPECT_EQ(0, mknod(FULLPATH, mode, rdev)) << strerror(errno);
164 }
165 
166 /*
167  * The daemon is responsible for checking file permissions (unless the
168  * default_permissions mount option was used)
169  */
170 TEST_F(Mknod, eperm)
171 {
172 	mode_t mode = S_IFIFO | 0755;
173 
174 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
175 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
176 
177 	EXPECT_CALL(*m_mock, process(
178 		ResultOf([=](auto in) {
179 			const char *name = (const char*)in.body.bytes +
180 				sizeof(fuse_mknod_in);
181 			return (in.header.opcode == FUSE_MKNOD &&
182 				in.body.mknod.mode == mode &&
183 				(0 == strcmp(RELPATH, name)));
184 		}, Eq(true)),
185 		_)
186 	).WillOnce(Invoke(ReturnErrno(EPERM)));
187 	EXPECT_NE(0, mkfifo(FULLPATH, mode));
188 	EXPECT_EQ(EPERM, errno);
189 }
190 
191 TEST_F(Mknod, fifo)
192 {
193 	mode_t mode = S_IFIFO | 0755;
194 	dev_t rdev = VNOVAL;		/* Fifos don't have device numbers */
195 	expect_mknod(mode, rdev);
196 	EXPECT_EQ(0, mkfifo(FULLPATH, mode)) << strerror(errno);
197 }
198 
199 /*
200  * Create a unix-domain socket.
201  *
202  * This test case doesn't actually need root privileges.
203  */
204 TEST_F(Mknod, socket)
205 {
206 	mode_t mode = S_IFSOCK | 0755;
207 	struct sockaddr_un sa;
208 	int fd;
209 	dev_t rdev = -1;	/* Really it's a don't care */
210 
211 	expect_mknod(mode, rdev);
212 
213 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
214 	ASSERT_LE(0, fd) << strerror(errno);
215 	sa.sun_family = AF_UNIX;
216 	strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path));
217 	ASSERT_EQ(0, bind(fd, (struct sockaddr*)&sa, sizeof(sa)))
218 		<< strerror(errno);
219 }
220 
221 /*
222  * fusefs(5) lacks VOP_WHITEOUT support.  No bugzilla entry, because that's a
223  * feature, not a bug
224  */
225 TEST_F(Mknod, DISABLED_whiteout)
226 {
227 	mode_t mode = S_IFWHT | 0755;
228 	dev_t rdev = VNOVAL;	/* whiteouts don't have device numbers */
229 	expect_mknod(mode, rdev);
230 	EXPECT_EQ(0, mknod(FULLPATH, mode, 0)) << strerror(errno);
231 }
232 
233 /* A server built at protocol version 7.11 or earlier can still use mknod */
234 TEST_F(Mknod_7_11, fifo)
235 {
236 	mode_t mode = S_IFIFO | 0755;
237 	dev_t rdev = VNOVAL;
238 	expect_mknod(mode, rdev);
239 	EXPECT_EQ(0, mkfifo(FULLPATH, mode)) << strerror(errno);
240 }
241