mbox series

[v2,bpf-next,0/2] Track read-only map contents as known scalars in BPF verifiers

Message ID 20191009201458.2679171-1-andriin@fb.com
Headers show
Series Track read-only map contents as known scalars in BPF verifiers | expand

Message

Andrii Nakryiko Oct. 9, 2019, 8:14 p.m. UTC
With BPF maps supporting direct map access (currently, array_map w/ single
element, used for global data) that are read-only both from system call and
BPF side, it's possible for BPF verifier to track its contents as known
constants.

Now it's possible for user-space control app to pre-initialize read-only map
(e.g., for .rodata section) with user-provided flags and parameters and rely
on BPF verifier to detect and eliminate dead code resulting from specific
combination of input parameters.

v1->v2:
- BPF_F_RDONLY means nothing, stick to just map->frozen (Daniel);
- stick to passing just offset into map_direct_value_addr (Martin).

Andrii Nakryiko (2):
  bpf: track contents of read-only maps as scalars
  selftests/bpf: add read-only map values propagation tests

 kernel/bpf/verifier.c                         | 57 ++++++++++-
 .../selftests/bpf/prog_tests/rdonly_maps.c    | 99 +++++++++++++++++++
 .../selftests/bpf/progs/test_rdonly_maps.c    | 83 ++++++++++++++++
 3 files changed, 237 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/rdonly_maps.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_rdonly_maps.c

Comments

Daniel Borkmann Oct. 10, 2019, 11:51 p.m. UTC | #1
On Wed, Oct 09, 2019 at 01:14:56PM -0700, Andrii Nakryiko wrote:
> With BPF maps supporting direct map access (currently, array_map w/ single
> element, used for global data) that are read-only both from system call and
> BPF side, it's possible for BPF verifier to track its contents as known
> constants.
> 
> Now it's possible for user-space control app to pre-initialize read-only map
> (e.g., for .rodata section) with user-provided flags and parameters and rely
> on BPF verifier to detect and eliminate dead code resulting from specific
> combination of input parameters.
> 
> v1->v2:
> - BPF_F_RDONLY means nothing, stick to just map->frozen (Daniel);
> - stick to passing just offset into map_direct_value_addr (Martin).
> 
> Andrii Nakryiko (2):
>   bpf: track contents of read-only maps as scalars
>   selftests/bpf: add read-only map values propagation tests
> 
>  kernel/bpf/verifier.c                         | 57 ++++++++++-
>  .../selftests/bpf/prog_tests/rdonly_maps.c    | 99 +++++++++++++++++++
>  .../selftests/bpf/progs/test_rdonly_maps.c    | 83 ++++++++++++++++
>  3 files changed, 237 insertions(+), 2 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/rdonly_maps.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_rdonly_maps.c

Applied, thanks!