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_release(ino, FH);
82 expect_getattr(ino, 0);
86 fd = open(FULLPATH, O_RDONLY);
97 * A variation of the Open.multiple_creds test showing how the bug can lead to a
98 * privilege elevation. The first process is privileged and opens a file only
99 * visible to root. The second process is unprivileged and shouldn't be able
100 * to open the file, but does thanks to the bug
102 TEST_F(AllowOther, privilege_escalation)
104 const static char FULLPATH[] = "mountpoint/some_file.txt";
105 const static char RELPATH[] = "some_file.txt";
107 const static uint64_t ino = 42;
108 const static uint64_t fh = 100;
110 /* Fork a child to open the file with different credentials */
113 expect_lookup(RELPATH, ino, S_IFREG | 0600, 0, 2);
114 EXPECT_CALL(*m_mock, process(
115 ResultOf([=](auto in) {
116 return (in->header.opcode == FUSE_OPEN &&
117 in->header.pid == (uint32_t)getpid() &&
118 in->header.uid == (uint32_t)geteuid() &&
119 in->header.nodeid == ino);
123 ReturnImmediate([](auto in __unused, auto out) {
124 out->body.open.fh = fh;
125 out->header.len = sizeof(out->header);
126 SET_OUT_HEADER_LEN(out, open);
129 EXPECT_CALL(*m_mock, process(
130 ResultOf([=](auto in) {
131 return (in->header.opcode == FUSE_OPEN &&
132 in->header.pid != (uint32_t)getpid() &&
133 in->header.uid != (uint32_t)geteuid() &&
134 in->header.nodeid == ino);
138 .WillRepeatedly(Invoke(ReturnErrno(EPERM)));
139 expect_getattr(ino, 0);
141 fd1 = open(FULLPATH, O_RDONLY);
142 EXPECT_LE(0, fd1) << strerror(errno);
146 fd0 = open(FULLPATH, O_RDONLY);
148 fprintf(stderr, "Privilege escalation!\n");
151 if (errno != EPERM) {
152 fprintf(stderr, "Unexpected error %s\n",
159 /* Deliberately leak fd1. close(2) will be tested in release.cc */
162 TEST_F(NoAllowOther, disallowed)
168 fd = open(FULLPATH, O_RDONLY);
170 fprintf(stderr, "open should've failed\n");
172 } else if (errno != EPERM) {
173 fprintf(stderr, "Unexpected error: %s\n",