-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ReadDMVeritySuperBlock function (#1257)
Make sure to assert the value of dmveritySuperblock.Signature before trying to read the root hash. Without the check, any valid footer with with valid size would be treated as a dm-verity superblock. Additionally wrap original Read errors, rather than custom errors. In some places when returning an error from ReadDMVeritySuperBlock, a custom error was wrapped, rather than the original error, which complicates assertions with errors.Cause/errors.Unwrap. Add unit tests to cover other edge cases. Signed-off-by: Maksim An <[email protected]>
- Loading branch information
Showing
4 changed files
with
124 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package dmverity | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func tempFileWithContentLength(t *testing.T, length int) *os.File { | ||
tmpFile, err := ioutil.TempFile("", "") | ||
if err != nil { | ||
t.Fatalf("failed to create temp file") | ||
} | ||
defer tmpFile.Close() | ||
|
||
content := make([]byte, length) | ||
if _, err := rand.Read(content); err != nil { | ||
t.Fatalf("failed to write random bytes to buffer") | ||
} | ||
if _, err := tmpFile.Write(content); err != nil { | ||
t.Fatalf("failed to write random bytes to temp file") | ||
} | ||
return tmpFile | ||
} | ||
|
||
func writeDMVeritySuperBlock(filename string) (*os.File, error) { | ||
out, err := os.OpenFile(filename, os.O_RDWR, 0777) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer out.Close() | ||
fsSize, err := out.Seek(0, io.SeekEnd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sb := NewDMVeritySuperblock(uint64(fsSize)) | ||
if err := binary.Write(out, binary.LittleEndian, sb); err != nil { | ||
return nil, err | ||
} | ||
sbSize := int(unsafe.Sizeof(*sb)) | ||
padding := bytes.Repeat([]byte{0}, blockSize-(sbSize%blockSize)) | ||
if _, err = out.Write(padding); err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
|
||
func TestInvalidReadEOF(t *testing.T) { | ||
tmpFile := tempFileWithContentLength(t, blockSize) | ||
_, err := ReadDMVerityInfo(tmpFile.Name(), blockSize) | ||
if err == nil { | ||
t.Fatalf("no error returned") | ||
} | ||
if errors.Cause(err) != io.EOF { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
} | ||
|
||
func TestInvalidReadNotEnoughBytes(t *testing.T) { | ||
tmpFile := tempFileWithContentLength(t, blockSize+blockSize/2) | ||
_, err := ReadDMVerityInfo(tmpFile.Name(), blockSize) | ||
if err == nil { | ||
t.Fatalf("no error returned") | ||
} | ||
if errors.Cause(err) != ErrSuperBlockReadFailure || !strings.Contains(err.Error(), "unexpected bytes read") { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
} | ||
|
||
func TestNotVeritySuperBlock(t *testing.T) { | ||
tmpFile := tempFileWithContentLength(t, 2*blockSize) | ||
_, err := ReadDMVerityInfo(tmpFile.Name(), blockSize) | ||
if err == nil { | ||
t.Fatalf("no error returned") | ||
} | ||
if err != ErrNotVeritySuperBlock { | ||
t.Fatalf("expected %q, got %q", ErrNotVeritySuperBlock, err) | ||
} | ||
} | ||
|
||
func TestNoMerkleTree(t *testing.T) { | ||
tmpFile := tempFileWithContentLength(t, blockSize) | ||
targetFile, err := writeDMVeritySuperBlock(tmpFile.Name()) | ||
if err != nil { | ||
t.Fatalf("failed to write dm-verity super-block: %s", err) | ||
} | ||
_, err = ReadDMVerityInfo(targetFile.Name(), blockSize) | ||
if err == nil { | ||
t.Fatalf("no error returned") | ||
} | ||
if errors.Cause(err) != io.EOF || !strings.Contains(err.Error(), "failed to read dm-verity root hash") { | ||
t.Fatalf("expected %q, got %q", io.EOF, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 10 additions & 5 deletions
15
test/vendor/github.com/Microsoft/hcsshim/ext4/dmverity/dmverity.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.