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 33 extern "C" { 34 #include <sys/param.h> 35 #include <sys/mman.h> 36 #include <sys/module.h> 37 #include <sys/sysctl.h> 38 #include <sys/wait.h> 39 40 #include <dirent.h> 41 #include <fcntl.h> 42 #include <grp.h> 43 #include <pwd.h> 44 #include <semaphore.h> 45 #include <unistd.h> 46 } 47 48 #include <gtest/gtest.h> 49 50 #include "mockfs.hh" 51 #include "utils.hh" 52 53 using namespace testing; 54 55 /* 56 * The default max_write is set to this formula in libfuse, though 57 * individual filesystems can lower it. The "- 4096" was added in 58 * commit 154ffe2, with the commit message "fix". 59 */ 60 const uint32_t libfuse_max_write = 32 * getpagesize() + 0x1000 - 4096; 61 62 /* 63 * Set the default max_write to a distinct value from MAXPHYS to catch bugs 64 * that confuse the two. 65 */ 66 const uint32_t default_max_write = MIN(libfuse_max_write, MAXPHYS / 2); 67 68 69 /* Check that fusefs(4) is accessible and the current user can mount(2) */ 70 void check_environment() 71 { 72 const char *devnode = "/dev/fuse"; 73 const char *bsdextended_node = "security.mac.bsdextended.enabled"; 74 int bsdextended_val = 0; 75 size_t bsdextended_size = sizeof(bsdextended_val); 76 int bsdextended_found; 77 const char *usermount_node = "vfs.usermount"; 78 int usermount_val = 0; 79 size_t usermount_size = sizeof(usermount_val); 80 if (eaccess(devnode, R_OK | W_OK)) { 81 if (errno == ENOENT) { 82 GTEST_SKIP() << devnode << " does not exist"; 83 } else if (errno == EACCES) { 84 GTEST_SKIP() << devnode << 85 " is not accessible by the current user"; 86 } else { 87 GTEST_SKIP() << strerror(errno); 88 } 89 } 90 // mac_bsdextended(4), when enabled, generates many more GETATTR 91 // operations. The fusefs tests' expectations don't account for those, 92 // and adding extra code to handle them obfuscates the real purpose of 93 // the tests. Better just to skip the fusefs tests if mac_bsdextended 94 // is enabled. 95 bsdextended_found = sysctlbyname(bsdextended_node, &bsdextended_val, 96 &bsdextended_size, NULL, 0); 97 if (bsdextended_found == 0 && bsdextended_val != 0) 98 GTEST_SKIP() << 99 "The fusefs tests are incompatible with mac_bsdextended."; 100 ASSERT_EQ(sysctlbyname(usermount_node, &usermount_val, &usermount_size, 101 NULL, 0), 102 0); 103 if (geteuid() != 0 && !usermount_val) 104 GTEST_SKIP() << "current user is not allowed to mount"; 105 } 106 107 const char *cache_mode_to_s(enum cache_mode cm) { 108 switch (cm) { 109 case Uncached: 110 return "Uncached"; 111 case Writethrough: 112 return "Writethrough"; 113 case Writeback: 114 return "Writeback"; 115 case WritebackAsync: 116 return "WritebackAsync"; 117 default: 118 return "Unknown"; 119 } 120 } 121 122 bool is_unsafe_aio_enabled(void) { 123 const char *node = "vfs.aio.enable_unsafe"; 124 int val = 0; 125 size_t size = sizeof(val); 126 127 if (sysctlbyname(node, &val, &size, NULL, 0)) { 128 perror("sysctlbyname"); 129 return (false); 130 } 131 return (val != 0); 132 } 133 134 class FuseEnv: public Environment { 135 virtual void SetUp() { 136 } 137 }; 138 139 void FuseTest::SetUp() { 140 const char *maxbcachebuf_node = "vfs.maxbcachebuf"; 141 const char *maxphys_node = "kern.maxphys"; 142 int val = 0; 143 size_t size = sizeof(val); 144 145 /* 146 * XXX check_environment should be called from FuseEnv::SetUp, but 147 * can't due to https://github.com/google/googletest/issues/2189 148 */ 149 check_environment(); 150 if (IsSkipped()) 151 return; 152 153 ASSERT_EQ(0, sysctlbyname(maxbcachebuf_node, &val, &size, NULL, 0)) 154 << strerror(errno); 155 m_maxbcachebuf = val; 156 ASSERT_EQ(0, sysctlbyname(maxphys_node, &val, &size, NULL, 0)) 157 << strerror(errno); 158 m_maxphys = val; 159 160 try { 161 m_mock = new MockFS(m_maxreadahead, m_allow_other, 162 m_default_permissions, m_push_symlinks_in, m_ro, 163 m_pm, m_init_flags, m_kernel_minor_version, 164 m_maxwrite, m_async, m_noclusterr, m_time_gran, 165 m_nointr); 166 /* 167 * FUSE_ACCESS is called almost universally. Expecting it in 168 * each test case would be super-annoying. Instead, set a 169 * default expectation for FUSE_ACCESS and return ENOSYS. 170 * 171 * Individual test cases can override this expectation since 172 * googlemock evaluates expectations in LIFO order. 173 */ 174 EXPECT_CALL(*m_mock, process( 175 ResultOf([=](auto in) { 176 return (in.header.opcode == FUSE_ACCESS); 177 }, Eq(true)), 178 _) 179 ).Times(AnyNumber()) 180 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS))); 181 /* 182 * FUSE_BMAP is called for most test cases that read data. Set 183 * a default expectation and return ENOSYS. 184 * 185 * Individual test cases can override this expectation since 186 * googlemock evaluates expectations in LIFO order. 187 */ 188 EXPECT_CALL(*m_mock, process( 189 ResultOf([=](auto in) { 190 return (in.header.opcode == FUSE_BMAP); 191 }, Eq(true)), 192 _) 193 ).Times(AnyNumber()) 194 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS))); 195 } catch (std::system_error err) { 196 FAIL() << err.what(); 197 } 198 } 199 200 void 201 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error) 202 { 203 EXPECT_CALL(*m_mock, process( 204 ResultOf([=](auto in) { 205 return (in.header.opcode == FUSE_ACCESS && 206 in.header.nodeid == ino && 207 in.body.access.mask == access_mode); 208 }, Eq(true)), 209 _) 210 ).WillOnce(Invoke(ReturnErrno(error))); 211 } 212 213 void 214 FuseTest::expect_destroy(int error) 215 { 216 EXPECT_CALL(*m_mock, process( 217 ResultOf([=](auto in) { 218 return (in.header.opcode == FUSE_DESTROY); 219 }, Eq(true)), 220 _) 221 ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) { 222 m_mock->m_quit = true; 223 out.header.len = sizeof(out.header); 224 out.header.unique = in.header.unique; 225 out.header.error = -error; 226 }))); 227 } 228 229 void 230 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r) 231 { 232 EXPECT_CALL(*m_mock, process( 233 ResultOf([=](auto in) { 234 return (in.header.opcode == FUSE_FLUSH && 235 in.header.nodeid == ino); 236 }, Eq(true)), 237 _) 238 ).Times(times) 239 .WillRepeatedly(Invoke(r)); 240 } 241 242 void 243 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem) 244 { 245 EXPECT_CALL(*m_mock, process( 246 ResultOf([=](auto in) { 247 return (in.header.opcode == FUSE_FORGET && 248 in.header.nodeid == ino && 249 in.body.forget.nlookup == nlookup); 250 }, Eq(true)), 251 _) 252 ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) { 253 if (sem != NULL) 254 sem_post(sem); 255 /* FUSE_FORGET has no response! */ 256 })); 257 } 258 259 void FuseTest::expect_getattr(uint64_t ino, uint64_t size) 260 { 261 EXPECT_CALL(*m_mock, process( 262 ResultOf([=](auto in) { 263 return (in.header.opcode == FUSE_GETATTR && 264 in.header.nodeid == ino); 265 }, Eq(true)), 266 _) 267 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) { 268 SET_OUT_HEADER_LEN(out, attr); 269 out.body.attr.attr.ino = ino; // Must match nodeid 270 out.body.attr.attr.mode = S_IFREG | 0644; 271 out.body.attr.attr.size = size; 272 out.body.attr.attr_valid = UINT64_MAX; 273 }))); 274 } 275 276 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode, 277 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid) 278 { 279 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath) 280 .Times(times) 281 .WillRepeatedly(Invoke( 282 ReturnImmediate([=](auto in __unused, auto& out) { 283 SET_OUT_HEADER_LEN(out, entry); 284 out.body.entry.attr.mode = mode; 285 out.body.entry.nodeid = ino; 286 out.body.entry.attr.nlink = 1; 287 out.body.entry.attr_valid = attr_valid; 288 out.body.entry.attr.size = size; 289 out.body.entry.attr.uid = uid; 290 out.body.entry.attr.gid = gid; 291 }))); 292 } 293 294 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode, 295 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid) 296 { 297 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath) 298 .Times(times) 299 .WillRepeatedly(Invoke( 300 ReturnImmediate([=](auto in __unused, auto& out) { 301 SET_OUT_HEADER_LEN(out, entry_7_8); 302 out.body.entry.attr.mode = mode; 303 out.body.entry.nodeid = ino; 304 out.body.entry.attr.nlink = 1; 305 out.body.entry.attr_valid = attr_valid; 306 out.body.entry.attr.size = size; 307 out.body.entry.attr.uid = uid; 308 out.body.entry.attr.gid = gid; 309 }))); 310 } 311 312 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times) 313 { 314 EXPECT_CALL(*m_mock, process( 315 ResultOf([=](auto in) { 316 return (in.header.opcode == FUSE_OPEN && 317 in.header.nodeid == ino); 318 }, Eq(true)), 319 _) 320 ).Times(times) 321 .WillRepeatedly(Invoke( 322 ReturnImmediate([=](auto in __unused, auto& out) { 323 out.header.len = sizeof(out.header); 324 SET_OUT_HEADER_LEN(out, open); 325 out.body.open.fh = FH; 326 out.body.open.open_flags = flags; 327 }))); 328 } 329 330 void FuseTest::expect_opendir(uint64_t ino) 331 { 332 /* opendir(3) calls fstatfs */ 333 EXPECT_CALL(*m_mock, process( 334 ResultOf([](auto in) { 335 return (in.header.opcode == FUSE_STATFS); 336 }, Eq(true)), 337 _) 338 ).WillRepeatedly(Invoke( 339 ReturnImmediate([=](auto i __unused, auto& out) { 340 SET_OUT_HEADER_LEN(out, statfs); 341 }))); 342 343 EXPECT_CALL(*m_mock, process( 344 ResultOf([=](auto in) { 345 return (in.header.opcode == FUSE_OPENDIR && 346 in.header.nodeid == ino); 347 }, Eq(true)), 348 _) 349 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 350 out.header.len = sizeof(out.header); 351 SET_OUT_HEADER_LEN(out, open); 352 out.body.open.fh = FH; 353 }))); 354 } 355 356 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize, 357 uint64_t osize, const void *contents, int flags) 358 { 359 EXPECT_CALL(*m_mock, process( 360 ResultOf([=](auto in) { 361 return (in.header.opcode == FUSE_READ && 362 in.header.nodeid == ino && 363 in.body.read.fh == FH && 364 in.body.read.offset == offset && 365 in.body.read.size == isize && 366 flags == -1 ? 367 (in.body.read.flags == O_RDONLY || 368 in.body.read.flags == O_RDWR) 369 : in.body.read.flags == (uint32_t)flags); 370 }, Eq(true)), 371 _) 372 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 373 out.header.len = sizeof(struct fuse_out_header) + osize; 374 memmove(out.body.bytes, contents, osize); 375 }))).RetiresOnSaturation(); 376 } 377 378 void FuseTest::expect_readdir(uint64_t ino, uint64_t off, 379 std::vector<struct dirent> &ents) 380 { 381 EXPECT_CALL(*m_mock, process( 382 ResultOf([=](auto in) { 383 return (in.header.opcode == FUSE_READDIR && 384 in.header.nodeid == ino && 385 in.body.readdir.fh == FH && 386 in.body.readdir.offset == off); 387 }, Eq(true)), 388 _) 389 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { 390 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body); 391 int i = 0; 392 393 out.header.error = 0; 394 out.header.len = 0; 395 396 for (const auto& it: ents) { 397 size_t entlen, entsize; 398 399 fde->ino = it.d_fileno; 400 fde->off = it.d_off; 401 fde->type = it.d_type; 402 fde->namelen = it.d_namlen; 403 strncpy(fde->name, it.d_name, it.d_namlen); 404 entlen = FUSE_NAME_OFFSET + fde->namelen; 405 entsize = FUSE_DIRENT_SIZE(fde); 406 /* 407 * The FUSE protocol does not require zeroing out the 408 * unused portion of the name. But it's a good 409 * practice to prevent information disclosure to the 410 * FUSE client, even though the client is usually the 411 * kernel 412 */ 413 memset(fde->name + fde->namelen, 0, entsize - entlen); 414 if (out.header.len + entsize > in.body.read.size) { 415 printf("Overflow in readdir expectation: i=%d\n" 416 , i); 417 break; 418 } 419 out.header.len += entsize; 420 fde = (struct fuse_dirent*) 421 ((intmax_t*)fde + entsize / sizeof(intmax_t)); 422 i++; 423 } 424 out.header.len += sizeof(out.header); 425 }))); 426 427 } 428 void FuseTest::expect_release(uint64_t ino, uint64_t fh) 429 { 430 EXPECT_CALL(*m_mock, process( 431 ResultOf([=](auto in) { 432 return (in.header.opcode == FUSE_RELEASE && 433 in.header.nodeid == ino && 434 in.body.release.fh == fh); 435 }, Eq(true)), 436 _) 437 ).WillOnce(Invoke(ReturnErrno(0))); 438 } 439 440 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r) 441 { 442 EXPECT_CALL(*m_mock, process( 443 ResultOf([=](auto in) { 444 return (in.header.opcode == FUSE_RELEASEDIR && 445 in.header.nodeid == ino && 446 in.body.release.fh == FH); 447 }, Eq(true)), 448 _) 449 ).WillOnce(Invoke(r)); 450 } 451 452 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error) 453 { 454 EXPECT_CALL(*m_mock, process( 455 ResultOf([=](auto in) { 456 return (in.header.opcode == FUSE_UNLINK && 457 0 == strcmp(path, in.body.unlink) && 458 in.header.nodeid == parent); 459 }, Eq(true)), 460 _) 461 ).WillOnce(Invoke(ReturnErrno(error))); 462 } 463 464 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize, 465 uint64_t osize, uint32_t flags_set, uint32_t flags_unset, 466 const void *contents) 467 { 468 EXPECT_CALL(*m_mock, process( 469 ResultOf([=](auto in) { 470 const char *buf = (const char*)in.body.bytes + 471 sizeof(struct fuse_write_in); 472 bool pid_ok; 473 uint32_t wf = in.body.write.write_flags; 474 475 if (wf & FUSE_WRITE_CACHE) 476 pid_ok = true; 477 else 478 pid_ok = (pid_t)in.header.pid == getpid(); 479 480 return (in.header.opcode == FUSE_WRITE && 481 in.header.nodeid == ino && 482 in.body.write.fh == FH && 483 in.body.write.offset == offset && 484 in.body.write.size == isize && 485 pid_ok && 486 (wf & flags_set) == flags_set && 487 (wf & flags_unset) == 0 && 488 (in.body.write.flags == O_WRONLY || 489 in.body.write.flags == O_RDWR) && 490 0 == bcmp(buf, contents, isize)); 491 }, Eq(true)), 492 _) 493 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 494 SET_OUT_HEADER_LEN(out, write); 495 out.body.write.size = osize; 496 }))); 497 } 498 499 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize, 500 uint64_t osize, const void *contents) 501 { 502 EXPECT_CALL(*m_mock, process( 503 ResultOf([=](auto in) { 504 const char *buf = (const char*)in.body.bytes + 505 FUSE_COMPAT_WRITE_IN_SIZE; 506 bool pid_ok = (pid_t)in.header.pid == getpid(); 507 return (in.header.opcode == FUSE_WRITE && 508 in.header.nodeid == ino && 509 in.body.write.fh == FH && 510 in.body.write.offset == offset && 511 in.body.write.size == isize && 512 pid_ok && 513 0 == bcmp(buf, contents, isize)); 514 }, Eq(true)), 515 _) 516 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 517 SET_OUT_HEADER_LEN(out, write); 518 out.body.write.size = osize; 519 }))); 520 } 521 522 void 523 get_unprivileged_id(uid_t *uid, gid_t *gid) 524 { 525 struct passwd *pw; 526 struct group *gr; 527 528 /* 529 * First try "tests", Kyua's default unprivileged user. XXX after 530 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API 531 */ 532 pw = getpwnam("tests"); 533 if (pw == NULL) { 534 /* Fall back to "nobody" */ 535 pw = getpwnam("nobody"); 536 } 537 if (pw == NULL) 538 GTEST_SKIP() << "Test requires an unprivileged user"; 539 /* Use group "nobody", which is Kyua's default unprivileged group */ 540 gr = getgrnam("nobody"); 541 if (gr == NULL) 542 GTEST_SKIP() << "Test requires an unprivileged group"; 543 *uid = pw->pw_uid; 544 *gid = gr->gr_gid; 545 } 546 547 void 548 FuseTest::fork(bool drop_privs, int *child_status, 549 std::function<void()> parent_func, 550 std::function<int()> child_func) 551 { 552 sem_t *sem; 553 int mprot = PROT_READ | PROT_WRITE; 554 int mflags = MAP_ANON | MAP_SHARED; 555 pid_t child; 556 uid_t uid; 557 gid_t gid; 558 559 if (drop_privs) { 560 get_unprivileged_id(&uid, &gid); 561 if (IsSkipped()) 562 return; 563 } 564 565 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0); 566 ASSERT_NE(MAP_FAILED, sem) << strerror(errno); 567 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno); 568 569 if ((child = ::fork()) == 0) { 570 /* In child */ 571 int err = 0; 572 573 if (sem_wait(sem)) { 574 perror("sem_wait"); 575 err = 1; 576 goto out; 577 } 578 579 if (drop_privs && 0 != setegid(gid)) { 580 perror("setegid"); 581 err = 1; 582 goto out; 583 } 584 if (drop_privs && 0 != setreuid(-1, uid)) { 585 perror("setreuid"); 586 err = 1; 587 goto out; 588 } 589 err = child_func(); 590 591 out: 592 sem_destroy(sem); 593 _exit(err); 594 } else if (child > 0) { 595 /* 596 * In parent. Cleanup must happen here, because it's still 597 * privileged. 598 */ 599 m_mock->m_child_pid = child; 600 ASSERT_NO_FATAL_FAILURE(parent_func()); 601 602 /* Signal the child process to go */ 603 ASSERT_EQ(0, sem_post(sem)) << strerror(errno); 604 605 ASSERT_LE(0, wait(child_status)) << strerror(errno); 606 } else { 607 FAIL() << strerror(errno); 608 } 609 munmap(sem, sizeof(*sem)); 610 return; 611 } 612 613 static void usage(char* progname) { 614 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname); 615 exit(2); 616 } 617 618 int main(int argc, char **argv) { 619 int ch; 620 FuseEnv *fuse_env = new FuseEnv; 621 622 InitGoogleTest(&argc, argv); 623 AddGlobalTestEnvironment(fuse_env); 624 625 while ((ch = getopt(argc, argv, "v")) != -1) { 626 switch (ch) { 627 case 'v': 628 verbosity++; 629 break; 630 default: 631 usage(argv[0]); 632 break; 633 } 634 } 635 636 return (RUN_ALL_TESTS()); 637 } 638