-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Fixed ptrace 'Operation not permitted' #15287
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,14 +54,14 @@ char *linux_reg_profile (RDebug *dbg) { | |
|
||
static void linux_detach_all (RDebug *dbg); | ||
static char *read_link (int pid, const char *file); | ||
static int linux_attach_single_pid (RDebug *dbg, int ptid); | ||
static bool linux_attach_single_pid (RDebug *dbg, int ptid); | ||
static void linux_attach_all (RDebug *dbg); | ||
static void linux_remove_thread (RDebug *dbg, int pid); | ||
static void linux_add_and_attach_new_thread (RDebug *dbg, int tid); | ||
static int linux_stop_process(int pid); | ||
|
||
int linux_handle_signals (RDebug *dbg) { | ||
siginfo_t siginfo = {0}; | ||
siginfo_t siginfo = { 0 }; | ||
int ret = r_debug_ptrace (dbg, PTRACE_GETSIGINFO, dbg->pid, 0, (r_ptrace_data_t)(size_t)&siginfo); | ||
if (ret == -1) { | ||
/* ESRCH means the process already went away :-/ */ | ||
|
@@ -243,6 +243,7 @@ bool linux_set_options(RDebug *dbg, int pid) { | |
/* SIGTRAP | 0x80 on signal handler .. not supported on all archs */ | ||
traceflags |= PTRACE_O_TRACESYSGOOD; | ||
if (r_debug_ptrace (dbg, PTRACE_SETOPTIONS, pid, 0, (r_ptrace_data_t)(size_t)traceflags) == -1) { | ||
perror ("ptrace (PT_SETOPTIONS)"); | ||
return false; | ||
} | ||
return true; | ||
|
@@ -408,10 +409,26 @@ static int linux_stop_process(int pid) { | |
return ret == pid; | ||
} | ||
|
||
static int linux_attach_single_pid(RDebug *dbg, int ptid) { | ||
linux_set_options (dbg, ptid); | ||
return r_debug_ptrace (dbg, PTRACE_ATTACH, ptid, NULL, | ||
(r_ptrace_data_t)(size_t)NULL); | ||
static bool linux_attach_single_pid(RDebug *dbg, int ptid) { | ||
siginfo_t sig = {0}; | ||
// Safely check if the PID has already been attached to avoid printing errors. | ||
// Attaching to a process that has already been started with PTRACE_TRACEME. | ||
// sets errno to "Operation not permitted" which may be misleading. | ||
// GETSIGINFO can be called multiple times and would fail without attachment. | ||
if (-1 == r_debug_ptrace(dbg, PTRACE_GETSIGINFO, ptid, NULL, | ||
(r_ptrace_data_t)&sig)) { | ||
if (-1 == r_debug_ptrace (dbg, PTRACE_ATTACH, ptid, NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
(r_ptrace_data_t)NULL)) { | ||
perror ("ptrace (PT_ATTACH)"); | ||
return false; | ||
} | ||
} | ||
|
||
if (!linux_set_options (dbg, ptid)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static RList *get_pid_thread_list (RDebug *dbg, int main_pid) { | ||
|
@@ -424,21 +441,16 @@ static RList *get_pid_thread_list (RDebug *dbg, int main_pid) { | |
} | ||
|
||
static void linux_attach_all (RDebug *dbg) { | ||
int ret = linux_attach_single_pid (dbg, dbg->main_pid); | ||
if (ret != -1) { | ||
perror ("ptrace (PT_ATTACH)"); | ||
} | ||
linux_attach_single_pid (dbg, dbg->main_pid); | ||
|
||
RList *list = dbg->threads; | ||
if (list) { | ||
RDebugPid *th; | ||
RListIter *it; | ||
r_list_foreach (list, it, th) { | ||
if (th->pid && th->pid != dbg->main_pid) { | ||
ret = linux_attach_single_pid (dbg, th->pid); | ||
if (ret == -1) { | ||
if (!linux_attach_single_pid (dbg, th->pid)) { | ||
eprintf ("PID %d\n", th->pid); | ||
perror ("ptrace (PT_ATTACH)"); | ||
} | ||
} | ||
} | ||
|
@@ -456,10 +468,7 @@ int linux_attach(RDebug *dbg, int pid) { | |
if (dbg->threads && !r_list_find (dbg->threads, &pid, &match_pid)) { | ||
goto out; | ||
} | ||
int ret = linux_attach_single_pid (dbg, pid); | ||
if (ret == -1) { | ||
perror ("ptrace (PT_ATTACH)"); | ||
} | ||
linux_attach_single_pid (dbg, pid); | ||
} | ||
out: | ||
return pid; | ||
|
@@ -881,7 +890,6 @@ int linux_reg_read(RDebug *dbg, int type, ut8 *buf, int size) { | |
ret = r_debug_ptrace (dbg, PTRACE_GETREGS, pid, ®s, NULL); | ||
#else | ||
/* linux -{arm/mips/riscv/x86/x86_64} */ | ||
eprintf ("REG READ\n"); | ||
ret = r_debug_ptrace (dbg, PTRACE_GETREGS, pid, NULL, ®s); | ||
#endif | ||
/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space before ( and piut the == -1 in the right side of the statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated