| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//go:build linux |
| 4 |
|
| 5 |
// Copyright (C) 2024-2025 Aleksa Sarai <[email protected]> |
| 6 |
// Copyright (C) 2024-2025 SUSE LLC |
| 7 |
// |
| 8 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 9 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 11 |
|
| 12 |
package procfs |
| 13 |
|
| 14 |
import ( |
| 15 |
"fmt" |
| 16 |
"os" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/stretchr/testify/assert" |
| 20 |
"github.com/stretchr/testify/require" |
| 21 |
|
| 22 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal" |
| 23 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/linux" |
| 24 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/testutils" |
| 25 |
) |
| 26 |
|
| 27 |
func TestProcfsLookupInRoot(t *testing.T) { |
| 28 |
testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 29 |
t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 30 |
// NOTE: We don't actually need root for unsafeHostProcRoot, but we |
| 31 |
// can't test for that because Go doesn't let you compare function |
| 32 |
// pointers... |
| 33 |
testutils.RequireRoot(t) |
| 34 |
|
| 35 |
// The openat2 and non-openat2 backends return different error |
| 36 |
// messages for the breakout case (".." and suspected magic-links). |
| 37 |
// The main issue is that openat2 just returns -EXDEV and returning |
| 38 |
// errUnsafeProcfs in all cases of the fallback resolver (for |
| 39 |
// consistency) doesn't make much sense. |
| 40 |
breakoutErr := internal.ErrPossibleBreakout |
| 41 |
if linux.HasOpenat2() { |
| 42 |
breakoutErr = errUnsafeProcfs |
| 43 |
} |
| 44 |
|
| 45 |
for _, test := range []struct { |
| 46 |
name string |
| 47 |
root, subpath string |
| 48 |
expectedPath string |
| 49 |
expectedErr error |
| 50 |
}{ |
| 51 |
{"nonproc-xdev", "/", "proc", "", errUnsafeProcfs}, |
| 52 |
{"proc-nonroot", "/proc/tty", ".", "", errUnsafeProcfs}, |
| 53 |
{"proc-emptypath", "/proc", "", "/proc", nil}, |
| 54 |
{"proc-root-dotdot", "/proc", "1/../..", "", breakoutErr}, |
| 55 |
{"proc-root-dotdot-top", "/proc", "..", "", breakoutErr}, |
| 56 |
{"proc-abs-slash", "/proc", "/", "", breakoutErr}, |
| 57 |
{"proc-abs-path", "/proc", "/etc/passwd", "", breakoutErr}, |
| 58 |
// {"dotdot", "1/..", breakoutErr}, // only errors out for fallback resolver |
| 59 |
{"proc-uptime", "/proc", "uptime", "/proc/uptime", nil}, |
| 60 |
{"proc-sys-kernel-arch", "/proc", "sys/kernel/arch", "/proc/sys/kernel/arch", nil}, |
| 61 |
{"proc-symlink-nofollow", "/proc", "self", "/proc/self", nil}, |
| 62 |
{"proc-symlink-follow", "/proc", "self/.", fmt.Sprintf("/proc/%d", os.Getpid()), nil}, |
| 63 |
{"proc-self-attr", "/proc", "self/attr/apparmor/exec", fmt.Sprintf("/proc/%d/attr/apparmor/exec", os.Getpid()), nil}, |
| 64 |
{"proc-magiclink-nofollow", "/proc", "self/exe", fmt.Sprintf("/proc/%d/exe", os.Getpid()), nil}, |
| 65 |
{"proc-magiclink-follow", "/proc", "self/cwd/.", "", breakoutErr}, |
| 66 |
} { |
| 67 |
test := test // copy iterator |
| 68 |
t.Run(test.name, func(t *testing.T) { |
| 69 |
root, err := os.Open(test.root) |
| 70 |
require.NoError(t, err, "open procfs resolver root") |
| 71 |
|
| 72 |
handle, err := procfsLookupInRoot(root, test.subpath) |
| 73 |
assert.ErrorIsf(t, err, test.expectedErr, "procfsLookupInRoot(%q)", test.subpath) //nolint:testifylint // this is an isolated operation so we can continue despite an error |
| 74 |
if handle != nil { |
| 75 |
handlePath, err := ProcSelfFdReadlink(handle) |
| 76 |
require.NoError(t, err, "ProcSelfFdReadlink handle") |
| 77 |
assert.Equal(t, test.expectedPath, handlePath, "ProcSelfFdReadlink of handle") |
| 78 |
_ = handle.Close() |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
}) |
| 83 |
} |