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>
42 #include <semaphore.h>
48 using namespace testing;
50 void sighandler(int __unused sig) {}
53 get_unprivileged_uid(int *uid)
58 * First try "tests", Kyua's default unprivileged user. XXX after
59 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
61 pw = getpwnam("tests");
63 /* Fall back to "nobody" */
64 pw = getpwnam("nobody");
67 GTEST_SKIP() << "Test requires an unprivileged user";
71 class NoAllowOther: public FuseTest {
74 /* Unprivileged user id */
77 virtual void SetUp() {
79 GTEST_SKIP() << "This test must be run as root";
81 get_unprivileged_uid(&m_uid);
89 class AllowOther: public NoAllowOther {
92 virtual void SetUp() {
94 NoAllowOther::SetUp();
98 TEST_F(AllowOther, allowed)
100 const char FULLPATH[] = "mountpoint/some_file.txt";
101 const char RELPATH[] = "some_file.txt";
106 int mprot = PROT_READ | PROT_WRITE;
107 int mflags = MAP_ANON | MAP_SHARED;
109 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
110 ASSERT_NE(NULL, sem) << strerror(errno);
111 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
113 if ((child = fork()) == 0) {
115 ASSERT_EQ(0, sem_wait(sem)) << strerror(errno);
117 /* Drop privileges before accessing */
118 if (0 != setreuid(-1, m_uid)) {
122 fd = open(FULLPATH, O_RDONLY);
129 /* Deliberately leak fd */
130 } else if (child > 0) {
134 * In parent. Cleanup must happen here, because it's still
137 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
138 expect_open(ino, 0, 1);
140 /* Until the attr cache is working, we may send an additional
142 expect_getattr(ino, 0);
143 m_mock->m_child_pid = child;
144 /* Signal the child process to go */
145 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
148 ASSERT_EQ(0, WEXITSTATUS(child_status));
150 FAIL() << strerror(errno);
154 TEST_F(NoAllowOther, disallowed)
156 const char FULLPATH[] = "mountpoint/some_file.txt";
160 int mprot = PROT_READ | PROT_WRITE;
161 int mflags = MAP_ANON | MAP_SHARED;
163 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
164 ASSERT_NE(NULL, sem) << strerror(errno);
165 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
167 if ((child = fork()) == 0) {
169 ASSERT_EQ(0, sem_wait(sem)) << strerror(errno);
171 /* Drop privileges before accessing */
172 if (0 != setreuid(-1, m_uid)) {
176 fd = open(FULLPATH, O_RDONLY);
178 fprintf(stderr, "open should've failed\n");
180 } else if (errno != EPERM) {
182 "Unexpected error: %s\n", strerror(errno));
187 /* Deliberately leak fd */
188 } else if (child > 0) {
190 * In parent. Cleanup must happen here, because it's still
193 m_mock->m_child_pid = child;
194 /* Signal the child process to go */
195 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
199 ASSERT_EQ(0, WEXITSTATUS(child_status));
201 FAIL() << strerror(errno);