History

bpf: Allow narrow loads of bpf_sysctl fields with offset > 0

"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.

Ilya Leoshkevich committed 7 years ago

Commit 7541c871 parent 050668c2 files · +9 −3Browse files

Unified · Split

2 files
  1. kernel/bpf/cgroup.c +2 −2
  2. tools/testing/selftests/bpf/test_sysctl.c +7 −1
kernel/bpf/cgroup.c+2−2
@@ -1311,12 +1311,12 @@ static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type, · 1310 unmodified lines
13111311 return false;
13121312
13131313 switch (off) {
1314− case offsetof(struct bpf_sysctl, write):
1314+ case bpf_ctx_range(struct bpf_sysctl, write):
13151315 if (type != BPF_READ)
13161316 return false;
13171317 bpf_ctx_record_field_size(info, size_default);
13181318 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):
13201320 if (type == BPF_READ) {
13211321 bpf_ctx_record_field_size(info, size_default);
13221322 return bpf_ctx_narrow_access_ok(off, size, size_default);
tools/testing/selftests/bpf/test_sysctl.c+7−1
@@ -161,9 +161,14 @@ static struct sysctl_test tests[] = { · 160 unmodified lines
161161 .descr = "ctx:file_pos sysctl:read read ok narrow",
162162 .insns = {
163163 /* If (file_pos == X) */
164+#if __BYTE_ORDER == __LITTLE_ENDIAN
164165 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_1,
165166 offsetof(struct bpf_sysctl, file_pos)),
166− BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0, 2),
167+#else
168+ BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_1,
169+ offsetof(struct bpf_sysctl, file_pos) + 3),
170+#endif
171+ BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 4, 2),
167172
168173 /* return ALLOW; */
169174 BPF_MOV64_IMM(BPF_REG_0, 1),
@@ -176,6 +181,7 @@ static struct sysctl_test tests[] = { · 6 unmodified lines
176181 .attach_type = BPF_CGROUP_SYSCTL,
177182 .sysctl = "kernel/ostype",
178183 .open_flags = O_RDONLY,
184+ .seek = 4,
179185 .result = SUCCESS,
180186 },
181187 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t