Created
July 22, 2016 23:56
-
-
Save dm0-/526e8f21b07f4d1d089b58de43cdfcfe to your computer and use it in GitHub Desktop.
Run a program without the ability to delete files
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
/* cc -o noodel -lseccomp noodel.c */ | |
#include <seccomp.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int | |
main (int argc, char *argv[]) | |
{ | |
scmp_filter_ctx ctx; | |
int rc = -1; | |
if (argc < 2 || access (argv[1], X_OK) != 0) | |
{ | |
fprintf (stderr, "%s /path/to/program [args]", argv[0]); | |
return 1; | |
} | |
/* Initialize libseccomp to allow everything by default. */ | |
ctx = seccomp_init (SCMP_ACT_ALLOW); | |
if (ctx == NULL) | |
goto abort; | |
/* Define the set of syscall filtering rules: mask unlink. */ | |
rc = seccomp_rule_add (ctx, SCMP_ACT_ERRNO (0), SCMP_SYS (unlink), 0); | |
if (rc != 0) | |
goto abort; | |
/* Load the rules into the kernel, then release their resources here. */ | |
rc = seccomp_load (ctx); | |
if (rc == 0) | |
seccomp_release (ctx); | |
else | |
goto abort; | |
/* Switch to the real target process. */ | |
rc = execv (argv[1], argv + 1); | |
abort: | |
seccomp_release (ctx); | |
return -rc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment