Repository navigation
mutate: verify layer digests in Extract and Time - #2303
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
extractLayer and layerTime read a layer through an archive/tar reader, which stops at the tar end-of-archive marker before the layer stream reaches io.EOF. The verifying reader in internal/verify checks the layer digest only at io.EOF, so on these paths the digest is never verified: mutate.Extract and mutate.Time accept a layer whose contents do not match the layer digest in the manifest. pkg/v1/validate/layer.go already drains the stream after its tar loop for this reason. Add the same drain to extractLayer and layerTime so the verifying reader reaches io.EOF and the digest is checked.
1e891d2 to
b96b250
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2303 +/- ##
==========================================
+ Coverage 56.85% 56.90% +0.05%
==========================================
Files 165 165
Lines 11355 11359 +4
==========================================
+ Hits 6456 6464 +8
+ Misses 4136 4134 -2
+ Partials 763 761 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Please provide tests that model the scenario you are describing. |
Add regression tests for the digest check added in the previous commit. Each test builds a layer whose Uncompressed stream is wrapped by internal/verify against a digest that does not match the bytes served -- modelling a layer whose contents disagree with the manifest's layer digest -- and asserts that Extract and Time reject it. The tests also cover the matching-digest case so the success path is exercised. Without the drain added to extractLayer and layerTime the mismatch cases pass through undetected; both fail if the fix is reverted.
|
Hello @Subserial, thank you for your comment I just added tests in pkg/v1/mutate/mutate_test.go (5f9072a). TestExtractVerifiesLayerDigest and TestTimeVerifiesLayerDigest each build a layer whose Uncompressed() stream is wrapped by internal/verify against a digest that doesn't match the bytes it serves, which is the case described in the PR. Both assert the operation fails with a checksum error, and both have a matching-digest case to cover the normal path. I reverted the fix locally to check the tests actually catch it: the mismatch cases pass through silently without the drain and fail with it. go test ./pkg/v1/mutate/... is green. |
crane 0.21.7 Created-by: HarmonybrewBot Commit-by: HarmonybrewBot Merged-by: HarmonybrewBot Description: Created by `brew bump` --- Created with `brew bump-formula-pr`.<details> <summary>release notes</summary> <pre>## What's Changed * tarball: return error instead of panicking on missing rootfs.diff_ids by @iahsanGill in google/go-containerregistry#2304 * gcrane: honor --platform flag in copy by @iahsanGill in google/go-containerregistry#2307 * mutate: verify layer digests in Extract and Time by @momenashrafff in google/go-containerregistry#2303 * tarball: close layer readers during Write by @nandbhat in google/go-containerregistry#2308 * build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2311 * build(deps): bump github.com/docker/cli from 29.4.3+incompatible to 29.5.2+incompatible in the go-deps group across 1 directory by @dependabot[bot] in google/go-containerregistry#2312 * BUGFIX: Fail with error when read exceeds maximum by @inteon in google/go-containerregistry#2328 * build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2327 * fix(name): anchor loopback registry detection by @rohan-patnaik in google/go-containerregistry#2314 * Reject symlinks in OCI layout blobs by @mosskappa in google/go-containerregistry#2306 * fix(crane): avoid creating export tar on pull failure by @Haihan-Jiang in google/go-containerregistry#2318 * feat(kubernetes): allow ignoring pull secrets by @rohan-patnaik in google/go-containerregistry#2315 * fix(name): preserve localhost registry references by @rohan-patnaik in google/go-containerregistry#2316 * pkg/registry: export ErrNotFound by @malt3 in google/go-containerregistry#2176 * pkg/registry: export RedirectError by @malt3 in google/go-containerregistry#2177 * build(deps): bump the go-deps group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2343 * build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2344 * fix: prevent SSRF in google.List() pagination by @tufstraka in google/go-containerregistry#2332 * internal/gzip: fix goroutine leak in ReadCloserLevel by @amarkdotdev in google/go-containerregistry#2347 * fix(transport): apply refreshed bearer token after cross-host redirect by @64johnlee in google/go-containerregistry#2337 * build(deps): bump the go-deps group across 3 directories with 4 updates by @dependabot[bot] in google/go-containerregistry#2348 * fix(tarball): normalize paths when matching files by @bstoll in google/go-containerregistry#2334 * transport: do not re-attach bearer token after cross-host redirect by @evilgensec in google/go-containerregistry#2349 * Bump CI go version to 1.26.4 by @Subserial in google/go-containerregistry#2350 ## New Contributors * @momenashrafff made their first contribution in google/go-containerregistry#2303 * @nandbhat made their first contribution in google/go-containerregistry#2308 * @inteon made their first contribution in google/go-containerregistry#2328 * @rohan-patnaik made their first contribution in google/go-containerregistry#2314 * @mosskappa made their first contribution in google/go-containerregistry#2306 * @Haihan-Jiang made their first contribution in google/go-containerregistry#2318 * @tufstraka made their first contribution in google/go-containerregistry#2332 * @amarkdotdev made their first contribution in google/go-containerregistry#2347 * @64johnlee made their first contribution in google/go-containerregistry#2337 * @bstoll made their first contribution in google/go-containerregistry#2334 **Full Changelog**: https://github.com/google/go-containerregistry/compare/v0.21.6...v0.21.7</pre> <p>View the full release notes at <a href="https://github.com/google/go-containerregistry/releases/tag/v0.21.7">https://github.com/google/go-containerregistry/releases/tag/v0.21.7</a>.</p> </details> <hr> See merge request: Harmonybrew/homebrew-core!11861
The puller now takes an HTTP client, nil for the default one, and sends its requests through its transport, so that tests can reach in-memory registries and override TLS settings. The loader passes the client it uses for remote files. As in go-containerregistry, registries are reached over HTTPS, and plain HTTP is only tried for local and private hosts; this is now documented. mutate.Extract stops reading a layer at the end of its tar archive, while go-containerregistry only verifies a blob against its digest once it is read to its end: a tampered layer was extracted without an error, even for a reference pinned by digest. Each layer is now read to its end once extracted, and a mismatch fails the pull. This was fixed in go-containerregistry v0.21.7 (google/go-containerregistry#2303); the wrapper can go once the dependency is bumped from v0.20.6. Files are copied to the file system instead of being read into memory first. ocitest builds and pushes images with crane, and runs registries with httptest.NewTestServer: on its in-memory network, serving HTTPS only, for the puller tests, and on the loopback interface for the build tests, which use the default client. The registries ignore the local docker credentials, as credential helpers may block. New tests cover untrusted certificates, HTTP-only registries, and malformed and tampered layers.
The puller now takes an HTTP client, nil for the default one, and sends its requests through its transport, so that tests can reach in-memory registries and override TLS settings. The loader passes the client it uses for remote files. As in go-containerregistry, registries are reached over HTTPS, and plain HTTP is only tried for local and private hosts; this is now documented. mutate.Extract stops reading a layer at the end of its tar archive, while go-containerregistry only verifies a blob against its digest once it is read to its end: a tampered layer was extracted without an error, even for a reference pinned by digest. Each layer is now read to its end once extracted, and a mismatch fails the pull. This was fixed in go-containerregistry v0.21.7 (google/go-containerregistry#2303); the wrapper can go once the dependency is bumped from v0.20.6. Files are copied to the file system instead of being read into memory first. ocitest builds and pushes images with crane, and runs registries with httptest.NewTestServer: on its in-memory network, serving HTTPS only, for the puller tests, and on the loopback interface for the build tests, which use the default client. The registries ignore the local docker credentials, as credential helpers may block. New tests cover untrusted certificates, HTTP-only registries, and malformed and tampered layers.
The puller now takes an HTTP client, nil for the default one, and sends its requests through its transport, so that tests can reach in-memory registries and override TLS settings. The loader passes the client it uses for remote files. As in go-containerregistry, registries are reached over HTTPS, and plain HTTP is only tried for local and private hosts; this is now documented. mutate.Extract stops reading a layer at the end of its tar archive, while go-containerregistry only verifies a blob against its digest once it is read to its end: a tampered layer was extracted without an error, even for a reference pinned by digest. Each layer is now read to its end once extracted, and a mismatch fails the pull. This was fixed in go-containerregistry v0.21.7 (google/go-containerregistry#2303); the wrapper can go once the dependency is bumped from v0.20.6. Files are copied to the file system instead of being read into memory first. ocitest builds and pushes images with crane, and runs registries with httptest.NewTestServer: on its in-memory network, serving HTTPS only, for the puller tests, and on the loopback interface for the build tests, which use the default client. The registries ignore the local docker credentials, as credential helpers may block. New tests cover untrusted certificates, HTTP-only registries, and malformed and tampered layers.
The puller now takes an HTTP client, nil for the default one, and sends its requests through its transport, so that tests can reach in-memory registries and override TLS settings. The loader passes the client it uses for remote files. As in go-containerregistry, registries are reached over HTTPS, and plain HTTP is only tried for local and private hosts; this is now documented. mutate.Extract stops reading a layer at the end of its tar archive, while go-containerregistry only verifies a blob against its digest once it is read to its end: a tampered layer was extracted without an error, even for a reference pinned by digest. Each layer is now read to its end once extracted, and a mismatch fails the pull. This was fixed in go-containerregistry v0.21.7 (google/go-containerregistry#2303); the wrapper can go once the dependency is bumped from v0.20.6. Files are copied to the file system instead of being read into memory first. ocitest builds and pushes images with crane, and runs registries with httptest.NewTestServer: on its in-memory network, serving HTTPS only, for the puller tests, and on the loopback interface for the build tests, which use the default client. The registries ignore the local docker credentials, as credential helpers may block. New tests cover untrusted certificates, HTTP-only registries, and malformed and tampered layers.
From v0.21.7 on, go-containerregistry reads each layer to its end once extracted, so that mutate.Extract verifies it against its digest and size (google/go-containerregistry#2303). Bump the dependency from v0.20.6 to v0.22.1, the latest release, and drop the wrapper that did this here. A tampered layer still fails the pull, now on go-containerregistry's own checks: the digest of the uncompressed layer, the size of the gzip one. The bump also removes estargz, docker/distribution, go-homedir, pkg/errors and tar-split from the dependencies of the api module. Note that extraction stands in for a file system view of the image until google/go-containerregistry#921 is implemented.
The puller now takes an HTTP client, nil for the default one, and sends its requests through its transport, so that tests can reach in-memory registries and override TLS settings. The loader passes the client it uses for remote files. As in go-containerregistry, registries are reached over HTTPS, and plain HTTP is only tried for local and private hosts; this is now documented. mutate.Extract stops reading a layer at the end of its tar archive, while go-containerregistry only verifies a blob against its digest once it is read to its end: a tampered layer was extracted without an error, even for a reference pinned by digest. Each layer is now read to its end once extracted, and a mismatch fails the pull. This was fixed in go-containerregistry v0.21.7 (google/go-containerregistry#2303); the wrapper can go once the dependency is bumped from v0.20.6. Files are copied to the file system instead of being read into memory first. ocitest builds and pushes images with crane, and runs registries with httptest.NewTestServer: on its in-memory network, serving HTTPS only, for the puller tests, and on the loopback interface for the build tests, which use the default client. The registries ignore the local docker credentials, as credential helpers may block. New tests cover untrusted certificates, HTTP-only registries, and malformed and tampered layers.
From v0.21.7 on, go-containerregistry reads each layer to its end once extracted, so that mutate.Extract verifies it against its digest and size (google/go-containerregistry#2303). Bump the dependency from v0.20.6 to v0.22.1, the latest release, and drop the wrapper that did this here. A tampered layer still fails the pull, now on go-containerregistry's own checks: the digest of the uncompressed layer, the size of the gzip one. The bump also removes estargz, docker/distribution, go-homedir, pkg/errors and tar-split from the dependencies of the api module. Note that extraction stands in for a file system view of the image until google/go-containerregistry#921 is implemented.
The puller now takes an HTTP client, nil for the default one, and sends its requests through its transport, so that tests can reach in-memory registries and override TLS settings. The loader passes the client it uses for remote files. As in go-containerregistry, registries are reached over HTTPS, and plain HTTP is only tried for local and private hosts; this is now documented. mutate.Extract stops reading a layer at the end of its tar archive, while go-containerregistry only verifies a blob against its digest once it is read to its end: a tampered layer was extracted without an error, even for a reference pinned by digest. Each layer is now read to its end once extracted, and a mismatch fails the pull. This was fixed in go-containerregistry v0.21.7 (google/go-containerregistry#2303); the wrapper can go once the dependency is bumped from v0.20.6. Files are copied to the file system instead of being read into memory first. ocitest builds and pushes images with crane, and runs registries with httptest.NewTestServer: on its in-memory network, serving HTTPS only, for the puller tests, and on the loopback interface for the build tests, which use the default client. The registries ignore the local docker credentials, as credential helpers may block. New tests cover untrusted certificates, HTTP-only registries, and malformed and tampered layers.
From v0.21.7 on, go-containerregistry reads each layer to its end once extracted, so that mutate.Extract verifies it against its digest and size (google/go-containerregistry#2303). Bump the dependency from v0.20.6 to v0.22.1, the latest release, and drop the wrapper that did this here. A tampered layer still fails the pull, now on go-containerregistry's own checks: the digest of the uncompressed layer, the size of the gzip one. The bump also removes estargz, docker/distribution, go-homedir, pkg/errors and tar-split from the dependencies of the api module. Note that extraction stands in for a file system view of the image until google/go-containerregistry#921 is implemented.
What
extractLayerandlayerTimeinpkg/v1/mutate/mutate.goread a layer through anarchive/tarreader.archive/tarreturnsio.EOFfromNext()as soon as it reads the two-block end-of-archive marker, so the loop stops before the underlying layer stream reaches its ownio.EOF.internal/verify's reader checks the layer's SHA-256 against the expected digest only when the wrapped stream returnsio.EOF(verifyReader.Read). Because the tar loop stops early that point is never reached, andClose()does not verify either. As a resultmutate.Extractandmutate.Time, andcrane export/crane editwhich build onmutate.Extract, accept a layer whose contents do not match the layer digest listed in the manifest.crane pullandpkg/v1/validateare not affected: they read each layer toio.EOF, so the digest check fires.Fix
pkg/v1/validate/layer.goalready drains the stream after itstar.Readerloop for this reason:This change adds the same drain after the
tar.Readerloop inextractLayerandlayerTime, so the underlying verifying reader reachesio.EOFand the layer digest is checked.Testing
crane exportbuilt from this branch fails witherror verifying sha256 checksuminstead of succeeding. Built from currentmainit succeeds.go test ./pkg/v1/mutate/...passes.