19821f1d3SAlan Somers /*- 29821f1d3SAlan Somers * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 39821f1d3SAlan Somers * 49821f1d3SAlan Somers * Copyright (c) 2019 The FreeBSD Foundation 59821f1d3SAlan Somers * 69821f1d3SAlan Somers * This software was developed by BFF Storage Systems, LLC under sponsorship 79821f1d3SAlan Somers * from the FreeBSD Foundation. 89821f1d3SAlan Somers * 99821f1d3SAlan Somers * Redistribution and use in source and binary forms, with or without 109821f1d3SAlan Somers * modification, are permitted provided that the following conditions 119821f1d3SAlan Somers * are met: 129821f1d3SAlan Somers * 1. Redistributions of source code must retain the above copyright 139821f1d3SAlan Somers * notice, this list of conditions and the following disclaimer. 149821f1d3SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright 159821f1d3SAlan Somers * notice, this list of conditions and the following disclaimer in the 169821f1d3SAlan Somers * documentation and/or other materials provided with the distribution. 179821f1d3SAlan Somers * 189821f1d3SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 199821f1d3SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 209821f1d3SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 219821f1d3SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 229821f1d3SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 239821f1d3SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 249821f1d3SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 259821f1d3SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 269821f1d3SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 279821f1d3SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 289821f1d3SAlan Somers * SUCH DAMAGE. 299821f1d3SAlan Somers */ 309821f1d3SAlan Somers 319821f1d3SAlan Somers /* 329821f1d3SAlan Somers * Tests for the "default_permissions" mount option. They must be in their own 339821f1d3SAlan Somers * file so they can be run as an unprivileged user 349821f1d3SAlan Somers */ 359821f1d3SAlan Somers 369821f1d3SAlan Somers extern "C" { 379821f1d3SAlan Somers #include <fcntl.h> 389821f1d3SAlan Somers #include <unistd.h> 399821f1d3SAlan Somers } 409821f1d3SAlan Somers 419821f1d3SAlan Somers #include "mockfs.hh" 429821f1d3SAlan Somers #include "utils.hh" 439821f1d3SAlan Somers 449821f1d3SAlan Somers using namespace testing; 459821f1d3SAlan Somers 469821f1d3SAlan Somers class DefaultPermissions: public FuseTest { 479821f1d3SAlan Somers 489821f1d3SAlan Somers virtual void SetUp() { 499821f1d3SAlan Somers FuseTest::SetUp(); 509821f1d3SAlan Somers 519821f1d3SAlan Somers if (geteuid() == 0) { 529821f1d3SAlan Somers GTEST_SKIP() << "This test requires an unprivileged user"; 539821f1d3SAlan Somers } 549821f1d3SAlan Somers m_default_permissions = true; 559821f1d3SAlan Somers } 569821f1d3SAlan Somers 579821f1d3SAlan Somers public: 589821f1d3SAlan Somers void expect_lookup(const char *relpath, uint64_t ino, mode_t mode) 599821f1d3SAlan Somers { 609821f1d3SAlan Somers FuseTest::expect_lookup(relpath, ino, S_IFREG | mode, 0, 1); 619821f1d3SAlan Somers } 629821f1d3SAlan Somers 639821f1d3SAlan Somers }; 649821f1d3SAlan Somers 659821f1d3SAlan Somers class Access: public DefaultPermissions {}; 669821f1d3SAlan Somers class Open: public DefaultPermissions {}; 679821f1d3SAlan Somers 689821f1d3SAlan Somers /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=216391 */ 699821f1d3SAlan Somers TEST_F(Access, DISABLED_eaccess) 709821f1d3SAlan Somers { 719821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt"; 729821f1d3SAlan Somers const char RELPATH[] = "some_file.txt"; 739821f1d3SAlan Somers uint64_t ino = 42; 749821f1d3SAlan Somers mode_t access_mode = X_OK; 759821f1d3SAlan Somers 769821f1d3SAlan Somers expect_lookup(RELPATH, ino, S_IFREG | 0644); 779821f1d3SAlan Somers /* 789821f1d3SAlan Somers * Once default_permissions is properly implemented, there might be 799821f1d3SAlan Somers * another FUSE_GETATTR or something in here. But there should not be 809821f1d3SAlan Somers * a FUSE_ACCESS 819821f1d3SAlan Somers */ 829821f1d3SAlan Somers 839821f1d3SAlan Somers ASSERT_NE(0, access(FULLPATH, access_mode)); 849821f1d3SAlan Somers ASSERT_EQ(EACCES, errno); 859821f1d3SAlan Somers } 869821f1d3SAlan Somers 87*91ff3a0dSAlan Somers /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236291 */ 88*91ff3a0dSAlan Somers TEST_F(Access, DISABLED_ok) 899821f1d3SAlan Somers { 909821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt"; 919821f1d3SAlan Somers const char RELPATH[] = "some_file.txt"; 929821f1d3SAlan Somers uint64_t ino = 42; 939821f1d3SAlan Somers mode_t access_mode = R_OK; 949821f1d3SAlan Somers 959821f1d3SAlan Somers expect_lookup(RELPATH, ino, S_IFREG | 0644); 96*91ff3a0dSAlan Somers expect_access(ino, access_mode, 0); 979821f1d3SAlan Somers /* 989821f1d3SAlan Somers * Once default_permissions is properly implemented, there might be 99*91ff3a0dSAlan Somers * another FUSE_GETATTR or something in here. 1009821f1d3SAlan Somers */ 1019821f1d3SAlan Somers 1029821f1d3SAlan Somers ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno); 1039821f1d3SAlan Somers } 1049821f1d3SAlan Somers 1059821f1d3SAlan Somers TEST_F(Open, ok) 1069821f1d3SAlan Somers { 1079821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt"; 1089821f1d3SAlan Somers const char RELPATH[] = "some_file.txt"; 1099821f1d3SAlan Somers uint64_t ino = 42; 1109821f1d3SAlan Somers int fd; 1119821f1d3SAlan Somers 1129821f1d3SAlan Somers expect_lookup(RELPATH, ino, S_IFREG | 0644); 1139821f1d3SAlan Somers expect_open(ino, 0, 1); 1149821f1d3SAlan Somers /* Until the attr cache is working, we may send an additional GETATTR */ 1159821f1d3SAlan Somers expect_getattr(ino, 0); 1169821f1d3SAlan Somers 1179821f1d3SAlan Somers fd = open(FULLPATH, O_RDONLY); 1189821f1d3SAlan Somers EXPECT_LE(0, fd) << strerror(errno); 1199821f1d3SAlan Somers /* Deliberately leak fd. close(2) will be tested in release.cc */ 1209821f1d3SAlan Somers } 1219821f1d3SAlan Somers 1229821f1d3SAlan Somers /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=216391 */ 1239821f1d3SAlan Somers TEST_F(Open, DISABLED_eperm) 1249821f1d3SAlan Somers { 1259821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt"; 1269821f1d3SAlan Somers const char RELPATH[] = "some_file.txt"; 1279821f1d3SAlan Somers uint64_t ino = 42; 1289821f1d3SAlan Somers 1299821f1d3SAlan Somers expect_lookup(RELPATH, ino, S_IFREG | 0644); 1309821f1d3SAlan Somers /* 1319821f1d3SAlan Somers * Once default_permissions is properly implemented, there might be 1329821f1d3SAlan Somers * another FUSE_GETATTR or something in here. But there should not be 1339821f1d3SAlan Somers * a FUSE_ACCESS 1349821f1d3SAlan Somers */ 1359821f1d3SAlan Somers 1369821f1d3SAlan Somers EXPECT_NE(0, open(FULLPATH, O_RDWR)); 1379821f1d3SAlan Somers EXPECT_EQ(EPERM, errno); 1389821f1d3SAlan Somers } 139