Skip to content

Commit

Permalink
nametransform: reject all-zero dirIV
Browse files Browse the repository at this point in the history
This should never happen in normal operation and is a sign of
data corruption. Catch it early.
  • Loading branch information
rfjakob committed May 25, 2017
1 parent 2ce269e commit 9a3f935
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/nametransform/diriv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nametransform

import (
"bytes"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -46,6 +47,9 @@ func ReadDirIVAt(dirfd *os.File) (iv []byte, err error) {
return fdReadDirIV(fd)
}

// allZeroDirIV is preallocated to quickly check if the data read from disk is all zero
var allZeroDirIV = make([]byte, DirIVLen)

// fdReadDirIV reads and verifies the DirIV from an opened gocryptfs.diriv file.
func fdReadDirIV(fd *os.File) (iv []byte, err error) {
// We want to detect if the file is bigger than DirIVLen, so
Expand All @@ -61,6 +65,10 @@ func fdReadDirIV(fd *os.File) (iv []byte, err error) {
tlog.Warn.Printf("ReadDirIVAt: wanted %d bytes, got %d. Returning EINVAL.", DirIVLen, len(iv))
return nil, syscall.EINVAL
}
if bytes.Equal(iv, allZeroDirIV) {
tlog.Warn.Printf("ReadDirIVAt: diriv is all-zero. Returning EINVAL.")
return nil, syscall.EINVAL
}
return iv, nil
}

Expand Down

0 comments on commit 9a3f935

Please sign in to comment.