Skip to content

Commit 0b20933

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
tools/bpftool: Strip away modifiers from global variables
Reliably remove all the type modifiers from read-only (.rodata) global variable definitions, including cases of inner field const modifiers and arrays of const values. Also modify one of selftests to ensure that const volatile struct doesn't prevent user-space from modifying .rodata variable. Fixes: 985ead4 ("bpftool: Add skeleton codegen command") Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200713232409.3062144-3-andriin@fb.com
1 parent 7c819e7 commit 0b20933

4 files changed

Lines changed: 18 additions & 19 deletions

File tree

‎tools/bpf/bpftool/gen.c‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static const char *get_map_ident(const struct bpf_map *map)
8888
return NULL;
8989
}
9090

91-
static void codegen_btf_dump_printf(void *ct, const char *fmt, va_list args)
91+
static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args)
9292
{
9393
vprintf(fmt, args);
9494
}
@@ -104,17 +104,20 @@ static int codegen_datasec_def(struct bpf_object *obj,
104104
int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec);
105105
const char *sec_ident;
106106
char var_ident[256];
107+
bool strip_mods = false;
107108

108-
if (strcmp(sec_name, ".data") == 0)
109+
if (strcmp(sec_name, ".data") == 0) {
109110
sec_ident = "data";
110-
else if (strcmp(sec_name, ".bss") == 0)
111+
} else if (strcmp(sec_name, ".bss") == 0) {
111112
sec_ident = "bss";
112-
else if (strcmp(sec_name, ".rodata") == 0)
113+
} else if (strcmp(sec_name, ".rodata") == 0) {
113114
sec_ident = "rodata";
114-
else if (strcmp(sec_name, ".kconfig") == 0)
115+
strip_mods = true;
116+
} else if (strcmp(sec_name, ".kconfig") == 0) {
115117
sec_ident = "kconfig";
116-
else
118+
} else {
117119
return 0;
120+
}
118121

119122
printf(" struct %s__%s {\n", obj_name, sec_ident);
120123
for (i = 0; i < vlen; i++, sec_var++) {
@@ -123,16 +126,10 @@ static int codegen_datasec_def(struct bpf_object *obj,
123126
DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts,
124127
.field_name = var_ident,
125128
.indent_level = 2,
129+
.strip_mods = strip_mods,
126130
);
127131
int need_off = sec_var->offset, align_off, align;
128132
__u32 var_type_id = var->type;
129-
const struct btf_type *t;
130-
131-
t = btf__type_by_id(btf, var_type_id);
132-
while (btf_is_mod(t)) {
133-
var_type_id = t->type;
134-
t = btf__type_by_id(btf, var_type_id);
135-
}
136133

137134
if (off > need_off) {
138135
p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",

‎tools/lib/bpf/btf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct btf_dump_emit_type_decl_opts {
147147
/* strip all the const/volatile/restrict mods */
148148
bool strip_mods;
149149
};
150-
#define btf_dump_emit_type_decl_opts__last_field indent_level
150+
#define btf_dump_emit_type_decl_opts__last_field strip_mods
151151

152152
LIBBPF_API int
153153
btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,

‎tools/testing/selftests/bpf/prog_tests/skeleton.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ void test_skeleton(void)
4141
CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
4242
CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
4343

44-
CHECK(rodata->in6 != 0, "in6", "got %d != exp %d\n", rodata->in6, 0);
44+
CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0);
4545
CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
4646

4747
/* validate we can pre-setup global variables, even in .bss */
4848
data->in1 = 10;
4949
data->in2 = 11;
5050
bss->in3 = 12;
5151
bss->in4 = 13;
52-
rodata->in6 = 14;
52+
rodata->in.in6 = 14;
5353

5454
err = test_skeleton__load(skel);
5555
if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
@@ -60,7 +60,7 @@ void test_skeleton(void)
6060
CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
6161
CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
6262
CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
63-
CHECK(rodata->in6 != 14, "in6", "got %d != exp %d\n", rodata->in6, 14);
63+
CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14);
6464

6565
/* now set new values and attach to get them into outX variables */
6666
data->in1 = 1;

‎tools/testing/selftests/bpf/progs/test_skeleton.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ long long in4 __attribute__((aligned(64))) = 0;
2020
struct s in5 = {};
2121

2222
/* .rodata section */
23-
const volatile int in6 = 0;
23+
const volatile struct {
24+
const int in6;
25+
} in = {};
2426

2527
/* .data section */
2628
int out1 = -1;
@@ -46,7 +48,7 @@ int handler(const void *ctx)
4648
out3 = in3;
4749
out4 = in4;
4850
out5 = in5;
49-
out6 = in6;
51+
out6 = in.in6;
5052

5153
bpf_syscall = CONFIG_BPF_SYSCALL;
5254
kern_ver = LINUX_KERNEL_VERSION;

0 commit comments

Comments
 (0)