From: Joe Stringer <joe@wand.net.nz>
To: daniel@iogearbox.net
Cc: netdev@vger.kernel.org, ast@kernel.org, john.fastabend@gmail.com,
tgraf@suug.ch, kafai@fb.com, nitin.hande@gmail.com,
mauricio.vasquez@polito.it
Subject: [PATCHv4 bpf-next 13/13] Documentation: Describe bpf reference tracking
Date: Tue, 2 Oct 2018 13:35:41 -0700 [thread overview]
Message-ID: <20181002203541.26599-14-joe@wand.net.nz> (raw)
In-Reply-To: <20181002203541.26599-1-joe@wand.net.nz>
Document the new pointer types in the verifier and how the pointer ID
tracking works to ensure that references which are taken are later
released.
Signed-off-by: Joe Stringer <joe@wand.net.nz>
Acked-by: Alexei Starovoitov <ast@kernel.org>
---
Documentation/networking/filter.txt | 64 +++++++++++++++++++++++++++++
1 file changed, 64 insertions(+)
diff --git a/Documentation/networking/filter.txt b/Documentation/networking/filter.txt
index e6b4ebb2b243..4443ce958862 100644
--- a/Documentation/networking/filter.txt
+++ b/Documentation/networking/filter.txt
@@ -1125,6 +1125,14 @@ pointer type. The types of pointers describe their base, as follows:
PTR_TO_STACK Frame pointer.
PTR_TO_PACKET skb->data.
PTR_TO_PACKET_END skb->data + headlen; arithmetic forbidden.
+ PTR_TO_SOCKET Pointer to struct bpf_sock_ops, implicitly refcounted.
+ PTR_TO_SOCKET_OR_NULL
+ Either a pointer to a socket, or NULL; socket lookup
+ returns this type, which becomes a PTR_TO_SOCKET when
+ checked != NULL. PTR_TO_SOCKET is reference-counted,
+ so programs must release the reference through the
+ socket release function before the end of the program.
+ Arithmetic on these pointers is forbidden.
However, a pointer may be offset from this base (as a result of pointer
arithmetic), and this is tracked in two parts: the 'fixed offset' and 'variable
offset'. The former is used when an exactly-known value (e.g. an immediate
@@ -1171,6 +1179,13 @@ over the Ethernet header, then reads IHL and addes (IHL * 4), the resulting
pointer will have a variable offset known to be 4n+2 for some n, so adding the 2
bytes (NET_IP_ALIGN) gives a 4-byte alignment and so word-sized accesses through
that pointer are safe.
+The 'id' field is also used on PTR_TO_SOCKET and PTR_TO_SOCKET_OR_NULL, common
+to all copies of the pointer returned from a socket lookup. This has similar
+behaviour to the handling for PTR_TO_MAP_VALUE_OR_NULL->PTR_TO_MAP_VALUE, but
+it also handles reference tracking for the pointer. PTR_TO_SOCKET implicitly
+represents a reference to the corresponding 'struct sock'. To ensure that the
+reference is not leaked, it is imperative to NULL-check the reference and in
+the non-NULL case, and pass the valid reference to the socket release function.
Direct packet access
--------------------
@@ -1444,6 +1459,55 @@ Error:
8: (7a) *(u64 *)(r0 +0) = 1
R0 invalid mem access 'imm'
+Program that performs a socket lookup then sets the pointer to NULL without
+checking it:
+value:
+ BPF_MOV64_IMM(BPF_REG_2, 0),
+ BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8),
+ BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+ BPF_MOV64_IMM(BPF_REG_3, 4),
+ BPF_MOV64_IMM(BPF_REG_4, 0),
+ BPF_MOV64_IMM(BPF_REG_5, 0),
+ BPF_EMIT_CALL(BPF_FUNC_sk_lookup_tcp),
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+Error:
+ 0: (b7) r2 = 0
+ 1: (63) *(u32 *)(r10 -8) = r2
+ 2: (bf) r2 = r10
+ 3: (07) r2 += -8
+ 4: (b7) r3 = 4
+ 5: (b7) r4 = 0
+ 6: (b7) r5 = 0
+ 7: (85) call bpf_sk_lookup_tcp#65
+ 8: (b7) r0 = 0
+ 9: (95) exit
+ Unreleased reference id=1, alloc_insn=7
+
+Program that performs a socket lookup but does not NULL-check the returned
+value:
+ BPF_MOV64_IMM(BPF_REG_2, 0),
+ BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8),
+ BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+ BPF_MOV64_IMM(BPF_REG_3, 4),
+ BPF_MOV64_IMM(BPF_REG_4, 0),
+ BPF_MOV64_IMM(BPF_REG_5, 0),
+ BPF_EMIT_CALL(BPF_FUNC_sk_lookup_tcp),
+ BPF_EXIT_INSN(),
+Error:
+ 0: (b7) r2 = 0
+ 1: (63) *(u32 *)(r10 -8) = r2
+ 2: (bf) r2 = r10
+ 3: (07) r2 += -8
+ 4: (b7) r3 = 4
+ 5: (b7) r4 = 0
+ 6: (b7) r5 = 0
+ 7: (85) call bpf_sk_lookup_tcp#65
+ 8: (95) exit
+ Unreleased reference id=1, alloc_insn=7
+
Testing
-------
--
2.17.1
next prev parent reply other threads:[~2018-10-03 3:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-02 20:35 [PATCHv4 bpf-next 00/13] Add socket lookup support Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 01/13] bpf: Add iterator for spilled registers Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 02/13] bpf: Simplify ptr_min_max_vals adjustment Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 03/13] bpf: Reuse canonical string formatter for ctx errs Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 04/13] bpf: Generalize ptr_or_null regs check Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 05/13] bpf: Add PTR_TO_SOCKET verifier type Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 06/13] bpf: Macrofy stack state copy Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 07/13] bpf: Add reference tracking to verifier Joe Stringer
2019-01-09 5:27 ` Alexei Starovoitov
2019-01-10 19:26 ` Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 08/13] bpf: Add helper to retrieve socket in BPF Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 09/13] selftests/bpf: Generalize dummy program types Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 10/13] selftests/bpf: Add tests for reference tracking Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 11/13] libbpf: Support loading individual progs Joe Stringer
2018-10-02 20:35 ` [PATCHv4 bpf-next 12/13] selftests/bpf: Add C tests for reference tracking Joe Stringer
2018-10-02 20:35 ` Joe Stringer [this message]
2018-10-03 1:08 ` [PATCHv4 bpf-next 00/13] Add socket lookup support Daniel Borkmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181002203541.26599-14-joe@wand.net.nz \
--to=joe@wand.net.nz \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=mauricio.vasquez@polito.it \
--cc=netdev@vger.kernel.org \
--cc=nitin.hande@gmail.com \
--cc=tgraf@suug.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).