"ctx:file_pos sysctl:read read ok narrow" works on s390 by accident: it reads the wrong byte, which happens to have the expected value of 0. Improve the test by seeking to the 4th byte and expecting 4 instead of 0. This makes the latent problem apparent: the test attempts to read the first byte of bpf_sysctl.file_pos, assuming this is the least-significant byte, which is not the case on big-endian machines: a non-zero offset is needed. The point of the test is to verify narrow loads, so we cannot cheat our way out by simply using BPF_W. The existence of the test means that such loads have to be supported, most likely because llvm can generate them. Fix the test by adding a big-endian variant, which uses an offset to access the least-significant byte of bpf_sysctl.file_pos. This reveals the final problem: verifier rejects accesses to bpf_sysctl fields with offset > 0. Such accesses are already allowed for a wide range of structs: __sk_buff, bpf_sock_addr and sk_msg_md to name a few. Extend this support to bpf_sysctl by using bpf_ctx_range instead of offsetof when matching field offsets.
Showing the changes to kernel (1 of 2 files). Show all
Unified · Split
| @@ -1311,12 +1311,12 @@ static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type, · 1310 unmodified lines | ||
| 1311 | 1311 | return false; |
| 1312 | 1312 | |
| 1313 | 1313 | switch (off) { |
| 1314 | − case offsetof(struct bpf_sysctl, write): | |
| 1314 | + case bpf_ctx_range(struct bpf_sysctl, write): | |
| 1315 | 1315 | if (type != BPF_READ) |
| 1316 | 1316 | return false; |
| 1317 | 1317 | bpf_ctx_record_field_size(info, size_default); |
| 1318 | 1318 | return bpf_ctx_narrow_access_ok(off, size, size_default); |
| 1319 | − case offsetof(struct bpf_sysctl, file_pos): | |
| 1319 | + case bpf_ctx_range(struct bpf_sysctl, file_pos): | |
| 1320 | 1320 | if (type == BPF_READ) { |
| 1321 | 1321 | bpf_ctx_record_field_size(info, size_default); |
| 1322 | 1322 | return bpf_ctx_narrow_access_ok(off, size, size_default); |