Skip to content

Commit

Permalink
selftests/bpf: Avoid running unprivileged tests with alignment requir…
Browse files Browse the repository at this point in the history
…ements

Some architectures have strict alignment requirements. In that case,
the BPF verifier detects if a program has unaligned accesses and
rejects them. A user can pass BPF_F_ANY_ALIGNMENT to a program to
override this check. That, however, will only work when a privileged
user loads a program. An unprivileged user loading a program with this
flag will be rejected prior entering the verifier.

Hence, it does not make sense to load unprivileged programs without
strict alignment when testing the verifier. This patch avoids exactly
that.

Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Luke Nelson <luke.r.nels@gmail.com>
Link: https://lore.kernel.org/bpf/20201118071640.83773-3-bjorn.topel@gmail.com
  • Loading branch information
Björn Töpel authored and Andrii Nakryiko committed Nov 19, 2020
1 parent 6016df8 commit c77b058
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tools/testing/selftests/bpf/test_verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,19 @@ static void get_unpriv_disabled()

static bool test_as_unpriv(struct bpf_test *test)
{
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
/* Some architectures have strict alignment requirements. In
* that case, the BPF verifier detects if a program has
* unaligned accesses and rejects them. A user can pass
* BPF_F_ANY_ALIGNMENT to a program to override this
* check. That, however, will only work when a privileged user
* loads a program. An unprivileged user loading a program
* with this flag will be rejected prior entering the
* verifier.
*/
if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
return false;
#endif
return !test->prog_type ||
test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
Expand Down

0 comments on commit c77b058

Please sign in to comment.