Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Avoid running unprivileged tests with alignment requirements

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

authored by

Björn Töpel and committed by
Andrii Nakryiko
c77b0589 6016df8f

+13
+13
tools/testing/selftests/bpf/test_verifier.c
··· 1152 1152 1153 1153 static bool test_as_unpriv(struct bpf_test *test) 1154 1154 { 1155 + #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1156 + /* Some architectures have strict alignment requirements. In 1157 + * that case, the BPF verifier detects if a program has 1158 + * unaligned accesses and rejects them. A user can pass 1159 + * BPF_F_ANY_ALIGNMENT to a program to override this 1160 + * check. That, however, will only work when a privileged user 1161 + * loads a program. An unprivileged user loading a program 1162 + * with this flag will be rejected prior entering the 1163 + * verifier. 1164 + */ 1165 + if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) 1166 + return false; 1167 + #endif 1155 1168 return !test->prog_type || 1156 1169 test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER || 1157 1170 test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;