diff --git a/backend/protondrive/crypto.go b/backend/protondrive/crypto.go index 4029cf5b0..54a6b3010 100644 --- a/backend/protondrive/crypto.go +++ b/backend/protondrive/crypto.go @@ -594,9 +594,9 @@ func generateContentKey(nodeKR *crypto.KeyRing, now time.Time) (contentKeyPacket // decryptContentKey returns the content session key from the base64 // key packet (v3 or v6 PKESK), decrypted with the node key nodeKR. // -// If contentKeyPacketSig is not empty, it is checked against nodeKR and -// extraVerifyKR (which may be nil): some clients signed with the -// address key. A signature which doesn't verify is returned as +// contentKeyPacketSig is checked against nodeKR and extraVerifyKR +// (which may be nil): some clients signed with the address key. A +// signature which doesn't verify, or is missing, is returned as // verifyErr with the session key. func decryptContentKey(contentKeyPacketB64, contentKeyPacketSig string, nodeKR, extraVerifyKR *crypto.KeyRing, now time.Time) (sk *crypto.SessionKey, verifyErr, err error) { keyPacket, err := base64.StdEncoding.DecodeString(contentKeyPacketB64) @@ -611,14 +611,11 @@ func decryptContentKey(contentKeyPacketB64, contentKeyPacketSig string, nodeKR, if err != nil { return nil, nil, fmt.Errorf("decrypt content key: %w", err) } - if contentKeyPacketSig != "" { - verifyKR, err := joinKeyRings(nodeKR, extraVerifyKR) - if err != nil { - return nil, nil, fmt.Errorf("decrypt content key: %w", err) - } - verifyErr = verifyDetached(sk.Key, contentKeyPacketSig, verifyKR, now) + verifyKR, err := joinKeyRings(nodeKR, extraVerifyKR) + if err != nil { + return nil, nil, fmt.Errorf("decrypt content key: %w", err) } - return sk, verifyErr, nil + return sk, verifyDetached(sk.Key, contentKeyPacketSig, verifyKR, now), nil } // encryptBlock returns the block plain encrypted with the content diff --git a/backend/protondrive/crypto_internal_test.go b/backend/protondrive/crypto_internal_test.go index 10336456a..e942528b1 100644 --- a/backend/protondrive/crypto_internal_test.go +++ b/backend/protondrive/crypto_internal_test.go @@ -518,7 +518,7 @@ func TestContentKeyRoundTrip(t *testing.T) { t.Run("NoSignature", func(t *testing.T) { got, verifyErr, err := decryptContentKey(b64, "", keys.keyRing, nil, now) require.NoError(t, err) - assert.NoError(t, verifyErr) + assert.ErrorIs(t, verifyErr, errSignatureMissing) assert.Equal(t, sk.Key, got.Key) }) t.Run("SignedByAddressKey", func(t *testing.T) { diff --git a/backend/protondrive/download.go b/backend/protondrive/download.go index be892bac0..986eb2e05 100644 --- a/backend/protondrive/download.go +++ b/backend/protondrive/download.go @@ -105,7 +105,7 @@ func (c *client) openRead(ctx context.Context, n *node, offset, limit int64, ful if offset < 0 { return nil, fmt.Errorf("open %q: negative offset %d", n.Name, offset) } - sk, err := c.contentKey(n) + sk, err := c.contentKey(n, "reading") if err != nil { return nil, fmt.Errorf("open %q: %w", n.Name, err) } @@ -183,17 +183,25 @@ func blockAt(sizes []int64, pos int64) (index int, offset int64) { return len(sizes) + 1, pos } -// contentKey returns the content session key of file n, checking its -// signature as the SDK's decryptContentKeyPacket does. +// contentKey returns the content session key of file n for op, +// "reading" or "updating", checking its signature as the SDK's +// decryptContentKeyPacket does, except that a missing signature fails +// too: anyone with the node's public key can make a content key packet. // -// An unverified node key is errIntegrity too: a key someone else made -// could decrypt any content they chose. -func (c *client) contentKey(n *node) (*crypto.SessionKey, error) { +// An unverified node key or content key is errIntegrity unless +// IgnoreSignatureErrors is set, when it is logged once per node and op: +// whoever made the key could read what is written with it and make up +// the content read. +func (c *client) contentKey(n *node, op string) (*crypto.SessionKey, error) { + hint := signatureHint + if op != "reading" { + hint = "set ignore_signature_errors to update the file anyway" + } if n.keys.unverified != nil { if !c.opt.IgnoreSignatureErrors { - return nil, fmt.Errorf("node key: %w: %w: %s", errIntegrity, n.keys.unverified, signatureHint) + return nil, fmt.Errorf("node key: %w: %w: %s", errIntegrity, n.keys.unverified, hint) } - c.reportUnverified("reading", n, "") + c.reportUnverified(op, n, "") } f := n.link.File now := c.s.serverTime() @@ -201,7 +209,7 @@ func (c *client) contentKey(n *node) (*crypto.SessionKey, error) { if err != nil { return nil, err } - if verifyErr != nil && n.CreateTime.Unix() <= contentKeyFallbackUntil { + if verifyErr != nil && !isNotSigned(verifyErr) && n.CreateTime.Unix() <= contentKeyFallbackUntil { // Old clients signed with an address key, and a rename or // move could change the node's signer without signing the // content key packet again. @@ -217,10 +225,10 @@ func (c *client) contentKey(n *node) (*crypto.SessionKey, error) { if verifyErr != nil { verifyErr = fmt.Errorf("content key: %w: %w", errIntegrity, verifyErr) if !c.opt.IgnoreSignatureErrors { - return nil, fmt.Errorf("%w: %s", verifyErr, signatureHint) + return nil, fmt.Errorf("%w: %s", verifyErr, hint) } - c.reportOnce("content key", n.ID, func() { - fs.Errorf(nil, "protondrive: %q (ID %s): %v", n.Name, n.ID, verifyErr) + c.reportOnce("content key "+op, n.ID, func() { + fs.Errorf(nil, "protondrive: %s %q (ID %s): %v", op, n.Name, n.ID, verifyErr) }) } return sk, nil diff --git a/backend/protondrive/download_internal_test.go b/backend/protondrive/download_internal_test.go index 279d127a8..933b6434c 100644 --- a/backend/protondrive/download_internal_test.go +++ b/backend/protondrive/download_internal_test.go @@ -259,7 +259,8 @@ func TestDownloadContentKey(t *testing.T) { created time.Time ok bool }{ - {"unsigned", "", time.Now(), true}, + {"unsigned", "", time.Now(), false}, + {"unsigned before 2023", "", before2023, false}, {"signer address", u.Email(), time.Now(), true}, {"other address before 2023", second, before2023, true}, {"other address after 2023", second, time.Now(), false}, diff --git a/backend/protondrive/golden_internal_test.go b/backend/protondrive/golden_internal_test.go index cfd945d24..f895f7283 100644 --- a/backend/protondrive/golden_internal_test.go +++ b/backend/protondrive/golden_internal_test.go @@ -782,7 +782,7 @@ func (gc *goldenCapture) file(ctx context.Context, n *node, gn goldenNode, store rk.note("XAttr: claims SHA-1 %s, the content has %s", n.X.SHA1, gn.Want.SHA1) } - sk, err := c.contentKey(n) + sk, err := c.contentKey(n, "reading") if err != nil { return fmt.Errorf("%q: %w", n.Name, err) } diff --git a/backend/protondrive/protontest/drive.go b/backend/protondrive/protontest/drive.go index 6e673d457..7d9a2f68e 100644 --- a/backend/protondrive/protontest/drive.go +++ b/backend/protondrive/protontest/drive.go @@ -59,6 +59,12 @@ const ( // DefaultMaxSpace is the MaxSpace of every user, 5 GiB. const DefaultMaxSpace int64 = 5 << 30 +// The claims of the "xattrs-unsigned" corruption. +const ( + ForgedModTime = "2099-01-01T00:00:00.000Z" + ForgedSHA1 = "0123456789abcdef0123456789abcdef01234567" +) + // drive is a user's volume with its main share and links. // // Stored values are never modified in place once a snapshot may have @@ -923,6 +929,18 @@ func (u *User) insertLink(parentID string, l *link) string { // - "foreign-block": the first block is the same plaintext encrypted // with another content key, with its hash and block signature to // match +// - "content-key-unsigned": the file's content key is one the server +// made, without a signature, and the blocks are encrypted under it +// with the blocks and manifest validly signed +// - "content-key-signature": as "content-key-unsigned", with the +// content key signed by an unrelated key +// - "hash-key-unsigned": the folder's hash key is one the server +// made, without a signature +// - "name-unsigned": the name is encrypted anew under a session key +// the server made, without a signature +// - "xattrs-unsigned": the extended attributes are encrypted anew +// without a signature, claiming ForgedModTime, and for a file a +// size of 1 TiB and ForgedSHA1 func (u *User) Corrupt(id string, what string) { impostor := generateKey("impostor", "impostor@proton.test") v := u.view() @@ -997,6 +1015,80 @@ func (u *User) Corrupt(id string, what string) { b.data, b.hash, b.encSignature = data, sum[:], encSig r.blocks[0] = &b }) + case "content-key-unsigned", "content-key-signature": + // A content key the server made, with the blocks encrypted + // under it, and the blocks and manifest signed anew by the + // user: more than a server can do, so that only the content + // key's signature can catch it. + key, oldSK := v.contentKey(l) + ck := newContentKey(key, oldSK.IsV6()) + sig := "" + if what == "content-key-signature" { + sig = signArmored(ck.sk.Key, impostor) + } + rev := l.activeRevision() + blocks := make([]*block, len(rev.blocks)) + var manifest []byte + for i, old := range rev.blocks { + plain, err := decryptBlock(old.data, old.encSignature, oldSK, key, v.verifier(old.signatureEmail, key), v.now) + if plain == nil { + mustCrypto("decrypt block", err) + } + b := *old + b.data, b.encSignature = ck.encryptBlock(plain, key, v.addrKey) + sum := sha256.Sum256(b.data) + b.hash = sum[:] + blocks[i] = &b + manifest = append(manifest, b.hash...) + } + manifestSig := signArmored(manifest, v.addrKey) + reblock := changeActiveRevision(func(r *revision) { r.blocks, r.manifestSignature = blocks, manifestSig }) + change = func(l *link) { + l.contentKeyPacket, l.contentKeyPacketSignature = ck.packet, sig + reblock(l) + } + case "hash-key-unsigned": + if l.typ != linkTypeFolder { + panic("protontest: hash-key-unsigned needs a folder") + } + key, _ := v.folderKeys(id) + bad := encryptArmored(newPassphrase(), key, nil, false) + change = func(l *link) { l.hashKey = bad } + case "name-unsigned": + parent, _ := v.parentKey(l) + name, err := decryptArmored(l.name, parent, nil, v.now) + mustCrypto("decrypt name", err) + bad := encryptArmored(name, parent, nil, false) + change = func(l *link) { l.name = bad } + case "xattrs-unsigned": + key, err := v.unlock(id) + if key == nil { + panic(fmt.Sprintf("protontest: unlock %s: %v", id, err)) + } + armored := l.xattr + if l.typ == linkTypeFile { + armored = l.activeRevision().xattr + } + var x fileXAttr + if armored != "" { + plain, err := decryptArmored(armored, key, nil, v.now) + mustCrypto("decrypt extended attributes", err) + mustCrypto("parse extended attributes", json.Unmarshal(plain, &x)) + } + x.Common.ModificationTime = ForgedModTime + if l.typ == linkTypeFile { + size := int64(1) << 40 + x.Common.Size = &size + x.Common.Digests = &struct{ SHA1 string }{SHA1: ForgedSHA1} + } + plain, err := json.Marshal(&x) + mustCrypto("marshal extended attributes", err) + bad := encryptArmored(plain, key, nil, true) + if l.typ == linkTypeFile { + change = changeActiveRevision(func(r *revision) { r.xattr = bad }) + } else { + change = func(l *link) { l.xattr = bad } + } default: panic("protontest: unknown corruption " + what) } diff --git a/backend/protondrive/security_test.go b/backend/protondrive/security_test.go index 61827c5f9..1d482df76 100644 --- a/backend/protondrive/security_test.go +++ b/backend/protondrive/security_test.go @@ -167,6 +167,16 @@ var hostileContents = []hostileContent{{ return u.SeedAnonymousFile(nested, "victim.bin", content, time.Now()) }, signatureOnly: true, +}, { + name: "unsigned content key the server made", + content: patternedContent(blockSize+10, 15), + prepare: seedCorrupted("content-key-unsigned"), + signatureOnly: true, +}, { + name: "content key signed by another key", + content: patternedContent(blockSize+10, 16), + prepare: seedCorrupted("content-key-signature"), + signatureOnly: true, }} // hostileNames are names a hostile server gives nodes, each of which @@ -538,6 +548,58 @@ func TestHostileServer(t *testing.T) { } }) + for _, corruption := range []string{"content-key-unsigned", "content-key-signature"} { + t.Run("ranged reads and updates of a file with "+corruption, func(t *testing.T) { + ctx := context.Background() + remote, _, u := setupFakeRemote(t) + content := patternedContent(blockSize+10, 17) + id := u.SeedFile(u.RootID(), "victim.bin", content, time.Now()) + u.Corrupt(id, corruption) + info := object.NewStaticObjectInfo("victim.bin", time.Now(), 6, true, nil, nil) + + f := newFakeFs(t, remote, "") + o, err := f.NewObject(ctx, "victim.bin") + require.NoError(t, err) + for _, r := range []fs.RangeOption{{Start: 1, End: 100}, {Start: blockSize, End: -1}} { + rc, err := o.Open(ctx, &r) + var got []byte + if err == nil { + got, err = io.ReadAll(rc) + _ = rc.Close() + } + require.Error(t, err, "%v read", r) + assert.Empty(t, got, "%v: bytes released", r) + assert.Contains(t, err.Error(), "ignore_signature_errors") + } + err = o.Update(ctx, bytes.NewReader([]byte("secret")), info) + require.Error(t, err, "a new revision was encrypted with the server's content key") + assert.Contains(t, err.Error(), "ignore_signature_errors") + for _, n := range u.Dump() { + if n.ID == id { + assert.Equal(t, content, n.Content) + } + } + + // With signature checks off it goes ahead, logged once + // per operation. + f = newFakeFs(t, strings.TrimSuffix(remote, ":")+",ignore_signature_errors=true:", "") + capture := captureLog(t, 0) + for range 2 { + o, err = f.NewObject(ctx, "victim.bin") + require.NoError(t, err) + got, err := readAll(ctx, o) + require.NoError(t, err) + assert.True(t, bytes.Equal(content, got)) + } + require.NoError(t, o.Update(ctx, bytes.NewReader([]byte("secret")), info)) + log := capture.String() + for _, op := range []string{"reading", "updating"} { + want := fmt.Sprintf("ERROR : protondrive: %s %q (ID %s): content key", op, "victim.bin", id) + assert.Equal(t, 1, strings.Count(log, want), "%q logged other than once in:\n%s", want, log) + } + }) + } + t.Run("names", func(t *testing.T) { // Its own stats, so the sync sees only its own errors. ctx := accounting.WithStatsGroup(context.Background(), "hostile names") diff --git a/backend/protondrive/upload.go b/backend/protondrive/upload.go index 9812496f6..a69fb3667 100644 --- a/backend/protondrive/upload.go +++ b/backend/protondrive/upload.go @@ -309,7 +309,7 @@ func (c *client) createRevision(ctx context.Context, n *node, size int64) (*draf if n.IsFolder || n.link == nil || n.link.File == nil || n.RevisionID == "" { return nil, fmt.Errorf("%q is not a file with content", n.Name) } - sk, err := c.contentKey(n) + sk, err := c.contentKey(n, "updating") if err != nil { return nil, err } diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md index c4a3e9de3..071007e3c 100644 --- a/docs/DECISIONS.md +++ b/docs/DECISIONS.md @@ -5,6 +5,32 @@ 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 refuses a file whose content key is unsigned + +**Status:** accepted (handoff ruling 29) +**Context:** Anyone holding a file's public node key, which the server +stores, can make a content key packet for it. Proton's SDK checks the +content key packet's signature only when one is present. A server could +then send its own content key without a signature: rclone would encrypt +every update of the file with a key the server knows, and a ranged read +(which can't check the manifest) would return whatever the server +encrypted under it. +**Decision:** A content key whose signature is missing or invalid makes +the file unverified, as an unverified node key does (rulings 18 and 22): +reads, full and ranged, and updates are refused unless +`ignore_signature_errors` is set, when rclone logs an `ERROR` once per file +and operation and goes ahead. The pre-2023 fallback to the account's other +address keys still applies to a signature which is present but fails, as +in the SDK. Integrity checks are never turned off. This is stricter than +the SDK. +**Alternatives:** Following the SDK (leaves the hole); a creation-date +exemption for unsigned keys (the date is server-supplied, so it would +exempt anything the server back-dates). +**Consequences:** A genuine file whose content key was never signed, if +any client ever wrote one, needs `ignore_signature_errors`. Every file in +the golden fixtures, written by Proton's web app, CLI and the old +library, has a signed content key. + ## 2026-11-12 - Proton Drive deletes an upload's draft only on an explicit refusal **Status:** accepted. Supersedes the read-back half of handoff ruling 11 diff --git a/docs/content/protondrive.md b/docs/content/protondrive.md index 0f2f35815..f0091c3cb 100644 --- a/docs/content/protondrive.md +++ b/docs/content/protondrive.md @@ -222,6 +222,9 @@ By default rclone refuses to: - read a file whose key, content key or manifest signature doesn't verify, or which is beneath a folder whose key isn't verified; +- update a file whose key or content key signature doesn't verify, as + its new content would be encrypted with a key someone else may hold. + A content key without a signature counts as one that doesn't verify; - create, upload, rename, move or copy anything into a folder whose key isn't verified, because what is written there would be encrypted to a key someone else may hold;