bpf: allow BPF_LOG_KERNEL from kernel space
PATCH v2 1/1 bpf: allow BPF_LOG_KERNEL from kernel space
When loading a BPF program or BTF from kernel space (e.g. using a light skeleton generated loader module), the kernel module may want to output verifier logs to dmesg by setting attr->log_level = BPF_LOG_KERNEL (16).
Currently, any attempt to do this through the standard syscall path (kern_sys_bpf -> bpf_vlog_init) fails with -EINVAL because bpf_verifier_log_attr_valid() rejects any log_level bit outside BPF_LOG_MASK. This prevents in-kernel loaders from successfully using BPF_LOG_KERNEL, causing silent failures if the BPF object is rejected by the verifier.
This patch modifies bpf_verifier_log_attr_valid() to accept BPF_LOG_KERNEL unconditionally. To prevent userspace from exploiting this to spam the kernel log, the patch enforces a strict privilege check at the syscall boundary points (btf_parse, bpf_log_attr_init, bpf_log_attr_create_vlog), ensuring BPF_LOG_KERNEL is only permitted when uattr.is_kernel is true.
---
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
To: John Fastabend <[email protected]>
--- Changes in v2:
- Refactored BPF_LOG_KERNEL validation to check uattr.is_kernel at syscall boundary points (btf_parse, bpf_log_attr_init, etc.) rather than plumbing an is_kernel boolean through the verifier log initialization functions.
- Link to v1:
https://lore.kernel.org/r/[email protected]
--- kernel/bpf/btf.c | 3 +++ kernel/bpf/log.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+)
kernel/bpf/btf.c +3 -0
@@ -5919,6 +5919,9 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr,if (attr->btf_size >BTF_MAX_SIZE )return ERR_PTR (-E2BIG ); +if (attr_log->level ==BPF_LOG_KERNEL && !uattr.is_kernel) +return ERR_PTR (-EINVAL ); + env =kzalloc_obj (*env,GFP_KERNEL | __GFP_NOWARN);if (!env)return ERR_PTR (-ENOMEM );
kernel/bpf/log.c +17 -0
@@ -21,6 +21,15 @@ static bool bpf_verifier_log_attr_valid(u32 log_level, char __user *log_buf, u32/* log buf without log_level is meaningless */ if (log_buf && log_level ==0 )return false; + +/* BPF_LOG_KERNEL is an exclusive internal mode, not a bit flag. +* It must be checked via strict equality to prevent it from being +* combined with userspace flags, which would cause the verifier +* to fallback to userspace logging and copy_to_user() failures. +*/ +if (log_level ==BPF_LOG_KERNEL ) +return true; +if (log_level & ~BPF_LOG_MASK )return false;if (log_size >UINT_MAX >>2 )
@@ -831,6 +840,11 @@ int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 lchar __user *ubuf_common =u64_to_user_ptr (common->log_buf);char __user *ubuf =u64_to_user_ptr (log_buf); +if (common->log_level ==BPF_LOG_KERNEL && !uattr_common.is_kernel) +return -EINVAL ; +if (log_level ==BPF_LOG_KERNEL && !uattr.is_kernel) +return -EINVAL ; +if (!bpf_verifier_log_attr_valid (common->log_level, ubuf_common, common->log_size) || !bpf_verifier_log_attr_valid (log_level, ubuf, log_size))return -EINVAL ;
@@ -873,6 +887,9 @@ struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *attr_log,if (!size)return NULL ; +if (common->log_level ==BPF_LOG_KERNEL && !uattr.is_kernel) +return ERR_PTR (-EINVAL ); + log =kzalloc_obj (*log,GFP_KERNEL );if (!log)return ERR_PTR (-ENOMEM );
---
base-commit: 502d801f0ab03e4f32f9a33d203154ce84887921
change-id: 20260622-bpf-lskel-fixes-cafd1a83dec2
Best regards,
Signature · 1 line
Siddharth Nayyar <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] BPF_LOG_KERNEL is silently ignored or overwritten when specified via BPF_COMMON_ATTRS due to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] BPF_LOG_KERNEL is silently ignored or overwritten when specified via BPF_COMMON_ATTRS due to flawed null-buffer checks in bpf_log_attr_init. --
commit efd637812e314de99108496755489e9a7983fe49
Author: Siddharth Nayyar <[email protected]>
bpf: allow BPF_LOG_KERNEL from kernel space
This commit allows kernel space loaders to output BPF verifier logs directly to dmesg by setting the log_level to BPF_LOG_KERNEL. It modifies bpf_verifier_log_attr_valid() to accept this level unconditionally and adds strict privilege checks at the syscall boundary points to prevent userspace from spamming the kernel log.
Quoted 4 lines
kernel/bpf/log.c +0
-0
[ ... ]
kernel/bpf/log.c int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 l · line 850
Quoted 12 lines
kernel/bpf/log.c +5
-0@@ -831,6 +840,11 @@ int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 lchar __user *ubuf_common =u64_to_user_ptr (common->log_buf);char __user *ubuf =u64_to_user_ptr (log_buf); +if (common->log_level ==BPF_LOG_KERNEL && !uattr_common.is_kernel) +return -EINVAL ; +if (log_level ==BPF_LOG_KERNEL && !uattr.is_kernel) +return -EINVAL ; +if (!bpf_verifier_log_attr_valid (common->log_level, ubuf_common, common->log_size) || !bpf_verifier_log_attr_valid (log_level, ubuf, log_size))return -EINVAL ;
[Severity: Medium]
Does this code handle BPF_LOG_KERNEL correctly when it is specified via BPF_COMMON_ATTRS?
Since BPF_LOG_KERNEL outputs directly to dmesg, the user buffer is 0, meaning ubuf and ubuf_common will be NULL.
Slightly further down in bpf_log_attr_init(), the mismatch checks and inheritance logic rely on these buffers being non-NULL:
if (ubuf && ubuf_common && (ubuf != ubuf_common || ...))
return -EINVAL;
...
if (!ubuf && ubuf_common) {
log->ubuf = ubuf_common;
log->level = common->log_level;
...
}If a caller passes BPF_LOG_KERNEL via common attributes (ubuf_common == NULL), it seems the inheritance condition (!ubuf && ubuf_common) will be false, and the common logging level will be silently ignored.
Additionally, if a caller specifies BPF_LOG_KERNEL alongside a standard common log level (ubuf == NULL, ubuf_common != NULL), the mismatch check is bypassed, and the specific BPF_LOG_KERNEL is silently overwritten by the common attributes instead of returning -EINVAL.
Could the null-buffer checks be updated to properly support inheriting and validating BPF_LOG_KERNEL?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 35 lines · On Wed, Jun 24, 2026 at 7:34 PM <[email protected]> wrote:
kernel/bpf/log.c int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 l · line 850
Quoted 13 lines
kernel/bpf/log.c +5
-0@@ -831,6 +840,11 @@ int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 lchar __user *ubuf_common =u64_to_user_ptr (common->log_buf);char __user *ubuf =u64_to_user_ptr (log_buf); +if (common->log_level ==BPF_LOG_KERNEL && !uattr_common.is_kernel) +return -EINVAL ; +if (log_level ==BPF_LOG_KERNEL && !uattr.is_kernel) +return -EINVAL ; +if (!bpf_verifier_log_attr_valid (common->log_level, ubuf_common, common->log_size) || !bpf_verifier_log_attr_valid (log_level, ubuf, log_size))return -EINVAL ;[Severity: Medium]
Does this code handleBPF_LOG_KERNELcorrectly when it is specified viaBPF_COMMON_ATTRS?Since
BPF_LOG_KERNELoutputs directly to dmesg, the user buffer is 0, meaning ubuf andubuf_commonwill be NULL.Slightly further down in
bpf_log_attr_init(), the mismatch checks and inheritance logic rely on these buffers being non-NULL:if (ubuf && ubuf_common && (ubuf != ubuf_common || ...)) return -EINVAL; ... if (!ubuf && ubuf_common) { log->ubuf = ubuf_common; log->level = common->log_level; ... }
Quoted 4 lines
If a caller passes
BPF_LOG_KERNELvia common attributes (ubuf_common== NULL), it seems the inheritance condition (!ubuf &&ubuf_common) will be false, and the common logging level will be silently ignored.
BPF_LOG_KERNEL should only be passed by in-kernel loaders. Entry point for in-kernel loaders is kern_sys_bpf() doesn't pass any common attributes. Hence this situation should not occur.
Quoted 4 lines
Additionally, if a caller specifies
BPF_LOG_KERNELalongside a standard common log level (ubuf == NULL,ubuf_common!= NULL), the mismatch check is bypassed, and the specificBPF_LOG_KERNELis silently overwritten by the common attributes instead of returning-EINVAL.
Again this situation should also be impossible due to the same reasoning as before.
Quoted 2 lines
Could the null-buffer checks be updated to properly support inheriting and validating
BPF_LOG_KERNEL?
I think we should add commentary in bpf_log_attr_init() to clarify these scenarios. Maybe rewriting some checks is also in order.
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
--
Regards,
Siddharth Nayyar
[email protected]
AI reviewThis isn't a bug, but would it read more in line with the BPF multi-line comment style to put the opening /* on its own line here? The text currently starts on the same line as the opening /*:
Quoted 5 lines
kernel/bpf/log.c +0
-0
kernel/bpf/log.c static bool bpf_verifier_log_attr_valid(u32 log_level, char __user *log_buf, u32 · line 29
Quoted 9 lines
kernel/bpf/log.c +6
-0@@ -21,6 +21,15 @@ static bool bpf_verifier_log_attr_valid(u32 log_level, char __user *log_buf, u32/* log buf without log_level is meaningless */ if (log_buf && log_level ==0 )return false; + +/* BPF_LOG_KERNEL is an exclusive internal mode, not a bit flag. +* It must be checked via strict equality to prevent it from being +* combined with userspace flags, which would cause the verifier +* to fallback to userspace logging and copy_to_user() failures. +*/
This isn't a bug, but would it read more in line with the BPF multi-line comment style to put the opening /* on its own line here? The text currently starts on the same line as the opening /*:
/* BPF_LOG_KERNEL is an exclusive internal mode, not a bit flag. * It must be checked via strict equality ...
kernel/bpf/log.c static bool bpf_verifier_log_attr_valid(u32 log_level, char __user *log_buf, u32 · line 35
Quoted 6 lines
kernel/bpf/log.c +3
-0+if (log_level ==BPF_LOG_KERNEL ) +return true; +if (log_level & ~BPF_LOG_MASK )return false;if (log_size >UINT_MAX >>2 )
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/28121265957
Quoted 13 lines · Siddharth Nayyar
When loading a BPF program or BTF from kernel space (e.g. using a light skeleton generated loader module), the kernel module may want to output verifier logs to dmesg by setting
attr->log_level=BPF_LOG_KERNEL(16).Currently, any attempt to do this through the standard syscall path (
kern_sys_bpf->bpf_vlog_init) fails with-EINVALbecausebpf_verifier_log_attr_valid()rejects anylog_levelbit outsideBPF_LOG_MASK. This prevents in-kernel loaders from successfully usingBPF_LOG_KERNEL, causing silent failures if the BPF object is rejected by the verifier.
Quoted 6 lines
This patch modifies
bpf_verifier_log_attr_valid()to acceptBPF_LOG_KERNELunconditionally. To prevent userspace from exploiting this to spam the kernel log, the patch enforces a strict privilege check at the syscall boundary points (btf_parse,bpf_log_attr_init,bpf_log_attr_create_vlog), ensuringBPF_LOG_KERNELis only permitted when uattr.is_kernel is true.
I don't think it's a good idea.BPF_LOG_KERNEL prints to dmesg and it's there for cases where BTF is corrupted in vmlinux or in the module. The verifier log can be large. It's never suitable for dmesg.
Let's step back and discuss the end goal.
Not just this patch, but overall.
You're sending a bunch of patches for various lskel things. Please explain the full picture.
pw-bot: cr
Quoted 6 lines · Alexei Starovoitov
I don't think it's a good idea.
BPF_LOG_KERNELprints to dmesg and it's there for cases where BTF is corrupted in vmlinux or in the module. The verifier log can be large. It's never suitable for dmesg.
Quoted 6 lines
Let's step back and discuss the end goal.
Not just this patch, but overall.
You're sending a bunch of patches for various lskel things. Please explain the full picture.pw-bot: cr
I am trying to enable support for STRUCT_OPS maps in light skeletons. The idea is to enable loading sched_ext eBPF programs using in-kernel loaders, such as a kernel module. I am preparing to send a patch series for the same.
The changes I have been sending fix bugs I found along the way, for example poisoning unresolved weak kfuncs and fixing a typo in struct_ops map FD generation.
This change, however, is not a bug fix but just a quality-of-life improvement. Having the ability to enable BPF logs in dmesg for in-kernel loaders has been helpful for debugging. Therefore I sent this change upstream. I agree that enabling dmesg logs for loaders in production is a bad idea, but in-kernel loaders can use this option during development to ease debugging.
That said, I am happy to drop this change since it is merely a quality of life improvement and doesn't deliver any crucial functionality.
Regards,
Siddharth Nayyar