1

i am trying to inject a library in android using PTRACE but when i try to attach ptrace to specific pid for monitoring got an error/; "Cannot attach to pid"... Here is the code

// Attach 
    if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
        printf("cannot attach to %d, error!\n", pid);
        exit(1);
    }
    waitpid(pid, NULL, 0);

i want to know why ptrace() is returning value less than zero causing error.... what should be returned by the ptrace() function to execute normally(error free)

Naveen
  • 31
  • 8
  • unless you're attaching to your child, you'll most likely need to be root at this point – Mark Segal Sep 26 '17 at 21:56
  • i am using rooted bluestack for testing plus also has given rights to the lib by chmod 777. i have set ptrace_scope =0 but no success.....therefore i am asking that if this error is for something other than root? – Naveen Sep 28 '17 at 03:39
  • `chmod 777` has nothing to do with it. You need to be root yourself to attach to process which isn't your child (the exact rules are quite more complicated, in some situations you can attach to other processes sharing your UID but in Android - its usually the case that you need to be root) – Mark Segal Sep 28 '17 at 17:08
  • this solved my issue thankyou – Naveen Sep 29 '17 at 05:51
  • than please accept my answer – Mark Segal Sep 29 '17 at 12:29
  • i have accepted but due to reputation less than 15 its recorded but not visible – Naveen Sep 30 '17 at 13:29

1 Answers1

0

In Android, to attach to a process which isn't your child and does not have the same UID as you do, you have to be root or have the appropriate capability.

Mark Segal
  • 5,427
  • 4
  • 31
  • 69