[PATCH bpf-next v4 1/6] bpf: Add syscall common attrs support for link_create

From: Leon Hwang

Date: Sat Oct 24 2026 - 11:38:06 EST


Similar to the syscall common attrs support for map_create [1], add the
syscall common attrs support for link_create.

Create and finalize the internal verifier log instance.

Then, report the error messages for invalid prog fd, invalid attach type,
and invalid prog type.

Links:
[1] https://lore.kernel.org/bpf/20260514004634.3DA09C19425@xxxxxxxxxxxxxxx/

Link: https://lore.kernel.org/bpf/20260514004634.3DA09C19425@xxxxxxxxxxxxxxx/
Signed-off-by: Leon Hwang <leon.hwang@xxxxxxxxx>
---
kernel/bpf/syscall.c | 35 ++++++++++++++++++++++++++++-------
1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 90bc7230afa8..db870e795d04 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -5862,8 +5862,11 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
}

#define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd
-static int link_create(union bpf_attr *attr, bpfptr_t uattr)
+static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_attr *attr_common,
+ bpfptr_t uattr_common, u32 size_common)
{
+ struct bpf_verifier_log *log;
+ struct bpf_log_attr attr_log;
struct bpf_prog *prog;
int ret;

@@ -5873,14 +5876,24 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
if (attr->link_create.attach_type == BPF_STRUCT_OPS)
return bpf_struct_ops_link_create(attr);

+ log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, size_common);
+ if (IS_ERR(log))
+ return PTR_ERR(log);
+
prog = bpf_prog_get(attr->link_create.prog_fd);
- if (IS_ERR(prog))
- return PTR_ERR(prog);
+ if (IS_ERR(prog)) {
+ bpf_log(log, "Invalid prog_fd.\n");
+ ret = PTR_ERR(prog);
+ prog = NULL;
+ goto out;
+ }

ret = bpf_prog_attach_check_attach_type(prog,
attr->link_create.attach_type);
- if (ret)
+ if (ret) {
+ bpf_log(log, "Invalid attach_type.\n");
goto out;
+ }

switch (prog->type) {
case BPF_PROG_TYPE_CGROUP_SKB:
@@ -5959,12 +5972,20 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
ret = bpf_uprobe_multi_link_attach(attr, prog);
break;
default:
+ bpf_log(log, "Invalid prog type.\n");
ret = -EINVAL;
}

out:
- if (ret < 0)
- bpf_prog_put(prog);
+ if (ret < 0) {
+ int err;
+
+ err = bpf_log_attr_finalize(&attr_log, log);
+ ret = err ? err : ret;
+ if (prog)
+ bpf_prog_put(prog);
+ }
+ kfree(log);
return ret;
}

@@ -6536,7 +6557,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
break;
case BPF_LINK_CREATE:
- err = link_create(&attr, uattr);
+ err = link_create(&attr, uattr, &attr_common, uattr_common, size_common);
break;
case BPF_LINK_UPDATE:
err = link_update(&attr);
--
2.55.0