mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
f88a6a6bc5
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>
31 lines
957 B
Go
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)
|
|
}
|