Skip to content

Instantly share code, notes, and snippets.

@kjelly
Created March 16, 2014 05:57
Show Gist options
  • Save kjelly/9579145 to your computer and use it in GitHub Desktop.
Save kjelly/9579145 to your computer and use it in GitHub Desktop.
libseccomp example. use `gcc test_seccomp.c -lseccomp` to compile
#include <stdio.h>
#include <stdlib.h>
#include <seccomp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
FILE *f1;
int fd;
int ret;
scmp_filter_ctx ctx;
f1 = fopen("/tmp/test1", "w");
ctx = seccomp_init(SCMP_ACT_ERRNO(5));
ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
if (!ret)
ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup), 0);
if (!ret)
ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
if (!ret)
ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0);
if (!ret)
ret = seccomp_load(ctx);
if (ret)
printf("error setting seccomp\n");
fprintf(f1, "hi there\n");
fd = open("/tmp/test2", O_RDWR);
if (fd >= 0)
printf("error, was able to open f2\n");
else
fprintf(f1, "open returned %d errno %d\n", fd, errno);
fclose(f1);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment