linux/samples/seccomp/bpf-fancy.c
<<
> > p/spa pspa class="lxr_search"> > ="+search" method="post" onsubmit="return do_search(this);"> > > > Search > p/spa > ="ajax+*" method="post" onsubmit="return false;"> pinput typ"v2hidden" nam"v2ajax_lookup" idv2ajax_lookup" lue="v2"> >
pdiv idv2file_contents"
o o1p/a>pspa
 class="comment">/*p/spa
  o o2p/a>pspa
 class="comment"> * Seccomp BPF example using a macro-based generator.p/spa
  o o3p/a>pspa
 class="comment"> *p/spa
  o o4p/a>pspa
 class="comment"> * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>p/spa
  o o5p/a>pspa
 class="comment"> * Author: Will Drewry <wad@chromium.org>p/spa
  o o6p/a>pspa
 class="comment"> *p/spa
  o o7p/a>pspa
 class="comment"> * The code may be used by anyone for any purpose,p/spa
  o o8p/a>pspa
 class="comment"> * and ca
 serve as a starting point for developingp/spa
  o o9p/a>pspa
 class="comment"> * applicaon>
s using prctl(PR_ATTACH_SECCOMP_FILTER).p/spa
  o pspa
 class="comment"> */p/spa
  o 11p/a> o 12p/a>#include <linux/filter.hp/a>> o 13p/a>#include <linux/seccomp.hp/a>> o 14p/a>#include <linux/unistd.hp/a>> o 15p/a>#include <stdio.hp/a>> o 16p/a>#include <string.hp/a>> o 17p/a>#include <sys/prctl.hp/a>> o 18p/a>#include <unistd.hp/a>> o 19p/a> o 20p/a>#include "bpf-helper.hp/a>" o 21p/a> o 22p/a>#ifndefopa href="+code=PR_SET_NO_NEW_PRIVS" class="sref">PR_SET_NO_NEW_PRIVSp/a> o 23p/a>#defineopa href="+code=PR_SET_NO_NEW_PRIVS" class="sref">PR_SET_NO_NEW_PRIVSp/a> 38 o 24p/a>#endif o 25p/a> o 26p/a>int pa href="+code=main" class="sref">mainp/a>(int pa href="+code=argc" class="sref">argcp/a>, char **pa href="+code=argv" class="sref">argvp/a>) o 27p/a>{ o 28p/a>        struct pa href="+code=bpf_labels" class="sref">bpf_labelsp/a> pa href="+code=l" class="sref">lp/a>; o 29p/a>        static const char pa href="+code=msg1" class="sref">msg1p/a>[] = pspa
 class="string">"Please typ" something: "o 30p/a>        static const char pa href="+code=msg2" class="sref">msg2p/a>[] = pspa
 class="string">"You typ"d: "o 31p/a>        char pa href="+code=buf" class="sref">bufp/a>[256]; o 32p/a>        struct pa href="+code=sock_filter" class="sref">sock_filterp/a> pa href="+code=filter" class="sref">filterp/a>[] = { o 33p/a>                pspa
 class="comment">/* TODO: LOAD_SYSCALL_NR(arch) and enforce an arch */p/spa
  o 34p/a>                pa href="+code=LOAD_SYSCALL_NR" class="sref">LOAD_SYSCALL_NRp/a>, o 35p/a>                pa href="+code=SYSCALL" class="sref">SYSCALLp/a>(pa href="+code=__NR_exit" class="sref">__NR_exitp/a>, pa href="+code=ALLOW" class="sref">ALLOWp/a>), o 36p/a>                pa href="+code=SYSCALL" class="sref">SYSCALLp/a>(pa href="+code=__NR_exit_group" class="sref">__NR_exit_groupp/a>, pa href="+code=ALLOW" class="sref">ALLOWp/a>), o 37p/a>                pa href="+code=SYSCALL" class="sref">SYSCALLp/a>(pa href="+code=__NR_write" class="sref">__NR_writep/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=write_fd" class="sref">write_fdp/a>)), o 38p/a>                pa href="+code=SYSCALL" class="sref">SYSCALLp/a>(pa href="+code=__NR_read" class="sref">__NR_readp/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=read" class="sref">readp/a>)), o 39p/a>                pa href="+code=DENY" class="sref">DENYp/a>,  pspa
 class="comment">/* Don't passthrough into a label */p/spa
  o 40p/a> o 41p/a>                pa href="+code=LABEL" class="sref">LABELp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=read" class="sref">readp/a>), o 42p/a>                pa href="+code=ARG" class="sref">ARGp/a>(0), o 43p/a>                pa href="+code=JNE" class="sref">JNEp/a>(pa href="+code=STDIN_FILENO" class="sref">STDIN_FILENOp/a>, pa href="+code=DENY" class="sref">DENYp/a>), o 44p/a>                pa href="+code=ARG" class="sref">ARGp/a>(1), o 45p/a>                pa href="+code=JNE" class="sref">JNEp/a>((unsigned long)pa href="+code=buf" class="sref">bufp/a>, pa href="+code=DENY" class="sref">DENYp/a>), o 46p/a>                pa href="+code=ARG" class="sref">ARGp/a>(2), o 47p/a>                pa href="+code=JGE" class="sref">JGEp/a>(sizeof(pa href="+code=buf" class="sref">bufp/a>), pa href="+code=DENY" class="sref">DENYp/a>), o 48p/a>                pa href="+code=ALLOW" class="sref">ALLOWp/a>, o 49p/a> o 50p/a>                pa href="+code=LABEL" class="sref">LABELp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=write_fd" class="sref">write_fdp/a>), o 51p/a>                pa href="+code=ARG" class="sref">ARGp/a>(0), o 52p/a>                pa href="+code=JEQ" class="sref">JEQp/a>(pa href="+code=STDOUT_FILENO" class="sref">STDOUT_FILENOp/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=write_buf" class="sref">write_bufp/a>)), o 53p/a>                pa href="+code=JEQ" class="sref">JEQp/a>(pa href="+code=STDERR_FILENO" class="sref">STDERR_FILENOp/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=write_buf" class="sref">write_bufp/a>)), o 54p/a>                pa href="+code=DENY" class="sref">DENYp/a>, o 55p/a> o 56p/a>                pa href="+code=LABEL" class="sref">LABELp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=write_buf" class="sref">write_bufp/a>), o 57p/a>                pa href="+code=ARG" class="sref">ARGp/a>(1), o 58p/a>                pa href="+code=JEQ" class="sref">JEQp/a>((unsigned long)pa href="+code=msg1" class="sref">msg1p/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=msg1_len" class="sref">msg1_lenp/a>)), o 59p/a>                pa href="+code=JEQ" class="sref">JEQp/a>((unsigned long)pa href="+code=msg2" class="sref">msg2p/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=msg2_len" class="sref">msg2_lenp/a>)), o 60p/a>                pa href="+code=JEQ" class="sref">JEQp/a>((unsigned long)pa href="+code=buf" class="sref">bufp/a>, pa href="+code=JUMP" class="sref">JUMPp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=buf_len" class="sref">buf_lenp/a>)), o 61p/a>                pa href="+code=DENY" class="sref">DENYp/a>, o 62p/a> o 63p/a>                pa href="+code=LABEL" class="sref">LABELp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=msg1_len" class="sref">msg1_lenp/a>), o 64p/a>                pa href="+code=ARG" class="sref">ARGp/a>(2), o 65p/a>                pa href="+code=JLT" class="sref">JLTp/a>(sizeof(pa href="+code=msg1" class="sref">msg1p/a>), pa href="+code=ALLOW" class="sref">ALLOWp/a>), o 66p/a>                pa href="+code=DENY" class="sref">DENYp/a>, o 67p/a> o 68p/a>                pa href="+code=LABEL" class="sref">LABELp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=msg2_len" class="sref">msg2_lenp/a>), o 69p/a>                pa href="+code=ARG" class="sref">ARGp/a>(2), o 70p/a>                pa href="+code=JLT" class="sref">JLTp/a>(sizeof(pa href="+code=msg2" class="sref">msg2p/a>), pa href="+code=ALLOW" class="sref">ALLOWp/a>), o 71p/a>                pa href="+code=DENY" class="sref">DENYp/a>, o 72p/a> o 73p/a>                pa href="+code=LABEL" class="sref">LABELp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=buf_len" class="sref">buf_lenp/a>), o 74p/a>                pa href="+code=ARG" class="sref">ARGp/a>(2), o 75p/a>                pa href="+code=JLT" class="sref">JLTp/a>(sizeof(pa href="+code=buf" class="sref">bufp/a>), pa href="+code=ALLOW" class="sref">ALLOWp/a>), o 76p/a>                pa href="+code=DENY" class="sref">DENYp/a>, o 77p/a>        }; o 78p/a>        struct pa href="+code=sock_fprog" class="sref">sock_fprogp/a> pa href="+code=prog" class="sref">progp/a> = { o 79p/a>                .pa href="+code=filter" class="sref">filterp/a> = pa href="+code=filter" class="sref">filterp/a>, o 80p/a>                .pa href="+code=len" class="sref">lenp/a> = (unsigned short)(sizeof(pa href="+code=filter" class="sref">filterp/a>)/sizeof(pa href="+code=filter" class="sref">filterp/a>[0])), o 81p/a>        }; o 82p/a>        pa href="+code=ssize_t" class="sref">ssize_tp/a> pa href="+code=bytes" class="sref">bytesp/a>; o 83p/a>        pa href="+code=bpf_resolve_jumps" class="sref">bpf_resolve_jumpsp/a>(&pa href="+code=l" class="sref">lp/a>, pa href="+code=filter" class="sref">filterp/a>, sizeof(pa href="+code=filter" class="sref">filterp/a>)/sizeof(*pa href="+code=filter" class="sref">filterp/a>)); o 84p/a> o 85p/a>        if (pa href="+code=prctl" class="sref">prctlp/a>(pa href="+code=PR_SET_NO_NEW_PRIVS" class="sref">PR_SET_NO_NEW_PRIVSp/a>, 1, 0, 0, 0)) { o 86p/a>                pa href="+code=perror" class="sref">perrorp/a>(pspa
 class="string">"prctl(NO_NEW_PRIVS)"o 87p/a>                return 1; o 88p/a>        } o 89p/a> o 90p/a>        if (pa href="+code=prctl" class="sref">prctlp/a>(pa href="+code=PR_SET_SECCOMP" class="sref">PR_SET_SECCOMPp/a>, pa href="+code=SECCOMP_MODE_FILTER" class="sref">SECCOMP_MODE_FILTERp/a>, &pa href="+code=prog" class="sref">progp/a>)) { o 91p/a>                pa href="+code=perror" class="sref">perrorp/a>(pspa
 class="string">"prctl(SECCOMP)"o 92p/a>                return 1; o 93p/a>        } o 94p/a>        pa href="+code=syscall" class="sref">syscallp/a>(pa href="+code=__NR_write" class="sref">__NR_writep/a>, pa href="+code=STDOUT_FILENO" class="sref">STDOUT_FILENOp/a>, pa href="+code=msg1" class="sref">msg1p/a>, pa href="+code=strlen" class="sref">strlenp/a>(pa href="+code=msg1" class="sref">msg1p/a>)); o 95p/a>        pa href="+code=bytes" class="sref">bytesp/a> = pa href="+code=syscall" class="sref">syscallp/a>(pa href="+code=__NR_read" class="sref">__NR_readp/a>, pa href="+code=STDIN_FILENO" class="sref">STDIN_FILENOp/a>, pa href="+code=buf" class="sref">bufp/a>, sizeof(pa href="+code=buf" class="sref">bufp/a>)-1); o 96p/a>        pa href="+code=bytes" class="sref">bytesp/a> = (pa href="+code=bytes" class="sref">bytesp/a> > 0 ? pa href="+code=bytes" class="sref">bytesp/a> : 0); o 97p/a>        pa href="+code=syscall" class="sref">syscallp/a>(pa href="+code=__NR_write" class="sref">__NR_writep/a>, pa href="+code=STDERR_FILENO" class="sref">STDERR_FILENOp/a>, pa href="+code=msg2" class="sref">msg2p/a>, pa href="+code=strlen" class="sref">strlenp/a>(pa href="+code=msg2" class="sref">msg2p/a>)); o 98p/a>        pa href="+code=syscall" class="sref">syscallp/a>(pa href="+code=__NR_write" class="sref">__NR_writep/a>, pa href="+code=STDERR_FILENO" class="sref">STDERR_FILENOp/a>, pa href="+code=buf" class="sref">bufp/a>, pa href="+code=bytes" class="sref">bytesp/a>); o 99p/a>        pspa
 class="comment">/* Now get killed */p/spa
  o100p/a>        pa href="+code=syscall" class="sref">syscallp/a>(pa href="+code=__NR_write" class="sref">__NR_writep/a>, pa href="+code=STDERR_FILENO" class="sref">STDERR_FILENOp/a>, pa href="+code=msg2" class="sref">msg2p/a>, pa href="+code=strlen" class="sref">strlenp/a>(pa href="+code=msg2" class="sref">msg2p/a>)+2); o101p/a>        return 0; o102p/a>} o103p/a>p/pre>
The original LXR software by theopa href="http://sourceforge.net/projects/lxr">LXR communityp/a>, this experimental versn vaby pa href="mailto:lxr@linux.no">lxr@linux.nop/a>. p/div pdiv class="subfooter"> lxr.linux.no kindly hosted by pa href="http://www.redpill-linpro.no">Redpill Linpro ASp/a>, provider of Linux consulting and operatn> s services since 1995. p/div p/body p/html