<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux/lib/test_bpf.c, branch v4.1</title>
<subtitle>Mirror of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
</subtitle>
<id>https://git.shady.money/linux/atom?h=v4.1</id>
<link rel='self' href='https://git.shady.money/linux/atom?h=v4.1'/>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/'/>
<updated>2014-12-09T01:23:22Z</updated>
<entry>
<title>test: bpf: expand DIV_KX to DIV_MOD_KX</title>
<updated>2014-12-09T01:23:22Z</updated>
<author>
<name>Denis Kirjanov</name>
<email>kda@linux-powerpc.org</email>
</author>
<published>2014-12-01T10:12:25Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=6867b17b26d80cfd419e491141feb75082915979'/>
<id>urn:sha1:6867b17b26d80cfd419e491141feb75082915979</id>
<content type='text'>
Expand DIV_KX to use BPF_MOD operation in the
DIV_KX bpf 'classic' test.

CC: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: Denis Kirjanov &lt;kda@linux-powerpc.org&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>test: bpf: add a testcase reduced from nmap</title>
<updated>2014-10-30T19:44:37Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-10-28T22:11:43Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=e21ab36a80c2e011a14149fecdd654b8b47d3e8c'/>
<id>urn:sha1:e21ab36a80c2e011a14149fecdd654b8b47d3e8c</id>
<content type='text'>
nmap generates classic BPF programs to filter ARP packets with given target MAC
which triggered a bug in eBPF x64 JIT. The bug was fixed in
commit e0ee9c12157d ("x86: bpf_jit: fix two bugs in eBPF JIT compiler")
This patch is adding a testcase in eBPF instructions (those that
were generated by classic-&gt;eBPF converter) to be processed by JIT.
The test is primarily targeting JIT compiler.

Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: bpf: fix compiler warnings in test_bpf</title>
<updated>2014-09-22T20:21:30Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-09-19T20:53:51Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=25ee7327d04bc3ff41a7a5ac42d74226f8d60ac6'/>
<id>urn:sha1:25ee7327d04bc3ff41a7a5ac42d74226f8d60ac6</id>
<content type='text'>
old gcc 4.2 used by avr32 architecture produces warnings:

lib/test_bpf.c:1741: warning: integer constant is too large for 'long' type
lib/test_bpf.c:1741: warning: integer constant is too large for 'long' type
lib/test_bpf.c: In function '__run_one':
lib/test_bpf.c:1897: warning: 'ret' may be used uninitialized in this function

silence these warnings.

Fixes: 02ab695bb37e ("net: filter: add "load 64-bit immediate" eBPF instruction")
Reported-by: Fengguang Wu &lt;fengguang.wu@intel.com&gt;
Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Acked-by: Daniel Borkmann &lt;dborkman@redhat.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: filter: add "load 64-bit immediate" eBPF instruction</title>
<updated>2014-09-09T17:26:47Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-09-05T05:17:17Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=02ab695bb37ee9ad515df0d0790d5977505dd04a'/>
<id>urn:sha1:02ab695bb37ee9ad515df0d0790d5977505dd04a</id>
<content type='text'>
add BPF_LD_IMM64 instruction to load 64-bit immediate value into a register.
All previous instructions were 8-byte. This is first 16-byte instruction.
Two consecutive 'struct bpf_insn' blocks are interpreted as single instruction:
insn[0].code = BPF_LD | BPF_DW | BPF_IMM
insn[0].dst_reg = destination register
insn[0].imm = lower 32-bit
insn[1].code = 0
insn[1].imm = upper 32-bit
All unused fields must be zero.

Classic BPF has similar instruction: BPF_LD | BPF_W | BPF_IMM
which loads 32-bit immediate value into a register.

x64 JITs it as single 'movabsq %rax, imm64'
arm64 may JIT as sequence of four 'movk x0, #imm16, lsl #shift' insn

Note that old eBPF programs are binary compatible with new interpreter.

It helps eBPF programs load 64-bit constant into a register with one
instruction instead of using two registers and 4 instructions:
BPF_MOV32_IMM(R1, imm32)
BPF_ALU64_IMM(BPF_LSH, R1, 32)
BPF_MOV32_IMM(R2, imm32)
BPF_ALU64_REG(BPF_OR, R1, R2)

User space generated programs will use this instruction to load constants only.

To tell kernel that user space needs a pointer the _pseudo_ variant of
this instruction may be added later, which will use extra bits of encoding
to indicate what type of pointer user space is asking kernel to provide.
For example 'off' or 'src_reg' fields can be used for such purpose.
src_reg = 1 could mean that user space is asking kernel to validate and
load in-kernel map pointer.
src_reg = 2 could mean that user space needs readonly data section pointer
src_reg = 3 could mean that user space needs a pointer to per-cpu local data
All such future pseudo instructions will not be carrying the actual pointer
as part of the instruction, but rather will be treated as a request to kernel
to provide one. The kernel will verify the request_for_a_pointer, then
will drop _pseudo_ marking and will store actual internal pointer inside
the instruction, so the end result is the interpreter and JITs never
see pseudo BPF_LD_IMM64 insns and only operate on generic BPF_LD_IMM64 that
loads 64-bit immediate into a register. User space never operates on direct
pointers and verifier can easily recognize request_for_pointer vs other
instructions.

Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: bpf: make eBPF interpreter images read-only</title>
<updated>2014-09-05T19:02:48Z</updated>
<author>
<name>Daniel Borkmann</name>
<email>dborkman@redhat.com</email>
</author>
<published>2014-09-02T20:53:44Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=60a3b2253c413cf601783b070507d7dd6620c954'/>
<id>urn:sha1:60a3b2253c413cf601783b070507d7dd6620c954</id>
<content type='text'>
With eBPF getting more extended and exposure to user space is on it's way,
hardening the memory range the interpreter uses to steer its command flow
seems appropriate.  This patch moves the to be interpreted bytecode to
read-only pages.

In case we execute a corrupted BPF interpreter image for some reason e.g.
caused by an attacker which got past a verifier stage, it would not only
provide arbitrary read/write memory access but arbitrary function calls
as well. After setting up the BPF interpreter image, its contents do not
change until destruction time, thus we can setup the image on immutable
made pages in order to mitigate modifications to that code. The idea
is derived from commit 314beb9bcabf ("x86: bpf_jit_comp: secure bpf jit
against spraying attacks").

This is possible because bpf_prog is not part of sk_filter anymore.
After setup bpf_prog cannot be altered during its life-time. This prevents
any modifications to the entire bpf_prog structure (incl. function/JIT
image pointer).

Every eBPF program (including classic BPF that are migrated) have to call
bpf_prog_select_runtime() to select either interpreter or a JIT image
as a last setup step, and they all are being freed via bpf_prog_free(),
including non-JIT. Therefore, we can easily integrate this into the
eBPF life-time, plus since we directly allocate a bpf_prog, we have no
performance penalty.

Tested with seccomp and test_bpf testsuite in JIT/non-JIT mode and manual
inspection of kernel_page_tables.  Brad Spengler proposed the same idea
via Twitter during development of this patch.

Joint work with Hannes Frederic Sowa.

Suggested-by: Brad Spengler &lt;spender@grsecurity.net&gt;
Signed-off-by: Daniel Borkmann &lt;dborkman@redhat.com&gt;
Signed-off-by: Hannes Frederic Sowa &lt;hannes@stressinduktion.org&gt;
Cc: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Cc: Kees Cook &lt;keescook@chromium.org&gt;
Acked-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>bpf: x86: add missing 'shift by register' instructions to x64 eBPF JIT</title>
<updated>2014-08-26T00:33:56Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-08-25T19:27:02Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=72b603ee8cfc6be587f301568d79ce38e7ed735d'/>
<id>urn:sha1:72b603ee8cfc6be587f301568d79ce38e7ed735d</id>
<content type='text'>
'shift by register' operations are supported by eBPF interpreter, but were
accidently left out of x64 JIT compiler. Fix it and add a testcase.

Reported-by: Brendan Gregg &lt;brendan.d.gregg@gmail.com&gt;
Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Fixes: 622582786c9e ("net: filter: x86: internal BPF JIT")
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: filter: split 'struct sk_filter' into socket and bpf parts</title>
<updated>2014-08-02T22:03:58Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-07-31T03:34:16Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=7ae457c1e5b45a1b826fad9d62b32191d2bdcfdb'/>
<id>urn:sha1:7ae457c1e5b45a1b826fad9d62b32191d2bdcfdb</id>
<content type='text'>
clean up names related to socket filtering and bpf in the following way:
- everything that deals with sockets keeps 'sk_*' prefix
- everything that is pure BPF is changed to 'bpf_*' prefix

split 'struct sk_filter' into
struct sk_filter {
	atomic_t        refcnt;
	struct rcu_head rcu;
	struct bpf_prog *prog;
};
and
struct bpf_prog {
        u32                     jited:1,
                                len:31;
        struct sock_fprog_kern  *orig_prog;
        unsigned int            (*bpf_func)(const struct sk_buff *skb,
                                            const struct bpf_insn *filter);
        union {
                struct sock_filter      insns[0];
                struct bpf_insn         insnsi[0];
                struct work_struct      work;
        };
};
so that 'struct bpf_prog' can be used independent of sockets and cleans up
'unattached' bpf use cases

split SK_RUN_FILTER macro into:
    SK_RUN_FILTER to be used with 'struct sk_filter *' and
    BPF_PROG_RUN to be used with 'struct bpf_prog *'

__sk_filter_release(struct sk_filter *) gains
__bpf_prog_release(struct bpf_prog *) helper function

also perform related renames for the functions that work
with 'struct bpf_prog *', since they're on the same lines:

sk_filter_size -&gt; bpf_prog_size
sk_filter_select_runtime -&gt; bpf_prog_select_runtime
sk_filter_free -&gt; bpf_prog_free
sk_unattached_filter_create -&gt; bpf_prog_create
sk_unattached_filter_destroy -&gt; bpf_prog_destroy
sk_store_orig_filter -&gt; bpf_prog_store_orig_filter
sk_release_orig_filter -&gt; bpf_release_orig_filter
__sk_migrate_filter -&gt; bpf_migrate_filter
__sk_prepare_filter -&gt; bpf_prepare_filter

API for attaching classic BPF to a socket stays the same:
sk_attach_filter(prog, struct sock *)/sk_detach_filter(struct sock *)
and SK_RUN_FILTER(struct sk_filter *, ctx) to execute a program
which is used by sockets, tun, af_packet

API for 'unattached' BPF programs becomes:
bpf_prog_create(struct bpf_prog **)/bpf_prog_destroy(struct bpf_prog *)
and BPF_PROG_RUN(struct bpf_prog *, ctx) to execute a program
which is used by isdn, ppp, team, seccomp, ptp, xt_bpf, cls_bpf, test_bpf

Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: filter: rename 'struct sock_filter_int' into 'struct bpf_insn'</title>
<updated>2014-07-25T06:27:17Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-07-24T23:38:21Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=2695fb552cbef1029aa025a98acb80cc51d66de5'/>
<id>urn:sha1:2695fb552cbef1029aa025a98acb80cc51d66de5</id>
<content type='text'>
eBPF is used by socket filtering, seccomp and soon by tracing and
exposed to userspace, therefore 'sock_filter_int' name is not accurate.
Rename it to 'bpf_insn'

Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: filter: fix nlattr and nlattr_nest BPF tests</title>
<updated>2014-06-11T07:13:16Z</updated>
<author>
<name>Alexei Starovoitov</name>
<email>ast@plumgrid.com</email>
</author>
<published>2014-06-07T00:48:20Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=df6d0f983a59c333bc2b0cb82b65441b34310cf7'/>
<id>urn:sha1:df6d0f983a59c333bc2b0cb82b65441b34310cf7</id>
<content type='text'>
- 'struct nlattr' must be 2 byte aligned
- provide big-endian input data for nlattr/nlattr_nest tests

Signed-off-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Acked-by: Daniel Borkmann &lt;dborkman@redhat.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
<entry>
<title>net: filter: fix length calculation in BPF testsuite</title>
<updated>2014-06-02T23:32:43Z</updated>
<author>
<name>Chema Gonzalez</name>
<email>chema@google.com</email>
</author>
<published>2014-05-30T17:15:12Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=e9d9450497f77ee40c7d895bf1b5b134b9347d5f'/>
<id>urn:sha1:e9d9450497f77ee40c7d895bf1b5b134b9347d5f</id>
<content type='text'>
The current probe_filter_length() (the function that calculates the
length of a test BPF filter) behavior is to declare the end of the
filter as soon as it finds {0, *, *, 0}. This is actually a valid
insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)"
fails (its length is cut short).

We are changing probe_filter_length() so as to start from the end, and
declare the end of the filter as the first instruction which is not
{0, *, *, 0}. This solution produces a simpler patch than the
alternative of using an explicit end-of-filter mark. It is technically
incorrect if your filter ends up with "ld #0", but that should not
happen anyway.

We also add a new test (LD_IMM_0) that includes ld #0 (does not work
without this patch).

Signed-off-by: Chema Gonzalez &lt;chema@google.com&gt;
Acked-by: Daniel Borkmann &lt;dborkman@redhat.com&gt;
Acked-by: Alexei Starovoitov &lt;ast@plumgrid.com&gt;
Signed-off-by: David S. Miller &lt;davem@davemloft.net&gt;
</content>
</entry>
</feed>
