Skip to contentSkip to conversation

Add skb + xdp dynptrs

PATCH Superseded Joanne Koong 2 participants 14 messages Feb 26, 2023

Subsystems (9)

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v12 cover Add skb + xdp dynptrs

This patchset is the 2nd in the dynptr series. The 1st can be found here [0].

This patchset adds skb and xdp type dynptrs, which have two main benefits for packet parsing:
* allowing operations on sizes that are not statically known at compile-time (eg variable-sized accesses).
* more ergonomic and less brittle iteration through data (eg does not need manual if checking for being within bounds of data_end)

When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs, there is no noticeable difference. Patch 9 contains more details as well as examples of how to use skb and xdp dynptrs.

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

--
Changelog:

v11 = https://lore.kernel.org/bpf/[email protected]/
v11 -> v12:
* Change constant mem size checking to use "__szk" kfunc annotation for slices
* Use autoloading for success selftests

v10 = https://lore.kernel.org/bpf/[email protected]/
v10 -> v11:
* Reject bpf_dynptr_slice_rdwr() for non-writable progs at load time instead of runtime
* Add additional patch (__uninit kfunc annotation)
* Expand on documentation
* Add bpf_dynptr_write() calls for persisting writes in tests

v9 = https://lore.kernel.org/bpf/[email protected]/
v9 -> v10:
* Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr interface
* Add some more tests
* Split up patchset into more parts to make it easier to review

v8 = https://lore.kernel.org/bpf/[email protected]/
v8 -> v9:
* Fix dynptr_get_type() to check non-stack dynptrs

v7 = https://lore.kernel.org/bpf/[email protected]/
v7 -> v8:
* Change helpers to kfuncs
* Add 2 new patches (1/5 and 2/5)

v6 = https://lore.kernel.org/bpf/[email protected]/
v6 -> v7
* Change bpf_dynptr_data() to return read-only data slices if the skb prog is read-only (Martin)
* Add test "skb_invalid_write" to test that writes to rd-only data slices are rejected

v5 = https://lore.kernel.org/bpf/[email protected]/
v5 -> v6
* Address kernel test robot errors by static inlining

v4 = https://lore.kernel.org/bpf/[email protected]/
v4 -> v5
* Address kernel test robot errors for configs w/out CONFIG_NET set
* For data slices, return PTR_TO_MEM instead of PTR_TO_PACKET (Kumar)
* Split selftests into subtests (Andrii)
* Remove insn patching. Use rdonly and rdwr protos for dynptr skb construction (Andrii)
* bpf_dynptr_data() returns NULL for rd-only dynptrs. There will be a separate bpf_dynptr_data_rdonly() added later (Andrii and Kumar)

v3 = https://lore.kernel.org/bpf/[email protected]/
v3 -> v4
* Forgot to commit --amend the kernel test robot error fixups

v2 = https://lore.kernel.org/bpf/[email protected]/
v2 -> v3
* Fix kernel test robot build test errors

v1 = https://lore.kernel.org/bpf/[email protected]/
v1 -> v2
* Return data slices to rd-only skb dynptrs (Martin)
* bpf_dynptr_write allows writes to frags for skb dynptrs, but always invalidates associated data slices (Martin)
* Use switch casing instead of ifs (Andrii)
* Use 0xFD for experimental kind number in the selftest (Zvi)
* Put selftest conversions w/ dynptrs into new files (Alexei)
* Add new selftest "test_cls_redirect_dynptr.c"

Joanne Koong (10):
bpf: Support "sk_buff" and "xdp_buff" as valid kfunc arg types
bpf: Refactor process_dynptr_func
bpf: Allow initializing dynptrs in kfuncs
bpf: Define no-ops for externally called bpf dynptr functions
bpf: Refactor verifier dynptr into get_dynptr_arg_reg
bpf: Add __uninit kfunc annotation
bpf: Add skb dynptrs
bpf: Add xdp dynptrs
bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr selftests/bpf: tests for using dynptrs to parse skb and xdp buffers

 Documentation/bpf/kfuncs.rst                  |  17 +
 include/linux/bpf.h                           |  95 +-
 include/linux/bpf_verifier.h                  |   3 -
 include/linux/filter.h                        |  46 +
 include/uapi/linux/bpf.h                      |  18 +-
 kernel/bpf/btf.c                              |  22 +
 kernel/bpf/helpers.c                          | 221 +++-
 kernel/bpf/verifier.c                         | 413 ++++++--
 net/core/filter.c                             | 108 +-
 tools/include/uapi/linux/bpf.h                |  18 +-
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  38 +
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |  74 +-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  93 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |  11 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 287 ++++-
 .../selftests/bpf/progs/dynptr_success.c      |  55 +-
 .../bpf/progs/test_cls_redirect_dynptr.c      | 980 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 487 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 114 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 257 +++++
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 24 files changed, 3318 insertions(+), 186 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/bpf_kfuncs.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v12 1/10 bpf: Support "sk_buff" and "xdp_buff" as valid kfunc arg types

The bpf mirror of the in-kernel sk_buff and xdp_buff data structures are __sk_buff and xdp_md. Currently, when we pass in the program ctx to a kfunc where the program ctx is a skb or xdp buffer, we reject the program if the in-kernel definition is sk_buff/xdp_buff instead of __sk_buff/xdp_md.

This change allows "sk_buff <--> __sk_buff" and "xdp_buff <--> xdp_md" to be recognized as valid matches. The user program may pass in their program ctx as a __sk_buff or xdp_md, and the in-kernel definition of the kfunc may define this arg as a sk_buff or xdp_buff.

---
 kernel/bpf/btf.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/btf.c +4 -0
index fa22ec79ac0e..84cca8473873 100644
@@ -5683,6 +5683,10 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
 	 * int socket_filter_bpf_prog(struct __sk_buff *skb)
 	 * { // no fields of skb are ever used }
 	 */
+	if (strcmp(ctx_tname, "__sk_buff") == 0 && strcmp(tname, "sk_buff") == 0)
+		return ctx_type;
+	if (strcmp(ctx_tname, "xdp_md") == 0 && strcmp(tname, "xdp_buff") == 0)
+		return ctx_type;
 	if (strcmp(ctx_tname, tname)) {
 		/* bpf_user_pt_regs_t is a typedef, so resolve it to
 		 * underlying struct and check name again
Signature · 1 line
2.34.1
#2 lore raw

PATCH v12 2/10 bpf: Refactor process_dynptr_func

In reply to Joanne Koong

This change cleans up process_dynptr_func's flow to be more intuitive and updates some comments with more context.

---
 include/linux/bpf_verifier.h |  3 --
 kernel/bpf/verifier.c        | 58 ++++++++++++++++++------------------
 2 files changed, 29 insertions(+), 32 deletions(-)
include/linux/bpf_verifier.h +0 -3
index cf1bb1cf4a7b..b26ff2a8f63b 100644
@@ -616,9 +616,6 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env,
 			   enum bpf_arg_type arg_type);
 int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
 		   u32 regno, u32 mem_size);
-struct bpf_call_arg_meta;
-int process_dynptr_func(struct bpf_verifier_env *env, int regno,
-			enum bpf_arg_type arg_type, struct bpf_call_arg_meta *meta);
 
 /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */
 static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
kernel/bpf/verifier.c +29 -29

104 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

PATCH v12 3/10 bpf: Allow initializing dynptrs in kfuncs

In reply to Joanne Koong

This change allows kfuncs to take in an uninitialized dynptr as a parameter. Before this change, only helper functions could successfully use uninitialized dynptrs. This change moves the memory access check (including stack state growing and slot marking) into process_dynptr_func(), which both helpers and kfuncs call into.

---
 kernel/bpf/verifier.c | 67 ++++++++++++++-----------------------------
 1 file changed, 22 insertions(+), 45 deletions(-)
kernel/bpf/verifier.c +22 -45
index baecccc5fa7a..a4cca21aefc3 100644
@@ -268,7 +268,6 @@ struct bpf_call_arg_meta {
 	u32 ret_btf_id;
 	u32 subprogno;
 	struct btf_field *kptr_field;
-	u8 uninit_dynptr_regno;
 };
 
 struct btf *btf_vmlinux;
@@ -6225,10 +6224,11 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
  * Helpers which do not mutate the bpf_dynptr set MEM_RDONLY in their argument
  * type, and declare it as 'const struct bpf_dynptr *' in their prototype.
  */
-int process_dynptr_func(struct bpf_verifier_env *env, int regno,
-			enum bpf_arg_type arg_type, struct bpf_call_arg_meta *meta)
+int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
+			enum bpf_arg_type arg_type)
 {
 	struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
+	int err;
 
 	/* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an
 	 * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*):
@@ -6254,23 +6254,23 @@ int process_dynptr_func(struct bpf_verifier_env *env, int regno,
 	 *		 to.
 	 */
 	if (arg_type & MEM_UNINIT) {
+		int i;
+
 		if (!is_dynptr_reg_valid_uninit(env, reg)) {
 			verbose(env, "Dynptr has to be an uninitialized dynptr\n");
 			return -EINVAL;
 		}
 
-		/* We only support one dynptr being uninitialized at the moment,
-		 * which is sufficient for the helper functions we have right now.
-		 */
-		if (meta->uninit_dynptr_regno) {
-			verbose(env, "verifier internal error: multiple uninitialized dynptr args\n");
-			return -EFAULT;
+		/* we write BPF_DW bits (8 bytes) at a time */
+		for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) {
+			err = check_mem_access(env, insn_idx, regno,
+					       i, BPF_DW, BPF_WRITE, -1, false);
+			if (err)
+				return err;
 		}
 
-		meta->uninit_dynptr_regno = regno;
+		err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx);
 	} else /* MEM_RDONLY and None case from above */ {
-		int err;
-
 		/* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */
 		if (reg->type == CONST_PTR_TO_DYNPTR && !(arg_type & MEM_RDONLY)) {
 			verbose(env, "cannot pass pointer to const bpf_dynptr, the helper mutates it\n");
@@ -6306,10 +6306,8 @@ int process_dynptr_func(struct bpf_verifier_env *env, int regno,
 		}
 
 		err = mark_dynptr_read(env, reg);
-		if (err)
-			return err;
 	}
-	return 0;
+	return err;
 }
 
 static bool arg_type_is_mem_size(enum bpf_arg_type type)
@@ -6719,7 +6717,8 @@ static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state
 
 static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 			  struct bpf_call_arg_meta *meta,
-			  const struct bpf_func_proto *fn)
+			  const struct bpf_func_proto *fn,
+			  int insn_idx)
 {
 	u32 regno = BPF_REG_1 + arg;
 	struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
@@ -6932,7 +6931,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 		err = check_mem_size_reg(env, reg, regno, true, meta);
 		break;
 	case ARG_PTR_TO_DYNPTR:
-		err = process_dynptr_func(env, regno, arg_type, meta);
+		err = process_dynptr_func(env, regno, insn_idx, arg_type);
 		if (err)
 			return err;
 		break;
@@ -8218,7 +8217,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 	meta.func_id = func_id;
 	/* check args */
 	for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
-		err = check_func_arg(env, i, &meta, fn);
+		err = check_func_arg(env, i, &meta, fn, insn_idx);
 		if (err)
 			return err;
 	}
@@ -8243,30 +8242,6 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 
 	regs = cur_regs(env);
 
-	/* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot
-	 * be reinitialized by any dynptr helper. Hence, mark_stack_slots_dynptr
-	 * is safe to do directly.
-	 */
-	if (meta.uninit_dynptr_regno) {
-		if (regs[meta.uninit_dynptr_regno].type == CONST_PTR_TO_DYNPTR) {
-			verbose(env, "verifier internal error: CONST_PTR_TO_DYNPTR cannot be initialized\n");
-			return -EFAULT;
-		}
-		/* we write BPF_DW bits (8 bytes) at a time */
-		for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) {
-			err = check_mem_access(env, insn_idx, meta.uninit_dynptr_regno,
-					       i, BPF_DW, BPF_WRITE, -1, false);
-			if (err)
-				return err;
-		}
-
-		err = mark_stack_slots_dynptr(env, &regs[meta.uninit_dynptr_regno],
-					      fn->arg_type[meta.uninit_dynptr_regno - BPF_REG_1],
-					      insn_idx);
-		if (err)
-			return err;
-	}
-
 	if (meta.release_regno) {
 		err = -EINVAL;
 		/* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot
@@ -9475,7 +9450,8 @@ static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env,
 						  &meta->arg_rbtree_root.field);
 }
 
-static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta)
+static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta,
+			    int insn_idx)
 {
 	const char *func_name = meta->func_name, *ref_tname;
 	const struct btf *btf = meta->btf;
@@ -9672,7 +9648,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 				return -EINVAL;
 			}
 
-			ret = process_dynptr_func(env, regno, ARG_PTR_TO_DYNPTR | MEM_RDONLY, NULL);
+			ret = process_dynptr_func(env, regno, insn_idx,
+						  ARG_PTR_TO_DYNPTR | MEM_RDONLY);
 			if (ret < 0)
 				return ret;
 			break;
@@ -9880,7 +9857,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	}
 
 	/* Check the arguments */
-	err = check_kfunc_args(env, &meta);
+	err = check_kfunc_args(env, &meta, insn_idx);
 	if (err < 0)
 		return err;
 	/* In case of release function, we get register number of refcounted
Signature · 1 line
2.34.1
#4 lore raw

PATCH v12 4/10 bpf: Define no-ops for externally called bpf dynptr functions

In reply to Joanne Koong

Some bpf dynptr functions will be called from places where if CONFIG_BPF_SYSCALL is not set, then the dynptr function is undefined. For example, when skb type dynptrs are added in the next commit, dynptr functions are called from net/core/filter.c

This patch defines no-op implementations of these dynptr functions so that they do not break compilation by being an undefined reference.

---
 include/linux/bpf.h | 75 +++++++++++++++++++++++++++------------------
 1 file changed, 45 insertions(+), 30 deletions(-)
include/linux/bpf.h +45 -30
index 520b238abd5a..296841a31749 100644
@@ -1124,6 +1124,33 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
 	return bpf_func(ctx, insnsi);
 }
 
+/* the implementation of the opaque uapi struct bpf_dynptr */
+struct bpf_dynptr_kern {
+	void *data;
+	/* Size represents the number of usable bytes of dynptr data.
+	 * If for example the offset is at 4 for a local dynptr whose data is
+	 * of type u64, the number of usable bytes is 4.
+	 *
+	 * The upper 8 bits are reserved. It is as follows:
+	 * Bits 0 - 23 = size
+	 * Bits 24 - 30 = dynptr type
+	 * Bit 31 = whether dynptr is read-only
+	 */
+	u32 size;
+	u32 offset;
+} __aligned(8);
+
+enum bpf_dynptr_type {
+	BPF_DYNPTR_TYPE_INVALID,
+	/* Points to memory that is local to the bpf program */
+	BPF_DYNPTR_TYPE_LOCAL,
+	/* Underlying data is a ringbuf record */
+	BPF_DYNPTR_TYPE_RINGBUF,
+};
+
+int bpf_dynptr_check_size(u32 size);
+u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr);
+
 #ifdef CONFIG_BPF_JIT
 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
@@ -2266,6 +2293,11 @@ static inline bool has_current_bpf_ctx(void)
 }
 
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
+
+void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
+		     enum bpf_dynptr_type type, u32 offset, u32 size);
+void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
+void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr);
 #else /* !CONFIG_BPF_SYSCALL */
 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
 {
@@ -2495,6 +2527,19 @@ static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 static inline void bpf_cgrp_storage_free(struct cgroup *cgroup)
 {
 }
+
+static inline void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
+				   enum bpf_dynptr_type type, u32 offset, u32 size)
+{
+}
+
+static inline void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
+{
+}
+
+static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
+{
+}
 #endif /* CONFIG_BPF_SYSCALL */
 
 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
@@ -2913,36 +2958,6 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
 			u32 num_args, struct bpf_bprintf_data *data);
 void bpf_bprintf_cleanup(struct bpf_bprintf_data *data);
 
-/* the implementation of the opaque uapi struct bpf_dynptr */
-struct bpf_dynptr_kern {
-	void *data;
-	/* Size represents the number of usable bytes of dynptr data.
-	 * If for example the offset is at 4 for a local dynptr whose data is
-	 * of type u64, the number of usable bytes is 4.
-	 *
-	 * The upper 8 bits are reserved. It is as follows:
-	 * Bits 0 - 23 = size
-	 * Bits 24 - 30 = dynptr type
-	 * Bit 31 = whether dynptr is read-only
-	 */
-	u32 size;
-	u32 offset;
-} __aligned(8);
-
-enum bpf_dynptr_type {
-	BPF_DYNPTR_TYPE_INVALID,
-	/* Points to memory that is local to the bpf program */
-	BPF_DYNPTR_TYPE_LOCAL,
-	/* Underlying data is a kernel-produced ringbuf record */
-	BPF_DYNPTR_TYPE_RINGBUF,
-};
-
-void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
-		     enum bpf_dynptr_type type, u32 offset, u32 size);
-void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
-int bpf_dynptr_check_size(u32 size);
-u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr);
-
 #ifdef CONFIG_BPF_LSM
 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
 void bpf_cgroup_atype_put(int cgroup_atype);
Signature · 1 line
2.34.1
#5 lore raw

PATCH v12 5/10 bpf: Refactor verifier dynptr into get_dynptr_arg_reg

In reply to Joanne Koong

This commit refactors the logic for determining which register in a function is the dynptr into "get_dynptr_arg_reg". This will be used in the future when the dynptr reg for BPF_FUNC_dynptr_write will need to be obtained in order to support writes for skb dynptrs.

---
 kernel/bpf/verifier.c | 80 +++++++++++++++++++++++++++----------------
 1 file changed, 50 insertions(+), 30 deletions(-)
kernel/bpf/verifier.c +50 -30
index a4cca21aefc3..cedd38292a62 100644
@@ -6689,6 +6689,28 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env,
 	}
 }
 
+static struct bpf_reg_state *get_dynptr_arg_reg(struct bpf_verifier_env *env,
+						const struct bpf_func_proto *fn,
+						struct bpf_reg_state *regs)
+{
+	struct bpf_reg_state *state = NULL;
+	int i;
+
+	for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++)
+		if (arg_type_is_dynptr(fn->arg_type[i])) {
+			if (state) {
+				verbose(env, "verifier internal error: multiple dynptr args\n");
+				return NULL;
+			}
+			state = &regs[BPF_REG_1 + i];
+		}
+
+	if (!state)
+		verbose(env, "verifier internal error: no dynptr arg found\n");
+
+	return state;
+}
+
 static int dynptr_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
 {
 	struct bpf_func_state *state = func(env, reg);
@@ -8326,43 +8348,41 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		}
 		break;
 	case BPF_FUNC_dynptr_data:
-		for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
-			if (arg_type_is_dynptr(fn->arg_type[i])) {
-				struct bpf_reg_state *reg = &regs[BPF_REG_1 + i];
-				int id, ref_obj_id;
-
-				if (meta.dynptr_id) {
-					verbose(env, "verifier internal error: meta.dynptr_id already set\n");
-					return -EFAULT;
-				}
-
-				if (meta.ref_obj_id) {
-					verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
-					return -EFAULT;
-				}
+	{
+		struct bpf_reg_state *reg;
+		int id, ref_obj_id;
 
-				id = dynptr_id(env, reg);
-				if (id < 0) {
-					verbose(env, "verifier internal error: failed to obtain dynptr id\n");
-					return id;
-				}
+		reg = get_dynptr_arg_reg(env, fn, regs);
+		if (!reg)
+			return -EFAULT;
 
-				ref_obj_id = dynptr_ref_obj_id(env, reg);
-				if (ref_obj_id < 0) {
-					verbose(env, "verifier internal error: failed to obtain dynptr ref_obj_id\n");
-					return ref_obj_id;
-				}
 
-				meta.dynptr_id = id;
-				meta.ref_obj_id = ref_obj_id;
-				break;
-			}
+		if (meta.dynptr_id) {
+			verbose(env, "verifier internal error: meta.dynptr_id already set\n");
+			return -EFAULT;
 		}
-		if (i == MAX_BPF_FUNC_REG_ARGS) {
-			verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");
+		if (meta.ref_obj_id) {
+			verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
 			return -EFAULT;
 		}
+
+		id = dynptr_id(env, reg);
+		if (id < 0) {
+			verbose(env, "verifier internal error: failed to obtain dynptr id\n");
+			return id;
+		}
+
+		ref_obj_id = dynptr_ref_obj_id(env, reg);
+		if (ref_obj_id < 0) {
+			verbose(env, "verifier internal error: failed to obtain dynptr ref_obj_id\n");
+			return ref_obj_id;
+		}
+
+		meta.dynptr_id = id;
+		meta.ref_obj_id = ref_obj_id;
+
 		break;
+	}
 	case BPF_FUNC_user_ringbuf_drain:
 		err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
 					set_user_ringbuf_callback_state);
Signature · 1 line
2.34.1
#6 lore raw

PATCH v12 6/10 bpf: Add __uninit kfunc annotation

In reply to Joanne Koong

This patch adds __uninit as a kfunc annotation.

This will be useful for scenarios such as for example in dynptrs, indicating whether the dynptr should be checked by the verifier as an initialized or an uninitialized dynptr.

Without this annotation, the alternative would be needing to hard-code in the verifier the specific kfunc to indicate that arg should be treated as an uninitialized arg.

---
 Documentation/bpf/kfuncs.rst | 17 +++++++++++++++++
 kernel/bpf/verifier.c        | 18 ++++++++++++++++--
 2 files changed, 33 insertions(+), 2 deletions(-)
Documentation/bpf/kfuncs.rst +17 -0
index 226313747be5..9a78533d25ac 100644
@@ -100,6 +100,23 @@ Hence, whenever a constant scalar argument is accepted by a kfunc which is not a
 size parameter, and the value of the constant matters for program safety, __k
 suffix should be used.
 
+2.2.2 __uninit Annotation
+--------------------
+
+This annotation is used to indicate that the argument will be treated as
+uninitialized.
+
+An example is given below::
+
+        __bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr_kern *ptr__uninit)
+        {
+        ...
+        }
+
+Here, the dynptr will be treated as an uninitialized dynptr. Without this
+annotation, the verifier will reject the program if the dynptr passed in is
+not initialized.
+
 .. _BPF_kfunc_nodef:
 
 2.3 Using an existing kernel function
kernel/bpf/verifier.c +16 -2
index cedd38292a62..3a7f69b29053 100644
@@ -8727,6 +8727,11 @@ static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param
 	return __kfunc_param_match_suffix(btf, arg, "__alloc");
 }
 
+static bool is_kfunc_arg_uninit(const struct btf *btf, const struct btf_param *arg)
+{
+	return __kfunc_param_match_suffix(btf, arg, "__uninit");
+}
+
 static bool is_kfunc_arg_scalar_with_name(const struct btf *btf,
 					  const struct btf_param *arg,
 					  const char *name)
@@ -9662,17 +9667,26 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 				return ret;
 			break;
 		case KF_ARG_PTR_TO_DYNPTR:
+		{
+			enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
+
 			if (reg->type != PTR_TO_STACK &&
 			    reg->type != CONST_PTR_TO_DYNPTR) {
 				verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i);
 				return -EINVAL;
 			}
 
-			ret = process_dynptr_func(env, regno, insn_idx,
-						  ARG_PTR_TO_DYNPTR | MEM_RDONLY);
+			if (reg->type == CONST_PTR_TO_DYNPTR)
+				dynptr_arg_type |= MEM_RDONLY;
+
+			if (is_kfunc_arg_uninit(btf, &args[i]))
+				dynptr_arg_type |= MEM_UNINIT;
+
+			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);
 			if (ret < 0)
 				return ret;
 			break;
+		}
 		case KF_ARG_PTR_TO_LIST_HEAD:
 			if (reg->type != PTR_TO_MAP_VALUE &&
 			    reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
Signature · 1 line
2.34.1
#7 lore raw

PATCH v12 7/10 bpf: Add skb dynptrs

In reply to Joanne Koong

Add skb dynptrs, which are dynptrs whose underlying pointer points to a skb. The dynptr acts on skb data. skb dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of skb->data and skb->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds of data_end).

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error)

For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, reading and writing from/to data in the head as well as from/to non-linear paged buffers is supported. Data slices through the bpf_dynptr_data API are not supported; instead bpf_dynptr_slice() and bpf_dynptr_slice_rdwr() (added in subsequent commit) should be used.

For examples of how skb dynptrs can be used, please see the attached selftests.

---
 include/linux/bpf.h            | 14 ++++++-
 include/linux/filter.h         | 18 ++++++++
 include/uapi/linux/bpf.h       | 13 +++++-
 kernel/bpf/btf.c               | 18 ++++++++
 kernel/bpf/helpers.c           | 76 +++++++++++++++++++++++++++-------
 kernel/bpf/verifier.c          | 61 +++++++++++++++++++++++++++
 net/core/filter.c              | 67 ++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 13 +++++-
 8 files changed, 261 insertions(+), 19 deletions(-)
include/linux/bpf.h +13 -1
index 296841a31749..e7436d7615b0 100644
@@ -607,11 +607,14 @@ enum bpf_type_flag {
 	 */
 	NON_OWN_REF		= BIT(14 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to sk_buff */
+	DYNPTR_TYPE_SKB		= BIT(15 + BPF_BASE_TYPE_BITS),
+
 	__BPF_TYPE_FLAG_MAX,
 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
 };
 
-#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
+#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB)
 
 /* Max number of base types. */
 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
@@ -1146,6 +1149,8 @@ enum bpf_dynptr_type {
 	BPF_DYNPTR_TYPE_LOCAL,
 	/* Underlying data is a ringbuf record */
 	BPF_DYNPTR_TYPE_RINGBUF,
+	/* Underlying data is a sk_buff */
+	BPF_DYNPTR_TYPE_SKB,
 };
 
 int bpf_dynptr_check_size(u32 size);
@@ -2846,6 +2851,8 @@ u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
 				struct bpf_insn *insn_buf,
 				struct bpf_prog *prog,
 				u32 *target_size);
+int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,
+			       struct bpf_dynptr_kern *ptr);
 #else
 static inline bool bpf_sock_common_is_valid_access(int off, int size,
 						   enum bpf_access_type type,
@@ -2867,6 +2874,11 @@ static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
 {
 	return 0;
 }
+static inline int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,
+					     struct bpf_dynptr_kern *ptr)
+{
+	return -EOPNOTSUPP;
+}
 #endif
 
 #ifdef CONFIG_INET
include/linux/filter.h +18 -0
index 1727898f1641..de18e844d15e 100644
@@ -1542,4 +1542,22 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u64 index
 	return XDP_REDIRECT;
 }
 
+#ifdef CONFIG_NET
+int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);
+int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
+			  u32 len, u64 flags);
+#else /* CONFIG_NET */
+static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
+				       void *to, u32 len)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset,
+					const void *from, u32 len, u64 flags)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* CONFIG_NET */
+
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +11 -2
index 62ce1f5d1b1d..d0351d30e551 100644
@@ -5325,11 +5325,17 @@ union bpf_attr {
  *	Description
  *		Write *len* bytes from *src* into *dst*, starting from *offset*
  *		into *dst*.
- *		*flags* is currently unused.
+ *
+ *		*flags* must be 0 except for skb-type dynptrs.
+ *
+ *		For skb-type dynptrs:
+ *		    *  For *flags*, please see the flags accepted by
+ *		       **bpf_skb_store_bytes**\ ().
  *	Return
  *		0 on success, -E2BIG if *offset* + *len* exceeds the length
  *		of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
- *		is a read-only dynptr or if *flags* is not 0.
+ *		is a read-only dynptr or if *flags* is not correct. For skb-type dynptrs,
+ *		other errors correspond to errors returned by **bpf_skb_store_bytes**\ ().
  *
  * void *bpf_dynptr_data(const struct bpf_dynptr *ptr, u32 offset, u32 len)
  *	Description
@@ -5337,6 +5343,9 @@ union bpf_attr {
  *
  *		*len* must be a statically known value. The returned data slice
  *		is invalidated whenever the dynptr is invalidated.
+ *
+ *		skb type dynptrs may not use bpf_dynptr_data. They should
+ *		instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr.
  *	Return
  *		Pointer to the underlying dynptr data, NULL if the dynptr is
  *		read-only, if the dynptr is invalid, or if the offset and length
kernel/bpf/btf.c +18 -0
index 84cca8473873..ef2d8969ed1f 100644
@@ -207,6 +207,11 @@ enum btf_kfunc_hook {
 	BTF_KFUNC_HOOK_TRACING,
 	BTF_KFUNC_HOOK_SYSCALL,
 	BTF_KFUNC_HOOK_FMODRET,
+	BTF_KFUNC_HOOK_CGROUP_SKB,
+	BTF_KFUNC_HOOK_SCHED_ACT,
+	BTF_KFUNC_HOOK_SK_SKB,
+	BTF_KFUNC_HOOK_SOCKET_FILTER,
+	BTF_KFUNC_HOOK_LWT,
 	BTF_KFUNC_HOOK_MAX,
 };
 
@@ -7708,6 +7713,19 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
 		return BTF_KFUNC_HOOK_TRACING;
 	case BPF_PROG_TYPE_SYSCALL:
 		return BTF_KFUNC_HOOK_SYSCALL;
+	case BPF_PROG_TYPE_CGROUP_SKB:
+		return BTF_KFUNC_HOOK_CGROUP_SKB;
+	case BPF_PROG_TYPE_SCHED_ACT:
+		return BTF_KFUNC_HOOK_SCHED_ACT;
+	case BPF_PROG_TYPE_SK_SKB:
+		return BTF_KFUNC_HOOK_SK_SKB;
+	case BPF_PROG_TYPE_SOCKET_FILTER:
+		return BTF_KFUNC_HOOK_SOCKET_FILTER;
+	case BPF_PROG_TYPE_LWT_OUT:
+	case BPF_PROG_TYPE_LWT_IN:
+	case BPF_PROG_TYPE_LWT_XMIT:
+	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
+		return BTF_KFUNC_HOOK_LWT;
 	default:
 		return BTF_KFUNC_HOOK_MAX;
 	}
kernel/bpf/helpers.c +62 -14

127 lines not on this page · raw message

kernel/bpf/verifier.c +61 -0

115 lines not on this page · raw message

net/core/filter.c +67 -0

82 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +11 -2
index 62ce1f5d1b1d..d0351d30e551 100644
@@ -5325,11 +5325,17 @@ union bpf_attr {
  *	Description
  *		Write *len* bytes from *src* into *dst*, starting from *offset*
  *		into *dst*.
- *		*flags* is currently unused.
+ *
+ *		*flags* must be 0 except for skb-type dynptrs.
+ *
+ *		For skb-type dynptrs:
+ *		    *  For *flags*, please see the flags accepted by
+ *		       **bpf_skb_store_bytes**\ ().
  *	Return
  *		0 on success, -E2BIG if *offset* + *len* exceeds the length
  *		of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
- *		is a read-only dynptr or if *flags* is not 0.
+ *		is a read-only dynptr or if *flags* is not correct. For skb-type dynptrs,
+ *		other errors correspond to errors returned by **bpf_skb_store_bytes**\ ().
  *
  * void *bpf_dynptr_data(const struct bpf_dynptr *ptr, u32 offset, u32 len)
  *	Description
@@ -5337,6 +5343,9 @@ union bpf_attr {
  *
  *		*len* must be a statically known value. The returned data slice
  *		is invalidated whenever the dynptr is invalidated.
+ *
+ *		skb type dynptrs may not use bpf_dynptr_data. They should
+ *		instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr.
  *	Return
  *		Pointer to the underlying dynptr data, NULL if the dynptr is
  *		read-only, if the dynptr is invalid, or if the offset and length
Signature · 1 line
2.34.1
#8 lore raw

PATCH v12 8/10 bpf: Add xdp dynptrs

In reply to Joanne Koong

Add xdp dynptrs, which are dynptrs whose underlying pointer points to a xdp_buff. The dynptr acts on xdp data. xdp dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of xdp->data and xdp->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds of data_end).

For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. Data slices through the bpf_dynptr_data API are not supported; instead bpf_dynptr_slice() and bpf_dynptr_slice_rdwr() should be used.

For examples of how xdp dynptrs can be used, please see the attached selftests.

---
 include/linux/bpf.h            |  8 +++++++-
 include/linux/filter.h         | 14 +++++++++++++
 include/uapi/linux/bpf.h       |  2 +-
 kernel/bpf/helpers.c           |  9 ++++++++-
 kernel/bpf/verifier.c          | 10 +++++++++
 net/core/filter.c              | 37 ++++++++++++++++++++++++++++++++--
 tools/include/uapi/linux/bpf.h |  2 +-
 7 files changed, 76 insertions(+), 6 deletions(-)
include/linux/bpf.h +7 -1
index e7436d7615b0..23ec684e660d 100644
@@ -610,11 +610,15 @@ enum bpf_type_flag {
 	/* DYNPTR points to sk_buff */
 	DYNPTR_TYPE_SKB		= BIT(15 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to xdp_buff */
+	DYNPTR_TYPE_XDP		= BIT(16 + BPF_BASE_TYPE_BITS),
+
 	__BPF_TYPE_FLAG_MAX,
 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
 };
 
-#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB)
+#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \
+				 | DYNPTR_TYPE_XDP)
 
 /* Max number of base types. */
 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
@@ -1151,6 +1155,8 @@ enum bpf_dynptr_type {
 	BPF_DYNPTR_TYPE_RINGBUF,
 	/* Underlying data is a sk_buff */
 	BPF_DYNPTR_TYPE_SKB,
+	/* Underlying data is a xdp_buff */
+	BPF_DYNPTR_TYPE_XDP,
 };
 
 int bpf_dynptr_check_size(u32 size);
include/linux/filter.h +14 -0
index de18e844d15e..3f6992261ec5 100644
@@ -1546,6 +1546,8 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u64 index
 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);
 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
 			  u32 len, u64 flags);
+int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
+int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
 #else /* CONFIG_NET */
 static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
 				       void *to, u32 len)
@@ -1558,6 +1560,18 @@ static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset,
 {
 	return -EOPNOTSUPP;
 }
+
+static inline int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset,
+				       void *buf, u32 len)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset,
+					void *buf, u32 len)
+{
+	return -EOPNOTSUPP;
+}
 #endif /* CONFIG_NET */
 
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +1 -1
index d0351d30e551..faa304c926cf 100644
@@ -5344,7 +5344,7 @@ union bpf_attr {
  *		*len* must be a statically known value. The returned data slice
  *		is invalidated whenever the dynptr is invalidated.
  *
- *		skb type dynptrs may not use bpf_dynptr_data. They should
+ *		skb and xdp type dynptrs may not use bpf_dynptr_data. They should
  *		instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr.
  *	Return
  *		Pointer to the underlying dynptr data, NULL if the dynptr is
kernel/bpf/helpers.c +8 -1
index 1863bea0a21f..78fad0e84ca4 100644
@@ -1530,6 +1530,8 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kern
 		return 0;
 	case BPF_DYNPTR_TYPE_SKB:
 		return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
+	case BPF_DYNPTR_TYPE_XDP:
+		return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len);
 	default:
 		WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
 		return -EFAULT;
@@ -1576,6 +1578,10 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
 	case BPF_DYNPTR_TYPE_SKB:
 		return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,
 					     flags);
+	case BPF_DYNPTR_TYPE_XDP:
+		if (flags)
+			return -EINVAL;
+		return __bpf_xdp_store_bytes(dst->data, dst->offset + offset, src, len);
 	default:
 		WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
 		return -EFAULT;
@@ -1615,7 +1621,8 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3
 	case BPF_DYNPTR_TYPE_RINGBUF:
 		return (unsigned long)(ptr->data + ptr->offset + offset);
 	case BPF_DYNPTR_TYPE_SKB:
-		/* skb dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */
+	case BPF_DYNPTR_TYPE_XDP:
+		/* skb and xdp dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */
 		return 0;
 	default:
 		WARN_ONCE(true, "bpf_dynptr_data: unknown dynptr type %d\n", type);
kernel/bpf/verifier.c +10 -0
index ef38ec52569a..cbb8c0178372 100644
@@ -752,6 +752,8 @@ static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type)
 		return BPF_DYNPTR_TYPE_RINGBUF;
 	case DYNPTR_TYPE_SKB:
 		return BPF_DYNPTR_TYPE_SKB;
+	case DYNPTR_TYPE_XDP:
+		return BPF_DYNPTR_TYPE_XDP;
 	default:
 		return BPF_DYNPTR_TYPE_INVALID;
 	}
@@ -6300,6 +6302,9 @@ int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
 			case DYNPTR_TYPE_SKB:
 				err_extra = "skb ";
 				break;
+			case DYNPTR_TYPE_XDP:
+				err_extra = "xdp ";
+				break;
 			default:
 				err_extra = "<unknown>";
 				break;
@@ -8943,6 +8948,7 @@ enum special_kfunc_type {
 	KF_bpf_rbtree_add,
 	KF_bpf_rbtree_first,
 	KF_bpf_dynptr_from_skb,
+	KF_bpf_dynptr_from_xdp,
 };
 
 BTF_SET_START(special_kfunc_set)
@@ -8958,6 +8964,7 @@ BTF_ID(func, bpf_rbtree_remove)
 BTF_ID(func, bpf_rbtree_add)
 BTF_ID(func, bpf_rbtree_first)
 BTF_ID(func, bpf_dynptr_from_skb)
+BTF_ID(func, bpf_dynptr_from_xdp)
 BTF_SET_END(special_kfunc_set)
 
 BTF_ID_LIST(special_kfunc_list)
@@ -8975,6 +8982,7 @@ BTF_ID(func, bpf_rbtree_remove)
 BTF_ID(func, bpf_rbtree_add)
 BTF_ID(func, bpf_rbtree_first)
 BTF_ID(func, bpf_dynptr_from_skb)
+BTF_ID(func, bpf_dynptr_from_xdp)
 
 static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta)
 {
@@ -9731,6 +9739,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 
 			if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb])
 				dynptr_arg_type |= DYNPTR_TYPE_SKB;
+			else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp])
+				dynptr_arg_type |= DYNPTR_TYPE_XDP;
 
 			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);
 			if (ret < 0)
net/core/filter.c +35 -2
index f3afa31a9b10..c692046fa7f6 100644
@@ -3839,7 +3839,7 @@ static const struct bpf_func_proto sk_skb_change_head_proto = {
 	.arg3_type	= ARG_ANYTHING,
 };
 
-BPF_CALL_1(bpf_xdp_get_buff_len, struct  xdp_buff*, xdp)
+BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
 {
 	return xdp_get_buff_len(xdp);
 }
@@ -3999,6 +3999,11 @@ static const struct bpf_func_proto bpf_xdp_load_bytes_proto = {
 	.arg4_type	= ARG_CONST_SIZE,
 };
 
+int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
+{
+	return ____bpf_xdp_load_bytes(xdp, offset, buf, len);
+}
+
 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
 	   void *, buf, u32, len)
 {
@@ -4026,6 +4031,11 @@ static const struct bpf_func_proto bpf_xdp_store_bytes_proto = {
 	.arg4_type	= ARG_CONST_SIZE,
 };
 
+int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
+{
+	return ____bpf_xdp_store_bytes(xdp, offset, buf, len);
+}
+
 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
 {
 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
@@ -11648,6 +11658,19 @@ __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags,
 
 	return 0;
 }
+
+__bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_buff *xdp, u64 flags,
+				    struct bpf_dynptr_kern *ptr__uninit)
+{
+	if (flags) {
+		bpf_dynptr_set_null(ptr__uninit);
+		return -EINVAL;
+	}
+
+	bpf_dynptr_init(ptr__uninit, xdp, BPF_DYNPTR_TYPE_XDP, 0, xdp_get_buff_len(xdp));
+
+	return 0;
+}
 __diag_pop();
 
 int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,
@@ -11668,11 +11691,20 @@ BTF_SET8_START(bpf_kfunc_check_set_skb)
 BTF_ID_FLAGS(func, bpf_dynptr_from_skb)
 BTF_SET8_END(bpf_kfunc_check_set_skb)
 
+BTF_SET8_START(bpf_kfunc_check_set_xdp)
+BTF_ID_FLAGS(func, bpf_dynptr_from_xdp)
+BTF_SET8_END(bpf_kfunc_check_set_xdp)
+
 static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
 	.owner = THIS_MODULE,
 	.set = &bpf_kfunc_check_set_skb,
 };
 
+static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = {
+	.owner = THIS_MODULE,
+	.set = &bpf_kfunc_check_set_xdp,
+};
+
 static int __init bpf_kfunc_init(void)
 {
 	int ret;
@@ -11685,6 +11717,7 @@ static int __init bpf_kfunc_init(void)
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_OUT, &bpf_kfunc_set_skb);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_IN, &bpf_kfunc_set_skb);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT, &bpf_kfunc_set_skb);
-	return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
+	return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
 }
 late_initcall(bpf_kfunc_init);
tools/include/uapi/linux/bpf.h +1 -1
index d0351d30e551..faa304c926cf 100644
@@ -5344,7 +5344,7 @@ union bpf_attr {
  *		*len* must be a statically known value. The returned data slice
  *		is invalidated whenever the dynptr is invalidated.
  *
- *		skb type dynptrs may not use bpf_dynptr_data. They should
+ *		skb and xdp type dynptrs may not use bpf_dynptr_data. They should
  *		instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr.
  *	Return
  *		Pointer to the underlying dynptr data, NULL if the dynptr is
Signature · 1 line
2.34.1
#9 lore raw

PATCH v12 9/10 bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr

In reply to Joanne Koong

Two new kfuncs are added, bpf_dynptr_slice and bpf_dynptr_slice_rdwr. The user must pass in a buffer to store the contents of the data slice if a direct pointer to the data cannot be obtained.

For skb and xdp type dynptrs, these two APIs are the only way to obtain a data slice. However, for other types of dynptrs, there is no difference between bpf_dynptr_slice(_rdwr) and bpf_dynptr_data.

For skb type dynptrs, the data is copied into the user provided buffer if any of the data is not in the linear portion of the skb. For xdp type dynptrs, the data is copied into the user provided buffer if the data is between xdp frags.

If the skb is cloned and a call to bpf_dynptr_data_rdwr is made, then the skb will be uncloned (see bpf_unclone_prologue()).

Please note that any bpf_dynptr_write() automatically invalidates any prior data slices of the skb dynptr. This is because the skb may be cloned or may need to pull its paged buffer into the head. As such, any bpf_dynptr_write() will automatically have its prior data slices invalidated, even if the write is to data in the skb head of an uncloned skb. Please note as well that any other helper calls that change the underlying packet buffer (eg bpf_skb_pull_data()) invalidates any data slices of the skb dynptr as well, for the same reasons.

---
 include/linux/filter.h         |  14 ++++
 include/uapi/linux/bpf.h       |   5 ++
 kernel/bpf/helpers.c           | 138 +++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c          | 125 ++++++++++++++++++++++++++++-
 net/core/filter.c              |   6 +-
 tools/include/uapi/linux/bpf.h |   5 ++
 6 files changed, 286 insertions(+), 7 deletions(-)
include/linux/filter.h +14 -0
index 3f6992261ec5..efa5d4a1677e 100644
@@ -1548,6 +1548,9 @@ int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
 			  u32 len, u64 flags);
 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
+void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
+void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+		      void *buf, unsigned long len, bool flush);
 #else /* CONFIG_NET */
 static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
 				       void *to, u32 len)
@@ -1572,6 +1575,17 @@ static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset,
 {
 	return -EOPNOTSUPP;
 }
+
+static inline void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
+{
+	return NULL;
+}
+
+static inline void *bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, void *buf,
+				     unsigned long len, bool flush)
+{
+	return NULL;
+}
 #endif /* CONFIG_NET */
 
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +5 -0
index faa304c926cf..c9699304aed2 100644
@@ -5329,6 +5329,11 @@ union bpf_attr {
  *		*flags* must be 0 except for skb-type dynptrs.
  *
  *		For skb-type dynptrs:
+ *		    *  All data slices of the dynptr are automatically
+ *		       invalidated after **bpf_dynptr_write**\ (). This is
+ *		       because writing may pull the skb and change the
+ *		       underlying packet buffer.
+ *
  *		    *  For *flags*, please see the flags accepted by
  *		       **bpf_skb_store_bytes**\ ().
  *	Return
kernel/bpf/helpers.c +138 -0

150 lines not on this page · raw message

kernel/bpf/verifier.c +121 -4

207 lines not on this page · raw message

net/core/filter.c +3 -3
index c692046fa7f6..8f3124e06133 100644
@@ -3894,8 +3894,8 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
 	.arg2_type	= ARG_ANYTHING,
 };
 
-static void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
-			     void *buf, unsigned long len, bool flush)
+void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+		      void *buf, unsigned long len, bool flush)
 {
 	unsigned long ptr_len, ptr_off = 0;
 	skb_frag_t *next_frag, *end_frag;
@@ -3941,7 +3941,7 @@ static void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
 	}
 }
 
-static void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
+void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
 {
 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
 	u32 size = xdp->data_end - xdp->data;
tools/include/uapi/linux/bpf.h +5 -0
index faa304c926cf..c9699304aed2 100644
@@ -5329,6 +5329,11 @@ union bpf_attr {
  *		*flags* must be 0 except for skb-type dynptrs.
  *
  *		For skb-type dynptrs:
+ *		    *  All data slices of the dynptr are automatically
+ *		       invalidated after **bpf_dynptr_write**\ (). This is
+ *		       because writing may pull the skb and change the
+ *		       underlying packet buffer.
+ *
  *		    *  For *flags*, please see the flags accepted by
  *		       **bpf_skb_store_bytes**\ ().
  *	Return
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH v12 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr

Thank you for the patch! Perhaps something to improve:

Hi Joanne,

Thank you for the patch! Perhaps something to improve:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Joanne-Koong/bpf-Support-sk_buff-and-xdp_buff-as-valid-kfunc-arg-types/20230226-165406
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230226085120.3907863-10-joannelkoong%40gmail.com
patch subject: [PATCH v12 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr
config: hexagon-randconfig-r026-20230226 (https://download.01.org/0day-ci/archive/20230226/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project db89896bbbd2251fff457699635acbbedeead27f)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/bc5a61c43c72539ef11e6435a168bf240a186ac1
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/bpf-Support-sk_buff-and-xdp_buff-as-valid-kfunc-arg-types/20230226-165406 git checkout bc5a61c43c72539ef11e6435a168bf240a186ac1
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from kernel/bpf/verifier.c:7:
In file included from include/linux/bpf-cgroup.h:5:
In file included from include/linux/bpf.h:31:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
                                                     ^
   In file included from kernel/bpf/verifier.c:7:
   In file included from include/linux/bpf-cgroup.h:5:
   In file included from include/linux/bpf.h:31:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:26:
   In file included from include/linux/kernel_stat.h:9:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
                                                     ^
   In file included from kernel/bpf/verifier.c:7:
   In file included from include/linux/bpf-cgroup.h:5:
   In file included from include/linux/bpf.h:31:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:26:
   In file included from include/linux/kernel_stat.h:9:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   kernel/bpf/verifier.c:6253:5: warning: no previous prototype for function 'process_dynptr_func' [-Wmissing-prototypes]
   int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
       ^
   kernel/bpf/verifier.c:6253:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
   ^
   static 
   kernel/bpf/verifier.c:9907:4: error: expected expression
                           struct bpf_reg_state *size_reg = &regs[regno + 1];
                           ^
   kernel/bpf/verifier.c:9910:40: error: use of undeclared identifier 'size_reg'; did you mean 'size_arg'?
                           ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);
                                                               ^~~~~~~~
                                                               size_arg
   kernel/bpf/verifier.c:9908:28: note: 'size_arg' declared here
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:9916:57: error: use of undeclared identifier 'size_reg'; did you mean 'size_arg'?
                           if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) {
                                                                                ^~~~~~~~
                                                                                size_arg
   kernel/bpf/verifier.c:9908:28: note: 'size_arg' declared here
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:9921:24: error: use of undeclared identifier 'size_reg'
                                   if (!tnum_is_const(size_reg->var_off)) {
                                                      ^
   kernel/bpf/verifier.c:9926:32: error: use of undeclared identifier 'size_reg'
                                   meta->arg_constant.value = size_reg->var_off.value;
                                                              ^
>> kernel/bpf/verifier.c:9908:28: warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^

8 warnings and 5 errors generated.

vim +9908 kernel/bpf/verifier.c

9584
9585 static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta,
9586 int insn_idx)
9587 {
9588 const char *func_name = meta->func_name, *ref_tname;
9589 const struct btf *btf = meta->btf;
9590 const struct btf_param *args;
9591 u32 i, nargs;
9592 int ret;
9593
9594 args = (const struct btf_param *)(meta->func_proto + 1);
9595 nargs = btf_type_vlen(meta->func_proto);
9596 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
9597 verbose(env, "Function %s has %d > %d args\n", func_name, nargs,
9598 MAX_BPF_FUNC_REG_ARGS);
9599 return -EINVAL;
9600 }
9601
9602 /* Check that BTF function arguments match actual types that the
9603 * verifier sees.
9604 */
9605 for (i = 0; i < nargs; i++) {
9606 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[i + 1];
9607 const struct btf_type *t, *ref_t, *resolve_ret;
9608 enum bpf_arg_type arg_type = ARG_DONTCARE;
9609 u32 regno = i + 1, ref_id, type_size;
9610 bool is_ret_buf_sz = false;
9611 int kf_arg_type;
9612
9613 t = btf_type_skip_modifiers(btf, args[i].type, NULL);
9614
9615 if (is_kfunc_arg_ignore(btf, &args[i]))
9616 continue;
9617
9618 if (btf_type_is_scalar(t)) {
9619 if (reg->type != SCALAR_VALUE) {
9620 verbose(env, "R%d is not a scalar\n", regno);
9621 return -EINVAL;
9622 }
9623
9624 if (is_kfunc_arg_constant(meta->btf, &args[i])) {
9625 if (meta->arg_constant.found) {
9626 verbose(env, "verifier internal error: only one constant argument permitted\n");
9627 return -EFAULT;
9628 }
9629 if (!tnum_is_const(reg->var_off)) {
9630 verbose(env, "R%d must be a known constant\n", regno);
9631 return -EINVAL;
9632 }
9633 ret = mark_chain_precision(env, regno);
9634 if (ret < 0)
9635 return ret;
9636 meta->arg_constant.found = true;
9637 meta->arg_constant.value = reg->var_off.value;
9638 } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) {
9639 meta->r0_rdonly = true;
9640 is_ret_buf_sz = true;
9641 } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) {
9642 is_ret_buf_sz = true;
9643 }
9644
9645 if (is_ret_buf_sz) {
9646 if (meta->r0_size) {
9647 verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc");
9648 return -EINVAL;
9649 }
9650
9651 if (!tnum_is_const(reg->var_off)) {
9652 verbose(env, "R%d is not a const\n", regno);
9653 return -EINVAL;
9654 }
9655
9656 meta->r0_size = reg->var_off.value;
9657 ret = mark_chain_precision(env, regno);
9658 if (ret)
9659 return ret;
9660 }
9661 continue;
9662 }
9663
9664 if (!btf_type_is_ptr(t)) {
9665 verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t));
9666 return -EINVAL;
9667 }
9668
9669 if (is_kfunc_trusted_args(meta) &&
9670 (register_is_null(reg) || type_may_be_null(reg->type))) {
9671 verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i);
9672 return -EACCES;
9673 }
9674
9675 if (reg->ref_obj_id) {
9676 if (is_kfunc_release(meta) && meta->ref_obj_id) {
9677 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
9678 regno, reg->ref_obj_id,
9679 meta->ref_obj_id);
9680 return -EFAULT;
9681 }
9682 meta->ref_obj_id = reg->ref_obj_id;
9683 if (is_kfunc_release(meta))
9684 meta->release_regno = regno;
9685 }
9686
9687 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
9688 ref_tname = btf_name_by_offset(btf, ref_t->name_off);
9689
9690 kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs);
9691 if (kf_arg_type < 0)
9692 return kf_arg_type;
9693
9694 switch (kf_arg_type) {
9695 case KF_ARG_PTR_TO_ALLOC_BTF_ID:
9696 case KF_ARG_PTR_TO_BTF_ID:
9697 if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta))
9698 break;
9699
9700 if (!is_trusted_reg(reg)) {
9701 if (!is_kfunc_rcu(meta)) {
9702 verbose(env, "R%d must be referenced or trusted\n", regno);
9703 return -EINVAL;
9704 }
9705 if (!is_rcu_reg(reg)) {
9706 verbose(env, "R%d must be a rcu pointer\n", regno);
9707 return -EINVAL;
9708 }
9709 }
9710
9711 fallthrough;
9712 case KF_ARG_PTR_TO_CTX:
9713 /* Trusted arguments have the same offset checks as release arguments */
9714 arg_type |= OBJ_RELEASE;
9715 break;
9716 case KF_ARG_PTR_TO_KPTR:
9717 case KF_ARG_PTR_TO_DYNPTR:
9718 case KF_ARG_PTR_TO_LIST_HEAD:
9719 case KF_ARG_PTR_TO_LIST_NODE:
9720 case KF_ARG_PTR_TO_RB_ROOT:
9721 case KF_ARG_PTR_TO_RB_NODE:
9722 case KF_ARG_PTR_TO_MEM:
9723 case KF_ARG_PTR_TO_MEM_SIZE:
9724 case KF_ARG_PTR_TO_CALLBACK:
9725 /* Trusted by default */
9726 break;
9727 default:
9728 WARN_ON_ONCE(1);
9729 return -EFAULT;
9730 }
9731
9732 if (is_kfunc_release(meta) && reg->ref_obj_id)
9733 arg_type |= OBJ_RELEASE;
9734 ret = check_func_arg_reg_off(env, reg, regno, arg_type);
9735 if (ret < 0)
9736 return ret;
9737
9738 switch (kf_arg_type) {
9739 case KF_ARG_PTR_TO_CTX:
9740 if (reg->type != PTR_TO_CTX) {
9741 verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t));
9742 return -EINVAL;
9743 }
9744
9745 if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) {
9746 ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog));
9747 if (ret < 0)
9748 return -EINVAL;
9749 meta->ret_btf_id = ret;
9750 }
9751 break;
9752 case KF_ARG_PTR_TO_ALLOC_BTF_ID:
9753 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9754 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9755 return -EINVAL;
9756 }
9757 if (!reg->ref_obj_id) {
9758 verbose(env, "allocated object must be referenced\n");
9759 return -EINVAL;
9760 }
9761 if (meta->btf == btf_vmlinux &&
9762 meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) {
9763 meta->arg_obj_drop.btf = reg->btf;
9764 meta->arg_obj_drop.btf_id = reg->btf_id;
9765 }
9766 break;
9767 case KF_ARG_PTR_TO_KPTR:
9768 if (reg->type != PTR_TO_MAP_VALUE) {
9769 verbose(env, "arg#0 expected pointer to map value\n");
9770 return -EINVAL;
9771 }
9772 ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i);
9773 if (ret < 0)
9774 return ret;
9775 break;
9776 case KF_ARG_PTR_TO_DYNPTR:
9777 {
9778 enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
9779
9780 if (reg->type != PTR_TO_STACK &&
9781 reg->type != CONST_PTR_TO_DYNPTR) {
9782 verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i);
9783 return -EINVAL;
9784 }
9785
9786 if (reg->type == CONST_PTR_TO_DYNPTR)
9787 dynptr_arg_type |= MEM_RDONLY;
9788
9789 if (is_kfunc_arg_uninit(btf, &args[i]))
9790 dynptr_arg_type |= MEM_UNINIT;
9791
9792 if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb])
9793 dynptr_arg_type |= DYNPTR_TYPE_SKB;
9794 else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp])
9795 dynptr_arg_type |= DYNPTR_TYPE_XDP;
9796
9797 ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);
9798 if (ret < 0)
9799 return ret;
9800
9801 if (!(dynptr_arg_type & MEM_UNINIT)) {
9802 int id = dynptr_id(env, reg);
9803
9804 if (id < 0) {
9805 verbose(env, "verifier internal error: failed to obtain dynptr id\n");
9806 return id;
9807 }
9808 meta->initialized_dynptr.id = id;
9809 meta->initialized_dynptr.type = dynptr_get_type(env, reg);
9810 }
9811
9812 break;
9813 }
9814 case KF_ARG_PTR_TO_LIST_HEAD:
9815 if (reg->type != PTR_TO_MAP_VALUE &&
9816 reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9817 verbose(env, "arg#%d expected pointer to map value or allocated object\n", i);
9818 return -EINVAL;
9819 }
9820 if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) {
9821 verbose(env, "allocated object must be referenced\n");
9822 return -EINVAL;
9823 }
9824 ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta);
9825 if (ret < 0)
9826 return ret;
9827 break;
9828 case KF_ARG_PTR_TO_RB_ROOT:
9829 if (reg->type != PTR_TO_MAP_VALUE &&
9830 reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9831 verbose(env, "arg#%d expected pointer to map value or allocated object\n", i);
9832 return -EINVAL;
9833 }
9834 if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) {
9835 verbose(env, "allocated object must be referenced\n");
9836 return -EINVAL;
9837 }
9838 ret = process_kf_arg_ptr_to_rbtree_root(env, reg, regno, meta);
9839 if (ret < 0)
9840 return ret;
9841 break;
9842 case KF_ARG_PTR_TO_LIST_NODE:
9843 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9844 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9845 return -EINVAL;
9846 }
9847 if (!reg->ref_obj_id) {
9848 verbose(env, "allocated object must be referenced\n");
9849 return -EINVAL;
9850 }
9851 ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta);
9852 if (ret < 0)
9853 return ret;
9854 break;
9855 case KF_ARG_PTR_TO_RB_NODE:
9856 if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) {
9857 if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) {
9858 verbose(env, "rbtree_remove node input must be non-owning ref\n");
9859 return -EINVAL;
9860 }
9861 if (in_rbtree_lock_required_cb(env)) {
9862 verbose(env, "rbtree_remove not allowed in rbtree cb\n");
9863 return -EINVAL;
9864 }
9865 } else {
9866 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9867 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9868 return -EINVAL;
9869 }
9870 if (!reg->ref_obj_id) {
9871 verbose(env, "allocated object must be referenced\n");
9872 return -EINVAL;
9873 }
9874 }
9875
9876 ret = process_kf_arg_ptr_to_rbtree_node(env, reg, regno, meta);
9877 if (ret < 0)
9878 return ret;
9879 break;
9880 case KF_ARG_PTR_TO_BTF_ID:
9881 /* Only base_type is checked, further checks are done here */
9882 if ((base_type(reg->type) != PTR_TO_BTF_ID ||
9883 (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) &&
9884 !reg2btf_ids[base_type(reg->type)]) {
9885 verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type));
9886 verbose(env, "expected %s or socket\n",
9887 reg_type_str(env, base_type(reg->type) |
9888 (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS)));
9889 return -EINVAL;
9890 }
9891 ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i);
9892 if (ret < 0)
9893 return ret;
9894 break;
9895 case KF_ARG_PTR_TO_MEM:
9896 resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
9897 if (IS_ERR(resolve_ret)) {
9898 verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
9899 i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret));
9900 return -EINVAL;
9901 }
9902 ret = check_mem_reg(env, reg, regno, type_size);
9903 if (ret < 0)
9904 return ret;
9905 break;
9906 case KF_ARG_PTR_TO_MEM_SIZE:
9907 struct bpf_reg_state *size_reg = &regs[regno + 1];

> 9908				const struct btf_param *size_arg = &args[i + 1];

9909
9910 ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);
9911 if (ret < 0) {
9912 verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1);
9913 return ret;
9914 }
9915
9916 if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) {
9917 if (meta->arg_constant.found) {
9918 verbose(env, "verifier internal error: only one constant argument permitted\n");
9919 return -EFAULT;
9920 }
9921 if (!tnum_is_const(size_reg->var_off)) {
9922 verbose(env, "R%d must be a known constant\n", regno + 1);
9923 return -EINVAL;
9924 }
9925 meta->arg_constant.found = true;
9926 meta->arg_constant.value = size_reg->var_off.value;
9927 }
9928
9929 /* Skip next '__sz' or '__szk' argument */
9930 i++;
9931 break;
9932 case KF_ARG_PTR_TO_CALLBACK:
9933 meta->subprogno = reg->subprogno;
9934 break;
9935 }
9936 }
9937
9938 if (is_kfunc_release(meta) && !meta->release_regno) {
9939 verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n",
9940 func_name);
9941 return -EINVAL;
9942 }
9943
9944 return 0;
9945 }
9946

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#11 lore raw

Re: [PATCH v12 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr

In reply to Joanne Koong
Thank you for the patch! Perhaps something to improve:

Hi Joanne,

Thank you for the patch! Perhaps something to improve:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Joanne-Koong/bpf-Support-sk_buff-and-xdp_buff-as-valid-kfunc-arg-types/20230226-165406
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230226085120.3907863-10-joannelkoong%40gmail.com
patch subject: [PATCH v12 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr
config: i386-randconfig-a002 (https://download.01.org/0day-ci/archive/20230226/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/bc5a61c43c72539ef11e6435a168bf240a186ac1
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/bpf-Support-sk_buff-and-xdp_buff-as-valid-kfunc-arg-types/20230226-165406 git checkout bc5a61c43c72539ef11e6435a168bf240a186ac1
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

kernel/bpf/verifier.c:6253:5: warning: no previous prototype for function 'process_dynptr_func' [-Wmissing-prototypes] int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,

       ^
   kernel/bpf/verifier.c:6253:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
   ^
   static 
   kernel/bpf/verifier.c:9907:4: error: expected expression
                           struct bpf_reg_state *size_reg = &regs[regno + 1];
                           ^
   kernel/bpf/verifier.c:9910:40: error: use of undeclared identifier 'size_reg'; did you mean 'size_arg'?
                           ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);
                                                               ^~~~~~~~
                                                               size_arg
   kernel/bpf/verifier.c:9908:28: note: 'size_arg' declared here
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:9916:57: error: use of undeclared identifier 'size_reg'; did you mean 'size_arg'?
                           if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) {
                                                                                ^~~~~~~~
                                                                                size_arg
   kernel/bpf/verifier.c:9908:28: note: 'size_arg' declared here
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:9921:24: error: use of undeclared identifier 'size_reg'
                                   if (!tnum_is_const(size_reg->var_off)) {
                                                      ^
   kernel/bpf/verifier.c:9926:32: error: use of undeclared identifier 'size_reg'
                                   meta->arg_constant.value = size_reg->var_off.value;
                                                              ^
>> kernel/bpf/verifier.c:9908:28: warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:10167:24: warning: array index 16 is past the end of the array (which contains 16 elements) [-Warray-bounds]
                                      meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) {
                                                      ^                  ~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/verifier.c:9017:1: note: array 'special_kfunc_list' declared here
   BTF_ID_LIST(special_kfunc_list)
   ^
   include/linux/btf_ids.h:207:27: note: expanded from macro 'BTF_ID_LIST'
   #define BTF_ID_LIST(name) static u32 __maybe_unused name[16];
                             ^

3 warnings and 5 errors generated.

vim +9908 kernel/bpf/verifier.c

9584
9585 static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta,
9586 int insn_idx)
9587 {
9588 const char *func_name = meta->func_name, *ref_tname;
9589 const struct btf *btf = meta->btf;
9590 const struct btf_param *args;
9591 u32 i, nargs;
9592 int ret;
9593
9594 args = (const struct btf_param *)(meta->func_proto + 1);
9595 nargs = btf_type_vlen(meta->func_proto);
9596 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
9597 verbose(env, "Function %s has %d > %d args\n", func_name, nargs,
9598 MAX_BPF_FUNC_REG_ARGS);
9599 return -EINVAL;
9600 }
9601
9602 /* Check that BTF function arguments match actual types that the
9603 * verifier sees.
9604 */
9605 for (i = 0; i < nargs; i++) {
9606 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[i + 1];
9607 const struct btf_type *t, *ref_t, *resolve_ret;
9608 enum bpf_arg_type arg_type = ARG_DONTCARE;
9609 u32 regno = i + 1, ref_id, type_size;
9610 bool is_ret_buf_sz = false;
9611 int kf_arg_type;
9612
9613 t = btf_type_skip_modifiers(btf, args[i].type, NULL);
9614
9615 if (is_kfunc_arg_ignore(btf, &args[i]))
9616 continue;
9617
9618 if (btf_type_is_scalar(t)) {
9619 if (reg->type != SCALAR_VALUE) {
9620 verbose(env, "R%d is not a scalar\n", regno);
9621 return -EINVAL;
9622 }
9623
9624 if (is_kfunc_arg_constant(meta->btf, &args[i])) {
9625 if (meta->arg_constant.found) {
9626 verbose(env, "verifier internal error: only one constant argument permitted\n");
9627 return -EFAULT;
9628 }
9629 if (!tnum_is_const(reg->var_off)) {
9630 verbose(env, "R%d must be a known constant\n", regno);
9631 return -EINVAL;
9632 }
9633 ret = mark_chain_precision(env, regno);
9634 if (ret < 0)
9635 return ret;
9636 meta->arg_constant.found = true;
9637 meta->arg_constant.value = reg->var_off.value;
9638 } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) {
9639 meta->r0_rdonly = true;
9640 is_ret_buf_sz = true;
9641 } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) {
9642 is_ret_buf_sz = true;
9643 }
9644
9645 if (is_ret_buf_sz) {
9646 if (meta->r0_size) {
9647 verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc");
9648 return -EINVAL;
9649 }
9650
9651 if (!tnum_is_const(reg->var_off)) {
9652 verbose(env, "R%d is not a const\n", regno);
9653 return -EINVAL;
9654 }
9655
9656 meta->r0_size = reg->var_off.value;
9657 ret = mark_chain_precision(env, regno);
9658 if (ret)
9659 return ret;
9660 }
9661 continue;
9662 }
9663
9664 if (!btf_type_is_ptr(t)) {
9665 verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t));
9666 return -EINVAL;
9667 }
9668
9669 if (is_kfunc_trusted_args(meta) &&
9670 (register_is_null(reg) || type_may_be_null(reg->type))) {
9671 verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i);
9672 return -EACCES;
9673 }
9674
9675 if (reg->ref_obj_id) {
9676 if (is_kfunc_release(meta) && meta->ref_obj_id) {
9677 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
9678 regno, reg->ref_obj_id,
9679 meta->ref_obj_id);
9680 return -EFAULT;
9681 }
9682 meta->ref_obj_id = reg->ref_obj_id;
9683 if (is_kfunc_release(meta))
9684 meta->release_regno = regno;
9685 }
9686
9687 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
9688 ref_tname = btf_name_by_offset(btf, ref_t->name_off);
9689
9690 kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs);
9691 if (kf_arg_type < 0)
9692 return kf_arg_type;
9693
9694 switch (kf_arg_type) {
9695 case KF_ARG_PTR_TO_ALLOC_BTF_ID:
9696 case KF_ARG_PTR_TO_BTF_ID:
9697 if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta))
9698 break;
9699
9700 if (!is_trusted_reg(reg)) {
9701 if (!is_kfunc_rcu(meta)) {
9702 verbose(env, "R%d must be referenced or trusted\n", regno);
9703 return -EINVAL;
9704 }
9705 if (!is_rcu_reg(reg)) {
9706 verbose(env, "R%d must be a rcu pointer\n", regno);
9707 return -EINVAL;
9708 }
9709 }
9710
9711 fallthrough;
9712 case KF_ARG_PTR_TO_CTX:
9713 /* Trusted arguments have the same offset checks as release arguments */
9714 arg_type |= OBJ_RELEASE;
9715 break;
9716 case KF_ARG_PTR_TO_KPTR:
9717 case KF_ARG_PTR_TO_DYNPTR:
9718 case KF_ARG_PTR_TO_LIST_HEAD:
9719 case KF_ARG_PTR_TO_LIST_NODE:
9720 case KF_ARG_PTR_TO_RB_ROOT:
9721 case KF_ARG_PTR_TO_RB_NODE:
9722 case KF_ARG_PTR_TO_MEM:
9723 case KF_ARG_PTR_TO_MEM_SIZE:
9724 case KF_ARG_PTR_TO_CALLBACK:
9725 /* Trusted by default */
9726 break;
9727 default:
9728 WARN_ON_ONCE(1);
9729 return -EFAULT;
9730 }
9731
9732 if (is_kfunc_release(meta) && reg->ref_obj_id)
9733 arg_type |= OBJ_RELEASE;
9734 ret = check_func_arg_reg_off(env, reg, regno, arg_type);
9735 if (ret < 0)
9736 return ret;
9737
9738 switch (kf_arg_type) {
9739 case KF_ARG_PTR_TO_CTX:
9740 if (reg->type != PTR_TO_CTX) {
9741 verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t));
9742 return -EINVAL;
9743 }
9744
9745 if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) {
9746 ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog));
9747 if (ret < 0)
9748 return -EINVAL;
9749 meta->ret_btf_id = ret;
9750 }
9751 break;
9752 case KF_ARG_PTR_TO_ALLOC_BTF_ID:
9753 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9754 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9755 return -EINVAL;
9756 }
9757 if (!reg->ref_obj_id) {
9758 verbose(env, "allocated object must be referenced\n");
9759 return -EINVAL;
9760 }
9761 if (meta->btf == btf_vmlinux &&
9762 meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) {
9763 meta->arg_obj_drop.btf = reg->btf;
9764 meta->arg_obj_drop.btf_id = reg->btf_id;
9765 }
9766 break;
9767 case KF_ARG_PTR_TO_KPTR:
9768 if (reg->type != PTR_TO_MAP_VALUE) {
9769 verbose(env, "arg#0 expected pointer to map value\n");
9770 return -EINVAL;
9771 }
9772 ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i);
9773 if (ret < 0)
9774 return ret;
9775 break;
9776 case KF_ARG_PTR_TO_DYNPTR:
9777 {
9778 enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
9779
9780 if (reg->type != PTR_TO_STACK &&
9781 reg->type != CONST_PTR_TO_DYNPTR) {
9782 verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i);
9783 return -EINVAL;
9784 }
9785
9786 if (reg->type == CONST_PTR_TO_DYNPTR)
9787 dynptr_arg_type |= MEM_RDONLY;
9788
9789 if (is_kfunc_arg_uninit(btf, &args[i]))
9790 dynptr_arg_type |= MEM_UNINIT;
9791
9792 if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb])
9793 dynptr_arg_type |= DYNPTR_TYPE_SKB;
9794 else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp])
9795 dynptr_arg_type |= DYNPTR_TYPE_XDP;
9796
9797 ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);
9798 if (ret < 0)
9799 return ret;
9800
9801 if (!(dynptr_arg_type & MEM_UNINIT)) {
9802 int id = dynptr_id(env, reg);
9803
9804 if (id < 0) {
9805 verbose(env, "verifier internal error: failed to obtain dynptr id\n");
9806 return id;
9807 }
9808 meta->initialized_dynptr.id = id;
9809 meta->initialized_dynptr.type = dynptr_get_type(env, reg);
9810 }
9811
9812 break;
9813 }
9814 case KF_ARG_PTR_TO_LIST_HEAD:
9815 if (reg->type != PTR_TO_MAP_VALUE &&
9816 reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9817 verbose(env, "arg#%d expected pointer to map value or allocated object\n", i);
9818 return -EINVAL;
9819 }
9820 if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) {
9821 verbose(env, "allocated object must be referenced\n");
9822 return -EINVAL;
9823 }
9824 ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta);
9825 if (ret < 0)
9826 return ret;
9827 break;
9828 case KF_ARG_PTR_TO_RB_ROOT:
9829 if (reg->type != PTR_TO_MAP_VALUE &&
9830 reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9831 verbose(env, "arg#%d expected pointer to map value or allocated object\n", i);
9832 return -EINVAL;
9833 }
9834 if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) {
9835 verbose(env, "allocated object must be referenced\n");
9836 return -EINVAL;
9837 }
9838 ret = process_kf_arg_ptr_to_rbtree_root(env, reg, regno, meta);
9839 if (ret < 0)
9840 return ret;
9841 break;
9842 case KF_ARG_PTR_TO_LIST_NODE:
9843 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9844 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9845 return -EINVAL;
9846 }
9847 if (!reg->ref_obj_id) {
9848 verbose(env, "allocated object must be referenced\n");
9849 return -EINVAL;
9850 }
9851 ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta);
9852 if (ret < 0)
9853 return ret;
9854 break;
9855 case KF_ARG_PTR_TO_RB_NODE:
9856 if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) {
9857 if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) {
9858 verbose(env, "rbtree_remove node input must be non-owning ref\n");
9859 return -EINVAL;
9860 }
9861 if (in_rbtree_lock_required_cb(env)) {
9862 verbose(env, "rbtree_remove not allowed in rbtree cb\n");
9863 return -EINVAL;
9864 }
9865 } else {
9866 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9867 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9868 return -EINVAL;
9869 }
9870 if (!reg->ref_obj_id) {
9871 verbose(env, "allocated object must be referenced\n");
9872 return -EINVAL;
9873 }
9874 }
9875
9876 ret = process_kf_arg_ptr_to_rbtree_node(env, reg, regno, meta);
9877 if (ret < 0)
9878 return ret;
9879 break;
9880 case KF_ARG_PTR_TO_BTF_ID:
9881 /* Only base_type is checked, further checks are done here */
9882 if ((base_type(reg->type) != PTR_TO_BTF_ID ||
9883 (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) &&
9884 !reg2btf_ids[base_type(reg->type)]) {
9885 verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type));
9886 verbose(env, "expected %s or socket\n",
9887 reg_type_str(env, base_type(reg->type) |
9888 (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS)));
9889 return -EINVAL;
9890 }
9891 ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i);
9892 if (ret < 0)
9893 return ret;
9894 break;
9895 case KF_ARG_PTR_TO_MEM:
9896 resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
9897 if (IS_ERR(resolve_ret)) {
9898 verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
9899 i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret));
9900 return -EINVAL;
9901 }
9902 ret = check_mem_reg(env, reg, regno, type_size);
9903 if (ret < 0)
9904 return ret;
9905 break;
9906 case KF_ARG_PTR_TO_MEM_SIZE:
9907 struct bpf_reg_state *size_reg = &regs[regno + 1];

> 9908				const struct btf_param *size_arg = &args[i + 1];

9909
9910 ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);
9911 if (ret < 0) {
9912 verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1);
9913 return ret;
9914 }
9915
9916 if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) {
9917 if (meta->arg_constant.found) {
9918 verbose(env, "verifier internal error: only one constant argument permitted\n");
9919 return -EFAULT;
9920 }
9921 if (!tnum_is_const(size_reg->var_off)) {
9922 verbose(env, "R%d must be a known constant\n", regno + 1);
9923 return -EINVAL;
9924 }
9925 meta->arg_constant.found = true;
9926 meta->arg_constant.value = size_reg->var_off.value;
9927 }
9928
9929 /* Skip next '__sz' or '__szk' argument */
9930 i++;
9931 break;
9932 case KF_ARG_PTR_TO_CALLBACK:
9933 meta->subprogno = reg->subprogno;
9934 break;
9935 }
9936 }
9937
9938 if (is_kfunc_release(meta) && !meta->release_regno) {
9939 verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n",
9940 func_name);
9941 return -EINVAL;
9942 }
9943
9944 return 0;
9945 }
9946

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#12 lore raw

Re: [PATCH v12 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr

In reply to Joanne Koong
Thank you for the patch! Yet something to improve:

Hi Joanne,

Thank you for the patch! Yet something to improve:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Joanne-Koong/bpf-Support-sk_buff-and-xdp_buff-as-valid-kfunc-arg-types/20230226-165406
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230226085120.3907863-10-joannelkoong%40gmail.com
patch subject: [PATCH v12 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr
config: hexagon-randconfig-r026-20230226 (https://download.01.org/0day-ci/archive/20230226/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project db89896bbbd2251fff457699635acbbedeead27f)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/bc5a61c43c72539ef11e6435a168bf240a186ac1
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/bpf-Support-sk_buff-and-xdp_buff-as-valid-kfunc-arg-types/20230226-165406 git checkout bc5a61c43c72539ef11e6435a168bf240a186ac1
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from kernel/bpf/verifier.c:7:
In file included from include/linux/bpf-cgroup.h:5:
In file included from include/linux/bpf.h:31:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
                                                     ^
   In file included from kernel/bpf/verifier.c:7:
   In file included from include/linux/bpf-cgroup.h:5:
   In file included from include/linux/bpf.h:31:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:26:
   In file included from include/linux/kernel_stat.h:9:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
                                                     ^
   In file included from kernel/bpf/verifier.c:7:
   In file included from include/linux/bpf-cgroup.h:5:
   In file included from include/linux/bpf.h:31:
   In file included from include/linux/memcontrol.h:13:
   In file included from include/linux/cgroup.h:26:
   In file included from include/linux/kernel_stat.h:9:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   kernel/bpf/verifier.c:6253:5: warning: no previous prototype for function 'process_dynptr_func' [-Wmissing-prototypes]
   int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
       ^
   kernel/bpf/verifier.c:6253:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
   ^
   static 
>> kernel/bpf/verifier.c:9907:4: error: expected expression
                           struct bpf_reg_state *size_reg = &regs[regno + 1];
                           ^
>> kernel/bpf/verifier.c:9910:40: error: use of undeclared identifier 'size_reg'; did you mean 'size_arg'?
                           ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);
                                                               ^~~~~~~~
                                                               size_arg
   kernel/bpf/verifier.c:9908:28: note: 'size_arg' declared here
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:9916:57: error: use of undeclared identifier 'size_reg'; did you mean 'size_arg'?
                           if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) {
                                                                                ^~~~~~~~
                                                                                size_arg
   kernel/bpf/verifier.c:9908:28: note: 'size_arg' declared here
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^
   kernel/bpf/verifier.c:9921:24: error: use of undeclared identifier 'size_reg'
                                   if (!tnum_is_const(size_reg->var_off)) {
                                                      ^
   kernel/bpf/verifier.c:9926:32: error: use of undeclared identifier 'size_reg'
                                   meta->arg_constant.value = size_reg->var_off.value;
                                                              ^
   kernel/bpf/verifier.c:9908:28: warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]
                           const struct btf_param *size_arg = &args[i + 1];
                                                   ^

8 warnings and 5 errors generated.

vim +9907 kernel/bpf/verifier.c

9584
9585 static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta,
9586 int insn_idx)
9587 {
9588 const char *func_name = meta->func_name, *ref_tname;
9589 const struct btf *btf = meta->btf;
9590 const struct btf_param *args;
9591 u32 i, nargs;
9592 int ret;
9593
9594 args = (const struct btf_param *)(meta->func_proto + 1);
9595 nargs = btf_type_vlen(meta->func_proto);
9596 if (nargs > MAX_BPF_FUNC_REG_ARGS) {
9597 verbose(env, "Function %s has %d > %d args\n", func_name, nargs,
9598 MAX_BPF_FUNC_REG_ARGS);
9599 return -EINVAL;
9600 }
9601
9602 /* Check that BTF function arguments match actual types that the
9603 * verifier sees.
9604 */
9605 for (i = 0; i < nargs; i++) {
9606 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[i + 1];
9607 const struct btf_type *t, *ref_t, *resolve_ret;
9608 enum bpf_arg_type arg_type = ARG_DONTCARE;
9609 u32 regno = i + 1, ref_id, type_size;
9610 bool is_ret_buf_sz = false;
9611 int kf_arg_type;
9612
9613 t = btf_type_skip_modifiers(btf, args[i].type, NULL);
9614
9615 if (is_kfunc_arg_ignore(btf, &args[i]))
9616 continue;
9617
9618 if (btf_type_is_scalar(t)) {
9619 if (reg->type != SCALAR_VALUE) {
9620 verbose(env, "R%d is not a scalar\n", regno);
9621 return -EINVAL;
9622 }
9623
9624 if (is_kfunc_arg_constant(meta->btf, &args[i])) {
9625 if (meta->arg_constant.found) {
9626 verbose(env, "verifier internal error: only one constant argument permitted\n");
9627 return -EFAULT;
9628 }
9629 if (!tnum_is_const(reg->var_off)) {
9630 verbose(env, "R%d must be a known constant\n", regno);
9631 return -EINVAL;
9632 }
9633 ret = mark_chain_precision(env, regno);
9634 if (ret < 0)
9635 return ret;
9636 meta->arg_constant.found = true;
9637 meta->arg_constant.value = reg->var_off.value;
9638 } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdonly_buf_size")) {
9639 meta->r0_rdonly = true;
9640 is_ret_buf_sz = true;
9641 } else if (is_kfunc_arg_scalar_with_name(btf, &args[i], "rdwr_buf_size")) {
9642 is_ret_buf_sz = true;
9643 }
9644
9645 if (is_ret_buf_sz) {
9646 if (meta->r0_size) {
9647 verbose(env, "2 or more rdonly/rdwr_buf_size parameters for kfunc");
9648 return -EINVAL;
9649 }
9650
9651 if (!tnum_is_const(reg->var_off)) {
9652 verbose(env, "R%d is not a const\n", regno);
9653 return -EINVAL;
9654 }
9655
9656 meta->r0_size = reg->var_off.value;
9657 ret = mark_chain_precision(env, regno);
9658 if (ret)
9659 return ret;
9660 }
9661 continue;
9662 }
9663
9664 if (!btf_type_is_ptr(t)) {
9665 verbose(env, "Unrecognized arg#%d type %s\n", i, btf_type_str(t));
9666 return -EINVAL;
9667 }
9668
9669 if (is_kfunc_trusted_args(meta) &&
9670 (register_is_null(reg) || type_may_be_null(reg->type))) {
9671 verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i);
9672 return -EACCES;
9673 }
9674
9675 if (reg->ref_obj_id) {
9676 if (is_kfunc_release(meta) && meta->ref_obj_id) {
9677 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
9678 regno, reg->ref_obj_id,
9679 meta->ref_obj_id);
9680 return -EFAULT;
9681 }
9682 meta->ref_obj_id = reg->ref_obj_id;
9683 if (is_kfunc_release(meta))
9684 meta->release_regno = regno;
9685 }
9686
9687 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
9688 ref_tname = btf_name_by_offset(btf, ref_t->name_off);
9689
9690 kf_arg_type = get_kfunc_ptr_arg_type(env, meta, t, ref_t, ref_tname, args, i, nargs);
9691 if (kf_arg_type < 0)
9692 return kf_arg_type;
9693
9694 switch (kf_arg_type) {
9695 case KF_ARG_PTR_TO_ALLOC_BTF_ID:
9696 case KF_ARG_PTR_TO_BTF_ID:
9697 if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta))
9698 break;
9699
9700 if (!is_trusted_reg(reg)) {
9701 if (!is_kfunc_rcu(meta)) {
9702 verbose(env, "R%d must be referenced or trusted\n", regno);
9703 return -EINVAL;
9704 }
9705 if (!is_rcu_reg(reg)) {
9706 verbose(env, "R%d must be a rcu pointer\n", regno);
9707 return -EINVAL;
9708 }
9709 }
9710
9711 fallthrough;
9712 case KF_ARG_PTR_TO_CTX:
9713 /* Trusted arguments have the same offset checks as release arguments */
9714 arg_type |= OBJ_RELEASE;
9715 break;
9716 case KF_ARG_PTR_TO_KPTR:
9717 case KF_ARG_PTR_TO_DYNPTR:
9718 case KF_ARG_PTR_TO_LIST_HEAD:
9719 case KF_ARG_PTR_TO_LIST_NODE:
9720 case KF_ARG_PTR_TO_RB_ROOT:
9721 case KF_ARG_PTR_TO_RB_NODE:
9722 case KF_ARG_PTR_TO_MEM:
9723 case KF_ARG_PTR_TO_MEM_SIZE:
9724 case KF_ARG_PTR_TO_CALLBACK:
9725 /* Trusted by default */
9726 break;
9727 default:
9728 WARN_ON_ONCE(1);
9729 return -EFAULT;
9730 }
9731
9732 if (is_kfunc_release(meta) && reg->ref_obj_id)
9733 arg_type |= OBJ_RELEASE;
9734 ret = check_func_arg_reg_off(env, reg, regno, arg_type);
9735 if (ret < 0)
9736 return ret;
9737
9738 switch (kf_arg_type) {
9739 case KF_ARG_PTR_TO_CTX:
9740 if (reg->type != PTR_TO_CTX) {
9741 verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t));
9742 return -EINVAL;
9743 }
9744
9745 if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx]) {
9746 ret = get_kern_ctx_btf_id(&env->log, resolve_prog_type(env->prog));
9747 if (ret < 0)
9748 return -EINVAL;
9749 meta->ret_btf_id = ret;
9750 }
9751 break;
9752 case KF_ARG_PTR_TO_ALLOC_BTF_ID:
9753 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9754 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9755 return -EINVAL;
9756 }
9757 if (!reg->ref_obj_id) {
9758 verbose(env, "allocated object must be referenced\n");
9759 return -EINVAL;
9760 }
9761 if (meta->btf == btf_vmlinux &&
9762 meta->func_id == special_kfunc_list[KF_bpf_obj_drop_impl]) {
9763 meta->arg_obj_drop.btf = reg->btf;
9764 meta->arg_obj_drop.btf_id = reg->btf_id;
9765 }
9766 break;
9767 case KF_ARG_PTR_TO_KPTR:
9768 if (reg->type != PTR_TO_MAP_VALUE) {
9769 verbose(env, "arg#0 expected pointer to map value\n");
9770 return -EINVAL;
9771 }
9772 ret = process_kf_arg_ptr_to_kptr(env, reg, ref_t, ref_tname, meta, i);
9773 if (ret < 0)
9774 return ret;
9775 break;
9776 case KF_ARG_PTR_TO_DYNPTR:
9777 {
9778 enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
9779
9780 if (reg->type != PTR_TO_STACK &&
9781 reg->type != CONST_PTR_TO_DYNPTR) {
9782 verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i);
9783 return -EINVAL;
9784 }
9785
9786 if (reg->type == CONST_PTR_TO_DYNPTR)
9787 dynptr_arg_type |= MEM_RDONLY;
9788
9789 if (is_kfunc_arg_uninit(btf, &args[i]))
9790 dynptr_arg_type |= MEM_UNINIT;
9791
9792 if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb])
9793 dynptr_arg_type |= DYNPTR_TYPE_SKB;
9794 else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp])
9795 dynptr_arg_type |= DYNPTR_TYPE_XDP;
9796
9797 ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);
9798 if (ret < 0)
9799 return ret;
9800
9801 if (!(dynptr_arg_type & MEM_UNINIT)) {
9802 int id = dynptr_id(env, reg);
9803
9804 if (id < 0) {
9805 verbose(env, "verifier internal error: failed to obtain dynptr id\n");
9806 return id;
9807 }
9808 meta->initialized_dynptr.id = id;
9809 meta->initialized_dynptr.type = dynptr_get_type(env, reg);
9810 }
9811
9812 break;
9813 }
9814 case KF_ARG_PTR_TO_LIST_HEAD:
9815 if (reg->type != PTR_TO_MAP_VALUE &&
9816 reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9817 verbose(env, "arg#%d expected pointer to map value or allocated object\n", i);
9818 return -EINVAL;
9819 }
9820 if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) {
9821 verbose(env, "allocated object must be referenced\n");
9822 return -EINVAL;
9823 }
9824 ret = process_kf_arg_ptr_to_list_head(env, reg, regno, meta);
9825 if (ret < 0)
9826 return ret;
9827 break;
9828 case KF_ARG_PTR_TO_RB_ROOT:
9829 if (reg->type != PTR_TO_MAP_VALUE &&
9830 reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9831 verbose(env, "arg#%d expected pointer to map value or allocated object\n", i);
9832 return -EINVAL;
9833 }
9834 if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && !reg->ref_obj_id) {
9835 verbose(env, "allocated object must be referenced\n");
9836 return -EINVAL;
9837 }
9838 ret = process_kf_arg_ptr_to_rbtree_root(env, reg, regno, meta);
9839 if (ret < 0)
9840 return ret;
9841 break;
9842 case KF_ARG_PTR_TO_LIST_NODE:
9843 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9844 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9845 return -EINVAL;
9846 }
9847 if (!reg->ref_obj_id) {
9848 verbose(env, "allocated object must be referenced\n");
9849 return -EINVAL;
9850 }
9851 ret = process_kf_arg_ptr_to_list_node(env, reg, regno, meta);
9852 if (ret < 0)
9853 return ret;
9854 break;
9855 case KF_ARG_PTR_TO_RB_NODE:
9856 if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) {
9857 if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) {
9858 verbose(env, "rbtree_remove node input must be non-owning ref\n");
9859 return -EINVAL;
9860 }
9861 if (in_rbtree_lock_required_cb(env)) {
9862 verbose(env, "rbtree_remove not allowed in rbtree cb\n");
9863 return -EINVAL;
9864 }
9865 } else {
9866 if (reg->type != (PTR_TO_BTF_ID | MEM_ALLOC)) {
9867 verbose(env, "arg#%d expected pointer to allocated object\n", i);
9868 return -EINVAL;
9869 }
9870 if (!reg->ref_obj_id) {
9871 verbose(env, "allocated object must be referenced\n");
9872 return -EINVAL;
9873 }
9874 }
9875
9876 ret = process_kf_arg_ptr_to_rbtree_node(env, reg, regno, meta);
9877 if (ret < 0)
9878 return ret;
9879 break;
9880 case KF_ARG_PTR_TO_BTF_ID:
9881 /* Only base_type is checked, further checks are done here */
9882 if ((base_type(reg->type) != PTR_TO_BTF_ID ||
9883 (bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) &&
9884 !reg2btf_ids[base_type(reg->type)]) {
9885 verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type));
9886 verbose(env, "expected %s or socket\n",
9887 reg_type_str(env, base_type(reg->type) |
9888 (type_flag(reg->type) & BPF_REG_TRUSTED_MODIFIERS)));
9889 return -EINVAL;
9890 }
9891 ret = process_kf_arg_ptr_to_btf_id(env, reg, ref_t, ref_tname, ref_id, meta, i);
9892 if (ret < 0)
9893 return ret;
9894 break;
9895 case KF_ARG_PTR_TO_MEM:
9896 resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
9897 if (IS_ERR(resolve_ret)) {
9898 verbose(env, "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
9899 i, btf_type_str(ref_t), ref_tname, PTR_ERR(resolve_ret));
9900 return -EINVAL;
9901 }
9902 ret = check_mem_reg(env, reg, regno, type_size);
9903 if (ret < 0)
9904 return ret;
9905 break;
9906 case KF_ARG_PTR_TO_MEM_SIZE:

> 9907				struct bpf_reg_state *size_reg = &regs[regno + 1];
  9908				const struct btf_param *size_arg = &args[i + 1];
  9909	
> 9910				ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);

9911 if (ret < 0) {
9912 verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1);
9913 return ret;
9914 }
9915
9916 if (is_kfunc_arg_const_mem_size(meta->btf, size_arg, size_reg)) {
9917 if (meta->arg_constant.found) {
9918 verbose(env, "verifier internal error: only one constant argument permitted\n");
9919 return -EFAULT;
9920 }
9921 if (!tnum_is_const(size_reg->var_off)) {
9922 verbose(env, "R%d must be a known constant\n", regno + 1);
9923 return -EINVAL;
9924 }
9925 meta->arg_constant.found = true;
9926 meta->arg_constant.value = size_reg->var_off.value;
9927 }
9928
9929 /* Skip next '__sz' or '__szk' argument */
9930 i++;
9931 break;
9932 case KF_ARG_PTR_TO_CALLBACK:
9933 meta->subprogno = reg->subprogno;
9934 break;
9935 }
9936 }
9937
9938 if (is_kfunc_release(meta) && !meta->release_regno) {
9939 verbose(env, "release kernel function %s expects refcounted PTR_TO_BTF_ID\n",
9940 func_name);
9941 return -EINVAL;
9942 }
9943
9944 return 0;
9945 }
9946

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#13 lore raw

PATCH v12 10/10 selftests/bpf: tests for using dynptrs to parse skb and xdp buffers

In reply to Joanne Koong

Test skb and xdp dynptr functionality in the following ways:

1) progs/test_cls_redirect_dynptr.c
* Rewrite "progs/test_cls_redirect.c" test to use dynptrs to parse skb data

* This is a great example of how dynptrs can be used to simplify a lot of the parsing logic for non-statically known values.

When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t cls_redirect"):
original version: 0.092 sec
with dynptrs: 0.078 sec

2) progs/test_xdp_dynptr.c
* Rewrite "progs/test_xdp.c" test to use dynptrs to parse xdp data

When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t xdp_attach"):
original version: 0.118 sec
with dynptrs: 0.094 sec

3) progs/test_l4lb_noinline_dynptr.c
* Rewrite "progs/test_l4lb_noinline.c" test to use dynptrs to parse skb data

When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t l4lb_all"):
original version: 0.062 sec
with dynptrs: 0.081 sec

For number of processed verifier instructions:
original version: 6268 insns
with dynptrs: 2588 insns

4) progs/test_parse_tcp_hdr_opt_dynptr.c
* Add sample code for parsing tcp hdr opt lookup using dynptrs. This logic is lifted from a real-world use case of packet parsing in katran [0], a layer 4 load balancer. The original version "progs/test_parse_tcp_hdr_opt.c" (not using dynptrs) is included here as well, for comparison.

When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t parse_tcp_hdr_opt"):
original version: 0.031 sec
with dynptrs: 0.045 sec

5) progs/dynptr_success.c
* Add test case "test_skb_readonly" for testing attempts at writes on a prog type with read-only skb ctx.
* Add "test_dynptr_skb_data" for testing that bpf_dynptr_data isn't supported for skb progs.

6) progs/dynptr_fail.c
* Add test cases "skb_invalid_data_slice{1,2,3,4}" and "xdp_invalid_data_slice{1,2}" for testing that helpers that modify the underlying packet buffer automatically invalidate the associated data slice.
* Add test cases "skb_invalid_ctx" and "xdp_invalid_ctx" for testing that prog types that do not support bpf_dynptr_from_skb/xdp don't have access to the API.
* Add test case "dynptr_slice_var_len{1,2}" for testing that variable-sized len can't be passed in to bpf_dynptr_slice
* Add test case "skb_invalid_slice_write" for testing that writes to a read-only data slice are rejected by the verifier.
* Add test case "data_slice_out_of_bounds_skb" for testing that writes to an area outside the slice are rejected.
* Add test case "invalid_slice_rdwr_rdonly" for testing that prog types that don't allow writes to packet data don't accept any calls to bpf_dynptr_slice_rdwr.

[0] https://github.com/facebookincubator/katran/blob/main/katran/lib/bpf/pckt_parsing.h

---
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  38 +
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |  74 +-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  93 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |  11 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 287 ++++-
 .../selftests/bpf/progs/dynptr_success.c      |  55 +-
 .../bpf/progs/test_cls_redirect_dynptr.c      | 980 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 487 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 114 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 257 +++++
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 14 files changed, 2520 insertions(+), 23 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/bpf_kfuncs.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c
tools/testing/selftests/bpf/bpf_kfuncs.h new file +38 -0
new file mode 100644
index 000000000000..8c993ec8ceea
@@ -0,0 +1,38 @@
+#ifndef __BPF_KFUNCS__
+#define __BPF_KFUNCS__
+
+/* Description
+ *  Initializes an skb-type dynptr
+ * Returns
+ *  Error code
+ */
+extern int bpf_dynptr_from_skb(struct __sk_buff *skb, __u64 flags,
+    struct bpf_dynptr *ptr__uninit) __ksym;
+
+/* Description
+ *  Initializes an xdp-type dynptr
+ * Returns
+ *  Error code
+ */
+extern int bpf_dynptr_from_xdp(struct xdp_md *xdp, __u64 flags,
+			       struct bpf_dynptr *ptr__uninit) __ksym;
+
+/* Description
+ *  Obtain a read-only pointer to the dynptr's data
+ * Returns
+ *  Either a direct pointer to the dynptr data or a pointer to the user-provided
+ *  buffer if unable to obtain a direct pointer
+ */
+extern void *bpf_dynptr_slice(const struct bpf_dynptr *ptr, __u32 offset,
+			      void *buffer, __u32 buffer__szk) __ksym;
+
+/* Description
+ *  Obtain a read-write pointer to the dynptr's data
+ * Returns
+ *  Either a direct pointer to the dynptr data or a pointer to the user-provided
+ *  buffer if unable to obtain a direct pointer
+ */
+extern void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *ptr, __u32 offset,
+			      void *buffer, __u32 buffer__szk) __ksym;
+
+#endif
tools/testing/selftests/bpf/prog_tests/cls_redirect.c +25 -0
index 224f016b0a53..2a55f717fc07 100644
@@ -13,6 +13,7 @@
 
 #include "progs/test_cls_redirect.h"
 #include "test_cls_redirect.skel.h"
+#include "test_cls_redirect_dynptr.skel.h"
 #include "test_cls_redirect_subprogs.skel.h"
 
 #define ENCAP_IP INADDR_LOOPBACK
@@ -446,6 +447,28 @@ static void test_cls_redirect_common(struct bpf_program *prog)
 	close_fds((int *)conns, sizeof(conns) / sizeof(conns[0][0]));
 }
 
+static void test_cls_redirect_dynptr(void)
+{
+	struct test_cls_redirect_dynptr *skel;
+	int err;
+
+	skel = test_cls_redirect_dynptr__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	skel->rodata->ENCAPSULATION_IP = htonl(ENCAP_IP);
+	skel->rodata->ENCAPSULATION_PORT = htons(ENCAP_PORT);
+
+	err = test_cls_redirect_dynptr__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	test_cls_redirect_common(skel->progs.cls_redirect);
+
+cleanup:
+	test_cls_redirect_dynptr__destroy(skel);
+}
+
 static void test_cls_redirect_inlined(void)
 {
 	struct test_cls_redirect *skel;
@@ -496,4 +519,6 @@ void test_cls_redirect(void)
 		test_cls_redirect_inlined();
 	if (test__start_subtest("cls_redirect_subprogs"))
 		test_cls_redirect_subprogs();
+	if (test__start_subtest("cls_redirect_dynptr"))
+		test_cls_redirect_dynptr();
 }
tools/testing/selftests/bpf/prog_tests/dynptr.c +58 -16

111 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/l4lb_all.c +2 -0
index 9c1a18573ffd..1eab286b14fe 100644
@@ -93,4 +93,6 @@ void test_l4lb_all(void)
 		test_l4lb("test_l4lb.bpf.o");
 	if (test__start_subtest("l4lb_noinline"))
 		test_l4lb("test_l4lb_noinline.bpf.o");
+	if (test__start_subtest("l4lb_noinline_dynptr"))
+		test_l4lb("test_l4lb_noinline_dynptr.bpf.o");
 }
tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c new file +93 -0

93 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/xdp_attach.c +9 -2
index d4cd9f873c14..fa3cac5488f5 100644
@@ -4,11 +4,10 @@
 #define IFINDEX_LO 1
 #define XDP_FLAGS_REPLACE		(1U << 4)
 
-void serial_test_xdp_attach(void)
+static void test_xdp_attach(const char *file)
 {
 	__u32 duration = 0, id1, id2, id0 = 0, len;
 	struct bpf_object *obj1, *obj2, *obj3;
-	const char *file = "./test_xdp.bpf.o";
 	struct bpf_prog_info info = {};
 	int err, fd1, fd2, fd3;
 	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
@@ -85,3 +84,11 @@ void serial_test_xdp_attach(void)
 out_1:
 	bpf_object__close(obj1);
 }
+
+void serial_test_xdp_attach(void)
+{
+	if (test__start_subtest("xdp_attach"))
+		test_xdp_attach("./test_xdp.bpf.o");
+	if (test__start_subtest("xdp_attach_dynptr"))
+		test_xdp_attach("./test_xdp_dynptr.bpf.o");
+}
tools/testing/selftests/bpf/progs/dynptr_fail.c +286 -1

321 lines not on this page · raw message

tools/testing/selftests/bpf/progs/dynptr_success.c +51 -4

82 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c new file +980 -0

980 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c new file +487 -0

487 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c new file +119 -0

119 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c new file +114 -0

114 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +257 -0

257 lines not on this page · raw message

tools/testing/selftests/bpf/test_tcp_hdr_options.h +1 -0
index 6118e3ab61fc..56c9f8a3ad3d 100644
@@ -50,6 +50,7 @@ struct linum_err {
 
 #define TCPOPT_EOL		0
 #define TCPOPT_NOP		1
+#define TCPOPT_MSS		2
 #define TCPOPT_WINDOW		3
 #define TCPOPT_EXP		254
 
Signature · 1 line
2.34.1