Skip to content

Commit e8a339b

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Add lazy global subprog validation tests
Add a few test that validate BPF verifier's lazy approach to validating global subprogs. We check that global subprogs that are called transitively through another global subprog is validated. We also check that invalid global subprog is not validated, if it's not called from the main program. And we also check that main program is always validated first, before any of the subprogs. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20231124035937.403208-4-andrii@kernel.org
1 parent 2afae08 commit e8a339b

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

tools/testing/selftests/bpf/prog_tests/verifier.c

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "verifier_direct_stack_access_wraparound.skel.h"
2626
#include "verifier_div0.skel.h"
2727
#include "verifier_div_overflow.skel.h"
28+
#include "verifier_global_subprogs.skel.h"
2829
#include "verifier_gotol.skel.h"
2930
#include "verifier_helper_access_var_len.skel.h"
3031
#include "verifier_helper_packet_access.skel.h"
@@ -134,6 +135,7 @@ void test_verifier_direct_packet_access(void) { RUN(verifier_direct_packet_acces
134135
void test_verifier_direct_stack_access_wraparound(void) { RUN(verifier_direct_stack_access_wraparound); }
135136
void test_verifier_div0(void) { RUN(verifier_div0); }
136137
void test_verifier_div_overflow(void) { RUN(verifier_div_overflow); }
138+
void test_verifier_global_subprogs(void) { RUN(verifier_global_subprogs); }
137139
void test_verifier_gotol(void) { RUN(verifier_gotol); }
138140
void test_verifier_helper_access_var_len(void) { RUN(verifier_helper_access_var_len); }
139141
void test_verifier_helper_packet_access(void) { RUN(verifier_helper_packet_access); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <stdbool.h>
5+
#include <errno.h>
6+
#include <string.h>
7+
#include <linux/bpf.h>
8+
#include <bpf/bpf_helpers.h>
9+
#include "bpf_misc.h"
10+
11+
int arr[1];
12+
int unkn_idx;
13+
14+
__noinline long global_bad(void)
15+
{
16+
return arr[unkn_idx]; /* BOOM */
17+
}
18+
19+
__noinline long global_good(void)
20+
{
21+
return arr[0];
22+
}
23+
24+
__noinline long global_calls_bad(void)
25+
{
26+
return global_good() + global_bad() /* does BOOM indirectly */;
27+
}
28+
29+
__noinline long global_calls_good_only(void)
30+
{
31+
return global_good();
32+
}
33+
34+
SEC("?raw_tp")
35+
__success __log_level(2)
36+
/* main prog is validated completely first */
37+
__msg("('global_calls_good_only') is global and assumed valid.")
38+
__msg("1: (95) exit")
39+
/* eventually global_good() is transitively validated as well */
40+
__msg("Validating global_good() func")
41+
__msg("('global_good') is safe for any args that match its prototype")
42+
int chained_global_func_calls_success(void)
43+
{
44+
return global_calls_good_only();
45+
}
46+
47+
SEC("?raw_tp")
48+
__failure __log_level(2)
49+
/* main prog validated successfully first */
50+
__msg("1: (95) exit")
51+
/* eventually we validate global_bad() and fail */
52+
__msg("Validating global_bad() func")
53+
__msg("math between map_value pointer and register") /* BOOM */
54+
int chained_global_func_calls_bad(void)
55+
{
56+
return global_calls_bad();
57+
}
58+
59+
/* do out of bounds access forcing verifier to fail verification if this
60+
* global func is called
61+
*/
62+
__noinline int global_unsupp(const int *mem)
63+
{
64+
if (!mem)
65+
return 0;
66+
return mem[100]; /* BOOM */
67+
}
68+
69+
const volatile bool skip_unsupp_global = true;
70+
71+
SEC("?raw_tp")
72+
__success
73+
int guarded_unsupp_global_called(void)
74+
{
75+
if (!skip_unsupp_global)
76+
return global_unsupp(NULL);
77+
return 0;
78+
}
79+
80+
SEC("?raw_tp")
81+
__failure __log_level(2)
82+
__msg("Func#1 ('global_unsupp') is global and assumed valid.")
83+
__msg("Validating global_unsupp() func#1...")
84+
__msg("value is outside of the allowed memory range")
85+
int unguarded_unsupp_global_called(void)
86+
{
87+
int x = 0;
88+
89+
return global_unsupp(&x);
90+
}
91+
92+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)