diff options
author | Chong Cai <chongc@google.com> | 2020-11-12 17:54:40 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-11-12 17:56:21 -0800 |
commit | f01f623879e87412e4d2340d37ff4d0fccdb6c2b (patch) | |
tree | 76d64e5d95a62fbef9761db86fb1a127fd51297e /pkg/merkletree/merkletree.go | |
parent | d700ba22abb9e5f29749cc3843991c31dc00384d (diff) |
Add children names into verity hash
children names map can be used to verify whether a child is expected
during walking, so that we can detect unexpected modifications that
deleted/renamed both the target file and the corresponding merkle tree
file.
PiperOrigin-RevId: 342170715
Diffstat (limited to 'pkg/merkletree/merkletree.go')
-rw-r--r-- | pkg/merkletree/merkletree.go | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/pkg/merkletree/merkletree.go b/pkg/merkletree/merkletree.go index e0a9e56c5..6acee90ef 100644 --- a/pkg/merkletree/merkletree.go +++ b/pkg/merkletree/merkletree.go @@ -19,6 +19,7 @@ import ( "bytes" "crypto/sha256" "crypto/sha512" + "encoding/gob" "fmt" "io" @@ -151,11 +152,15 @@ type VerityDescriptor struct { Mode uint32 UID uint32 GID uint32 + Children map[string]struct{} RootHash []byte } func (d *VerityDescriptor) String() string { - return fmt.Sprintf("Name: %s, Size: %d, Mode: %d, UID: %d, GID: %d, RootHash: %v", d.Name, d.FileSize, d.Mode, d.UID, d.GID, d.RootHash) + b := new(bytes.Buffer) + e := gob.NewEncoder(b) + e.Encode(d.Children) + return fmt.Sprintf("Name: %s, Size: %d, Mode: %d, UID: %d, GID: %d, Children: %v, RootHash: %v", d.Name, d.FileSize, d.Mode, d.UID, d.GID, b.Bytes(), d.RootHash) } // verify generates a hash from d, and compares it with expected. @@ -202,6 +207,9 @@ type GenerateParams struct { UID uint32 // GID is the group ID of the target file. GID uint32 + // Children is a map of children names for a directory. It should be + // empty for a regular file. + Children map[string]struct{} // HashAlgorithms is the algorithms used to hash data. HashAlgorithms int // TreeReader is a reader for the Merkle tree. @@ -294,6 +302,7 @@ func Generate(params *GenerateParams) ([]byte, error) { Mode: params.Mode, UID: params.UID, GID: params.GID, + Children: params.Children, RootHash: root, } return hashData([]byte(descriptor.String()), params.HashAlgorithms) @@ -318,6 +327,9 @@ type VerifyParams struct { UID uint32 // GID is the group ID of the target file. GID uint32 + // Children is a map of children names for a directory. It should be + // empty for a regular file. + Children map[string]struct{} // HashAlgorithms is the algorithms used to hash data. HashAlgorithms int // ReadOffset is the offset of the data range to be verified. @@ -348,6 +360,7 @@ func verifyMetadata(params *VerifyParams, layout *Layout) error { Mode: params.Mode, UID: params.UID, GID: params.GID, + Children: params.Children, RootHash: root, } return descriptor.verify(params.Expected, params.HashAlgorithms) @@ -409,6 +422,7 @@ func Verify(params *VerifyParams) (int64, error) { Mode: params.Mode, UID: params.UID, GID: params.GID, + Children: params.Children, } if err := verifyBlock(params.Tree, &descriptor, &layout, buf, i, params.HashAlgorithms, params.Expected); err != nil { return 0, err |