This patch series builds upon the discussion in "[PATCH bpf-next v4 0/4] bpf: Improve error reporting for freplace attachment failure" [1].
This patch series introduces support for *common attributes* in the BPF syscall, providing a unified mechanism for passing shared metadata across all BPF commands.
The initial set of common attributes includes:
1. 'log_buf': User-provided buffer for storing log output. 2. 'log_size': Size of the provided log buffer. 3. 'log_level': Verbosity level for logging. 4. 'log_true_size': The size of log reported by kernel.
With this extension, the BPF syscall will be able to return meaningful error messages (e.g., failures of creating map), improving debuggability and user experience.
Changes: RFC v3 -> v4: * Drop RFC. * Address comments from Andrii: * Add parentheses in 'sys_bpf_ext()'. * Avoid creating new fd in 'probe_sys_bpf_ext()'. * Add a new struct to wrap log fields in libbpf. * Address comments from Alexei: * Do not skip writing to user space when log_true_size is zero. * Do not use 'bool' arguments. * Drop the adding WARN_ON_ONCE()'s.
RFC v2 -> RFC v3: * Rename probe_sys_bpf_extended to probe_sys_bpf_ext. * Refactor reporting 'log_true_size' for prog_load. * Refactor reporting 'btf_log_true_size' for btf_load. * Add warnings for internal bugs in map_create. * Check log_true_size in test cases. * Address comment from Alexei: * Change kvzalloc/kvfree to kzalloc/kfree. * Address comments from Andrii: * Move BPF_COMMON_ATTRS to 'enum bpf_cmd' alongside brief comment. * Add bpf_check_uarg_tail_zero() for extra checks. * Rename sys_bpf_extended to sys_bpf_ext. * Rename sys_bpf_fd_extended to sys_bpf_ext_fd. * Probe the new feature using NULL and -EFAULT. * Move probe_sys_bpf_ext to libbpf_internal.h and drop LIBBPF_API. * Return -EUSERS when log attrs are conflict between bpf_attr and bpf_common_attr. * Avoid touching bpf_vlog_init(). * Update the reason messages in map_create. * Finalize the log using __cleanup(). * Report log size to users. * Change type of log_buf from '__u64' to 'const char *' and cast type using ptr_to_u64() in bpf_map_create(). * Do not return -EOPNOTSUPP when kernel doesn't support this feature in bpf_map_create(). * Add log_level support for map creation for consistency. * Address comment from Eduard: * Use common_attrs->log_level instead of BPF_LOG_FIXED.
RFC v1 -> RFC v2: * Fix build error reported by test bot. * Address comments from Alexei: * Drop new uapi for freplace. * Add common attributes support for prog_load and btf_load. * Add common attributes support for map_create.
Links: [1] https://lore.kernel.org/bpf/20250224153352.64689-1-leon.hwang@linux.dev/
Leon Hwang (9): bpf: Extend bpf syscall with common attributes support libbpf: Add support for extended bpf syscall bpf: Refactor reporting log_true_size for prog_load bpf: Add common attr support for prog_load bpf: Refactor reporting btf_log_true_size for btf_load bpf: Add common attr support for btf_load bpf: Add common attr support for map_create libbpf: Add common attr support for map_create selftests/bpf: Add tests to verify map create failure log
include/linux/bpf.h | 2 +- include/linux/btf.h | 2 +- include/linux/syscalls.h | 3 +- include/uapi/linux/bpf.h | 8 + kernel/bpf/btf.c | 25 +- kernel/bpf/syscall.c | 223 ++++++++++++++++-- kernel/bpf/verifier.c | 12 +- tools/include/uapi/linux/bpf.h | 8 + tools/lib/bpf/bpf.c | 49 +++- tools/lib/bpf/bpf.h | 17 +- tools/lib/bpf/features.c | 8 + tools/lib/bpf/libbpf_internal.h | 3 + .../selftests/bpf/prog_tests/map_init.c | 143 +++++++++++ 13 files changed, 448 insertions(+), 55 deletions(-)
-- 2.52.0
Extend the BPF syscall to support a set of common attributes shared across all BPF commands:
1. 'log_buf': User-provided buffer for storing logs. 2. 'log_size': Size of the log buffer. 3. 'log_level': Log verbosity level. 4. 'log_true_size': The size of log reported by kernel.
These common attributes are passed as the 4th argument to the BPF syscall, with the 5th argument specifying the size of this structure.
To indicate the use of these common attributes from userspace, a new flag 'BPF_COMMON_ATTRS' ('1 << 16') is introduced. This flag is OR-ed into the 'cmd' field of the syscall.
When 'cmd & BPF_COMMON_ATTRS' is set, the kernel will copy the common attributes from userspace into kernel space for use.
Signed-off-by: Leon Hwang leon.hwang@linux.dev --- include/linux/syscalls.h | 3 ++- include/uapi/linux/bpf.h | 8 ++++++++ kernel/bpf/syscall.c | 25 +++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 8 ++++++++ 4 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index cf84d98964b2..729659202d77 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -937,7 +937,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags, asmlinkage long sys_getrandom(char __user *buf, size_t count, unsigned int flags); asmlinkage long sys_memfd_create(const char __user *uname_ptr, unsigned int flags); -asmlinkage long sys_bpf(int cmd, union bpf_attr __user *attr, unsigned int size); +asmlinkage long sys_bpf(int cmd, union bpf_attr __user *attr, unsigned int size, + struct bpf_common_attr __user *attr_common, unsigned int size_common); asmlinkage long sys_execveat(int dfd, const char __user *filename, const char __user *const __user *argv, const char __user *const __user *envp, int flags); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 84ced3ed2d21..dcae1f3e50b7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -986,6 +986,7 @@ enum bpf_cmd { BPF_PROG_STREAM_READ_BY_FD, BPF_PROG_ASSOC_STRUCT_OPS, __MAX_BPF_CMD, + BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying bpf_common_attr. */ };
enum bpf_map_type { @@ -1489,6 +1490,13 @@ struct bpf_stack_build_id { }; };
+struct bpf_common_attr { + __u64 log_buf; + __u32 log_size; + __u32 log_level; + __u32 log_true_size; +}; + #define BPF_OBJ_NAME_LEN 16U
enum { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6dd2ad2f9e81..8f464b847405 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6160,8 +6160,10 @@ static int prog_assoc_struct_ops(union bpf_attr *attr) return ret; }
-static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) +static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, + bpfptr_t uattr_common, unsigned int size_common) { + struct bpf_common_attr common_attrs; union bpf_attr attr; int err;
@@ -6175,6 +6177,20 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) if (copy_from_bpfptr(&attr, uattr, size) != 0) return -EFAULT;
+ memset(&common_attrs, 0, sizeof(common_attrs)); + if (cmd & BPF_COMMON_ATTRS) { + err = bpf_check_uarg_tail_zero(uattr_common, sizeof(common_attrs), size_common); + if (err) + return err; + + cmd &= ~BPF_COMMON_ATTRS; + size_common = min_t(u32, size_common, sizeof(common_attrs)); + if (copy_from_bpfptr(&common_attrs, uattr_common, size_common) != 0) + return -EFAULT; + } else { + size_common = 0; + } + err = security_bpf(cmd, &attr, size, uattr.is_kernel); if (err < 0) return err; @@ -6310,9 +6326,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) return err; }
-SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) +SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size, + struct bpf_common_attr __user *, uattr_common, unsigned int, size_common) { - return __sys_bpf(cmd, USER_BPFPTR(uattr), size); + return __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common); }
static bool syscall_prog_is_valid_access(int off, int size, @@ -6343,7 +6360,7 @@ BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size) default: return -EINVAL; } - return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size); + return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size, KERNEL_BPFPTR(NULL), 0); }
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6b92b0847ec2..2cb847b38f20 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -986,6 +986,7 @@ enum bpf_cmd { BPF_PROG_STREAM_READ_BY_FD, BPF_PROG_ASSOC_STRUCT_OPS, __MAX_BPF_CMD, + BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying bpf_common_attr. */ };
enum bpf_map_type { @@ -1489,6 +1490,13 @@ struct bpf_stack_build_id { }; };
+struct bpf_common_attr { + __u64 log_buf; + __u32 log_size; + __u32 log_level; + __u32 log_true_size; +}; + #define BPF_OBJ_NAME_LEN 16U
enum {
To support the extended BPF syscall introduced in the previous commit, introduce the following internal APIs:
* 'sys_bpf_ext()' * 'sys_bpf_ext_fd()' They wrap the raw 'syscall()' interface to support passing extended attributes. * 'probe_sys_bpf_ext()' Check whether current kernel supports the extended attributes.
Signed-off-by: Leon Hwang leon.hwang@linux.dev --- tools/lib/bpf/bpf.c | 34 +++++++++++++++++++++++++++++++++ tools/lib/bpf/features.c | 8 ++++++++ tools/lib/bpf/libbpf_internal.h | 3 +++ 3 files changed, 45 insertions(+)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 21b57a629916..689ade4a822b 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -69,6 +69,40 @@ static inline __u64 ptr_to_u64(const void *ptr) return (__u64) (unsigned long) ptr; }
+static inline int sys_bpf_ext(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + cmd = common_attrs ? (cmd | BPF_COMMON_ATTRS) : (cmd & ~BPF_COMMON_ATTRS); + return syscall(__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_ext_fd(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + int fd; + + fd = sys_bpf_ext(cmd, attr, size, common_attrs, size_common); + return ensure_good_fd(fd); +} + +int probe_sys_bpf_ext(void) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + union bpf_attr attr; + int fd; + + memset(&attr, 0, attr_sz); + fd = syscall(__NR_bpf, BPF_PROG_LOAD | BPF_COMMON_ATTRS, &attr, attr_sz, NULL, + sizeof(struct bpf_common_attr)); + if (fd >= 0) + close(fd); + return errno == EFAULT; +} + static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) { diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index b842b83e2480..d786a815f1ae 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -506,6 +506,11 @@ static int probe_kern_arg_ctx_tag(int token_fd) return probe_fd(prog_fd); }
+static int probe_kern_extended_syscall(int token_fd) +{ + return probe_sys_bpf_ext(); +} + typedef int (*feature_probe_fn)(int /* token_fd */);
static struct kern_feature_cache feature_cache; @@ -581,6 +586,9 @@ static struct kern_feature_desc { [FEAT_BTF_QMARK_DATASEC] = { "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec, }, + [FEAT_EXTENDED_SYSCALL] = { + "Kernel supports extended syscall", probe_kern_extended_syscall, + }, };
bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index fc59b21b51b5..e2a6ef4b45ae 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -392,6 +392,8 @@ enum kern_feature_id { FEAT_ARG_CTX_TAG, /* Kernel supports '?' at the front of datasec names */ FEAT_BTF_QMARK_DATASEC, + /* Kernel supports extended syscall */ + FEAT_EXTENDED_SYSCALL, __FEAT_CNT, };
@@ -757,4 +759,5 @@ int probe_fd(int fd); #define SHA256_DWORD_SIZE SHA256_DIGEST_LENGTH / sizeof(__u64)
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH]); +int probe_sys_bpf_ext(void); #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
In the next commit, it will be able to report logs via extended common attributes, which will report 'log_true_size' via the extended common attributes meanwhile.
Therefore, refactor the way of 'log_true_size' reporting in order to report 'log_true_size' via the extended common attributes easily.
Signed-off-by: Leon Hwang leon.hwang@linux.dev --- include/linux/bpf.h | 2 +- kernel/bpf/syscall.c | 21 +++++++++++++++++---- kernel/bpf/verifier.c | 12 ++---------- 3 files changed, 20 insertions(+), 15 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h index a63e47d2109c..26fbc550e5aa 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2868,7 +2868,7 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size, size_t actual_size);
/* verify correctness of eBPF program */ -int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size); +int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
#ifndef CONFIG_BPF_JIT_ALWAYS_ON void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 8f464b847405..1739601fb7bd 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2862,7 +2862,7 @@ static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog) /* last field in 'union bpf_attr' used by this command */ #define BPF_PROG_LOAD_LAST_FIELD keyring_id
-static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) +static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) { enum bpf_prog_type type = attr->prog_type; struct bpf_prog *prog, *dst_prog = NULL; @@ -3080,7 +3080,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) goto free_prog_sec;
/* run eBPF verifier */ - err = bpf_check(&prog, attr, uattr, uattr_size); + err = bpf_check(&prog, attr, uattr); if (err < 0) goto free_used_maps;
@@ -6160,12 +6160,22 @@ static int prog_assoc_struct_ops(union bpf_attr *attr) return ret; }
+static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size) +{ + if (size >= offsetofend(union bpf_attr, log_true_size) && + copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), + &attr->log_true_size, sizeof(attr->log_true_size))) + return -EFAULT; + + return 0; +} + static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, bpfptr_t uattr_common, unsigned int size_common) { struct bpf_common_attr common_attrs; union bpf_attr attr; - int err; + int err, ret;
err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); if (err) @@ -6215,7 +6225,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err = map_freeze(&attr); break; case BPF_PROG_LOAD: - err = bpf_prog_load(&attr, uattr, size); + attr.log_true_size = 0; + err = bpf_prog_load(&attr, uattr); + ret = copy_prog_load_log_true_size(&attr, uattr, size); + err = ret ? ret : err; break; case BPF_OBJ_PIN: err = bpf_obj_pin(&attr); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 9394b0de2ef0..ab5eacdde92c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -25096,12 +25096,11 @@ static int compute_scc(struct bpf_verifier_env *env) return err; }
-int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size) +int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) { u64 start_time = ktime_get_ns(); struct bpf_verifier_env *env; int i, len, ret = -EINVAL, err; - u32 log_true_size; bool is_priv;
BTF_TYPE_EMIT(enum bpf_features); @@ -25300,17 +25299,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->prog->aux->verified_insns = env->insn_processed;
/* preserve original error even if log finalization is successful */ - err = bpf_vlog_finalize(&env->log, &log_true_size); + err = bpf_vlog_finalize(&env->log, &attr->log_true_size); if (err) ret = err;
- if (uattr_size >= offsetofend(union bpf_attr, log_true_size) && - copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), - &log_true_size, sizeof(log_true_size))) { - ret = -EFAULT; - goto err_release_maps; - } - if (ret) goto err_release_maps;
The log buffer of common attributes would be confusing with the one in 'union bpf_attr' for BPF_PROG_LOAD.
In order to clarify the usage of these two log buffers, they both can be used for logging if:
* They are same, including 'log_buf', 'log_level' and 'log_size'. * One of them is missing, then another one will be used for logging.
If they both have 'log_buf' but they are not same totally, return -EUSERS.
Signed-off-by: Leon Hwang leon.hwang@linux.dev --- kernel/bpf/syscall.c | 51 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 1739601fb7bd..ad565f569a4f 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6160,14 +6160,55 @@ static int prog_assoc_struct_ops(union bpf_attr *attr) return ret; }
-static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size) +static int check_log_attrs(u64 log_buf, u32 log_size, u32 log_level, + struct bpf_common_attr *common_attrs) +{ + if (log_buf && common_attrs->log_buf && (log_buf != common_attrs->log_buf || + log_size != common_attrs->log_size || + log_level != common_attrs->log_level)) + return -EUSERS; + + return 0; +} + +static int check_prog_load_log_attrs(union bpf_attr *attr, struct bpf_common_attr *common_attrs) +{ + int err; + + err = check_log_attrs(attr->log_buf, attr->log_size, attr->log_level, common_attrs); + if (err) + return err; + + if (!attr->log_buf && common_attrs->log_buf) { + attr->log_buf = common_attrs->log_buf; + attr->log_size = common_attrs->log_size; + attr->log_level = common_attrs->log_level; + } + + return 0; +} + +static int copy_common_attr_log_true_size(bpfptr_t uattr, unsigned int size, u32 *log_true_size) +{ + if (size >= offsetofend(struct bpf_common_attr, log_true_size) && + copy_to_bpfptr_offset(uattr, offsetof(struct bpf_common_attr, log_true_size), + log_true_size, sizeof(*log_true_size))) + return -EFAULT; + + return 0; +} + +static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size, + struct bpf_common_attr *common_attrs, bpfptr_t uattr_common, + unsigned int size_common) { if (size >= offsetofend(union bpf_attr, log_true_size) && copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), &attr->log_true_size, sizeof(attr->log_true_size))) return -EFAULT;
- return 0; + return copy_common_attr_log_true_size(uattr_common, size_common, + &attr->log_true_size); }
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, @@ -6225,9 +6266,13 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err = map_freeze(&attr); break; case BPF_PROG_LOAD: + err = check_prog_load_log_attrs(&attr, &common_attrs); + if (err) + break; attr.log_true_size = 0; err = bpf_prog_load(&attr, uattr); - ret = copy_prog_load_log_true_size(&attr, uattr, size); + ret = copy_prog_load_log_true_size(&attr, uattr, size, &common_attrs, uattr_common, + size_common); err = ret ? ret : err; break; case BPF_OBJ_PIN:
In the next commit, it will be able to report logs via extended common attributes, which will report 'log_true_size' via the extended common attributes meanwhile.
Therefore, refactor the way of 'btf_log_true_size' reporting in order to report 'log_true_size' via the extended common attributes easily.
Signed-off-by: Leon Hwang leon.hwang@linux.dev --- include/linux/btf.h | 2 +- kernel/bpf/btf.c | 25 +++++-------------------- kernel/bpf/syscall.c | 19 ++++++++++++++++--- 3 files changed, 22 insertions(+), 24 deletions(-)
diff --git a/include/linux/btf.h b/include/linux/btf.h index 691f09784933..2b27fdd567f5 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -144,7 +144,7 @@ const char *btf_get_name(const struct btf *btf); void btf_get(struct btf *btf); void btf_put(struct btf *btf); const struct btf_header *btf_header(const struct btf *btf); -int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); +int btf_new_fd(union bpf_attr *attr, bpfptr_t uattr); struct btf *btf_get_by_fd(int fd); int btf_get_info_by_fd(const struct btf *btf, const union bpf_attr *attr, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 539c9fdea41d..9efcbb489edb 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5745,22 +5745,7 @@ static int btf_check_type_tags(struct btf_verifier_env *env, return 0; }
-static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 uattr_size) -{ - u32 log_true_size; - int err; - - err = bpf_vlog_finalize(log, &log_true_size); - - if (uattr_size >= offsetofend(union bpf_attr, btf_log_true_size) && - copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size), - &log_true_size, sizeof(log_true_size))) - err = -EFAULT; - - return err; -} - -static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) +static struct btf *btf_parse(union bpf_attr *attr, bpfptr_t uattr) { bpfptr_t btf_data = make_bpfptr(attr->btf, uattr.is_kernel); char __user *log_ubuf = u64_to_user_ptr(attr->btf_log_buf); @@ -5841,7 +5826,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat } }
- err = finalize_log(&env->log, uattr, uattr_size); + err = bpf_vlog_finalize(&env->log, &attr->btf_log_true_size); if (err) goto errout_free;
@@ -5853,7 +5838,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat btf_free_struct_meta_tab(btf); errout: /* overwrite err with -ENOSPC or -EFAULT */ - ret = finalize_log(&env->log, uattr, uattr_size); + ret = bpf_vlog_finalize(&env->log, &attr->btf_log_true_size); if (ret) err = ret; errout_free: @@ -8017,12 +8002,12 @@ static int __btf_new_fd(struct btf *btf) return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); }
-int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) +int btf_new_fd(union bpf_attr *attr, bpfptr_t uattr) { struct btf *btf; int ret;
- btf = btf_parse(attr, uattr, uattr_size); + btf = btf_parse(attr, uattr); if (IS_ERR(btf)) return PTR_ERR(btf);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ad565f569a4f..ce349a059d5d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5422,7 +5422,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
-static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size) +static int bpf_btf_load(union bpf_attr *attr, bpfptr_t uattr) { struct bpf_token *token = NULL;
@@ -5449,7 +5449,7 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_
bpf_token_put(token);
- return btf_new_fd(attr, uattr, uattr_size); + return btf_new_fd(attr, uattr); }
#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd @@ -6211,6 +6211,16 @@ static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, un &attr->log_true_size); }
+static int copy_btf_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size) +{ + if (size >= offsetofend(union bpf_attr, btf_log_true_size) && + copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size), + &attr->btf_log_true_size, sizeof(attr->btf_log_true_size))) + return -EFAULT; + + return 0; +} + static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, bpfptr_t uattr_common, unsigned int size_common) { @@ -6318,7 +6328,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err = bpf_raw_tracepoint_open(&attr); break; case BPF_BTF_LOAD: - err = bpf_btf_load(&attr, uattr, size); + attr.btf_log_true_size = 0; + err = bpf_btf_load(&attr, uattr); + ret = copy_btf_load_log_true_size(&attr, uattr, size); + err = ret ? ret : err; break; case BPF_BTF_GET_FD_BY_ID: err = bpf_btf_get_fd_by_id(&attr);
linux-kselftest-mirror@lists.linaro.org