Skip to content

Commit f80442a

Browse files
iamkafaiborkmann
authored andcommitted
bpf: btf: Change how section is supported in btf_header
There are currently unused section descriptions in the btf_header. Those sections are here to support future BTF use cases. For example, the func section (func_off) is to support function signature (e.g. the BPF prog function signature). Instead of spelling out all potential sections up-front in the btf_header. This patch makes changes to btf_header such that extending it (e.g. adding a section) is possible later. The unused ones can be removed for now and they can be added back later. This patch: 1. adds a hdr_len to the btf_header. It will allow adding sections (and other info like parent_label and parent_name) later. The check is similar to the existing bpf_attr. If a user passes in a longer hdr_len, the kernel ensures the extra tailing bytes are 0. 2. allows the section order in the BTF object to be different from its sec_off order in btf_header. 3. each sec_off is followed by a sec_len. It must not have gap or overlapping among sections. The string section is ensured to be at the end due to the 4 bytes alignment requirement of the type section. The above changes will allow enough flexibility to add new sections (and other info) to the btf_header later. This patch also removes an unnecessary !err check at the end of btf_parse(). Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent dcab51f commit f80442a

2 files changed

Lines changed: 160 additions & 57 deletions

File tree

‎include/uapi/linux/btf.h‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@ struct btf_header {
1212
__u16 magic;
1313
__u8 version;
1414
__u8 flags;
15-
16-
__u32 parent_label;
17-
__u32 parent_name;
15+
__u32 hdr_len;
1816

1917
/* All offsets are in bytes relative to the end of this header */
20-
__u32 label_off; /* offset of label section */
21-
__u32 object_off; /* offset of data object section*/
22-
__u32 func_off; /* offset of function section */
2318
__u32 type_off; /* offset of type section */
19+
__u32 type_len; /* length of type section */
2420
__u32 str_off; /* offset of string section */
2521
__u32 str_len; /* length of string section */
2622
};

‎kernel/bpf/btf.c‎

Lines changed: 158 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/uaccess.h>
1313
#include <linux/kernel.h>
1414
#include <linux/idr.h>
15+
#include <linux/sort.h>
1516
#include <linux/bpf_verifier.h>
1617
#include <linux/btf.h>
1718

@@ -184,15 +185,13 @@ static DEFINE_IDR(btf_idr);
184185
static DEFINE_SPINLOCK(btf_idr_lock);
185186

186187
struct btf {
187-
union {
188-
struct btf_header *hdr;
189-
void *data;
190-
};
188+
void *data;
191189
struct btf_type **types;
192190
u32 *resolved_ids;
193191
u32 *resolved_sizes;
194192
const char *strings;
195193
void *nohdr_data;
194+
struct btf_header hdr;
196195
u32 nr_types;
197196
u32 types_size;
198197
u32 data_size;
@@ -228,6 +227,11 @@ enum resolve_mode {
228227

229228
#define MAX_RESOLVE_DEPTH 32
230229

230+
struct btf_sec_info {
231+
u32 off;
232+
u32 len;
233+
};
234+
231235
struct btf_verifier_env {
232236
struct btf *btf;
233237
u8 *visit_states;
@@ -418,14 +422,14 @@ static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
418422
static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
419423
{
420424
return !BTF_STR_TBL_ELF_ID(offset) &&
421-
BTF_STR_OFFSET(offset) < btf->hdr->str_len;
425+
BTF_STR_OFFSET(offset) < btf->hdr.str_len;
422426
}
423427

424428
static const char *btf_name_by_offset(const struct btf *btf, u32 offset)
425429
{
426430
if (!BTF_STR_OFFSET(offset))
427431
return "(anon)";
428-
else if (BTF_STR_OFFSET(offset) < btf->hdr->str_len)
432+
else if (BTF_STR_OFFSET(offset) < btf->hdr.str_len)
429433
return &btf->strings[BTF_STR_OFFSET(offset)];
430434
else
431435
return "(invalid-name-offset)";
@@ -536,7 +540,8 @@ static void btf_verifier_log_member(struct btf_verifier_env *env,
536540
__btf_verifier_log(log, "\n");
537541
}
538542

539-
static void btf_verifier_log_hdr(struct btf_verifier_env *env)
543+
static void btf_verifier_log_hdr(struct btf_verifier_env *env,
544+
u32 btf_data_size)
540545
{
541546
struct bpf_verifier_log *log = &env->log;
542547
const struct btf *btf = env->btf;
@@ -545,19 +550,16 @@ static void btf_verifier_log_hdr(struct btf_verifier_env *env)
545550
if (!bpf_verifier_log_needed(log))
546551
return;
547552

548-
hdr = btf->hdr;
553+
hdr = &btf->hdr;
549554
__btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
550555
__btf_verifier_log(log, "version: %u\n", hdr->version);
551556
__btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
552-
__btf_verifier_log(log, "parent_label: %u\n", hdr->parent_label);
553-
__btf_verifier_log(log, "parent_name: %u\n", hdr->parent_name);
554-
__btf_verifier_log(log, "label_off: %u\n", hdr->label_off);
555-
__btf_verifier_log(log, "object_off: %u\n", hdr->object_off);
556-
__btf_verifier_log(log, "func_off: %u\n", hdr->func_off);
557+
__btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
557558
__btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
559+
__btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
558560
__btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
559561
__btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
560-
__btf_verifier_log(log, "btf_total_size: %u\n", btf->data_size);
562+
__btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
561563
}
562564

563565
static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
@@ -1754,9 +1756,9 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
17541756
struct btf_header *hdr;
17551757
void *cur, *end;
17561758

1757-
hdr = btf->hdr;
1759+
hdr = &btf->hdr;
17581760
cur = btf->nohdr_data + hdr->type_off;
1759-
end = btf->nohdr_data + hdr->str_off;
1761+
end = btf->nohdr_data + hdr->type_len;
17601762

17611763
env->log_type_id = 1;
17621764
while (cur < end) {
@@ -1866,8 +1868,20 @@ static int btf_check_all_types(struct btf_verifier_env *env)
18661868

18671869
static int btf_parse_type_sec(struct btf_verifier_env *env)
18681870
{
1871+
const struct btf_header *hdr = &env->btf->hdr;
18691872
int err;
18701873

1874+
/* Type section must align to 4 bytes */
1875+
if (hdr->type_off & (sizeof(u32) - 1)) {
1876+
btf_verifier_log(env, "Unaligned type_off");
1877+
return -EINVAL;
1878+
}
1879+
1880+
if (!hdr->type_len) {
1881+
btf_verifier_log(env, "No type found");
1882+
return -EINVAL;
1883+
}
1884+
18711885
err = btf_check_all_metas(env);
18721886
if (err)
18731887
return err;
@@ -1881,10 +1895,15 @@ static int btf_parse_str_sec(struct btf_verifier_env *env)
18811895
struct btf *btf = env->btf;
18821896
const char *start, *end;
18831897

1884-
hdr = btf->hdr;
1898+
hdr = &btf->hdr;
18851899
start = btf->nohdr_data + hdr->str_off;
18861900
end = start + hdr->str_len;
18871901

1902+
if (end != btf->data + btf->data_size) {
1903+
btf_verifier_log(env, "String section is not at the end");
1904+
return -EINVAL;
1905+
}
1906+
18881907
if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
18891908
start[0] || end[-1]) {
18901909
btf_verifier_log(env, "Invalid string section");
@@ -1896,20 +1915,122 @@ static int btf_parse_str_sec(struct btf_verifier_env *env)
18961915
return 0;
18971916
}
18981917

1899-
static int btf_parse_hdr(struct btf_verifier_env *env)
1918+
static const size_t btf_sec_info_offset[] = {
1919+
offsetof(struct btf_header, type_off),
1920+
offsetof(struct btf_header, str_off),
1921+
};
1922+
1923+
static int btf_sec_info_cmp(const void *a, const void *b)
1924+
{
1925+
const struct btf_sec_info *x = a;
1926+
const struct btf_sec_info *y = b;
1927+
1928+
return (int)(x->off - y->off) ? : (int)(x->len - y->len);
1929+
}
1930+
1931+
static int btf_check_sec_info(struct btf_verifier_env *env,
1932+
u32 btf_data_size)
19001933
{
1934+
const unsigned int nr_secs = ARRAY_SIZE(btf_sec_info_offset);
1935+
struct btf_sec_info secs[nr_secs];
1936+
u32 total, expected_total, i;
19011937
const struct btf_header *hdr;
1902-
struct btf *btf = env->btf;
1903-
u32 meta_left;
1938+
const struct btf *btf;
1939+
1940+
btf = env->btf;
1941+
hdr = &btf->hdr;
19041942

1905-
if (btf->data_size < sizeof(*hdr)) {
1943+
/* Populate the secs from hdr */
1944+
for (i = 0; i < nr_secs; i++)
1945+
secs[i] = *(struct btf_sec_info *)((void *)hdr +
1946+
btf_sec_info_offset[i]);
1947+
1948+
sort(secs, nr_secs, sizeof(struct btf_sec_info),
1949+
btf_sec_info_cmp, NULL);
1950+
1951+
/* Check for gaps and overlap among sections */
1952+
total = 0;
1953+
expected_total = btf_data_size - hdr->hdr_len;
1954+
for (i = 0; i < nr_secs; i++) {
1955+
if (expected_total < secs[i].off) {
1956+
btf_verifier_log(env, "Invalid section offset");
1957+
return -EINVAL;
1958+
}
1959+
if (total < secs[i].off) {
1960+
/* gap */
1961+
btf_verifier_log(env, "Unsupported section found");
1962+
return -EINVAL;
1963+
}
1964+
if (total > secs[i].off) {
1965+
btf_verifier_log(env, "Section overlap found");
1966+
return -EINVAL;
1967+
}
1968+
if (expected_total - total < secs[i].len) {
1969+
btf_verifier_log(env,
1970+
"Total section length too long");
1971+
return -EINVAL;
1972+
}
1973+
total += secs[i].len;
1974+
}
1975+
1976+
/* There is data other than hdr and known sections */
1977+
if (expected_total != total) {
1978+
btf_verifier_log(env, "Unsupported section found");
1979+
return -EINVAL;
1980+
}
1981+
1982+
return 0;
1983+
}
1984+
1985+
static int btf_parse_hdr(struct btf_verifier_env *env, void __user *btf_data,
1986+
u32 btf_data_size)
1987+
{
1988+
const struct btf_header *hdr;
1989+
u32 hdr_len, hdr_copy;
1990+
/*
1991+
* Minimal part of the "struct btf_header" that
1992+
* contains the hdr_len.
1993+
*/
1994+
struct btf_min_header {
1995+
u16 magic;
1996+
u8 version;
1997+
u8 flags;
1998+
u32 hdr_len;
1999+
} __user *min_hdr;
2000+
struct btf *btf;
2001+
int err;
2002+
2003+
btf = env->btf;
2004+
min_hdr = btf_data;
2005+
2006+
if (btf_data_size < sizeof(*min_hdr)) {
2007+
btf_verifier_log(env, "hdr_len not found");
2008+
return -EINVAL;
2009+
}
2010+
2011+
if (get_user(hdr_len, &min_hdr->hdr_len))
2012+
return -EFAULT;
2013+
2014+
if (btf_data_size < hdr_len) {
19062015
btf_verifier_log(env, "btf_header not found");
19072016
return -EINVAL;
19082017
}
19092018

1910-
btf_verifier_log_hdr(env);
2019+
err = bpf_check_uarg_tail_zero(btf_data, sizeof(btf->hdr), hdr_len);
2020+
if (err) {
2021+
if (err == -E2BIG)
2022+
btf_verifier_log(env, "Unsupported btf_header");
2023+
return err;
2024+
}
2025+
2026+
hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
2027+
if (copy_from_user(&btf->hdr, btf_data, hdr_copy))
2028+
return -EFAULT;
2029+
2030+
hdr = &btf->hdr;
2031+
2032+
btf_verifier_log_hdr(env, btf_data_size);
19112033

1912-
hdr = btf->hdr;
19132034
if (hdr->magic != BTF_MAGIC) {
19142035
btf_verifier_log(env, "Invalid magic");
19152036
return -EINVAL;
@@ -1925,26 +2046,14 @@ static int btf_parse_hdr(struct btf_verifier_env *env)
19252046
return -ENOTSUPP;
19262047
}
19272048

1928-
meta_left = btf->data_size - sizeof(*hdr);
1929-
if (!meta_left) {
2049+
if (btf_data_size == hdr->hdr_len) {
19302050
btf_verifier_log(env, "No data");
19312051
return -EINVAL;
19322052
}
19332053

1934-
if (meta_left < hdr->type_off || hdr->str_off <= hdr->type_off ||
1935-
/* Type section must align to 4 bytes */
1936-
hdr->type_off & (sizeof(u32) - 1)) {
1937-
btf_verifier_log(env, "Invalid type_off");
1938-
return -EINVAL;
1939-
}
1940-
1941-
if (meta_left < hdr->str_off ||
1942-
meta_left - hdr->str_off < hdr->str_len) {
1943-
btf_verifier_log(env, "Invalid str_off or str_len");
1944-
return -EINVAL;
1945-
}
1946-
1947-
btf->nohdr_data = btf->hdr + 1;
2054+
err = btf_check_sec_info(env, btf_data_size);
2055+
if (err)
2056+
return err;
19482057

19492058
return 0;
19502059
}
@@ -1987,6 +2096,11 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
19872096
err = -ENOMEM;
19882097
goto errout;
19892098
}
2099+
env->btf = btf;
2100+
2101+
err = btf_parse_hdr(env, btf_data, btf_data_size);
2102+
if (err)
2103+
goto errout;
19902104

19912105
data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
19922106
if (!data) {
@@ -1996,18 +2110,13 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
19962110

19972111
btf->data = data;
19982112
btf->data_size = btf_data_size;
2113+
btf->nohdr_data = btf->data + btf->hdr.hdr_len;
19992114

20002115
if (copy_from_user(data, btf_data, btf_data_size)) {
20012116
err = -EFAULT;
20022117
goto errout;
20032118
}
20042119

2005-
env->btf = btf;
2006-
2007-
err = btf_parse_hdr(env);
2008-
if (err)
2009-
goto errout;
2010-
20112120
err = btf_parse_str_sec(env);
20122121
if (err)
20132122
goto errout;
@@ -2016,16 +2125,14 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
20162125
if (err)
20172126
goto errout;
20182127

2019-
if (!err && log->level && bpf_verifier_log_full(log)) {
2128+
if (log->level && bpf_verifier_log_full(log)) {
20202129
err = -ENOSPC;
20212130
goto errout;
20222131
}
20232132

2024-
if (!err) {
2025-
btf_verifier_env_free(env);
2026-
refcount_set(&btf->refcnt, 1);
2027-
return btf;
2028-
}
2133+
btf_verifier_env_free(env);
2134+
refcount_set(&btf->refcnt, 1);
2135+
return btf;
20292136

20302137
errout:
20312138
btf_verifier_env_free(env);

0 commit comments

Comments
 (0)