Skip to contentSkip to conversation

bpf: Add syscall common attrs support for xdp link_create

PATCH Superseded Leon Hwang 4 participants 25 messages Oct 17 – Oct 18

Subsystems (9)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

cover bpf: Add syscall common attrs support for xdp link_create

I'm planning to add syscall common attrs support for LINK_CREATE cmd.

This is the first series for the plan, that adds syscall common attrs support for xdp LINK_CREATE.

Thereafter, retire the tracepoint bpf_xdp_link_attach_failed, because the underlying driver's error can be back-propagated to user space via the syscall common attrs.

Leon Hwang (6):
bpf: Add syscall common attrs support for link_create
bpf: Add syscall common attrs support for xdp link_create
bpf: Retire tracepoint bpf_xdp_link_attach_failed
libbpf: Factor out __bpf_sys_fd helper
libbpf: Add bpf_link_create_log API selftests/bpf: Add tests to verify link_create logs

 include/linux/netdevice.h                     |   3 +-
 include/trace/events/xdp.h                    |  17 ---
 kernel/bpf/syscall.c                          |  36 +++--
 net/core/dev.c                                |   7 +-
 tools/lib/bpf/bpf.c                           |  59 +++++---
 tools/lib/bpf/bpf.h                           |   5 +
 tools/lib/bpf/libbpf.map                      |   1 +
 .../selftests/bpf/prog_tests/xdp_attach.c     |  65 ---------
 .../selftests/bpf/prog_tests/xdp_link.c       | 132 ++++++++++++++++++
 .../bpf/progs/test_xdp_attach_fail.c          |  54 -------
 10 files changed, 215 insertions(+), 164 deletions(-)
 delete mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/6 bpf: Add syscall common attrs support for link_create

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 and invalid attach type.

Links:
[1] https://lore.kernel.org/bpf/[email protected]/

---
 kernel/bpf/syscall.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)
kernel/bpf/syscall.c +27 -7
index 0f27604a54aa..252b479937a4 100644
@@ -5839,10 +5839,13 @@ 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;
+	int ret, err;
 
 	if (CHECK_ATTR(BPF_LINK_CREATE))
 		return -EINVAL;
@@ -5850,14 +5853,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:
@@ -5940,7 +5953,14 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 	}
 
 out:
-	if (ret < 0)
+	/* preserve original error even if log finalization is successful */
+	err = bpf_log_attr_finalize(&attr_log, log);
+	if (err)
+		ret = err;
+
+	kfree(log);
+
+	if (ret < 0 && prog)
 		bpf_prog_put(prog);
 	return ret;
 }
@@ -6513,7 +6533,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);
Signature · 1 line
2.55.0
author
#4 lore raw

PATCH 2/6 bpf: Add syscall common attrs support for xdp link_create

In reply to Leon Hwang

With the syscall common attrs support for link_create, add syscall common attrs support for xdp link_create in order to report the invalid target ifindex error and the underlying driver's error.

---
 include/linux/netdevice.h | 3 ++-
 kernel/bpf/syscall.c      | 2 +-
 net/core/dev.c            | 6 +++++-
 3 files changed, 8 insertions(+), 3 deletions(-)
include/linux/netdevice.h +2 -1
index 3cff2174dc03..4548b0fd824d 100644
@@ -4417,7 +4417,8 @@ struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *d
 struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 				    struct netdev_queue *txq, int *ret);
 
-int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
+int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+			struct bpf_verifier_log *log);
 u8 dev_xdp_prog_count(struct net_device *dev);
 int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
 int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
kernel/bpf/syscall.c +1 -1
index 252b479937a4..e96d5f09486f 100644
@@ -5921,7 +5921,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
 		break;
 #ifdef CONFIG_NET
 	case BPF_PROG_TYPE_XDP:
-		ret = bpf_xdp_link_attach(attr, prog);
+		ret = bpf_xdp_link_attach(attr, prog, log);
 		break;
 	case BPF_PROG_TYPE_SCHED_CLS:
 		if (attr->link_create.attach_type == BPF_TCX_INGRESS ||
net/core/dev.c +5 -1
index 18dc88990510..fdc1de21f358 100644
@@ -97,6 +97,7 @@
 #include <linux/kthread.h>
 #include <linux/bpf.h>
 #include <linux/bpf_trace.h>
+#include <linux/bpf_verifier.h>
 #include <net/net_namespace.h>
 #include <net/sock.h>
 #include <net/busy_poll.h>
@@ -10771,7 +10772,8 @@ static const struct bpf_link_ops bpf_xdp_link_lops = {
 	.update_prog = bpf_xdp_link_update,
 };
 
-int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+			struct bpf_verifier_log *log)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct bpf_link_primer link_primer;
@@ -10784,6 +10786,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
 	if (!dev) {
 		rtnl_unlock();
+		bpf_log(log, "Invalid target_ifindex.\n");
 		return -EINVAL;
 	}
 
@@ -10812,6 +10815,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
+		bpf_log(log, extack._msg);
 		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}
Signature · 1 line
2.55.0
author
#13 lore raw

PATCH 3/6 bpf: Retire tracepoint bpf_xdp_link_attach_failed

In reply to Leon Hwang

In the previous patch, it has a proper way to back-propagate the underlying driver error message to user space via the BPF syscall common attributes.

The tracepoint bpf_xdp_link_attach_failed seems redundant to report the error message. And, the tracepoint doesn't report the ifindex alongside the error message [1].

So, retire the tracepoint bpf_xdp_link_attach_failed and its selftest.

According to the tracepoint change discussion [2], please revert this patch if users complain about the disappeared tracepoint bpf_xdp_link_attach_failed in the future.

Links:
[1] https://lore.kernel.org/bpf/CAFk-A4mE9Jweo2hfX7y_85xbPyt0FqpMT1EvqX1OcYZ=LTLgRA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/CY8PR11MB7134346A3E4BB28ECA28D6E989132@CY8PR11MB7134.namprd11.prod.outlook.com/

---
 include/trace/events/xdp.h                    | 17 -----
 net/core/dev.c                                |  1 -
 .../selftests/bpf/prog_tests/xdp_attach.c     | 65 -------------------
 .../bpf/progs/test_xdp_attach_fail.c          | 54 ---------------
 4 files changed, 137 deletions(-)
 delete mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
include/trace/events/xdp.h +0 -17
index 18c0ac514fcb..f1d4d0702884 100644
@@ -362,23 +362,6 @@ TRACE_EVENT(mem_connect,
 	)
 );
 
-TRACE_EVENT(bpf_xdp_link_attach_failed,
-
-	TP_PROTO(const char *msg),
-
-	TP_ARGS(msg),
-
-	TP_STRUCT__entry(
-		__string(msg, msg)
-	),
-
-	TP_fast_assign(
-		__assign_str(msg);
-	),
-
-	TP_printk("errmsg=%s", __get_str(msg))
-);
-
 #endif /* _TRACE_XDP_H */
 
 #include <trace/define_trace.h>
net/core/dev.c +0 -1
index fdc1de21f358..266b510abd53 100644
@@ -10816,7 +10816,6 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
 		bpf_log(log, extack._msg);
-		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}
 
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +0 -65

80 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c deleted +0 -54

54 lines not on this page · raw message

Signature · 1 line
2.55.0
author
#14 lore raw

PATCH 4/6 libbpf: Factor out __bpf_sys_fd helper

In reply to Leon Hwang

In the next patch, it also needs to use the extended BPF syscall to receive the reported log.

So, factor out __bpf_sys_fd helper.

No functional changes intended.

---
 tools/lib/bpf/bpf.c | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)
tools/lib/bpf/bpf.c +22 -13
index b49822d212ae..17e73e443bc7 100644
@@ -203,6 +203,27 @@ int bump_rlimit_memlock(void)
 	return 0;
 }
 
+static int __sys_bpf_fd(union bpf_attr *attr, const size_t attr_sz, enum bpf_cmd cmd,
+			struct bpf_log_opts *log_opts)
+{
+	const size_t attr_common_sz = sizeof(struct bpf_common_attr);
+	struct bpf_common_attr attr_common;
+	int fd;
+
+	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
+		memset(&attr_common, 0, attr_common_sz);
+		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
+		attr_common.log_size = OPTS_GET(log_opts, size, 0);
+		attr_common.log_level = OPTS_GET(log_opts, level, 0);
+		fd = sys_bpf_ext_fd(cmd, attr, attr_sz, &attr_common, attr_common_sz);
+		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
+	} else {
+		fd = sys_bpf_fd(cmd, attr, attr_sz);
+		OPTS_SET(log_opts, true_size, 0);
+	}
+	return fd;
+}
+
 int bpf_map_create(enum bpf_map_type map_type,
 		   const char *map_name,
 		   __u32 key_size,
@@ -211,8 +232,6 @@ int bpf_map_create(enum bpf_map_type map_type,
 		   const struct bpf_map_create_opts *opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, excl_prog_hash_size);
-	const size_t attr_common_sz = sizeof(struct bpf_common_attr);
-	struct bpf_common_attr attr_common;
 	struct bpf_log_opts *log_opts;
 	union bpf_attr attr;
 	int fd;
@@ -251,17 +270,7 @@ int bpf_map_create(enum bpf_map_type map_type,
 	if (!OPTS_VALID(log_opts, bpf_log_opts))
 		return libbpf_err(-EINVAL);
 
-	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
-		memset(&attr_common, 0, attr_common_sz);
-		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
-		attr_common.log_size = OPTS_GET(log_opts, size, 0);
-		attr_common.log_level = OPTS_GET(log_opts, level, 0);
-		fd = sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &attr_common, attr_common_sz);
-		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
-	} else {
-		fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
-		OPTS_SET(log_opts, true_size, 0);
-	}
+	fd = __sys_bpf_fd(&attr, attr_sz, BPF_MAP_CREATE, log_opts);
 	return libbpf_err_errno(fd);
 }
 
Signature · 1 line
2.55.0
author
#17 lore raw

PATCH 5/6 libbpf: Add bpf_link_create_log API

In reply to Leon Hwang

Add a new API to accept the log opts, instead of extending the existing struct bpf_link_create_opts.

In the struct bpf_link_create_opts, it seems not proper to append a tailing common log field prior to those variant length fields.

Add a new API to avoid breaking the struct bpf_link_create_opts.

---
 tools/lib/bpf/bpf.c      | 24 ++++++++++++++++++++----
 tools/lib/bpf/bpf.h      |  5 +++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 26 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf.c +20 -4
index 17e73e443bc7..eb127d71f3c8 100644
@@ -788,9 +788,10 @@ int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
 	return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL);
 }
 
-int bpf_link_create(int prog_fd, int target_fd,
-		    enum bpf_attach_type attach_type,
-		    const struct bpf_link_create_opts *opts)
+static int __bpf_link_create(int prog_fd, int target_fd,
+			     enum bpf_attach_type attach_type,
+			     const struct bpf_link_create_opts *opts,
+			     struct bpf_log_opts *log_opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
 	__u32 target_btf_id, iter_info_len, relative_id;
@@ -965,7 +966,7 @@ int bpf_link_create(int prog_fd, int target_fd,
 		break;
 	}
 proceed:
-	fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+	fd = __sys_bpf_fd(&attr, attr_sz, BPF_LINK_CREATE, log_opts);
 	if (fd >= 0)
 		return fd;
 	/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
@@ -999,6 +1000,21 @@ int bpf_link_create(int prog_fd, int target_fd,
 	}
 }
 
+int bpf_link_create(int prog_fd, int target_fd,
+		    enum bpf_attach_type attach_type,
+		    const struct bpf_link_create_opts *opts)
+{
+	return __bpf_link_create(prog_fd, target_fd, attach_type, opts, NULL);
+}
+
+int bpf_link_create_log(int prog_fd, int target_fd,
+			enum bpf_attach_type attach_type,
+			const struct bpf_link_create_opts *opts,
+			struct bpf_log_opts *log_opts)
+{
+	return __bpf_link_create(prog_fd, target_fd, attach_type, opts, log_opts);
+}
+
 int bpf_link_detach(int link_fd)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
tools/lib/bpf/bpf.h +5 -0
index 826d9cc9ab65..dedaf8086902 100644
@@ -484,6 +484,11 @@ LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
 			       enum bpf_attach_type attach_type,
 			       const struct bpf_link_create_opts *opts);
 
+LIBBPF_API int bpf_link_create_log(int prog_fd, int target_fd,
+				   enum bpf_attach_type attach_type,
+				   const struct bpf_link_create_opts *opts,
+				   struct bpf_log_opts *log_opts);
+
 LIBBPF_API int bpf_link_detach(int link_fd);
 
 struct bpf_link_update_opts {
tools/lib/bpf/libbpf.map +1 -0
index eeba4072a5da..990e2f926ecc 100644
@@ -458,6 +458,7 @@ LIBBPF_1.7.0 {
 
 LIBBPF_1.8.0 {
 	global:
+		bpf_link_create_log;
 		bpf_map__attach_cgroup_opts;
 		bpf_prog_stream_open;
 		bpf_program__add_flags;
Signature · 1 line
2.55.0
author
#22 lore raw

PATCH 6/6 selftests/bpf: Add tests to verify link_create logs

In reply to Leon Hwang

Add tests to verify the new logs in link_create and xdp link_create, including a log reported via extack when fail to create an xdp link.

---
 .../selftests/bpf/prog_tests/xdp_link.c       | 132 ++++++++++++++++++
 1 file changed, 132 insertions(+)
tools/testing/selftests/bpf/prog_tests/xdp_link.c +132 -0
index e7e9f3c22edf..e2b2ec3cc4e3 100644
@@ -150,3 +150,135 @@ void serial_test_xdp_link(void)
 	test_xdp_link__destroy(skel1);
 	test_xdp_link__destroy(skel2);
 }
+
+static void test_link_create_invalid_prog_fd(void)
+{
+	const char *msg = "Invalid prog_fd.\n";
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	fd = bpf_link_create_log(-1, IFINDEX_LO, BPF_XDP, NULL, &log_opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) {
+		close(fd);
+		return;
+	}
+
+	ASSERT_EQ(fd, -EBADF, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+}
+
+static void test_link_create_invalid_attach_type(void)
+{
+	const char *msg = "Invalid attach_type.\n";
+	struct test_xdp_link *skel;
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	skel = test_xdp_link__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load"))
+		return;
+
+	log_opts.true_size = 0;
+	fd = bpf_link_create_log(bpf_program__fd(skel->progs.xdp_handler), IFINDEX_LO,
+				 BPF_CGROUP_INET_INGRESS, NULL, &log_opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) {
+		close(fd);
+		goto cleanup;
+	}
+
+	ASSERT_EQ(fd, -EINVAL, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+cleanup:
+	test_xdp_link__destroy(skel);
+}
+
+static void test_link_create_xdp_invalid_target_ifindex(void)
+{
+	const char *msg = "Invalid target_ifindex.\n";
+	struct test_xdp_link *skel;
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	skel = test_xdp_link__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load"))
+		return;
+
+	/* Interface indices are positive; zero cannot identify a device. */
+	fd = bpf_link_create_log(bpf_program__fd(skel->progs.xdp_handler), 0,
+				 BPF_XDP, NULL, &log_opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) {
+		close(fd);
+		goto cleanup;
+	}
+
+	ASSERT_EQ(fd, -EINVAL, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+cleanup:
+	test_xdp_link__destroy(skel);
+}
+
+static void test_link_create_xdp_extack(void)
+{
+	const char *msg = "Invalid XDP flags for BPF link attachment";
+	struct test_xdp_link *skel;
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_link_create_opts, opts,
+		    .flags = XDP_FLAGS_REPLACE,
+	);
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	skel = test_xdp_link__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load"))
+		return;
+
+	fd = bpf_link_create_log(bpf_program__fd(skel->progs.xdp_handler), IFINDEX_LO,
+				 BPF_XDP, &opts, &log_opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) {
+		close(fd);
+		goto cleanup;
+	}
+
+	ASSERT_EQ(fd, -EINVAL, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+
+cleanup:
+	test_xdp_link__destroy(skel);
+}
+
+void test_link_create_log(void)
+{
+	if (test__start_subtest("invalid_prog_fd"))
+		test_link_create_invalid_prog_fd();
+	if (test__start_subtest("invalid_attach_type"))
+		test_link_create_invalid_attach_type();
+	if (test__start_subtest("xdp/invalid_target_ifindex"))
+		test_link_create_xdp_invalid_target_ifindex();
+	if (test__start_subtest("xdp/extack"))
+		test_link_create_xdp_extack();
+}
Signature · 1 line
2.55.0
#2 lore raw

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

In reply to Leon Hwang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] bpf: prog refcount underflow and fd leak in link_create() on log error --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] bpf: prog refcount underflow and fd leak in link_create() on log error --

commit 56fa3f4abf91b9651c1cd02884b19754bd9ebcb5
Author: Leon Hwang <[email protected]>

bpf: Add syscall common attrs support for link_create

This commit adds syscall common attributes support to link_create(), creating and finalizing an internal verifier log instance and reporting error messages for an invalid prog_fd or attach_type.

Quoted 54 lines
kernel/bpf/syscall.c +19 -6
index 0f27604a54aae..252b479937a4d 100644
@@ -5839,10 +5839,13 @@ 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;
+	int ret, err;
 
 	if (CHECK_ATTR(BPF_LINK_CREATE))
 		return -EINVAL;
@@ -5850,14 +5853,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:
@@ -5940,7 +5953,14 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 	}
 
 out:
-	if (ret < 0)
+	/* preserve original error even if log finalization is successful */

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr) · line 5966

Quoted 10 lines
kernel/bpf/syscall.c +7 -0
+	err = bpf_log_attr_finalize(&attr_log, log);
+	if (err)
+		ret = err;
+
+	kfree(log);
+
+	if (ret < 0 && prog)
 		bpf_prog_put(prog);
 	return ret;
 }

[Severity: Critical]
Can this underflow the refcount on prog and leak the newly installed link file descriptor if bpf_log_attr_finalize() fails after link attachment succeeds?

When one of the link attach helpers in switch (prog->type) succeeds, it transfers ownership of the prog reference acquired by bpf_prog_get() to link->prog and calls bpf_link_settle() (or bpf_link_new_fd()), which installs the file descriptor via fd_install() and returns the non-negative fd in ret.

At the out label, bpf_log_attr_finalize() is then called:

kernel/bpf/log.c:bpf_log_attr_finalize() {
	err = bpf_vlog_finalize(log, &log_true_size);

	if (attr->offsetof_true_size &&
	    copy_to_bpfptr_offset(attr->uattr, attr->offsetof_true_size, &log_true_size,
				  sizeof(log_true_size)))
		return -EFAULT;

	return err;
}

If uattr_common was passed with size_common large enough to include log_true_size and resides in read-only memory (or is unmapped before finalization), copy_to_bpfptr_offset() returns -EFAULT even when nothing was logged.

In link_create(), ret is then overwritten with -EFAULT, causing if (ret < 0 && prog) to call bpf_prog_put(prog) on the reference that is now owned by the active bpf_link. When the installed link fd is later closed, bpf_link_free() -> bpf_link_dealloc() will call bpf_prog_put(link->prog) a second time:

kernel/bpf/syscall.c:bpf_link_dealloc() {
	if (link->prog)
		bpf_prog_put(link->prog);
	...
}

Should the ret < 0 check for bpf_prog_put(prog) happen before ret is overwritten by err from bpf_log_attr_finalize(), or should log finalization be handled before settling the link?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#18 lore raw

Re: [PATCH bpf-next 5/6] libbpf: Add bpf_link_create_log API

In reply to Leon Hwang
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] libbpf: missing OPTS_VALID() check for log_opts in __bpf_link_create() - [Medium] libbpf…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] libbpf: missing OPTS_VALID() check for log_opts in __bpf_link_create()
- [Medium] libbpf: bpf_link_create_log added to released LIBBPF_1.8.0 in libbpf.map --

commit 5adb04f5088a9ab6aa21d83e4c9370c13ebdaab6
Author: Leon Hwang <[email protected]>

libbpf: Add bpf_link_create_log API

This commit introduces the bpf_link_create_log() API to pass struct bpf_log_opts when creating BPF links without modifying struct bpf_link_create_opts.

Quoted 19 lines
tools/lib/bpf/bpf.c +4 -3
index 17e73e443bc7a..eb127d71f3c83 100644
@@ -788,9 +788,10 @@ int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
 	return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL);
 }
 
-int bpf_link_create(int prog_fd, int target_fd,
-		    enum bpf_attach_type attach_type,
-		    const struct bpf_link_create_opts *opts)
+static int __bpf_link_create(int prog_fd, int target_fd,
+			     enum bpf_attach_type attach_type,
+			     const struct bpf_link_create_opts *opts,
+			     struct bpf_log_opts *log_opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
 	__u32 target_btf_id, iter_info_len, relative_id;

tools/lib/bpf/bpf.c int bpf_link_create(int prog_fd, int target_fd, · line 969

Quoted 5 lines
tools/lib/bpf/bpf.c +1 -1
@@ -965,7 +966,7 @@ int bpf_link_create(int prog_fd, int target_fd,
 		break;
 	}
 proceed:
-	fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+	fd = __sys_bpf_fd(&attr, attr_sz, BPF_LINK_CREATE, log_opts);

[Severity: Medium]
Should __bpf_link_create() validate log_opts with OPTS_VALID(log_opts, bpf_log_opts) before passing it to __sys_bpf_fd()?

Currently __bpf_link_create() validates opts, but not log_opts:

tools/lib/bpf/bpf.c:__bpf_link_create() {
	if (!OPTS_VALID(opts, bpf_link_create_opts))
		return libbpf_err(-EINVAL);
}

When a caller passes a malformed struct bpf_log_opts (such as an invalid sz or non-zero unknown trailing fields) to bpf_link_create_log(), __bpf_link_create() forwards it directly to __sys_bpf_fd(), which accesses log_opts via OPTS_GET() and OPTS_SET() without checking OPTS_VALID().

For comparison, bpf_map_create() validates log_opts before calling __sys_bpf_fd():

tools/lib/bpf/bpf.c:bpf_map_create() {
	log_opts = OPTS_GET(opts, log_opts, NULL);
	if (!OPTS_VALID(log_opts, bpf_log_opts))
		return libbpf_err(-EINVAL);

	fd = __sys_bpf_fd(&attr, attr_sz, BPF_MAP_CREATE, log_opts);
}

[ ... ]

tools/lib/bpf/libbpf.map LIBBPF_1.7.0 { · line 464

Quoted 12 lines
tools/lib/bpf/libbpf.map +1 -0
index eeba4072a5dac..990e2f926ecc3 100644
@@ -458,6 +458,7 @@ LIBBPF_1.7.0 {
 
 LIBBPF_1.8.0 {
 	global:
+		bpf_link_create_log;
 		bpf_map__attach_cgroup_opts;
 		bpf_prog_stream_open;
 		bpf_program__add_flags;

[Severity: Medium]
Should bpf_link_create_log be placed under the LIBBPF_1.9.0 stanza in tools/lib/bpf/libbpf.map instead of LIBBPF_1.8.0?

LIBBPF_1.8.0 is already succeeded by LIBBPF_1.9.0 at the bottom of tools/lib/bpf/libbpf.map:

LIBBPF_1.9.0 {
} LIBBPF_1.8.0;

Adding bpf_link_create_log to LIBBPF_1.8.0 exports the new symbol under the previous version node rather than the current LIBBPF_1.9.0 development cycle.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#5 lore raw
In reply to Leon Hwang
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] bpf: NULL deref and format string bug in bpf_xdp_link_attach() via bpf_log() - [Critical] bpf…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] bpf: NULL deref and format string bug in bpf_xdp_link_attach() via bpf_log()
- [Critical] bpf: prog refcount underflow in link_create() if bpf_log_attr_finalize() fails
- [Low] bpf: link failure in bpf_xdp_link_attach() when !CONFIG_BPF_SYSCALL --

commit fa16fd00d8499a718392032e57d440248e048afc
Author: Leon Hwang <[email protected]>

bpf: Add syscall common attrs support for xdp link_create

This commit passes the verifier log to bpf_xdp_link_attach() so that XDP link creation can report target_ifindex and driver errors through BPF syscall common attributes.

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a · line 5927

Quoted 13 lines
kernel/bpf/syscall.c +1 -1
index 252b479937a4d..e96d5f09486f5 100644
@@ -5921,7 +5921,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
 		break;
 #ifdef CONFIG_NET
 	case BPF_PROG_TYPE_XDP:
-		ret = bpf_xdp_link_attach(attr, prog);
+		ret = bpf_xdp_link_attach(attr, prog, log);
 		break;
 	case BPF_PROG_TYPE_SCHED_CLS:
 		if (attr->link_create.attach_type == BPF_TCX_INGRESS ||

[Severity: Critical]
Can the cleanup path in link_create() underflow prog's reference count if bpf_log_attr_finalize() fails after bpf_xdp_link_attach() succeeds?

When bpf_xdp_link_attach() succeeds, bpf_link_init() transfers ownership of prog's reference to link->link.prog and bpf_link_settle() installs the link file descriptor, returning a non-negative fd in ret.

At the out label in link_create(), bpf_log_attr_finalize() writes log_true_size to uattr_common even when nothing was logged:

kernel/bpf/log.c:bpf_log_attr_finalize() {
	...
	if (attr->offsetof_true_size &&
	    copy_to_bpfptr_offset(attr->uattr, attr->offsetof_true_size, &log_true_size,
				  sizeof(log_true_size)))
		return -EFAULT;
}

If uattr_common points to read-only memory, copy_from_bpfptr() in __sys_bpf() succeeds, bpf_xdp_link_attach() attaches and settles the link, and then bpf_log_attr_finalize() returns -EFAULT:

kernel/bpf/syscall.c:link_create() {
out:

	/* preserve original error even if log finalization is successful */
	err = bpf_log_attr_finalize(&attr_log, log);
	if (err)
		ret = err;

	kfree(log);

	if (ret < 0 && prog)
		bpf_prog_put(prog);
	return ret;
}

Overwriting ret with err causes link_create() to call bpf_prog_put(prog) on a reference that is now owned by the settled bpf_link, leading to a use-after-free of struct bpf_prog while the link remains attached in the process fd table.

Quoted 4 lines
net/core/dev.c +0 -0
index 18dc88990510a..fdc1de21f3589 100644

[ ... ]

Quoted 11 lines
net/core/dev.c +2 -1
@@ -10771,7 +10772,8 @@ static const struct bpf_link_ops bpf_xdp_link_lops = {
 	.update_prog = bpf_xdp_link_update,
 };
 
-int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+			struct bpf_verifier_log *log)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct bpf_link_primer link_primer;

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10791

Quoted 6 lines
net/core/dev.c +1 -0
@@ -10784,6 +10786,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
 	if (!dev) {
 		rtnl_unlock();
+		bpf_log(log, "Invalid target_ifindex.\n");
 		return -EINVAL;
 	}

[Severity: Low]
Will calling bpf_log() in bpf_xdp_link_attach() break linking when CONFIG_NET=y and CONFIG_BPF_SYSCALL=n?

net/core/dev.c is compiled whenever CONFIG_NET=y, and bpf_xdp_link_attach() is not guarded by CONFIG_BPF_SYSCALL. While helpers like bpf_link_init() and bpf_link_prime() have static inline stubs in include/linux/bpf.h when !CONFIG_BPF_SYSCALL, bpf_log() is only compiled via kernel/bpf/log.o when CONFIG_BPF_SYSCALL=y and has no stub in include/linux/bpf_verifier.h.

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10821

Quoted 9 lines
net/core/dev.c +1 -0
@@ -10812,6 +10815,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
+		bpf_log(log, extack._msg);
 		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}

[Severity: Critical]
What happens here when dev_xdp_attach_link() fails without setting an extack message, or when extack._msg contains format specifiers?

In bpf_xdp_link_attach(), extack is zero-initialized on the stack, so extack._msg starts as NULL. Many failure paths under dev_xdp_attach_link() -> dev_xdp_attach() -> dev_xdp_install() return an error without calling NL_SET_ERR_MSG(), such as netif_xdp_propagate() when a device has no ndo_bpf callback, or driver ndo_bpf callbacks like netkit_xdp() and veth_xdp() returning -EOPNOTSUPP or -EINVAL.

When a user provides a log buffer via syscall common attributes and extack._msg is NULL, bpf_log() passes fmt = NULL into bpf_verifier_vlog() -> vscnprintf() -> vsnprintf(), which dereferences fmt unconditionally and crashes the kernel.

Additionally, because bpf_log() is a printf-style function, passing extack._msg directly as the format string rather than using a "%s\n" format specifier allows any '%' characters in a formatted extack message (for example from NL_SET_ERR_MSG_FMT() containing an interface name) to be interpreted as format specifiers by vscnprintf(), and omits the trailing newline. Should this be guarded with if (extack._msg) and logged with bpf_log(log, "%s\n", extack._msg)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#15 lore raw

Re: [PATCH bpf-next 4/6] libbpf: Factor out __bpf_sys_fd helper

In reply to Leon Hwang
TL;DR: Commit message nit only: subject and body call the new helper __bpf_sys_fd, but the code names it __sys_bpf_fd. Should the message match?
Quoted 1 line

libbpf: Factor out __bpf_sys_fd helper

TL;DR: Commit message nit only: subject and body call the new helper __bpf_sys_fd, but the code names it __sys_bpf_fd. Should the message match?

Quoted 10 lines

libbpf: Factor out __bpf_sys_fd helper

In the next patch, it also needs to use the extended BPF syscall to receive the reported log.

So, factor out __bpf_sys_fd helper.

No functional changes intended.

This isn't a bug, but the subject and body name the helper __bpf_sys_fd, while the function the patch adds is:

+static int __sys_bpf_fd(union bpf_attr *attr, const size_t attr_sz, enum bpf_cmd cmd,
+ struct bpf_log_opts *log_opts)

which follows the existing sys_bpf(), sys_bpf_fd() and sys_bpf_ext_fd() naming.

Should the subject and body say __sys_bpf_fd instead, so that searching the log for the real function name finds this commit?

---
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/39042048581

#23 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: Add tests to verify link_create logs

In reply to Leon Hwang
TL;DR: Nits only, in the new link_create log selftests in xdp_link.c: the four subtests repeat the same setup and assertions and could share a helper, and one has a redundant true_size = 0.
Quoted 1 line

selftests/bpf: Add tests to verify link_create logs

TL;DR: Nits only, in the new link_create log selftests in xdp_link.c: the four subtests repeat the same setup and assertions and could share a helper, and one has a redundant true_size = 0.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +0 -0

[ ... ]

Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +12 -0
@@ -150,3 +150,135 @@ void serial_test_xdp_link(void)
 	test_xdp_link__destroy(skel1);
 	test_xdp_link__destroy(skel2);
 }
+
+static void test_link_create_invalid_prog_fd(void)
+{
+	const char *msg = "Invalid prog_fd.\n";
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+

tools/testing/selftests/bpf/prog_tests/xdp_link.c void serial_test_xdp_link(void) · line 174

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +10 -0
+	fd = bpf_link_create_log(-1, IFINDEX_LO, BPF_XDP, NULL, &log_opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) {
+		close(fd);
+		return;
+	}
+
+	ASSERT_EQ(fd, -EBADF, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+}

This isn't a bug, but the four new test_link_create_*() functions repeat the same log_buf and LIBBPF_OPTS(bpf_log_opts, ...) setup, and the same trailing ASSERT_EQ(fd, ...) / ASSERT_STREQ(log_buf, msg) / ASSERT_EQ(log_opts.true_size, strlen(msg) + 1) sequence. They differ only in prog fd, target, attach type, expected errno and message.

This looks close to test_map_create() in map_init.c. Could the four link_create log tests share a small helper that takes the prog fd, target, attach type, opts, expected errno and message?

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +8 -0
+
+static void test_link_create_invalid_attach_type(void)
+{
+	const char *msg = "Invalid attach_type.\n";
+	struct test_xdp_link *skel;
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,

tools/testing/selftests/bpf/prog_tests/xdp_link.c void serial_test_xdp_link(void) · line 192

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +10 -0
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	skel = test_xdp_link__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load"))
+		return;
+
+	log_opts.true_size = 0;

This isn't a bug, but is the log_opts.true_size = 0; line needed here? LIBBPF_OPTS() already zero-initializes log_opts, true_size is only written by libbpf after the syscall, and the other three tests omit this line.

tools/testing/selftests/bpf/prog_tests/xdp_link.c void serial_test_xdp_link(void) · line 194

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +2 -0
+	fd = bpf_link_create_log(bpf_program__fd(skel->progs.xdp_handler), IFINDEX_LO,
+				 BPF_CGROUP_INET_INGRESS, NULL, &log_opts);

[ ... ]

---
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/39042048581

#20 lore raw

Re: [PATCH bpf-next 5/6] libbpf: Add bpf_link_create_log API

In reply to Leon Hwang
TL;DR: Commit message only: the rationale for adding bpf_link_create_log() instead of extending bpf_link_create_opts is vague ("it seems not proper") and one sentence is self-contradictory; no code…
Quoted 1 line

libbpf: Add bpf_link_create_log API

TL;DR: Commit message only: the rationale for adding bpf_link_create_log() instead of extending bpf_link_create_opts is vague ("it seems not proper") and one sentence is self-contradictory; no code issues raised.

Quoted 11 lines

libbpf: Add bpf_link_create_log API

Add a new API to accept the log opts, instead of extending the existing struct bpf_link_create_opts.

In the struct bpf_link_create_opts, it seems not proper to append a tailing common log field prior to those variant length fields.

Add a new API to avoid breaking the struct bpf_link_create_opts.

This isn't a bug, but could the changelog say more concretely why a log field cannot be added to struct bpf_link_create_opts (for example, the trailing union growing for future link types), rather than "it seems not proper"?

The sentence "append a tailing common log field prior to those variant length fields" also reads as self-contradictory, since "append" and "prior to" point in opposite directions.

---
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/39042048581

#7 lore raw
In reply to Leon Hwang
TL;DR: - bpf_log(log, extack._msg) in bpf_xdp_link_attach() may dereference NULL when no extack message is set - bpf_log() may be an undefined reference when CONFIG_BPF_SYSCALL=n
Quoted 1 line

bpf: Add syscall common attrs support for xdp link_create

TL;DR:
- bpf_log(log, extack._msg) in bpf_xdp_link_attach() may dereference NULL when no extack message is set
- bpf_log() may be an undefined reference when CONFIG_BPF_SYSCALL=n

Quoted 4 lines
net/core/dev.c +0 -0
index 18dc88990510..fdc1de21f358 100644

[ ... ]

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10792

Quoted 8 lines
net/core/dev.c +1 -0
@@ -10784,6 +10786,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
 	if (!dev) {
 		rtnl_unlock();
+		bpf_log(log, "Invalid target_ifindex.\n");
 		return -EINVAL;
 	}
 

Can this fail to link when CONFIG_BPF_SYSCALL is not set?

bpf_xdp_link_attach() is built whenever CONFIG_NET=y, but bpf_log() lives in kernel/bpf/log.c, which kernel/bpf/Makefile only builds under CONFIG_BPF_SYSCALL:

  obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o ... log.o ...

include/linux/bpf_verifier.h declares bpf_log() with no stub for the !CONFIG_BPF_SYSCALL case, and CONFIG_NET only does "select BPF". A config with CONFIG_NET=y and CONFIG_BPF_SYSCALL=n looks like it would end with an undefined reference to bpf_log from both of the new calls in this function.

include/linux/bpf.h provides static inline stubs for bpf_link_init(), bpf_link_prime(), bpf_link_settle() and bpf_link_cleanup() so that this function builds without the bpf() syscall.

Should bpf_log() get a similar stub, or should the calls be guarded with IS_ENABLED(CONFIG_BPF_SYSCALL)? None of the later patches in the series add a stub or change the Makefile.

[ ... ]

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10821

Quoted 8 lines
net/core/dev.c +1 -0
@@ -10812,6 +10815,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
+		bpf_log(log, extack._msg);
 		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}

Can extack._msg be NULL here?

extack is zero-initialised (struct netlink_ext_ack extack = {};), and _msg stays NULL whenever dev_xdp_attach_link() fails without anyone calling NL_SET_ERR_MSG(). Many ndo_bpf implementations do exactly that. For example, ixgbe_xdp() -> ixgbe_xdp_setup() has several -EINVAL and -ENOMEM returns that don't set an extack message, and mlx5e_xdp_set() -> mlx5e_xdp_allowed() returns -EINVAL with only a netdev_warn().

If the caller passed BPF_COMMON_ATTRS with log_level != 0 (bpf_verifier_log_attr_valid() accepts log_level=1 even with a NULL log_buf and a log_size of 0), bpf_verifier_log_needed() is true and the call chain becomes:

bpf_log(log, NULL)

    -> bpf_verifier_vlog(log, NULL, args)
      -> vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, NULL, args)
        -> vsnprintf()    /* while (*fmt.str) with fmt.str == NULL */

which dereferences NULL. The tracepoint on the next line copes with NULL because __assign_str() substitutes a placeholder string, and the netlink ack path also checks extack->_msg, so this new bpf_log() call looks like the only consumer without a NULL check.

Passing the message as the format string also means a '%' in an extack message would be interpreted as a conversion, and unlike the other bpf_log() messages in link_create(), this one has no trailing newline. Would something like this be better?

  if (extack._msg)
          bpf_log(log, "%s\n", extack._msg);

This isn't fixed later in the series. Commit 2a63a314e533 ("bpf: Retire tracepoint bpf_xdp_link_attach_failed") removes the tracepoint but keeps bpf_log(log, extack._msg), so no NULL-safe consumer is left on this path.

---
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/39042048581

#9 lore raw
In reply to Leon Hwang
kernel test robot noticed the following build errors:

Hi Leon,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Leon-Hwang/bpf-Add-syscall-common-attrs-support-for-link_create/20261017-222644
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20261017142649.51047-3-leon.hwang%40linux.dev
patch subject: [PATCH bpf-next 2/6] bpf: Add syscall common attrs support for xdp link_create
config: um-i386_defconfig (https://download.01.org/0day-ci/archive/20261018/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261018/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

/usr/bin/ld: net/core/dev.o: in function `bpf_xdp_link_attach':

>> net/core/dev.c:10789:(.text+0x96f9): undefined reference to `bpf_log'

collect2: error: ld returned 1 exit status

vim +10789 net/core/dev.c

10774
10775 int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
10776 struct bpf_verifier_log *log)
10777 {
10778 struct net *net = current->nsproxy->net_ns;
10779 struct bpf_link_primer link_primer;
10780 struct netlink_ext_ack extack = {};
10781 struct bpf_xdp_link *link;
10782 struct net_device *dev;
10783 int err, fd;
10784
10785 rtnl_lock();
10786 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
10787 if (!dev) {
10788 rtnl_unlock();
10789 bpf_log(log, "Invalid target_ifindex.\n");
10790 return -EINVAL;
10791 }
10792
10793 link = kzalloc_obj(*link, GFP_USER);
10794 if (!link) {
10795 err = -ENOMEM;
10796 goto unlock;
10797 }
10798
10799 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog,
10800 attr->link_create.attach_type);
10801 link->dev = dev;
10802 link->flags = attr->link_create.flags;
10803
10804 err = bpf_link_prime(&link->link, &link_primer);
10805 if (err) {
10806 kfree(link);
10807 goto unlock;
10808 }
10809
10810 netdev_lock_ops(dev);
10811 err = dev_xdp_attach_link(dev, &extack, link);
10812 netdev_unlock_ops(dev);
10813 rtnl_unlock();
10814
10815 if (err) {
10816 link->dev = NULL;
10817 bpf_link_cleanup(&link_primer);
10818 bpf_log(log, extack._msg);
10819 trace_bpf_xdp_link_attach_failed(extack._msg);
10820 goto out_put_dev;
10821 }
10822
10823 fd = bpf_link_settle(&link_primer);
10824 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
10825 dev_put(dev);
10826 return fd;
10827
10828 unlock:
10829 rtnl_unlock();
10830
10831 out_put_dev:
10832 dev_put(dev);
10833 return err;
10834 }
10835

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#11 lore raw
In reply to Leon Hwang
kernel test robot noticed the following build errors:

Hi Leon,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Leon-Hwang/bpf-Add-syscall-common-attrs-support-for-link_create/20261017-222644
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20261017142649.51047-3-leon.hwang%40linux.dev
patch subject: [PATCH bpf-next 2/6] bpf: Add syscall common attrs support for xdp link_create
config: um-defconfig (https://download.01.org/0day-ci/archive/20261018/[email protected]/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 6719b9e31d6a93099612c474eec76d9f41d0275a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261018/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

/usr/bin/ld: warning: .tmp_vmlinux1 has a LOAD segment with RWX permissions /usr/bin/ld: net/core/dev.o: in function `bpf_xdp_link_attach':

>> net/core/dev.c:10789:(.ltext+0xcb71): undefined reference to `bpf_log'

clang: error: linker command failed with exit code 1 (use -v to see invocation)

vim +10789 net/core/dev.c

10774
10775 int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
10776 struct bpf_verifier_log *log)
10777 {
10778 struct net *net = current->nsproxy->net_ns;
10779 struct bpf_link_primer link_primer;
10780 struct netlink_ext_ack extack = {};
10781 struct bpf_xdp_link *link;
10782 struct net_device *dev;
10783 int err, fd;
10784
10785 rtnl_lock();
10786 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
10787 if (!dev) {
10788 rtnl_unlock();
10789 bpf_log(log, "Invalid target_ifindex.\n");
10790 return -EINVAL;
10791 }
10792
10793 link = kzalloc_obj(*link, GFP_USER);
10794 if (!link) {
10795 err = -ENOMEM;
10796 goto unlock;
10797 }
10798
10799 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog,
10800 attr->link_create.attach_type);
10801 link->dev = dev;
10802 link->flags = attr->link_create.flags;
10803
10804 err = bpf_link_prime(&link->link, &link_primer);
10805 if (err) {
10806 kfree(link);
10807 goto unlock;
10808 }
10809
10810 netdev_lock_ops(dev);
10811 err = dev_xdp_attach_link(dev, &extack, link);
10812 netdev_unlock_ops(dev);
10813 rtnl_unlock();
10814
10815 if (err) {
10816 link->dev = NULL;
10817 bpf_link_cleanup(&link_primer);
10818 bpf_log(log, extack._msg);
10819 trace_bpf_xdp_link_attach_failed(extack._msg);
10820 goto out_put_dev;
10821 }
10822
10823 fd = bpf_link_settle(&link_primer);
10824 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
10825 dev_put(dev);
10826 return fd;
10827
10828 unlock:
10829 rtnl_unlock();
10830
10831 out_put_dev:
10832 dev_put(dev);
10833 return err;
10834 }
10835

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

author
#3 lore raw

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

In reply to [email protected]

On 2026/10/17 22:39, [email protected] wrote:

[...]

Quoted 10 lines
Quoted 10 lines

out:

kernel/bpf/syscall.c +8 -1
-	if (ret < 0)
+	/* preserve original error even if log finalization is successful */
+	err = bpf_log_attr_finalize(&attr_log, log);
+	if (err)
+		ret = err;
+
+	kfree(log);
+
+	if (ret < 0 && prog)
Quoted 8 lines

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr) · line 5966

Quoted 3 lines
kernel/bpf/syscall.c +0 -0
 		bpf_prog_put(prog);
 	return ret;
 }

[Severity: Critical]
Can this underflow the refcount on prog and leak the newly installed link file descriptor if bpf_log_attr_finalize() fails after link attachment succeeds?

Correct.

To avoid such prog leak, I'd like to report the log_true_size twice.

ret = bpf_log_attr_report_size(&attr_log, 0);
if (ret)

	goto out;
switch (prog->type) {
	/* ... */
}

out:

if (ret) {
	int err =  bpf_log_attr_finalize(&attr_log, log);

	ret = err ? err : ret;
}

if (ret < 0 && prog)

	bpf_prog_put(prog);

kfree(log);
return ret;

Thanks,
Leon

Quoted 1 line

[...]

author
#12 lore raw
In reply to kernel test robot

On 2026/10/18 11:46, kernel test robot wrote:

[...]

Quoted 7 lines

All errors (new ones prefixed by >>):

/usr/bin/ld: warning: .tmp_vmlinux1 has a LOAD segment with RWX permissions /usr/bin/ld: net/core/dev.o: in function `bpf_xdp_link_attach':

Quoted 1 line
Quoted 1 line

net/core/dev.c:10789:(.ltext+0xcb71): undefined reference to `bpf_log'

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Will fix the issue in the next revision.

Thanks,
Leon

Quoted 1 line

[...]

author
#10 lore raw
In reply to kernel test robot

On 2026/10/18 11:28, kernel test robot wrote:

[...]

Quoted 6 lines

All errors (new ones prefixed by >>):

/usr/bin/ld: net/core/dev.o: in function `bpf_xdp_link_attach':

Quoted 1 line
Quoted 1 line

net/core/dev.c:10789:(.text+0x96f9): undefined reference to `bpf_log'

collect2: error: ld returned 1 exit status

Will fix the issue in the next revision.

Thanks,
Leon

Quoted 1 line

[...]

author
#8 lore raw
In reply to [email protected]
Quoted 36 lines · On 2026/10/17 23:03, [email protected] wrote:
Quoted 1 line

bpf: Add syscall common attrs support for xdp link_create

TL;DR:
- bpf_log(log, extack._msg) in bpf_xdp_link_attach() may dereference NULL when no extack message is set
- bpf_log() may be an undefined reference when CONFIG_BPF_SYSCALL=n

Quoted 4 lines
net/core/dev.c +0 -0
index 18dc88990510..fdc1de21f358 100644

[ ... ]

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10792

Quoted 8 lines
net/core/dev.c +1 -0
@@ -10784,6 +10786,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
 	if (!dev) {
 		rtnl_unlock();
+		bpf_log(log, "Invalid target_ifindex.\n");
 		return -EINVAL;
 	}
 

Can this fail to link when CONFIG_BPF_SYSCALL is not set?

bpf_xdp_link_attach() is built whenever CONFIG_NET=y, but bpf_log() lives in kernel/bpf/log.c, which kernel/bpf/Makefile only builds under CONFIG_BPF_SYSCALL:

  obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o ... log.o ...

include/linux/bpf_verifier.h declares bpf_log() with no stub for the !CONFIG_BPF_SYSCALL case, and CONFIG_NET only does "select BPF". A config with CONFIG_NET=y and CONFIG_BPF_SYSCALL=n looks like it would end with an undefined reference to bpf_log from both of the new calls in this function.

Quoted 7 lines

include/linux/bpf.h provides static inline stubs for bpf_link_init(), bpf_link_prime(), bpf_link_settle() and bpf_link_cleanup() so that this function builds without the bpf() syscall.

Should bpf_log() get a similar stub, or should the calls be guarded with IS_ENABLED(CONFIG_BPF_SYSCALL)? None of the later patches in the series add a stub or change the Makefile.

Will add a stub bpf_log() when CONFIG_BPF_SYSCALL is not set.>

Quoted 35 lines

[ ... ]

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10821

Quoted 8 lines
net/core/dev.c +1 -0
@@ -10812,6 +10815,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
+		bpf_log(log, extack._msg);
 		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}

Can extack._msg be NULL here?

extack is zero-initialised (struct netlink_ext_ack extack = {};), and _msg stays NULL whenever dev_xdp_attach_link() fails without anyone calling NL_SET_ERR_MSG(). Many ndo_bpf implementations do exactly that. For example, ixgbe_xdp() -> ixgbe_xdp_setup() has several -EINVAL and -ENOMEM returns that don't set an extack message, and mlx5e_xdp_set() -> mlx5e_xdp_allowed() returns -EINVAL with only a netdev_warn().

If the caller passed BPF_COMMON_ATTRS with log_level != 0 (bpf_verifier_log_attr_valid() accepts log_level=1 even with a NULL log_buf and a log_size of 0), bpf_verifier_log_needed() is true and the call chain becomes:

bpf_log(log, NULL)

    -> bpf_verifier_vlog(log, NULL, args)
      -> vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, NULL, args)
        -> vsnprintf()    /* while (*fmt.str) with fmt.str == NULL */

which dereferences NULL. The tracepoint on the next line copes with NULL because __assign_str() substitutes a placeholder string, and the netlink ack path also checks extack->_msg, so this new bpf_log() call looks like the only consumer without a NULL check.

Quoted 7 lines

Passing the message as the format string also means a '%' in an extack message would be interpreted as a conversion, and unlike the other bpf_log() messages in link_create(), this one has no trailing newline. Would something like this be better?

  if (extack._msg)
          bpf_log(log, "%s\n", extack._msg);

Will change to this.

Thanks,
Leon

Quoted 1 line

[...]

author
#6 lore raw
In reply to [email protected]
Quoted 24 lines · On 2026/10/17 22:48, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] bpf: NULL deref and format string bug in bpf_xdp_link_attach() via bpf_log()
- [Critical] bpf: prog refcount underflow in link_create() if bpf_log_attr_finalize() fails
- [Low] bpf: link failure in bpf_xdp_link_attach() when !CONFIG_BPF_SYSCALL --

commit fa16fd00d8499a718392032e57d440248e048afc
Author: Leon Hwang <[email protected]>

bpf: Add syscall common attrs support for xdp link_create

This commit passes the verifier log to bpf_xdp_link_attach() so that XDP link creation can report target_ifindex and driver errors through BPF syscall common attributes.

Quoted 9 lines
kernel/bpf/syscall.c +0 -1
index 252b479937a4d..e96d5f09486f5 100644
@@ -5921,7 +5921,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a
 		break;
 #ifdef CONFIG_NET
 	case BPF_PROG_TYPE_XDP:
-		ret = bpf_xdp_link_attach(attr, prog);
Quoted 8 lines

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a · line 5927

Quoted 4 lines
kernel/bpf/syscall.c +1 -0
+		ret = bpf_xdp_link_attach(attr, prog, log);
 		break;
 	case BPF_PROG_TYPE_SCHED_CLS:
 		if (attr->link_create.attach_type == BPF_TCX_INGRESS ||

[Severity: Critical]
Can the cleanup path in link_create() underflow prog's reference count if bpf_log_attr_finalize() fails after bpf_xdp_link_attach() succeeds?

Will fix the issue by reporting log_true_size twice. See the pseudocode in patch 1 reply.

Quoted 57 lines

When bpf_xdp_link_attach() succeeds, bpf_link_init() transfers ownership of prog's reference to link->link.prog and bpf_link_settle() installs the link file descriptor, returning a non-negative fd in ret.

At the out label in link_create(), bpf_log_attr_finalize() writes log_true_size to uattr_common even when nothing was logged:

kernel/bpf/log.c:bpf_log_attr_finalize() {
	...
	if (attr->offsetof_true_size &&
	    copy_to_bpfptr_offset(attr->uattr, attr->offsetof_true_size, &log_true_size,
				  sizeof(log_true_size)))
		return -EFAULT;
}

If uattr_common points to read-only memory, copy_from_bpfptr() in __sys_bpf() succeeds, bpf_xdp_link_attach() attaches and settles the link, and then bpf_log_attr_finalize() returns -EFAULT:

kernel/bpf/syscall.c:link_create() {
out:

	/* preserve original error even if log finalization is successful */
	err = bpf_log_attr_finalize(&attr_log, log);
	if (err)
		ret = err;

	kfree(log);

	if (ret < 0 && prog)
		bpf_prog_put(prog);
	return ret;
}

Overwriting ret with err causes link_create() to call bpf_prog_put(prog) on a reference that is now owned by the settled bpf_link, leading to a use-after-free of struct bpf_prog while the link remains attached in the process fd table.

Quoted 4 lines
net/core/dev.c +0 -0
index 18dc88990510a..fdc1de21f3589 100644

[ ... ]

Quoted 13 lines
net/core/dev.c +2 -1
@@ -10771,7 +10772,8 @@ static const struct bpf_link_ops bpf_xdp_link_lops = {
 	.update_prog = bpf_xdp_link_update,
 };
 
-int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
+int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog,
+			struct bpf_verifier_log *log)
 {
 	struct net *net = current->nsproxy->net_ns;
 	struct bpf_link_primer link_primer;
@@ -10784,6 +10786,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
 	if (!dev) {
Quoted 8 lines

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10791

Quoted 4 lines
net/core/dev.c +1 -0
 		rtnl_unlock();
+		bpf_log(log, "Invalid target_ifindex.\n");
 		return -EINVAL;
 	}

[Severity: Low]
Will calling bpf_log() in bpf_xdp_link_attach() break linking when CONFIG_NET=y and CONFIG_BPF_SYSCALL=n?

Correct.

Will provide a stub bpf_log() when CONFIG_BPF_SYSCALL is not set.

Quoted 32 lines

net/core/dev.c is compiled whenever CONFIG_NET=y, and bpf_xdp_link_attach() is not guarded by CONFIG_BPF_SYSCALL. While helpers like bpf_link_init() and bpf_link_prime() have static inline stubs in include/linux/bpf.h when !CONFIG_BPF_SYSCALL, bpf_log() is only compiled via kernel/bpf/log.o when CONFIG_BPF_SYSCALL=y and has no stub in include/linux/bpf_verifier.h.

net/core/dev.c int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 10821

Quoted 9 lines
net/core/dev.c +1 -0
@@ -10812,6 +10815,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
+		bpf_log(log, extack._msg);
 		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}

[Severity: Critical]
What happens here when dev_xdp_attach_link() fails without setting an extack message, or when extack._msg contains format specifiers?>
In bpf_xdp_link_attach(), extack is zero-initialized on the stack, so extack._msg starts as NULL. Many failure paths under dev_xdp_attach_link() -> dev_xdp_attach() -> dev_xdp_install() return an error without calling NL_SET_ERR_MSG(), such as netif_xdp_propagate() when a device has no ndo_bpf callback, or driver ndo_bpf callbacks like netkit_xdp() and veth_xdp() returning -EOPNOTSUPP or -EINVAL.

When a user provides a log buffer via syscall common attributes and extack._msg is NULL, bpf_log() passes fmt = NULL into bpf_verifier_vlog() -> vscnprintf() -> vsnprintf(), which dereferences fmt unconditionally and crashes the kernel.

Quoted 7 lines

Additionally, because bpf_log() is a printf-style function, passing extack._msg directly as the format string rather than using a "%s\n" format specifier allows any '%' characters in a formatted extack message (for example from NL_SET_ERR_MSG_FMT() containing an interface name) to be interpreted as format specifiers by vscnprintf(), and omits the trailing newline. Should this be guarded with if (extack._msg) and logged with bpf_log(log, "%s\n", extack._msg)?

Sure, will change to

if (extack._msg)

	bpf_log(log, "%s\n", extack._msg);

Thanks,
Leon

author
#16 lore raw

Re: [PATCH bpf-next 4/6] libbpf: Factor out __bpf_sys_fd helper

In reply to [email protected]
Quoted 22 lines · On 2026/10/17 22:53, [email protected] wrote:
Quoted 1 line

libbpf: Factor out __bpf_sys_fd helper

TL;DR: Commit message nit only: subject and body call the new helper __bpf_sys_fd, but the code names it __sys_bpf_fd. Should the message match?

Quoted 10 lines

libbpf: Factor out __bpf_sys_fd helper

In the next patch, it also needs to use the extended BPF syscall to receive the reported log.

So, factor out __bpf_sys_fd helper.

No functional changes intended.

This isn't a bug, but the subject and body name the helper __bpf_sys_fd, while the function the patch adds is:

tools/lib/bpf/bpf.c +2 -0
+static int __sys_bpf_fd(union bpf_attr *attr, const size_t attr_sz, enum bpf_cmd cmd,
+			struct bpf_log_opts *log_opts)
Quoted 5 lines

which follows the existing sys_bpf(), sys_bpf_fd() and sys_bpf_ext_fd() naming.

Should the subject and body say __sys_bpf_fd instead, so that searching the log for the real function name finds this commit?

Will change __bpf_sys_fd to __sys_bpf_fd in the subject and body.

Thanks,
Leon

Quoted 7 lines

---
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/39042048581

author
#21 lore raw

Re: [PATCH bpf-next 5/6] libbpf: Add bpf_link_create_log API

In reply to [email protected]
Quoted 18 lines · On 2026/10/17 23:03, [email protected] wrote:
Quoted 1 line

libbpf: Add bpf_link_create_log API

TL;DR: Commit message only: the rationale for adding bpf_link_create_log() instead of extending bpf_link_create_opts is vague ("it seems not proper") and one sentence is self-contradictory; no code issues raised.

Quoted 11 lines

libbpf: Add bpf_link_create_log API

Add a new API to accept the log opts, instead of extending the existing struct bpf_link_create_opts.

In the struct bpf_link_create_opts, it seems not proper to append a tailing common log field prior to those variant length fields.

Add a new API to avoid breaking the struct bpf_link_create_opts.

Quoted 8 lines

This isn't a bug, but could the changelog say more concretely why a log field cannot be added to struct bpf_link_create_opts (for example, the trailing union growing for future link types), rather than "it seems not proper"?

The sentence "append a tailing common log field prior to those variant length fields" also reads as self-contradictory, since "append" and "prior to" point in opposite directions.

Will describe my justification with a concrete case in the next revision.

Thanks,
Leon

Quoted 7 lines

---
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/39042048581

author
#19 lore raw

Re: [PATCH bpf-next 5/6] libbpf: Add bpf_link_create_log API

In reply to [email protected]
Quoted 43 lines · On 2026/10/17 22:43, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] libbpf: missing OPTS_VALID() check for log_opts in __bpf_link_create()
- [Medium] libbpf: bpf_link_create_log added to released LIBBPF_1.8.0 in libbpf.map --

commit 5adb04f5088a9ab6aa21d83e4c9370c13ebdaab6
Author: Leon Hwang <[email protected]>

libbpf: Add bpf_link_create_log API

This commit introduces the bpf_link_create_log() API to pass struct bpf_log_opts when creating BPF links without modifying struct bpf_link_create_opts.

Quoted 19 lines
tools/lib/bpf/bpf.c +4 -3
index 17e73e443bc7a..eb127d71f3c83 100644
@@ -788,9 +788,10 @@ int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
 	return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL);
 }
 
-int bpf_link_create(int prog_fd, int target_fd,
-		    enum bpf_attach_type attach_type,
-		    const struct bpf_link_create_opts *opts)
+static int __bpf_link_create(int prog_fd, int target_fd,
+			     enum bpf_attach_type attach_type,
+			     const struct bpf_link_create_opts *opts,
+			     struct bpf_log_opts *log_opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
 	__u32 target_btf_id, iter_info_len, relative_id;

tools/lib/bpf/bpf.c int bpf_link_create(int prog_fd, int target_fd, · line 969

Quoted 5 lines
tools/lib/bpf/bpf.c +1 -1
@@ -965,7 +966,7 @@ int bpf_link_create(int prog_fd, int target_fd,
 		break;
 	}
 proceed:
-	fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
+	fd = __sys_bpf_fd(&attr, attr_sz, BPF_LINK_CREATE, log_opts);

[Severity: Medium]
Should __bpf_link_create() validate log_opts with OPTS_VALID(log_opts, bpf_log_opts) before passing it to __sys_bpf_fd()?

Quoted 6 lines

Currently __bpf_link_create() validates opts, but not log_opts:

tools/lib/bpf/bpf.c:__bpf_link_create() {
	if (!OPTS_VALID(opts, bpf_link_create_opts))
		return libbpf_err(-EINVAL);
}

Will move OPTS_VALID(opts, bpf_link_create_opts) to __sys_bpf_fd() in the next revision.

Quoted 36 lines

When a caller passes a malformed struct bpf_log_opts (such as an invalid sz or non-zero unknown trailing fields) to bpf_link_create_log(), __bpf_link_create() forwards it directly to __sys_bpf_fd(), which accesses log_opts via OPTS_GET() and OPTS_SET() without checking OPTS_VALID().

For comparison, bpf_map_create() validates log_opts before calling __sys_bpf_fd():

tools/lib/bpf/bpf.c:bpf_map_create() {
	log_opts = OPTS_GET(opts, log_opts, NULL);
	if (!OPTS_VALID(log_opts, bpf_log_opts))
		return libbpf_err(-EINVAL);

	fd = __sys_bpf_fd(&attr, attr_sz, BPF_MAP_CREATE, log_opts);
}

[ ... ]

tools/lib/bpf/libbpf.map LIBBPF_1.7.0 { · line 464

Quoted 12 lines
tools/lib/bpf/libbpf.map +1 -0
index eeba4072a5dac..990e2f926ecc3 100644
@@ -458,6 +458,7 @@ LIBBPF_1.7.0 {
 
 LIBBPF_1.8.0 {
 	global:
+		bpf_link_create_log;
 		bpf_map__attach_cgroup_opts;
 		bpf_prog_stream_open;
 		bpf_program__add_flags;

[Severity: Medium]
Should bpf_link_create_log be placed under the LIBBPF_1.9.0 stanza in tools/lib/bpf/libbpf.map instead of LIBBPF_1.8.0?

Quoted 9 lines

LIBBPF_1.8.0 is already succeeded by LIBBPF_1.9.0 at the bottom of tools/lib/bpf/libbpf.map:

LIBBPF_1.9.0 {
} LIBBPF_1.8.0;

Adding bpf_link_create_log to LIBBPF_1.8.0 exports the new symbol under the previous version node rather than the current LIBBPF_1.9.0 development cycle.

Oops. Will move it to the LIBBPF_1.9.0 section.

Thanks,
Leon

author
#24 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: Add tests to verify link_create logs

In reply to [email protected]
Quoted 36 lines · On 2026/10/17 23:02, [email protected] wrote:
Quoted 1 line

selftests/bpf: Add tests to verify link_create logs

TL;DR: Nits only, in the new link_create log selftests in xdp_link.c: the four subtests repeat the same setup and assertions and could share a helper, and one has a redundant true_size = 0.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +0 -0

[ ... ]

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +20 -0
@@ -150,3 +150,135 @@ void serial_test_xdp_link(void)
 	test_xdp_link__destroy(skel1);
 	test_xdp_link__destroy(skel2);
 }
+
+static void test_link_create_invalid_prog_fd(void)
+{
+	const char *msg = "Invalid prog_fd.\n";
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	fd = bpf_link_create_log(-1, IFINDEX_LO, BPF_XDP, NULL, &log_opts);
+	if (!ASSERT_LT(fd, 0, "bpf_link_create_log")) {
+		close(fd);
+		return;
+	}
+
+	ASSERT_EQ(fd, -EBADF, "fd");
+	ASSERT_STREQ(log_buf, msg, "log_buf");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_link.c void serial_test_xdp_link(void) · line 174

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +2 -0
+	ASSERT_EQ(log_opts.true_size, strlen(msg) + 1, "log_size");
+}

This isn't a bug, but the four new test_link_create_*() functions repeat the same log_buf and LIBBPF_OPTS(bpf_log_opts, ...) setup, and the same trailing ASSERT_EQ(fd, ...) / ASSERT_STREQ(log_buf, msg) / ASSERT_EQ(log_opts.true_size, strlen(msg) + 1) sequence. They differ only in prog fd, target, attach type, expected errno and message.

Will factor out helpers to simplify the tests code.

Quoted 19 lines

This looks close to test_map_create() in map_init.c. Could the four link_create log tests share a small helper that takes the prog fd, target, attach type, opts, expected errno and message?

Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +14 -0
+
+static void test_link_create_invalid_attach_type(void)
+{
+	const char *msg = "Invalid attach_type.\n";
+	struct test_xdp_link *skel;
+	char log_buf[128] = {};
+	int fd;
+	LIBBPF_OPTS(bpf_log_opts, log_opts,
+		    .buf = log_buf,
+		    .size = sizeof(log_buf),
+		    .level = 1,
+	);
+
+	skel = test_xdp_link__open_and_load();
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_link.c void serial_test_xdp_link(void) · line 192

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_link.c +4 -0
+	if (!ASSERT_OK_PTR(skel, "test_xdp_link__open_and_load"))
+		return;
+
+	log_opts.true_size = 0;

This isn't a bug, but is the log_opts.true_size = 0; line needed here? LIBBPF_OPTS() already zero-initializes log_opts, true_size is only written by libbpf after the syscall, and the other three tests omit this line.

Good catch.

This line is unnecessary. Will drop it in the next revision.

Thanks,
Leon