Skip to content

Commit 7229d47

Browse files
fmaurer-rhYucong Sun
authored andcommitted
selftests: bpf: Check bpf_msg_push_data return value
bpf_msg_push_data may return a non-zero value to indicate an error. The return value should be checked to prevent undetected errors. To indicate an error, the BPF programs now perform a different action than their intended one to make the userspace test program notice the error, i.e., the programs supposed to pass/redirect drop, the program supposed to drop passes. Fixes: 84fbfe0 ("bpf: test_sockmap add options to use msg_push_data") Signed-off-by: Felix Maurer <[email protected]> Acked-by: John Fastabend <[email protected]>
1 parent 0e0485f commit 7229d47

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

tools/testing/selftests/bpf/progs/test_sockmap_kern.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ SEC("sk_msg1")
235235
int bpf_prog4(struct sk_msg_md *msg)
236236
{
237237
int *bytes, zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5;
238-
int *start, *end, *start_push, *end_push, *start_pop, *pop;
238+
int *start, *end, *start_push, *end_push, *start_pop, *pop, err = 0;
239239

240240
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
241241
if (bytes)
@@ -249,8 +249,11 @@ int bpf_prog4(struct sk_msg_md *msg)
249249
bpf_msg_pull_data(msg, *start, *end, 0);
250250
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
251251
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
252-
if (start_push && end_push)
253-
bpf_msg_push_data(msg, *start_push, *end_push, 0);
252+
if (start_push && end_push) {
253+
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
254+
if (err)
255+
return SK_DROP;
256+
}
254257
start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
255258
pop = bpf_map_lookup_elem(&sock_bytes, &five);
256259
if (start_pop && pop)
@@ -263,6 +266,7 @@ int bpf_prog6(struct sk_msg_md *msg)
263266
{
264267
int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5, key = 0;
265268
int *bytes, *start, *end, *start_push, *end_push, *start_pop, *pop, *f;
269+
int err = 0;
266270
__u64 flags = 0;
267271

268272
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
@@ -279,8 +283,11 @@ int bpf_prog6(struct sk_msg_md *msg)
279283

280284
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
281285
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
282-
if (start_push && end_push)
283-
bpf_msg_push_data(msg, *start_push, *end_push, 0);
286+
if (start_push && end_push) {
287+
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
288+
if (err)
289+
return SK_DROP;
290+
}
284291

285292
start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
286293
pop = bpf_map_lookup_elem(&sock_bytes, &five);
@@ -338,7 +345,7 @@ SEC("sk_msg5")
338345
int bpf_prog10(struct sk_msg_md *msg)
339346
{
340347
int *bytes, *start, *end, *start_push, *end_push, *start_pop, *pop;
341-
int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5;
348+
int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5, err = 0;
342349

343350
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
344351
if (bytes)
@@ -352,8 +359,11 @@ int bpf_prog10(struct sk_msg_md *msg)
352359
bpf_msg_pull_data(msg, *start, *end, 0);
353360
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
354361
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
355-
if (start_push && end_push)
356-
bpf_msg_push_data(msg, *start_push, *end_push, 0);
362+
if (start_push && end_push) {
363+
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
364+
if (err)
365+
return SK_PASS;
366+
}
357367
start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
358368
pop = bpf_map_lookup_elem(&sock_bytes, &five);
359369
if (start_pop && pop)

0 commit comments

Comments
 (0)