Compare commits

...
Author SHA1 Message Date
Chris LuandClaude Opus 5.5 9ba1839547 sftp: answer permission denials with PERMISSION_DENIED
weed sftp refused requests with a bare os.ErrPermission. pkg/sftp maps
only syscall errors to SSH_FX_PERMISSION_DENIED, so clients got
SSH_FX_FAILURE with the text "permission denied" and could not tell a
refusal from a transient failure. Return an error that is still
os.ErrPermission inside the server and carries the SFTP status for the
client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-06 00:52:33 -08:00
3 changed files with 66 additions and 10 deletions

No files matched your search

+6 -6
View File
@@ -449,7 +449,7 @@ func (fs *SftpServer) checkRenameTargetDelete(absTarget, targetDir string) (requ
if matchingAclPrefix(fs.user, absTarget) != matchingAclPrefix(fs.user, targetDir) {
if !HasExplicitPermission(fs.user, absTarget, PermDelete, false) {
glog.V(0).Infof("permission denied for user %s: rename target %s carries a delete-protected ACL", fs.user.Username, absTarget)
return false, os.ErrPermission
return false, errPermissionDenied
}
return false, nil
}
@@ -535,7 +535,7 @@ func (fs *SftpServer) checkRenameKeepsAcl(absPath, absTarget string) error {
for _, perm := range []string{PermRead, PermList, PermWrite, PermDelete} {
if !HasExplicitPermission(fs.user, pair.src, perm, isDir) && HasExplicitPermission(fs.user, pair.dst, perm, isDir) {
glog.V(0).Infof("permission denied for user %s: rename of %s to %s would drop the ACL on %s", fs.user.Username, absPath, absTarget, pair.src)
return os.ErrPermission
return errPermissionDenied
}
}
}
@@ -910,7 +910,7 @@ func (fs *SftpServer) deleteEntry(p string, recursive bool) error {
func (fs *SftpServer) checkEntryProtected(p string, entry *filer_pb.Entry) error {
if string(entry.Extended[s3_constants.ExtDeleteMarkerKey]) != "true" && s3_objectlock.EntryHasActiveLock(entry, time.Now()) {
glog.V(0).Infof("permission denied for user %s: %s is under Object Lock", fs.user.Username, p)
return os.ErrPermission
return errPermissionDenied
}
if entry.WormEnforcedAtTsNs == 0 {
return nil
@@ -921,7 +921,7 @@ func (fs *SftpServer) checkEntryProtected(p string, entry *filer_pb.Entry) error
}
if fc.WormEnforced(p, entry.WormEnforcedAtTsNs) {
glog.V(0).Infof("permission denied for user %s: %s is write-once", fs.user.Username, p)
return os.ErrPermission
return errPermissionDenied
}
return nil
}
@@ -935,7 +935,7 @@ func (fs *SftpServer) checkSubtreeProtected(dir string) error {
}
if fc.IntersectsWorm(dir) {
glog.V(0).Infof("permission denied for user %s: %s intersects a WORM rule", fs.user.Username, dir)
return os.ErrPermission
return errPermissionDenied
}
var locked bool
if err := fs.callWithClient(true, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
@@ -946,7 +946,7 @@ func (fs *SftpServer) checkSubtreeProtected(dir string) error {
}
if locked {
glog.V(0).Infof("permission denied for user %s: %s holds objects under Object Lock", fs.user.Username, dir)
return os.ErrPermission
return errPermissionDenied
}
return nil
}
+18 -4
View File
@@ -6,6 +6,7 @@ import (
stdpath "path"
"strings"
"github.com/pkg/sftp"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
@@ -25,6 +26,19 @@ const (
PermReadWrite = "readwrite"
)
// errPermissionDenied is os.ErrPermission that clients also see as
// SSH_FX_PERMISSION_DENIED. pkg/sftp maps only syscall errors to that status
// and answers a bare os.ErrPermission with SSH_FX_FAILURE.
var errPermissionDenied error = permissionDenied{}
type permissionDenied struct{}
func (permissionDenied) Error() string { return os.ErrPermission.Error() }
func (permissionDenied) Unwrap() []error {
return []error{os.ErrPermission, sftp.ErrSSHFxPermissionDenied}
}
// Entry represents a filesystem entry with attributes
type Entry struct {
IsDirectory bool
@@ -57,7 +71,7 @@ func (fs *SftpServer) CheckFilePermission(path string, perm string) error {
if fs.user == nil {
glog.V(0).Infof("permission denied. No user associated with the SftpServer.")
return os.ErrPermission
return errPermissionDenied
}
// Special case for "create" or "write" permissions on non-existent paths
@@ -69,7 +83,7 @@ func (fs *SftpServer) CheckFilePermission(path string, perm string) error {
if err == os.ErrNotExist {
parentPath := stdpath.Dir(path)
if parentPath == path {
return os.ErrPermission
return errPermissionDenied
}
// Check if user can write to the parent directory
return fs.CheckFilePermission(parentPath, perm)
@@ -84,7 +98,7 @@ func (fs *SftpServer) CheckFilePermission(path string, perm string) error {
func (fs *SftpServer) checkEntryPermission(path string, entry *filer_pb.Entry, perm string) error {
if fs.user == nil {
glog.V(0).Infof("permission denied. No user associated with the SftpServer.")
return os.ErrPermission
return errPermissionDenied
}
var err error
@@ -124,7 +138,7 @@ func (fs *SftpServer) checkEntryPermission(path string, entry *filer_pb.Entry, p
}
}
glog.V(0).Infof("permission denied for user %s on path %s for permission %s", fs.user.Username, path, perm)
return os.ErrPermission
return errPermissionDenied
}
// pathWithin reports whether candidate is base itself or a descendant of base,
+42
View File
@@ -822,3 +822,45 @@ func TestReadSymlinkFails(t *testing.T) {
t.Fatalf("reading a symlink: got %v, want OP_UNSUPPORTED", err)
}
}
// Clients such as rclone tell a refusal from a transient failure by the
// status code, so every permission denial has to reach them as
// PERMISSION_DENIED.
func TestPermissionDenialsAnswerPermissionDenied(t *testing.T) {
for _, c := range []struct {
name string
perms string
op func(*sftp.Client) error
}{
{"remove", PermRead, func(c *sftp.Client) error { return c.Remove("/b/rw.txt") }},
{"mkdir", PermRead, func(c *sftp.Client) error { return c.Mkdir("/b/dir") }},
{"rename", PermRead, func(c *sftp.Client) error { return c.Rename("/b/rw.txt", "/b/moved.txt") }},
{"chmod", PermRead, func(c *sftp.Client) error { return c.Chmod("/b/rw.txt", 0o600) }},
{"create", PermRead, func(c *sftp.Client) error {
_, err := c.OpenFile("/b/new.txt", os.O_WRONLY|os.O_CREATE)
return err
}},
{"open for read", PermWrite, func(c *sftp.Client) error {
_, err := c.Open("/b/rw.txt")
return err
}},
{"read on a read-write handle", PermWrite, func(c *sftp.Client) error {
f, err := c.OpenFile("/b/rw.txt", os.O_RDWR)
if err != nil {
return err
}
defer f.Close()
_, err = f.ReadAt(make([]byte, 2), 0)
return err
}},
} {
t.Run(c.name, func(t *testing.T) {
rw := newRWTest(t, c.perms)
// The client reports PERMISSION_DENIED as os.ErrPermission and
// any other status as a *sftp.StatusError.
if err := c.op(rw.client); !errors.Is(err, os.ErrPermission) {
t.Fatalf("got %#v, want PERMISSION_DENIED", err)
}
})
}
}