Skip to content

Instantly share code, notes, and snippets.

@lcfr-eth
Created October 10, 2024 18:10
Show Gist options
  • Select an option

  • Save lcfr-eth/02f540cac0d4dacf207493cd21e8ba9e to your computer and use it in GitHub Desktop.

Select an option

Save lcfr-eth/02f540cac0d4dacf207493cd21e8ba9e to your computer and use it in GitHub Desktop.
/*
$ ./a
Parent: 76922
Child: 76923, trying to trace parent: 76922
ptrace: Operation not permitted
failed. Errno: 1
Parent: rip children
*/
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main() {
pid_t parent, child;
parent = getpid();
printf("Parent: %d\n", parent);
child = fork();
if (child == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (child == 0) {
printf("Child: %d, trying to trace parent: %d\n", getpid(), parent);
if (ptrace(PTRACE_ATTACH, parent, NULL, NULL) == -1) {
perror("ptrace");
printf("failed. Errno: %d\n", errno);
} else {
printf("child did the thing!\n");
ptrace(PTRACE_DETACH, parent, NULL, NULL);
}
exit(EXIT_SUCCESS);
} else {
wait(NULL);
printf("Parent: rip children\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment