From b3c88f573f4c1b269e78dd7d1dbd6de73d477694 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 6 Oct 2019 19:34:32 +0200 Subject: contentenc: add explicit test for all-zero header https://github.com/rfjakob/gocryptfs/issues/363 --- internal/contentenc/file_header.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'internal') diff --git a/internal/contentenc/file_header.go b/internal/contentenc/file_header.go index 5f8bd63..6ce2e3b 100644 --- a/internal/contentenc/file_header.go +++ b/internal/contentenc/file_header.go @@ -44,21 +44,25 @@ func (h *FileHeader) Pack() []byte { // allZeroFileID is preallocated to quickly check if the data read from disk is all zero var allZeroFileID = make([]byte, headerIDLen) +var allZeroHeader = make([]byte, HeaderLen) // ParseHeader - parse "buf" into fileHeader object func ParseHeader(buf []byte) (*FileHeader, error) { if len(buf) != HeaderLen { return nil, fmt.Errorf("ParseHeader: invalid length, want=%d have=%d", HeaderLen, len(buf)) } + if bytes.Equal(buf, allZeroHeader) { + return nil, fmt.Errorf("ParseHeader: header is all-zero. Header hexdump: %s", hex.EncodeToString(buf)) + } var h FileHeader h.Version = binary.BigEndian.Uint16(buf[0:headerVersionLen]) if h.Version != CurrentVersion { - return nil, fmt.Errorf("ParseHeader: invalid version, want=%d have=%d. Hexdump: %s", + return nil, fmt.Errorf("ParseHeader: invalid version, want=%d have=%d. Header hexdump: %s", CurrentVersion, h.Version, hex.EncodeToString(buf)) } h.ID = buf[headerVersionLen:] if bytes.Equal(h.ID, allZeroFileID) { - return nil, fmt.Errorf("ParseHeader: file id is all-zero. Hexdump: %s", + return nil, fmt.Errorf("ParseHeader: file id is all-zero. Header hexdump: %s", hex.EncodeToString(buf)) } return &h, nil -- cgit v1.2.3