github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/fsimpl/kernfs/symlink.go (about)

     1  // Copyright 2019 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 kernfs
    16  
    17  import (
    18  	"github.com/MerlinKodo/gvisor/pkg/abi/linux"
    19  	"github.com/MerlinKodo/gvisor/pkg/context"
    20  	"github.com/MerlinKodo/gvisor/pkg/errors/linuxerr"
    21  	"github.com/MerlinKodo/gvisor/pkg/sentry/kernel/auth"
    22  	"github.com/MerlinKodo/gvisor/pkg/sentry/vfs"
    23  )
    24  
    25  // StaticSymlink provides an Inode implementation for symlinks that point to
    26  // a immutable target.
    27  //
    28  // +stateify savable
    29  type StaticSymlink struct {
    30  	InodeAttrs
    31  	InodeNoopRefCount
    32  	InodeNotAnonymous
    33  	InodeSymlink
    34  	InodeNoStatFS
    35  	InodeWatches
    36  
    37  	target string
    38  }
    39  
    40  var _ Inode = (*StaticSymlink)(nil)
    41  
    42  // NewStaticSymlink creates a new symlink file pointing to 'target'.
    43  func NewStaticSymlink(ctx context.Context, creds *auth.Credentials, devMajor, devMinor uint32, ino uint64, target string) Inode {
    44  	inode := &StaticSymlink{}
    45  	inode.Init(ctx, creds, devMajor, devMinor, ino, target)
    46  	return inode
    47  }
    48  
    49  // Init initializes the instance.
    50  func (s *StaticSymlink) Init(ctx context.Context, creds *auth.Credentials, devMajor uint32, devMinor uint32, ino uint64, target string) {
    51  	s.target = target
    52  	s.InodeAttrs.Init(ctx, creds, devMajor, devMinor, ino, linux.ModeSymlink|0777)
    53  }
    54  
    55  // Readlink implements Inode.Readlink.
    56  func (s *StaticSymlink) Readlink(_ context.Context, _ *vfs.Mount) (string, error) {
    57  	return s.target, nil
    58  }
    59  
    60  // Getlink implements Inode.Getlink.
    61  func (s *StaticSymlink) Getlink(context.Context, *vfs.Mount) (vfs.VirtualDentry, string, error) {
    62  	return vfs.VirtualDentry{}, s.target, nil
    63  }
    64  
    65  // SetStat implements Inode.SetStat not allowing inode attributes to be changed.
    66  func (*StaticSymlink) SetStat(context.Context, *vfs.Filesystem, *auth.Credentials, vfs.SetStatOptions) error {
    67  	return linuxerr.EPERM
    68  }