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>
45 using namespace testing;
47 const static char FULLPATH[] = "mountpoint/some_file.txt";
48 const static char RELPATH[] = "some_file.txt";
50 class NoAllowOther: public FuseTest {
53 /* Unprivileged user id */
56 virtual void SetUp() {
58 GTEST_SKIP() << "This test must be run as root";
65 class AllowOther: public NoAllowOther {
68 virtual void SetUp() {
70 NoAllowOther::SetUp();
74 TEST_F(AllowOther, allowed)
79 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
80 expect_open(ino, 0, 1);
81 expect_flush(ino, 1, ReturnErrno(0));
82 expect_release(ino, FH);
83 expect_getattr(ino, 0);
87 fd = open(FULLPATH, O_RDONLY);
98 * A variation of the Open.multiple_creds test showing how the bug can lead to a
99 * privilege elevation. The first process is privileged and opens a file only
100 * visible to root. The second process is unprivileged and shouldn't be able
101 * to open the file, but does thanks to the bug
103 TEST_F(AllowOther, privilege_escalation)
105 const static char FULLPATH[] = "mountpoint/some_file.txt";
106 const static char RELPATH[] = "some_file.txt";
108 const static uint64_t ino = 42;
109 const static uint64_t fh = 100;
111 /* Fork a child to open the file with different credentials */
114 expect_lookup(RELPATH, ino, S_IFREG | 0600, 0, 2);
115 EXPECT_CALL(*m_mock, process(
116 ResultOf([=](auto in) {
117 return (in->header.opcode == FUSE_OPEN &&
118 in->header.pid == (uint32_t)getpid() &&
119 in->header.uid == (uint32_t)geteuid() &&
120 in->header.nodeid == ino);
124 ReturnImmediate([](auto in __unused, auto out) {
125 out->body.open.fh = fh;
126 out->header.len = sizeof(out->header);
127 SET_OUT_HEADER_LEN(out, open);
130 EXPECT_CALL(*m_mock, process(
131 ResultOf([=](auto in) {
132 return (in->header.opcode == FUSE_OPEN &&
133 in->header.pid != (uint32_t)getpid() &&
134 in->header.uid != (uint32_t)geteuid() &&
135 in->header.nodeid == ino);
139 .WillRepeatedly(Invoke(ReturnErrno(EPERM)));
140 expect_getattr(ino, 0);
142 fd1 = open(FULLPATH, O_RDONLY);
143 EXPECT_LE(0, fd1) << strerror(errno);
147 fd0 = open(FULLPATH, O_RDONLY);
149 fprintf(stderr, "Privilege escalation!\n");
152 if (errno != EPERM) {
153 fprintf(stderr, "Unexpected error %s\n",
160 /* Deliberately leak fd1. close(2) will be tested in release.cc */
163 TEST_F(NoAllowOther, disallowed)
169 fd = open(FULLPATH, O_RDONLY);
171 fprintf(stderr, "open should've failed\n");
173 } else if (errno != EPERM) {
174 fprintf(stderr, "Unexpected error: %s\n",