validate process isn't already being scoped
@@ -72,6 +72,7 @@ func (rc *Config) Run(args []string) {
// Attach scopes an existing PID
func (rc *Config) Attach(args []string) {
+ // Validate user has root permissions
user, err := user.Current()
if err != nil {
util.ErrAndExit("Unable to get current user: %v", err)
@@ -79,6 +80,7 @@ func (rc *Config) Attach(args []string) {
if user.Uid != "0" {
util.ErrAndExit("You must have administrator privileges to attach to a process")
}
+ // Validate PTRACE capability
c, err := capability.NewPid2(0)
if err != nil {
util.ErrAndExit("Unable to get linux capabilities for current process: %v", err)
@@ -90,6 +92,20 @@ func (rc *Config) Attach(args []string) {
if !c.Get(capability.EFFECTIVE, capability.CAP_SYS_PTRACE) {
util.ErrAndExit("You must have PTRACE capabilities to attach to a process")
}
+ // Validate PID exists
+ pidPath := fmt.Sprintf("/proc/%s", args[0])
+ if !util.CheckDirExists(pidPath) {
+ util.ErrAndExit("PID does not exist: \"%s\"", args[0])
+ }
+ // Validate the process isn't already being scoped
+ pidMapFile, err := ioutil.ReadFile(pidPath + "/maps")
+ if err != nil {
+ util.ErrAndExit("Map does not exist for PID: \"%s\"", args[0])
+ }
+ pidMap := string(pidMapFile)
+ if strings.Contains(pidMap, "libscope") {
+ util.ErrAndExit("Attach failed. This process is already being scoped")
+ }
if err := createLdscope(); err != nil {
util.ErrAndExit("error creating ldscope: %v", err)
}
@@ -110,10 +126,6 @@ func (rc *Config) Attach(args []string) {
env = append(env, "SCOPE_CONF_PATH="+filepath.Join(rc.WorkDir, "scope.yml"))
log.Info().Bool("passthrough", rc.Passthrough).Strs("args", args).Msg("calling syscall.Exec")
}
- // Validate PID exists
- if !util.CheckDirExists(fmt.Sprintf("/proc/%s", args[0])) {
- util.ErrAndExit("PID does not exist: \"%s\"", args[0])
- }
// Prepend "--attach" to args
args = append([]string{"--attach"}, args...)
if !rc.Subprocess {
