github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/fsimpl/mqfs/root.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mqfs
    16  
    17  import (
    18  	"github.com/nicocha30/gvisor-ligolo/pkg/abi/linux"
    19  	"github.com/nicocha30/gvisor-ligolo/pkg/context"
    20  	"github.com/nicocha30/gvisor-ligolo/pkg/errors/linuxerr"
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/fsimpl/kernfs"
    22  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/kernel/auth"
    23  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/vfs"
    24  )
    25  
    26  // rootInode represents inode for filesystem's root directory (/dev/mqueue).
    27  //
    28  // +stateify savable
    29  type rootInode struct {
    30  	rootInodeRefs
    31  	kernfs.InodeAlwaysValid
    32  	kernfs.InodeAnonymous
    33  	kernfs.InodeAttrs
    34  	kernfs.InodeDirectoryNoNewChildren
    35  	kernfs.InodeNotSymlink
    36  	kernfs.InodeTemporary
    37  	kernfs.InodeWatches
    38  	kernfs.OrderedChildren
    39  
    40  	locks vfs.FileLocks
    41  }
    42  
    43  var _ kernfs.Inode = (*rootInode)(nil)
    44  
    45  // newRootInode returns a new, initialized rootInode.
    46  func (fs *filesystem) newRootInode(ctx context.Context, creds *auth.Credentials) kernfs.Inode {
    47  	inode := &rootInode{}
    48  	inode.InodeAttrs.Init(ctx, creds, linux.UNNAMED_MAJOR, fs.devMinor, fs.NextIno(), linux.ModeDirectory|linux.FileMode(0555))
    49  	inode.OrderedChildren.Init(kernfs.OrderedChildrenOptions{Writable: true})
    50  	inode.InitRefs()
    51  	return inode
    52  }
    53  
    54  // Open implements kernfs.Inode.Open.
    55  func (i *rootInode) Open(ctx context.Context, rp *vfs.ResolvingPath, d *kernfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
    56  	fd, err := kernfs.NewGenericDirectoryFD(rp.Mount(), d, &i.OrderedChildren, &i.locks, &opts, kernfs.GenericDirectoryFDOptions{
    57  		SeekEnd: kernfs.SeekEndZero,
    58  	})
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return fd.VFSFileDescription(), nil
    63  }
    64  
    65  // DecRef implements kernfs.Inode.DecRef.
    66  func (i *rootInode) DecRef(ctx context.Context) {
    67  	i.rootInodeRefs.DecRef(func() { i.Destroy(ctx) })
    68  }
    69  
    70  // Rename implements Inode.Rename and overrides OrderedChildren.Rename. mqueue
    71  // filesystem allows files to be unlinked, but not renamed.
    72  func (i *rootInode) Rename(ctx context.Context, oldname, newname string, child, dstDir kernfs.Inode) error {
    73  	return linuxerr.EPERM
    74  }
    75  
    76  // SetStat implements kernfs.Inode.SetStat not allowing inode attributes to be changed.
    77  func (*rootInode) SetStat(context.Context, *vfs.Filesystem, *auth.Credentials, vfs.SetStatOptions) error {
    78  	return linuxerr.EPERM
    79  }
    80  
    81  // StatFS implements kernfs.Inode.StatFS.
    82  func (*rootInode) StatFS(context.Context, *vfs.Filesystem) (linux.Statfs, error) {
    83  	return vfs.GenericStatFS(linux.MQUEUE_MAGIC), nil
    84  }