2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * Tests for the "allow_other" mount option. They must be in their own
33 * file so they can be run as root
37 #include <sys/types.h>
38 #include <sys/extattr.h>
47 using namespace testing;
49 const static char FULLPATH[] = "mountpoint/some_file.txt";
50 const static char RELPATH[] = "some_file.txt";
52 class NoAllowOther: public FuseTest {
55 /* Unprivileged user id */
58 virtual void SetUp() {
60 GTEST_SKIP() << "This test must be run as root";
67 class AllowOther: public NoAllowOther {
70 virtual void SetUp() {
72 NoAllowOther::SetUp();
76 TEST_F(AllowOther, allowed)
80 fork(true, &status, [&] {
83 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
84 expect_open(ino, 0, 1);
85 expect_flush(ino, 1, ReturnErrno(0));
86 expect_release(ino, FH);
90 fd = open(FULLPATH, O_RDONLY);
98 ASSERT_EQ(0, WEXITSTATUS(status));
101 /* Check that fusefs uses the correct credentials for FUSE operations */
102 TEST_F(AllowOther, creds)
108 get_unprivileged_id(&uid, &gid);
109 fork(true, &status, [=] {
110 EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) {
111 return (in->header.opcode == FUSE_LOOKUP &&
112 in->header.uid == uid &&
113 in->header.gid == gid);
117 .WillOnce(Invoke(ReturnErrno(ENOENT)));
119 eaccess(FULLPATH, F_OK);
123 ASSERT_EQ(0, WEXITSTATUS(status));
127 * A variation of the Open.multiple_creds test showing how the bug can lead to a
128 * privilege elevation. The first process is privileged and opens a file only
129 * visible to root. The second process is unprivileged and shouldn't be able
130 * to open the file, but does thanks to the bug
132 TEST_F(AllowOther, privilege_escalation)
134 const static char FULLPATH[] = "mountpoint/some_file.txt";
135 const static char RELPATH[] = "some_file.txt";
137 const static uint64_t ino = 42;
138 const static uint64_t fh = 100;
140 /* Fork a child to open the file with different credentials */
141 fork(true, &status, [&] {
143 expect_lookup(RELPATH, ino, S_IFREG | 0600, 0, 2);
144 EXPECT_CALL(*m_mock, process(
145 ResultOf([=](auto in) {
146 return (in->header.opcode == FUSE_OPEN &&
147 in->header.pid == (uint32_t)getpid() &&
148 in->header.uid == (uint32_t)geteuid() &&
149 in->header.nodeid == ino);
153 ReturnImmediate([](auto in __unused, auto out) {
154 out->body.open.fh = fh;
155 out->header.len = sizeof(out->header);
156 SET_OUT_HEADER_LEN(out, open);
159 EXPECT_CALL(*m_mock, process(
160 ResultOf([=](auto in) {
161 return (in->header.opcode == FUSE_OPEN &&
162 in->header.pid != (uint32_t)getpid() &&
163 in->header.uid != (uint32_t)geteuid() &&
164 in->header.nodeid == ino);
168 .WillRepeatedly(Invoke(ReturnErrno(EPERM)));
170 fd1 = open(FULLPATH, O_RDONLY);
171 EXPECT_LE(0, fd1) << strerror(errno);
175 fd0 = open(FULLPATH, O_RDONLY);
177 fprintf(stderr, "Privilege escalation!\n");
180 if (errno != EPERM) {
181 fprintf(stderr, "Unexpected error %s\n",
188 ASSERT_EQ(0, WEXITSTATUS(status));
189 /* Deliberately leak fd1. close(2) will be tested in release.cc */
192 TEST_F(NoAllowOther, disallowed)
196 fork(true, &status, [] {
200 fd = open(FULLPATH, O_RDONLY);
202 fprintf(stderr, "open should've failed\n");
204 } else if (errno != EPERM) {
205 fprintf(stderr, "Unexpected error: %s\n",
212 ASSERT_EQ(0, WEXITSTATUS(status));
216 * When -o allow_other is not used, users other than the owner aren't allowed
217 * to open anything inside of the mount point, not just the mountpoint itself
218 * This is a regression test for bug 237052
220 TEST_F(NoAllowOther, disallowed_beneath_root)
222 const static char FULLPATH[] = "mountpoint/some_dir";
223 const static char RELPATH[] = "some_dir";
224 const static char RELPATH2[] = "other_dir";
225 const static uint64_t ino = 42;
226 const static uint64_t ino2 = 43;
229 expect_lookup(RELPATH, ino, S_IFDIR | 0755, 0, 1);
230 EXPECT_LOOKUP(ino, RELPATH2)
231 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
232 SET_OUT_HEADER_LEN(out, entry);
233 out->body.entry.attr.mode = S_IFREG | 0644;
234 out->body.entry.nodeid = ino2;
235 out->body.entry.attr.nlink = 1;
236 out->body.entry.attr_valid = UINT64_MAX;
239 dfd = open(FULLPATH, O_DIRECTORY);
240 ASSERT_LE(0, dfd) << strerror(errno);
242 fork(true, &status, [] {
246 fd = openat(dfd, RELPATH2, O_RDONLY);
248 fprintf(stderr, "openat should've failed\n");
250 } else if (errno != EPERM) {
251 fprintf(stderr, "Unexpected error: %s\n",
258 ASSERT_EQ(0, WEXITSTATUS(status));
262 * Provide coverage for the extattr methods, which have a slightly different
265 TEST_F(NoAllowOther, setextattr)
267 int ino = 42, status;
269 fork(true, &status, [&] {
270 EXPECT_LOOKUP(1, RELPATH)
272 ReturnImmediate([=](auto in __unused, auto out) {
273 SET_OUT_HEADER_LEN(out, entry);
274 out->body.entry.attr_valid = UINT64_MAX;
275 out->body.entry.entry_valid = UINT64_MAX;
276 out->body.entry.attr.mode = S_IFREG | 0644;
277 out->body.entry.nodeid = ino;
281 * lookup the file to get it into the cache.
282 * Otherwise, the unprivileged lookup will fail with
285 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
287 const char value[] = "whatever";
288 ssize_t value_len = strlen(value) + 1;
289 int ns = EXTATTR_NAMESPACE_USER;
292 r = extattr_set_file(FULLPATH, ns, "foo",
293 (void*)value, value_len);
295 fprintf(stderr, "should've failed\n");
297 } else if (errno != EPERM) {
298 fprintf(stderr, "Unexpected error: %s\n",
305 ASSERT_EQ(0, WEXITSTATUS(status));