Skip to content

Commit 924886f

Browse files
Hou Taogregkh
Hou Tao
authored andcommitted
bpf: Disallow BPF_LOG_KERNEL log level for bpf(BPF_BTF_LOAD)
[ Upstream commit 866de40 ] BPF_LOG_KERNEL is only used internally, so disallow bpf_btf_load() to set log level as BPF_LOG_KERNEL. The same checking has already been done in bpf_check(), so factor out a helper to check the validity of log attributes and use it in both places. Fixes: 8580ac9 ("bpf: Process in-kernel BTF") Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent 218d952 commit 924886f

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

include/linux/bpf_verifier.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
367367
log->level == BPF_LOG_KERNEL);
368368
}
369369

370+
static inline bool
371+
bpf_verifier_log_attr_valid(const struct bpf_verifier_log *log)
372+
{
373+
return log->len_total >= 128 && log->len_total <= UINT_MAX >> 2 &&
374+
log->level && log->ubuf && !(log->level & ~BPF_LOG_MASK);
375+
}
376+
370377
#define BPF_MAX_SUBPROGS 256
371378

372379
struct bpf_subprog_info {

kernel/bpf/btf.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4135,8 +4135,7 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
41354135
log->len_total = log_size;
41364136

41374137
/* log attributes have to be sane */
4138-
if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
4139-
!log->level || !log->ubuf) {
4138+
if (!bpf_verifier_log_attr_valid(log)) {
41404139
err = -EINVAL;
41414140
goto errout;
41424141
}

kernel/bpf/verifier.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12349,11 +12349,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1234912349
log->ubuf = (char __user *) (unsigned long) attr->log_buf;
1235012350
log->len_total = attr->log_size;
1235112351

12352-
ret = -EINVAL;
1235312352
/* log attributes have to be sane */
12354-
if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
12355-
!log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
12353+
if (!bpf_verifier_log_attr_valid(log)) {
12354+
ret = -EINVAL;
1235612355
goto err_unlock;
12356+
}
1235712357
}
1235812358

1235912359
if (IS_ERR(btf_vmlinux)) {

0 commit comments

Comments
 (0)