Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscall.Ptrace* always start in new thread, causing the tracee untraceable #43685

Closed
jm33-m0 opened this issue Jan 14, 2021 · 1 comment
Closed

Comments

@jm33-m0
Copy link

jm33-m0 commented Jan 14, 2021

What version of Go are you using (go version)?

$ go version
go version go1.13.8 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""  
GOARCH="amd64"
GOBIN=""    
GOCACHE="/home/jm33/.cache/go-build"                                   
GOENV="/home/jm33/.config/go/env"                                      
GOEXE=""    
GOFLAGS=""                                             
GOHOSTARCH="amd64"       
GOHOSTOS="linux"        
GONOPROXY=""
GONOSUMDB=""                                           
GOOS="linux" 
GOPATH="/home/jm33/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"                              
GOROOT="/usr/lib/go-1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""        
GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64"                      
GCCGO="gccgo"        
AR="ar"            
CC="gcc"            
CXX="g++"              
CGO_ENABLED="1"                                                                                                
GOMOD=""             
CGO_CFLAGS="-g -O2"                                                    
CGO_CPPFLAGS=""                                                                                                
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"                                                    
CGO_LDFLAGS="-g -O2"                                                   
PKG_CONFIG="pkg-config"                                                
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build432196136=/tmp/go-build -gno-record-gcc-switches"

What did you do?

// attach to a running process
err = syscall.PtraceAttach(pid)
if err != nil {
	return fmt.Errorf("ptrace attach: %v", err)		
}

// read registers
regs := &syscall.PtraceRegs{}
err = syscall.PtraceGetRegs(pid, regs)
if err != nil {
	return fmt.Errorf("my pid is %d, reading regs from %d: %v", os.Getpid(), pid, err)
}

What did you expect to see?

Registers are successfully returned

What did you see instead?

my pid is 4018, reading regs from 4028: no such process from syscall.PtraceGetRegs(pid, regs)

The issue

  • The process I was trying to attach to is attachable, as syscall.PtraceAttach(pid) returns no error
  • Checking the /proc/$(pidof tracee)/status, found a TracerPid matching a non-existent process, I think it's a thread (which has already exited) of my go program
  • Putting runtime.LockOSThread() at the beginning of my function doesn't solve this issue, TracerPid still doesn't match my current go process, which it should
@jm33-m0 jm33-m0 changed the title syscall.Ptrace* always start in new thread, causing the tracee untracable syscall.Ptrace* always start in new thread, causing the tracee untraceable Jan 14, 2021
@jm33-m0
Copy link
Author

jm33-m0 commented Jan 14, 2021

Adding:

runtime.LockOSThread()
defer runtime.UnlockOSThread()

right before syscall.PtraceAttach(pid) (instead of at the top of calling function) seems to fix this issue

@jm33-m0 jm33-m0 closed this as completed Jan 14, 2021
@golang golang locked and limited conversation to collaborators Jan 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants