From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Joe Stringer <joe@wand.net.nz>
Cc: daniel@iogearbox.net, netdev@vger.kernel.org, ast@kernel.org,
john.fastabend@gmail.com, kafai@fb.com
Subject: Re: [RFC bpf-next 11/11] Documentation: Describe bpf reference tracking
Date: Mon, 14 May 2018 20:19:32 -0700 [thread overview]
Message-ID: <20180515031931.7olac7wnh47acqu5@ast-mbp> (raw)
In-Reply-To: <20180509210709.7201-12-joe@wand.net.nz>
On Wed, May 09, 2018 at 02:07:09PM -0700, Joe Stringer wrote:
> Signed-off-by: Joe Stringer <joe@wand.net.nz>
> ---
> Documentation/networking/filter.txt | 64 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 64 insertions(+)
>
> diff --git a/Documentation/networking/filter.txt b/Documentation/networking/filter.txt
> index 5032e1263bc9..77be17977bc5 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
> @@ -1168,6 +1176,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
> --------------------
> @@ -1441,6 +1456,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),
> + 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#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),
> + 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#65
> + 8: (95) exit
> + Unreleased reference id=1, alloc_insn=7
Nice. Thank you for updating this doc. We haven't touched it in long time.
It probably long overdue for complete overhaul.
next prev parent reply other threads:[~2018-05-15 3:19 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-09 21:06 [RFC bpf-next 00/11] Add socket lookup support Joe Stringer
2018-05-09 21:06 ` [RFC bpf-next 01/11] bpf: Add iterator for spilled registers Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 02/11] bpf: Simplify ptr_min_max_vals adjustment Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 03/11] bpf: Generalize ptr_or_null regs check Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 04/11] bpf: Add PTR_TO_SOCKET verifier type Joe Stringer
2018-05-15 2:37 ` Alexei Starovoitov
2018-05-16 23:56 ` Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 05/11] bpf: Macrofy stack state copy Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 06/11] bpf: Add reference tracking to verifier Joe Stringer
2018-05-15 3:04 ` Alexei Starovoitov
2018-05-17 1:05 ` Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 07/11] bpf: Add helper to retrieve socket in BPF Joe Stringer
2018-05-11 5:00 ` Martin KaFai Lau
2018-05-11 21:08 ` Joe Stringer
2018-05-11 21:41 ` Martin KaFai Lau
2018-05-12 0:54 ` Joe Stringer
2018-05-15 3:16 ` Alexei Starovoitov
2018-05-15 16:48 ` Martin KaFai Lau
2018-05-16 18:55 ` Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 08/11] selftests/bpf: Add tests for reference tracking Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 09/11] libbpf: Support loading individual progs Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 10/11] selftests/bpf: Add C tests for reference tracking Joe Stringer
2018-05-09 21:07 ` [RFC bpf-next 11/11] Documentation: Describe bpf " Joe Stringer
2018-05-15 3:19 ` Alexei Starovoitov [this message]
2018-05-16 19:05 ` [RFC bpf-next 00/11] Add socket lookup support Joe Stringer
2018-05-16 20:04 ` Alexei Starovoitov
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=20180515031931.7olac7wnh47acqu5@ast-mbp \
--to=alexei.starovoitov@gmail.com \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=joe@wand.net.nz \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
/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