Skip to content

Commit 9f426d3

Browse files
committed
oci: cas: use blob file size as ExpectedSize
In a future patch, ExpectedSize < 0 will no longer be supported by VerifiedReadCloser. However, this also gives us an opportunity to add a bit of extra hardening here -- if an attacker can write blobs to our store then they could in theory trigger a DoS by constantly writing more bytes (or expanding a zero section of a sparse file) if we do not have some hard limit. The current file size is as good a limit as any (and is going to be correct in all reasonable cases). This also lets us avoid double-hashing blobs in the common case where the blob size is correct (because then the VerifiedReadCloser returned by GetVerifiedBlob() will be a no-op). Signed-off-by: Aleksa Sarai <[email protected]>
1 parent f585d05 commit 9f426d3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

oci/cas/dir/dir.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,17 @@ func (e *dirEngine) GetBlob(_ context.Context, digest digest.Digest) (io.ReadClo
244244
if err != nil {
245245
return nil, fmt.Errorf("open blob: %w", err)
246246
}
247+
st, err := fh.Stat()
248+
if err != nil {
249+
return nil, fmt.Errorf("stat blob: %w", err)
250+
}
247251
return &hardening.VerifiedReadCloser{
248252
Reader: fh,
249253
ExpectedDigest: digest,
250-
ExpectedSize: int64(-1), // We don't know the expected size.
254+
// Assume the file size is the blob size. This is almost certainly true
255+
// in general, and if an attacker is modifying the blobs underneath us
256+
// then snapshotting the size makes sure we don't read endlessly.
257+
ExpectedSize: st.Size(),
251258
}, nil
252259
}
253260

0 commit comments

Comments
 (0)