Skip to content

Commit 13f35b0

Browse files
committed
proc: checkSymlinkOvermount -> checkSubpathOvermount
The logic is not symlink-specific, and will be used for non-symlinks in the O_PATH resolver. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent c79c2f1 commit 13f35b0

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

open_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Reopen(handle *os.File, flags int) (*os.File, error) {
8888
// [1]: Linux commit ee2e3f50629f ("mount: fix mounting of detached mounts
8989
// onto targets that reside on shared mounts").
9090
fdStr := strconv.Itoa(int(handle.Fd()))
91-
if err := checkSymlinkOvermount(procRoot, procFdDir, fdStr); err != nil {
91+
if err := checkSubpathOvermount(procRoot, procFdDir, fdStr); err != nil {
9292
return nil, fmt.Errorf("check safety of /proc/thread-self/fd/%s magiclink: %w", fdStr, err)
9393
}
9494

procfs_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func getMountID(dir *os.File, path string) (uint64, error) {
347347
return stx.Mnt_id, nil
348348
}
349349

350-
func checkSymlinkOvermount(procRoot *os.File, dir *os.File, path string) error {
350+
func checkSubpathOvermount(procRoot *os.File, dir *os.File, path string) error {
351351
// Get the mntID of our procfs handle.
352352
expectedMountID, err := getMountID(procRoot, "")
353353
if err != nil {
@@ -363,7 +363,7 @@ func checkSymlinkOvermount(procRoot *os.File, dir *os.File, path string) error {
363363
// using unsafeHostProcRoot() then an attaker could change this after we
364364
// did this check.)
365365
if expectedMountID != gotMountID {
366-
return fmt.Errorf("%w: symlink %s/%s has an overmount obscuring the real link (mount ids do not match %d != %d)", errUnsafeProcfs, dir.Name(), path, expectedMountID, gotMountID)
366+
return fmt.Errorf("%w: subpath %s/%s has an overmount obscuring the real link (mount ids do not match %d != %d)", errUnsafeProcfs, dir.Name(), path, expectedMountID, gotMountID)
367367
}
368368
return nil
369369
}
@@ -386,7 +386,7 @@ func doRawProcSelfFdReadlink(procRoot *os.File, fd int) (string, error) {
386386
//
387387
// [1]: Linux commit ee2e3f50629f ("mount: fix mounting of detached mounts
388388
// onto targets that reside on shared mounts").
389-
if err := checkSymlinkOvermount(procRoot, procFdLink, ""); err != nil {
389+
if err := checkSubpathOvermount(procRoot, procFdLink, ""); err != nil {
390390
return "", fmt.Errorf("check safety of /proc/thread-self/fd/%d magiclink: %w", fd, err)
391391
}
392392

procfs_linux_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func testProcOvermountSubdir(t *testing.T, procRootFn procRootFunc, expectOvermo
114114
// moved to libpathrs.
115115
{"/proc/self/sched", "attr/current", "", unix.MS_BIND},
116116
// Bind-mounts on top of symlinks should be detected by
117-
// checkSymlinkOvermount.
117+
// checkSubpathOvermount.
118118
{"/proc/1/fd/0", "exe", "", unix.MS_BIND},
119119
{"/proc/1/exe", "fd/0", "", unix.MS_BIND},
120120
// TODO: Add a test for mounting on top of /proc/self or
@@ -150,7 +150,7 @@ func testProcOvermountSubdir(t *testing.T, procRootFn procRootFunc, expectOvermo
150150
defer closer()
151151

152152
// Open these paths directly to emulate a non-openat2 handle that
153-
// didn't detect a bind-mount to check that checkSymlinkOvermount works
153+
// didn't detect a bind-mount to check that checkSubpathOvermount works
154154
// properly for AT_EMPTY_PATH checks as well.
155155
procCwd, err := openatFile(procSelf, "cwd", unix.O_PATH|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
156156
require.NoError(t, err)
@@ -160,14 +160,14 @@ func testProcOvermountSubdir(t *testing.T, procRootFn procRootFunc, expectOvermo
160160
defer procExe.Close() //nolint:errcheck // test code
161161

162162
// no overmount
163-
err = checkSymlinkOvermount(procRoot, procCwd, "")
163+
err = checkSubpathOvermount(procRoot, procCwd, "")
164164
assert.NoError(t, err, "checking /proc/self/cwd with no overmount should succeed") //nolint:testifylint // this is an isolated operation so we can continue despite an error
165-
err = checkSymlinkOvermount(procRoot, procSelf, "cwd")
165+
err = checkSubpathOvermount(procRoot, procSelf, "cwd")
166166
assert.NoError(t, err, "checking /proc/self/cwd with no overmount should succeed") //nolint:testifylint // this is an isolated operation so we can continue despite an error
167167
// basic overmount
168-
err = checkSymlinkOvermount(procRoot, procExe, "")
168+
err = checkSubpathOvermount(procRoot, procExe, "")
169169
assert.ErrorIs(t, err, symlinkOvermountErr, "unexpected /proc/self/exe overmount result") //nolint:testifylint // this is an isolated operation so we can continue despite an error
170-
err = checkSymlinkOvermount(procRoot, procSelf, "exe")
170+
err = checkSubpathOvermount(procRoot, procSelf, "exe")
171171
assert.ErrorIs(t, err, symlinkOvermountErr, "unexpected /proc/self/exe overmount result") //nolint:testifylint // this is an isolated operation so we can continue despite an error
172172

173173
// fd no overmount

0 commit comments

Comments
 (0)