bpf: Lift hashtab key_size limit
Subsystems (5)
PATCH v5 1/1 bpf: Lift hashtab key_size limit
Currently key_size of hashtab is limited to MAX_BPF_STACK. As the key of hashtab can also be a value from a per cpu map it can be larger than MAX_BPF_STACK.
The use-case for this patch originates to implement allow/disallow lists for files and file paths. The maximum length of file paths is defined by PATH_MAX with 4096 chars including nul.
This limit exceeds MAX_BPF_STACK.
Changelog:
v5:
- Fix cast overflow
v4:
- Utilize BPF skeleton in tests
- Rebase
v3:
- Rebase
v2:
- Add a test for bpf side
--- kernel/bpf/hashtab.c | 16 +++---- .../selftests/bpf/prog_tests/hash_large_key.c | 43 ++++++++++++++++++ .../selftests/bpf/progs/test_hash_large_key.c | 44 +++++++++++++++++++ tools/testing/selftests/bpf/test_maps.c | 3 +- 4 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/hash_large_key.c create mode 100644 tools/testing/selftests/bpf/progs/test_hash_large_key.c
kernel/bpf/hashtab.c +5 -11
@@ -390,17 +390,11 @@ static int htab_map_alloc_check(union bpf_attr *attr) attr->value_size == 0)return -EINVAL ; -if (attr->key_size >MAX_BPF_STACK ) -/* eBPF programs initialize keys on stack, so they cannot be -* larger than max stack size -*/ -return -E2BIG ; - -if (attr->value_size >=KMALLOC_MAX_SIZE - -MAX_BPF_STACK -sizeof (struct htab_elem )) -/* if value_size is bigger, the user space won't be able to -* access the elements via bpf syscall. This check also makes -* sure that the elem_size doesn't overflow and it's +if ((u64 )attr->key_size + attr->value_size >=KMALLOC_MAX_SIZE - +sizeof (struct htab_elem )) +/* if key_size + value_size is bigger, the user space won't be +* able to access the elements via bpf syscall. This check +* also makes sure that the elem_size doesn't overflow and it's * kmalloc-able later in htab_map_update_elem() */ return -E2BIG ;
tools/testing/selftests/bpf/prog_tests/hash_large_key.c new file +43 -0
43 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_hash_large_key.c new file +44 -0
44 lines not on this page · raw message
tools/testing/selftests/bpf/test_maps.c +2 -1
@@ -1223,9 +1223,10 @@ static void test_map_in_map(void)static void test_map_large (void ) { +struct bigkey {int a; -char b[116 ]; +char b[4096 ];long long c; } key;int fd, i, value;
Signature · 1 line
2.26.2
Quoted 22 lines · Florian Lehner
Currently
key_sizeof hashtab is limited toMAX_BPF_STACK. As the key of hashtab can also be a value from a per cpu map it can be larger thanMAX_BPF_STACK.The use-case for this patch originates to implement allow/disallow lists for files and file paths. The maximum length of file paths is defined by
PATH_MAXwith 4096 chars including nul.
This limit exceedsMAX_BPF_STACK.Changelog:
v5:
- Fix cast overflowv4:
- Utilize BPF skeleton in tests
- Rebasev3:
- Rebase
Quoted 6 lines
v2:
- Add a test for bpf sideSigned-off-by Florian Lehner <[email protected]>Acked-by John Fastabend <[email protected]>---
LGTM.
Quoted 8 lines
kernel/bpf/hashtab.c | 16 +++---- .../selftests/bpf/prog_tests/hash_large_key.c | 43 ++++++++++++++++++ .../selftests/bpf/progs/test_hash_large_key.c | 44 +++++++++++++++++++ tools/testing/selftests/bpf/test_maps.c | 3 +- 4 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/hash_large_key.c create mode 100644 tools/testing/selftests/bpf/progs/test_hash_large_key.c
[...]
Alexei Starovoitov
AppliedQuoted 27 lines · Andrii Nakryiko
Quoted 25 lines · Florian Lehner
Currently
key_sizeof hashtab is limited toMAX_BPF_STACK. As the key of hashtab can also be a value from a per cpu map it can be larger thanMAX_BPF_STACK.The use-case for this patch originates to implement allow/disallow lists for files and file paths. The maximum length of file paths is defined by
PATH_MAXwith 4096 chars including nul.
This limit exceedsMAX_BPF_STACK.Changelog:
v5:
- Fix cast overflowv4:
- Utilize BPF skeleton in tests
- Rebasev3:
- Rebasev2:
- Add a test for bpf side
Quoted 6 lines
Quoted 3 lines
Signed-off-by Florian Lehner <[email protected]>Acked-by John Fastabend <[email protected]>---
LGTM.
Acked-by Andrii Nakryiko <[email protected]>
Applied.
Sorry for the delay. The patch got lost in patchwork.