diff mbox series

[RFC,v2,7/7] selftests/bpf: Change return value in test_libbpf_get_fd_by_id_opts.c

Message ID 20221207172434.435893-8-roberto.sassu@huaweicloud.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series bpf-lsm: Check return values of security modules | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply, async
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-3 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-4 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-5 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-8 success Logs for llvm-toolchain
bpf/vmtest-bpf-VM_Test-9 success Logs for set-matrix

Commit Message

Roberto Sassu Dec. 7, 2022, 5:24 p.m. UTC
From: Roberto Sassu <roberto.sassu@huawei.com>

In the no_alu32 version, the eBPF assembly looks like:

       0:	b7 00 00 00 00 00 00 00	r0 = 0
       1:	79 12 00 00 00 00 00 00	r2 = *(u64 *)(r1 + 0)
       2:	18 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00	r3 = 0 ll
       4:	5d 32 04 00 00 00 00 00	if r2 != r3 goto +4 <LBB0_2>
       5:	79 10 08 00 00 00 00 00	r0 = *(u64 *)(r1 + 8)
       6:	67 00 00 00 3e 00 00 00	r0 <<= 62
       7:	c7 00 00 00 3f 00 00 00	r0 s>>= 63
       8:	57 00 00 00 f3 ff ff ff	r0 &= -13

Unfortunately, ANDing of negative numbers is not yet supported in the
verifier. As a consequence, current bounds are lost in the AND operation,
resulting in estimating a positive return value, even if there isn't.

For now, return -EPERM instead of -EACCES, to remove the AND operation and
let the verifier know that the return value is legitimate (negative or
zero).

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 .../selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c    | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c b/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
index f5ac5f3e8919..a143dbbd5573 100644
--- a/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
+++ b/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
@@ -29,8 +29,13 @@  int BPF_PROG(check_access, struct bpf_map *map, fmode_t fmode)
 	if (map != (struct bpf_map *)&data_input)
 		return 0;
 
+	/*
+	 * Prefer -EPERM to -EACCES to avoid ANDing negative numbers in the
+	 * no_alu32 version, which results in the current register bounds to
+	 * be lost.
+	 */
 	if (fmode & FMODE_WRITE)
-		return -EACCES;
+		return -EPERM;
 
 	return 0;
 }