From 1228e6c788a0a6f35b9792aaa1ffe20ac72ce1c6 Mon Sep 17 00:00:00 2001 From: Shambhavi Srivastava Date: Thu, 19 May 2022 18:23:59 -0700 Subject: [PATCH] Adding tests to handle empty size & uint64 overflow in parsing size. PiperOrigin-RevId: 449876821 --- pkg/sentry/fsimpl/tmpfs/tmpfs.go | 11 +++++++++-- pkg/sentry/fsimpl/tmpfs/tmpfs_test.go | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/sentry/fsimpl/tmpfs/tmpfs.go b/pkg/sentry/fsimpl/tmpfs/tmpfs.go index 9ac6d5518..e9fb3b90f 100644 --- a/pkg/sentry/fsimpl/tmpfs/tmpfs.go +++ b/pkg/sentry/fsimpl/tmpfs/tmpfs.go @@ -972,7 +972,14 @@ func parseSize(s string) (uint64, error) { count = count << 10 s = s[:len(s)-1] } - bytes, err := strconv.ParseUint(s, 10, 64) - bytes = bytes * uint64(count) + byteTmp, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return 0, linuxerr.EINVAL + } + // Check for overflow. + bytes := byteTmp * uint64(count) + if byteTmp != 0 && bytes/byteTmp != uint64(count) { + return 0, fmt.Errorf("size overflow") + } return bytes, err } diff --git a/pkg/sentry/fsimpl/tmpfs/tmpfs_test.go b/pkg/sentry/fsimpl/tmpfs/tmpfs_test.go index 2aa58c8ec..df3ad2286 100644 --- a/pkg/sentry/fsimpl/tmpfs/tmpfs_test.go +++ b/pkg/sentry/fsimpl/tmpfs/tmpfs_test.go @@ -171,6 +171,8 @@ func TestParseSize(t *testing.T) { {"5P", (5 * 1024 * 1024 * 1024 * 1024 * 1024), false}, {"5e", (5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024), false}, {"5e3", 0, true}, + {"", 0, true}, + {"9999999999999999P", 0, true}, } for _, tt := range tests { testname := fmt.Sprintf("%s", tt.s)