package provider import ( "os" "path/filepath" "strings" "golang.org/x/sys/unix" ) // openNoLinks opens path refusing a symbolic link in any component // (O_NOFOLLOW_ANY, macOS 11 and later). func openNoLinks(path string, flags int) (int, error) { return unix.Open(path, flags|unix.O_NOFOLLOW_ANY, 0) } // OpenBeneath opens rel beneath the open directory dir: no symbolic link // in any component (O_NOFOLLOW_ANY), and rel may not climb out of dir. func OpenBeneath(dir *os.File, rel string, flags int) (*os.File, error) { if filepath.IsAbs(rel) || rel == ".." || strings.HasPrefix(rel, "../") || strings.Contains(rel, "/../") { return nil, ErrPolicy } fd, err := unix.Openat(int(dir.Fd()), rel, flags|unix.O_NOFOLLOW_ANY|unix.O_CLOEXEC, 0) if err != nil { return nil, err } return os.NewFile(uintptr(fd), filepath.Join(dir.Name(), rel)), nil }