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