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 s3: stop auth proxy users using each other's multipart uploads and metadata GHSA-35g6-9fjv-rmx5 CVE-PENDING

Nick Craig-Wooda16a901de76a
diff --git a/cmd/serve/s3/backend.go b/cmd/serve/s3/backend.go
index c4e959a96..e38d65124 100644
--- a/cmd/serve/s3/backend.go
+++ b/cmd/serve/s3/backend.go
@@ -113,7 +113,7 @@ func newBackend(s *Server) *s3Backend {
// tenant returns the state of the user making the request in ctx.
func (b *s3Backend) tenant(ctx context.Context) *tenant {
- id := ""
+ id := b.s.getTenant(ctx)
b.tenantsMu.Lock()
defer b.tenantsMu.Unlock()
t, ok := b.tenants[id]
diff --git a/cmd/serve/s3/backend_test.go b/cmd/serve/s3/backend_test.go
index d93763204..78d74d958 100644
--- a/cmd/serve/s3/backend_test.go
+++ b/cmd/serve/s3/backend_test.go
@@ -235,3 +235,39 @@ func TestCopyObjectMissingSource(t *testing.T) {
_, err := b.HeadObject(ctx, "bucket", "other.txt")
assert.True(t, gofakes3.HasErrorCode(err, gofakes3.ErrNoSuchKey))
}
+
+// tenantCtx returns the context of a request authenticated by the auth
+// proxy as accessKeyID.
+func tenantCtx(accessKeyID string) context.Context {
+ return context.WithValue(context.Background(), ctxKeyAccessKeyID, accessKeyID)
+}
+
+// TestMetadataOtherTenant checks that with an auth proxy the metadata
+// one user (access key ID) stores for an object isn't returned for
+// another user's object at the same path.
+func TestMetadataOtherTenant(t *testing.T) {
+ b, _ := newTestBackend(t)
+ ctxA, ctxB := tenantCtx("tenantA"), tenantCtx("tenantB")
+ const secretKey = "X-Amz-Meta-Secret"
+
+ put := func(ctx context.Context, meta map[string]string) {
+ _, err := b.PutObject(ctx, "bucket", "meta.txt", meta, strings.NewReader("data"), 4)
+ require.NoError(t, err)
+ }
+ head := func(ctx context.Context) map[string]string {
+ obj, err := b.HeadObject(ctx, "bucket", "meta.txt")
+ require.NoError(t, err)
+ return obj.Metadata
+ }
+
+ put(ctxB, map[string]string{})
+ put(ctxA, map[string]string{secretKey: "tenantA's"})
+
+ assert.Equal(t, "tenantA's", head(ctxA)[secretKey])
+ assert.NotContains(t, head(ctxB), secretKey)
+
+ obj, err := b.GetObject(ctxB, "bucket", "meta.txt", nil)
+ require.NoError(t, err)
+ require.NoError(t, obj.Contents.Close())
+ assert.NotContains(t, obj.Metadata, secretKey)
+}
diff --git a/cmd/serve/s3/multipart_test.go b/cmd/serve/s3/multipart_test.go
index 3085aac1f..edd344fc3 100644
--- a/cmd/serve/s3/multipart_test.go
+++ b/cmd/serve/s3/multipart_test.go
@@ -631,6 +631,34 @@ func TestMultipartCompleteRenameFailureKeepsUpload(t *testing.T) {
require.NoError(t, err, "the upload record must survive a retryable Complete failure")
}
+// TestMultipartOtherTenant checks that with an auth proxy one user
+// (access key ID) can't upload parts to, complete or abort another
+// user's multipart upload even if it knows the upload ID.
+func TestMultipartOtherTenant(t *testing.T) {
+ b, f, bucket := newPutTestBackend(t, "", nil)
+ ctxA, ctxB := tenantCtx("tenantA"), tenantCtx("tenantB")
+ const object = "victim.bin"
+
+ uploadID, err := b.CreateMultipartUpload(ctxA, bucket, object, nil)
+ require.NoError(t, err)
+
+ data := []byte(random.String(1024))
+ _, err = b.UploadPart(ctxB, bucket, object, uploadID, 1, int64(len(data)), bytes.NewReader(data))
+ assert.ErrorIs(t, err, gofakes3.ErrNoSuchUpload, "UploadPart by another user")
+ require.ErrorIs(t, b.AbortMultipartUpload(ctxB, bucket, object, uploadID), gofakes3.ErrNoSuchUpload, "Abort by another user")
+
+ etag, err := b.UploadPart(ctxA, bucket, object, uploadID, 1, int64(len(data)), bytes.NewReader(data))
+ require.NoError(t, err)
+ input := &gofakes3.CompleteMultipartUploadRequest{Parts: []gofakes3.CompletedPart{{PartNumber: 1, ETag: etag}}}
+ _, _, err = b.CompleteMultipartUpload(ctxB, bucket, object, uploadID, input)
+ assert.ErrorIs(t, err, gofakes3.ErrNoSuchUpload, "Complete by another user")
+
+ // The owner's upload is untouched by the other user's attempts.
+ _, _, err = b.CompleteMultipartUpload(ctxA, bucket, object, uploadID, input)
+ require.NoError(t, err)
+ assert.Equal(t, data, readObject(t, f, bucket, object))
+}
+
// TestMultipartReaper checks that an incomplete multipart upload abandoned
// by its client is aborted and cleaned up after --multipart-expiry, and that
// late operations on it fail with NoSuchUpload.
diff --git a/cmd/serve/s3/s3_test.go b/cmd/serve/s3/s3_test.go
index 28c2ae7a2..6bca830e5 100644
--- a/cmd/serve/s3/s3_test.go
+++ b/cmd/serve/s3/s3_test.go
@@ -10,9 +10,11 @@ import (
"io"
"net/http"
"net/url"
+ "os"
"path"
"path/filepath"
"slices"
+ "strings"
"testing"
"time"
@@ -430,6 +432,77 @@ func TestAuthProxyKeysNotRegistered(t *testing.T) {
assert.NotEqual(t, signature.ErrNone, signature.V4SignVerify(sign()), "proxy secret was registered in the gofakes3 key store")
}
+// serveS3AuthProxy serves root over s3 with an auth proxy which maps
+// every one of users (access key IDs) to root, returning a client for
+// each user.
+func serveS3AuthProxy(t *testing.T, root string, users ...string) map[string]*minio.Core {
+ fstest.Initialise()
+ prog, err := filepath.Abs("../servetest/proxy_code.go")
+ require.NoError(t, err)
+ secrets := map[string]string{}
+ var pairs []string
+ for _, user := range users {
+ secrets[user] = random.String(16)
+ pairs = append(pairs, user+","+secrets[user])
+ }
+ t.Setenv("RCLONE_TEST_PROXY_AUTH_KEY", strings.Join(pairs, ";"))
+
+ opt := Opt
+ opt.HTTP.ListenAddr = []string{endpoint}
+ proxyOpt := proxy.Opt
+ proxyOpt.AuthProxy = "go run " + prog + " " + root
+ w, err := newServer(context.Background(), nil, &opt, &vfscommon.Opt, &proxyOpt)
+ require.NoError(t, err)
+ go func() {
+ require.NoError(t, w.Serve())
+ }()
+ t.Cleanup(func() { _ = w.Shutdown() })
+ testURL, err := url.Parse(w.server.URLs()[0])
+ require.NoError(t, err)
+
+ clients := map[string]*minio.Core{}
+ for _, user := range users {
+ clients[user], err = minio.NewCore(testURL.Host, &minio.Options{
+ Creds: credentials.NewStaticV4(user, secrets[user], ""),
+ })
+ require.NoError(t, err)
+ }
+ return clients
+}
+
+// TestAuthProxyUsersKeptApart checks that one auth proxy user can't use
+// another user's multipart upload or see the metadata of their objects,
+// even when both are mapped to the same backend.
+func TestAuthProxyUsersKeptApart(t *testing.T) {
+ ctx := context.Background()
+ root := t.TempDir()
+ const bucket = "bucket"
+ require.NoError(t, os.Mkdir(filepath.Join(root, bucket), 0777))
+ clients := serveS3AuthProxy(t, root, "alice", "bob")
+ alice, bob := clients["alice"], clients["bob"]
+
+ uploadID, err := alice.NewMultipartUpload(ctx, bucket, "upload", minio.PutObjectOptions{})
+ require.NoError(t, err)
+ data := []byte("alice's data")
+ _, err = bob.PutObjectPart(ctx, bucket, "upload", uploadID, 1, bytes.NewReader(data), int64(len(data)), minio.PutObjectPartOptions{})
+ assert.Equal(t, "NoSuchUpload", minio.ToErrorResponse(err).Code, "bob uploading a part to alice's upload")
+ err = bob.AbortMultipartUpload(ctx, bucket, "upload", uploadID)
+ assert.Equal(t, "NoSuchUpload", minio.ToErrorResponse(err).Code, "bob aborting alice's upload")
+ part, err := alice.PutObjectPart(ctx, bucket, "upload", uploadID, 1, bytes.NewReader(data), int64(len(data)), minio.PutObjectPartOptions{})
+ require.NoError(t, err)
+ _, err = alice.CompleteMultipartUpload(ctx, bucket, "upload", uploadID, []minio.CompletePart{{PartNumber: 1, ETag: part.ETag}}, minio.PutObjectOptions{})
+ require.NoError(t, err)
+
+ _, err = alice.PutObject(ctx, bucket, "meta", bytes.NewReader(data), int64(len(data)), "", "", minio.PutObjectOptions{UserMetadata: map[string]string{"Secret": "alice's"}})
+ require.NoError(t, err)
+ info, err := alice.StatObject(ctx, bucket, "meta", minio.StatObjectOptions{})
+ require.NoError(t, err)
+ assert.Equal(t, "alice's", info.UserMetadata["Secret"])
+ info, err = bob.StatObject(ctx, bucket, "meta", minio.StatObjectOptions{})
+ require.NoError(t, err)
+ assert.NotContains(t, info.UserMetadata, "Secret", "bob seeing alice's metadata")
+}
+
// TestAuthKeyPerServer checks that two servers in the same process
// with different --auth-key pairs only accept their own credentials.
func TestAuthKeyPerServer(t *testing.T) {
diff --git a/cmd/serve/s3/server.go b/cmd/serve/s3/server.go
index 640838733..0b48c3a2e 100644
--- a/cmd/serve/s3/server.go
+++ b/cmd/serve/s3/server.go
@@ -25,6 +25,7 @@ type ctxKey int
const (
ctxKeyID ctxKey = iota
+ ctxKeyAccessKeyID
)
// Server is a s3.FileSystem interface
@@ -154,6 +155,19 @@ func (w *Server) etagHash(_vfs *vfs.VFS) hash.Type {
return w.etagHashType
}
+// getTenant returns the ID of the user making the request in ctx: the
+// access key ID the auth proxy authenticated the request with, or "" when
+// not using an auth proxy.
+//
+// The proxy may map each access key ID to a different backend, so state
+// kept across requests must be scoped to it. The *vfs.VFS can't be used
+// for this as the proxy may hand the same access key ID different ones,
+// for example from different client IPs or after its cache expires.
+func (w *Server) getTenant(ctx context.Context) string {
+ accessKeyID, _ := ctx.Value(ctxKeyAccessKeyID).(string)
+ return accessKeyID
+}
+
// auth authenticates the request via the auth proxy.
//
// The proxy maps the access key ID to a VFS and a secret access key
@@ -234,8 +248,9 @@ func proxyAuthMiddleware(next http.Handler, ws *Server) http.Handler {
accessDenied(w)
return
}
- r = r.WithContext(context.WithValue(r.Context(), ctxKeyID, VFS))
- next.ServeHTTP(w, r)
+ ctx := context.WithValue(r.Context(), ctxKeyID, VFS)
+ ctx = context.WithValue(ctx, ctxKeyAccessKeyID, accessKey)
+ next.ServeHTTP(w, r.WithContext(ctx))
})
}
diff --git a/cmd/serve/servetest/proxy_code.go b/cmd/serve/servetest/proxy_code.go
index 10e9a3f98..c35d9c05c 100644
--- a/cmd/serve/servetest/proxy_code.go
+++ b/cmd/serve/servetest/proxy_code.go
@@ -3,9 +3,9 @@
// A simple auth proxy for testing purposes
//
// For S3 access key auth (no "pass" or "public_key" in the input) the
-// access key ID and secret are read from the environment variable
-// RCLONE_TEST_PROXY_AUTH_KEY as "accessKeyID,secretAccessKey" and any
-// other access key ID is refused.
+// access key IDs and secrets are read from the environment variable
+// RCLONE_TEST_PROXY_AUTH_KEY as "accessKeyID,secretAccessKey" pairs
+// separated by ";" and any other access key ID is refused.
package main
import (
@@ -38,14 +38,14 @@ func main() {
_, havePass := in["pass"]
_, havePublicKey := in["public_key"]
if !havePass && !havePublicKey {
- accessKeyID, secret, ok := strings.Cut(os.Getenv("RCLONE_TEST_PROXY_AUTH_KEY"), ",")
- if !ok {
- log.Fatal("RCLONE_TEST_PROXY_AUTH_KEY not set")
+ for pair := range strings.SplitSeq(os.Getenv("RCLONE_TEST_PROXY_AUTH_KEY"), ";") {
+ if accessKeyID, secret, ok := strings.Cut(pair, ","); ok && in["user"] == accessKeyID {
+ out["_secret_access_key"] = secret
+ }
}
- if in["user"] != accessKeyID {
+ if out["_secret_access_key"] == "" {
log.Fatalf("unknown access key ID %q", in["user"])
}
- out["_secret_access_key"] = secret
}
json.NewEncoder(os.Stdout).Encode(&out)
if err != nil {