xref: /freebsd-src/tests/sys/fs/fusefs/default_permissions_privileged.cc (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1e5ff3a7eSAlan Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3e5ff3a7eSAlan Somers  *
4e5ff3a7eSAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
5e5ff3a7eSAlan Somers  *
6e5ff3a7eSAlan Somers  * This software was developed by BFF Storage Systems, LLC under sponsorship
7e5ff3a7eSAlan Somers  * from the FreeBSD Foundation.
8e5ff3a7eSAlan Somers  *
9e5ff3a7eSAlan Somers  * Redistribution and use in source and binary forms, with or without
10e5ff3a7eSAlan Somers  * modification, are permitted provided that the following conditions
11e5ff3a7eSAlan Somers  * are met:
12e5ff3a7eSAlan Somers  * 1. Redistributions of source code must retain the above copyright
13e5ff3a7eSAlan Somers  *    notice, this list of conditions and the following disclaimer.
14e5ff3a7eSAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
15e5ff3a7eSAlan Somers  *    notice, this list of conditions and the following disclaimer in the
16e5ff3a7eSAlan Somers  *    documentation and/or other materials provided with the distribution.
17e5ff3a7eSAlan Somers  *
18e5ff3a7eSAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19e5ff3a7eSAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20e5ff3a7eSAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21e5ff3a7eSAlan Somers  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22e5ff3a7eSAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23e5ff3a7eSAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24e5ff3a7eSAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25e5ff3a7eSAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26e5ff3a7eSAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27e5ff3a7eSAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28e5ff3a7eSAlan Somers  * SUCH DAMAGE.
29e5ff3a7eSAlan Somers  */
30e5ff3a7eSAlan Somers 
31e5ff3a7eSAlan Somers /*
32e5ff3a7eSAlan Somers  * Tests for the "default_permissions" mount option that require a privileged
33e5ff3a7eSAlan Somers  * user.
34e5ff3a7eSAlan Somers  */
35e5ff3a7eSAlan Somers 
36e5ff3a7eSAlan Somers extern "C" {
37e5ff3a7eSAlan Somers #include <sys/types.h>
38e5ff3a7eSAlan Somers #include <sys/extattr.h>
39e5ff3a7eSAlan Somers 
40e5ff3a7eSAlan Somers #include <fcntl.h>
41e5ff3a7eSAlan Somers #include <unistd.h>
42e5ff3a7eSAlan Somers }
43e5ff3a7eSAlan Somers 
44e5ff3a7eSAlan Somers #include "mockfs.hh"
45e5ff3a7eSAlan Somers #include "utils.hh"
46e5ff3a7eSAlan Somers 
47e5ff3a7eSAlan Somers using namespace testing;
48e5ff3a7eSAlan Somers 
49e5ff3a7eSAlan Somers class DefaultPermissionsPrivileged: public FuseTest {
SetUp()50e5ff3a7eSAlan Somers virtual void SetUp() {
51e5ff3a7eSAlan Somers 	m_default_permissions = true;
52e5ff3a7eSAlan Somers 	FuseTest::SetUp();
53e5ff3a7eSAlan Somers 	if (HasFatalFailure() || IsSkipped())
54e5ff3a7eSAlan Somers 		return;
55e5ff3a7eSAlan Somers 
56e5ff3a7eSAlan Somers 	if (geteuid() != 0) {
57e5ff3a7eSAlan Somers 		GTEST_SKIP() << "This test requires a privileged user";
58e5ff3a7eSAlan Somers 	}
59e5ff3a7eSAlan Somers 
60e5ff3a7eSAlan Somers 	/* With -o default_permissions, FUSE_ACCESS should never be called */
61e5ff3a7eSAlan Somers 	EXPECT_CALL(*m_mock, process(
62e5ff3a7eSAlan Somers 		ResultOf([=](auto in) {
6329edc611SAlan Somers 			return (in.header.opcode == FUSE_ACCESS);
64e5ff3a7eSAlan Somers 		}, Eq(true)),
65e5ff3a7eSAlan Somers 		_)
66e5ff3a7eSAlan Somers 	).Times(0);
67e5ff3a7eSAlan Somers }
68e5ff3a7eSAlan Somers 
69e5ff3a7eSAlan Somers public:
expect_getattr(uint64_t ino,mode_t mode,uint64_t attr_valid,int times,uid_t uid=0,gid_t gid=0)70e5ff3a7eSAlan Somers void expect_getattr(uint64_t ino, mode_t mode, uint64_t attr_valid, int times,
71e5ff3a7eSAlan Somers 	uid_t uid = 0, gid_t gid = 0)
72e5ff3a7eSAlan Somers {
73e5ff3a7eSAlan Somers 	EXPECT_CALL(*m_mock, process(
74e5ff3a7eSAlan Somers 		ResultOf([=](auto in) {
7529edc611SAlan Somers 			return (in.header.opcode == FUSE_GETATTR &&
7629edc611SAlan Somers 				in.header.nodeid == ino);
77e5ff3a7eSAlan Somers 		}, Eq(true)),
78e5ff3a7eSAlan Somers 		_)
79e5ff3a7eSAlan Somers 	).Times(times)
8029edc611SAlan Somers 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
81e5ff3a7eSAlan Somers 		SET_OUT_HEADER_LEN(out, attr);
8229edc611SAlan Somers 		out.body.attr.attr.ino = ino;	// Must match nodeid
8329edc611SAlan Somers 		out.body.attr.attr.mode = mode;
8429edc611SAlan Somers 		out.body.attr.attr.size = 0;
8529edc611SAlan Somers 		out.body.attr.attr.uid = uid;
86a22a7807SAlan Somers 		out.body.attr.attr.gid = gid;
8729edc611SAlan Somers 		out.body.attr.attr_valid = attr_valid;
88e5ff3a7eSAlan Somers 	})));
89e5ff3a7eSAlan Somers }
90e5ff3a7eSAlan Somers 
expect_lookup(const char * relpath,uint64_t ino,mode_t mode,uint64_t attr_valid,uid_t uid=0,gid_t gid=0)91e5ff3a7eSAlan Somers void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
92e5ff3a7eSAlan Somers 	uint64_t attr_valid, uid_t uid = 0, gid_t gid = 0)
93e5ff3a7eSAlan Somers {
94e5ff3a7eSAlan Somers 	FuseTest::expect_lookup(relpath, ino, mode, 0, 1, attr_valid, uid, gid);
95e5ff3a7eSAlan Somers }
96e5ff3a7eSAlan Somers 
97e5ff3a7eSAlan Somers };
98e5ff3a7eSAlan Somers 
99e5ff3a7eSAlan Somers class Setattr: public DefaultPermissionsPrivileged {};
100e5ff3a7eSAlan Somers 
TEST_F(Setattr,sticky_regular_file)101a90e32deSAlan Somers TEST_F(Setattr, sticky_regular_file)
102e5ff3a7eSAlan Somers {
103e5ff3a7eSAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
104e5ff3a7eSAlan Somers 	const char RELPATH[] = "some_file.txt";
105e5ff3a7eSAlan Somers 	const uint64_t ino = 42;
106e5ff3a7eSAlan Somers 	const mode_t oldmode = 0644;
107e5ff3a7eSAlan Somers 	const mode_t newmode = 01644;
108e5ff3a7eSAlan Somers 
109e5ff3a7eSAlan Somers 	expect_getattr(1, S_IFDIR | 0755, UINT64_MAX, 1);
110e5ff3a7eSAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | oldmode, UINT64_MAX, geteuid());
111e5ff3a7eSAlan Somers 	EXPECT_CALL(*m_mock, process(
112e5ff3a7eSAlan Somers 		ResultOf([](auto in) {
11329edc611SAlan Somers 			return (in.header.opcode == FUSE_SETATTR);
114e5ff3a7eSAlan Somers 		}, Eq(true)),
115e5ff3a7eSAlan Somers 		_)
11629edc611SAlan Somers 	).WillOnce(Invoke(ReturnImmediate([](auto in __unused, auto& out) {
117e5ff3a7eSAlan Somers 		SET_OUT_HEADER_LEN(out, attr);
11829edc611SAlan Somers 		out.body.attr.attr.mode = S_IFREG | newmode;
119e5ff3a7eSAlan Somers 	})));
120e5ff3a7eSAlan Somers 
121e5ff3a7eSAlan Somers 	EXPECT_EQ(0, chmod(FULLPATH, newmode)) << strerror(errno);
122e5ff3a7eSAlan Somers }
123e5ff3a7eSAlan Somers 
124e5ff3a7eSAlan Somers 
125