Skip to content

Commit d5fefb3

Browse files
Nils WireklintNils Wireklint
Nils Wireklint
authored and
Nils Wireklint
committed
Trace path traversal in Windows directory removal
To give better error messages for which file or directory operation fails.
1 parent 61e97ca commit d5fefb3

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

pkg/filesystem/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ go_library(
5151
],
5252
"@rules_go//go/platform:windows": [
5353
"//pkg/filesystem/windowsext",
54+
"//pkg/util",
5455
"@org_golang_google_grpc//codes",
5556
"@org_golang_google_grpc//status",
5657
"@org_golang_x_sys//windows",

pkg/filesystem/local_directory_windows.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/buildbarn/bb-storage/pkg/filesystem/path"
1818
"github.com/buildbarn/bb-storage/pkg/filesystem/windowsext"
19+
"github.com/buildbarn/bb-storage/pkg/util"
1920

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

587588
// On NTFS mount point is a reparse point, no need to unmount.
588589
func (d *localDirectory) RemoveAllChildren() error {
590+
return d.removeAllChildren(nil)
591+
}
592+
593+
func (d *localDirectory) removeAllChildren(dPath *path.Trace) error {
589594
defer runtime.KeepAlive(d)
590595

591596
names, err := readdirnames(d.handle)
592597
if err != nil {
593-
return err
598+
return util.StatusWrapf(err, "Failed to read contents of directory %#v", dPath.GetUNIXString())
594599
}
595600
for _, name := range names {
596601
component := path.MustNewComponent(name)
597602
fileType, err := d.lstat(component)
603+
childPath := dPath.Append(component)
598604
if err != nil {
599-
return err
605+
return util.StatusWrapf(err, "Failed to stat component %#v", childPath)
600606
}
601607
if fileType == FileTypeDirectory {
602608
subdirectory, err := d.enter(component, true)
603609
if err != nil {
604-
return err
610+
return util.StatusWrapf(err, "Failed to enter component %#v", childPath)
605611
}
606-
err = subdirectory.RemoveAllChildren()
612+
err = subdirectory.removeAllChildren(childPath)
607613
subdirectory.Close()
608614
if err != nil {
609615
return err
610616
}
611617
}
612618
err = d.Remove(component)
613619
if err != nil {
614-
return err
620+
return util.StatusWrapf(err, "Failed to remove component %#v", childPath)
615621
}
616622
}
617623
return nil

0 commit comments

Comments
 (0)