Skip to content

Commit f243789

Browse files
authored
handle ESRCH in more ptrace callsites (#91)
we are seeing processes exit with the following: ``` run process: run_parent03:44 Caused by:03:44 0: handle_syscall pid=11203:44 1: get_target_path03:44 2: ptrace::getregs03:44 3: ESRCH: No such process ``` a traced process can exit at any time so any ptrace calls that operate on the pid must check esrch to see if the traced process has exited and handle it appropriately we already do it in a bunch of places but do it in more
1 parent 3dfa2da commit f243789

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

npm/darwin-arm64/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@replit/ruspty-darwin-arm64",
3-
"version": "3.5.2",
3+
"version": "3.5.3",
44
"os": [
55
"darwin"
66
],

npm/darwin-x64/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@replit/ruspty-darwin-x64",
3-
"version": "3.5.2",
3+
"version": "3.5.3",
44
"os": [
55
"darwin"
66
],

npm/linux-x64-gnu/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@replit/ruspty-linux-x64-gnu",
3-
"version": "3.5.2",
3+
"version": "3.5.3",
44
"os": [
55
"linux"
66
],

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@replit/ruspty",
3-
"version": "3.5.2",
3+
"version": "3.5.3",
44
"main": "dist/wrapper.js",
55
"types": "dist/wrapper.d.ts",
66
"author": "Szymon Kaliski <hi@szymonkaliski.com>",

src/sandbox.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ struct SyscallTarget {
8686

8787
/// Get the tracee's target path for the syscall that is about to be executed by the kernel.
8888
fn get_syscall_targets(pid: Pid) -> Result<Vec<SyscallTarget>> {
89-
let regs = ptrace::getregs(pid).context("ptrace::getregs")?;
89+
let regs = match ptrace::getregs(pid) {
90+
Ok(regs) => regs,
91+
Err(Error::ESRCH) => return Ok(vec![]), // process gone, no targets
92+
Err(err) => return Err(err).context("ptrace::getregs"),
93+
};
9094
if regs.rax != (-(Error::ENOSYS as i32)) as u64 {
9195
// This is a syscall-exit-stop, and we have already made the decision of allowing / denying the operation.
9296
return Ok(vec![]);
@@ -483,7 +487,16 @@ fn run_parent(main_pid: Pid, options: &Options) -> Result<i32> {
483487
return Err(err).context("ptrace::setoptions");
484488
}
485489
}
486-
ptrace::syscall(main_pid, None).context("Failed continue process")?;
490+
match ptrace::syscall(main_pid, None) {
491+
Ok(_) => {}
492+
Err(Error::ESRCH) => {
493+
// The child process has already exited.
494+
return Ok(0);
495+
}
496+
Err(err) => {
497+
return Err(err).context("failed to continue process");
498+
}
499+
}
487500

488501
loop {
489502
match wait() {
@@ -514,16 +527,21 @@ fn run_parent(main_pid: Pid, options: &Options) -> Result<i32> {
514527
}
515528
signum @ Signal::SIGSTOP => {
516529
debug!(signal:?=signum, pid:? = pid; "signal");
517-
ptrace::setoptions(
530+
match ptrace::setoptions(
518531
pid,
519532
ptrace::Options::PTRACE_O_TRACESYSGOOD
520533
| ptrace::Options::PTRACE_O_TRACEFORK
521534
| ptrace::Options::PTRACE_O_TRACEVFORK
522535
| ptrace::Options::PTRACE_O_TRACECLONE
523536
| ptrace::Options::PTRACE_O_EXITKILL
524537
| ptrace::Options::PTRACE_O_TRACEEXIT,
525-
)
526-
.context("setoptions")?;
538+
) {
539+
Ok(_) => {}
540+
Err(Error::ESRCH) => {}
541+
Err(err) => {
542+
return Err(anyhow::Error::new(err).context("setoptions"));
543+
}
544+
}
527545
match ptrace::syscall(pid, None) {
528546
Ok(_) => {}
529547
Err(Error::ESRCH) => {}

0 commit comments

Comments
 (0)