Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-24.2: storage: fix incorrect mount association #137113

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/storage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -142,6 +142,7 @@ go_test(
"read_as_of_iterator_test.go",
"sst_test.go",
"sst_writer_test.go",
"store_properties_test.go",
"temp_engine_test.go",
],
data = glob(["testdata/**"]),
23 changes: 19 additions & 4 deletions pkg/storage/store_properties.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ package storage
import (
"context"
"path/filepath"
"strings"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/log"
@@ -75,10 +76,7 @@ func getFileSystemProperties(ctx context.Context, dir string) roachpb.FileStoreP
// is typically being deployed are well-behaved in that regard:
// Kubernetes mirrors /proc/mount in /etc/mtab.
for i := len(fslist.List) - 1; i >= 0; i-- {
// filepath.Rel can reliably tell us if a path is relative to
// another: if it is not, an error is returned.
_, err := filepath.Rel(fslist.List[i].DirName, absPath)
if err == nil {
if pathIsInside(fslist.List[i].DirName, absPath) {
fsInfo = &fslist.List[i]
break
}
@@ -95,3 +93,20 @@ func getFileSystemProperties(ctx context.Context, dir string) roachpb.FileStoreP
fsprops.MountOptions = fsInfo.Options
return fsprops
}

// pathIsInside returns true if the absolute target path is inside a base path.
func pathIsInside(basePath string, absTargetPath string) bool {
// filepath.Rel can reliably tell us if a path is relative to
// another: if it is not, an error is returned.
relPath, err := filepath.Rel(basePath, absTargetPath)
if err != nil {
return false
}
if strings.HasPrefix(relPath, "..") {
// This check is consistent with internal filepath code (like isLocal).
if len(relPath) == 2 || relPath[2] == filepath.Separator {
return false
}
}
return true
}
67 changes: 67 additions & 0 deletions pkg/storage/store_properties_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.

package storage

import (
"path/filepath"
"testing"

"github.com/cockroachdb/cockroach/pkg/util/leaktest"
)

func TestPathIsInside(t *testing.T) {
defer leaktest.AfterTest(t)()

testCases := []struct {
base, target string
expected bool
}{
{
base: "/",
target: "/cockroach/cockroach-data",
expected: true,
},
{
base: "/cockroach",
target: "/cockroach/cockroach-data",
expected: true,
},
{
base: "/cockroach/cockroach-data",
target: "/cockroach/cockroach-data",
expected: true,
},
{
base: "/cockroach/cockroach-data/foo",
target: "/cockroach/cockroach-data",
expected: false,
},
{
base: "/cockroach/cockroach-data1",
target: "/cockroach/cockroach-data",
expected: false,
},
{
base: "/run/user/1001",
target: "/cockroach/cockroach-data",
expected: false,
},
{
base: "/..foo",
target: "/..foo/data",
expected: true,
},
}

for _, tc := range testCases {
t.Run("", func(t *testing.T) {
result := pathIsInside(filepath.FromSlash(tc.base), filepath.FromSlash(tc.target))
if result != tc.expected {
t.Fatalf("%q, %q: expected %t, got %t", tc.base, tc.target, tc.expected, result)
}
})
}
}