linux/samples/seccomp/bpf-helper.h
<<
>>
Prefs
   1/*
   2 * Example wrapper around BPF macros.
   3 *
   4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
   5 * Author: Will Drewry <wad@chromium.org>
   6 *
   7 * The code may be used by anyone for any purpose,
   8 * and can serve as a starting point for developing
   9 * applications using prctl(PR_SET_SECCOMP, 2, ...).
  10 *
  11 * No guarantees are provided with respect to the correctness
  12 * or functionality of this code.
  13 */
  14#ifndef __BPF_HELPER_H__
  15#define __BPF_HELPER_H__
  16
  17#include <asm/bitsperlong.h>    /* for __BITS_PER_LONG */
  18#include <endian.h>
  19#include <linux/filter.h>
  20#include <linux/seccomp.h>      /* for seccomp_data */
  21#include <linux/types.h>
  22#include <linux/unistd.h>
  23#include <stddef.h>
  24
  25#define BPF_LABELS_MAX 256
  26struct bpf_labels {
  27        int count;
  28        struct __bpf_label {
  29                const char *label;
  30                __u32 location;
  31        } labels[BPF_LABELS_MAX];
  32};
  33
  34int bpf_resolve_jumps(struct bpf_labels *labels,
  35                      struct sock_filter *filter, size_t count);
  36__u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label);
  37void seccomp_bpf_print(struct sock_filter *filter, size_t count);
  38
  39#define JUMP_JT 0xff
  40#define JUMP_JF 0xff
  41#define LABEL_JT 0xfe
  42#define LABEL_JF 0xfe
  43
  44#define ALLOW \
  45        BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
  46#define DENY \
  47        BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
  48#define JUMP(labels, label) \
  49        BPF_JUMP(BPF_JMP+BPF_JA, FIND_LABEL((labels), (label)), \
  50                 JUMP_JT, JUMP_JF)
  51#define LABEL(labels, label) \
  52        BPF_JUMP(BPF_JMP+BPF_JA, FIND_LABEL((labels), (label)), \
  53                 LABEL_JT, LABEL_JF)
  54#define SYSCALL(nr, jt) \
  55        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (nr), 0, 1), \
  56        jt
  57
  58/* Lame, but just an example */
  59#define FIND_LABEL(labels, label) seccomp_bpf_label((labels), #label)
  60
  61#define EXPAND(...) __VA_ARGS__
  62/* Map all width-sensitive operations */
  63#if __BITS_PER_LONG == 32
  64
  65#define JEQ(x, jt) JEQ32(x, EXPAND(jt))
  66#define JNE(x, jt) JNE32(x, EXPAND(jt))
  67#define JGT(x, jt) JGT32(x, EXPAND(jt))
  68#define JLT(x, jt) JLT32(x, EXPAND(jt))
  69#define JGE(x, jt) JGE32(x, EXPAND(jt))
  70#define JLE(x, jt) JLE32(x, EXPAND(jt))
  71#define JA(x, jt) JA32(x, EXPAND(jt))
  72#define ARG(i) ARG_32(i)
  73#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
  74
  75#elif __BITS_PER_LONG == 64
  76
  77/* Ensure that we load the logically correct offset. */
  78#if __BYTE_ORDER == __LITTLE_ENDIAN
  79#define ENDIAN(_lo, _hi) _lo, _hi
  80#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
  81#define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
  82#elif __BYTE_ORDER == __BIG_ENDIAN
  83#define ENDIAN(_lo, _hi) _hi, _lo
  84#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
  85#define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
  86#else
  87#error "Unknown endianness"
  88#endif
  89
  90union arg64 {
  91        struct {
  92                __u32 ENDIAN(lo32, hi32);
  93        };
  94        __u64 u64;
  95};
  96
  97#define JEQ(x, jt) \
  98        JEQ64(((union arg64){.u64 = (x)}).lo32, \
  99              ((union arg64){.u64 = (x)}).hi32, \
 100              EXPAND(jt))
 101#define JGT(x, jt) \
 102        JGT64(((union arg64){.u64 = (x)}).lo32, \
 103              ((union arg64){.u64 = (x)}).hi32, \
 104              EXPAND(jt))
 105#define JGE(x, jt) \
 106        JGE64(((union arg64){.u64 = (x)}).lo32, \
 107              ((union arg64){.u64 = (x)}).hi32, \
 108              EXPAND(jt))
 109#define JNE(x, jt) \
 110        JNE64(((union arg64){.u64 = (x)}).lo32, \
 111              ((union arg64){.u64 = (x)}).hi32, \
 112              EXPAND(jt))
 113#define JLT(x, jt) \
 114        JLT64(((union arg64){.u64 = (x)}).lo32, \
 115              ((union arg64){.u64 = (x)}).hi32, \
 116              EXPAND(jt))
 117#define JLE(x, jt) \
 118        JLE64(((union arg64){.u64 = (x)}).lo32, \
 119              ((union arg64){.u64 = (x)}).hi32, \
 120              EXPAND(jt))
 121
 122#define JA(x, jt) \
 123        JA64(((union arg64){.u64 = (x)}).lo32, \
 124               ((union arg64){.u64 = (x)}).hi32, \
 125               EXPAND(jt))
 126#define ARG(i) ARG_64(i)
 127
 128#else
 129#error __BITS_PER_LONG value unusable.
 130#endif
 131
 132/* Loads the arg into A */
 133#define ARG_32(idx) \
 134        BPF_STMT(BPF_LD+BPF_W+BPF_ABS, LO_ARG(idx))
 135
 136/* Loads hi into A and lo in X */
 137#define ARG_64(idx) \
 138        BPF_STMT(BPF_LD+BPF_W+BPF_ABS, LO_ARG(idx)), \
 139        BPF_STMT(BPF_ST, 0), /* lo -> M[0] */ \
 140        BPF_STMT(BPF_LD+BPF_W+BPF_ABS, HI_ARG(idx)), \
 141        BPF_STMT(BPF_ST, 1) /* hi -> M[1] */
 142
 143#define JEQ32(value, jt) \
 144        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (value), 0, 1), \
 145        jt
 146
 147#define JNE32(value, jt) \
 148        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (value), 1, 0), \
 149        jt
 150
 151/* Checks the lo, then swaps to check the hi. A=lo,X=hi */
 152#define JEQ64(lo, hi, jt) \
 153        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
 154        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 155        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (lo), 0, 2), \
 156        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 157        jt, \
 158        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 159
 160#define JNE64(lo, hi, jt) \
 161        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 5, 0), \
 162        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 163        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (lo), 2, 0), \
 164        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 165        jt, \
 166        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 167
 168#define JA32(value, jt) \
 169        BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, (value), 0, 1), \
 170        jt
 171
 172#define JA64(lo, hi, jt) \
 173        BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, (hi), 3, 0), \
 174        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 175        BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, (lo), 0, 2), \
 176        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 177        jt, \
 178        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 179
 180#define JGE32(value, jt) \
 181        BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (value), 0, 1), \
 182        jt
 183
 184#define JLT32(value, jt) \
 185        BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (value), 1, 0), \
 186        jt
 187
 188/* Shortcut checking if hi > arg.hi. */
 189#define JGE64(lo, hi, jt) \
 190        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (hi), 4, 0), \
 191        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
 192        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 193        BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (lo), 0, 2), \
 194        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 195        jt, \
 196        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 197
 198#define JLT64(lo, hi, jt) \
 199        BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (hi), 0, 4), \
 200        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
 201        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 202        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (lo), 2, 0), \
 203        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 204        jt, \
 205        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 206
 207#define JGT32(value, jt) \
 208        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (value), 0, 1), \
 209        jt
 210
 211#define JLE32(value, jt) \
 212        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (value), 1, 0), \
 213        jt
 214
 215/* Check hi > args.hi first, then do the GE checking */
 216#define JGT64(lo, hi, jt) \
 217        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (hi), 4, 0), \
 218        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
 219        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 220        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (lo), 0, 2), \
 221        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 222        jt, \
 223        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 224
 225#define JLE64(lo, hi, jt) \
 226        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (hi), 6, 0), \
 227        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 3), \
 228        BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
 229        BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (lo), 2, 0), \
 230        BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
 231        jt, \
 232        BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
 233
 234#define LOAD_SYSCALL_NR \
 235        BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
 236                 offsetof(struct seccomp_data, nr))
 237
 238#endif  /* __BPF_HELPER_H__ */
 239
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.