protondrive: refuse metadata which decompresses to an excessive size
Names, node passphrases, hash keys and extended attributes are encrypted to public keys, so the server, or anyone, can write them as a small compressed message which expands to gigabytes when decrypted. They were decompressed into memory without a limit. Names, passphrases and hash keys are now limited to 64 KiB and extended attributes to 16 MiB, enough for the block sizes of an 8 TiB file. Extended attributes with a negative Size are now read as having no size, as the reference SDK reads other invalid sizes, so a file is never listed with a negative size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3fac25de75
commit
32710eefd1
3 files changed
+71
-8
No files matched your search
@@ -196,11 +196,25 @@ func encryptArmored(plain []byte, sk *crypto.SessionKey, encKR, signKR *crypto.K
|
||||
return armorMessage(msg, sk)
|
||||
}
|
||||
|
||||
// Limits of decrypted metadata, which decryptMessage enforces: a
|
||||
// compressed message can expand a thousandfold or more, and anyone
|
||||
// holding the public key can write one.
|
||||
const (
|
||||
// maxSmallMessageSize bounds a name, passphrase or hash key. A name
|
||||
// is at most 255 UTF-16 code units, so 1020 bytes of UTF-8.
|
||||
maxSmallMessageSize = 64 << 10
|
||||
// maxXAttrsSize bounds extended attributes, whose block sizes take
|
||||
// about 2 MiB for a 1 TiB file.
|
||||
maxXAttrsSize = 16 << 20
|
||||
)
|
||||
|
||||
// decryptMessage decrypts the armored message with decKR and returns
|
||||
// it with its session key. Its embedded signature is checked against
|
||||
// verifyKR alone, and any failure returned as verifyErr; a nil verifyKR
|
||||
// checks it against no key, so verifyErr is set.
|
||||
func decryptMessage(armored string, decKR, verifyKR *crypto.KeyRing, now time.Time) (res *crypto.VerifiedDataResult, sk *crypto.SessionKey, verifyErr, err error) {
|
||||
// checks it against no key, so verifyErr is set. A compressed message
|
||||
// which expands to more than maxSize bytes, counting the packet headers
|
||||
// and signatures inside it, is an error.
|
||||
func decryptMessage(armored string, decKR, verifyKR *crypto.KeyRing, now time.Time, maxSize int64) (res *crypto.VerifiedDataResult, sk *crypto.SessionKey, verifyErr, err error) {
|
||||
msg, err := crypto.NewPGPMessageFromArmored(armored)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
@@ -222,7 +236,7 @@ func decryptMessage(armored string, decKR, verifyKR *crypto.KeyRing, now time.Ti
|
||||
// is decrypted with the session key alone: otherwise a signature by
|
||||
// any holder of decKR would verify. gopenpgp can then not check the
|
||||
// intended recipients a signature lists, so that is done below.
|
||||
b := crypto.PGP().Decryption().SessionKey(sk).VerificationKeys(verifyKR).DisableIntendedRecipients()
|
||||
b := crypto.PGP().Decryption().SessionKey(sk).VerificationKeys(verifyKR).DisableIntendedRecipients().MaxDecompressedMessageSize(maxSize)
|
||||
if !now.IsZero() {
|
||||
b = b.VerifyTime(now.Unix())
|
||||
}
|
||||
@@ -408,7 +422,7 @@ func encryptPassphrase(keys *nodeKeys, parentKR, signerKR *crypto.KeyRing, now t
|
||||
// A signature which doesn't verify, or is missing, is returned as
|
||||
// verifyErr with the keys; err is set only if the keys can't be had.
|
||||
func decryptNodeKeys(parentKR, verifyKR *crypto.KeyRing, armoredKey, encPassphrase, passphraseSig string, now time.Time) (keys *nodeKeys, verifyErr, err error) {
|
||||
res, sk, _, err := decryptMessage(encPassphrase, parentKR, nil, now)
|
||||
res, sk, _, err := decryptMessage(encPassphrase, parentKR, nil, now, maxSmallMessageSize)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("decrypt node passphrase: %w", err)
|
||||
}
|
||||
@@ -450,7 +464,7 @@ func encryptName(name string, sk *crypto.SessionKey, parentKR, signerKR *crypto.
|
||||
// against verifyKR. A signature which doesn't verify is returned as
|
||||
// verifyErr with the name.
|
||||
func decryptName(encName string, parentKR, verifyKR *crypto.KeyRing, now time.Time) (name string, verifyErr, err error) {
|
||||
res, _, verifyErr, err := decryptMessage(encName, parentKR, verifyKR, now)
|
||||
res, _, verifyErr, err := decryptMessage(encName, parentKR, verifyKR, now, maxSmallMessageSize)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("decrypt name: %w", err)
|
||||
}
|
||||
@@ -528,7 +542,7 @@ func decryptHashKey(encHashKey string, nodeKR, extraVerifyKR *crypto.KeyRing, no
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("decrypt hash key: %w", err)
|
||||
}
|
||||
res, _, verifyErr, err := decryptMessage(encHashKey, nodeKR, verifyKR, now)
|
||||
res, _, verifyErr, err := decryptMessage(encHashKey, nodeKR, verifyKR, now, maxSmallMessageSize)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("decrypt hash key: %w", err)
|
||||
}
|
||||
@@ -817,7 +831,9 @@ func parseXAttrs(data []byte) *xattrs {
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = json.Unmarshal(common.Size, &x.Size)
|
||||
if json.Unmarshal(common.Size, &x.Size) != nil || x.Size < 0 {
|
||||
x.Size = 0
|
||||
}
|
||||
var blockSizes []int64
|
||||
if json.Unmarshal(common.BlockSizes, &blockSizes) == nil && len(blockSizes) > 0 {
|
||||
x.BlockSizes = blockSizes
|
||||
@@ -857,7 +873,7 @@ func decryptXAttrs(enc string, nodeKR, verifyKR *crypto.KeyRing, now time.Time)
|
||||
if enc == "" {
|
||||
return &xattrs{}, nil, nil
|
||||
}
|
||||
res, _, verifyErr, err := decryptMessage(enc, nodeKR, verifyKR, now)
|
||||
res, _, verifyErr, err := decryptMessage(enc, nodeKR, verifyKR, now, maxXAttrsSize)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("decrypt extended attributes: %w", err)
|
||||
}
|
||||
|
||||
@@ -765,6 +765,49 @@ func TestXAttrs(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &xattrs{}, got)
|
||||
})
|
||||
t.Run("NegativeSize", func(t *testing.T) {
|
||||
in := `{"Common":{"Size":-5,"BlockSizes":[5],"Digests":{"SHA1":"abc"}}}`
|
||||
got, _, err := decryptXAttrs(encryptSigned(t, []byte(in), nodeKR, signer, now), nodeKR, publicOf(t, signer), now)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &xattrs{BlockSizes: []int64{5}, SHA1: "abc"}, got)
|
||||
})
|
||||
}
|
||||
|
||||
// TestDecryptedSizeBounded checks a compressed message which expands
|
||||
// past what its kind of metadata needs is refused rather than
|
||||
// decompressed into memory: anyone holding the public key, the server
|
||||
// included, can make one.
|
||||
func TestDecryptedSizeBounded(t *testing.T) {
|
||||
now := time.Now()
|
||||
parent, signer := newTestKeyRing(t, now), newTestKeyRing(t, now)
|
||||
signerPub := publicOf(t, signer)
|
||||
compressed := func(plain []byte) string {
|
||||
enc, err := encryptArmored(plain, nil, parent, signer, true, now)
|
||||
require.NoError(t, err)
|
||||
require.Less(t, len(enc), len(plain)/10, "compressed")
|
||||
return enc
|
||||
}
|
||||
padded := func(prefix string, n int) []byte {
|
||||
return append([]byte(prefix), bytes.Repeat([]byte(" "), n-len(prefix))...)
|
||||
}
|
||||
|
||||
// The limit counts the packets inside the compressed one: the data
|
||||
// with its header, and the signature.
|
||||
const overhead = 1 << 10
|
||||
_, _, err := decryptName(compressed(padded("name", maxSmallMessageSize-overhead)), parent, signerPub, now)
|
||||
require.NoError(t, err, "within the limit")
|
||||
_, _, err = decryptName(compressed(padded("name", maxSmallMessageSize+1)), parent, signerPub, now)
|
||||
assert.Error(t, err)
|
||||
_, _, err = decryptHashKey(compressed(padded("key", maxSmallMessageSize+1)), parent, signerPub, now)
|
||||
assert.Error(t, err)
|
||||
_, _, err = decryptNodeKeys(parent, signerPub, "", compressed(padded("passphrase", maxSmallMessageSize+1)), "", now)
|
||||
assert.ErrorContains(t, err, "passphrase")
|
||||
|
||||
x, _, err := decryptXAttrs(compressed(padded(`{"Common":{"Size":7}}`, maxXAttrsSize-overhead)), parent, signerPub, now)
|
||||
require.NoError(t, err, "within the limit")
|
||||
assert.Equal(t, int64(7), x.Size)
|
||||
_, _, err = decryptXAttrs(compressed(padded(`{"Common":{"Size":7}}`, maxXAttrsSize+1)), parent, signerPub, now)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestServerTimeUsedForSignatures(t *testing.T) {
|
||||
|
||||
@@ -129,3 +129,7 @@ Small, specific, independently actionable. Remove only when actually done.
|
||||
- [ ] protondrive: `TestRegressionManyNewFs` can't see from the fake
|
||||
server that key unlocks run one at a time; only
|
||||
`TestLoadAccountSerialised` (internal, `unlockHook`) checks it.
|
||||
- [ ] protondrive: an address key's `Token` (account.go
|
||||
`unlockAddressKey`) is decrypted without a decompression limit, unlike
|
||||
node metadata (`decryptMessage`); a hostile server could send a
|
||||
compression bomb there too. Seen in Task 17.
|
||||
Reference in new issue
Block a user