github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/context"
    20  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/auth"
    22  	"github.com/SagerNet/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  	InodeSymlink
    33  	InodeNoStatFS
    34  
    35  	target string
    36  }
    37  
    38  var _ Inode = (*StaticSymlink)(nil)
    39  
    40  // NewStaticSymlink creates a new symlink file pointing to 'target'.
    41  func NewStaticSymlink(ctx context.Context, creds *auth.Credentials, devMajor, devMinor uint32, ino uint64, target string) Inode {
    42  	inode := &StaticSymlink{}
    43  	inode.Init(ctx, creds, devMajor, devMinor, ino, target)
    44  	return inode
    45  }
    46  
    47  // Init initializes the instance.
    48  func (s *StaticSymlink) Init(ctx context.Context, creds *auth.Credentials, devMajor uint32, devMinor uint32, ino uint64, target string) {
    49  	s.target = target
    50  	s.InodeAttrs.Init(ctx, creds, devMajor, devMinor, ino, linux.ModeSymlink|0777)
    51  }
    52  
    53  // Readlink implements Inode.Readlink.
    54  func (s *StaticSymlink) Readlink(_ context.Context, _ *vfs.Mount) (string, error) {
    55  	return s.target, nil
    56  }
    57  
    58  // Getlink implements Inode.Getlink.
    59  func (s *StaticSymlink) Getlink(context.Context, *vfs.Mount) (vfs.VirtualDentry, string, error) {
    60  	return vfs.VirtualDentry{}, s.target, nil
    61  }
    62  
    63  // SetStat implements Inode.SetStat not allowing inode attributes to be changed.
    64  func (*StaticSymlink) SetStat(context.Context, *vfs.Filesystem, *auth.Credentials, vfs.SetStatOptions) error {
    65  	return linuxerr.EPERM
    66  }