github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/syscalls/linux/sys_utsname.go (about)

     1  // Copyright 2018 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 linux
    16  
    17  import (
    18  	"github.com/metacubex/gvisor/pkg/abi/linux"
    19  	"github.com/metacubex/gvisor/pkg/errors/linuxerr"
    20  	"github.com/metacubex/gvisor/pkg/sentry/arch"
    21  	"github.com/metacubex/gvisor/pkg/sentry/kernel"
    22  )
    23  
    24  // Uname implements linux syscall uname.
    25  func Uname(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    26  	version := t.SyscallTable().Version
    27  
    28  	uts := t.UTSNamespace()
    29  
    30  	// Fill in structure fields.
    31  	var u linux.UtsName
    32  	copy(u.Sysname[:], version.Sysname)
    33  	copy(u.Nodename[:], uts.HostName())
    34  	copy(u.Release[:], version.Release)
    35  	copy(u.Version[:], version.Version)
    36  	// build tag above.
    37  	switch t.SyscallTable().Arch {
    38  	case arch.AMD64:
    39  		copy(u.Machine[:], "x86_64")
    40  	case arch.ARM64:
    41  		copy(u.Machine[:], "aarch64")
    42  	default:
    43  		copy(u.Machine[:], "unknown")
    44  	}
    45  	copy(u.Domainname[:], uts.DomainName())
    46  
    47  	// Copy out the result.
    48  	va := args[0].Pointer()
    49  	_, err := u.CopyOut(t, va)
    50  	return 0, nil, err
    51  }
    52  
    53  // Setdomainname implements Linux syscall setdomainname.
    54  func Setdomainname(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    55  	nameAddr := args[0].Pointer()
    56  	size := args[1].Int()
    57  
    58  	utsns := t.UTSNamespace()
    59  	if !t.HasCapabilityIn(linux.CAP_SYS_ADMIN, utsns.UserNamespace()) {
    60  		return 0, nil, linuxerr.EPERM
    61  	}
    62  	if size < 0 || size > linux.UTSLen {
    63  		return 0, nil, linuxerr.EINVAL
    64  	}
    65  
    66  	name, err := t.CopyInString(nameAddr, int(size))
    67  	if err != nil {
    68  		return 0, nil, err
    69  	}
    70  
    71  	utsns.SetDomainName(name)
    72  	return 0, nil, nil
    73  }
    74  
    75  // Sethostname implements Linux syscall sethostname.
    76  func Sethostname(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    77  	nameAddr := args[0].Pointer()
    78  	size := args[1].Int()
    79  
    80  	utsns := t.UTSNamespace()
    81  	if !t.HasCapabilityIn(linux.CAP_SYS_ADMIN, utsns.UserNamespace()) {
    82  		return 0, nil, linuxerr.EPERM
    83  	}
    84  	if size < 0 || size > linux.UTSLen {
    85  		return 0, nil, linuxerr.EINVAL
    86  	}
    87  
    88  	name := make([]byte, size)
    89  	if _, err := t.CopyInBytes(nameAddr, name); err != nil {
    90  		return 0, nil, err
    91  	}
    92  
    93  	utsns.SetHostName(string(name))
    94  	return 0, nil, nil
    95  }