diff --git a/backend/protondrive/crypto.go b/backend/protondrive/crypto.go index cdf652316..7e02bdcfe 100644 --- a/backend/protondrive/crypto.go +++ b/backend/protondrive/crypto.go @@ -214,8 +214,14 @@ const ( // verifyKR alone, and any failure returned as verifyErr; a nil verifyKR // 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. +// and signatures inside it, is an error, and so is an armored message +// too large to hold at most maxSize bytes uncompressed. func decryptMessage(armored string, decKR, verifyKR *crypto.KeyRing, now time.Time, maxSize int64) (res *crypto.VerifiedDataResult, sk *crypto.SessionKey, verifyErr, err error) { + // Armor takes 4/3 of the binary size and line breaks; the rest is + // room for the key packets. + if maxArmored := 2*maxSize + 64<<10; int64(len(armored)) > maxArmored { + return nil, nil, nil, fmt.Errorf("message is larger than %d bytes", maxArmored) + } msg, err := crypto.NewPGPMessageFromArmored(armored) if err != nil { return nil, nil, nil, err diff --git a/backend/protondrive/crypto_internal_test.go b/backend/protondrive/crypto_internal_test.go index 95a0852de..25fe2aba0 100644 --- a/backend/protondrive/crypto_internal_test.go +++ b/backend/protondrive/crypto_internal_test.go @@ -895,6 +895,13 @@ func TestDecryptedSizeBounded(t *testing.T) { assert.Equal(t, int64(7), x.Size) _, _, err = decryptXAttrs(compressed(padded(`{"Common":{"Size":7}}`, maxXAttrsSize+1)), parent, signerPub, now) assert.Error(t, err) + + // An uncompressed message far over the limit is refused before it + // is decrypted into memory. + huge, err := encryptArmored(padded("name", 4*maxSmallMessageSize), nil, parent, signer, false, now) + require.NoError(t, err) + _, _, err = decryptName(huge, parent, signerPub, now) + assert.ErrorContains(t, err, "larger than") } func TestServerTimeUsedForSignatures(t *testing.T) { diff --git a/backend/protondrive/nodes.go b/backend/protondrive/nodes.go index f7aef5fb2..b982985f7 100644 --- a/backend/protondrive/nodes.go +++ b/backend/protondrive/nodes.go @@ -744,6 +744,12 @@ func (c *client) fetchNodeIn(ctx context.Context, id string, below []string) (*n } parent, err := c.folderKeysIn(ctx, d.Link.ParentLinkID, append(below[:len(below):len(below)], id)) if err != nil { + if len(below) > 0 { + // Wrapped once, by the node asked for: at every level + // of a chain a server can make 2048 deep, it would + // grow without bound. + return nil, err + } return nil, fmt.Errorf("get node %s: %w", id, err) } n, unreadable, err := c.decryptNode(ctx, &d, parent) diff --git a/backend/protondrive/nodes_internal_test.go b/backend/protondrive/nodes_internal_test.go index a08acf093..35cc5bfbe 100644 --- a/backend/protondrive/nodes_internal_test.go +++ b/backend/protondrive/nodes_internal_test.go @@ -1318,6 +1318,10 @@ func TestFolderDepthLimit(t *testing.T) { _, err := c.getNode(context.Background(), ids[5]) require.ErrorIs(t, err, errIntegrity) assert.Contains(t, err.Error(), "more than 4 deep") + // The error names the node asked for and the one too deep, not + // every folder between, which a hostile server could make 2048. + assert.Equal(t, 2, strings.Count(err.Error(), "get node"), err.Error()) + assert.True(t, strings.HasPrefix(err.Error(), "get node "+ids[5]+": "), err.Error()) // Within the limit is fine. c = newTestClientFor(t, srv, u, clientOptions{}) c.maxFolderDepth = 6 diff --git a/docs/TODO.md b/docs/TODO.md index 04f37ea67..a3d49c913 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -111,10 +111,6 @@ Small, specific, independently actionable. Remove only when actually done. read with only an `ERROR` logged (spec 6.2, the SDK's behaviour), and names aren't bound to their node, so a server can swap the encrypted names of two files in one folder undetected. Raise with Proton. -- [ ] protondrive: a hostile 10 MiB name is decrypted in full before - `validateName` rejects it (about 230 MiB allocated listing it, the - fake included). Consider refusing an encrypted name over a few KiB - before decrypting it. - [ ] lib/pacer: `TokenDispenser.Get` takes no context, so a cancelled protondrive block download still waits for a download token (held by