Files
runc/internal/sys/verify_inode_unix.go
T
Aleksa Sarai f88a6a6bc5 internal/sys: add VerifyInode helper
This will be used for a few security patches in later patches in this
patchset. The need to verify what kind of inode we are operating on in a
race-free way turns out to be quite a common pattern...

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-10-16 11:29:30 +11:00

31 lines
957 B
Go

package sys
import (
"fmt"
"os"
"runtime"
"golang.org/x/sys/unix"
)
// VerifyInodeFunc is the callback passed to [VerifyInode] to check if the
// inode is the expected type (and on the correct filesystem type, in the case
// of filesystem-specific inodes).
type VerifyInodeFunc func(stat *unix.Stat_t, statfs *unix.Statfs_t) error
// VerifyInode verifies that the underlying inode for the given file matches an
// expected inode type (possibly on a particular kind of filesystem). This is
// mainly a wrapper around [VerifyInodeFunc].
func VerifyInode(file *os.File, checkFunc VerifyInodeFunc) error {
var stat unix.Stat_t
if err := unix.Fstat(int(file.Fd()), &stat); err != nil {
return fmt.Errorf("fstat %q: %w", file.Name(), err)
}
var statfs unix.Statfs_t
if err := unix.Fstatfs(int(file.Fd()), &statfs); err != nil {
return fmt.Errorf("fstatfs %q: %w", file.Name(), err)
}
runtime.KeepAlive(file)
return checkFunc(&stat, &statfs)
}