Skip to content

Commit 832d478

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 2571173 commit 832d478

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
@@ -396,6 +396,13 @@ static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
396396
log->level == BPF_LOG_KERNEL);
397397
}
398398

399+
static inline bool
400+
bpf_verifier_log_attr_valid(const struct bpf_verifier_log *log)
401+
{
402+
return log->len_total >= 128 && log->len_total <= UINT_MAX >> 2 &&
403+
log->level && log->ubuf && !(log->level & ~BPF_LOG_MASK);
404+
}
405+
399406
#define BPF_MAX_SUBPROGS 256
400407

401408
struct bpf_subprog_info {

kernel/bpf/btf.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,8 +4332,7 @@ static struct btf *btf_parse(bpfptr_t btf_data, u32 btf_data_size,
43324332
log->len_total = log_size;
43334333

43344334
/* log attributes have to be sane */
4335-
if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
4336-
!log->level || !log->ubuf) {
4335+
if (!bpf_verifier_log_attr_valid(log)) {
43374336
err = -EINVAL;
43384337
goto errout;
43394338
}

kernel/bpf/verifier.c

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

13762-
ret = -EINVAL;
1376313762
/* log attributes have to be sane */
13764-
if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
13765-
!log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
13763+
if (!bpf_verifier_log_attr_valid(log)) {
13764+
ret = -EINVAL;
1376613765
goto err_unlock;
13766+
}
1376713767
}
1376813768

1376913769
if (IS_ERR(btf_vmlinux)) {

0 commit comments

Comments
 (0)