Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve sftp: fix auth proxy running for public keys the client can't use GHSA-66qv-66mg-9h74 CVE-PENDING

Nick Craig-Wood866e7ac577aa
diff --git a/cmd/serve/proxy/proxy_code.go b/cmd/serve/proxy/proxy_code.go
index ec53bdff6..5fcc1c86a 100644
--- a/cmd/serve/proxy/proxy_code.go
+++ b/cmd/serve/proxy/proxy_code.go
@@ -40,6 +40,10 @@ func main() {
if os.Getenv("RCLONE_TEST_PROXY_KEY_ONLY") != "" {
log.Fatalf("password refused for %q", in["user"])
}
+ case "public_key":
+ if os.Getenv("RCLONE_TEST_PROXY_REFUSE_KEYS") != "" {
+ log.Fatalf("public key refused for %q", in["user"])
+ }
}
out[k] = v
}
diff --git a/cmd/serve/sftp/server.go b/cmd/serve/sftp/server.go
index 81f3790e4..e1098089b 100644
--- a/cmd/serve/sftp/server.go
+++ b/cmd/serve/sftp/server.go
@@ -196,6 +196,24 @@ func (s *server) configure() (err error) {
},
PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
fs.Debugf(describeConn(c), "Public key login attempt for %s", c.User())
+ if s.provider.IsProxy() {
+ // This is also called for keys offered without a
+ // signature, so leave asking the proxy until
+ // VerifiedPublicKeyCallback when the client has proved
+ // it holds the private key.
+ return &ssh.Permissions{}, nil
+ }
+ if _, ok := authorizedKeysMap[string(pubKey.Marshal())]; ok {
+ return &ssh.Permissions{
+ // Record the public key used for authentication.
+ Extensions: map[string]string{
+ "pubkey-fp": ssh.FingerprintSHA256(pubKey),
+ },
+ }, nil
+ }
+ return nil, fmt.Errorf("unknown public key for %q", c.User())
+ },
+ VerifiedPublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey, perms *ssh.Permissions, _ string) (*ssh.Permissions, error) {
if s.provider.IsProxy() {
//query the proxy for the config
_, vfsKey, err := s.provider.Proxy().Call(
@@ -214,15 +232,7 @@ func (s *server) configure() (err error) {
},
}, nil
}
- if _, ok := authorizedKeysMap[string(pubKey.Marshal())]; ok {
- return &ssh.Permissions{
- // Record the public key used for authentication.
- Extensions: map[string]string{
- "pubkey-fp": ssh.FingerprintSHA256(pubKey),
- },
- }, nil
- }
- return nil, fmt.Errorf("unknown public key for %q", c.User())
+ return perms, nil
},
AuthLogCallback: func(conn ssh.ConnMetadata, method string, err error) {
status := "OK"
diff --git a/cmd/serve/sftp/sftp_test.go b/cmd/serve/sftp/sftp_test.go
index 27dbabfdd..1c40ae79f 100644
--- a/cmd/serve/sftp/sftp_test.go
+++ b/cmd/serve/sftp/sftp_test.go
@@ -12,6 +12,10 @@ import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
+ "errors"
+ "io"
+ "os"
+ "path/filepath"
"strings"
"testing"
"time"
@@ -162,6 +166,137 @@ func TestAuthProxyPublicKeyAsPassword(t *testing.T) {
require.ErrorContains(t, login(ssh.Password(publicKeyString)), "unable to authenticate")
}
+// querySigner offers a public key without being able to sign with it,
+// so the client only asks the server whether the key is acceptable.
+type querySigner struct {
+ key ssh.PublicKey
+}
+
+func (s querySigner) PublicKey() ssh.PublicKey {
+ return s.key
+}
+
+func (s querySigner) Sign(io.Reader, []byte) (*ssh.Signature, error) {
+ return nil, errors.New("no private key")
+}
+
+// TestAuthProxyPublicKeyQuery checks that offering a public key without
+// proving possession of its private key doesn't run the auth proxy or
+// log in.
+func TestAuthProxyPublicKeyQuery(t *testing.T) {
+ runLog := filepath.Join(t.TempDir(), "proxy-runs")
+ t.Setenv("RCLONE_TEST_PROXY_RUN_LOG", runLog)
+ 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)
+
+ 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
+ }
+ proxyRuns := func() int {
+ data, err := os.ReadFile(runLog)
+ if os.IsNotExist(err) {
+ return 0
+ }
+ require.NoError(t, err)
+ return strings.Count(string(data), "\n")
+ }
+
+ // Offering the key without signing must not run the proxy
+ require.Error(t, login(ssh.PublicKeys(querySigner{signer.PublicKey()})))
+ assert.Equal(t, 0, proxyRuns())
+
+ // The owner of the key can still log in with it
+ require.NoError(t, login(ssh.PublicKeys(signer)))
+ assert.Equal(t, 1, proxyRuns())
+
+ // A key the client signs with must still be refused if the proxy
+ // refuses it
+ t.Setenv("RCLONE_TEST_PROXY_REFUSE_KEYS", "1")
+ _, otherPrivateKey, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ otherSigner, err := ssh.NewSignerFromKey(otherPrivateKey)
+ require.NoError(t, err)
+ require.ErrorContains(t, login(ssh.PublicKeys(otherSigner)), "unable to authenticate")
+ assert.Equal(t, 2, proxyRuns())
+}
+
+// TestAuthorizedKeys checks that only keys in --authorized-keys can log
+// in, and only when the client signs with the private key.
+func TestAuthorizedKeys(t *testing.T) {
+ newSigner := func() ssh.Signer {
+ _, privateKey, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ signer, err := ssh.NewSignerFromKey(privateKey)
+ require.NoError(t, err)
+ return signer
+ }
+ signer := newSigner()
+ otherSigner := newSigner()
+
+ authKeysFile := filepath.Join(t.TempDir(), "authorized_keys")
+ require.NoError(t, os.WriteFile(authKeysFile, ssh.MarshalAuthorizedKey(signer.PublicKey()), 0600))
+
+ f, err := fs.NewFs(context.Background(), t.TempDir())
+ require.NoError(t, err)
+ opt := Opt
+ opt.ListenAddr = testBindAddress
+ opt.AuthorizedKeys = authKeysFile
+ w, err := newServer(context.Background(), f, &opt, &vfscommon.Opt, &proxy.Opt)
+ require.NoError(t, err)
+ go func() {
+ assert.NoError(t, w.Serve())
+ }()
+ defer func() {
+ assert.NoError(t, w.Shutdown())
+ }()
+ addr := w.Addr().String()
+
+ login := func(auth ssh.AuthMethod) error {
+ client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
+ User: testUser,
+ Auth: []ssh.AuthMethod{auth},
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ Timeout: 30 * time.Second,
+ })
+ if err != nil {
+ return err
+ }
+ _ = client.Close()
+ return nil
+ }
+
+ require.NoError(t, login(ssh.PublicKeys(signer)))
+ require.ErrorContains(t, login(ssh.PublicKeys(otherSigner)), "unable to authenticate")
+ require.Error(t, login(ssh.PublicKeys(querySigner{signer.PublicKey()})))
+}
+
func TestRc(t *testing.T) {
servetest.TestRc(t, rc.Params{
"type": "sftp",