Skip to content

Commit 19e3c52

Browse files
committed
proc: split verifyProcRoot
The verification logic that something is on procfs is needed in other places, we can easily split it into two functions so folks can verify the latter if they prefer. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 13f35b0 commit 19e3c52

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

procfs_linux.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,25 @@ const (
3939
procRootIno = 1 // PROC_ROOT_INO
4040
)
4141

42-
func verifyProcRoot(procRoot *os.File) error {
43-
if statfs, err := fstatfs(procRoot); err != nil {
42+
// verifyProcHandle checks that the handle is from a procfs filesystem.
43+
// Contrast this to [verifyProcRoot], which also verifies that the handle is
44+
// the root of a procfs mount.
45+
func verifyProcHandle(procHandle *os.File) error {
46+
if statfs, err := fstatfs(procHandle); err != nil {
4447
return err
4548
} else if statfs.Type != procSuperMagic {
4649
return fmt.Errorf("%w: incorrect procfs root filesystem type 0x%x", errUnsafeProcfs, statfs.Type)
4750
}
51+
return nil
52+
}
53+
54+
// verifyProcRoot verifies that the handle is the root of a procfs filesystem.
55+
// Contrast this to [verifyProcHandle], which only verifies if the handle is
56+
// some file on procfs (regardless of what file it is).
57+
func verifyProcRoot(procRoot *os.File) error {
58+
if err := verifyProcHandle(procRoot); err != nil {
59+
return err
60+
}
4861
if stat, err := fstat(procRoot); err != nil {
4962
return err
5063
} else if stat.Ino != procRootIno {

procfs_linux_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,37 +386,43 @@ func TestProcSelfFdPath_DeadDir(t *testing.T) {
386386
})
387387
}
388388

389-
func testVerifyProcRoot(t *testing.T, procRoot string, expectedErr error, errString string) {
389+
func testVerifyProcRoot(t *testing.T, procRoot string, expectedHandleErr, expectedRootErr error, errString string) {
390390
fakeProcRoot, err := os.OpenFile(procRoot, unix.O_PATH|unix.O_CLOEXEC, 0)
391391
require.NoError(t, err)
392392
defer fakeProcRoot.Close() //nolint:errcheck // test code
393393

394394
err = verifyProcRoot(fakeProcRoot)
395-
require.ErrorIsf(t, err, expectedErr, "verifyProcRoot(%s)", procRoot)
396-
if expectedErr != nil {
395+
require.ErrorIsf(t, err, expectedRootErr, "verifyProcRoot(%s)", procRoot)
396+
if expectedRootErr != nil {
397397
require.ErrorContainsf(t, err, errString, "verifyProcRoot(%s)", procRoot)
398398
}
399+
400+
err = verifyProcHandle(fakeProcRoot)
401+
require.ErrorIsf(t, err, expectedHandleErr, "verifyProcHandle(%s)", procRoot)
402+
if expectedHandleErr != nil {
403+
require.ErrorContainsf(t, err, errString, "verifyProcHandle(%s)", procRoot)
404+
}
399405
}
400406

401407
func TestVerifyProcRoot_Regular(t *testing.T) {
402408
testForceProcThreadSelf(t, func(t *testing.T) {
403-
testVerifyProcRoot(t, "/proc", nil, "")
409+
testVerifyProcRoot(t, "/proc", nil, nil, "")
404410
})
405411
}
406412

407413
func TestVerifyProcRoot_ProcNonRoot(t *testing.T) {
408414
testForceProcThreadSelf(t, func(t *testing.T) {
409-
testVerifyProcRoot(t, "/proc/self", errUnsafeProcfs, "incorrect procfs root inode number")
410-
testVerifyProcRoot(t, "/proc/mounts", errUnsafeProcfs, "incorrect procfs root inode number")
411-
testVerifyProcRoot(t, "/proc/stat", errUnsafeProcfs, "incorrect procfs root inode number")
415+
testVerifyProcRoot(t, "/proc/self", nil, errUnsafeProcfs, "incorrect procfs root inode number")
416+
testVerifyProcRoot(t, "/proc/mounts", nil, errUnsafeProcfs, "incorrect procfs root inode number")
417+
testVerifyProcRoot(t, "/proc/stat", nil, errUnsafeProcfs, "incorrect procfs root inode number")
412418
})
413419
}
414420

415421
func TestVerifyProcRoot_NotProc(t *testing.T) {
416422
testForceProcThreadSelf(t, func(t *testing.T) {
417-
testVerifyProcRoot(t, "/", errUnsafeProcfs, "incorrect procfs root filesystem type")
418-
testVerifyProcRoot(t, ".", errUnsafeProcfs, "incorrect procfs root filesystem type")
419-
testVerifyProcRoot(t, t.TempDir(), errUnsafeProcfs, "incorrect procfs root filesystem type")
423+
testVerifyProcRoot(t, "/", errUnsafeProcfs, errUnsafeProcfs, "incorrect procfs root filesystem type")
424+
testVerifyProcRoot(t, ".", errUnsafeProcfs, errUnsafeProcfs, "incorrect procfs root filesystem type")
425+
testVerifyProcRoot(t, t.TempDir(), errUnsafeProcfs, errUnsafeProcfs, "incorrect procfs root filesystem type")
420426
})
421427
}
422428

0 commit comments

Comments
 (0)