Return helpful error when attaching to a process is impossible

This commit is contained in:
aarzilli 2015-06-27 11:42:58 +02:00 committed by Derek Parker
parent 29ed169848
commit 358fb75fc6
3 changed files with 41 additions and 1 deletions

@ -46,7 +46,7 @@ func New(config *Config) (*Debugger, error) {
log.Printf("attaching to pid %d", d.config.AttachPid)
p, err := proc.Attach(d.config.AttachPid)
if err != nil {
return nil, fmt.Errorf("could not attach to pid %d: %s", d.config.AttachPid, err)
return nil, attachErrorMessage(d.config.AttachPid, err)
}
d.process = p
} else {

@ -0,0 +1,10 @@
package debugger
import (
"fmt"
)
func attachErrorMessage(pid int, err error) error {
//TODO: mention certificates?
return fmt.Errorf("could not attach to pid %d: %s", pid, err)
}

@ -0,0 +1,30 @@
package debugger
import (
"fmt"
"io/ioutil"
"os"
"syscall"
)
func attachErrorMessage(pid int, err error) error {
fallbackerr := fmt.Errorf("could not attach to pid %d: %s", pid, err)
if serr, ok := err.(syscall.Errno); ok {
switch serr {
case syscall.EPERM:
bs, err := ioutil.ReadFile("/proc/sys/kernel/yama/ptrace_scope")
if err == nil && len(bs) >= 1 && bs[0] != '0' {
// Yama documentation: https://www.kernel.org/doc/Documentation/security/Yama.txt
return fmt.Errorf("Could not attach to pid %d: set /proc/sys/kernel/yama/ptrace_scope to 0", pid)
}
fi, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
if err != nil {
return fallbackerr
}
if fi.Sys().(*syscall.Stat_t).Uid != uint32(os.Getuid()) {
return fmt.Errorf("Could not attach to pid %d: current user does not own the process", pid)
}
}
}
return fallbackerr
}