github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/proc/yama.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 proc
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"sync/atomic"
    21  
    22  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    23  	"github.com/SagerNet/gvisor/pkg/context"
    24  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    25  	"github.com/SagerNet/gvisor/pkg/hostarch"
    26  	"github.com/SagerNet/gvisor/pkg/sentry/fsimpl/kernfs"
    27  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    28  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/auth"
    29  	"github.com/SagerNet/gvisor/pkg/usermem"
    30  )
    31  
    32  func (fs *filesystem) newYAMAPtraceScopeFile(ctx context.Context, k *kernel.Kernel, creds *auth.Credentials) kernfs.Inode {
    33  	s := &yamaPtraceScope{level: &k.YAMAPtraceScope}
    34  	s.Init(ctx, creds, linux.UNNAMED_MAJOR, fs.devMinor, fs.NextIno(), s, 0644)
    35  	return s
    36  }
    37  
    38  // yamaPtraceScope implements vfs.WritableDynamicBytesSource for
    39  // /sys/kernel/yama/ptrace_scope.
    40  //
    41  // +stateify savable
    42  type yamaPtraceScope struct {
    43  	kernfs.DynamicBytesFile
    44  
    45  	// level is the ptrace_scope level.
    46  	level *int32
    47  }
    48  
    49  // Generate implements vfs.DynamicBytesSource.Generate.
    50  func (s *yamaPtraceScope) Generate(ctx context.Context, buf *bytes.Buffer) error {
    51  	_, err := fmt.Fprintf(buf, "%d\n", atomic.LoadInt32(s.level))
    52  	return err
    53  }
    54  
    55  // Write implements vfs.WritableDynamicBytesSource.Write.
    56  func (s *yamaPtraceScope) Write(ctx context.Context, src usermem.IOSequence, offset int64) (int64, error) {
    57  	if offset != 0 {
    58  		// Ignore partial writes.
    59  		return 0, linuxerr.EINVAL
    60  	}
    61  	if src.NumBytes() == 0 {
    62  		return 0, nil
    63  	}
    64  
    65  	// Limit the amount of memory allocated.
    66  	src = src.TakeFirst(hostarch.PageSize - 1)
    67  
    68  	var v int32
    69  	n, err := usermem.CopyInt32StringInVec(ctx, src.IO, src.Addrs, &v, src.Opts)
    70  	if err != nil {
    71  		return 0, err
    72  	}
    73  
    74  	// We do not support YAMA levels > YAMA_SCOPE_RELATIONAL.
    75  	if v < linux.YAMA_SCOPE_DISABLED || v > linux.YAMA_SCOPE_RELATIONAL {
    76  		return 0, linuxerr.EINVAL
    77  	}
    78  
    79  	atomic.StoreInt32(s.level, v)
    80  	return n, nil
    81  }