Files
Ryan El Kochta efb3bcd85f Implement openat2(2) in gVisor
Seems that GNU tar extraction has been broken in gVisor since 75b03fd
(11/15/2025), which started using openat2(2) to address CVE-2025-45582.

tar uses openat2(2) for the RESOLVE_BENEATH flag which allows scoping
the path resolution underneath the specified dirfd.

This commit implements openat2(2) in gVisor, with support for all flags
supported by Linux: RESOLVE_BENEATH, RESOLVE_IN_ROOT, RESOLVE_CACHED,
RESOLVE_NO_MAGICLINKS, RESOLVE_NO_SYMLINKS, and RESOLVE_NO_XDEV.

This commit also introduces VirtualFilesystem.renameSeq, a global
sequence counter that along with mountSeq detects mount or rename
operations that race with scoped traversals.

A final "resolved-path-is-under-dirfd" check is performed after
the traversal. This is for two reasons: (1) in case of a bug in
any of the above logic, or (2) in case of an (extremely unlikely)
attack where mountSeq overflows by exactly 2^32 during a path
resolution. Unfortunately, due to the way path traversal in gVisor
is tightly coupled to the operation being performed by a VFS method,
using scoped traversals in an operation that *mutates* anything will
be difficult, since without major refactoring, the check must happen
after the side effect has taken place.

Currently, Linux only exposes scoped traversals through openat2(2),
so destroying the created fd is all we need to do if the final check
fails. (As a result of this limitation, most VFS functions have been
modified to reject scoped traversals "just-in-case".)

The RESOLVE_NO_XDEV implementation has a slight divergence from
Linux. It is a bit looser, permitting absolute symlinks in any case
where the root dentry's mount matches the mount the traversal is
locked to. Linux's behavior here is likely a bug, and since we can
safely be *more* permissive, there's not much reason to match it.

This commit also adds syscall tests and an strace formatter for
openat2(2).
2026-08-21 10:34:56 -07:00

282 lines
7.9 KiB
Go

// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linux
import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/bits"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/fspath"
"gvisor.dev/gvisor/pkg/hostarch"
"gvisor.dev/gvisor/pkg/sentry/arch"
"gvisor.dev/gvisor/pkg/sentry/kernel"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/vfs"
)
// Stat implements Linux syscall stat(2).
func Stat(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
pathAddr := args[0].Pointer()
statAddr := args[1].Pointer()
return 0, nil, fstatat(t, linux.AT_FDCWD, pathAddr, statAddr, 0 /* flags */)
}
// Lstat implements Linux syscall lstat(2).
func Lstat(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
pathAddr := args[0].Pointer()
statAddr := args[1].Pointer()
return 0, nil, fstatat(t, linux.AT_FDCWD, pathAddr, statAddr, linux.AT_SYMLINK_NOFOLLOW)
}
// Newfstatat implements Linux syscall newfstatat, which backs fstatat(2).
func Newfstatat(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
dirfd := args[0].Int()
pathAddr := args[1].Pointer()
statAddr := args[2].Pointer()
flags := args[3].Int()
return 0, nil, fstatat(t, dirfd, pathAddr, statAddr, flags)
}
func fstatat(t *kernel.Task, dirfd int32, pathAddr, statAddr hostarch.Addr, flags int32) error {
// TODO(b/270247637): gVisor does not yet support automount, so
// AT_NO_AUTOMOUNT flag is a no-op.
flags &= ^linux.AT_NO_AUTOMOUNT
if flags&^(linux.AT_EMPTY_PATH|linux.AT_SYMLINK_NOFOLLOW) != 0 {
return linuxerr.EINVAL
}
opts := vfs.StatOptions{
Mask: linux.STATX_BASIC_STATS,
}
path, err := copyInPath(t, pathAddr)
if err != nil {
return err
}
root := t.FSContext().RootDirectory()
defer root.DecRef(t)
start := root
if !path.Absolute {
if !path.HasComponents() && flags&linux.AT_EMPTY_PATH == 0 {
return linuxerr.ENOENT
}
if dirfd == linux.AT_FDCWD {
start = t.FSContext().WorkingDirectory()
defer start.DecRef(t)
} else {
dirfile := t.GetFile(dirfd)
if dirfile == nil {
return linuxerr.EBADF
}
if !path.HasComponents() {
// Use FileDescription.Stat() instead of
// VirtualFilesystem.StatAt() for fstatat(fd, ""), since the
// former may be able to use opened file state to expedite the
// Stat.
statx, err := dirfile.Stat(t, opts)
dirfile.DecRef(t)
if err != nil {
return err
}
var stat linux.Stat
convertStatxToUserStat(t, &statx, &stat)
_, err = stat.CopyOut(t, statAddr)
return err
}
start = dirfile.VirtualDentry()
start.IncRef()
defer start.DecRef(t)
dirfile.DecRef(t)
}
}
statx, err := t.Kernel().VFS().StatAt(t, t.Credentials(), &vfs.PathOperation{
Root: root,
Start: start,
Path: path,
FollowFinalSymlink: flags&linux.AT_SYMLINK_NOFOLLOW == 0,
}, &opts)
if err != nil {
return err
}
var stat linux.Stat
convertStatxToUserStat(t, &statx, &stat)
_, err = stat.CopyOut(t, statAddr)
return err
}
func timespecFromStatxTimestamp(sxts linux.StatxTimestamp) linux.Timespec {
return linux.Timespec{
Sec: sxts.Sec,
Nsec: int64(sxts.Nsec),
}
}
// Fstat implements Linux syscall fstat(2).
func Fstat(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
fd := args[0].Int()
statAddr := args[1].Pointer()
file := t.GetFile(fd)
if file == nil {
return 0, nil, linuxerr.EBADF
}
defer file.DecRef(t)
statx, err := file.Stat(t, vfs.StatOptions{
Mask: linux.STATX_BASIC_STATS,
})
if err != nil {
return 0, nil, err
}
var stat linux.Stat
convertStatxToUserStat(t, &statx, &stat)
_, err = stat.CopyOut(t, statAddr)
return 0, nil, err
}
// Statx implements Linux syscall statx(2).
func Statx(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
dirfd := args[0].Int()
pathAddr := args[1].Pointer()
flags := args[2].Int()
mask := args[3].Uint()
statxAddr := args[4].Pointer()
// TODO(b/270247637): gVisor does not yet support automount, so
// AT_NO_AUTOMOUNT flag is a no-op.
flags &= ^linux.AT_NO_AUTOMOUNT
if flags&^(linux.AT_EMPTY_PATH|linux.AT_SYMLINK_NOFOLLOW|linux.AT_STATX_SYNC_TYPE) != 0 {
return 0, nil, linuxerr.EINVAL
}
// Make sure that only one sync type option is set.
syncType := uint32(flags & linux.AT_STATX_SYNC_TYPE)
if syncType != 0 && !bits.IsPowerOfTwo(syncType) {
return 0, nil, linuxerr.EINVAL
}
if mask&linux.STATX__RESERVED != 0 {
return 0, nil, linuxerr.EINVAL
}
opts := vfs.StatOptions{
Mask: mask,
Sync: uint32(flags & linux.AT_STATX_SYNC_TYPE),
}
path, err := copyInPath(t, pathAddr)
if err != nil {
return 0, nil, err
}
root := t.FSContext().RootDirectory()
defer root.DecRef(t)
start := root
if !path.Absolute {
if !path.HasComponents() && flags&linux.AT_EMPTY_PATH == 0 {
return 0, nil, linuxerr.ENOENT
}
if dirfd == linux.AT_FDCWD {
start = t.FSContext().WorkingDirectory()
defer start.DecRef(t)
} else {
dirfile := t.GetFile(dirfd)
if dirfile == nil {
return 0, nil, linuxerr.EBADF
}
if !path.HasComponents() {
// Use FileDescription.Stat() instead of
// VirtualFilesystem.StatAt() for statx(fd, ""), since the
// former may be able to use opened file state to expedite the
// Stat.
statx, err := dirfile.Stat(t, opts)
dirfile.DecRef(t)
if err != nil {
return 0, nil, err
}
userifyStatx(t, &statx)
_, err = statx.CopyOut(t, statxAddr)
return 0, nil, err
}
start = dirfile.VirtualDentry()
start.IncRef()
defer start.DecRef(t)
dirfile.DecRef(t)
}
}
statx, err := t.Kernel().VFS().StatAt(t, t.Credentials(), &vfs.PathOperation{
Root: root,
Start: start,
Path: path,
FollowFinalSymlink: flags&linux.AT_SYMLINK_NOFOLLOW == 0,
}, &opts)
if err != nil {
return 0, nil, err
}
userifyStatx(t, &statx)
_, err = statx.CopyOut(t, statxAddr)
return 0, nil, err
}
func userifyStatx(t *kernel.Task, statx *linux.Statx) {
userns := t.UserNamespace()
statx.UID = uint32(auth.KUID(statx.UID).In(userns).OrOverflow())
statx.GID = uint32(auth.KGID(statx.GID).In(userns).OrOverflow())
}
// Statfs implements Linux syscall statfs(2).
func Statfs(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
pathAddr := args[0].Pointer()
bufAddr := args[1].Pointer()
path, err := copyInPath(t, pathAddr)
if err != nil {
return 0, nil, err
}
tpop, err := getTaskPathOperation(t, linux.AT_FDCWD, path, disallowEmptyPath, followFinalSymlink, 0)
if err != nil {
return 0, nil, err
}
defer tpop.Release(t)
statfs, err := t.Kernel().VFS().StatFSAt(t, t.Credentials(), &tpop.pop)
if err != nil {
return 0, nil, err
}
_, err = statfs.CopyOut(t, bufAddr)
return 0, nil, err
}
// Fstatfs implements Linux syscall fstatfs(2).
func Fstatfs(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
fd := args[0].Int()
bufAddr := args[1].Pointer()
tpop, err := getTaskPathOperation(t, fd, fspath.Path{}, allowEmptyPath, nofollowFinalSymlink, 0)
if err != nil {
return 0, nil, err
}
defer tpop.Release(t)
statfs, err := t.Kernel().VFS().StatFSAt(t, t.Credentials(), &tpop.pop)
if err != nil {
return 0, nil, err
}
_, err = statfs.CopyOut(t, bufAddr)
return 0, nil, err
}