diff --git a/internal/fsext/parent.go b/internal/fsext/parent.go index 4a9e58cd6d7f208fca4023b2fd8ba70753026ea0..1b04143660e7700c51693ededf90ef7489a10e18 100644 --- a/internal/fsext/parent.go +++ b/internal/fsext/parent.go @@ -8,6 +8,8 @@ import ( // SearchParent searches for a target file or directory starting from dir // and walking up the directory tree until found or root or home is reached. +// It also checks the ownership of directories to ensure that the search does +// not cross ownership boundaries. // Returns the full path to the target if found, empty string and false otherwise. // The search includes the starting directory itself. func SearchParent(dir, target string) (string, bool) { @@ -24,6 +26,10 @@ func SearchParent(dir, target string) (string, bool) { } previousParent := absDir + previousOwner, err := Owner(previousParent) + if err != nil { + return "", false + } for { parent := filepath.Dir(previousParent) @@ -31,6 +37,14 @@ func SearchParent(dir, target string) (string, bool) { return "", false } + parentOwner, err := Owner(parent) + if err != nil { + return "", false + } + if parentOwner != previousOwner { + return "", false + } + path := filepath.Join(parent, target) if _, err := os.Stat(path); err == nil { return path, true @@ -39,5 +53,6 @@ func SearchParent(dir, target string) (string, bool) { } previousParent = parent + previousOwner = parentOwner } }