"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
serve sftp: fix auth proxy letting a public key be used as a password GHSA-66qv-66mg-9h74 CVE-PENDING
Nick Craig-Woodf6ca72fabb09
diff --git a/cmd/serve/proxy/proxy.go b/cmd/serve/proxy/proxy.goindex bf107dcb2..f89decdfe 100644--- a/cmd/serve/proxy/proxy.go+++ b/cmd/serve/proxy/proxy.go@@ -143,12 +143,15 @@ to make proxy to many different sftp backends, you could make thein the output and the user to |user|. For security you'd probably wantto restrict the |host| to a limited list.-An internal cache of backends is keyed on the |user|, a hash of the-|pass| or |public_key|, and the |client_ip|. This means that if a-user's password or public-key changes, the client connects from a new IP-address, or the proxy returns different config parameters (eg a rotated-|api_key|), a fresh backend will be created on the next request rather-than the cached one being reused.+An internal cache of backends is keyed on the |user|, the kind of+credential presented, a hash of the |pass| or |public_key|, and the+|client_ip|. This means that if a user's password or public-key+changes, the client connects from a new IP address, or the proxy+returns different config parameters (eg a rotated |api_key|), a fresh+backend will be created on the next request rather than the cached one+being reused. A backend created for a public key login is never+reused for a password login, so a public key cannot be sent as a+password to skip the program.This can be used to build general purpose proxies to any kind ofbackend that rclone supports.@@ -188,6 +191,7 @@ type Proxy struct {// cacheEntry is what is stored in the vfsCachetype cacheEntry struct {vfs *vfs.VFS // stored VFS+ kind authKind // kind of credential the entry was created forpwHash [sha256.Size]byte // sha256 hash of the password/publicKeysecret string // secret access key returned by the proxy for access key authrefreshed *atomic.Int64 // unix nanoseconds when the proxy last confirmed this entry@@ -308,9 +312,14 @@ func ipFromAddr(addr string) string {}// generateCacheKey creates a composite cache key from the user, the auth-// credentials and the client's IP address.-func generateCacheKey(user, auth, clientIP string) string {+// credentials, the kind of credential and the client's IP address.+//+// The kind is part of the key so that a public key, which is not a+// secret, presented as a password can't hit the entry created when its+// owner logged in with it.+func generateCacheKey(user, auth string, kind authKind, clientIP string) string {mac := hmac.New(sha256.New, cacheKeyHMACKey)+ mac.Write([]byte{byte(kind)})mac.Write([]byte(auth))// Separate the two so ("ab", "c") can't collide with ("a", "bc")mac.Write([]byte{0})@@ -370,7 +379,7 @@ func (p *Proxy) call(user, auth string, kind authKind, clientIP string) (value a// Make the cache key include the auth and the client IP so that// changes to either (eg the proxy returning new config) create a// fresh backend rather than reusing the cached one.- cacheKey := generateCacheKey(user, auth, clientIP)+ cacheKey := generateCacheKey(user, auth, kind, clientIP)// For access key auth the secret isn't part of the cache key as// it isn't known until the proxy has been run, so retire any@@ -389,7 +398,7 @@ func (p *Proxy) call(user, auth string, kind authKind, clientIP string) (value adefer p.accessKeyMu.Unlock()if old, ok := p.vfsCache.GetMaybe(cacheKey); ok {if entry, ok := old.(cacheEntry); ok && entry.secret != secret {- p.vfsCache.Rename(cacheKey, generateCacheKey(user, entry.secret, clientIP))+ p.vfsCache.Rename(cacheKey, generateCacheKey(user, entry.secret, kind, clientIP))}}}@@ -420,6 +429,7 @@ func (p *Proxy) call(user, auth string, kind authKind, clientIP string) (value a// after the unencrypted password in memory most likely.entry := cacheEntry{vfs: vfs.New(p.ctx, f, &p.vfsOpt),+ kind: kind,pwHash: sha256.Sum256([]byte(auth)),secret: secret,refreshed: new(atomic.Int64),@@ -445,20 +455,20 @@ func (p *Proxy) call(user, auth string, kind authKind, clientIP string) (value a// connecting over a unix socket.func (p *Proxy) Call(user, auth string, isPublicKey bool, remoteAddr string) (VFS *vfs.VFS, vfsKey string, err error) {clientIP := ipFromAddr(remoteAddr)+ kind := authPassword+ if isPublicKey {+ kind = authPublicKey+ }- // Cache key includes the auth and the client IP so credential or- // address changes don't hit a stale entry- cacheKey := generateCacheKey(user, auth, clientIP)+ // Cache key includes the auth, its kind and the client IP so+ // credential, method or address changes don't hit a stale entry+ cacheKey := generateCacheKey(user, auth, kind, clientIP)// Look in the cache first with the credential-aware keyvalue, ok := p.vfsCache.GetMaybe(cacheKey)// If not found then call the proxy for a fresh answerif !ok {- kind := authPassword- if isPublicKey {- kind = authPublicKey- }// Logins with the same credentials which arrive together// share one call, as each call which made its own cache// entry would take a reference to the VFS but only the@@ -477,13 +487,14 @@ func (p *Proxy) Call(user, auth string, isPublicKey bool, remoteAddr string) (VFreturn nil, "", fmt.Errorf("proxy: value is not cache entry: %#v", value)}- // Check the password / public key matches the cached entry. The- // cache key already includes a hash of the auth, so a changed- // credential lands on a fresh key rather than this entry; this- // check is a final guard against a hash collision on the key- // returning a backend created with different auth.+ // Check the password / public key and its kind match the cached+ // entry. The cache key already includes a hash of the auth and its+ // kind, so a changed credential or method lands on a fresh key+ // rather than this entry; this check is a final guard against a+ // hash collision on the key returning a backend created with+ // different auth.authHash := sha256.Sum256([]byte(auth))- if subtle.ConstantTimeCompare(authHash[:], entry.pwHash[:]) != 1 {+ if entry.kind != kind || subtle.ConstantTimeCompare(authHash[:], entry.pwHash[:]) != 1 {if isPublicKey {return nil, "", errors.New("proxy: incorrect public key")}@@ -514,7 +525,7 @@ func (p *Proxy) Call(user, auth string, isPublicKey bool, remoteAddr string) (VF// remoteAddr is the address of the client as returned by net.Addr.String().func (p *Proxy) CallAccessKey(accessKeyID, remoteAddr string, refresh bool) (VFS *vfs.VFS, secret string, err error) {clientIP := ipFromAddr(remoteAddr)- cacheKey := generateCacheKey(accessKeyID, "", clientIP)+ cacheKey := generateCacheKey(accessKeyID, "", authAccessKey, clientIP)value, ok := p.vfsCache.GetMaybe(cacheKey)if ok {if entry, isEntry := value.(cacheEntry); isEntry {diff --git a/cmd/serve/proxy/proxy_code.go b/cmd/serve/proxy/proxy_code.goindex f4e9948d3..ec53bdff6 100644--- a/cmd/serve/proxy/proxy_code.go+++ b/cmd/serve/proxy/proxy_code.go@@ -35,6 +35,11 @@ func main() {v += "-test"case "error":log.Fatal(v)+ case "pass":+ // Simulate a key-only policy which refuses every password+ if os.Getenv("RCLONE_TEST_PROXY_KEY_ONLY") != "" {+ log.Fatalf("password refused for %q", in["user"])+ }}out[k] = v}diff --git a/cmd/serve/proxy/proxy_test.go b/cmd/serve/proxy/proxy_test.goindex 9b6dc4d72..78e6e6bca 100644--- a/cmd/serve/proxy/proxy_test.go+++ b/cmd/serve/proxy/proxy_test.go@@ -107,14 +107,25 @@ func TestRun(t *testing.T) {// affect the cache key, otherwise the proxy would be run for// every connection rather than once per client.assert.Equal(t,- generateCacheKey(testUser, testPass, ipFromAddr(testIP+":1024")),- generateCacheKey(testUser, testPass, ipFromAddr(testIP+":2048")))+ generateCacheKey(testUser, testPass, authPassword, ipFromAddr(testIP+":1024")),+ generateCacheKey(testUser, testPass, authPassword, ipFromAddr(testIP+":2048")))// A different client IP must produce a different key so the// proxy is consulted againassert.NotEqual(t,- generateCacheKey(testUser, testPass, ipFromAddr(testAddr)),- generateCacheKey(testUser, testPass, ipFromAddr(otherAddr)))+ generateCacheKey(testUser, testPass, authPassword, ipFromAddr(testAddr)),+ generateCacheKey(testUser, testPass, authPassword, ipFromAddr(otherAddr)))++ // The same credential presented by a different method must+ // produce a different key, otherwise a public key, which isn't+ // a secret, sent as a password would hit the entry made when+ // its owner logged in with it and skip the proxy.+ assert.NotEqual(t,+ generateCacheKey(testUser, testPass, authPublicKey, ipFromAddr(testAddr)),+ generateCacheKey(testUser, testPass, authPassword, ipFromAddr(testAddr)))+ assert.NotEqual(t,+ generateCacheKey(testUser, "", authAccessKey, ipFromAddr(testAddr)),+ generateCacheKey(testUser, "", authPassword, ipFromAddr(testAddr)))})t.Run("call w/Password", func(t *testing.T) {@@ -133,7 +144,7 @@ func TestRun(t *testing.T) {require.NotNil(t, entry.vfs)f := entry.vfs.Fs()require.NotNil(t, f)- cacheKey := generateCacheKey(testUser, testPass, testIP)+ cacheKey := generateCacheKey(testUser, testPass, authPassword, testIP)assert.Equal(t, "proxy-"+cacheKey, f.Name())assert.True(t, strings.HasPrefix(f.String(), "Local file system"))@@ -149,7 +160,7 @@ func TestRun(t *testing.T) {assert.Equal(t, 0, p.vfsCache.Entries())defer p.vfsCache.Clear()- cacheKey := generateCacheKey(testUser, testPass, testIP)+ cacheKey := generateCacheKey(testUser, testPass, authPassword, testIP)vfs, vfsKey, err := p.Call(testUser, testPass, false, testAddr)require.NoError(t, err)require.NotNil(t, vfs)@@ -224,7 +235,7 @@ func TestRun(t *testing.T) {vfs, vfsKey, err := p.Call(testUser, testPass, false, "")require.NoError(t, err)require.NotNil(t, vfs)- assert.Equal(t, generateCacheKey(testUser, testPass, ""), vfsKey)+ assert.Equal(t, generateCacheKey(testUser, testPass, authPassword, ""), vfsKey)assert.Equal(t, 1, p.vfsCache.Entries())})@@ -254,7 +265,7 @@ func TestRun(t *testing.T) {require.NotNil(t, entry.vfs)f := entry.vfs.Fs()require.NotNil(t, f)- cacheKey := generateCacheKey(testUser, publicKeyString, testIP)+ cacheKey := generateCacheKey(testUser, publicKeyString, authPublicKey, testIP)assert.Equal(t, "proxy-"+cacheKey, f.Name())assert.True(t, strings.HasPrefix(f.String(), "Local file system"))@@ -270,7 +281,7 @@ func TestRun(t *testing.T) {assert.Equal(t, 0, p.vfsCache.Entries())defer p.vfsCache.Clear()- cacheKey := generateCacheKey(testUser, publicKeyString, testIP)+ cacheKey := generateCacheKey(testUser, publicKeyString, authPublicKey, testIP)vfs, vfsKey, err := p.Call(testUser,publicKeyString,@@ -323,7 +334,7 @@ func TestRun(t *testing.T) {// If a cached entry's pwHash somehow doesn't match the supplied// auth (eg a hash collision on the cache key), Call must reject// it. Simulate by corrupting the cached pwHash.- entry := cacheEntry{vfs: vfs, pwHash: sha256.Sum256([]byte("tampered"))}+ entry := cacheEntry{vfs: vfs, kind: authPublicKey, pwHash: sha256.Sum256([]byte("tampered"))}p.vfsCache.Put(cacheKey, entry)vfs, vfsKey, err = p.Call(testUser, publicKeyString, true, testAddr)require.Error(t, err)@@ -331,6 +342,41 @@ func TestRun(t *testing.T) {require.Nil(t, vfs)require.Equal(t, "", vfsKey)})++ t.Run("Call w/PublicKey as Password", func(t *testing.T) {+ // A public key isn't a secret, so once its owner has logged in+ // with it anyone who knows it must not be able to send it as a+ // password from the same address and get the cached backend+ // without the proxy being asked whether that password is valid.+ assert.Equal(t, 0, p.vfsCache.Entries())+ defer p.vfsCache.Clear()++ vfs, vfsKey, err := p.Call(testUser, publicKeyString, true, testAddr)+ require.NoError(t, err)+ require.NotNil(t, vfs)+ assert.Equal(t, 1, p.vfsCache.Entries())++ // The proxy must be consulted, which shows up as a fresh cache+ // entry with a different key and backend+ vfs2, vfsKey2, err := p.Call(testUser, publicKeyString, false, testAddr)+ require.NoError(t, err)+ require.NotNil(t, vfs2)+ assert.NotEqual(t, vfsKey, vfsKey2)+ assert.Equal(t, 2, p.vfsCache.Entries())+ if vfs.Fs() == vfs2.Fs() {+ t.Error("password login got the backend created for a public key login")+ }++ // A cached entry of the wrong kind must be rejected even if+ // the cache key somehow matches (eg a hash collision).+ entry := cacheEntry{vfs: vfs2, kind: authPublicKey, pwHash: sha256.Sum256([]byte(publicKeyString))}+ p.vfsCache.Put(vfsKey2, entry)+ vfs2, vfsKey2, err = p.Call(testUser, publicKeyString, false, testAddr)+ require.Error(t, err)+ require.Contains(t, err.Error(), "incorrect password")+ require.Nil(t, vfs2)+ require.Equal(t, "", vfsKey2)+ })}// TestCallAccessKeyConcurrentRefresh checks that concurrent refreshesdiff --git a/cmd/serve/sftp/sftp_test.go b/cmd/serve/sftp/sftp_test.goindex de6c89d8e..27dbabfdd 100644--- a/cmd/serve/sftp/sftp_test.go+++ b/cmd/serve/sftp/sftp_test.go@@ -9,8 +9,12 @@ package sftpimport ("context"+ "crypto/ed25519"+ "crypto/rand"+ "encoding/base64""strings""testing"+ "time""github.com/pkg/sftp"_ "github.com/rclone/rclone/backend/local"@@ -23,6 +27,7 @@ import ("github.com/rclone/rclone/vfs/vfscommon""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ "golang.org/x/crypto/ssh")const (@@ -104,6 +109,59 @@ func TestNewServerPerServerAuthProxy(t *testing.T) {assert.Nil(t, w.provider.VFS(), "expected no fixed VFS when auth proxy is in use")}+// TestAuthProxyPublicKeyAsPassword checks that a public key which has+// just been used to log in through the auth proxy can't be sent as a+// password from the same address to get the same backend without the+// proxy being asked.+func TestAuthProxyPublicKeyAsPassword(t *testing.T) {+ // The proxy accepts any public key but refuses every password, so+ // a password login can only succeed by skipping the proxy+ t.Setenv("RCLONE_TEST_PROXY_KEY_ONLY", "1")+ proxyOpt := proxy.Opt+ proxyOpt.AuthProxy = "go run ../proxy/proxy_code.go"++ opt := Opt+ opt.ListenAddr = testBindAddress+ w, err := newServer(context.Background(), nil, &opt, &vfscommon.Opt, &proxyOpt)+ require.NoError(t, err)+ go func() {+ assert.NoError(t, w.Serve())+ }()+ defer func() {+ assert.NoError(t, w.Shutdown())+ }()+ addr := w.Addr().String()++ _, privateKey, err := ed25519.GenerateKey(rand.Reader)+ require.NoError(t, err)+ signer, err := ssh.NewSignerFromKey(privateKey)+ require.NoError(t, err)+ publicKeyString := base64.StdEncoding.EncodeToString(signer.PublicKey().Marshal())++ login := func(auth ssh.AuthMethod) error {+ client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{+ User: "victim",+ Auth: []ssh.AuthMethod{auth},+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),+ Timeout: 30 * time.Second,+ })+ if err != nil {+ return err+ }+ _ = client.Close()+ return nil+ }++ // Check the proxy refuses the public key as a password on a cold cache+ require.ErrorContains(t, login(ssh.Password(publicKeyString)), "unable to authenticate")++ // The owner of the key logs in with it+ require.NoError(t, login(ssh.PublicKeys(signer)))++ // Now the public key must still be refused as a password+ require.ErrorContains(t, login(ssh.Password(publicKeyString)), "unable to authenticate")+}+func TestRc(t *testing.T) {servetest.TestRc(t, rc.Params{"type": "sftp",