From e39ed91daaf94689b9aeb20df6733d8d24d2a45d Mon Sep 17 00:00:00 2001 From: Kevin Krakauer Date: Thu, 18 Jul 2024 15:09:11 -0700 Subject: [PATCH] sentry: support NULL mount source NULL mount sources can be valid, e.g. when mounting "proc". PiperOrigin-RevId: 653769241 --- pkg/sentry/syscalls/linux/sys_mount.go | 9 ++++++--- test/syscalls/linux/mount.cc | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/sentry/syscalls/linux/sys_mount.go b/pkg/sentry/syscalls/linux/sys_mount.go index faf09c710..b9776bc42 100644 --- a/pkg/sentry/syscalls/linux/sys_mount.go +++ b/pkg/sentry/syscalls/linux/sys_mount.go @@ -116,9 +116,12 @@ func Mount(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, } // Only copy in source, fstype, and data if we are doing a normal mount. - source, err := t.CopyInString(sourceAddr, hostarch.PageSize) - if err != nil { - return 0, nil, err + var source string + if sourceAddr != 0 { + source, err = t.CopyInString(sourceAddr, hostarch.PageSize) + if err != nil { + return 0, nil, err + } } fsType, err := t.CopyInString(typeAddr, hostarch.PageSize) if err != nil { diff --git a/test/syscalls/linux/mount.cc b/test/syscalls/linux/mount.cc index 172e774a9..edbabbd79 100644 --- a/test/syscalls/linux/mount.cc +++ b/test/syscalls/linux/mount.cc @@ -2328,6 +2328,28 @@ TEST(MountTest, DetachedMountBindFails) { SyscallFailsWithErrno(EINVAL)); } +TEST(MountTest, MountProc) { + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); + + // Mount procfs with a NULL source to a temporary directory. + const TempPath dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); + ASSERT_THAT(mount(NULL, dir.path().c_str(), "proc", 0, NULL), + SyscallSucceeds()); + auto cleanup = Cleanup([&dir] { + EXPECT_THAT(umount2(dir.path().c_str(), 0), SyscallSucceeds()); + }); + + // Verify that /proc/self/mountinfo describes the device as "none". + const std::vector mountinfo = + ASSERT_NO_ERRNO_AND_VALUE(ProcSelfMountInfoEntries()); + for (auto const& e : mountinfo) { + if (e.mount_point == dir.path()) { + EXPECT_EQ(e.fstype, "proc"); + EXPECT_EQ(e.mount_source, "none"); + } + } +} + } // namespace } // namespace testing