1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2019 The FreeBSD Foundation
5 *
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33extern "C" {
34#include <sys/param.h>
35#include <sys/mount.h>
36#include <sys/uio.h>
37
38#include "mntopts.h"	// for build_iovec
39}
40
41#include "mockfs.hh"
42#include "utils.hh"
43
44using namespace testing;
45
46class UpdateOk: public FuseTest, public WithParamInterface<const char*> {};
47class UpdateErr: public FuseTest, public WithParamInterface<const char*> {};
48
49int mntflag_from_string(const char *s)
50{
51	if (0 == strcmp("MNT_RDONLY", s))
52		return MNT_RDONLY;
53	else if (0 == strcmp("MNT_NOEXEC", s))
54		return MNT_NOEXEC;
55	else if (0 == strcmp("MNT_NOSUID", s))
56		return MNT_NOSUID;
57	else if (0 == strcmp("MNT_NOATIME", s))
58		return MNT_NOATIME;
59	else if (0 == strcmp("MNT_SUIDDIR", s))
60		return MNT_SUIDDIR;
61	else if (0 == strcmp("MNT_USER", s))
62		return MNT_USER;
63	else
64		return 0;
65}
66
67/* Some mount options can be changed by mount -u */
68TEST_P(UpdateOk, update)
69{
70	struct statfs statbuf;
71	struct iovec *iov = NULL;
72	int iovlen = 0;
73	int flag;
74	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
75
76	flag = mntflag_from_string(GetParam());
77	if (flag == MNT_NOSUID && 0 != geteuid())
78		GTEST_SKIP() << "Only root may clear MNT_NOSUID";
79	if (flag == MNT_SUIDDIR && 0 != geteuid())
80		GTEST_SKIP() << "Only root may set MNT_SUIDDIR";
81
82	EXPECT_CALL(*m_mock, process(
83		ResultOf([](auto in) {
84			return (in.header.opcode == FUSE_STATFS);
85		}, Eq(true)),
86		_)
87	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
88		/*
89		 * All of the fields except f_flags are don't care, and f_flags is set by
90		 * the VFS
91		 */
92		SET_OUT_HEADER_LEN(out, statfs);
93	})));
94
95	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
96	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
97
98	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
99	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
100	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
101	ASSERT_EQ(0, nmount(iov, iovlen, newflags)) << strerror(errno);
102
103	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
104	EXPECT_FALSE((newflags ^ statbuf.f_flags) & flag);
105}
106
107/* Some mount options cannnot be changed by mount -u */
108TEST_P(UpdateErr, update)
109{
110	struct statfs statbuf;
111	struct iovec *iov = NULL;
112	int iovlen = 0;
113	int flag;
114	int newflags = MNT_UPDATE | MNT_SYNCHRONOUS;
115
116	flag = mntflag_from_string(GetParam());
117	EXPECT_CALL(*m_mock, process(
118		ResultOf([](auto in) {
119			return (in.header.opcode == FUSE_STATFS);
120		}, Eq(true)),
121		_)
122	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
123		/*
124		 * All of the fields except f_flags are don't care, and f_flags is set by
125		 * the VFS
126		 */
127		SET_OUT_HEADER_LEN(out, statfs);
128	})));
129
130	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
131	newflags = (statbuf.f_flags | MNT_UPDATE) ^ flag;
132
133	build_iovec(&iov, &iovlen, "fstype", (void*)statbuf.f_fstypename, -1);
134	build_iovec(&iov, &iovlen, "fspath", (void*)statbuf.f_mntonname, -1);
135	build_iovec(&iov, &iovlen, "from", __DECONST(void *, "/dev/fuse"), -1);
136	/*
137	 * Don't check nmount's return value, because vfs_domount may "fix" the
138	 * options for us.  The important thing is to check the final value of
139	 * statbuf.f_flags below.
140	 */
141	(void)nmount(iov, iovlen, newflags);
142
143	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
144	EXPECT_TRUE((newflags ^ statbuf.f_flags) & flag);
145}
146
147INSTANTIATE_TEST_CASE_P(Mount, UpdateOk,
148		::testing::Values("MNT_RDONLY", "MNT_NOEXEC", "MNT_NOSUID", "MNT_NOATIME",
149		"MNT_SUIDDIR")
150);
151
152INSTANTIATE_TEST_CASE_P(Mount, UpdateErr,
153		::testing::Values( "MNT_USER");
154);
155