mount: decide chown's set-id clearing by the caller, keep it on directories (#12955)

* mount: decide chown's set-id clearing by the caller, keep it on directories

SetAttr read input.Uid as the caller, but in SetAttrIn that field is the new
owner; the caller is input.Caller.Uid. Root giving a file away cleared its
SUID/SGID bits, a user giving a file to root kept them, and every chown to a
non-root owner cleared the SGID bit of a directory, which Linux keeps so new
entries go on inheriting the group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: let Linux decide chown's set-id clearing

The mount does not handle killpriv, so Linux sends the cleared mode with a
chown that must drop the set-id bits. Clearing them again by caller dropped
the SGID bit Linux keeps on a file without group execute when a user changes
only its group. Other systems keep the caller rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-24 12:36:05 -08:00
1 parent ae60559da9
commit 488bdafbbb
2 files changed
+84 -3

No files matched your search

+12 -3
View File
@@ -3,6 +3,7 @@ package mount
import (
"context"
"os"
"runtime"
"syscall"
"time"
@@ -223,7 +224,7 @@ func retainSetAttr(input *fuse.SetAttrIn, live, acked *filer_pb.Entry) {
if gidSet {
live.Attributes.Gid = acked.Attributes.Gid
}
if (uidSet || gidSet) && input.Uid != 0 {
if (uidSet || gidSet) && chownKillsSetId(runtime.GOOS, input.Caller.Uid, live.IsDirectory) {
live.Attributes.FileMode &^= 0o6000
}
if _, ok := input.GetMTime(); ok {
@@ -232,6 +233,15 @@ func retainSetAttr(input *fuse.SetAttrIn, live, acked *filer_pb.Entry) {
live.Attributes.Ctime, live.Attributes.CtimeNs = acked.Attributes.Ctime, acked.Attributes.CtimeNs
}
// chownKillsSetId reports whether the mount clears the SUID and SGID bits on a
// chown. Linux sends the cleared mode with the chown itself, since the mount
// does not handle killpriv. Elsewhere a caller other than root clears them,
// except on a directory. input.Uid is the new owner; the caller is
// input.Caller.Uid.
func chownKillsSetId(goos string, callerUid uint32, isDirectory bool) bool {
return goos != "linux" && callerUid != 0 && !isDirectory
}
func (wfs *WFS) applySetAttr(path util.FullPath, input *fuse.SetAttrIn, entry *filer_pb.Entry) fuse.Status {
wormEnforced, wormEnabled := wfs.wormEnforcedForEntry(path, entry)
if wormEnforced {
@@ -267,8 +277,7 @@ func (wfs *WFS) applySetAttr(path util.FullPath, input *fuse.SetAttrIn, entry *f
}
}
// POSIX: clear SUID/SGID bits when ownership changes (unless caller is root).
if ownerChanged && input.Uid != 0 {
if ownerChanged && chownKillsSetId(runtime.GOOS, input.Caller.Uid, entry.IsDirectory) {
entry.Attributes.FileMode &^= 0o6000
}
+72
View File
@@ -0,0 +1,72 @@
package mount
import (
"runtime"
"testing"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
func TestChownKillsSetId(t *testing.T) {
for _, tc := range []struct {
goos string
callerUid uint32
isDirectory bool
want bool
}{
{"linux", 1000, false, false},
{"linux", 0, false, false},
{"darwin", 1000, false, true},
{"darwin", 0, false, false},
{"darwin", 1000, true, false},
{"freebsd", 1000, false, true},
} {
if got := chownKillsSetId(tc.goos, tc.callerUid, tc.isDirectory); got != tc.want {
t.Errorf("chownKillsSetId(%q, %d, %v) = %v, want %v", tc.goos, tc.callerUid, tc.isDirectory, got, tc.want)
}
}
}
// On Linux the kernel sends the cleared mode with a chown that must drop the
// set-id bits; an ownership-only request keeps them.
func TestChownKeepsSetIdBitsTheKernelKeeps(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("the kernel clears the set-id bits on Linux")
}
for _, tc := range []struct {
name string
caller uint32
valid uint32
mode uint32
directory bool
}{
{"root gives a file away", 0, fuse.FATTR_UID | fuse.FATTR_GID, 0o6644, false},
{"user changes the group of a file without group execute", 1000, fuse.FATTR_GID, 0o2644, false},
{"root gives a directory away", 0, fuse.FATTR_UID | fuse.FATTR_GID, 0o6755, true},
{"user changes a directory's group", 1000, fuse.FATTR_GID, 0o2775, true},
} {
t.Run(tc.name, func(t *testing.T) {
wfs := &WFS{option: &Option{}}
entry := &filer_pb.Entry{Name: "x", IsDirectory: tc.directory, Attributes: &filer_pb.FuseAttributes{FileMode: tc.mode, Uid: 1000, Gid: 1000}}
in := &fuse.SetAttrIn{}
in.NodeId = 2
in.Caller.Uid = tc.caller
in.Valid = tc.valid
in.Owner = fuse.Owner{Uid: 2000, Gid: 2000}
if status := wfs.applySetAttr("/x", in, entry); status != fuse.OK {
t.Fatalf("applySetAttr: %v", status)
}
if got := entry.Attributes.FileMode; got != tc.mode {
t.Fatalf("mode after chown = %o, want %o", got, tc.mode)
}
live := &filer_pb.Entry{Name: "x", IsDirectory: tc.directory, Attributes: &filer_pb.FuseAttributes{FileMode: tc.mode}}
retainSetAttr(in, live, entry)
if got := live.Attributes.FileMode; got != tc.mode {
t.Fatalf("retained mode after chown = %o, want %o", got, tc.mode)
}
})
}
}