Skip to content

Commit

Permalink
Trace path traversal in Windows directory removal
Browse files Browse the repository at this point in the history
To give better error messages for which file or directory operation
fails.
  • Loading branch information
Nils Wireklint authored and Nils Wireklint committed Feb 8, 2024
1 parent 6deceb5 commit 1c26964
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/filesystem/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ go_library(
],
"@io_bazel_rules_go//go/platform:windows": [
"//pkg/filesystem/windowsext",
"//pkg/util",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
"@org_golang_x_sys//windows",
Expand Down
16 changes: 11 additions & 5 deletions pkg/filesystem/local_directory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/buildbarn/bb-storage/pkg/filesystem/path"
"github.com/buildbarn/bb-storage/pkg/filesystem/windowsext"
"github.com/buildbarn/bb-storage/pkg/util"

"golang.org/x/sys/windows"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -586,32 +587,37 @@ func (d *localDirectory) Remove(name path.Component) error {

// On NTFS mount point is a reparse point, no need to unmount.
func (d *localDirectory) RemoveAllChildren() error {
return d.removeAllChildren(nil)
}

func (d *localDirectory) removeAllChildren(dPath *path.Trace) error {
defer runtime.KeepAlive(d)

names, err := readdirnames(d.handle)
if err != nil {
return err
return util.StatusWrapf(err, "Failed to read contents of directory %#v", dPath.String())
}
for _, name := range names {
component := path.MustNewComponent(name)
fileType, err := d.lstat(component)
childPath := dPath.Append(component)
if err != nil {
return err
return util.StatusWrapf(err, "Failed to stat component %#v", childPath)
}
if fileType == FileTypeDirectory {
subdirectory, err := d.enter(component, true)
if err != nil {
return err
return util.StatusWrapf(err, "Failed to enter component %#v", childPath)
}
err = subdirectory.RemoveAllChildren()
err = subdirectory.removeAllChildren(childPath)
subdirectory.Close()
if err != nil {
return err
}
}
err = d.Remove(component)
if err != nil {
return err
return util.StatusWrapf(err, "Failed to remove component %#v", childPath)
}
}
return nil
Expand Down

0 comments on commit 1c26964

Please sign in to comment.