Skip to content

Instantly share code, notes, and snippets.

@gwisp2
Created February 21, 2023 15:14
Show Gist options
  • Select an option

  • Save gwisp2/aec6b92922d6e2252263e95ce507bc98 to your computer and use it in GitHub Desktop.

Select an option

Save gwisp2/aec6b92922d6e2252263e95ce507bc98 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("No path to executable to start provided\n");
return 255;
}
if (syscall(SYS_close_range, 3U, ~0U, 0U)) {
printf("Failed to close file descriptors: %s\n", strerror(errno));
return 255;
}
if (execv(argv[1], &argv[1])) {
printf("Exec %s failed: %s\n", argv[1], strerror(errno));
return 255;
}
return 0;
}
@gwisp2
Copy link
Author

gwisp2 commented Feb 21, 2023

Linux kernel 5.9 or newer is required because close_range syscall is used. However, the same idea could be rewritten without using the newest kernel features.

How to build:

gcc -O3 cleanexec.c -o cleanexec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment