linux/samples/seccomp/bpf-helper.h

/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Example wrapper around BPF macros.
 *
 * Copyright (c) 2012 The Chromium OS Authors <[email protected]>
 * Author: Will Drewry <[email protected]>
 *
 * The code may be used by anyone for any purpose,
 * and can serve as a starting point for developing
 * applications using prctl(PR_SET_SECCOMP, 2, ...).
 *
 * No guarantees are provided with respect to the correctness
 * or functionality of this code.
 */
#ifndef __BPF_HELPER_H__
#define __BPF_HELPER_H__

#include <asm/bitsperlong.h>	/* for __BITS_PER_LONG */
#include <endian.h>
#include <linux/filter.h>
#include <linux/seccomp.h>	/* for seccomp_data */
#include <linux/types.h>
#include <linux/unistd.h>
#include <stddef.h>

#define BPF_LABELS_MAX
struct bpf_labels {};

int bpf_resolve_jumps(struct bpf_labels *labels,
		      struct sock_filter *filter, size_t count);
__u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label);
void seccomp_bpf_print(struct sock_filter *filter, size_t count);

#define JUMP_JT
#define JUMP_JF
#define LABEL_JT
#define LABEL_JF

#define ALLOW
#define DENY
#define JUMP(labels, label)
#define LABEL(labels, label)
#define SYSCALL(nr, jt)

/* Lame, but just an example */
#define FIND_LABEL(labels, label)

#define EXPAND(...)

/* Ensure that we load the logically correct offset. */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LO_ARG(idx)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define LO_ARG
#else
#error "Unknown endianness"
#endif

/* Map all width-sensitive operations */
#if __BITS_PER_LONG == 32

#define JEQ
#define JNE
#define JGT
#define JLT
#define JGE
#define JLE
#define JA
#define ARG

#elif __BITS_PER_LONG == 64

/* Ensure that we load the logically correct offset. */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define ENDIAN(_lo, _hi)
#define HI_ARG(idx)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define ENDIAN
#define HI_ARG
#endif

arg64;

#define JEQ(x, jt)
#define JGT(x, jt)
#define JGE(x, jt)
#define JNE(x, jt)
#define JLT(x, jt)
#define JLE(x, jt)

#define JA(x, jt)
#define ARG(i)

#else
#error __BITS_PER_LONG value unusable.
#endif

/* Loads the arg into A */
#define ARG_32(idx)

/* Loads lo into M[0] and hi into M[1] and A */
#define ARG_64(idx)

#define JEQ32(value, jt)

#define JNE32(value, jt)

#define JA32(value, jt)

#define JGE32(value, jt)

#define JGT32(value, jt)

#define JLE32(value, jt)

#define JLT32(value, jt)

/*
 * All the JXX64 checks assume lo is saved in M[0] and hi is saved in both
 * A and M[1]. This invariant is kept by restoring A if necessary.
 */
#define JEQ64(lo, hi, jt)

#define JNE64(lo, hi, jt)

#define JA64(lo, hi, jt)

#define JGE64(lo, hi, jt)

#define JGT64(lo, hi, jt)

#define JLE64(lo, hi, jt)

#define JLT64(lo, hi, jt)

#define LOAD_SYSCALL_NR

#endif  /* __BPF_HELPER_H__ */