2015-06-27 09:42:58 +00:00
package debugger
import (
"fmt"
"os"
"syscall"
)
2022-09-21 16:48:35 +00:00
func init ( ) {
attachErrorMessage = attachErrorMessageLinux
}
2021-09-28 19:07:42 +00:00
//lint:file-ignore ST1005 errors here can be capitalized
2022-09-21 16:48:35 +00:00
func attachErrorMessageLinux ( pid int , err error ) error {
2015-06-27 09:42:58 +00:00
fallbackerr := fmt . Errorf ( "could not attach to pid %d: %s" , pid , err )
if serr , ok := err . ( syscall . Errno ) ; ok {
switch serr {
case syscall . EPERM :
2023-09-25 18:41:59 +00:00
bs , err := os . ReadFile ( "/proc/sys/kernel/yama/ptrace_scope" )
2015-06-27 09:42:58 +00:00
if err == nil && len ( bs ) >= 1 && bs [ 0 ] != '0' {
// Yama documentation: https://www.kernel.org/doc/Documentation/security/Yama.txt
2018-08-18 09:21:28 +00:00
return fmt . Errorf ( "Could not attach to pid %d: this could be caused by a kernel security setting, try writing \"0\" to /proc/sys/kernel/yama/ptrace_scope" , pid )
2015-06-27 09:42:58 +00:00
}
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
}