@@ -8,6 +8,8 @@ import (
88
99// SearchParent searches for a target file or directory starting from dir
1010// and walking up the directory tree until found or root or home is reached.
11+// It also checks the ownership of directories to ensure that the search does
12+// not cross ownership boundaries.
1113// Returns the full path to the target if found, empty string and false otherwise.
1214// The search includes the starting directory itself.
1315func SearchParent(dir, target string) (string, bool) {
@@ -24,6 +26,10 @@ func SearchParent(dir, target string) (string, bool) {
2426 }
2527
2628 previousParent := absDir
29+ previousOwner, err := Owner(previousParent)
30+ if err != nil {
31+ return "", false
32+ }
2733
2834 for {
2935 parent := filepath.Dir(previousParent)
@@ -31,6 +37,14 @@ func SearchParent(dir, target string) (string, bool) {
3137 return "", false
3238 }
3339
40+ parentOwner, err := Owner(parent)
41+ if err != nil {
42+ return "", false
43+ }
44+ if parentOwner != previousOwner {
45+ return "", false
46+ }
47+
3448 path := filepath.Join(parent, target)
3549 if _, err := os.Stat(path); err == nil {
3650 return path, true
@@ -39,5 +53,6 @@ func SearchParent(dir, target string) (string, bool) {
3953 }
4054
4155 previousParent = parent
56+ previousOwner = parentOwner
4257 }
4358}