protondrive: refuse node keys whose Argon2 parameters would exhaust memory

A node or share key from the server was unlocked with whatever Argon2
parameters it carried, so a crafted key could make every listing
including it allocate up to 2 TiB and crash rclone. Node and share keys
locked with Argon2 beyond 4 passes, parallelism 8 or 128 MiB are now
refused before the key derivation runs, as an integrity error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5.5 committed 2026-11-12 14:23:50 +01:00
1 parent e95317b52a
commit db7afafc08
5 files changed
+239 -4

No files matched your search

+118 -1
View File
@@ -19,6 +19,7 @@ import (
"unicode/utf8"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/go-crypto/openpgp/s2k"
"github.com/ProtonMail/go-srp"
"github.com/ProtonMail/gopenpgp/v3/armor"
"github.com/ProtonMail/gopenpgp/v3/constants"
@@ -428,7 +429,7 @@ func decryptNodeKeys(parentKR, verifyKR *crypto.KeyRing, armoredKey, encPassphra
}
passphrase := res.Bytes()
verifyErr = verifyDetached(passphrase, passphraseSig, verifyKR, now)
key, err := unlockKey(armoredKey, passphrase)
key, err := unlockForeignKey(armoredKey, passphrase)
if err != nil {
clear(passphrase)
return nil, nil, fmt.Errorf("unlock node key: %w", err)
@@ -442,6 +443,122 @@ func decryptNodeKeys(parentKR, verifyKR *crypto.KeyRing, armoredKey, encPassphra
return &nodeKeys{keyRing: kr, passphrase: passphrase, passphraseSessionKey: sk}, verifyErr, nil
}
// Limits of an Argon2 S2K locking a key from the server or another
// user, checked before the KDF runs, which go-crypto lets use up to
// 2 TiB of memory and 255 passes. gopenpgp and OpenPGP.js write 3
// passes, parallelism 4 and 64 MiB by default; the node keys Proton's
// clients write use an iterated and salted S2K instead, whose one byte
// count encoding bounds its cost to 65011712 bytes hashed, 4 times the
// default.
const (
maxArgon2MemoryExp = 17 // 128 MiB
maxArgon2Passes = 4
maxArgon2Parallelism = 8
)
// unlockForeignKey is unlockKey for a key from the server or another
// user. It returns an error wrapping errIntegrity, without running the
// S2K, if the key is locked with an Argon2 S2K beyond the limits.
func unlockForeignKey(armored string, passphrase []byte) (*crypto.Key, error) {
data, err := armor.Unarmor(armored)
if err != nil {
return nil, err
}
if err := checkKeyS2K(data); err != nil {
return nil, err
}
locked, err := crypto.NewKey(data)
if err != nil {
return nil, err
}
return locked.Unlock(passphrase)
}
// checkKeyS2K returns an error wrapping errIntegrity if a secret key or
// subkey packet of the binary key data is locked with an Argon2 S2K
// beyond the limits.
func checkKeyS2K(data []byte) error {
r := packet.NewOpaqueReader(bytes.NewReader(data))
for {
op, err := r.Next()
if err == io.EOF {
return nil
}
if err != nil {
return fmt.Errorf("read key: %w", err)
}
// 5 and 7 are the secret key and secret subkey packets.
if op.Tag != 5 && op.Tag != 7 {
continue
}
spec, err := s2kSpecifier(op)
if err != nil {
return err
}
if len(spec) < 20 || spec[0] != byte(s2k.Argon2S2K) {
continue
}
passes, parallelism, memExp := spec[17], spec[18], spec[19]
if passes > maxArgon2Passes || parallelism > maxArgon2Parallelism || memExp > maxArgon2MemoryExp {
return fmt.Errorf("%w: the key is locked with Argon2 of %d passes, parallelism %d and 2^%d KiB, more than rclone accepts", errIntegrity, passes, parallelism, memExp)
}
}
}
// s2kSpecifier returns the bytes of the secret key or subkey packet op
// from its S2K specifier on, or nil if it isn't locked with one.
func s2kSpecifier(op *packet.OpaquePacket) ([]byte, error) {
var buf bytes.Buffer
if err := op.Serialize(&buf); err != nil {
return nil, fmt.Errorf("read key: %w", err)
}
p, err := packet.Read(&buf)
if err != nil {
return nil, fmt.Errorf("read key: %w", err)
}
pk, ok := p.(*packet.PrivateKey)
if !ok {
return nil, fmt.Errorf("read key: packet is %T", p)
}
// The secret packet's body starts with the public key's.
buf.Reset()
if err := pk.PublicKey.Serialize(&buf); err != nil {
return nil, fmt.Errorf("read key: %w", err)
}
pub, err := packet.NewOpaqueReader(&buf).Next()
if err != nil {
return nil, fmt.Errorf("read key: %w", err)
}
if !bytes.HasPrefix(op.Contents, pub.Contents) {
return nil, errors.New("read key: secret key packet doesn't start with its public key")
}
// RFC 9580 section 5.5.3: the S2K usage octet, a count octet in
// v5 and v6, the cipher, an AEAD mode for usage 253, and an S2K
// specifier length octet in v6, as go-crypto parses them.
rest := op.Contents[len(pub.Contents):]
if len(rest) == 0 {
return nil, errors.New("read key: secret key packet is truncated")
}
usage, i := rest[0], 1
if usage != 253 && usage != 254 && usage != 255 {
return nil, nil
}
if pk.Version == 5 || pk.Version == 6 {
i++
}
i++
if usage == 253 {
i++
}
if pk.Version == 6 {
i++
}
if i >= len(rest) {
return nil, errors.New("read key: secret key packet is truncated")
}
return rest[i:], nil
}
// reencryptPassphrase returns the passphrase of keys encrypted to
// newParentKR, with the same session key, and a new detached signature
// by signerKR, for a move.
+75 -3
View File
@@ -22,6 +22,7 @@ import (
"github.com/ProtonMail/gopenpgp/v3/armor"
"github.com/ProtonMail/gopenpgp/v3/constants"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/ProtonMail/gopenpgp/v3/profile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -95,7 +96,7 @@ func TestDecryptForkPayload(t *testing.T) {
}
// newTestKeyRing returns a key ring of a new unlocked key created at now.
func newTestKeyRing(t *testing.T, now time.Time) *crypto.KeyRing {
func newTestKeyRing(t testing.TB, now time.Time) *crypto.KeyRing {
t.Helper()
key, err := crypto.PGP().KeyGeneration().AddUserId("test", "test@proton.test").GenerationTime(now.Unix()).New().GenerateKey()
require.NoError(t, err)
@@ -126,7 +127,7 @@ func readPackets(t *testing.T, msg []byte, n int) []packet.Packet {
}
// unarmor returns the binary form of the armored message.
func unarmor(t *testing.T, armored string) []byte {
func unarmor(t testing.TB, armored string) []byte {
t.Helper()
b, err := armor.Unarmor(armored)
require.NoError(t, err)
@@ -147,7 +148,7 @@ func requireNonAEAD(t *testing.T, armored string) {
}
// testNodeKeys returns new node keys under parentKR, signed by signerKR.
func testNodeKeys(t *testing.T, parentKR, signerKR *crypto.KeyRing, aead bool, now time.Time) (armoredKey, encPassphrase, passphraseSig string, keys *nodeKeys) {
func testNodeKeys(t testing.TB, parentKR, signerKR *crypto.KeyRing, aead bool, now time.Time) (armoredKey, encPassphrase, passphraseSig string, keys *nodeKeys) {
t.Helper()
armoredKey, encPassphrase, passphraseSig, keys, err := generateNodeKeys(parentKR, signerKR, aead, now)
require.NoError(t, err)
@@ -207,6 +208,77 @@ func TestNodeKeysRoundTrip(t *testing.T) {
}
}
// argon2Locked returns the key of keys locked with its passphrase
// under a cheap Argon2 S2K, with the S2K's passes, parallelism and
// memory exponent then set to those given.
func argon2Locked(t testing.TB, keys *nodeKeys, passes, parallelism, memExp byte) string {
t.Helper()
pgp := crypto.PGPWithProfile(&profile.Custom{
SetKeyAlgorithm: func(*packet.Config, int8) {},
AeadKeyEncryption: &packet.AEADConfig{},
S2kKeyEncryption: &s2k.Config{
S2KMode: s2k.Argon2S2K,
Argon2Config: &s2k.Argon2Config{NumberOfPasses: 1, DegreeOfParallelism: 1, Memory: 8},
},
})
locked, err := pgp.LockKey(keys.keyRing.GetKeys()[0], keys.passphrase)
require.NoError(t, err)
b, err := locked.Serialize()
require.NoError(t, err)
// Each specifier is mode 4, a 16 byte salt, then passes 1,
// parallelism 1 and memory exponent 3 (8 KiB).
patched := 0
for i := 0; i+19 < len(b); i++ {
if b[i] == byte(s2k.Argon2S2K) && b[i+17] == 1 && b[i+18] == 1 && b[i+19] == 3 {
b[i+17], b[i+18], b[i+19] = passes, parallelism, memExp
patched++
}
}
require.Equal(t, 2, patched, "S2K specifiers of the key and subkey")
armored, err := armor.ArmorWithType(b, constants.PrivateKeyHeader)
require.NoError(t, err)
return armored
}
func TestNodeKeyS2KLimits(t *testing.T) {
now := time.Now()
parent, signer := newTestKeyRing(t, now), newTestKeyRing(t, now)
_, encPassphrase, passphraseSig, keys := testNodeKeys(t, parent, signer, true, now)
verifyKR := publicOf(t, signer)
t.Run("Accepted", func(t *testing.T) {
got, verifyErr, err := decryptNodeKeys(parent, verifyKR, argon2Locked(t, keys, 1, 1, 3), encPassphrase, passphraseSig, now)
require.NoError(t, err)
require.NoError(t, verifyErr)
assert.Equal(t, keys.keyRing.GetKeys()[0].GetFingerprint(), got.keyRing.GetKeys()[0].GetFingerprint())
// What gopenpgp and OpenPGP.js write by default.
data := unarmor(t, argon2Locked(t, keys, 3, 4, 16))
assert.NoError(t, checkKeyS2K(data))
})
for _, tc := range []struct {
name string
passes, parallelism, memEx byte
}{
// Unlocking any of these would run the KDF first: 256 MiB, or
// up to 2 TiB, for the memory exponents.
{"Memory", 1, 1, 18},
{"Passes", 255, 1, 3},
{"Parallelism", 1, 255, 11},
} {
t.Run(tc.name, func(t *testing.T) {
start := time.Now()
_, _, err := decryptNodeKeys(parent, verifyKR, argon2Locked(t, keys, tc.passes, tc.parallelism, tc.memEx), encPassphrase, passphraseSig, now)
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "Argon2")
assert.Less(t, time.Since(start), 2*time.Second, "the KDF ran")
})
}
t.Run("MemoryAtTheLibraryLimit", func(t *testing.T) {
data := unarmor(t, argon2Locked(t, keys, 1, 1, 31))
assert.ErrorIs(t, checkKeyS2K(data), errIntegrity)
})
}
// secretKeyLocking returns the version, algorithm, S2K usage and S2K
// specifier of a v4 secret key or subkey packet body of an ECC key.
func secretKeyLocking(t *testing.T, body []byte) (version, algo, usage, specifier byte) {
+17
View File
@@ -211,6 +211,23 @@ func checkXAttrs(t *testing.T, x *xattrs) {
assert.NotContains(t, x.Extra, "Common")
}
func FuzzCheckKeyS2K(f *testing.F) {
f.Add(unarmor(f, loadOldlibFixture(f, "file.json").NodeKey))
now := time.Now()
parent, signer := newTestKeyRing(f, now), newTestKeyRing(f, now)
_, _, _, keys := testNodeKeys(f, parent, signer, true, now)
for _, memExp := range []byte{3, 17, 18, 31} {
f.Add(unarmor(f, argon2Locked(f, keys, 1, 1, memExp)))
}
f.Fuzz(func(t *testing.T, data []byte) {
// Whatever the bytes, the check never runs a KDF and never
// panics; a key it lets through still parses or fails to.
if checkKeyS2K(data) == nil {
_, _ = crypto.NewKey(data)
}
})
}
func FuzzDecryptXAttrs(f *testing.F) {
fx := loadOldlibFixture(f, "file.json")
key, err := unlockKey(fx.NodeKey, []byte(fx.NodePassphrasePlain))
+22
View File
@@ -5,6 +5,28 @@ it with a new one that links back. One entry per decision a future maintainer
would otherwise have to reverse-engineer.
No index: `grep -n '^## ' docs/DECISIONS.md` lists every entry.
## 2026-11-12 - Proton Drive caps the Argon2 cost of node and share keys
**Status:** accepted
**Context:** go-crypto unlocks an Argon2-locked key with whatever
parameters it carries, up to 2 TiB of memory and 255 passes, and runs the
KDF before it can tell the passphrase is wrong. Node and share keys come
from the server, or from other users' clients in shared folders, so one
crafted key could crash every listing that includes it.
**Decision:** Before unlocking a node or share key, every secret key
packet's S2K is checked: Argon2 may use at most 4 passes, parallelism 8
and 2^17 KiB (128 MiB). gopenpgp and OpenPGP.js write 3, 4 and 64 MiB by
default, and Proton's node keys use an iterated and salted S2K, whose
one-byte count encoding already bounds it to 4 times the default. A key
beyond the limits is an integrity error, making the node unreadable, and
`ignore_signature_errors` doesn't change that.
**Alternatives:** Refusing Argon2 on node keys outright (no genuine node
key is known to use it, but a future client might); capping the iterated
count below the encoding's maximum (could refuse genuine old keys for a
bounded cost).
**Consequences:** At most 128 MiB per unlock while a listing decrypts
nodes. The account's own user keys aren't checked yet (`docs/TODO.md`).
## 2026-11-12 - Proton Drive refuses a file whose content key is unsigned
**Status:** accepted (handoff ruling 29)
+7
View File
@@ -265,3 +265,10 @@ Small, specific, independently actionable. Remove only when actually done.
listing instead since Task 17.
- [ ] protondrive: spec 9.5 asks for a fuzz target for event decoding;
add it with events (fuzz_internal_test.go).
- [ ] protondrive: the account's own user keys (account.go `loadAccount`)
are unlocked without the Argon2 limits node and share keys get
(crypto.go `checkKeyS2K`): the server stores them too, so a hostile
server could make sign-in run a KDF of up to 2 TiB. Left out because a
genuine user key's S2K parameters aren't known to be within the
limits; check what Proton's apps write before applying them. Address
keys are safe: their token's signature is checked first.