gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/platform/systrap/subprocess_amd64.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  //go:build amd64
    16  // +build amd64
    17  
    18  package systrap
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  
    24  	"golang.org/x/sys/unix"
    25  	"gvisor.dev/gvisor/pkg/abi/linux"
    26  	"gvisor.dev/gvisor/pkg/seccomp"
    27  	"gvisor.dev/gvisor/pkg/sentry/arch"
    28  	"gvisor.dev/gvisor/pkg/sentry/platform/systrap/sysmsg"
    29  )
    30  
    31  const (
    32  	// initRegsRipAdjustment is the size of the syscall instruction.
    33  	initRegsRipAdjustment = 2
    34  )
    35  
    36  // resetSysemuRegs sets up emulation registers.
    37  //
    38  // This should be called prior to calling sysemu.
    39  func (s *subprocess) resetSysemuRegs(regs *arch.Registers) {
    40  	regs.Cs = s.sysmsgInitRegs.Cs
    41  	regs.Ss = s.sysmsgInitRegs.Ss
    42  	regs.Ds = s.sysmsgInitRegs.Ds
    43  	regs.Es = s.sysmsgInitRegs.Es
    44  	regs.Fs = s.sysmsgInitRegs.Fs
    45  	regs.Gs = s.sysmsgInitRegs.Gs
    46  }
    47  
    48  // createSyscallRegs sets up syscall registers.
    49  //
    50  // This should be called to generate registers for a system call.
    51  func createSyscallRegs(initRegs *arch.Registers, sysno uintptr, args ...arch.SyscallArgument) arch.Registers {
    52  	// Copy initial registers.
    53  	regs := *initRegs
    54  
    55  	// Set our syscall number.
    56  	regs.Rax = uint64(sysno)
    57  	if len(args) >= 1 {
    58  		regs.Rdi = args[0].Uint64()
    59  	}
    60  	if len(args) >= 2 {
    61  		regs.Rsi = args[1].Uint64()
    62  	}
    63  	if len(args) >= 3 {
    64  		regs.Rdx = args[2].Uint64()
    65  	}
    66  	if len(args) >= 4 {
    67  		regs.R10 = args[3].Uint64()
    68  	}
    69  	if len(args) >= 5 {
    70  		regs.R8 = args[4].Uint64()
    71  	}
    72  	if len(args) >= 6 {
    73  		regs.R9 = args[5].Uint64()
    74  	}
    75  
    76  	return regs
    77  }
    78  
    79  // updateSyscallRegs updates registers after finishing sysemu.
    80  func updateSyscallRegs(regs *arch.Registers) {
    81  	// Ptrace puts -ENOSYS in rax on syscall-enter-stop.
    82  	regs.Rax = regs.Orig_rax
    83  }
    84  
    85  // syscallReturnValue extracts a sensible return from registers.
    86  func syscallReturnValue(regs *arch.Registers) (uintptr, error) {
    87  	rval := int64(regs.Rax)
    88  	if rval < 0 {
    89  		return 0, unix.Errno(-rval)
    90  	}
    91  	return uintptr(rval), nil
    92  }
    93  
    94  func dumpRegs(regs *arch.Registers) string {
    95  	var m strings.Builder
    96  
    97  	fmt.Fprintf(&m, "Registers:\n")
    98  	fmt.Fprintf(&m, "\tR15\t = %016x\n", regs.R15)
    99  	fmt.Fprintf(&m, "\tR14\t = %016x\n", regs.R14)
   100  	fmt.Fprintf(&m, "\tR13\t = %016x\n", regs.R13)
   101  	fmt.Fprintf(&m, "\tR12\t = %016x\n", regs.R12)
   102  	fmt.Fprintf(&m, "\tRbp\t = %016x\n", regs.Rbp)
   103  	fmt.Fprintf(&m, "\tRbx\t = %016x\n", regs.Rbx)
   104  	fmt.Fprintf(&m, "\tR11\t = %016x\n", regs.R11)
   105  	fmt.Fprintf(&m, "\tR10\t = %016x\n", regs.R10)
   106  	fmt.Fprintf(&m, "\tR9\t = %016x\n", regs.R9)
   107  	fmt.Fprintf(&m, "\tR8\t = %016x\n", regs.R8)
   108  	fmt.Fprintf(&m, "\tRax\t = %016x\n", regs.Rax)
   109  	fmt.Fprintf(&m, "\tRcx\t = %016x\n", regs.Rcx)
   110  	fmt.Fprintf(&m, "\tRdx\t = %016x\n", regs.Rdx)
   111  	fmt.Fprintf(&m, "\tRsi\t = %016x\n", regs.Rsi)
   112  	fmt.Fprintf(&m, "\tRdi\t = %016x\n", regs.Rdi)
   113  	fmt.Fprintf(&m, "\tOrig_rax = %016x\n", regs.Orig_rax)
   114  	fmt.Fprintf(&m, "\tRip\t = %016x\n", regs.Rip)
   115  	fmt.Fprintf(&m, "\tCs\t = %016x\n", regs.Cs)
   116  	fmt.Fprintf(&m, "\tEflags\t = %016x\n", regs.Eflags)
   117  	fmt.Fprintf(&m, "\tRsp\t = %016x\n", regs.Rsp)
   118  	fmt.Fprintf(&m, "\tSs\t = %016x\n", regs.Ss)
   119  	fmt.Fprintf(&m, "\tFs_base\t = %016x\n", regs.Fs_base)
   120  	fmt.Fprintf(&m, "\tGs_base\t = %016x\n", regs.Gs_base)
   121  	fmt.Fprintf(&m, "\tDs\t = %016x\n", regs.Ds)
   122  	fmt.Fprintf(&m, "\tEs\t = %016x\n", regs.Es)
   123  	fmt.Fprintf(&m, "\tFs\t = %016x\n", regs.Fs)
   124  	fmt.Fprintf(&m, "\tGs\t = %016x\n", regs.Gs)
   125  
   126  	return m.String()
   127  }
   128  
   129  // adjustInitregsRip adjust the current register RIP value to
   130  // be just before the system call instruction execution
   131  func (t *thread) adjustInitRegsRip() {
   132  	t.initRegs.Rip -= initRegsRipAdjustment
   133  }
   134  
   135  // Pass the expected PPID to the child via R15 when creating stub process.
   136  func initChildProcessPPID(initregs *arch.Registers, ppid int32) {
   137  	// Rbx has to be set to 1 when creating stub process.
   138  	initregs.Rbx = _NEW_STUB
   139  }
   140  
   141  // patchSignalInfo patches the signal info to account for hitting the seccomp
   142  // filters from vsyscall emulation, specified below. We allow for SIGSYS as a
   143  // synchronous trap, but patch the structure to appear like a SIGSEGV with the
   144  // Rip as the faulting address.
   145  //
   146  // Note that this should only be called after verifying that the signalInfo has
   147  // been generated by the kernel.
   148  // Returns true if the signal info was patched, false otherwise.
   149  func maybePatchSignalInfo(regs *arch.Registers, signalInfo *linux.SignalInfo) bool {
   150  	if signalInfo.Addr() < linux.VSyscallStartAddr ||
   151  		signalInfo.Addr() >= linux.VSyscallEndAddr {
   152  		return false
   153  	}
   154  	// The syscall event was triggered from vsyscall emulation.
   155  	signalInfo.Signo = int32(linux.SIGSEGV)
   156  
   157  	// Unwind the kernel emulation, if any has occurred. A SIGSYS is delivered
   158  	// with the si_call_addr field pointing to the current RIP. This field
   159  	// aligns with the si_addr field for a SIGSEGV, so we don't need to touch
   160  	// anything there. We do need to unwind emulation however, so we set the
   161  	// instruction pointer to the faulting value, and "unpop" the stack.
   162  	regs.Rip = signalInfo.Addr()
   163  	regs.Rsp -= 8
   164  	return true
   165  }
   166  
   167  // enableCpuidFault enables cpuid-faulting.
   168  //
   169  // This may fail on older kernels or hardware, so we just disregard the result.
   170  // Host CPUID will be enabled.
   171  //
   172  // This is safe to call in an afterFork context.
   173  //
   174  //go:nosplit
   175  //go:norace
   176  func enableCpuidFault() {
   177  	unix.RawSyscall6(unix.SYS_ARCH_PRCTL, linux.ARCH_SET_CPUID, 0, 0, 0, 0, 0)
   178  }
   179  
   180  // appendArchSeccompRules append architecture specific seccomp rules when creating BPF program.
   181  // Ref attachedThread() for more detail.
   182  func appendArchSeccompRules(rules []seccomp.RuleSet) []seccomp.RuleSet {
   183  	return append(rules, []seccomp.RuleSet{
   184  		// Rules for trapping vsyscall access.
   185  		{
   186  			Rules: seccomp.MakeSyscallRules(map[uintptr]seccomp.SyscallRule{
   187  				unix.SYS_GETTIMEOFDAY: seccomp.MatchAll{},
   188  				unix.SYS_TIME:         seccomp.MatchAll{},
   189  				unix.SYS_GETCPU:       seccomp.MatchAll{}, // SYS_GETCPU was not defined in package syscall on amd64.
   190  			}),
   191  			Action:   linux.SECCOMP_RET_TRAP,
   192  			Vsyscall: true,
   193  		},
   194  		{
   195  			Rules: seccomp.MakeSyscallRules(map[uintptr]seccomp.SyscallRule{
   196  				unix.SYS_ARCH_PRCTL: seccomp.Or{
   197  					seccomp.PerArg{seccomp.EqualTo(linux.ARCH_SET_CPUID), seccomp.EqualTo(0)},
   198  					seccomp.PerArg{seccomp.EqualTo(linux.ARCH_SET_FS)},
   199  					seccomp.PerArg{seccomp.EqualTo(linux.ARCH_GET_FS)},
   200  				},
   201  			}),
   202  			Action: linux.SECCOMP_RET_ALLOW,
   203  		},
   204  	}...)
   205  }
   206  
   207  func restoreArchSpecificState(ctx *sysmsg.ThreadContext, ac *arch.Context64) {
   208  }
   209  
   210  func setArchSpecificRegs(sysThread *sysmsgThread, regs *arch.Registers) {
   211  	// Set the start function and initial stack.
   212  	regs.PtraceRegs.Rip = uint64(stubSysmsgStart + uintptr(sysmsg.Sighandler_blob_offset____export_start))
   213  	regs.PtraceRegs.Rsp = uint64(sysmsg.StackAddrToSyshandlerStack(sysThread.sysmsgPerThreadMemAddr()))
   214  
   215  	// Set gs_base; this is the only time we set it and we don't expect it to ever
   216  	// change for any thread.
   217  	regs.Gs_base = sysThread.msg.Self
   218  }
   219  
   220  func retrieveArchSpecificState(ctx *sysmsg.ThreadContext, ac *arch.Context64) {
   221  }
   222  
   223  func archSpecificSysmsgThreadInit(sysThread *sysmsgThread) {
   224  }