"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
local: set only the ownership a non-root user may set
Dirk Petersene906ecf25f0f
diff --git a/backend/local/local.go b/backend/local/local.goindex 67a3d9ea4..d80756300 100644--- a/backend/local/local.go+++ b/backend/local/local.go@@ -87,8 +87,10 @@ Metadata is supported on files and directories.When restoring metadata with ` + "`--metadata`" + ` rclone applies the"mode", "uid" and "gid" from the source. These come from the sourceremote which may not be trusted, so restoring metadata as root from an-untrusted source can change file ownership and is not recommended. The-setuid, setgid and sticky bits are not restored by default - see the+untrusted source can change file ownership and is not recommended. A+user other than root keeps its own uid and sets the gid only to a group+it is a member of, as rsync does. The setuid, setgid and sticky bits are+not restored by default - see the` + "`--local-metadata-restore-special-bits`" + ` flag.`,},diff --git a/backend/local/metadata.go b/backend/local/metadata.goindex bdbcad1f9..58384ecd5 100644--- a/backend/local/metadata.go+++ b/backend/local/metadata.go@@ -5,7 +5,9 @@ import ("math""os""runtime"+ "slices""strconv"+ "sync""time""github.com/rclone/rclone/fs"@@ -121,13 +123,15 @@ func (o *Object) writeMetadataToFile(m fs.Metadata) (outErr error) {uid, hasUID := o.parseMetadataInt(m, "uid", 10)gid, hasGID := o.parseMetadataInt(m, "gid", 10)if hasUID {- // FIXME should read UID and GID of current user and only attempt to set it if differentif !hasGID {gid = uid}if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {fs.Debugf(o, "Ignoring request to set ownership %o.%o on this OS", gid, uid)+ } else if setUID, setGID := ownerToSet(uid, gid); setUID < 0 && setGID < 0 {+ fs.Debugf(o, "Not setting ownership %d.%d: only root can", uid, gid)} else {+ uid, gid = setUID, setGIDif o.translatedLink {err = os.Lchown(o.path, uid, gid)} else {@@ -169,3 +173,27 @@ func (o *Object) writeMetadataToFile(m fs.Metadata) (outErr error) {// FIXME not parsing rdev yetreturn outErr}++// processGroups are the supplementary groups of this process+var processGroups = sync.OnceValue(func() []int {+ groups, _ := os.Getgroups()+ return groups+})++// ownerToSet returns the uid and gid to pass to chown for the wanted+// ownership: unchanged for root, else -1 (leave it as it is) for a uid+// other than our own and for a group we are not a member of, which+// only root may set.+func ownerToSet(uid, gid int) (setUID, setGID int) {+ euid := os.Geteuid()+ if euid <= 0 {+ return uid, gid+ }+ if uid != euid {+ uid = -1+ }+ if gid != os.Getegid() && !slices.Contains(processGroups(), gid) {+ gid = -1+ }+ return uid, gid+}diff --git a/backend/local/metadata_owner_unix_test.go b/backend/local/metadata_owner_unix_test.gonew file mode 100644index 000000000..4cc184a1b--- /dev/null+++ b/backend/local/metadata_owner_unix_test.go@@ -0,0 +1,50 @@+//go:build !windows && !plan9 && !js++package local++import (+ "context"+ "os"+ "path/filepath"+ "slices"+ "syscall"+ "testing"+ "time"++ "github.com/rclone/rclone/fs"+ "github.com/rclone/rclone/fstest"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func TestMetadataOwnershipAsNonRoot(t *testing.T) {+ if os.Geteuid() == 0 {+ t.Skip("needs a user other than root")+ }+ uid, gid := os.Geteuid(), os.Getegid()+ other := uid + 1+ notMember := gid + 1+ for slices.Contains(processGroups(), notMember) {+ notMember+++ }++ // only what a non-root user may set: its own uid, its groups+ setUID, setGID := ownerToSet(other, notMember)+ assert.Equal(t, [2]int{-1, -1}, [2]int{setUID, setGID})+ setUID, setGID = ownerToSet(other, gid)+ assert.Equal(t, [2]int{-1, gid}, [2]int{setUID, setGID})+ setUID, setGID = ownerToSet(uid, notMember)+ assert.Equal(t, [2]int{uid, -1}, [2]int{setUID, setGID})++ // a file of another user (e.g. root:root) is copied with our ownership+ // instead of failing with "operation not permitted"+ r := fstest.NewRun(t)+ r.WriteFile("owned.txt", "payload", time.Now())+ f := r.Flocal.(*Fs)+ obj, err := f.NewObject(context.Background(), "owned.txt")+ require.NoError(t, err)+ require.NoError(t, obj.(*Object).writeMetadataToFile(fs.Metadata{"uid": "0", "gid": "0"}))+ fi, err := os.Stat(filepath.Join(f.root, "owned.txt"))+ require.NoError(t, err)+ assert.Equal(t, uint32(uid), fi.Sys().(*syscall.Stat_t).Uid)+}