github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/kernel/ptrace_amd64.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  //go:build amd64
    16  // +build amd64
    17  
    18  package kernel
    19  
    20  import (
    21  	"github.com/ttpreport/gvisor-ligolo/pkg/abi/linux"
    22  	"github.com/ttpreport/gvisor-ligolo/pkg/errors/linuxerr"
    23  	"github.com/ttpreport/gvisor-ligolo/pkg/hostarch"
    24  	"github.com/ttpreport/gvisor-ligolo/pkg/usermem"
    25  )
    26  
    27  // ptraceArch implements arch-specific ptrace commands.
    28  func (t *Task) ptraceArch(target *Task, req int64, addr, data hostarch.Addr) error {
    29  	switch req {
    30  	case linux.PTRACE_PEEKUSR: // aka PTRACE_PEEKUSER
    31  		n, err := target.Arch().PtracePeekUser(uintptr(addr))
    32  		if err != nil {
    33  			return err
    34  		}
    35  		_, err = n.CopyOut(t, data)
    36  		return err
    37  
    38  	case linux.PTRACE_POKEUSR: // aka PTRACE_POKEUSER
    39  		return target.Arch().PtracePokeUser(uintptr(addr), uintptr(data))
    40  
    41  	case linux.PTRACE_GETREGS:
    42  		// "Copy the tracee's general-purpose ... registers ... to the address
    43  		// data in the tracer. ... (addr is ignored.) Note that SPARC systems
    44  		// have the meaning of data and addr reversed ..."
    45  		_, err := target.Arch().PtraceGetRegs(&usermem.IOReadWriter{
    46  			Ctx:  t,
    47  			IO:   t.MemoryManager(),
    48  			Addr: data,
    49  			Opts: usermem.IOOpts{
    50  				AddressSpaceActive: true,
    51  			},
    52  		})
    53  		return err
    54  
    55  	case linux.PTRACE_GETFPREGS:
    56  		s := target.Arch().FloatingPointData()
    57  		_, err := target.Arch().FloatingPointData().PtraceGetFPRegs(&usermem.IOReadWriter{
    58  			Ctx:  t,
    59  			IO:   t.MemoryManager(),
    60  			Addr: data,
    61  			Opts: usermem.IOOpts{
    62  				AddressSpaceActive: true,
    63  			},
    64  		}, len(*s))
    65  		return err
    66  
    67  	case linux.PTRACE_SETREGS:
    68  		_, err := target.Arch().PtraceSetRegs(&usermem.IOReadWriter{
    69  			Ctx:  t,
    70  			IO:   t.MemoryManager(),
    71  			Addr: data,
    72  			Opts: usermem.IOOpts{
    73  				AddressSpaceActive: true,
    74  			},
    75  		})
    76  		if err == nil {
    77  			target.p.FullStateChanged()
    78  		}
    79  		return err
    80  
    81  	case linux.PTRACE_SETFPREGS:
    82  		s := target.Arch().FloatingPointData()
    83  		_, err := s.PtraceSetFPRegs(&usermem.IOReadWriter{
    84  			Ctx:  t,
    85  			IO:   t.MemoryManager(),
    86  			Addr: data,
    87  			Opts: usermem.IOOpts{
    88  				AddressSpaceActive: true,
    89  			},
    90  		}, len(*s))
    91  		if err == nil {
    92  			target.p.FullStateChanged()
    93  		}
    94  		return err
    95  
    96  	default:
    97  		return linuxerr.EIO
    98  	}
    99  }