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
This commit is contained in:
Shambhavi Srivastava 2022-05-11 18:33:34 -07:00 committed by gVisor bot
parent 74f3befece
commit a7cad2b092
2 changed files with 12 additions and 1 deletions

View File

@ -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 {

View File

@ -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