From 8fa6763976a2512a1c56945e4e38441e68eec160 Mon Sep 17 00:00:00 2001 From: Maksim An Date: Sun, 14 Nov 2021 13:30:53 -0800 Subject: [PATCH 1/2] Fix ReadExt4SuperBlock function Previously the function would read bytes from a given file and convert them into internal ext4 super block object, without checking that the read bytes are actually ext4 super block. Fix the behavior by checking ext4 super block magic. Signed-off-by: Maksim An --- ext4/tar2ext4/tar2ext4.go | 4 ++++ .../github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/ext4/tar2ext4/tar2ext4.go b/ext4/tar2ext4/tar2ext4.go index 1ffca8ecfc..742df5eb86 100644 --- a/ext4/tar2ext4/tar2ext4.go +++ b/ext4/tar2ext4/tar2ext4.go @@ -272,6 +272,10 @@ func ReadExt4SuperBlock(vhdPath string) (*format.SuperBlock, error) { if err := binary.Read(vhd, binary.LittleEndian, &sb); err != nil { return nil, err } + // Make sure the magic bytes are correct. + if sb.Magic != format.SuperBlockMagic { + return nil, fmt.Errorf("not an ext4 file system") + } return &sb, nil } diff --git a/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go b/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go index 1ffca8ecfc..742df5eb86 100644 --- a/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go +++ b/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go @@ -272,6 +272,10 @@ func ReadExt4SuperBlock(vhdPath string) (*format.SuperBlock, error) { if err := binary.Read(vhd, binary.LittleEndian, &sb); err != nil { return nil, err } + // Make sure the magic bytes are correct. + if sb.Magic != format.SuperBlockMagic { + return nil, fmt.Errorf("not an ext4 file system") + } return &sb, nil } From a8d4ea683cb6009dce52cda96aca2a97dd9ecf8c Mon Sep 17 00:00:00 2001 From: Maksim An Date: Tue, 16 Nov 2021 14:42:14 -0800 Subject: [PATCH 2/2] pr feedback: change to errors.New Signed-off-by: Maksim An --- ext4/tar2ext4/tar2ext4.go | 2 +- .../github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext4/tar2ext4/tar2ext4.go b/ext4/tar2ext4/tar2ext4.go index 742df5eb86..a82462f024 100644 --- a/ext4/tar2ext4/tar2ext4.go +++ b/ext4/tar2ext4/tar2ext4.go @@ -274,7 +274,7 @@ func ReadExt4SuperBlock(vhdPath string) (*format.SuperBlock, error) { } // Make sure the magic bytes are correct. if sb.Magic != format.SuperBlockMagic { - return nil, fmt.Errorf("not an ext4 file system") + return nil, errors.New("not an ext4 file system") } return &sb, nil } diff --git a/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go b/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go index 742df5eb86..a82462f024 100644 --- a/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go +++ b/test/vendor/github.com/Microsoft/hcsshim/ext4/tar2ext4/tar2ext4.go @@ -274,7 +274,7 @@ func ReadExt4SuperBlock(vhdPath string) (*format.SuperBlock, error) { } // Make sure the magic bytes are correct. if sb.Magic != format.SuperBlockMagic { - return nil, fmt.Errorf("not an ext4 file system") + return nil, errors.New("not an ext4 file system") } return &sb, nil }