From a7cad2b092de8430562886ceaa57c14c6eec7763 Mon Sep 17 00:00:00 2001 From: Shambhavi Srivastava Date: Wed, 11 May 2022 18:33:34 -0700 Subject: [PATCH] Tmpfs with size option enabled bug fix. Adding test case to check empty size parsing in Linux. If a value is not passed with size the mount(2) should return EINVAL. Handling empty size parsing in gVisor. PiperOrigin-RevId: 448132149 --- pkg/sentry/fsimpl/tmpfs/tmpfs.go | 5 ++++- test/syscalls/linux/mount.cc | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/sentry/fsimpl/tmpfs/tmpfs.go b/pkg/sentry/fsimpl/tmpfs/tmpfs.go index 432a03490..fabc46f24 100644 --- a/pkg/sentry/fsimpl/tmpfs/tmpfs.go +++ b/pkg/sentry/fsimpl/tmpfs/tmpfs.go @@ -204,7 +204,7 @@ func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.Virt delete(mopts, "size") maxSizeInBytes, err := parseSize(maxSizeStr) if err != nil { - ctx.Warningf("tmpfs.FilesystemType.GetFilesystem: invalid size: %q", maxSizeStr) + ctx.Debugf("tmpfs.FilesystemType.GetFilesystem: parseSize() failed: %v", err) return nil, nil, linuxerr.EINVAL } // Convert size in bytes to nearest Page Size bytes @@ -948,6 +948,9 @@ func (*fileDescription) Sync(context.Context) error { // parseSize converts size in string to an integer bytes. // Supported suffixes in string are:K, M, G, T, P, E. func parseSize(s string) (uint64, error) { + if len(s) == 0 { + return 0, fmt.Errorf("size parameter empty") + } suffix := s[len(s)-1] count := 1 switch suffix { diff --git a/test/syscalls/linux/mount.cc b/test/syscalls/linux/mount.cc index 0b127643f..ebb201eda 100644 --- a/test/syscalls/linux/mount.cc +++ b/test/syscalls/linux/mount.cc @@ -614,6 +614,14 @@ TEST(MountTest, TmpfsHardLinkAllocCheck) { EXPECT_THAT(unlink(fileOne.c_str()), SyscallSucceeds()); } +// Tests memory allocation for empty size. +TEST(MountTest, TmpfsEmptySizeAllocCheck) { + SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); + auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); + ASSERT_THAT(mount("", dir.path().c_str(), "tmpfs", 0, "size"), + SyscallFailsWithErrno(EINVAL)); +} + } // namespace } // namespace testing