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>
47 using namespace testing;
49 void sighandler(int __unused sig) {}
52 get_unprivileged_uid(int *uid)
57 * First try "tests", Kyua's default unprivileged user. XXX after
58 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
60 pw = getpwnam("tests");
62 /* Fall back to "nobody" */
63 pw = getpwnam("nobody");
66 GTEST_SKIP() << "Test requires an unprivileged user";
70 class NoAllowOther: public FuseTest {
73 /* Unprivileged user id */
76 virtual void SetUp() {
78 GTEST_SKIP() << "This test must be run as root";
80 get_unprivileged_uid(&m_uid);
88 class AllowOther: public NoAllowOther {
91 virtual void SetUp() {
93 NoAllowOther::SetUp();
97 TEST_F(AllowOther, allowed)
99 const char FULLPATH[] = "mountpoint/some_file.txt";
100 const char RELPATH[] = "some_file.txt";
105 signal(SIGUSR2, sighandler);
107 if ((child = fork()) == 0) {
111 /* Drop privileges before accessing */
112 if (0 != setreuid(-1, m_uid)) {
116 fd = open(FULLPATH, O_RDONLY);
123 /* Deliberately leak fd */
124 } else if (child > 0) {
126 * In parent. Cleanup must happen here, because it's still
129 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
130 expect_open(ino, 0, 1);
131 expect_release(ino, 1, 0, 0);
132 /* Until the attr cache is working, we may send an additional
134 expect_getattr(ino, 0);
135 m_mock->m_child_pid = child;
136 /* Signal the child process to go */
137 kill(child, SIGUSR2);
141 ASSERT_EQ(0, WEXITSTATUS(child_status));
143 FAIL() << strerror(errno);
147 TEST_F(NoAllowOther, disallowed)
149 const char FULLPATH[] = "mountpoint/some_file.txt";
153 signal(SIGUSR2, sighandler);
155 if ((child = fork()) == 0) {
159 /* Drop privileges before accessing */
160 if (0 != setreuid(-1, m_uid)) {
164 fd = open(FULLPATH, O_RDONLY);
166 fprintf(stderr, "open should've failed\n");
168 } else if (errno != EPERM) {
170 "Unexpected error: %s\n", strerror(errno));
175 /* Deliberately leak fd */
176 } else if (child > 0) {
178 * In parent. Cleanup must happen here, because it's still
181 m_mock->m_child_pid = child;
182 /* Signal the child process to go */
183 kill(child, SIGUSR2);
187 ASSERT_EQ(0, WEXITSTATUS(child_status));
189 FAIL() << strerror(errno);