fix: anchor profile directory traversal

This commit is contained in:
proxyctl coordinator
2026-07-17 02:39:42 +08:00
parent 1fa7e0fb2d
commit 6c06859783
4 changed files with 265 additions and 29 deletions
+126 -23
View File
@@ -18,9 +18,10 @@ import (
)
type FileStore struct {
path string
owner *UserIdentity
operations fileOperations
path string
owner *UserIdentity
trustedRoot string
operations fileOperations
}
type fileOperations interface {
@@ -43,11 +44,19 @@ func NewFileStore(path string) *FileStore {
}
func NewOwnedFileStore(path string, owner UserIdentity) *FileStore {
return &FileStore{path: path, owner: &owner, operations: systemFileOperations{}}
return NewOwnedFileStoreAt(path, owner, owner.HomeDir)
}
func NewOwnedFileStoreAt(path string, owner UserIdentity, trustedRoot string) *FileStore {
return &FileStore{path: path, owner: &owner, trustedRoot: trustedRoot, operations: systemFileOperations{}}
}
func newFileStore(path string, owner *UserIdentity, operations fileOperations) *FileStore {
return &FileStore{path: path, owner: owner, operations: operations}
trustedRoot := ""
if owner != nil {
trustedRoot = owner.HomeDir
}
return &FileStore{path: path, owner: owner, trustedRoot: trustedRoot, operations: operations}
}
type UserIdentity struct {
@@ -58,8 +67,9 @@ type UserIdentity struct {
}
type Configuration struct {
Path string
Owner UserIdentity
Path string
Owner UserIdentity
TrustedRoot string
}
type IdentityEnvironment interface {
@@ -132,17 +142,22 @@ func ResolveConfiguration(environment IdentityEnvironment) (Configuration, error
}
configDir := ""
trustedRoot := owner.HomeDir
if value, ok := environment.LookupEnv("XDG_CONFIG_HOME"); ok && filepath.IsAbs(value) {
if !invokingUser || pathWithin(value, owner.HomeDir) {
configDir = value
if !pathWithin(value, owner.HomeDir) {
trustedRoot = value
}
}
}
if configDir == "" {
configDir = filepath.Join(owner.HomeDir, ".config")
}
return Configuration{
Path: filepath.Join(configDir, "proxyctl", "profiles.json"),
Owner: owner,
Path: filepath.Join(configDir, "proxyctl", "profiles.json"),
Owner: owner,
TrustedRoot: trustedRoot,
}, nil
}
@@ -171,7 +186,7 @@ func (s *FileStore) Load() (State, error) {
}
func (s *FileStore) Mutate(change func(State) (State, error)) (State, error) {
directory, err := s.secureDirectory()
directory, err := s.openConfigurationDirectory(true)
if err != nil {
return State{}, err
}
@@ -221,14 +236,17 @@ func openAt(directory *os.File, name string, flags int, mode uint32) (*os.File,
}
func (s *FileStore) load() (State, error) {
directory, err := openNoFollow(filepath.Dir(s.path), unix.O_RDONLY|unix.O_DIRECTORY, 0)
directory, err := s.openConfigurationDirectory(false)
if errors.Is(err, fs.ErrNotExist) {
return State{}, nil
}
if err != nil {
return State{}, fmt.Errorf("open Profile configuration directory without following links: %w", err)
return State{}, err
}
defer directory.Close()
if err := s.secureExistingLock(directory); err != nil {
return State{}, err
}
return s.loadFrom(directory)
}
@@ -276,26 +294,111 @@ func (s *FileStore) secureRegularFile(file *os.File, description string) error {
return nil
}
func (s *FileStore) secureDirectory() (*os.File, error) {
directoryPath := filepath.Dir(s.path)
if err := os.MkdirAll(directoryPath, 0o700); err != nil {
return nil, fmt.Errorf("create Profile configuration directory: %w", err)
func (s *FileStore) openConfigurationDirectory(create bool) (*os.File, error) {
directoryPath := filepath.Clean(filepath.Dir(s.path))
if !filepath.IsAbs(directoryPath) {
return nil, errors.New("Profile configuration path must be absolute")
}
directory, err := openNoFollow(directoryPath, unix.O_RDONLY|unix.O_DIRECTORY, 0)
if directoryPath == string(filepath.Separator) {
return nil, errors.New("Profile configuration directory cannot be the filesystem root")
}
creationRoot := directoryPath
if s.owner != nil {
creationRoot = filepath.Clean(s.trustedRoot)
if !filepath.IsAbs(creationRoot) || !pathWithin(directoryPath, creationRoot) || directoryPath == creationRoot {
return nil, errors.New("Profile configuration directory escapes its trusted root")
}
}
current, err := openNoFollow(string(filepath.Separator), unix.O_RDONLY|unix.O_DIRECTORY, 0)
if err != nil {
return nil, fmt.Errorf("open Profile configuration directory without following links: %w", err)
return nil, fmt.Errorf("open filesystem root: %w", err)
}
currentPath := string(filepath.Separator)
for _, component := range strings.Split(strings.TrimPrefix(directoryPath, string(filepath.Separator)), string(filepath.Separator)) {
if component == "" {
continue
}
nextPath := filepath.Join(currentPath, component)
next, openErr := openAt(current, component, unix.O_RDONLY|unix.O_DIRECTORY, 0)
if errors.Is(openErr, fs.ErrNotExist) && create && s.mayCreateDirectory(nextPath, directoryPath, creationRoot) {
created := false
if mkdirErr := unix.Mkdirat(int(current.Fd()), component, 0o700); mkdirErr != nil {
if !errors.Is(mkdirErr, fs.ErrExist) {
current.Close()
return nil, fmt.Errorf("create Profile configuration directory component %q: %w", nextPath, mkdirErr)
}
} else {
created = true
}
next, openErr = openAt(current, component, unix.O_RDONLY|unix.O_DIRECTORY, 0)
if openErr == nil && created {
if err := s.secureCreatedDirectory(next, nextPath); err != nil {
next.Close()
_ = unix.Unlinkat(int(current.Fd()), component, unix.AT_REMOVEDIR)
current.Close()
return nil, err
}
}
}
current.Close()
if openErr != nil {
if errors.Is(openErr, fs.ErrNotExist) {
return nil, openErr
}
return nil, fmt.Errorf("open Profile configuration ancestor %q without following links: %w", nextPath, openErr)
}
current = next
currentPath = nextPath
}
if err := s.secureConfigurationDirectory(current); err != nil {
current.Close()
return nil, err
}
return current, nil
}
func (s *FileStore) mayCreateDirectory(path, directoryPath, creationRoot string) bool {
if s.owner == nil {
return path == directoryPath
}
return path != creationRoot && pathWithin(path, creationRoot)
}
func (s *FileStore) secureCreatedDirectory(directory *os.File, path string) error {
if s.owner != nil {
if err := s.operations.SetOwner(directory, s.owner.UID, s.owner.GID); err != nil {
directory.Close()
return nil, fmt.Errorf("set Profile configuration directory owner: %w", err)
return fmt.Errorf("set created Profile configuration directory %q owner: %w", path, err)
}
}
if err := directory.Chmod(0o700); err != nil {
directory.Close()
return nil, fmt.Errorf("secure Profile configuration directory: %w", err)
return fmt.Errorf("secure created Profile configuration directory %q: %w", path, err)
}
return directory, nil
return nil
}
func (s *FileStore) secureConfigurationDirectory(directory *os.File) error {
if s.owner != nil {
if err := s.operations.SetOwner(directory, s.owner.UID, s.owner.GID); err != nil {
return fmt.Errorf("set Profile configuration directory owner: %w", err)
}
}
if err := directory.Chmod(0o700); err != nil {
return fmt.Errorf("secure Profile configuration directory: %w", err)
}
return nil
}
func (s *FileStore) secureExistingLock(directory *os.File) error {
lock, err := openAt(directory, filepath.Base(s.path)+".lock", unix.O_RDWR, 0)
if errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
return fmt.Errorf("open existing Profile configuration lock without following links: %w", err)
}
defer lock.Close()
return s.secureRegularFile(lock, "Profile configuration lock")
}
func (s *FileStore) openLockAt(directory *os.File) (*os.File, error) {