Skip to content

Commit 2a59e89

Browse files
authored
Validate the written chunks when using a seed (folbricht#214)
The seed values are validated at the beginning when we create the extraction "plan". However a seed might point to a location where is possible to read and write data. In that case, even if at the beginning we validated the seed data, the assumption that when we use `WriteInto()` the seed data is still valid might not be true anymore. For this reason we should also re-validate the written data to ensure that what ended up in the destination is exactly what we were expecting. Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
1 parent 8a266d6 commit 2a59e89

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

assemble.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ func AssembleFile(ctx context.Context, name string, idx Index, s Store, seeds []
128128
if err != nil {
129129
return err
130130
}
131+
132+
// Validate that the written chunks are exactly what we were expecting.
133+
// Because the seed might point to a RW location, if the data changed
134+
// while we were extracting an index, we might end up writing to the
135+
// destination some unexpected values.
136+
for _, c := range job.segment.chunks() {
137+
b := make([]byte, c.Size)
138+
if _, err := f.ReadAt(b, int64(c.Start)); err != nil {
139+
return err
140+
}
141+
sum := Digest.Sum(b)
142+
if sum != c.ID {
143+
return fmt.Errorf("written data in %s doesn't match its expected hash value, seed may have changed during processing", name)
144+
}
145+
}
146+
131147
stats.addBytesCopied(copied)
132148
stats.addBytesCloned(cloned)
133149
// Record this segment's been written in the self-seed to make it

0 commit comments

Comments
 (0)