xref: /freebsd/tests/sys/fs/fusefs/default_permissions.cc (revision 9821f1d3231d4dea3e1319358f416425be2266a6)
1*9821f1d3SAlan Somers /*-
2*9821f1d3SAlan Somers  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*9821f1d3SAlan Somers  *
4*9821f1d3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
5*9821f1d3SAlan Somers  *
6*9821f1d3SAlan Somers  * This software was developed by BFF Storage Systems, LLC under sponsorship
7*9821f1d3SAlan Somers  * from the FreeBSD Foundation.
8*9821f1d3SAlan Somers  *
9*9821f1d3SAlan Somers  * Redistribution and use in source and binary forms, with or without
10*9821f1d3SAlan Somers  * modification, are permitted provided that the following conditions
11*9821f1d3SAlan Somers  * are met:
12*9821f1d3SAlan Somers  * 1. Redistributions of source code must retain the above copyright
13*9821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer.
14*9821f1d3SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
15*9821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
16*9821f1d3SAlan Somers  *    documentation and/or other materials provided with the distribution.
17*9821f1d3SAlan Somers  *
18*9821f1d3SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*9821f1d3SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*9821f1d3SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*9821f1d3SAlan Somers  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*9821f1d3SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*9821f1d3SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*9821f1d3SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*9821f1d3SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*9821f1d3SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*9821f1d3SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*9821f1d3SAlan Somers  * SUCH DAMAGE.
29*9821f1d3SAlan Somers  */
30*9821f1d3SAlan Somers 
31*9821f1d3SAlan Somers /*
32*9821f1d3SAlan Somers  * Tests for the "default_permissions" mount option.  They must be in their own
33*9821f1d3SAlan Somers  * file so they can be run as an unprivileged user
34*9821f1d3SAlan Somers  */
35*9821f1d3SAlan Somers 
36*9821f1d3SAlan Somers extern "C" {
37*9821f1d3SAlan Somers #include <fcntl.h>
38*9821f1d3SAlan Somers #include <unistd.h>
39*9821f1d3SAlan Somers }
40*9821f1d3SAlan Somers 
41*9821f1d3SAlan Somers #include "mockfs.hh"
42*9821f1d3SAlan Somers #include "utils.hh"
43*9821f1d3SAlan Somers 
44*9821f1d3SAlan Somers using namespace testing;
45*9821f1d3SAlan Somers 
46*9821f1d3SAlan Somers class DefaultPermissions: public FuseTest {
47*9821f1d3SAlan Somers 
48*9821f1d3SAlan Somers virtual void SetUp() {
49*9821f1d3SAlan Somers 	FuseTest::SetUp();
50*9821f1d3SAlan Somers 
51*9821f1d3SAlan Somers 	if (geteuid() == 0) {
52*9821f1d3SAlan Somers 		GTEST_SKIP() << "This test requires an unprivileged user";
53*9821f1d3SAlan Somers 	}
54*9821f1d3SAlan Somers 	m_default_permissions = true;
55*9821f1d3SAlan Somers }
56*9821f1d3SAlan Somers 
57*9821f1d3SAlan Somers public:
58*9821f1d3SAlan Somers void expect_lookup(const char *relpath, uint64_t ino, mode_t mode)
59*9821f1d3SAlan Somers {
60*9821f1d3SAlan Somers 	FuseTest::expect_lookup(relpath, ino, S_IFREG | mode, 0, 1);
61*9821f1d3SAlan Somers }
62*9821f1d3SAlan Somers 
63*9821f1d3SAlan Somers };
64*9821f1d3SAlan Somers 
65*9821f1d3SAlan Somers class Access: public DefaultPermissions {};
66*9821f1d3SAlan Somers class Open: public DefaultPermissions {};
67*9821f1d3SAlan Somers 
68*9821f1d3SAlan Somers /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=216391 */
69*9821f1d3SAlan Somers TEST_F(Access, DISABLED_eaccess)
70*9821f1d3SAlan Somers {
71*9821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
72*9821f1d3SAlan Somers 	const char RELPATH[] = "some_file.txt";
73*9821f1d3SAlan Somers 	uint64_t ino = 42;
74*9821f1d3SAlan Somers 	mode_t	access_mode = X_OK;
75*9821f1d3SAlan Somers 
76*9821f1d3SAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644);
77*9821f1d3SAlan Somers 	/*
78*9821f1d3SAlan Somers 	 * Once default_permissions is properly implemented, there might be
79*9821f1d3SAlan Somers 	 * another FUSE_GETATTR or something in here.  But there should not be
80*9821f1d3SAlan Somers 	 * a FUSE_ACCESS
81*9821f1d3SAlan Somers 	 */
82*9821f1d3SAlan Somers 
83*9821f1d3SAlan Somers 	ASSERT_NE(0, access(FULLPATH, access_mode));
84*9821f1d3SAlan Somers 	ASSERT_EQ(EACCES, errno);
85*9821f1d3SAlan Somers }
86*9821f1d3SAlan Somers 
87*9821f1d3SAlan Somers TEST_F(Access, ok)
88*9821f1d3SAlan Somers {
89*9821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
90*9821f1d3SAlan Somers 	const char RELPATH[] = "some_file.txt";
91*9821f1d3SAlan Somers 	uint64_t ino = 42;
92*9821f1d3SAlan Somers 	mode_t	access_mode = R_OK;
93*9821f1d3SAlan Somers 
94*9821f1d3SAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644);
95*9821f1d3SAlan Somers 	/*
96*9821f1d3SAlan Somers 	 * Once default_permissions is properly implemented, there might be
97*9821f1d3SAlan Somers 	 * another FUSE_GETATTR or something in here.  But there should not be
98*9821f1d3SAlan Somers 	 * a FUSE_ACCESS
99*9821f1d3SAlan Somers 	 */
100*9821f1d3SAlan Somers 
101*9821f1d3SAlan Somers 	ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
102*9821f1d3SAlan Somers }
103*9821f1d3SAlan Somers 
104*9821f1d3SAlan Somers TEST_F(Open, ok)
105*9821f1d3SAlan Somers {
106*9821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
107*9821f1d3SAlan Somers 	const char RELPATH[] = "some_file.txt";
108*9821f1d3SAlan Somers 	uint64_t ino = 42;
109*9821f1d3SAlan Somers 	int fd;
110*9821f1d3SAlan Somers 
111*9821f1d3SAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644);
112*9821f1d3SAlan Somers 	expect_open(ino, 0, 1);
113*9821f1d3SAlan Somers 	/* Until the attr cache is working, we may send an additional GETATTR */
114*9821f1d3SAlan Somers 	expect_getattr(ino, 0);
115*9821f1d3SAlan Somers 
116*9821f1d3SAlan Somers 	fd = open(FULLPATH, O_RDONLY);
117*9821f1d3SAlan Somers 	EXPECT_LE(0, fd) << strerror(errno);
118*9821f1d3SAlan Somers 	/* Deliberately leak fd.  close(2) will be tested in release.cc */
119*9821f1d3SAlan Somers }
120*9821f1d3SAlan Somers 
121*9821f1d3SAlan Somers /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=216391 */
122*9821f1d3SAlan Somers TEST_F(Open, DISABLED_eperm)
123*9821f1d3SAlan Somers {
124*9821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
125*9821f1d3SAlan Somers 	const char RELPATH[] = "some_file.txt";
126*9821f1d3SAlan Somers 	uint64_t ino = 42;
127*9821f1d3SAlan Somers 
128*9821f1d3SAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644);
129*9821f1d3SAlan Somers 	/*
130*9821f1d3SAlan Somers 	 * Once default_permissions is properly implemented, there might be
131*9821f1d3SAlan Somers 	 * another FUSE_GETATTR or something in here.  But there should not be
132*9821f1d3SAlan Somers 	 * a FUSE_ACCESS
133*9821f1d3SAlan Somers 	 */
134*9821f1d3SAlan Somers 
135*9821f1d3SAlan Somers 	EXPECT_NE(0, open(FULLPATH, O_RDWR));
136*9821f1d3SAlan Somers 	EXPECT_EQ(EPERM, errno);
137*9821f1d3SAlan Somers }
138