Implement an anon_inode equivalent for VFS2.

PiperOrigin-RevId: 291986033
This commit is contained in:
Jamie Liu
2020-01-28 12:08:00 -08:00
committed by gVisor bot
parent 76483b8b1e
commit 1119644080
5 changed files with 289 additions and 188 deletions
+1 -2
View File
@@ -5,6 +5,7 @@ licenses(["notice"])
go_library(
name = "vfs",
srcs = [
"anonfs.go",
"context.go",
"debug.go",
"dentry.go",
@@ -20,7 +21,6 @@ go_library(
"pathname.go",
"permissions.go",
"resolving_path.go",
"testutil.go",
"vfs.go",
],
visibility = ["//pkg/sentry:internal"],
@@ -50,7 +50,6 @@ go_test(
"//pkg/abi/linux",
"//pkg/context",
"//pkg/sentry/contexttest",
"//pkg/sentry/kernel/auth",
"//pkg/sync",
"//pkg/syserror",
"//pkg/usermem",
+259
View File
@@ -0,0 +1,259 @@
// 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 vfs
import (
"fmt"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/fspath"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/syserror"
"gvisor.dev/gvisor/pkg/usermem"
)
// NewAnonVirtualDentry returns a VirtualDentry with the given synthetic name,
// consistent with Linux's fs/anon_inodes.c:anon_inode_getfile(). References
// are taken on the returned VirtualDentry.
func (vfs *VirtualFilesystem) NewAnonVirtualDentry(name string) VirtualDentry {
d := anonDentry{
name: name,
}
d.vfsd.Init(&d)
vfs.anonMount.IncRef()
// anonDentry no-ops refcounting.
return VirtualDentry{
mount: vfs.anonMount,
dentry: &d.vfsd,
}
}
const anonfsBlockSize = usermem.PageSize // via fs/libfs.c:pseudo_fs_fill_super()
// anonFilesystem is the implementation of FilesystemImpl that backs
// VirtualDentries returned by VirtualFilesystem.NewAnonVirtualDentry().
//
// Since all Dentries in anonFilesystem are non-directories, all FilesystemImpl
// methods that would require an anonDentry to be a directory return ENOTDIR.
type anonFilesystem struct {
vfsfs Filesystem
devMinor uint32
}
type anonDentry struct {
vfsd Dentry
name string
}
// Release implements FilesystemImpl.Release.
func (fs *anonFilesystem) Release() {
}
// Sync implements FilesystemImpl.Sync.
func (fs *anonFilesystem) Sync(ctx context.Context) error {
return nil
}
// GetDentryAt implements FilesystemImpl.GetDentryAt.
func (fs *anonFilesystem) GetDentryAt(ctx context.Context, rp *ResolvingPath, opts GetDentryOptions) (*Dentry, error) {
if !rp.Done() {
return nil, syserror.ENOTDIR
}
if opts.CheckSearchable {
return nil, syserror.ENOTDIR
}
// anonDentry no-ops refcounting.
return rp.Start(), nil
}
// GetParentDentryAt implements FilesystemImpl.GetParentDentryAt.
func (fs *anonFilesystem) GetParentDentryAt(ctx context.Context, rp *ResolvingPath) (*Dentry, error) {
if !rp.Final() {
return nil, syserror.ENOTDIR
}
// anonDentry no-ops refcounting.
return rp.Start(), nil
}
// LinkAt implements FilesystemImpl.LinkAt.
func (fs *anonFilesystem) LinkAt(ctx context.Context, rp *ResolvingPath, vd VirtualDentry) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// MkdirAt implements FilesystemImpl.MkdirAt.
func (fs *anonFilesystem) MkdirAt(ctx context.Context, rp *ResolvingPath, opts MkdirOptions) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// MknodAt implements FilesystemImpl.MknodAt.
func (fs *anonFilesystem) MknodAt(ctx context.Context, rp *ResolvingPath, opts MknodOptions) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// OpenAt implements FilesystemImpl.OpenAt.
func (fs *anonFilesystem) OpenAt(ctx context.Context, rp *ResolvingPath, opts OpenOptions) (*FileDescription, error) {
if !rp.Done() {
return nil, syserror.ENOTDIR
}
return nil, syserror.ENODEV
}
// ReadlinkAt implements FilesystemImpl.ReadlinkAt.
func (fs *anonFilesystem) ReadlinkAt(ctx context.Context, rp *ResolvingPath) (string, error) {
if !rp.Done() {
return "", syserror.ENOTDIR
}
return "", syserror.EINVAL
}
// RenameAt implements FilesystemImpl.RenameAt.
func (fs *anonFilesystem) RenameAt(ctx context.Context, rp *ResolvingPath, oldParentVD VirtualDentry, oldName string, opts RenameOptions) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// RmdirAt implements FilesystemImpl.RmdirAt.
func (fs *anonFilesystem) RmdirAt(ctx context.Context, rp *ResolvingPath) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// SetStatAt implements FilesystemImpl.SetStatAt.
func (fs *anonFilesystem) SetStatAt(ctx context.Context, rp *ResolvingPath, opts SetStatOptions) error {
if !rp.Done() {
return syserror.ENOTDIR
}
// Linux actually permits anon_inode_inode's metadata to be set, which is
// visible to all users of anon_inode_inode. We just silently ignore
// metadata changes.
return nil
}
// StatAt implements FilesystemImpl.StatAt.
func (fs *anonFilesystem) StatAt(ctx context.Context, rp *ResolvingPath, opts StatOptions) (linux.Statx, error) {
if !rp.Done() {
return linux.Statx{}, syserror.ENOTDIR
}
// See fs/anon_inodes.c:anon_inode_init() => fs/libfs.c:alloc_anon_inode().
return linux.Statx{
Mask: linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_NLINK | linux.STATX_UID | linux.STATX_GID | linux.STATX_INO | linux.STATX_SIZE | linux.STATX_BLOCKS,
Blksize: anonfsBlockSize,
Nlink: 1,
UID: uint32(auth.RootKUID),
GID: uint32(auth.RootKGID),
Mode: 0600, // no type is correct
Ino: 1,
Size: 0,
Blocks: 0,
DevMajor: 0,
DevMinor: fs.devMinor,
}, nil
}
// StatFSAt implements FilesystemImpl.StatFSAt.
func (fs *anonFilesystem) StatFSAt(ctx context.Context, rp *ResolvingPath) (linux.Statfs, error) {
if !rp.Done() {
return linux.Statfs{}, syserror.ENOTDIR
}
return linux.Statfs{
Type: linux.ANON_INODE_FS_MAGIC,
BlockSize: anonfsBlockSize,
}, nil
}
// SymlinkAt implements FilesystemImpl.SymlinkAt.
func (fs *anonFilesystem) SymlinkAt(ctx context.Context, rp *ResolvingPath, target string) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// UnlinkAt implements FilesystemImpl.UnlinkAt.
func (fs *anonFilesystem) UnlinkAt(ctx context.Context, rp *ResolvingPath) error {
if !rp.Final() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// ListxattrAt implements FilesystemImpl.ListxattrAt.
func (fs *anonFilesystem) ListxattrAt(ctx context.Context, rp *ResolvingPath) ([]string, error) {
if !rp.Done() {
return nil, syserror.ENOTDIR
}
return nil, nil
}
// GetxattrAt implements FilesystemImpl.GetxattrAt.
func (fs *anonFilesystem) GetxattrAt(ctx context.Context, rp *ResolvingPath, name string) (string, error) {
if !rp.Done() {
return "", syserror.ENOTDIR
}
return "", syserror.ENOTSUP
}
// SetxattrAt implements FilesystemImpl.SetxattrAt.
func (fs *anonFilesystem) SetxattrAt(ctx context.Context, rp *ResolvingPath, opts SetxattrOptions) error {
if !rp.Done() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// RemovexattrAt implements FilesystemImpl.RemovexattrAt.
func (fs *anonFilesystem) RemovexattrAt(ctx context.Context, rp *ResolvingPath, name string) error {
if !rp.Done() {
return syserror.ENOTDIR
}
return syserror.EPERM
}
// PrependPath implements FilesystemImpl.PrependPath.
func (fs *anonFilesystem) PrependPath(ctx context.Context, vfsroot, vd VirtualDentry, b *fspath.Builder) error {
b.PrependComponent(fmt.Sprintf("anon_inode:%s", vd.dentry.impl.(*anonDentry).name))
return PrependPathSyntheticError{}
}
// IncRef implements DentryImpl.IncRef.
func (d *anonDentry) IncRef() {
// no-op
}
// TryIncRef implements DentryImpl.TryIncRef.
func (d *anonDentry) TryIncRef() bool {
return true
}
// DecRef implements DentryImpl.DecRef.
func (d *anonDentry) DecRef() {
// no-op
}
@@ -24,7 +24,6 @@ import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/sentry/contexttest"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/syserror"
"gvisor.dev/gvisor/pkg/usermem"
)
@@ -46,9 +45,11 @@ type genCountFD struct {
count uint64 // accessed using atomic memory ops
}
func newGenCountFD(mnt *Mount, vfsd *Dentry) *FileDescription {
func newGenCountFD(vfsObj *VirtualFilesystem) *FileDescription {
vd := vfsObj.NewAnonVirtualDentry("genCountFD")
defer vd.DecRef()
var fd genCountFD
fd.vfsfd.Init(&fd, 0 /* statusFlags */, mnt, vfsd, &FileDescriptionOptions{})
fd.vfsfd.Init(&fd, 0 /* statusFlags */, vd.Mount(), vd.Dentry(), &FileDescriptionOptions{})
fd.DynamicBytesFileDescriptionImpl.SetDataSource(&fd)
return &fd.vfsfd
}
@@ -86,18 +87,9 @@ func (fd *genCountFD) Generate(ctx context.Context, buf *bytes.Buffer) error {
func TestGenCountFD(t *testing.T) {
ctx := contexttest.Context(t)
creds := auth.CredentialsFromContext(ctx)
vfsObj := New() // vfs.New()
vfsObj.MustRegisterFilesystemType("testfs", FDTestFilesystemType{}, &RegisterFilesystemTypeOptions{})
mntns, err := vfsObj.NewMountNamespace(ctx, creds, "", "testfs", &GetFilesystemOptions{})
if err != nil {
t.Fatalf("failed to create testfs root mount: %v", err)
}
vd := mntns.Root()
defer vd.DecRef()
fd := newGenCountFD(vd.Mount(), vd.Dentry())
fd := newGenCountFD(vfsObj)
defer fd.DecRef()
// The first read causes Generate to be called to fill the FD's buffer.
-173
View File
@@ -1,173 +0,0 @@
// Copyright 2019 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 vfs
import (
"fmt"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/fspath"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/syserror"
)
// FDTestFilesystemType is a test-only FilesystemType that produces Filesystems
// for which all FilesystemImpl methods taking a path return EPERM. It is used
// to produce Mounts and Dentries for testing of FileDescriptionImpls that do
// not depend on their originating Filesystem.
type FDTestFilesystemType struct{}
// FDTestFilesystem is a test-only FilesystemImpl produced by
// FDTestFilesystemType.
type FDTestFilesystem struct {
vfsfs Filesystem
}
// GetFilesystem implements FilesystemType.GetFilesystem.
func (fstype FDTestFilesystemType) GetFilesystem(ctx context.Context, vfsObj *VirtualFilesystem, creds *auth.Credentials, source string, opts GetFilesystemOptions) (*Filesystem, *Dentry, error) {
var fs FDTestFilesystem
fs.vfsfs.Init(vfsObj, &fs)
return &fs.vfsfs, fs.NewDentry(), nil
}
// Release implements FilesystemImpl.Release.
func (fs *FDTestFilesystem) Release() {
}
// Sync implements FilesystemImpl.Sync.
func (fs *FDTestFilesystem) Sync(ctx context.Context) error {
return nil
}
// GetDentryAt implements FilesystemImpl.GetDentryAt.
func (fs *FDTestFilesystem) GetDentryAt(ctx context.Context, rp *ResolvingPath, opts GetDentryOptions) (*Dentry, error) {
return nil, syserror.EPERM
}
// GetParentDentryAt implements FilesystemImpl.GetParentDentryAt.
func (fs *FDTestFilesystem) GetParentDentryAt(ctx context.Context, rp *ResolvingPath) (*Dentry, error) {
return nil, syserror.EPERM
}
// LinkAt implements FilesystemImpl.LinkAt.
func (fs *FDTestFilesystem) LinkAt(ctx context.Context, rp *ResolvingPath, vd VirtualDentry) error {
return syserror.EPERM
}
// MkdirAt implements FilesystemImpl.MkdirAt.
func (fs *FDTestFilesystem) MkdirAt(ctx context.Context, rp *ResolvingPath, opts MkdirOptions) error {
return syserror.EPERM
}
// MknodAt implements FilesystemImpl.MknodAt.
func (fs *FDTestFilesystem) MknodAt(ctx context.Context, rp *ResolvingPath, opts MknodOptions) error {
return syserror.EPERM
}
// OpenAt implements FilesystemImpl.OpenAt.
func (fs *FDTestFilesystem) OpenAt(ctx context.Context, rp *ResolvingPath, opts OpenOptions) (*FileDescription, error) {
return nil, syserror.EPERM
}
// ReadlinkAt implements FilesystemImpl.ReadlinkAt.
func (fs *FDTestFilesystem) ReadlinkAt(ctx context.Context, rp *ResolvingPath) (string, error) {
return "", syserror.EPERM
}
// RenameAt implements FilesystemImpl.RenameAt.
func (fs *FDTestFilesystem) RenameAt(ctx context.Context, rp *ResolvingPath, oldParentVD VirtualDentry, oldName string, opts RenameOptions) error {
return syserror.EPERM
}
// RmdirAt implements FilesystemImpl.RmdirAt.
func (fs *FDTestFilesystem) RmdirAt(ctx context.Context, rp *ResolvingPath) error {
return syserror.EPERM
}
// SetStatAt implements FilesystemImpl.SetStatAt.
func (fs *FDTestFilesystem) SetStatAt(ctx context.Context, rp *ResolvingPath, opts SetStatOptions) error {
return syserror.EPERM
}
// StatAt implements FilesystemImpl.StatAt.
func (fs *FDTestFilesystem) StatAt(ctx context.Context, rp *ResolvingPath, opts StatOptions) (linux.Statx, error) {
return linux.Statx{}, syserror.EPERM
}
// StatFSAt implements FilesystemImpl.StatFSAt.
func (fs *FDTestFilesystem) StatFSAt(ctx context.Context, rp *ResolvingPath) (linux.Statfs, error) {
return linux.Statfs{}, syserror.EPERM
}
// SymlinkAt implements FilesystemImpl.SymlinkAt.
func (fs *FDTestFilesystem) SymlinkAt(ctx context.Context, rp *ResolvingPath, target string) error {
return syserror.EPERM
}
// UnlinkAt implements FilesystemImpl.UnlinkAt.
func (fs *FDTestFilesystem) UnlinkAt(ctx context.Context, rp *ResolvingPath) error {
return syserror.EPERM
}
// ListxattrAt implements FilesystemImpl.ListxattrAt.
func (fs *FDTestFilesystem) ListxattrAt(ctx context.Context, rp *ResolvingPath) ([]string, error) {
return nil, syserror.EPERM
}
// GetxattrAt implements FilesystemImpl.GetxattrAt.
func (fs *FDTestFilesystem) GetxattrAt(ctx context.Context, rp *ResolvingPath, name string) (string, error) {
return "", syserror.EPERM
}
// SetxattrAt implements FilesystemImpl.SetxattrAt.
func (fs *FDTestFilesystem) SetxattrAt(ctx context.Context, rp *ResolvingPath, opts SetxattrOptions) error {
return syserror.EPERM
}
// RemovexattrAt implements FilesystemImpl.RemovexattrAt.
func (fs *FDTestFilesystem) RemovexattrAt(ctx context.Context, rp *ResolvingPath, name string) error {
return syserror.EPERM
}
// PrependPath implements FilesystemImpl.PrependPath.
func (fs *FDTestFilesystem) PrependPath(ctx context.Context, vfsroot, vd VirtualDentry, b *fspath.Builder) error {
b.PrependComponent(fmt.Sprintf("vfs.fdTestDentry:%p", vd.dentry.impl.(*fdTestDentry)))
return PrependPathSyntheticError{}
}
type fdTestDentry struct {
vfsd Dentry
}
// NewDentry returns a new Dentry.
func (fs *FDTestFilesystem) NewDentry() *Dentry {
var d fdTestDentry
d.vfsd.Init(&d)
return &d.vfsd
}
// IncRef implements DentryImpl.IncRef.
func (d *fdTestDentry) IncRef() {
}
// TryIncRef implements DentryImpl.TryIncRef.
func (d *fdTestDentry) TryIncRef() bool {
return true
}
// DecRef implements DentryImpl.DecRef.
func (d *fdTestDentry) DecRef() {
}
+24
View File
@@ -75,6 +75,14 @@ type VirtualFilesystem struct {
// mountpoints is analogous to Linux's mountpoint_hashtable.
mountpoints map[*Dentry]map[*Mount]struct{}
// anonMount is a Mount, not included in mounts or mountpoints,
// representing an anonFilesystem. anonMount is used to back
// VirtualDentries returned by VirtualFilesystem.NewAnonVirtualDentry().
// anonMount is immutable.
//
// anonMount is analogous to Linux's anon_inode_mnt.
anonMount *Mount
// devices contains all registered Devices. devices is protected by
// devicesMu.
devicesMu sync.RWMutex
@@ -110,6 +118,22 @@ func New() *VirtualFilesystem {
filesystems: make(map[*Filesystem]struct{}),
}
vfs.mounts.Init()
// Construct vfs.anonMount.
anonfsDevMinor, err := vfs.GetAnonBlockDevMinor()
if err != nil {
panic(fmt.Sprintf("VirtualFilesystem.GetAnonBlockDevMinor() failed during VirtualFilesystem construction: %v", err))
}
anonfs := anonFilesystem{
devMinor: anonfsDevMinor,
}
anonfs.vfsfs.Init(vfs, &anonfs)
vfs.anonMount = &Mount{
vfs: vfs,
fs: &anonfs.vfsfs,
refs: 1,
}
return vfs
}