feat: add protected SMB disk-root sharing
This commit is contained in:
18 files changed
+480
-21
No files matched your search
@@ -14,3 +14,5 @@ This is a maintained local adaptation, not an unmodified dependency. RouterShelf
|
||||
Review upstream changes and security issues before updating. Reapply/review each adaptation and run `make test`, race checks, fuzz checks and both builds. Require native Finder and target validation before promoting support claims. This young implementation has not received a RouterShelf independent security audit; it is for private trusted clients, never Internet exposure. Its internal API is not a supported public RouterShelf library.
|
||||
|
||||
The initial 16-credit adaptation stalled the native macOS client before mkdir; 128 credits passed the local native smoke check. Credits bound the request window, while the synchronous parser and frame/I/O limits independently bound allocations. Preserve this native-client regression check when tuning credits.
|
||||
|
||||
RouterShelf also adds `guarded_root.go` and its tests for explicit disk-root views: protected relative subtrees are hidden/blocked, aliases refused, protected ancestors cannot be renamed/deleted, and parent descriptors are pinned with os.Root. Cross-directory rename and descriptor timestamp operations use BSD-licensed golang.org/x/sys/unix. Default subdirectory shares preserve the original os.Root behavior.
|
||||
@@ -0,0 +1,247 @@
|
||||
// RouterShelf addition. Licensed under Apache-2.0; see LICENSE.
|
||||
|
||||
package smbserver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// guardedRoot keeps excluded service subtrees inaccessible in an explicitly
|
||||
// selected disk-root view. Parent directories are pinned with os.Root, and
|
||||
// symlinks/hard-linked regular files are refused in this profile. A lexical
|
||||
// filter alone would let aliases and renames expose credentials.
|
||||
type guardedRoot struct {
|
||||
*os.Root
|
||||
denied []string
|
||||
}
|
||||
|
||||
func foldedPrefix(p, prefix string) bool {
|
||||
if prefix == "." {
|
||||
return true
|
||||
}
|
||||
a, b := strings.Split(p, "/"), strings.Split(prefix, "/")
|
||||
if len(a) < len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range b {
|
||||
if !strings.EqualFold(a[i], b[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (r *guardedRoot) allowed(p string, ancestors bool) bool {
|
||||
if p != "." && !fs.ValidPath(p) {
|
||||
return false
|
||||
}
|
||||
for _, d := range r.denied {
|
||||
if foldedPrefix(p, d) || ancestors && foldedPrefix(d, p) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *guardedRoot) parent(p string) (*os.Root, string, func(), error) {
|
||||
if !r.allowed(p, false) {
|
||||
return nil, "", nil, fs.ErrPermission
|
||||
}
|
||||
dir, leaf := path.Dir(p), path.Base(p)
|
||||
current := r.Root
|
||||
cleanup := func() {
|
||||
if current != r.Root {
|
||||
current.Close()
|
||||
}
|
||||
}
|
||||
if dir == "." {
|
||||
return current, leaf, cleanup, nil
|
||||
}
|
||||
for _, part := range strings.Split(dir, "/") {
|
||||
before, err := current.Lstat(part)
|
||||
if err != nil {
|
||||
cleanup()
|
||||
return nil, "", nil, err
|
||||
}
|
||||
if !before.IsDir() {
|
||||
cleanup()
|
||||
return nil, "", nil, fs.ErrPermission
|
||||
}
|
||||
next, err := current.OpenRoot(part)
|
||||
if err != nil {
|
||||
cleanup()
|
||||
return nil, "", nil, err
|
||||
}
|
||||
actual, err := next.Stat(".")
|
||||
if err != nil || !os.SameFile(before, actual) {
|
||||
next.Close()
|
||||
cleanup()
|
||||
return nil, "", nil, fs.ErrPermission
|
||||
}
|
||||
cleanup()
|
||||
current = next
|
||||
}
|
||||
return current, leaf, cleanup, nil
|
||||
}
|
||||
func safeInfo(fi os.FileInfo) bool {
|
||||
if fi.Mode()&os.ModeSymlink != 0 {
|
||||
return false
|
||||
}
|
||||
if fi.Mode().IsRegular() {
|
||||
if st, ok := sysStat(fi); ok && st.nlink > 1 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (r *guardedRoot) Stat(p string) (os.FileInfo, error) {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Stat(p)
|
||||
}
|
||||
return r.Lstat(p)
|
||||
}
|
||||
func (r *guardedRoot) Lstat(p string) (os.FileInfo, error) {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Lstat(p)
|
||||
}
|
||||
parent, leaf, close, err := r.parent(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer close()
|
||||
fi, err := parent.Lstat(leaf)
|
||||
if err == nil && !safeInfo(fi) {
|
||||
return nil, fs.ErrPermission
|
||||
}
|
||||
return fi, err
|
||||
}
|
||||
func (r *guardedRoot) Open(p string) (*os.File, error) { return r.OpenFile(p, os.O_RDONLY, 0) }
|
||||
func (r *guardedRoot) OpenFile(p string, flags int, mode os.FileMode) (*os.File, error) {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.OpenFile(p, flags, mode)
|
||||
}
|
||||
parent, leaf, close, err := r.parent(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer close()
|
||||
f, err := parent.OpenFile(leaf, flags&^os.O_TRUNC|unix.O_NOFOLLOW|unix.O_NONBLOCK, mode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
if err != nil || !safeInfo(fi) || !(fi.IsDir() || fi.Mode().IsRegular()) {
|
||||
f.Close()
|
||||
return nil, fs.ErrPermission
|
||||
}
|
||||
if flags&os.O_TRUNC != 0 {
|
||||
if err := f.Truncate(0); err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
func (r *guardedRoot) Mkdir(p string, mode os.FileMode) error {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Mkdir(p, mode)
|
||||
}
|
||||
parent, leaf, close, err := r.parent(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer close()
|
||||
return parent.Mkdir(leaf, mode)
|
||||
}
|
||||
func (r *guardedRoot) Remove(p string) error {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Remove(p)
|
||||
}
|
||||
if !r.allowed(p, true) {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
parent, leaf, close, err := r.parent(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer close()
|
||||
return parent.Remove(leaf)
|
||||
}
|
||||
func (r *guardedRoot) Rename(from, to string) error {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Rename(from, to)
|
||||
}
|
||||
if !r.allowed(from, true) || !r.allowed(to, true) {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
a, old, closeA, err := r.parent(from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeA()
|
||||
b, newName, closeB, err := r.parent(to)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeB()
|
||||
ad, err := a.Open(".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ad.Close()
|
||||
bd, err := b.Open(".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer bd.Close()
|
||||
return unix.Renameat(int(ad.Fd()), old, int(bd.Fd()), newName)
|
||||
}
|
||||
func (r *guardedRoot) Chmod(p string, mode os.FileMode) error {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Chmod(p, mode)
|
||||
}
|
||||
f, err := r.Open(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
return f.Chmod(mode)
|
||||
}
|
||||
func (r *guardedRoot) Chtimes(p string, atime, mtime time.Time) error {
|
||||
if len(r.denied) == 0 {
|
||||
return r.Root.Chtimes(p, atime, mtime)
|
||||
}
|
||||
f, err := r.Open(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if atime.IsZero() {
|
||||
atime = fi.ModTime()
|
||||
if st, ok := sysStat(fi); ok {
|
||||
atime = st.atime
|
||||
}
|
||||
}
|
||||
if mtime.IsZero() {
|
||||
mtime = fi.ModTime()
|
||||
}
|
||||
return unix.Futimes(int(f.Fd()), []unix.Timeval{unix.NsecToTimeval(atime.UnixNano()), unix.NsecToTimeval(mtime.UnixNano())})
|
||||
}
|
||||
func newGuardedRoot(root *os.Root, denied []string) (*guardedRoot, error) {
|
||||
for _, p := range denied {
|
||||
if !fs.ValidPath(p) || p == "." {
|
||||
return nil, errors.New("invalid protected relative path")
|
||||
}
|
||||
}
|
||||
return &guardedRoot{Root: root, denied: append([]string(nil), denied...)}, nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// RouterShelf addition. Licensed under Apache-2.0; see LICENSE.
|
||||
|
||||
package smbserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProtectedDiskRootProtocol(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for _, d := range []string{".smb-service", ".public-share-service", "parent/private", "ordinary", "destination"} {
|
||||
if err := os.MkdirAll(filepath.Join(root, d), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, p := range []string{".smb-service/binary", ".public-share-service/key", "parent/private/secret"} {
|
||||
if err := os.WriteFile(filepath.Join(root, p), []byte("private sentinel"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Symlink(".public-share-service", filepath.Join(root, "alias")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Link(filepath.Join(root, "parent/private/secret"), filepath.Join(root, "hardlink")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := connect(t, startServer(t, Config{Root: root, DiskMetadata: true, ProtectedPaths: []string{".smb-service", ".public-share-service", "parent/private"}}))
|
||||
for _, name := range []string{".smb-service/binary", ".public-share-service/key", "alias/key", "hardlink", "parent/private/secret", ".SMB-SERVICE/binary", ".ſmb-service/binary"} {
|
||||
name = strings.ReplaceAll(name, "/", `\`)
|
||||
for _, disp := range []uint32{dispOpen, dispOpenIf, dispOverwriteIf} {
|
||||
if id, st := c.create(name, accessRW, disp, 0); st == statusSuccess {
|
||||
c.close(id)
|
||||
t.Fatalf("protected path opened: %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, name := range c.list("") {
|
||||
if strings.EqualFold(name, ".smb-service") || name == ".public-share-service" || name == "alias" || name == "hardlink" {
|
||||
t.Fatalf("protected name listed: %q", name)
|
||||
}
|
||||
}
|
||||
for _, name := range c.list("parent") {
|
||||
if name == "private" {
|
||||
t.Fatal("nested protected directory listed")
|
||||
}
|
||||
}
|
||||
parent, st := c.create("parent", accessRW, dispOpen, optDirectory)
|
||||
if st != statusSuccess {
|
||||
t.Fatal(st)
|
||||
}
|
||||
if st, _ := c.do(cmdSetInfo, setInfoBody(parent, fileRename, renameData("relocated", false))); st == statusSuccess {
|
||||
t.Fatal("renamed ancestor of protected data")
|
||||
}
|
||||
c.close(parent)
|
||||
id, st := c.create(`ordinary\file`, accessRW, dispCreate, 0)
|
||||
if st != statusSuccess {
|
||||
t.Fatal(st)
|
||||
}
|
||||
if st := c.write(id, 0, []byte("ordinary data")); st != statusSuccess {
|
||||
t.Fatal(st)
|
||||
}
|
||||
basic := make([]byte, 40)
|
||||
le.PutUint64(basic[16:], filetime(time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)))
|
||||
if st, _ := c.do(cmdSetInfo, setInfoBody(id, fileBasic, basic)); st != statusSuccess {
|
||||
t.Fatalf("descriptor timestamps: %x", st)
|
||||
}
|
||||
if st, _ := c.do(cmdSetInfo, setInfoBody(id, fileRename, renameData(`destination\moved`, false))); st != statusSuccess {
|
||||
t.Fatalf("cross-directory rename: %x", st)
|
||||
}
|
||||
if st, _ := c.do(cmdSetInfo, setInfoBody(id, fileRename, renameData(`.smb-service\replacement`, true))); st == statusSuccess {
|
||||
t.Fatal("renamed into protected service directory")
|
||||
}
|
||||
c.close(id)
|
||||
if data, err := os.ReadFile(filepath.Join(root, "destination/moved")); err != nil || !bytes.Equal(data, []byte("ordinary data")) {
|
||||
t.Fatalf("ordinary file corrupted: %q %v", data, err)
|
||||
}
|
||||
for _, p := range []string{".smb-service/binary", ".public-share-service/key", "parent/private/secret"} {
|
||||
if b, err := os.ReadFile(filepath.Join(root, p)); err != nil || string(b) != "private sentinel" {
|
||||
t.Fatalf("private data modified: %s", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@ type Config struct {
|
||||
// Root is the directory to serve. Nothing outside of it is reachable,
|
||||
// including through symbolic links.
|
||||
Root string
|
||||
// ProtectedPaths excludes relative subtrees in an explicit disk-root view.
|
||||
ProtectedPaths []string
|
||||
// ExpectedRoot prevents serving a replacement between validation and open.
|
||||
ExpectedRoot os.FileInfo
|
||||
// Ready runs after confinement and cancellation are initialized.
|
||||
@@ -71,7 +73,7 @@ const (
|
||||
|
||||
type server struct {
|
||||
cfg Config
|
||||
root *os.Root
|
||||
root *guardedRoot
|
||||
rootPath string
|
||||
guid [16]byte
|
||||
start time.Time
|
||||
@@ -172,9 +174,14 @@ func newServer(cfg Config) (*server, error) {
|
||||
return nil, errors.New("smbserver: storage identity changed before startup")
|
||||
}
|
||||
}
|
||||
guarded, err := newGuardedRoot(root, cfg.ProtectedPaths)
|
||||
if err != nil {
|
||||
root.Close()
|
||||
return nil, err
|
||||
}
|
||||
s := &server{
|
||||
cfg: cfg,
|
||||
root: root,
|
||||
root: guarded,
|
||||
rootPath: abs,
|
||||
start: time.Now(),
|
||||
conns: make(map[*conn]struct{}),
|
||||
|
||||
Reference in new issue
Block a user