Skip to contentSkip to conversation

Add skb + xdp dynptrs

PATCH Superseded Joanne Koong 6 participants 43 messages Aug 22, 2022 – Aug 27, 2022

Subsystems (8)

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v4 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 for the more simple cases, there is no noticeable difference. For the more complex cases where lengths are non-statically known at compile time, there can be a significant speed-up when using dynptrs (eg a 2x speed up for cls redirection). Patch 3 contains more details as well as examples of how to use skb and xdp dynptrs.

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

--
Changelog:

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 (3):
bpf: Add skb dynptrs
bpf: Add xdp dynptrs
selftests/bpf: tests for using dynptrs to parse skb and xdp buffers

 include/linux/bpf.h                           |  87 +-
 include/linux/filter.h                        |   7 +
 include/uapi/linux/bpf.h                      |  59 +-
 kernel/bpf/helpers.c                          |  93 +-
 kernel/bpf/verifier.c                         | 105 +-
 net/core/filter.c                             |  99 +-
 tools/include/uapi/linux/bpf.h                |  59 +-
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c | 132 ++-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  85 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |   9 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 111 ++
 .../selftests/bpf/progs/dynptr_success.c      |  29 +
 .../bpf/progs/test_cls_redirect_dynptr.c      | 968 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 468 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 110 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 240 +++++
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 20 files changed, 2693 insertions(+), 115 deletions(-)
 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.30.2
#1 lore raw

PATCH v4 1/3 bpf: Add skb dynptrs

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. For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, this supports reading and writing into data in the non-linear paged buffers. For data slices (through the bpf_dynptr_data() interface), if the data is in a paged buffer, the user must first call bpf_skb_pull_data() to pull the data into the linear portion. The returned data slice from a call to bpf_dynptr_data() is of reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any bpf_dynptr_write() automatically invalidates any prior data slices to the skb dynptr. This is because a bpf_dynptr_write() may be writing to data in a paged buffer, so it will need to pull the buffer first into the head. The reason it needs to be pulled instead of writing directly to the paged buffers is because they may be cloned (only the head of the skb is by default uncloned). 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 (the verifier has no way of differentiating whether the write is to the head or paged buffers during program load time). 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. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes skb dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            | 83 +++++++++++++++++-----------
 include/linux/filter.h         |  4 ++
 include/uapi/linux/bpf.h       | 40 ++++++++++++--
 kernel/bpf/helpers.c           | 81 +++++++++++++++++++++++++---
 kernel/bpf/verifier.c          | 99 ++++++++++++++++++++++++++++------
 net/core/filter.c              | 53 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h | 40 ++++++++++++--
 7 files changed, 335 insertions(+), 65 deletions(-)
include/linux/bpf.h +53 -30
index 39bd36359c1e..30615d1a0c13 100644
@@ -407,11 +407,14 @@ enum bpf_type_flag {
 	/* Size is known at compile time. */
 	MEM_FIXED_SIZE		= BIT(10 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to sk_buff */
+	DYNPTR_TYPE_SKB		= BIT(11 + 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)
@@ -903,6 +906,36 @@ 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,
+	/* 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);
+
 #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);
@@ -1975,6 +2008,12 @@ static inline bool has_current_bpf_ctx(void)
 {
 	return !!current->bpf_ctx;
 }
+
+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)
 {
@@ -2188,6 +2227,19 @@ static inline bool has_current_bpf_ctx(void)
 {
 	return false;
 }
+
+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,
@@ -2548,35 +2600,6 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
 			u32 **bin_buf, u32 num_args);
 void bpf_bprintf_cleanup(void);
 
-/* 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,
-};
-
-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);
-
 #ifdef CONFIG_BPF_LSM
 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
 void bpf_cgroup_atype_put(int cgroup_atype);
include/linux/filter.h +4 -0
index a5f21dc3c432..649063d9cbfd 100644
@@ -1532,4 +1532,8 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u32 ifind
 	return XDP_REDIRECT;
 }
 
+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);
+
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +37 -3
index 934a2a8beb87..320e6b95d95c 100644
@@ -5253,11 +5253,22 @@ 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:
+ *		    *  All data slices of the dynptr are automatically
+ *		       invalidated after **bpf_dynptr_write**\ (). If you wish to
+ *		       avoid this, please perform the write using direct data slices
+ *		       instead.
+ *
+ *		    *  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(struct bpf_dynptr *ptr, u32 offset, u32 len)
  *	Description
@@ -5265,10 +5276,20 @@ union bpf_attr {
  *
  *		*len* must be a statically known value. The returned data slice
  *		is invalidated whenever the dynptr is invalidated.
+ *
+ *		For skb-type dynptrs:
+ *		    * If *offset* + *len* extends into the skb's paged buffers,
+ *		      the user should manually pull the skb with **bpf_skb_pull_data**\ ()
+ *		      and try again.
+ *
+ *		    * The data slice is automatically invalidated anytime
+ *		      **bpf_dynptr_write**\ () or a helper call that changes
+ *		      the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *		      is called.
  *	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
- *		is out of bounds.
+ *		is out of bounds or in a paged buffer for skb-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *	Description
@@ -5355,6 +5376,18 @@ union bpf_attr {
  *	Return
  *		Current *ktime*.
  *
+ * long bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags, struct bpf_dynptr *ptr)
+ *	Description
+ *		Get a dynptr to the data in *skb*. *skb* must be the BPF program
+ *		context. Depending on program type, the dynptr may be read-only.
+ *
+ *		Calls that change the *skb*'s underlying packet buffer
+ *		(eg **bpf_skb_pull_data**\ ()) do not invalidate the dynptr, but
+ *		they do invalidate any data slices associated with the dynptr.
+ *
+ *		*flags* is currently unused, it must be 0 for now.
+ *	Return
+ *		0 on success or -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5566,6 +5599,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv4),	\
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
+	FN(dynptr_from_skb),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/helpers.c +73 -8

133 lines not on this page · raw message

kernel/bpf/verifier.c +82 -17

151 lines not on this page · raw message

net/core/filter.c +49 -4

107 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +37 -3
index 1d6085e15fc8..3f1800a2b77c 100644
@@ -5253,11 +5253,22 @@ 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:
+ *		    *  All data slices of the dynptr are automatically
+ *		       invalidated after **bpf_dynptr_write**\ (). If you wish to
+ *		       avoid this, please perform the write using direct data slices
+ *		       instead.
+ *
+ *		    *  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(struct bpf_dynptr *ptr, u32 offset, u32 len)
  *	Description
@@ -5265,10 +5276,20 @@ union bpf_attr {
  *
  *		*len* must be a statically known value. The returned data slice
  *		is invalidated whenever the dynptr is invalidated.
+ *
+ *		For skb-type dynptrs:
+ *		    * If *offset* + *len* extends into the skb's paged buffers,
+ *		      the user should manually pull the skb with **bpf_skb_pull_data**\ ()
+ *		      and try again.
+ *
+ *		    * The data slice is automatically invalidated anytime
+ *		      **bpf_dynptr_write**\ () or a helper call that changes
+ *		      the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *		      is called.
  *	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
- *		is out of bounds.
+ *		is out of bounds or in a paged buffer for skb-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *	Description
@@ -5355,6 +5376,18 @@ union bpf_attr {
  *	Return
  *		Current *ktime*.
  *
+ * long bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags, struct bpf_dynptr *ptr)
+ *	Description
+ *		Get a dynptr to the data in *skb*. *skb* must be the BPF program
+ *		context. Depending on program type, the dynptr may be read-only.
+ *
+ *		Calls that change the *skb*'s underlying packet buffer
+ *		(eg **bpf_skb_pull_data**\ ()) do not invalidate the dynptr, but
+ *		they do invalidate any data slices associated with the dynptr.
+ *
+ *		*flags* is currently unused, it must be 0 for now.
+ *	Return
+ *		0 on success or -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5566,6 +5599,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv4),	\
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
+	FN(dynptr_from_skb),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

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/Add-skb-xdp-dynptrs/20220823-080022
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: arm-buildonly-randconfig-r002-20220823 (https://download.01.org/0day-ci/archive/20220824/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 12.1.0

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/a2c8a74d8f0b7fd0b0008dc9bc5ccf9887317f36
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/Add-skb-xdp-dynptrs/20220823-080022
git checkout a2c8a74d8f0b7fd0b0008dc9bc5ccf9887317f36
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: kernel/bpf/helpers.o: in function `bpf_dynptr_read':

>> helpers.c:(.text+0xb1c): undefined reference to `__bpf_skb_load_bytes'
   arm-linux-gnueabi-ld: kernel/bpf/helpers.o: in function `bpf_dynptr_write':
>> helpers.c:(.text+0xc30): undefined reference to `__bpf_skb_store_bytes'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#3 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

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/Add-skb-xdp-dynptrs/20220823-080022
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: csky-randconfig-r022-20220823 (https://download.01.org/0day-ci/archive/20220824/[email protected]/config)
compiler: csky-linux-gcc (GCC) 12.1.0

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/a2c8a74d8f0b7fd0b0008dc9bc5ccf9887317f36
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/Add-skb-xdp-dynptrs/20220823-080022
git checkout a2c8a74d8f0b7fd0b0008dc9bc5ccf9887317f36
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=csky SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

csky-linux-ld: kernel/bpf/helpers.o: in function `____bpf_dynptr_read':

>> kernel/bpf/helpers.c:1543: undefined reference to `__bpf_skb_load_bytes'
   csky-linux-ld: kernel/bpf/helpers.o: in function `bpf_dynptr_read':
   kernel/bpf/helpers.c:1522: undefined reference to `__bpf_skb_load_bytes'
   csky-linux-ld: kernel/bpf/helpers.o: in function `____bpf_dynptr_write':
>> kernel/bpf/helpers.c:1584: undefined reference to `__bpf_skb_store_bytes'

csky-linux-ld: kernel/bpf/helpers.o: in function `bpf_dynptr_write':
kernel/bpf/helpers.c:1561: undefined reference to `__bpf_skb_store_bytes'

vim +1543 kernel/bpf/helpers.c

1521
1522 BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src,
1523 u32, offset, u64, flags)
1524 {
1525 enum bpf_dynptr_type type;
1526 int err;
1527
1528 if (!src->data || flags)
1529 return -EINVAL;
1530
1531 err = bpf_dynptr_check_off_len(src, offset, len);
1532 if (err)
1533 return err;
1534
1535 type = bpf_dynptr_get_type(src);
1536
1537 switch (type) {
1538 case BPF_DYNPTR_TYPE_LOCAL:
1539 case BPF_DYNPTR_TYPE_RINGBUF:
1540 memcpy(dst, src->data + src->offset + offset, len);
1541 return 0;
1542 case BPF_DYNPTR_TYPE_SKB:

> 1543			return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
  1544		default:
  1545			WARN(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
  1546			return -EFAULT;
  1547		}
  1548	}
  1549	
  1550	static const struct bpf_func_proto bpf_dynptr_read_proto = {
  1551		.func		= bpf_dynptr_read,
  1552		.gpl_only	= false,
  1553		.ret_type	= RET_INTEGER,
  1554		.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
  1555		.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
  1556		.arg3_type	= ARG_PTR_TO_DYNPTR,
  1557		.arg4_type	= ARG_ANYTHING,
  1558		.arg5_type	= ARG_ANYTHING,
  1559	};
  1560	
  1561	BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
  1562		   u32, len, u64, flags)
  1563	{
  1564		enum bpf_dynptr_type type;
  1565		int err;
  1566	
  1567		if (!dst->data || bpf_dynptr_is_rdonly(dst))
  1568			return -EINVAL;
  1569	
  1570		err = bpf_dynptr_check_off_len(dst, offset, len);
  1571		if (err)
  1572			return err;
  1573	
  1574		type = bpf_dynptr_get_type(dst);
  1575	
  1576		switch (type) {
  1577		case BPF_DYNPTR_TYPE_LOCAL:
  1578		case BPF_DYNPTR_TYPE_RINGBUF:
  1579			if (flags)
  1580				return -EINVAL;
  1581			memcpy(dst->data + dst->offset + offset, src, len);
  1582			return 0;
  1583		case BPF_DYNPTR_TYPE_SKB:
> 1584			return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,

1585 flags);
1586 default:
1587 WARN(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
1588 return -EFAULT;
1589 }
1590 }
1591

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#4 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

In reply to Joanne Koong
Quoted 50 lines · 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. For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, this supports reading and writing into data in the non-linear paged buffers. For data slices (through the bpf_dynptr_data() interface), if the data is in a paged buffer, the user must first call bpf_skb_pull_data() to pull the data into the linear portion. The returned data slice from a call to bpf_dynptr_data() is of reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any bpf_dynptr_write() automatically invalidates any prior data slices to the skb dynptr. This is because a bpf_dynptr_write() may be writing to data in a paged buffer, so it will need to pull the buffer first into the head. The reason it needs to be pulled instead of writing directly to the paged buffers is because they may be cloned (only the head of the skb is by default uncloned). 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 (the verifier has no way of differentiating whether the write is to the head or paged buffers during program load time). 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. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes skb dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            | 83 +++++++++++++++++-----------
 include/linux/filter.h         |  4 ++
 include/uapi/linux/bpf.h       | 40 ++++++++++++--
 kernel/bpf/helpers.c           | 81 +++++++++++++++++++++++++---
 kernel/bpf/verifier.c          | 99 ++++++++++++++++++++++++++++------
 net/core/filter.c              | 53 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h | 40 ++++++++++++--
 7 files changed, 335 insertions(+), 65 deletions(-)

[...]

Quoted 7 lines
kernel/bpf/helpers.c +1 -1
@@ -1521,9 +1532,19 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
        if (err)
                return err;
 
-       memcpy(dst, src->data + src->offset + offset, len);
+       type = bpf_dynptr_get_type(src);
 

kernel/bpf/helpers.c BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src · line 1545

Quoted 10 lines
kernel/bpf/helpers.c +9 -1
-       return 0;
+       switch (type) {
+       case BPF_DYNPTR_TYPE_LOCAL:
+       case BPF_DYNPTR_TYPE_RINGBUF:
+               memcpy(dst, src->data + src->offset + offset, len);
+               return 0;
+       case BPF_DYNPTR_TYPE_SKB:
+               return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
+       default:
+               WARN(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);

nit: probably better to WARN_ONCE?

Quoted 26 lines
kernel/bpf/helpers.c +7 -3
+               return -EFAULT;
+       }
 }
 
 static const struct bpf_func_proto bpf_dynptr_read_proto = {
@@ -1540,18 +1561,32 @@ static const struct bpf_func_proto bpf_dynptr_read_proto = {
 BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
           u32, len, u64, flags)
 {
+       enum bpf_dynptr_type type;
        int err;
 
-       if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
+       if (!dst->data || bpf_dynptr_is_rdonly(dst))
                return -EINVAL;
 
        err = bpf_dynptr_check_off_len(dst, offset, len);
        if (err)
                return err;
 
-       memcpy(dst->data + dst->offset + offset, src, len);
+       type = bpf_dynptr_get_type(dst);
 
-       return 0;
+       switch (type) {
+       case BPF_DYNPTR_TYPE_LOCAL:

kernel/bpf/helpers.c static const struct bpf_func_proto bpf_dynptr_read_proto = { · line 1587

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       case BPF_DYNPTR_TYPE_RINGBUF:
+               if (flags)
+                       return -EINVAL;
+               memcpy(dst->data + dst->offset + offset, src, len);
+               return 0;
+       case BPF_DYNPTR_TYPE_SKB:
+               return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,
+                                            flags);
+       default:
+               WARN(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);

ditto about WARN_ONCE

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
+               return -EFAULT;
+       }
 }
 
 static const struct bpf_func_proto bpf_dynptr_write_proto = {

[...]

Quoted 26 lines
kernel/bpf/verifier.c +9 -2
+static enum bpf_dynptr_type stack_slot_get_dynptr_info(struct bpf_verifier_env *env,
+                                                      struct bpf_reg_state *reg,
+                                                      int *ref_obj_id)
 {
        struct bpf_func_state *state = func(env, reg);
        int spi = get_spi(reg->off);
 
-       return state->stack[spi].spilled_ptr.id;
+       if (ref_obj_id)
+               *ref_obj_id = state->stack[spi].spilled_ptr.id;
+
+       return state->stack[spi].spilled_ptr.dynptr.type;
 }
 
 static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
@@ -6056,7 +6075,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                        case DYNPTR_TYPE_RINGBUF:
                                err_extra = "ringbuf ";
                                break;
-                       default:
+                       case DYNPTR_TYPE_SKB:
+                               err_extra = "skb ";
                                break;
                        }
 

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7172

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -7149,6 +7169,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 {
        enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
        const struct bpf_func_proto *fn = NULL;
+       enum bpf_dynptr_type dynptr_type;

compiler technically can complain about use of uninitialized dynptr_type, maybe initialize it to BPF_DYNPTR_TYPE_INVALID ?

Quoted 15 lines
kernel/bpf/verifier.c +0 -8
        enum bpf_return_type ret_type;
        enum bpf_type_flag ret_flag;
        struct bpf_reg_state *regs;
@@ -7320,24 +7341,43 @@ 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])) {
-                               if (meta.ref_obj_id) {
-                                       verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
-                                       return -EFAULT;
-                               }
-                               /* Find the id of the dynptr we're tracking the reference of */

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7350

Quoted 10 lines
kernel/bpf/verifier.c +7 -3
-                               meta.ref_obj_id = stack_slot_get_id(env, &regs[BPF_REG_1 + i]);
-                               break;
-                       }
+       case BPF_FUNC_dynptr_write:
+       {
+               struct bpf_reg_state *reg;
+
+               reg = get_dynptr_arg_reg(fn, regs);
+               if (!reg) {
+                       verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");

s/bpf_dynptr_data/bpf_dynptr_write/

Quoted 53 lines
kernel/bpf/verifier.c +33 -3
+                       return -EFAULT;
                }
-               if (i == MAX_BPF_FUNC_REG_ARGS) {
+
+               /* bpf_dynptr_write() for skb-type dynptrs may pull the skb, so we must
+                * invalidate all data slices associated with it
+                */
+               if (stack_slot_get_dynptr_info(env, reg, NULL) == BPF_DYNPTR_TYPE_SKB)
+                       changes_data = true;
+
+               break;
+       }
+       case BPF_FUNC_dynptr_data:
+       {
+               struct bpf_reg_state *reg;
+
+               reg = get_dynptr_arg_reg(fn, regs);
+               if (!reg) {
                        verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");
                        return -EFAULT;
                }
+
+               if (meta.ref_obj_id) {
+                       verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
+                       return -EFAULT;
+               }
+
+               dynptr_type = stack_slot_get_dynptr_info(env, reg, &meta.ref_obj_id);
                break;
        }
+       }
 
        if (err)
                return err;
@@ -7397,8 +7437,15 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
                break;
        case RET_PTR_TO_ALLOC_MEM:
                mark_reg_known_zero(env, regs, BPF_REG_0);
-               regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
-               regs[BPF_REG_0].mem_size = meta.mem_size;
+
+               if (func_id == BPF_FUNC_dynptr_data &&
+                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                       regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
+                       regs[BPF_REG_0].range = meta.mem_size;
+               } else {
+                       regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
+                       regs[BPF_REG_0].mem_size = meta.mem_size;
+               }
                break;
        case RET_PTR_TO_MEM_OR_BTF_ID:
        {

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 14196

Quoted 9 lines
kernel/bpf/verifier.c +6 -0
@@ -14141,6 +14188,24 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
                        goto patch_call_imm;
                }
 
+               if (insn->imm == BPF_FUNC_dynptr_from_skb) {
+                       bool is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
+
+                       insn_buf[0] = BPF_MOV32_IMM(BPF_REG_4, is_rdonly);
+                       insn_buf[1] = *insn;
+                       cnt = 2;

This might have been discussed before, sorry if I missed it. But why this special rewrite to provide read-only flag vs having two implementations of bpf_dynptr_from_skb() depending on program types? If program type allows read+write access, return bpf_dynptr_from_skb_rdwr(), for those that have read-only access - bpf_dynptr_from_skb_rdonly(), and for others return NULL proto (disable helper)?

You can then use it very similarly for bpf_dynptr_from_skb_meta().

Quoted 12 lines
kernel/bpf/verifier.c +12 -0
+
+                       new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+                       if (!new_prog)
+                               return -ENOMEM;
+
+                       delta += cnt - 1;
+                       env->prog = new_prog;
+                       prog = new_prog;
+                       insn = new_prog->insnsi + i + delta;
+                       goto patch_call_imm;
+               }
+

[...]

Quoted 31 lines

BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)

 {
        return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
net/core/filter.c +6 -0
@@ -7726,6 +7763,8 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_get_socket_uid_proto;
        case BPF_FUNC_perf_event_output:
                return &bpf_skb_event_output_proto;
+       case BPF_FUNC_dynptr_from_skb:
+               return &bpf_dynptr_from_skb_proto;
        default:
                return bpf_sk_base_func_proto(func_id);
        }
@@ -7909,6 +7948,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_tcp_raw_check_syncookie_ipv6_proto;
 #endif
 #endif
+       case BPF_FUNC_dynptr_from_skb:
+               return &bpf_dynptr_from_skb_proto;
        default:
                return bpf_sk_base_func_proto(func_id);
        }
@@ -8104,6 +8145,8 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
        case BPF_FUNC_skc_lookup_tcp:
                return &bpf_skc_lookup_tcp_proto;
 #endif
+       case BPF_FUNC_dynptr_from_skb:
+               return &bpf_dynptr_from_skb_proto;
        default:
                return bpf_sk_base_func_proto(func_id);
        }

net/core/filter.c lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 8189

Quoted 5 lines
net/core/filter.c +2 -0
@@ -8142,6 +8185,8 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_get_smp_processor_id_proto;
        case BPF_FUNC_skb_under_cgroup:
                return &bpf_skb_under_cgroup_proto;
+       case BPF_FUNC_dynptr_from_skb:
+               return &bpf_dynptr_from_skb_proto;

so like here you'd return read-only prototype for dynptr_from_skb (seems like LWT programs have only read-only access, according to may_access_direct_pkt_data), but in cases where may_access_direct_pkt_data() allows read-write access we'd choose rdwr variant of dynptr_from_skb?

Quoted 3 lines
net/core/filter.c +0 -0
        default:
                return bpf_sk_base_func_proto(func_id);
        }

[...]

#5 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 255 lines · Andrii Nakryiko
Quoted 50 lines · 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. For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, this supports reading and writing into data in the non-linear paged buffers. For data slices (through the bpf_dynptr_data() interface), if the data is in a paged buffer, the user must first call bpf_skb_pull_data() to pull the data into the linear portion. The returned data slice from a call to bpf_dynptr_data() is of reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any bpf_dynptr_write() automatically invalidates any prior data slices to the skb dynptr. This is because a bpf_dynptr_write() may be writing to data in a paged buffer, so it will need to pull the buffer first into the head. The reason it needs to be pulled instead of writing directly to the paged buffers is because they may be cloned (only the head of the skb is by default uncloned). 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 (the verifier has no way of differentiating whether the write is to the head or paged buffers during program load time). 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. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes skb dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            | 83 +++++++++++++++++-----------
 include/linux/filter.h         |  4 ++
 include/uapi/linux/bpf.h       | 40 ++++++++++++--
 kernel/bpf/helpers.c           | 81 +++++++++++++++++++++++++---
 kernel/bpf/verifier.c          | 99 ++++++++++++++++++++++++++++------
 net/core/filter.c              | 53 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h | 40 ++++++++++++--
 7 files changed, 335 insertions(+), 65 deletions(-)

[...]

Quoted 7 lines
kernel/bpf/helpers.c +1 -1
@@ -1521,9 +1532,19 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
        if (err)
                return err;
 
-       memcpy(dst, src->data + src->offset + offset, len);
+       type = bpf_dynptr_get_type(src);
 

kernel/bpf/helpers.c BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src · line 1545

Quoted 10 lines
kernel/bpf/helpers.c +9 -1
-       return 0;
+       switch (type) {
+       case BPF_DYNPTR_TYPE_LOCAL:
+       case BPF_DYNPTR_TYPE_RINGBUF:
+               memcpy(dst, src->data + src->offset + offset, len);
+               return 0;
+       case BPF_DYNPTR_TYPE_SKB:
+               return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
+       default:
+               WARN(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);

nit: probably better to WARN_ONCE?

Quoted 26 lines
kernel/bpf/helpers.c +7 -3
+               return -EFAULT;
+       }
 }
 
 static const struct bpf_func_proto bpf_dynptr_read_proto = {
@@ -1540,18 +1561,32 @@ static const struct bpf_func_proto bpf_dynptr_read_proto = {
 BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
           u32, len, u64, flags)
 {
+       enum bpf_dynptr_type type;
        int err;
 
-       if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
+       if (!dst->data || bpf_dynptr_is_rdonly(dst))
                return -EINVAL;
 
        err = bpf_dynptr_check_off_len(dst, offset, len);
        if (err)
                return err;
 
-       memcpy(dst->data + dst->offset + offset, src, len);
+       type = bpf_dynptr_get_type(dst);
 
-       return 0;
+       switch (type) {
+       case BPF_DYNPTR_TYPE_LOCAL:

kernel/bpf/helpers.c static const struct bpf_func_proto bpf_dynptr_read_proto = { · line 1587

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       case BPF_DYNPTR_TYPE_RINGBUF:
+               if (flags)
+                       return -EINVAL;
+               memcpy(dst->data + dst->offset + offset, src, len);
+               return 0;
+       case BPF_DYNPTR_TYPE_SKB:
+               return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,
+                                            flags);
+       default:
+               WARN(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);

ditto about WARN_ONCE

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
+               return -EFAULT;
+       }
 }
 
 static const struct bpf_func_proto bpf_dynptr_write_proto = {

[...]

Quoted 26 lines
kernel/bpf/verifier.c +9 -2
+static enum bpf_dynptr_type stack_slot_get_dynptr_info(struct bpf_verifier_env *env,
+                                                      struct bpf_reg_state *reg,
+                                                      int *ref_obj_id)
 {
        struct bpf_func_state *state = func(env, reg);
        int spi = get_spi(reg->off);
 
-       return state->stack[spi].spilled_ptr.id;
+       if (ref_obj_id)
+               *ref_obj_id = state->stack[spi].spilled_ptr.id;
+
+       return state->stack[spi].spilled_ptr.dynptr.type;
 }
 
 static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
@@ -6056,7 +6075,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                        case DYNPTR_TYPE_RINGBUF:
                                err_extra = "ringbuf ";
                                break;
-                       default:
+                       case DYNPTR_TYPE_SKB:
+                               err_extra = "skb ";
                                break;
                        }
 

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7172

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -7149,6 +7169,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 {
        enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
        const struct bpf_func_proto *fn = NULL;
+       enum bpf_dynptr_type dynptr_type;

compiler technically can complain about use of uninitialized dynptr_type, maybe initialize it to BPF_DYNPTR_TYPE_INVALID ?

Quoted 15 lines
kernel/bpf/verifier.c +0 -8
        enum bpf_return_type ret_type;
        enum bpf_type_flag ret_flag;
        struct bpf_reg_state *regs;
@@ -7320,24 +7341,43 @@ 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])) {
-                               if (meta.ref_obj_id) {
-                                       verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
-                                       return -EFAULT;
-                               }
-                               /* Find the id of the dynptr we're tracking the reference of */

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7350

Quoted 10 lines
kernel/bpf/verifier.c +7 -3
-                               meta.ref_obj_id = stack_slot_get_id(env, &regs[BPF_REG_1 + i]);
-                               break;
-                       }
+       case BPF_FUNC_dynptr_write:
+       {
+               struct bpf_reg_state *reg;
+
+               reg = get_dynptr_arg_reg(fn, regs);
+               if (!reg) {
+                       verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");

s/bpf_dynptr_data/bpf_dynptr_write/

Quoted 53 lines
kernel/bpf/verifier.c +33 -3
+                       return -EFAULT;
                }
-               if (i == MAX_BPF_FUNC_REG_ARGS) {
+
+               /* bpf_dynptr_write() for skb-type dynptrs may pull the skb, so we must
+                * invalidate all data slices associated with it
+                */
+               if (stack_slot_get_dynptr_info(env, reg, NULL) == BPF_DYNPTR_TYPE_SKB)
+                       changes_data = true;
+
+               break;
+       }
+       case BPF_FUNC_dynptr_data:
+       {
+               struct bpf_reg_state *reg;
+
+               reg = get_dynptr_arg_reg(fn, regs);
+               if (!reg) {
                        verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");
                        return -EFAULT;
                }
+
+               if (meta.ref_obj_id) {
+                       verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
+                       return -EFAULT;
+               }
+
+               dynptr_type = stack_slot_get_dynptr_info(env, reg, &meta.ref_obj_id);
                break;
        }
+       }
 
        if (err)
                return err;
@@ -7397,8 +7437,15 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
                break;
        case RET_PTR_TO_ALLOC_MEM:
                mark_reg_known_zero(env, regs, BPF_REG_0);
-               regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
-               regs[BPF_REG_0].mem_size = meta.mem_size;
+
+               if (func_id == BPF_FUNC_dynptr_data &&
+                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                       regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
+                       regs[BPF_REG_0].range = meta.mem_size;
+               } else {
+                       regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
+                       regs[BPF_REG_0].mem_size = meta.mem_size;
+               }
                break;
        case RET_PTR_TO_MEM_OR_BTF_ID:
        {

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 14196

Quoted 9 lines
kernel/bpf/verifier.c +6 -0
@@ -14141,6 +14188,24 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
                        goto patch_call_imm;
                }
 
+               if (insn->imm == BPF_FUNC_dynptr_from_skb) {
+                       bool is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
+
+                       insn_buf[0] = BPF_MOV32_IMM(BPF_REG_4, is_rdonly);
+                       insn_buf[1] = *insn;
+                       cnt = 2;

This might have been discussed before, sorry if I missed it. But why this special rewrite to provide read-only flag vs having two implementations of bpf_dynptr_from_skb() depending on program types? If program type allows read+write access, return bpf_dynptr_from_skb_rdwr(), for those that have read-only access - bpf_dynptr_from_skb_rdonly(), and for others return NULL proto (disable helper)?

Quoted 2 lines

You can then use it very similarly for bpf_dynptr_from_skb_meta().

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

#6 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 242 lines · Kumar Kartikeya Dwivedi
Quoted 240 lines · Andrii Nakryiko
Quoted 50 lines · 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. For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, this supports reading and writing into data in the non-linear paged buffers. For data slices (through the bpf_dynptr_data() interface), if the data is in a paged buffer, the user must first call bpf_skb_pull_data() to pull the data into the linear portion. The returned data slice from a call to bpf_dynptr_data() is of reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any bpf_dynptr_write() automatically invalidates any prior data slices to the skb dynptr. This is because a bpf_dynptr_write() may be writing to data in a paged buffer, so it will need to pull the buffer first into the head. The reason it needs to be pulled instead of writing directly to the paged buffers is because they may be cloned (only the head of the skb is by default uncloned). 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 (the verifier has no way of differentiating whether the write is to the head or paged buffers during program load time). 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. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes skb dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            | 83 +++++++++++++++++-----------
 include/linux/filter.h         |  4 ++
 include/uapi/linux/bpf.h       | 40 ++++++++++++--
 kernel/bpf/helpers.c           | 81 +++++++++++++++++++++++++---
 kernel/bpf/verifier.c          | 99 ++++++++++++++++++++++++++++------
 net/core/filter.c              | 53 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h | 40 ++++++++++++--
 7 files changed, 335 insertions(+), 65 deletions(-)

[...]

Quoted 7 lines
kernel/bpf/helpers.c +1 -1
@@ -1521,9 +1532,19 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
        if (err)
                return err;
 
-       memcpy(dst, src->data + src->offset + offset, len);
+       type = bpf_dynptr_get_type(src);
 

kernel/bpf/helpers.c BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src · line 1545

Quoted 10 lines
kernel/bpf/helpers.c +9 -1
-       return 0;
+       switch (type) {
+       case BPF_DYNPTR_TYPE_LOCAL:
+       case BPF_DYNPTR_TYPE_RINGBUF:
+               memcpy(dst, src->data + src->offset + offset, len);
+               return 0;
+       case BPF_DYNPTR_TYPE_SKB:
+               return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
+       default:
+               WARN(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);

nit: probably better to WARN_ONCE?

Quoted 26 lines
kernel/bpf/helpers.c +7 -3
+               return -EFAULT;
+       }
 }
 
 static const struct bpf_func_proto bpf_dynptr_read_proto = {
@@ -1540,18 +1561,32 @@ static const struct bpf_func_proto bpf_dynptr_read_proto = {
 BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
           u32, len, u64, flags)
 {
+       enum bpf_dynptr_type type;
        int err;
 
-       if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
+       if (!dst->data || bpf_dynptr_is_rdonly(dst))
                return -EINVAL;
 
        err = bpf_dynptr_check_off_len(dst, offset, len);
        if (err)
                return err;
 
-       memcpy(dst->data + dst->offset + offset, src, len);
+       type = bpf_dynptr_get_type(dst);
 
-       return 0;
+       switch (type) {
+       case BPF_DYNPTR_TYPE_LOCAL:

kernel/bpf/helpers.c static const struct bpf_func_proto bpf_dynptr_read_proto = { · line 1587

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       case BPF_DYNPTR_TYPE_RINGBUF:
+               if (flags)
+                       return -EINVAL;
+               memcpy(dst->data + dst->offset + offset, src, len);
+               return 0;
+       case BPF_DYNPTR_TYPE_SKB:
+               return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,
+                                            flags);
+       default:
+               WARN(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);

ditto about WARN_ONCE

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
+               return -EFAULT;
+       }
 }
 
 static const struct bpf_func_proto bpf_dynptr_write_proto = {

[...]

Quoted 26 lines
kernel/bpf/verifier.c +9 -2
+static enum bpf_dynptr_type stack_slot_get_dynptr_info(struct bpf_verifier_env *env,
+                                                      struct bpf_reg_state *reg,
+                                                      int *ref_obj_id)
 {
        struct bpf_func_state *state = func(env, reg);
        int spi = get_spi(reg->off);
 
-       return state->stack[spi].spilled_ptr.id;
+       if (ref_obj_id)
+               *ref_obj_id = state->stack[spi].spilled_ptr.id;
+
+       return state->stack[spi].spilled_ptr.dynptr.type;
 }
 
 static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
@@ -6056,7 +6075,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                        case DYNPTR_TYPE_RINGBUF:
                                err_extra = "ringbuf ";
                                break;
-                       default:
+                       case DYNPTR_TYPE_SKB:
+                               err_extra = "skb ";
                                break;
                        }
 

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7172

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -7149,6 +7169,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 {
        enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
        const struct bpf_func_proto *fn = NULL;
+       enum bpf_dynptr_type dynptr_type;

compiler technically can complain about use of uninitialized dynptr_type, maybe initialize it to BPF_DYNPTR_TYPE_INVALID ?

Quoted 15 lines
kernel/bpf/verifier.c +0 -8
        enum bpf_return_type ret_type;
        enum bpf_type_flag ret_flag;
        struct bpf_reg_state *regs;
@@ -7320,24 +7341,43 @@ 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])) {
-                               if (meta.ref_obj_id) {
-                                       verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
-                                       return -EFAULT;
-                               }
-                               /* Find the id of the dynptr we're tracking the reference of */

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7350

Quoted 10 lines
kernel/bpf/verifier.c +7 -3
-                               meta.ref_obj_id = stack_slot_get_id(env, &regs[BPF_REG_1 + i]);
-                               break;
-                       }
+       case BPF_FUNC_dynptr_write:
+       {
+               struct bpf_reg_state *reg;
+
+               reg = get_dynptr_arg_reg(fn, regs);
+               if (!reg) {
+                       verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");

s/bpf_dynptr_data/bpf_dynptr_write/

Quoted 56 lines
kernel/bpf/verifier.c +33 -3
+                       return -EFAULT;
                }
-               if (i == MAX_BPF_FUNC_REG_ARGS) {
+
+               /* bpf_dynptr_write() for skb-type dynptrs may pull the skb, so we must
+                * invalidate all data slices associated with it
+                */
+               if (stack_slot_get_dynptr_info(env, reg, NULL) == BPF_DYNPTR_TYPE_SKB)
+                       changes_data = true;
+
+               break;
+       }
+       case BPF_FUNC_dynptr_data:
+       {
+               struct bpf_reg_state *reg;
+
+               reg = get_dynptr_arg_reg(fn, regs);
+               if (!reg) {
                        verbose(env, "verifier internal error: no dynptr in bpf_dynptr_data()\n");
                        return -EFAULT;
                }
+
+               if (meta.ref_obj_id) {
+                       verbose(env, "verifier internal error: meta.ref_obj_id already set\n");
+                       return -EFAULT;
+               }
+
+               dynptr_type = stack_slot_get_dynptr_info(env, reg, &meta.ref_obj_id);
                break;
        }
+       }
 
        if (err)
                return err;
@@ -7397,8 +7437,15 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
                break;
        case RET_PTR_TO_ALLOC_MEM:
                mark_reg_known_zero(env, regs, BPF_REG_0);
-               regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
-               regs[BPF_REG_0].mem_size = meta.mem_size;
+
+               if (func_id == BPF_FUNC_dynptr_data &&
+                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                       regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
+                       regs[BPF_REG_0].range = meta.mem_size;
+               } else {
+                       regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
+                       regs[BPF_REG_0].mem_size = meta.mem_size;
+               }
                break;
        case RET_PTR_TO_MEM_OR_BTF_ID:
        {
@@ -14141,6 +14188,24 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
                        goto patch_call_imm;
                }
 
Quoted 14 lines
Quoted 7 lines

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 14196

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+               if (insn->imm == BPF_FUNC_dynptr_from_skb) {
+                       bool is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
+
+                       insn_buf[0] = BPF_MOV32_IMM(BPF_REG_4, is_rdonly);
+                       insn_buf[1] = *insn;
+                       cnt = 2;
Quoted 7 lines

This might have been discussed before, sorry if I missed it. But why this special rewrite to provide read-only flag vs having two implementations of bpf_dynptr_from_skb() depending on program types? If program type allows read+write access, return bpf_dynptr_from_skb_rdwr(), for those that have read-only access - bpf_dynptr_from_skb_rdonly(), and for others return NULL proto (disable helper)?

Ooh I love this idea, thanks Andrii! I'll add this in v5.

Quoted 4 lines
Quoted 3 lines

You can then use it very similarly for bpf_dynptr_from_skb_meta().

Quoted 9 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

There's a prior discussion about this in v1 [0] as well.

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

#7 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

#8 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

On the whole, I think given that both Martin and you have specified your preferences for this, we should reject at load time instead of runtime. I'll make this change for v5 :)

#9 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 65 lines · Joanne Koong
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

Quoted 4 lines

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

What I'm trying to understand is how dynptr_data is supposed to work if this dynptr read only bit is only known at runtime. Or will it be always known statically so that it can set returned pointer as read only? Because then it doesn't seem it is required or useful to track the readonly bit at runtime.

It is fine if _everything_ checks it at runtime, but that doesn't seem possible, hence the question. We would need a new slice helper that only returns read-only slices, because dynptr_data can return rw slices currently and it is already UAPI so changing that is not possible anymore.

#10 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 65 lines · Joanne Koong
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

Quoted 4 lines

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

Quoted 5 lines

What I'm trying to understand is how dynptr_data is supposed to work if this dynptr read only bit is only known at runtime. Or will it be always known statically so that it can set returned pointer as read only? Because then it doesn't seem it is required or useful to track the readonly bit at runtime.

I think it'll always be known statically whether the dynptr is read-only or not. If we make all writable dynptr helper functions reject read-only dynptrs at load time instead of run time, then yes we can remove the read-only bit in the bpf_dynptr_kern struct.

There's also the question of whether this constraint (eg all read-only writes are rejected at load time) is too rigid - for example, what if in the future we want to add a helper function where if a certain condition is met, then we write some number of bytes, else we read some number of bytes? This would be not possible to add then, since we'll only know at runtime whether the condition is met.

I personally lean towards rejecting helper function writes at runtime, but if you think it's a non-trivial benefit to reject at load time instead, I'm fine going with that.

Quoted 6 lines

It is fine if _everything_ checks it at runtime, but that doesn't seem possible, hence the question. We would need a new slice helper that only returns read-only slices, because dynptr_data can return rw slices currently and it is already UAPI so changing that is not possible anymore.

I don't agree that if bpf_dynptr_write() is checked at runtime, then bpf_dynptr_data must also be checked at runtime to be consistent. I think it's fine if writes through helper functions are rejected at runtime, and writes through direct access are rejected at load time. That doesn't seem inconsistent to me.

#11 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 93 lines · Joanne Koong
Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 65 lines · Joanne Koong
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

Quoted 4 lines

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

Quoted 5 lines

What I'm trying to understand is how dynptr_data is supposed to work if this dynptr read only bit is only known at runtime. Or will it be always known statically so that it can set returned pointer as read only? Because then it doesn't seem it is required or useful to track the readonly bit at runtime.

I think it'll always be known statically whether the dynptr is read-only or not. If we make all writable dynptr helper functions reject read-only dynptrs at load time instead of run time, then yes we can remove the read-only bit in the bpf_dynptr_kern struct.

There's also the question of whether this constraint (eg all read-only writes are rejected at load time) is too rigid - for example, what if in the future we want to add a helper function where if a certain condition is met, then we write some number of bytes, else we read some number of bytes? This would be not possible to add then, since we'll only know at runtime whether the condition is met.

Quoted 4 lines

I personally lean towards rejecting helper function writes at runtime, but if you think it's a non-trivial benefit to reject at load time instead, I'm fine going with that.

My personal opinion is this:

When I am working with a statically known read only dynptr, it is like declaring a variable const. Every function expecting it to be non-const should fail compilation, and trying to mutate the variables through writes should also fail compilation. For BPF compilation is analogous to program load.

It might be that said variable is not const, then those operations may fail at runtime due to some other reason. Being dynamically read-only is then a runtime failure condition, which will cause failure at runtime. Both are distinct cases in my mind, and it is fine to fail at runtime when we don't know. In general, you save a lot of time of the user in my opinion (esp. people new to things) if you reject known incorrectness as early as possible.

E.g. load a dynptr from a map, where the field accepts storing both read only and non-read only ones. Then it is expected that writes may fail at runtime. That also allows you to switch read-only ness at runtime back to rw. But if the field only expects rdonly dynptr, verifier knows that the type is const statically, so it triggers failures for writes at load time instead.

Taking this a step further, you may even store rw dynptr to a map field expecting rdonly dynptr. That's like returning a const pointer from a function for a rw memory, where you want to limit access of the user, even better if you do it statically. Then functions trying to write to dynptr loaded from said map field will fail load itself, while others having access to rw dynptr can do it just fine.

When the verifier does not know, it does not know. There will be such cases when you make const-ness a runtime property.

Quoted 7 lines
Quoted 6 lines

It is fine if _everything_ checks it at runtime, but that doesn't seem possible, hence the question. We would need a new slice helper that only returns read-only slices, because dynptr_data can return rw slices currently and it is already UAPI so changing that is not possible anymore.

Quoted 5 lines

I don't agree that if bpf_dynptr_write() is checked at runtime, then bpf_dynptr_data must also be checked at runtime to be consistent. I think it's fine if writes through helper functions are rejected at runtime, and writes through direct access are rejected at load time. That doesn't seem inconsistent to me.

My point was more that dynptr_data cannot propagate runtime read-only-ness to its returned pointer. The verifier has to know statically, at which point I don't see why we can't just reject other cases at load anyway.

When we have dynptrs which have const-ness as a runtime property, it is ok to support that by failing at runtime (but then you'll have a hard time deciding how you want dynptr_data to work, most likely you'll need another helper which returns only a rdonly slice, when it fails, we call dynptr_data for rw slice).

But as I said before, I don't know how dynptr is going to evolve in the future, so you'll have a better idea, and I'll leave it up to you decide how you want to design its API. Enough words exchanged about this :).

#12 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 146 lines · Kumar Kartikeya Dwivedi
Quoted 93 lines · Joanne Koong
Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 65 lines · Joanne Koong
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

Quoted 4 lines

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

Quoted 5 lines

What I'm trying to understand is how dynptr_data is supposed to work if this dynptr read only bit is only known at runtime. Or will it be always known statically so that it can set returned pointer as read only? Because then it doesn't seem it is required or useful to track the readonly bit at runtime.

I think it'll always be known statically whether the dynptr is read-only or not. If we make all writable dynptr helper functions reject read-only dynptrs at load time instead of run time, then yes we can remove the read-only bit in the bpf_dynptr_kern struct.

There's also the question of whether this constraint (eg all read-only writes are rejected at load time) is too rigid - for example, what if in the future we want to add a helper function where if a certain condition is met, then we write some number of bytes, else we read some number of bytes? This would be not possible to add then, since we'll only know at runtime whether the condition is met.

Quoted 4 lines

I personally lean towards rejecting helper function writes at runtime, but if you think it's a non-trivial benefit to reject at load time instead, I'm fine going with that.

My personal opinion is this:

When I am working with a statically known read only dynptr, it is like declaring a variable const. Every function expecting it to be non-const should fail compilation, and trying to mutate the variables through writes should also fail compilation. For BPF compilation is analogous to program load.

It might be that said variable is not const, then those operations may fail at runtime due to some other reason. Being dynamically read-only is then a runtime failure condition, which will cause failure at runtime. Both are distinct cases in my mind, and it is fine to fail at runtime when we don't know. In general, you save a lot of time of the user in my opinion (esp. people new to things) if you reject known incorrectness as early as possible.

E.g. load a dynptr from a map, where the field accepts storing both read only and non-read only ones. Then it is expected that writes may fail at runtime. That also allows you to switch read-only ness at runtime back to rw. But if the field only expects rdonly dynptr, verifier knows that the type is const statically, so it triggers failures for writes at load time instead.

Taking this a step further, you may even store rw dynptr to a map field expecting rdonly dynptr. That's like returning a const pointer from a function for a rw memory, where you want to limit access of the user, even better if you do it statically. Then functions trying to write to dynptr loaded from said map field will fail load itself, while others having access to rw dynptr can do it just fine.

When the verifier does not know, it does not know. There will be such cases when you make const-ness a runtime property.

Quoted 7 lines
Quoted 6 lines

It is fine if _everything_ checks it at runtime, but that doesn't seem possible, hence the question. We would need a new slice helper that only returns read-only slices, because dynptr_data can return rw slices currently and it is already UAPI so changing that is not possible anymore.

Quoted 5 lines

I don't agree that if bpf_dynptr_write() is checked at runtime, then bpf_dynptr_data must also be checked at runtime to be consistent. I think it's fine if writes through helper functions are rejected at runtime, and writes through direct access are rejected at load time. That doesn't seem inconsistent to me.

Quoted 4 lines

My point was more that dynptr_data cannot propagate runtime read-only-ness to its returned pointer. The verifier has to know statically, at which point I don't see why we can't just reject other cases at load anyway.

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

It's only slightly tangential, but I still dread my experience proving to BPF verifier that some value is strictly greater than zero for BPF helper that expected ARG_CONST_SIZE (not ARG_CONST_SIZE_OR_ZERO). There were also cases were absolutely correct program had to be mangled just to prove to BPF verifier that it indeed can return just 0 or 1, etc. This is not to bash BPF verifier, but just to point out that sometimes unnecessary strictness adds nothing but unnecessary pain to user. So, let's not reject anything at load, we can check all that at runtime and return NULL.

But bpf_dynptr_data_rdonly() seems useful for cases where we know we are not going to write

Quoted 11 lines

When we have dynptrs which have const-ness as a runtime property, it is ok to support that by failing at runtime (but then you'll have a hard time deciding how you want dynptr_data to work, most likely you'll need another helper which returns only a rdonly slice, when it fails, we call dynptr_data for rw slice).

But as I said before, I don't know how dynptr is going to evolve in the future, so you'll have a better idea, and I'll leave it up to you decide how you want to design its API. Enough words exchanged about this :).

directionally, dynptr is about offloading decisions to runtime, so I think avoiding unnecessary restrictions at verification time is the right trade off

#13 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 154 lines · Andrii Nakryiko
Quoted 146 lines · Kumar Kartikeya Dwivedi
Quoted 93 lines · Joanne Koong
Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 65 lines · Joanne Koong
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

Quoted 4 lines

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

Quoted 5 lines

What I'm trying to understand is how dynptr_data is supposed to work if this dynptr read only bit is only known at runtime. Or will it be always known statically so that it can set returned pointer as read only? Because then it doesn't seem it is required or useful to track the readonly bit at runtime.

I think it'll always be known statically whether the dynptr is read-only or not. If we make all writable dynptr helper functions reject read-only dynptrs at load time instead of run time, then yes we can remove the read-only bit in the bpf_dynptr_kern struct.

There's also the question of whether this constraint (eg all read-only writes are rejected at load time) is too rigid - for example, what if in the future we want to add a helper function where if a certain condition is met, then we write some number of bytes, else we read some number of bytes? This would be not possible to add then, since we'll only know at runtime whether the condition is met.

Quoted 4 lines

I personally lean towards rejecting helper function writes at runtime, but if you think it's a non-trivial benefit to reject at load time instead, I'm fine going with that.

My personal opinion is this:

When I am working with a statically known read only dynptr, it is like declaring a variable const. Every function expecting it to be non-const should fail compilation, and trying to mutate the variables through writes should also fail compilation. For BPF compilation is analogous to program load.

It might be that said variable is not const, then those operations may fail at runtime due to some other reason. Being dynamically read-only is then a runtime failure condition, which will cause failure at runtime. Both are distinct cases in my mind, and it is fine to fail at runtime when we don't know. In general, you save a lot of time of the user in my opinion (esp. people new to things) if you reject known incorrectness as early as possible.

E.g. load a dynptr from a map, where the field accepts storing both read only and non-read only ones. Then it is expected that writes may fail at runtime. That also allows you to switch read-only ness at runtime back to rw. But if the field only expects rdonly dynptr, verifier knows that the type is const statically, so it triggers failures for writes at load time instead.

Taking this a step further, you may even store rw dynptr to a map field expecting rdonly dynptr. That's like returning a const pointer from a function for a rw memory, where you want to limit access of the user, even better if you do it statically. Then functions trying to write to dynptr loaded from said map field will fail load itself, while others having access to rw dynptr can do it just fine.

When the verifier does not know, it does not know. There will be such cases when you make const-ness a runtime property.

Quoted 7 lines
Quoted 6 lines

It is fine if _everything_ checks it at runtime, but that doesn't seem possible, hence the question. We would need a new slice helper that only returns read-only slices, because dynptr_data can return rw slices currently and it is already UAPI so changing that is not possible anymore.

Quoted 5 lines

I don't agree that if bpf_dynptr_write() is checked at runtime, then bpf_dynptr_data must also be checked at runtime to be consistent. I think it's fine if writes through helper functions are rejected at runtime, and writes through direct access are rejected at load time. That doesn't seem inconsistent to me.

Quoted 4 lines

My point was more that dynptr_data cannot propagate runtime read-only-ness to its returned pointer. The verifier has to know statically, at which point I don't see why we can't just reject other cases at load anyway.

Quoted 5 lines

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

Shouldn't it be the other way around? bpf_dynptr_data_rdonly() should work for both ro and rw dynptrs, and bpf_dynptr_data() only for rw dynptr?

And yes, you're kind of painting yourself in a corner if you allow dynptr_data to work with statically ro dynptrs now, ascertaining the ro bit for the returned slice, and then later you plan to add dynptrs whose ro vs rw is not known to the verifier statically. Then it works well for statically known ones (returning both ro and rw slices), but has to return NULL at runtime for statically unknown read only ones, and always rw slice in verifier state for them.

Quoted 6 lines

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

Right, that might be too restrictive if we require them to be statically read only.

But it's not about forcing it to be statically ro, it is more about rejecting load when we know the program is incorrect (e.g. the types are incorrect when passed to helpers), otherwise we throw the error at runtime anyway, which seems to be the convention afaicu. But maybe I missed the memo and we gradually want to move away from such strict static checks.

I view the situation here similar to if we were rejecting direct writes to PTR_TO_MEM | MEM_RDONLY at load time, but offloading as runtime check in the helper writing to it as rw memory arg. It's as if we pretend it's part of the 'type' of the register when doing direct writes, but then ignore it while matching it against the said helper's argument type.

Quoted 20 lines

It's only slightly tangential, but I still dread my experience proving to BPF verifier that some value is strictly greater than zero for BPF helper that expected ARG_CONST_SIZE (not ARG_CONST_SIZE_OR_ZERO). There were also cases were absolutely correct program had to be mangled just to prove to BPF verifier that it indeed can return just 0 or 1, etc. This is not to bash BPF verifier, but just to point out that sometimes unnecessary strictness adds nothing but unnecessary pain to user. So, let's not reject anything at load, we can check all that at runtime and return NULL.

But bpf_dynptr_data_rdonly() seems useful for cases where we know we are not going to write

Quoted 7 lines

When we have dynptrs which have const-ness as a runtime property, it is ok to support that by failing at runtime (but then you'll have a hard time deciding how you want dynptr_data to work, most likely you'll need another helper which returns only a rdonly slice, when it fails, we call dynptr_data for rw slice).

Quoted 8 lines
Quoted 4 lines

But as I said before, I don't know how dynptr is going to evolve in the future, so you'll have a better idea, and I'll leave it up to you decide how you want to design its API. Enough words exchanged about this :).

directionally, dynptr is about offloading decisions to runtime, so I think avoiding unnecessary restrictions at verification time is the right trade off

Fair enough, I guess I've made my point. Let's go with what you both feel would be best.

#14 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 162 lines · Kumar Kartikeya Dwivedi
Quoted 154 lines · Andrii Nakryiko
Quoted 146 lines · Kumar Kartikeya Dwivedi
Quoted 93 lines · Joanne Koong
Quoted 72 lines · Kumar Kartikeya Dwivedi
Quoted 65 lines · Joanne Koong
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Joanne Koong

[...]

Quoted 10 lines

Related question, it seems we know statically if dynptr is read only or not, so why even do all this hidden parameter passing and instead just reject writes directly? You only need to be able to set MEM_RDONLY on dynptr_data returned PTR_TO_PACKETs, and reject dynptr_write when dynptr type is xdp/skb (and ctx is only one). That seems simpler than checking it at runtime. Verifier already handles MEM_RDONLY generically, you only need to add the guard for check_packet_acces (and check_helper_mem_access for meta->raw_mode under pkt case), and rejecting dynptr_write seems like a if condition.

Quoted 6 lines

There will be other helper functions that do writes (eg memcpy to dynptrs, strncpy to dynptrs, probe read user to dynptrs, hashing dynptrs, ...) so it's more scalable if we reject these at runtime rather than enforce these at the verifier level. I also think it's cleaner to keep the verifier logic as simple as possible and do the checking in the helper.

I won't be pushing this further, since you know what you plan to add in the future better, but I still disagree.

I'm guessing there might be dynptrs where this read only property is set dynamically at runtime, which is why you want to go this route? I.e. you might not know statically whether dynptr is read only or not?

My main confusion is the inconsistency here.

Right now the patch implicitly relies on may_access_direct_pkt_data to protect slices returned from dynptr_data, instead of setting MEM_RDONLY on the returned PTR_TO_PACKET. Which is fine, it's not needed. So indirectly, you are relying on knowing statically whether the dynptr is read only or not. But then you also set this bit at runtime.

So you reject some cases at load time, and the rest of them only at runtime. Direct writes to dynptr slice fails load, writes through helper does not (only fails at runtime).

Also, dynptr_data needs to know whether dynptr is read only statically, to protect writes to its returned pointer, unless you decide to introduce another helper for the dynamic rdonly bit case (like dynptr_data_rdonly). Then you have a mismatch, where dynptr_data works for some rdonly dynptrs (known to be rdonly statically, like this skb one), but not for others.

Quoted 4 lines

I also don't agree about the complexity or scalability part, all the infra and precedence is already there. We already have similar checks for meta->raw_mode where we reject writes to read only pointers in check_helper_mem_access.

My point about scalability is that if we reject bpf_dynptr_write() at load time, then we must reject any future dynptr helper that does any writing at load time as well, to be consistent.

I don't feel strongly about whether we reject at load time or run time. Rejecting at load time instead of runtime doesn't seem that useful to me, but there's a good chance I'm wrong here since Martin stated that he prefers rejecting at load time as well.

Quoted 4 lines

As for the added complexity part, what I mean is that we'll need to keep track of some more stuff to support this, such as whether the dynptr is read only and which helper functions need to check whether the dynptr is read only or not.

Quoted 5 lines

What I'm trying to understand is how dynptr_data is supposed to work if this dynptr read only bit is only known at runtime. Or will it be always known statically so that it can set returned pointer as read only? Because then it doesn't seem it is required or useful to track the readonly bit at runtime.

I think it'll always be known statically whether the dynptr is read-only or not. If we make all writable dynptr helper functions reject read-only dynptrs at load time instead of run time, then yes we can remove the read-only bit in the bpf_dynptr_kern struct.

There's also the question of whether this constraint (eg all read-only writes are rejected at load time) is too rigid - for example, what if in the future we want to add a helper function where if a certain condition is met, then we write some number of bytes, else we read some number of bytes? This would be not possible to add then, since we'll only know at runtime whether the condition is met.

Quoted 4 lines

I personally lean towards rejecting helper function writes at runtime, but if you think it's a non-trivial benefit to reject at load time instead, I'm fine going with that.

My personal opinion is this:

When I am working with a statically known read only dynptr, it is like declaring a variable const. Every function expecting it to be non-const should fail compilation, and trying to mutate the variables through writes should also fail compilation. For BPF compilation is analogous to program load.

It might be that said variable is not const, then those operations may fail at runtime due to some other reason. Being dynamically read-only is then a runtime failure condition, which will cause failure at runtime. Both are distinct cases in my mind, and it is fine to fail at runtime when we don't know. In general, you save a lot of time of the user in my opinion (esp. people new to things) if you reject known incorrectness as early as possible.

E.g. load a dynptr from a map, where the field accepts storing both read only and non-read only ones. Then it is expected that writes may fail at runtime. That also allows you to switch read-only ness at runtime back to rw. But if the field only expects rdonly dynptr, verifier knows that the type is const statically, so it triggers failures for writes at load time instead.

Taking this a step further, you may even store rw dynptr to a map field expecting rdonly dynptr. That's like returning a const pointer from a function for a rw memory, where you want to limit access of the user, even better if you do it statically. Then functions trying to write to dynptr loaded from said map field will fail load itself, while others having access to rw dynptr can do it just fine.

When the verifier does not know, it does not know. There will be such cases when you make const-ness a runtime property.

Quoted 7 lines
Quoted 6 lines

It is fine if _everything_ checks it at runtime, but that doesn't seem possible, hence the question. We would need a new slice helper that only returns read-only slices, because dynptr_data can return rw slices currently and it is already UAPI so changing that is not possible anymore.

Quoted 5 lines

I don't agree that if bpf_dynptr_write() is checked at runtime, then bpf_dynptr_data must also be checked at runtime to be consistent. I think it's fine if writes through helper functions are rejected at runtime, and writes through direct access are rejected at load time. That doesn't seem inconsistent to me.

Quoted 4 lines

My point was more that dynptr_data cannot propagate runtime read-only-ness to its returned pointer. The verifier has to know statically, at which point I don't see why we can't just reject other cases at load anyway.

Quoted 5 lines

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

Quoted 3 lines

Shouldn't it be the other way around? bpf_dynptr_data_rdonly() should work for both ro and rw dynptrs, and bpf_dynptr_data() only for rw dynptr?

Right, that's what I proposed:

"bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr"

so if you pass read-write dynptr, it will return NULL (because it's unsafe to take writable direct pointer).

bpf_dynptr_data_rdonly() should still work fine with both rdonly and read-write dynptr.
bpf_dynptr_data() only works (in the sense returns non-NULL) for read-write dynptr only.

Quoted 8 lines

And yes, you're kind of painting yourself in a corner if you allow dynptr_data to work with statically ro dynptrs now, ascertaining the ro bit for the returned slice, and then later you plan to add dynptrs whose ro vs rw is not known to the verifier statically. Then it works well for statically known ones (returning both ro and rw slices), but has to return NULL at runtime for statically unknown read only ones, and always rw slice in verifier state for them.

Right, will be both inconsistent and puzzling.

Quoted 18 lines
Quoted 7 lines

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

Right, that might be too restrictive if we require them to be statically read only.

But it's not about forcing it to be statically ro, it is more about rejecting load when we know the program is incorrect (e.g. the types are incorrect when passed to helpers), otherwise we throw the error at runtime anyway, which seems to be the convention afaicu. But maybe I missed the memo and we gradually want to move away from such strict static checks.

Quoted 6 lines

I view the situation here similar to if we were rejecting direct writes to PTR_TO_MEM | MEM_RDONLY at load time, but offloading as runtime check in the helper writing to it as rw memory arg. It's as if we pretend it's part of the 'type' of the register when doing direct writes, but then ignore it while matching it against the said helper's argument type.

I disagree, it's not the same. bpf_dynptr_data/bpf_dynptr_data_rdonly turns completely dynamic dynptr into static slice of memory. Only after that point it makes sense for BPF verifier to reject something. Until then it's not incorrect. BPF program will always have to deal with some dynptr operations potentially failing. dynptr can always be NULL internally, you can still call bpf_dynptr_xxx() operations on it, they will just do nothing and return error. That doesn't make BPF program incorrect.

As I said, dynptr is about shifting static verification to runtime checks for stuff that BPF verifier can't or shouldn't verify statically. Like dynptr's dynamic size, for example. We've used a special data/data_end pointer types to try to solve this for skb, but it is quite painful in practice and relies on compiler generating "good" sequence of instructions understandable to BPF verifier.

dynptr takes a different approach, shifts validation to runtime and "interfaces" with static verification through bpf_dynptr_data() and similar APIs.

Quoted 26 lines
Quoted 26 lines

It's only slightly tangential, but I still dread my experience proving to BPF verifier that some value is strictly greater than zero for BPF helper that expected ARG_CONST_SIZE (not ARG_CONST_SIZE_OR_ZERO). There were also cases were absolutely correct program had to be mangled just to prove to BPF verifier that it indeed can return just 0 or 1, etc. This is not to bash BPF verifier, but just to point out that sometimes unnecessary strictness adds nothing but unnecessary pain to user. So, let's not reject anything at load, we can check all that at runtime and return NULL.

But bpf_dynptr_data_rdonly() seems useful for cases where we know we are not going to write

Quoted 11 lines

When we have dynptrs which have const-ness as a runtime property, it is ok to support that by failing at runtime (but then you'll have a hard time deciding how you want dynptr_data to work, most likely you'll need another helper which returns only a rdonly slice, when it fails, we call dynptr_data for rw slice).

But as I said before, I don't know how dynptr is going to evolve in the future, so you'll have a better idea, and I'll leave it up to you decide how you want to design its API. Enough words exchanged about this :).

Quoted 6 lines
Quoted 3 lines

directionally, dynptr is about offloading decisions to runtime, so I think avoiding unnecessary restrictions at verification time is the right trade off

Fair enough, I guess I've made my point. Let's go with what you both feel would be best.

Sounds good. I tried to point out the "philosophy" behind dynptr. It doesn't preclude making the BPF verifier smarter and track more things. But it's a deliberate design of dynptr to shift more checks to runtime because in a lot of cases it makes more sense than "fighting BPF verifier". This "fighting verifier" phrase is like a meme now. I've heard this exact phrase from multiple unrelated engineers. BPF verifier should be helpful, we should engineers having to "fight" it, so overly strict checks from verifier should be avoided if they don't compromise correctness, IMO.

#15 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 64 lines · Andrii Nakryiko
Quoted 11 lines · Kumar Kartikeya Dwivedi

[...]

Quoted 6 lines

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

Shouldn't it be the other way around? bpf_dynptr_data_rdonly() should work for both ro and rw dynptrs, and bpf_dynptr_data() only for rw dynptr?

Right, that's what I proposed:

"bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr"

so if you pass read-write dynptr, it will return NULL (because it's unsafe to take writable direct pointer).

bpf_dynptr_data_rdonly() should still work fine with both rdonly and read-write dynptr.
bpf_dynptr_data() only works (in the sense returns non-NULL) for read-write dynptr only.

Quoted 8 lines

And yes, you're kind of painting yourself in a corner if you allow dynptr_data to work with statically ro dynptrs now, ascertaining the ro bit for the returned slice, and then later you plan to add dynptrs whose ro vs rw is not known to the verifier statically. Then it works well for statically known ones (returning both ro and rw slices), but has to return NULL at runtime for statically unknown read only ones, and always rw slice in verifier state for them.

Right, will be both inconsistent and puzzling.

Quoted 18 lines
Quoted 7 lines

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

Right, that might be too restrictive if we require them to be statically read only.

But it's not about forcing it to be statically ro, it is more about rejecting load when we know the program is incorrect (e.g. the types are incorrect when passed to helpers), otherwise we throw the error at runtime anyway, which seems to be the convention afaicu. But maybe I missed the memo and we gradually want to move away from such strict static checks.

Quoted 6 lines

I view the situation here similar to if we were rejecting direct writes to PTR_TO_MEM | MEM_RDONLY at load time, but offloading as runtime check in the helper writing to it as rw memory arg. It's as if we pretend it's part of the 'type' of the register when doing direct writes, but then ignore it while matching it against the said helper's argument type.

Quoted 8 lines

I disagree, it's not the same. bpf_dynptr_data/bpf_dynptr_data_rdonly turns completely dynamic dynptr into static slice of memory. Only after that point it makes sense for BPF verifier to reject something. Until then it's not incorrect. BPF program will always have to deal with some dynptr operations potentially failing. dynptr can always be NULL internally, you can still call bpf_dynptr_xxx() operations on it, they will just do nothing and return error. That doesn't make BPF program incorrect.

Let me just explain one last time why I'm unable to swallow this 'completely dynamic dynptr' explanation, because it is not treated as completely dynamic by all dynptr helpers.

No pushback, but it would be great if you could further help me wrap my head around this, so that we're in sync for future discussions.

So you say you may not know the type of dynptr (read-only, rw, local, ringbuf, etc.). Hence you want to treat every dynptr as 'dynamic dynptr' you know nothing about even when you do know some information about it statically (e.g. if it's on stack). You don't want to reject things early at load even if you have all the info to do so. You want operations on it to fail at runtime instead.

If you cannot track ro vs rw in the future statically, you won't be be able to track local or ringbuf or skb either (since both are part of the type of the dynptr). If you can, you can just as well encode const-ness as part of the type where you declare it (e.g. in a map field where the value is assigned dynamically, where you say dynptr_ringbuf etc.). Type comprises local vs skb vs ringbuf | ro vs rw for me. But maybe I could be wrong.

So following this line of reasoning, will you be relaxing the argument type of helpers like bpf_ringbuf_submit_dynptr? Right now they take 'dynamic dynptr' as arg, but also expect DYNPTR_TYPE_RINGBUF, so you reject load when it's not a ringbuf dynptr. Will it then fallback to checking the type at runtime when the type will not be known? But then it will also permit passing local or skb dynptr in the future which it rejects right now.

I'm just hoping you are able to see why it's looking a bit inconsistent to me. If DYNPTR_TYPE_RINGBUF has to be checked, it felt to me like DYNPTR_RDONLY is as much part of that kind of type safety wrt helpers. It would be set on the dynptr when skb passed to dynptr_from_skb is rdonly in some program types, along with DYNPTR_TYPE_SKB, and expect to be matched when invoking helpers on such dynptrs.

It is fine to push checks to runtime, especially when you won't know the type, because verifier cannot reasonably track the dynptr type then.

But right now there's still some level of state you maintain in the verifier about dynptrs (like it's type), and it seems to me like some helpers are using that state to reject things at load time, while some other helper will ignore it and fallback to runtime checks.

I hope this is a clear enough description to atleast justify why I'm (still) a bit confused.

#16 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 113 lines · Kumar Kartikeya Dwivedi
Quoted 64 lines · Andrii Nakryiko
Quoted 11 lines · Kumar Kartikeya Dwivedi

[...]

Quoted 6 lines

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

Shouldn't it be the other way around? bpf_dynptr_data_rdonly() should work for both ro and rw dynptrs, and bpf_dynptr_data() only for rw dynptr?

Right, that's what I proposed:

"bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr"

so if you pass read-write dynptr, it will return NULL (because it's unsafe to take writable direct pointer).

bpf_dynptr_data_rdonly() should still work fine with both rdonly and read-write dynptr.
bpf_dynptr_data() only works (in the sense returns non-NULL) for read-write dynptr only.

Quoted 8 lines

And yes, you're kind of painting yourself in a corner if you allow dynptr_data to work with statically ro dynptrs now, ascertaining the ro bit for the returned slice, and then later you plan to add dynptrs whose ro vs rw is not known to the verifier statically. Then it works well for statically known ones (returning both ro and rw slices), but has to return NULL at runtime for statically unknown read only ones, and always rw slice in verifier state for them.

Right, will be both inconsistent and puzzling.

Quoted 18 lines
Quoted 7 lines

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

Right, that might be too restrictive if we require them to be statically read only.

But it's not about forcing it to be statically ro, it is more about rejecting load when we know the program is incorrect (e.g. the types are incorrect when passed to helpers), otherwise we throw the error at runtime anyway, which seems to be the convention afaicu. But maybe I missed the memo and we gradually want to move away from such strict static checks.

Quoted 6 lines

I view the situation here similar to if we were rejecting direct writes to PTR_TO_MEM | MEM_RDONLY at load time, but offloading as runtime check in the helper writing to it as rw memory arg. It's as if we pretend it's part of the 'type' of the register when doing direct writes, but then ignore it while matching it against the said helper's argument type.

Quoted 8 lines

I disagree, it's not the same. bpf_dynptr_data/bpf_dynptr_data_rdonly turns completely dynamic dynptr into static slice of memory. Only after that point it makes sense for BPF verifier to reject something. Until then it's not incorrect. BPF program will always have to deal with some dynptr operations potentially failing. dynptr can always be NULL internally, you can still call bpf_dynptr_xxx() operations on it, they will just do nothing and return error. That doesn't make BPF program incorrect.

Let me just explain one last time why I'm unable to swallow this 'completely dynamic dynptr' explanation, because it is not treated as completely dynamic by all dynptr helpers.

No pushback, but it would be great if you could further help me wrap my head around this, so that we're in sync for future discussions.

So you say you may not know the type of dynptr (read-only, rw, local, ringbuf, etc.). Hence you want to treat every dynptr as 'dynamic dynptr' you know nothing about even when you do know some information about it statically (e.g. if it's on stack). You don't want to reject things early at load even if you have all the info to do so. You want operations on it to fail at runtime instead.

If you cannot track ro vs rw in the future statically, you won't be be able to track local or ringbuf or skb either (since both are part of the type of the dynptr). If you can, you can just as well encode const-ness as part of the type where you declare it (e.g. in a map field where the value is assigned dynamically, where you say dynptr_ringbuf etc.). Type comprises local vs skb vs ringbuf | ro vs rw for me. But maybe I could be wrong.

So following this line of reasoning, will you be relaxing the argument type of helpers like bpf_ringbuf_submit_dynptr? Right now they take 'dynamic dynptr' as arg, but also expect DYNPTR_TYPE_RINGBUF, so you reject load when it's not a ringbuf dynptr. Will it then fallback to checking the type at runtime when the type will not be known? But then it will also permit passing local or skb dynptr in the future which it rejects right now.

I'm just hoping you are able to see why it's looking a bit inconsistent to me. If DYNPTR_TYPE_RINGBUF has to be checked, it felt to me like DYNPTR_RDONLY is as much part of that kind of type safety wrt helpers. It would be set on the dynptr when skb passed to dynptr_from_skb is rdonly in some program types, along with DYNPTR_TYPE_SKB, and expect to be matched when invoking helpers on such dynptrs.

Quoted 4 lines

It is fine to push checks to runtime, especially when you won't know the type, because verifier cannot reasonably track the dynptr type then.

To further expand on this point, in my mind when you have actual 'completely dynamic dynptrs', you will most likely track them as DYNPTR_TYPE_UNKNOWN in the verifier. Then you would capture free bits to encode the types at runtime, and start checking that in helpers.

All helpers will start taking such DYNPTR_TYPE_UNKNOWN, or you can have a casting helper like we already have for normal pointers. No need for extra verifier complexity to teach it to recognize type when it is unknown. It will then be checked dynamically at runtime for dynptr operations.

Being strictly type safe by default for helper arguments is not going to lead you to 'fighting the verifier'. Very much the opposite, the verifier is helping you catch errors that will rather occur at runtime anyway.

Quoted 7 lines

But right now there's still some level of state you maintain in the verifier about dynptrs (like it's type), and it seems to me like some helpers are using that state to reject things at load time, while some other helper will ignore it and fallback to runtime checks.

I hope this is a clear enough description to atleast justify why I'm (still) a bit confused.

#17 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

In reply to Kumar Kartikeya Dwivedi
Quoted 79 lines · Kumar Kartikeya Dwivedi
Quoted 64 lines · Andrii Nakryiko
Quoted 11 lines · Kumar Kartikeya Dwivedi

[...]

Quoted 6 lines

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

Shouldn't it be the other way around? bpf_dynptr_data_rdonly() should work for both ro and rw dynptrs, and bpf_dynptr_data() only for rw dynptr?

Right, that's what I proposed:

"bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr"

so if you pass read-write dynptr, it will return NULL (because it's unsafe to take writable direct pointer).

bpf_dynptr_data_rdonly() should still work fine with both rdonly and read-write dynptr.
bpf_dynptr_data() only works (in the sense returns non-NULL) for read-write dynptr only.

Quoted 8 lines

And yes, you're kind of painting yourself in a corner if you allow dynptr_data to work with statically ro dynptrs now, ascertaining the ro bit for the returned slice, and then later you plan to add dynptrs whose ro vs rw is not known to the verifier statically. Then it works well for statically known ones (returning both ro and rw slices), but has to return NULL at runtime for statically unknown read only ones, and always rw slice in verifier state for them.

Right, will be both inconsistent and puzzling.

Quoted 18 lines
Quoted 7 lines

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

Right, that might be too restrictive if we require them to be statically read only.

But it's not about forcing it to be statically ro, it is more about rejecting load when we know the program is incorrect (e.g. the types are incorrect when passed to helpers), otherwise we throw the error at runtime anyway, which seems to be the convention afaicu. But maybe I missed the memo and we gradually want to move away from such strict static checks.

Quoted 6 lines

I view the situation here similar to if we were rejecting direct writes to PTR_TO_MEM | MEM_RDONLY at load time, but offloading as runtime check in the helper writing to it as rw memory arg. It's as if we pretend it's part of the 'type' of the register when doing direct writes, but then ignore it while matching it against the said helper's argument type.

Quoted 8 lines

I disagree, it's not the same. bpf_dynptr_data/bpf_dynptr_data_rdonly turns completely dynamic dynptr into static slice of memory. Only after that point it makes sense for BPF verifier to reject something. Until then it's not incorrect. BPF program will always have to deal with some dynptr operations potentially failing. dynptr can always be NULL internally, you can still call bpf_dynptr_xxx() operations on it, they will just do nothing and return error. That doesn't make BPF program incorrect.

Let me just explain one last time why I'm unable to swallow this 'completely dynamic dynptr' explanation, because it is not treated as completely dynamic by all dynptr helpers.

Quoted 5 lines

No pushback, but it would be great if you could further help me wrap my head around this, so that we're in sync for future discussions.

So you say you may not know the type of dynptr (read-only, rw, local, ringbuf, etc.). Hence you want to treat every dynptr as 'dynamic

No, that's not what I said. I didn't talk about dynptr type (ringbuf vs skb vs local). Type we have to track statically precisely to limit what kind of helpers can be used on dynptrs of specific type.

But note that there are helpers that don't care about dynptr and, in theory, should work with any dynptr. Because dynptr is an abstraction of "logically continuous range of memory". So in some cases even types don't matter.

But regardless, I just don't think about read-only bit as part of dynptr types. I don't see why it has to be statically known and even why it has to stay as read-only or read-write for the entire duration of dynptr lifetime. Why can't we allow flipping dynptr from read-write to read-only before passing it to some custom BPF subprog, potentially provided from some BPF library which you don't control; just to make sure that it cannot really modify underlying memory (even if it is fundamentally modifiable, like with ringbuf)?

So the point I'm trying to communicate is that things that don't **need** to be knowable statically to BPF verifier - **should not** be knowable and should be checked at runtime only. With any dynptr helper that is interfacing its runtime nature into static thing that verifier enforces (which is what bpf_dynptr_data/bpf_dynptr_data_rdonly are), it will always be "fallible" and users will have to expect that they might return NULL or do nothing, or whatever way to deal with failure case.

And I believe dynptr read-onliness is not something that BPF verifier should be tracking statically. PTR_TO_MEM -- yes, dynptr -- no. That's it. Dynptr type/kind -- yes, track statically, it's way more important to determine what you can at all do with dynptr.

Quoted 5 lines

dynptr' you know nothing about even when you do know some information about it statically (e.g. if it's on stack). You don't want to reject things early at load even if you have all the info to do so. You want operations on it to fail at runtime instead.

Quoted 7 lines

If you cannot track ro vs rw in the future statically, you won't be be able to track local or ringbuf or skb either (since both are part of the type of the dynptr). If you can, you can just as well encode const-ness as part of the type where you declare it (e.g. in a map field where the value is assigned dynamically, where you say dynptr_ringbuf etc.). Type comprises local vs skb vs ringbuf | ro vs rw for me. But maybe I could be wrong.

I don't consider read-only to be a part of type. It's more like offset and size to me, rather than type. And even if we can track constness (e.g., we can teach BPF verifier to recognize that hypothetical bpf_dynptr_set_read_only() makes dynptr into dynptr_rdonly, but why?)

Quoted 8 lines

So following this line of reasoning, will you be relaxing the argument type of helpers like bpf_ringbuf_submit_dynptr? Right now they take 'dynamic dynptr' as arg, but also expect DYNPTR_TYPE_RINGBUF, so you reject load when it's not a ringbuf dynptr. Will it then fallback to checking the type at runtime when the type will not be known? But then it will also permit passing local or skb dynptr in the future which it rejects right now.

No, which is exactly why we track dynptr type statically. Because some operations don't make sense and they have additional semantics (like with ringbuf submit).

Quoted 8 lines

I'm just hoping you are able to see why it's looking a bit inconsistent to me. If DYNPTR_TYPE_RINGBUF has to be checked, it felt to me like DYNPTR_RDONLY is as much part of that kind of type safety wrt helpers. It would be set on the dynptr when skb passed to dynptr_from_skb is rdonly in some program types, along with DYNPTR_TYPE_SKB, and expect to be matched when invoking helpers on such dynptrs.

I understand your view point. But you are taking "completely dynamic" nature of dynptr too far (not tracking dynptr type) in one case, or making it too restrictive (tracking read-only state) in another. We can argue what substitutes "inconsistent", but I'm just leaning towards slightly different tradeoffs. I don't see a huge value in preventing bpf_dynptr_write() to be called on read-only dynptr **statically**. I've ran into various cases where I'd rather BPF verifier not pretend to be all-knowing and too helpful, because it sometimes turns into a game of proving to BPF verifier that "I know what I'm doing, and it's correct, so please just let me do it".

Quoted 9 lines

It is fine to push checks to runtime, especially when you won't know the type, because verifier cannot reasonably track the dynptr type then.

But right now there's still some level of state you maintain in the verifier about dynptrs (like it's type), and it seems to me like some helpers are using that state to reject things at load time, while some other helper will ignore it and fallback to runtime checks.

Exactly. There are helpers that can fail no matter what, and read-onliness is just another condition they will have to check and reject.

And some helpers really don't and shouldn't care about the nature of dynptr (like bpf_dynptr_get_size(), for example).

Quoted 3 lines

I hope this is a clear enough description to atleast justify why I'm (still) a bit confused.

I don't think you are confused, you just prefer a different tradeoff. Hopefully I gave a few more arguments in favor of less static treatment of dynptr where it doesn't hurt correctness.

P.S. I'm going away on vacation, so unlikely to be able to continue this discussion in a timely manner going forward.

#18 lore raw

Re: [PATCH bpf-next v4 1/3] bpf: Add skb dynptrs

Quoted 116 lines · Andrii Nakryiko
Quoted 79 lines · Kumar Kartikeya Dwivedi
Quoted 64 lines · Andrii Nakryiko
Quoted 11 lines · Kumar Kartikeya Dwivedi

[...]

Quoted 6 lines

I think the right answer here is to not make bpf_dynptr_data() return direct pointer of changing read-only-ness. Maybe the right answer here is another helper, bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr and PTR_TO_MEM | MEM_RDONLY if dynptr is indeed read-only?

Shouldn't it be the other way around? bpf_dynptr_data_rdonly() should work for both ro and rw dynptrs, and bpf_dynptr_data() only for rw dynptr?

Right, that's what I proposed:

"bpf_dynptr_data_rdonly(), that will return NULL for non-read-only dynptr"

so if you pass read-write dynptr, it will return NULL (because it's unsafe to take writable direct pointer).

bpf_dynptr_data_rdonly() should still work fine with both rdonly and read-write dynptr.
bpf_dynptr_data() only works (in the sense returns non-NULL) for read-write dynptr only.

Quoted 8 lines

And yes, you're kind of painting yourself in a corner if you allow dynptr_data to work with statically ro dynptrs now, ascertaining the ro bit for the returned slice, and then later you plan to add dynptrs whose ro vs rw is not known to the verifier statically. Then it works well for statically known ones (returning both ro and rw slices), but has to return NULL at runtime for statically unknown read only ones, and always rw slice in verifier state for them.

Right, will be both inconsistent and puzzling.

Quoted 18 lines
Quoted 7 lines

By saying that read-only-ness of dynptr should be statically known and rejecting some dynptr functions at load time places us at the mercy of verifier's complete knowledge of application logic, which is exactly against the spirit of dynptr.

Right, that might be too restrictive if we require them to be statically read only.

But it's not about forcing it to be statically ro, it is more about rejecting load when we know the program is incorrect (e.g. the types are incorrect when passed to helpers), otherwise we throw the error at runtime anyway, which seems to be the convention afaicu. But maybe I missed the memo and we gradually want to move away from such strict static checks.

Quoted 6 lines

I view the situation here similar to if we were rejecting direct writes to PTR_TO_MEM | MEM_RDONLY at load time, but offloading as runtime check in the helper writing to it as rw memory arg. It's as if we pretend it's part of the 'type' of the register when doing direct writes, but then ignore it while matching it against the said helper's argument type.

Quoted 8 lines

I disagree, it's not the same. bpf_dynptr_data/bpf_dynptr_data_rdonly turns completely dynamic dynptr into static slice of memory. Only after that point it makes sense for BPF verifier to reject something. Until then it's not incorrect. BPF program will always have to deal with some dynptr operations potentially failing. dynptr can always be NULL internally, you can still call bpf_dynptr_xxx() operations on it, they will just do nothing and return error. That doesn't make BPF program incorrect.

Let me just explain one last time why I'm unable to swallow this 'completely dynamic dynptr' explanation, because it is not treated as completely dynamic by all dynptr helpers.

Quoted 5 lines

No pushback, but it would be great if you could further help me wrap my head around this, so that we're in sync for future discussions.

So you say you may not know the type of dynptr (read-only, rw, local, ringbuf, etc.). Hence you want to treat every dynptr as 'dynamic

No, that's not what I said. I didn't talk about dynptr type (ringbuf vs skb vs local). Type we have to track statically precisely to limit what kind of helpers can be used on dynptrs of specific type.

But note that there are helpers that don't care about dynptr and, in theory, should work with any dynptr. Because dynptr is an abstraction of "logically continuous range of memory". So in some cases even types don't matter.

But regardless, I just don't think about read-only bit as part of dynptr types. I don't see why it has to be statically known and even why it has to stay as read-only or read-write for the entire duration of dynptr lifetime. Why can't we allow flipping dynptr from read-write to read-only before passing it to some custom BPF subprog, potentially provided from some BPF library which you don't control; just to make sure that it cannot really modify underlying memory (even if it is fundamentally modifiable, like with ringbuf)?

So the point I'm trying to communicate is that things that don't **need** to be knowable statically to BPF verifier - **should not** be knowable and should be checked at runtime only. With any dynptr helper that is interfacing its runtime nature into static thing that verifier enforces (which is what bpf_dynptr_data/bpf_dynptr_data_rdonly are), it will always be "fallible" and users will have to expect that they might return NULL or do nothing, or whatever way to deal with failure case.

Quoted 5 lines

And I believe dynptr read-onliness is not something that BPF verifier should be tracking statically. PTR_TO_MEM -- yes, dynptr -- no. That's it. Dynptr type/kind -- yes, track statically, it's way more important to determine what you can at all do with dynptr.

Understood, it's more clear now how you're thinking about this :), especially that you don't consider read-only bit to be a property of dynptr type. I think that was the main point I was not following.

And yes, I guess it's about different tradeoffs. Taking your BPF library example, my idea would be that we will anyway be verifying the global or static subprog in the library by the BTF of its function prototype. The verifier has to verify safety of the subprog by setting up argument types if it is global.

There, I find it more appropriate for global ones to declare const bpf_dynptr * as the argument type if the intent is to not write to the dynptr. Then user can safely pass both rw and ro dynptrs to it, without having to flip it each time at runtime just to ensure safety. If it is static it will also be able to catch incorrect writes for callers.

But indeed there may be cases where you want to control this at runtime, IMPO that should be orthogonal to type level const-ness. Those will be rw at the type level but may change const-ness at runtime, then operations start failing at runtime.

Anyway, I guess we are done here. Again, thanks for sticking along and taking the time to describe it in detail :).

Quoted 2 lines

[...]

#19 lore raw

PATCH v4 2/3 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. For data slices, direct access to data in fragments is also permitted, but access across fragments is not. The returned data slice is reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any helper calls that change the underlying packet buffer (eg bpf_xdp_adjust_head) invalidates any data slices of the associated dynptr. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes xdp dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            |  6 ++++-
 include/linux/filter.h         |  3 +++
 include/uapi/linux/bpf.h       | 25 +++++++++++++++---
 kernel/bpf/helpers.c           | 14 ++++++++++-
 kernel/bpf/verifier.c          |  8 +++++-
 net/core/filter.c              | 46 +++++++++++++++++++++++++++++-----
 tools/include/uapi/linux/bpf.h | 25 +++++++++++++++---
 7 files changed, 112 insertions(+), 15 deletions(-)
include/linux/bpf.h +5 -1
index 30615d1a0c13..455a215b6c57 100644
@@ -410,11 +410,15 @@ enum bpf_type_flag {
 	/* DYNPTR points to sk_buff */
 	DYNPTR_TYPE_SKB		= BIT(11 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to xdp_buff */
+	DYNPTR_TYPE_XDP		= BIT(12 + 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)
include/linux/filter.h +3 -0
index 649063d9cbfd..80f030239877 100644
@@ -1535,5 +1535,8 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u32 ifind
 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);
+void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
 
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +22 -3
index 320e6b95d95c..9feea29eebcd 100644
@@ -5283,13 +5283,18 @@ union bpf_attr {
  *		      and try again.
  *
  *		    * The data slice is automatically invalidated anytime
- *		      **bpf_dynptr_write**\ () or a helper call that changes
- *		      the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *		      **bpf_dynptr_write**\ () is called.
+ *
+ *		For skb-type and xdp-type dynptrs:
+ *		    * The data slice is automatically invalidated anytime a
+ *		      helper call that changes the underlying packet buffer
+ *		      (eg **bpf_skb_pull_data**\ (), **bpf_xdp_adjust_head**\ ())
  *		      is called.
  *	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
- *		is out of bounds or in a paged buffer for skb-type dynptrs.
+ *		is out of bounds or in a paged buffer for skb-type dynptrs or
+ *		across fragments for xdp-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *	Description
@@ -5388,6 +5393,19 @@ union bpf_attr {
  *		*flags* is currently unused, it must be 0 for now.
  *	Return
  *		0 on success or -EINVAL if flags is not 0.
+ *
+ * long bpf_dynptr_from_xdp(struct xdp_buff *xdp_md, u64 flags, struct bpf_dynptr *ptr)
+ *	Description
+ *		Get a dynptr to the data in *xdp_md*. *xdp_md* must be the BPF program
+ *		context.
+ *
+ *		Calls that change the *xdp_md*'s underlying packet buffer
+ *		(eg **bpf_xdp_adjust_head**\ ()) do not invalidate the dynptr, but
+ *		they do invalidate any data slices associated with the dynptr.
+ *
+ *		*flags* is currently unused, it must be 0 for now.
+ *	Return
+ *		0 on success, -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5600,6 +5618,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
 	FN(dynptr_from_skb),		\
+	FN(dynptr_from_xdp),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/helpers.c +13 -1
index 471a01a9b6ae..2b9dc4c6de04 100644
@@ -1541,6 +1541,8 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
 		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(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
 		return -EFAULT;
@@ -1583,6 +1585,10 @@ BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *,
 	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(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
 		return -EFAULT;
@@ -1616,7 +1622,7 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
 
 	type = bpf_dynptr_get_type(ptr);
 
-	/* Only skb dynptrs can get read-only data slices, because the
+	/* Only skb and xdp dynptrs can get read-only data slices, because the
 	 * verifier enforces PTR_TO_PACKET accesses
 	 */
 	is_rdonly = bpf_dynptr_is_rdonly(ptr);
@@ -1640,6 +1646,12 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
 		data = skb->data;
 		break;
 	}
+	case BPF_DYNPTR_TYPE_XDP:
+		/* if the requested data in across fragments, then it cannot
+		 * be accessed directly - bpf_xdp_pointer will return NULL
+		 */
+		return (unsigned long)bpf_xdp_pointer(ptr->data,
+						      ptr->offset + offset, len);
 	default:
 		WARN(true, "bpf_dynptr_data: unknown dynptr type %d\n", type);
 		return 0;
kernel/bpf/verifier.c +7 -1
index 1ea295f47525..d33648eee188 100644
@@ -686,6 +686,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;
 	}
@@ -6078,6 +6080,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 			case DYNPTR_TYPE_SKB:
 				err_extra = "skb ";
 				break;
+			case DYNPTR_TYPE_XDP:
+				err_extra = "xdp ";
+				break;
 			}
 
 			verbose(env, "Expected an initialized %sdynptr as arg #%d\n",
@@ -7439,7 +7444,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		mark_reg_known_zero(env, regs, BPF_REG_0);
 
 		if (func_id == BPF_FUNC_dynptr_data &&
-		    dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+		    (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+		     dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
 			regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
 			regs[BPF_REG_0].range = meta.mem_size;
 		} else {
net/core/filter.c +40 -6

88 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +22 -3
index 3f1800a2b77c..0d5b0117db2a 100644
@@ -5283,13 +5283,18 @@ union bpf_attr {
  *		      and try again.
  *
  *		    * The data slice is automatically invalidated anytime
- *		      **bpf_dynptr_write**\ () or a helper call that changes
- *		      the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *		      **bpf_dynptr_write**\ () is called.
+ *
+ *		For skb-type and xdp-type dynptrs:
+ *		    * The data slice is automatically invalidated anytime a
+ *		      helper call that changes the underlying packet buffer
+ *		      (eg **bpf_skb_pull_data**\ (), **bpf_xdp_adjust_head**\ ())
  *		      is called.
  *	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
- *		is out of bounds or in a paged buffer for skb-type dynptrs.
+ *		is out of bounds or in a paged buffer for skb-type dynptrs or
+ *		across fragments for xdp-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *	Description
@@ -5388,6 +5393,19 @@ union bpf_attr {
  *		*flags* is currently unused, it must be 0 for now.
  *	Return
  *		0 on success or -EINVAL if flags is not 0.
+ *
+ * long bpf_dynptr_from_xdp(struct xdp_buff *xdp_md, u64 flags, struct bpf_dynptr *ptr)
+ *	Description
+ *		Get a dynptr to the data in *xdp_md*. *xdp_md* must be the BPF program
+ *		context.
+ *
+ *		Calls that change the *xdp_md*'s underlying packet buffer
+ *		(eg **bpf_xdp_adjust_head**\ ()) do not invalidate the dynptr, but
+ *		they do invalidate any data slices associated with the dynptr.
+ *
+ *		*flags* is currently unused, it must be 0 for now.
+ *	Return
+ *		0 on success, -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5600,6 +5618,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
 	FN(dynptr_from_skb),		\
+	FN(dynptr_from_xdp),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.30.2
#20 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

+Cc XDP folks

Quoted 10 lines · 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).

Quoted 2 lines

For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. For data slices, direct access to

It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Quoted 183 lines

data in fragments is also permitted, but access across fragments is not. The returned data slice is reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any helper calls that change the underlying packet buffer (eg bpf_xdp_adjust_head) invalidates any data slices of the associated dynptr. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes xdp dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            |  6 ++++-
 include/linux/filter.h         |  3 +++
 include/uapi/linux/bpf.h       | 25 +++++++++++++++---
 kernel/bpf/helpers.c           | 14 ++++++++++-
 kernel/bpf/verifier.c          |  8 +++++-
 net/core/filter.c              | 46 +++++++++++++++++++++++++++++-----
 tools/include/uapi/linux/bpf.h | 25 +++++++++++++++---
 7 files changed, 112 insertions(+), 15 deletions(-)
include/linux/bpf.h +5 -1
index 30615d1a0c13..455a215b6c57 100644
@@ -410,11 +410,15 @@ enum bpf_type_flag {
        /* DYNPTR points to sk_buff */
        DYNPTR_TYPE_SKB         = BIT(11 + BPF_BASE_TYPE_BITS),
 
+       /* DYNPTR points to xdp_buff */
+       DYNPTR_TYPE_XDP         = BIT(12 + 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)
include/linux/filter.h +3 -0
index 649063d9cbfd..80f030239877 100644
@@ -1535,5 +1535,8 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u32 ifind
 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);
+void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
 
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +22 -3
index 320e6b95d95c..9feea29eebcd 100644
@@ -5283,13 +5283,18 @@ union bpf_attr {
  *                   and try again.
  *
  *                 * The data slice is automatically invalidated anytime
- *                   **bpf_dynptr_write**\ () or a helper call that changes
- *                   the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *                   **bpf_dynptr_write**\ () is called.
+ *
+ *             For skb-type and xdp-type dynptrs:
+ *                 * The data slice is automatically invalidated anytime a
+ *                   helper call that changes the underlying packet buffer
+ *                   (eg **bpf_skb_pull_data**\ (), **bpf_xdp_adjust_head**\ ())
  *                   is called.
  *     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
- *             is out of bounds or in a paged buffer for skb-type dynptrs.
+ *             is out of bounds or in a paged buffer for skb-type dynptrs or
+ *             across fragments for xdp-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *     Description
@@ -5388,6 +5393,19 @@ union bpf_attr {
  *             *flags* is currently unused, it must be 0 for now.
  *     Return
  *             0 on success or -EINVAL if flags is not 0.
+ *
+ * long bpf_dynptr_from_xdp(struct xdp_buff *xdp_md, u64 flags, struct bpf_dynptr *ptr)
+ *     Description
+ *             Get a dynptr to the data in *xdp_md*. *xdp_md* must be the BPF program
+ *             context.
+ *
+ *             Calls that change the *xdp_md*'s underlying packet buffer
+ *             (eg **bpf_xdp_adjust_head**\ ()) do not invalidate the dynptr, but
+ *             they do invalidate any data slices associated with the dynptr.
+ *
+ *             *flags* is currently unused, it must be 0 for now.
+ *     Return
+ *             0 on success, -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5600,6 +5618,7 @@ union bpf_attr {
        FN(tcp_raw_check_syncookie_ipv6),       \
        FN(ktime_get_tai_ns),           \
        FN(dynptr_from_skb),            \
+       FN(dynptr_from_xdp),            \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/helpers.c +13 -1
index 471a01a9b6ae..2b9dc4c6de04 100644
@@ -1541,6 +1541,8 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
                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(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
                return -EFAULT;
@@ -1583,6 +1585,10 @@ BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *,
        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(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
                return -EFAULT;
@@ -1616,7 +1622,7 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
 
        type = bpf_dynptr_get_type(ptr);
 
-       /* Only skb dynptrs can get read-only data slices, because the
+       /* Only skb and xdp dynptrs can get read-only data slices, because the
         * verifier enforces PTR_TO_PACKET accesses
         */
        is_rdonly = bpf_dynptr_is_rdonly(ptr);
@@ -1640,6 +1646,12 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
                data = skb->data;
                break;
        }
+       case BPF_DYNPTR_TYPE_XDP:
+               /* if the requested data in across fragments, then it cannot
+                * be accessed directly - bpf_xdp_pointer will return NULL
+                */
+               return (unsigned long)bpf_xdp_pointer(ptr->data,
+                                                     ptr->offset + offset, len);
        default:
                WARN(true, "bpf_dynptr_data: unknown dynptr type %d\n", type);
                return 0;
kernel/bpf/verifier.c +5 -0
index 1ea295f47525..d33648eee188 100644
@@ -686,6 +686,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;
        }
@@ -6078,6 +6080,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                        case DYNPTR_TYPE_SKB:
                                err_extra = "skb ";
                                break;
+                       case DYNPTR_TYPE_XDP:
+                               err_extra = "xdp ";
+                               break;
                        }
 
                        verbose(env, "Expected an initialized %sdynptr as arg #%d\n",

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7450

Quoted 8 lines
kernel/bpf/verifier.c +2 -1
@@ -7439,7 +7444,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
                mark_reg_known_zero(env, regs, BPF_REG_0);
 
                if (func_id == BPF_FUNC_dynptr_data &&
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

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

#21 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 17 lines · Kumar Kartikeya Dwivedi

+Cc XDP folks

Quoted 10 lines · 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).

Quoted 2 lines

For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. For data slices, direct access to

Quoted 7 lines

It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Yeah, it'd be great if the behavior for skb/xdp progs could be the same, but I'm not seeing a better solution here (unless we invalidate data slices on writes in xdp progs, just to make it match more :P).

Regarding having 2 different interfaces bpf_dynptr_from_{skb/xdp}, I'm not convinced this is much of a problem - xdp and skb programs already have different interfaces for doing things (eg bpf_{skb/xdp}_{store/load}_bytes).

Quoted 227 lines
Quoted 184 lines

data in fragments is also permitted, but access across fragments is not. The returned data slice is reg type PTR_TO_PACKET | PTR_MAYBE_NULL.

Any helper calls that change the underlying packet buffer (eg bpf_xdp_adjust_head) invalidates any data slices of the associated dynptr. Whenever such a helper call is made, the verifier marks any PTR_TO_PACKET reg type (which includes xdp dynptr slices since they are PTR_TO_PACKETs) as unknown. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown()

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

---
 include/linux/bpf.h            |  6 ++++-
 include/linux/filter.h         |  3 +++
 include/uapi/linux/bpf.h       | 25 +++++++++++++++---
 kernel/bpf/helpers.c           | 14 ++++++++++-
 kernel/bpf/verifier.c          |  8 +++++-
 net/core/filter.c              | 46 +++++++++++++++++++++++++++++-----
 tools/include/uapi/linux/bpf.h | 25 +++++++++++++++---
 7 files changed, 112 insertions(+), 15 deletions(-)
include/linux/bpf.h +5 -1
index 30615d1a0c13..455a215b6c57 100644
@@ -410,11 +410,15 @@ enum bpf_type_flag {
        /* DYNPTR points to sk_buff */
        DYNPTR_TYPE_SKB         = BIT(11 + BPF_BASE_TYPE_BITS),
 
+       /* DYNPTR points to xdp_buff */
+       DYNPTR_TYPE_XDP         = BIT(12 + 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)
include/linux/filter.h +3 -0
index 649063d9cbfd..80f030239877 100644
@@ -1535,5 +1535,8 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u32 ifind
 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);
+void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
 
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +22 -3
index 320e6b95d95c..9feea29eebcd 100644
@@ -5283,13 +5283,18 @@ union bpf_attr {
  *                   and try again.
  *
  *                 * The data slice is automatically invalidated anytime
- *                   **bpf_dynptr_write**\ () or a helper call that changes
- *                   the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *                   **bpf_dynptr_write**\ () is called.
+ *
+ *             For skb-type and xdp-type dynptrs:
+ *                 * The data slice is automatically invalidated anytime a
+ *                   helper call that changes the underlying packet buffer
+ *                   (eg **bpf_skb_pull_data**\ (), **bpf_xdp_adjust_head**\ ())
  *                   is called.
  *     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
- *             is out of bounds or in a paged buffer for skb-type dynptrs.
+ *             is out of bounds or in a paged buffer for skb-type dynptrs or
+ *             across fragments for xdp-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *     Description
@@ -5388,6 +5393,19 @@ union bpf_attr {
  *             *flags* is currently unused, it must be 0 for now.
  *     Return
  *             0 on success or -EINVAL if flags is not 0.
+ *
+ * long bpf_dynptr_from_xdp(struct xdp_buff *xdp_md, u64 flags, struct bpf_dynptr *ptr)
+ *     Description
+ *             Get a dynptr to the data in *xdp_md*. *xdp_md* must be the BPF program
+ *             context.
+ *
+ *             Calls that change the *xdp_md*'s underlying packet buffer
+ *             (eg **bpf_xdp_adjust_head**\ ()) do not invalidate the dynptr, but
+ *             they do invalidate any data slices associated with the dynptr.
+ *
+ *             *flags* is currently unused, it must be 0 for now.
+ *     Return
+ *             0 on success, -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5600,6 +5618,7 @@ union bpf_attr {
        FN(tcp_raw_check_syncookie_ipv6),       \
        FN(ktime_get_tai_ns),           \
        FN(dynptr_from_skb),            \
+       FN(dynptr_from_xdp),            \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/helpers.c +13 -1
index 471a01a9b6ae..2b9dc4c6de04 100644
@@ -1541,6 +1541,8 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
                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(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
                return -EFAULT;
@@ -1583,6 +1585,10 @@ BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *,
        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(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
                return -EFAULT;
@@ -1616,7 +1622,7 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
 
        type = bpf_dynptr_get_type(ptr);
 
-       /* Only skb dynptrs can get read-only data slices, because the
+       /* Only skb and xdp dynptrs can get read-only data slices, because the
         * verifier enforces PTR_TO_PACKET accesses
         */
        is_rdonly = bpf_dynptr_is_rdonly(ptr);
@@ -1640,6 +1646,12 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
                data = skb->data;
                break;
        }
+       case BPF_DYNPTR_TYPE_XDP:
+               /* if the requested data in across fragments, then it cannot
+                * be accessed directly - bpf_xdp_pointer will return NULL
+                */
+               return (unsigned long)bpf_xdp_pointer(ptr->data,
+                                                     ptr->offset + offset, len);
        default:
                WARN(true, "bpf_dynptr_data: unknown dynptr type %d\n", type);
                return 0;
kernel/bpf/verifier.c +5 -0
index 1ea295f47525..d33648eee188 100644
@@ -686,6 +686,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;
        }
@@ -6078,6 +6080,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                        case DYNPTR_TYPE_SKB:
                                err_extra = "skb ";
                                break;
+                       case DYNPTR_TYPE_XDP:
+                               err_extra = "xdp ";
+                               break;
                        }
 
                        verbose(env, "Expected an initialized %sdynptr as arg #%d\n",

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7450

Quoted 8 lines
kernel/bpf/verifier.c +2 -1
@@ -7439,7 +7444,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
                mark_reg_known_zero(env, regs, BPF_REG_0);
 
                if (func_id == BPF_FUNC_dynptr_data &&
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

Quoted 2 lines

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

Ooh interesting, I hadn't previously taken a look at try_match_pkt_pointers(), thanks for mentioning it :)

The cleanest solution to me is to add the flag "DYNPTR_TYPE_{SKB/XDP}" to PTR_TO_PACKET and change reg_is_init_pkt_pointer() to return false if the DYNPTR_TYPE_{SKB/XDP} flag is present. I prefer this over returning PTR_TO_MEM because it seems more robust (eg if in the future we reject x behavior on the packet data reg types, this will automatically apply to the data slices), and because it'll keep the logic more efficient/simpler for the case when the pkt pointer has to be cleared after any helper that changes pkt data is called (aka the case where the data slice gets invalidated).

What are your thoughts?

Quoted 2 lines

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

#22 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 27 lines · Joanne Koong
Quoted 17 lines · Kumar Kartikeya Dwivedi

+Cc XDP folks

Quoted 10 lines · 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).

Quoted 2 lines

For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. For data slices, direct access to

Quoted 7 lines

It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Quoted 8 lines

Yeah, it'd be great if the behavior for skb/xdp progs could be the same, but I'm not seeing a better solution here (unless we invalidate data slices on writes in xdp progs, just to make it match more :P).

Regarding having 2 different interfaces bpf_dynptr_from_{skb/xdp}, I'm not convinced this is much of a problem - xdp and skb programs already have different interfaces for doing things (eg bpf_{skb/xdp}_{store/load}_bytes).

This is true, but it's quite possible to paper over these differences and write BPF code that works for both TC and XDP. Subtle semantic differences in otherwise identical functions makes this harder.

Today you can write a function like:

static inline int parse_pkt(void *data, void* data_end)

{
        /* parse data */
}

And call it like:

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        return parse_pkt(ctx->data, ctx->data_end);
}

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        return parse_pkt(skb->data, skb->data_end);
}

IMO the goal should be to be able to do the equivalent for dynptrs, like:

static inline int parse_pkt(struct bpf_dynptr *ptr)

{
        __u64 *data;
        
	data = bpf_dynptr_data(ptr, 0, sizeof(*data));
	if (!data)
		return 0;
        /* parse data */
}

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
	struct bpf_dynptr ptr;

	bpf_dynptr_from_xdp(ctx, 0, &ptr);
        return parse_pkt(&ptr);
}

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
	struct bpf_dynptr ptr;

	bpf_dynptr_from_skb(skb, 0, &ptr);
        return parse_pkt(&ptr);
}

If the dynptr-based parse_pkt() function has to take special care to figure out where the dynptr comes from, it makes it a lot more difficult to write reusable packet parsing functions. So I'd be in favour of restricting the dynptr interface to the lowest common denominator of the skb and xdp interfaces even if that makes things slightly more awkward in the specialised cases...

-Toke

#23 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 86 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Joanne Koong
Quoted 17 lines · Kumar Kartikeya Dwivedi

+Cc XDP folks

Quoted 10 lines · 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).

Quoted 2 lines

For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. For data slices, direct access to

Quoted 7 lines

It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Quoted 8 lines

Yeah, it'd be great if the behavior for skb/xdp progs could be the same, but I'm not seeing a better solution here (unless we invalidate data slices on writes in xdp progs, just to make it match more :P).

Regarding having 2 different interfaces bpf_dynptr_from_{skb/xdp}, I'm not convinced this is much of a problem - xdp and skb programs already have different interfaces for doing things (eg bpf_{skb/xdp}_{store/load}_bytes).

This is true, but it's quite possible to paper over these differences and write BPF code that works for both TC and XDP. Subtle semantic differences in otherwise identical functions makes this harder.

Today you can write a function like:

static inline int parse_pkt(void *data, void* data_end)

{
        /* parse data */
}

And call it like:

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        return parse_pkt(ctx->data, ctx->data_end);
}

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        return parse_pkt(skb->data, skb->data_end);
}

IMO the goal should be to be able to do the equivalent for dynptrs, like:

static inline int parse_pkt(struct bpf_dynptr *ptr)

{
        __u64 *data;

        data = bpf_dynptr_data(ptr, 0, sizeof(*data));
        if (!data)
                return 0;
        /* parse data */
}

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_xdp(ctx, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_skb(skb, 0, &ptr);
        return parse_pkt(&ptr);
}

To clarify, this is already possible when using data slices, since the behavior for data slices is equivalent between xdp and tc programs for non-fragmented accesses. From looking through the selftests, I anticipate that data slices will be the main way programs interact with dynptrs. For the cases where the program may write into frags, then bpf_dynptr_write will be needed (which is where functionality between xdp and tc start differing) - today, we're not able to write common code that writes into the frags since tc uses bpf_skb_store_bytes and xdp uses bpf_xdp_store_bytes.

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

for xdp-type progs:
- nothing

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

This will unite the functionality for skb and xdp programs across bpf_dyntpr_data, bpf_dynptr_write, and bpf_dynptr_read. As for whether we should unite bpf_dynptr_from_skb and bpf_dynptr_from_xdp into one common bpf_dynptr_from_packet as Jakub brought in [0], I'm leaning towards no because 1) if in the future there's some irreconcilable aspect between skb and xdp that gets added, that'll be hard to support since the expectation is that there is just one overall "packet dynptr" 2) the "packet dynptr" view is not completely accurate (eg bpf_dynptr_write accepts flags from skb progs and not xdp progs) 3) this adds some additional hardcoding in the verifier since there's no organic mapping between prog type -> prog ctx

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

Quoted 10 lines

If the dynptr-based parse_pkt() function has to take special care to figure out where the dynptr comes from, it makes it a lot more difficult to write reusable packet parsing functions. So I'd be in favour of restricting the dynptr interface to the lowest common denominator of the skb and xdp interfaces even if that makes things slightly more awkward in the specialised cases...

-Toke

#24 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 79 lines · Joanne Koong
Quoted 67 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Joanne Koong
Quoted 8 lines

[...]
It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Quoted 8 lines

Yeah, it'd be great if the behavior for skb/xdp progs could be the same, but I'm not seeing a better solution here (unless we invalidate data slices on writes in xdp progs, just to make it match more :P).

Regarding having 2 different interfaces bpf_dynptr_from_{skb/xdp}, I'm not convinced this is much of a problem - xdp and skb programs already have different interfaces for doing things (eg bpf_{skb/xdp}_{store/load}_bytes).

This is true, but it's quite possible to paper over these differences and write BPF code that works for both TC and XDP. Subtle semantic differences in otherwise identical functions makes this harder.

Today you can write a function like:

static inline int parse_pkt(void *data, void* data_end)

{
        /* parse data */
}

And call it like:

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        return parse_pkt(ctx->data, ctx->data_end);
}

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        return parse_pkt(skb->data, skb->data_end);
}

IMO the goal should be to be able to do the equivalent for dynptrs, like:

static inline int parse_pkt(struct bpf_dynptr *ptr)

{
        __u64 *data;

        data = bpf_dynptr_data(ptr, 0, sizeof(*data));
        if (!data)
                return 0;
        /* parse data */
}

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_xdp(ctx, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_skb(skb, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

To clarify, this is already possible when using data slices, since the behavior for data slices is equivalent between xdp and tc programs for non-fragmented accesses. From looking through the selftests, I anticipate that data slices will be the main way programs interact with dynptrs. For the cases where the program may write into frags, then bpf_dynptr_write will be needed (which is where functionality between xdp and tc start differing) - today, we're not able to write common code that writes into the frags since tc uses bpf_skb_store_bytes and xdp uses bpf_xdp_store_bytes.

Yes, we cannot write code that just swaps those two calls right now. The key difference is, the two above are separate functions already and have different semantics. Here in this set you have the same function, but with different semantics depending on ctx, which is the point of contention.

Quoted 29 lines

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

Quoted 7 lines

for xdp-type progs:
- nothing

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

There is also the other option: failing to write until you pull skb, which looks a lot better to me if we are adding this helper anyway...

Quoted 12 lines

This will unite the functionality for skb and xdp programs across bpf_dyntpr_data, bpf_dynptr_write, and bpf_dynptr_read. As for whether we should unite bpf_dynptr_from_skb and bpf_dynptr_from_xdp into one common bpf_dynptr_from_packet as Jakub brought in [0], I'm leaning towards no because 1) if in the future there's some irreconcilable aspect between skb and xdp that gets added, that'll be hard to support since the expectation is that there is just one overall "packet dynptr" 2) the "packet dynptr" view is not completely accurate (eg bpf_dynptr_write accepts flags from skb progs and not xdp progs) 3) this adds some additional hardcoding in the verifier since there's no organic mapping between prog type -> prog ctx

There is, e.g. see how btf_get_prog_ctx_type is doing it (unless I misunderstood what you meant).

I also had a different tangential question (unrelated to 'reconciliation'). Let's forget about that for a moment. I was listening to the talk here [0]. At 12:00, I can hear someone mentioning that you'd have a dynptr for each segment/frag.

Right now, you have a skb/xdp dynptr, which is representing discontiguous memory, i.e. you represent all linear page + fragments using one dynptr. This seems a bit inconsistent with the idea of a dynptr, since it's conceptually a slice of variable length memory. dynptr_data gives you a constant length slice into dynptr's variable length memory (which the verifier can track bounds of statically).

So thinking about the idea in [0], one way would be to change from_xdp/from_skb helpers to take an index (into the frags array), and return dynptr for each frag. Or determine frag from offset + len. For the skb case it might require kmap_atomic to access the frag dynptr, which might need a paired kunmap_atomic call as well, and it may not return dynptr for frags in cloned skbs, but for XDP multi-buff all that doesn't count.

Was this approach/line of thinking considered and/or rejected? What were the reasons? Just trying to understand the background here.

What I'm wondering is that we already have helpers to do reads and writes that you are wrapping over in dynptr interfaces, but what we're missing is to be able to get direct access to frags (or 'next' ctx, essentially). When we know we can avoid pulls, it might be cheaper to then do access this way to skb, right? Especially for a case where just a part of the header lies in the next frag?

But I'm no expert here, so I might be missing some subtleties.

[0]: https://www.youtube.com/watch?v=EZ5PDrXvs7M

Quoted 13 lines

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

Quoted 10 lines

If the dynptr-based parse_pkt() function has to take special care to figure out where the dynptr comes from, it makes it a lot more difficult to write reusable packet parsing functions. So I'd be in favour of restricting the dynptr interface to the lowest common denominator of the skb and xdp interfaces even if that makes things slightly more awkward in the specialised cases...

-Toke

#25 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 129 lines · Kumar Kartikeya Dwivedi
Quoted 79 lines · Joanne Koong
Quoted 67 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Joanne Koong
Quoted 8 lines

[...]
It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Quoted 8 lines

Yeah, it'd be great if the behavior for skb/xdp progs could be the same, but I'm not seeing a better solution here (unless we invalidate data slices on writes in xdp progs, just to make it match more :P).

Regarding having 2 different interfaces bpf_dynptr_from_{skb/xdp}, I'm not convinced this is much of a problem - xdp and skb programs already have different interfaces for doing things (eg bpf_{skb/xdp}_{store/load}_bytes).

This is true, but it's quite possible to paper over these differences and write BPF code that works for both TC and XDP. Subtle semantic differences in otherwise identical functions makes this harder.

Today you can write a function like:

static inline int parse_pkt(void *data, void* data_end)

{
        /* parse data */
}

And call it like:

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        return parse_pkt(ctx->data, ctx->data_end);
}

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        return parse_pkt(skb->data, skb->data_end);
}

IMO the goal should be to be able to do the equivalent for dynptrs, like:

static inline int parse_pkt(struct bpf_dynptr *ptr)

{
        __u64 *data;

        data = bpf_dynptr_data(ptr, 0, sizeof(*data));
        if (!data)
                return 0;
        /* parse data */
}

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_xdp(ctx, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_skb(skb, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

To clarify, this is already possible when using data slices, since the behavior for data slices is equivalent between xdp and tc programs for non-fragmented accesses. From looking through the selftests, I anticipate that data slices will be the main way programs interact with dynptrs. For the cases where the program may write into frags, then bpf_dynptr_write will be needed (which is where functionality between xdp and tc start differing) - today, we're not able to write common code that writes into the frags since tc uses bpf_skb_store_bytes and xdp uses bpf_xdp_store_bytes.

Yes, we cannot write code that just swaps those two calls right now. The key difference is, the two above are separate functions already and have different semantics. Here in this set you have the same function, but with different semantics depending on ctx, which is the point of contention.

Quoted 32 lines

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

for xdp-type progs:
- nothing

Quoted 7 lines
Quoted 4 lines

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

There is also the other option: failing to write until you pull skb, which looks a lot better to me if we are adding this helper anyway...

This was also previously discussed in [0].

After using skb dynptrs for the test_cls_redirect_dynptr.c selftest [1], I'm convinced that allowing bpf_dynptr_writes into frags is the correct way to go. There are instances where the data is probably in head with a slight off-chance that it's in a fragment - being able to call bpf_dynptr_write makes it super easy whereas the alternative is either 1) always pulling, which in most cases will be unnecessary or
2) adding special casing for the case where the bpf_dynptr_write fails. As well, failing to write until you pull the skb will hurt being able to write common code that both xdp/skb can use.

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

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

Quoted 14 lines
Quoted 13 lines

This will unite the functionality for skb and xdp programs across bpf_dyntpr_data, bpf_dynptr_write, and bpf_dynptr_read. As for whether we should unite bpf_dynptr_from_skb and bpf_dynptr_from_xdp into one common bpf_dynptr_from_packet as Jakub brought in [0], I'm leaning towards no because 1) if in the future there's some irreconcilable aspect between skb and xdp that gets added, that'll be hard to support since the expectation is that there is just one overall "packet dynptr" 2) the "packet dynptr" view is not completely accurate (eg bpf_dynptr_write accepts flags from skb progs and not xdp progs) 3) this adds some additional hardcoding in the verifier since there's no organic mapping between prog type -> prog ctx

Quoted 2 lines

There is, e.g. see how btf_get_prog_ctx_type is doing it (unless I misunderstood what you meant).

Cool, that should be pretty straightforwarwd then; from what it looks like, btf_get_prog_ctx_type() + 1 will give the btf id and then from that use btf_type_by_id() to get the corresponding struct btf_type, then use btf_type->name_off to check whether the name corresponds to "__sk_buff" or "xdp_md"

Quoted 24 lines

I also had a different tangential question (unrelated to 'reconciliation'). Let's forget about that for a moment. I was listening to the talk here [0]. At 12:00, I can hear someone mentioning that you'd have a dynptr for each segment/frag.

Right now, you have a skb/xdp dynptr, which is representing discontiguous memory, i.e. you represent all linear page + fragments using one dynptr. This seems a bit inconsistent with the idea of a dynptr, since it's conceptually a slice of variable length memory. dynptr_data gives you a constant length slice into dynptr's variable length memory (which the verifier can track bounds of statically).

So thinking about the idea in [0], one way would be to change from_xdp/from_skb helpers to take an index (into the frags array), and return dynptr for each frag. Or determine frag from offset + len. For the skb case it might require kmap_atomic to access the frag dynptr, which might need a paired kunmap_atomic call as well, and it may not return dynptr for frags in cloned skbs, but for XDP multi-buff all that doesn't count.

Was this approach/line of thinking considered and/or rejected? What were the reasons? Just trying to understand the background here.

Quoted 6 lines

What I'm wondering is that we already have helpers to do reads and writes that you are wrapping over in dynptr interfaces, but what we're missing is to be able to get direct access to frags (or 'next' ctx, essentially). When we know we can avoid pulls, it might be cheaper to then do access this way to skb, right? Especially for a case where just a part of the header lies in the next frag?

Imo, having a separate dynptr for each frag is confusing for users. My intuition is that in the majority of cases, the user doesn't care how it's laid out at the frag level, they just want to write some data at some offset and length. If every frag is a separate dynptr, then the user needs to be aware whether they're crossing frag (and thus dynptr) boundaries. In cases where the user doesn't know if the data is across head/frag or across frags, then their prog code will need to be more complex to handle these different scenarios. There's the additional issue as well that this doesn't work for cloned skb frags - now users need to know whether the skb is cloned or uncloned to know whether to pull first. Imo it makes the interface more confusing. I'm still not sure what is gained by having separate dynptrs for separate frags.

If the goal is to avoid pulls, then we're able to do that without having separate dynptrs. If a write is to an uncloned frag, then bpf_dynptr_write() can kmap, write the data, and kunmap (though it's still unclear as to whether this should be done, since maybe it's faster to pull and do large numbers of writes instead of multiple kmap/kunmap calls).

Quoted 26 lines

But I'm no expert here, so I might be missing some subtleties.

[0]: https://www.youtube.com/watch?v=EZ5PDrXvs7M

Quoted 13 lines

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

Quoted 10 lines

If the dynptr-based parse_pkt() function has to take special care to figure out where the dynptr comes from, it makes it a lot more difficult to write reusable packet parsing functions. So I'd be in favour of restricting the dynptr interface to the lowest common denominator of the skb and xdp interfaces even if that makes things slightly more awkward in the specialised cases...

-Toke

#26 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

In reply to Kumar Kartikeya Dwivedi
Quoted 91 lines · Kumar Kartikeya Dwivedi
Quoted 79 lines · Joanne Koong
Quoted 67 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Joanne Koong
Quoted 8 lines

[...]
It's a bit awkward to have such a difference between xdp and skb dynptr's read/write. I understand why it is the way it is, but it still doesn't feel right. I'm not sure if we can reconcile the differences, but it makes writing common code for both xdp and tc harder as it needs to be aware of the differences (and then the flags for dynptr_write would differ too). So we're 90% there but not the whole way...

Quoted 8 lines

Yeah, it'd be great if the behavior for skb/xdp progs could be the same, but I'm not seeing a better solution here (unless we invalidate data slices on writes in xdp progs, just to make it match more :P).

Regarding having 2 different interfaces bpf_dynptr_from_{skb/xdp}, I'm not convinced this is much of a problem - xdp and skb programs already have different interfaces for doing things (eg bpf_{skb/xdp}_{store/load}_bytes).

This is true, but it's quite possible to paper over these differences and write BPF code that works for both TC and XDP. Subtle semantic differences in otherwise identical functions makes this harder.

Today you can write a function like:

static inline int parse_pkt(void *data, void* data_end)

{
        /* parse data */
}

And call it like:

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        return parse_pkt(ctx->data, ctx->data_end);
}

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        return parse_pkt(skb->data, skb->data_end);
}

IMO the goal should be to be able to do the equivalent for dynptrs, like:

static inline int parse_pkt(struct bpf_dynptr *ptr)

{
        __u64 *data;

        data = bpf_dynptr_data(ptr, 0, sizeof(*data));
        if (!data)
                return 0;
        /* parse data */
}

SEC("xdp")
int parse_xdp(struct xdp_md *ctx)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_xdp(ctx, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

SEC("tc")
int parse_tc(struct __sk_buff *skb)

{
        struct bpf_dynptr ptr;

        bpf_dynptr_from_skb(skb, 0, &ptr);
        return parse_pkt(&ptr);
}
Quoted 9 lines

To clarify, this is already possible when using data slices, since the behavior for data slices is equivalent between xdp and tc programs for non-fragmented accesses. From looking through the selftests, I anticipate that data slices will be the main way programs interact with dynptrs. For the cases where the program may write into frags, then bpf_dynptr_write will be needed (which is where functionality between xdp and tc start differing) - today, we're not able to write common code that writes into the frags since tc uses bpf_skb_store_bytes and xdp uses bpf_xdp_store_bytes.

Quoted 5 lines

Yes, we cannot write code that just swaps those two calls right now. The key difference is, the two above are separate functions already and have different semantics. Here in this set you have the same function, but with different semantics depending on ctx, which is the point of contention.

bpf_dynptr_{read,write}() are effectively virtual methods of dynptr and we have few different implementations of dynptr, depending on what data they are wrapping, so having different semantics depending on ctx makes sense, if they are dictated by good reasons (like good performance for skb and xdp).

Quoted 33 lines
Quoted 33 lines

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

for xdp-type progs:
- nothing

Quoted 7 lines
Quoted 4 lines

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

There is also the other option: failing to write until you pull skb, which looks a lot better to me if we are adding this helper anyway...

Wouldn't this kill performance for typical cases when writes don't go into frags?

Quoted 14 lines
Quoted 13 lines

This will unite the functionality for skb and xdp programs across bpf_dyntpr_data, bpf_dynptr_write, and bpf_dynptr_read. As for whether we should unite bpf_dynptr_from_skb and bpf_dynptr_from_xdp into one common bpf_dynptr_from_packet as Jakub brought in [0], I'm leaning towards no because 1) if in the future there's some irreconcilable aspect between skb and xdp that gets added, that'll be hard to support since the expectation is that there is just one overall "packet dynptr" 2) the "packet dynptr" view is not completely accurate (eg bpf_dynptr_write accepts flags from skb progs and not xdp progs) 3) this adds some additional hardcoding in the verifier since there's no organic mapping between prog type -> prog ctx

Quoted 7 lines

There is, e.g. see how btf_get_prog_ctx_type is doing it (unless I misunderstood what you meant).

I also had a different tangential question (unrelated to 'reconciliation'). Let's forget about that for a moment. I was listening to the talk here [0]. At 12:00, I can hear someone mentioning that you'd have a dynptr for each segment/frag.

One of those people was me :) I think what you are referring to was an idea that for frags we might end a special iterator function that will call a callback for each linear frag. And in such case linear frag will be presented to callback as dynptr (just as an interface to statically unknown memory region, it will be DYNPTR_LOCAL at that point, probably).

It's different from DYNPTR_SKB that Joanne is adding here, though.

Quoted 7 lines

Right now, you have a skb/xdp dynptr, which is representing discontiguous memory, i.e. you represent all linear page + fragments using one dynptr. This seems a bit inconsistent with the idea of a dynptr, since it's conceptually a slice of variable length memory. dynptr_data gives you a constant length slice into dynptr's variable length memory (which the verifier can track bounds of statically).

All the data in skb is conceptually a slice of variable length memory with no gaps in between. It's just physically discontiguous, but there is still a linear offset addressing (conceptually). So I think it makes sense to represent skb data as a whole as one SKB dynptr. There are technical restrictions on direct memory access through bpf_dynptr_data(), inevitablly.

Quoted 8 lines

So thinking about the idea in [0], one way would be to change from_xdp/from_skb helpers to take an index (into the frags array), and return dynptr for each frag. Or determine frag from offset + len. For the skb case it might require kmap_atomic to access the frag dynptr, which might need a paired kunmap_atomic call as well, and it may not return dynptr for frags in cloned skbs, but for XDP multi-buff all that doesn't count.

We could do something like bpf_dynptr_from_skb_frag(skb, N) and get access to frag #N (where we can special case 0 being linear skb data), but that would be DYNPTR_LOCAL at that point, probably? And it's a separate extension in addition to DYNPTR_SKB. Having generic bpf_dynptr_{read,write}() working over entire skb range is very useful, while this per-frag dynptr would prevent this. So let's keep those two separate.

Quoted 36 lines

Was this approach/line of thinking considered and/or rejected? What were the reasons? Just trying to understand the background here.

What I'm wondering is that we already have helpers to do reads and writes that you are wrapping over in dynptr interfaces, but what we're missing is to be able to get direct access to frags (or 'next' ctx, essentially). When we know we can avoid pulls, it might be cheaper to then do access this way to skb, right? Especially for a case where just a part of the header lies in the next frag?

But I'm no expert here, so I might be missing some subtleties.

[0]: https://www.youtube.com/watch?v=EZ5PDrXvs7M

Quoted 13 lines

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

Quoted 10 lines

If the dynptr-based parse_pkt() function has to take special care to figure out where the dynptr comes from, it makes it a lot more difficult to write reusable packet parsing functions. So I'd be in favour of restricting the dynptr interface to the lowest common denominator of the skb and xdp interfaces even if that makes things slightly more awkward in the specialised cases...

-Toke

#27 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

In reply to Kumar Kartikeya Dwivedi
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Joanne Koong

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

Quoted 7 lines
Quoted 7 lines

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

I don't think it is necessary (or help) to restrict xdp slice from getting a fragment. In any case, the xdp prog has to deal with the case that bpf_dynptr_data(xdp_dynptr, offset, len) is across two fragments. Although unlikely, it still need to handle it (dynptr_data returns NULL) properly by using bpf_dynptr_read(). The same that the skb case also needs to handle dynptr_data returning NULL.

Something like Kumar's sample in [0] should work for both xdp and skb dynptr but replace the helpers with bpf_dynptr_{data,read,write}().

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

Quoted 8 lines
Quoted 8 lines

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

Quoted 6 lines
Quoted 6 lines

for xdp-type progs:
- nothing

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

I wonder if the 'Before' behavior can be kept as is.

The bpf prog that runs in both xdp and bpf should be the one always expecting the data-slice will be invalidated and it has to call the bpf_dynptr_data() again after writing. Yes, it is unnecessary for xdp but the bpf prog needs to the same anyway if the verifier was the one enforcing it for both skb and xdp dynptr type.

If the bpf prog is written for xdp alone, then it has no need to re-get the bpf_dynptr_data() after writing.

Quoted 4 lines
Quoted 1 line

There is also the other option: failing to write until you pull skb, which looks a lot better to me if we are adding this helper anyway...

#28 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 46 lines · Martin KaFai Lau
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Joanne Koong

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

Quoted 7 lines
Quoted 7 lines

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

I don't think it is necessary (or help) to restrict xdp slice from getting a fragment. In any case, the xdp prog has to deal with the case that bpf_dynptr_data(xdp_dynptr, offset, len) is across two fragments. Although unlikely, it still need to handle it (dynptr_data returns NULL) properly by using bpf_dynptr_read(). The same that the skb case also needs to handle dynptr_data returning NULL.

Something like Kumar's sample in [0] should work for both xdp and skb dynptr but replace the helpers with bpf_dynptr_{data,read,write}().

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

Quoted 11 lines
Quoted 11 lines

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

for xdp-type progs:
- nothing

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

I wonder if the 'Before' behavior can be kept as is.

The bpf prog that runs in both xdp and bpf should be

typo: both xdp and *skb

Quoted 13 lines

the one always expecting the data-slice will be invalidated and it has to call the bpf_dynptr_data() again after writing. Yes, it is unnecessary for xdp but the bpf prog needs to the same anyway if the verifier was the one enforcing it for both skb and xdp dynptr type.

If the bpf prog is written for xdp alone, then it has no need to re-get the bpf_dynptr_data() after writing.

Quoted 4 lines
Quoted 1 line

There is also the other option: failing to write until you pull skb, which looks a lot better to me if we are adding this helper anyway...

#29 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

In reply to Martin KaFai Lau
Quoted 59 lines · Martin KaFai Lau
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Joanne Koong

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

Quoted 7 lines
Quoted 7 lines

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

I don't think it is necessary (or help) to restrict xdp slice from getting a fragment. In any case, the xdp prog has to deal with the case that bpf_dynptr_data(xdp_dynptr, offset, len) is across two fragments. Although unlikely, it still need to handle it (dynptr_data returns NULL) properly by using bpf_dynptr_read(). The same that the skb case also needs to handle dynptr_data returning NULL.

Something like Kumar's sample in [0] should work for both xdp and skb dynptr but replace the helpers with bpf_dynptr_{data,read,write}().

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

Quoted 8 lines
Quoted 8 lines

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

Quoted 6 lines
Quoted 6 lines

for xdp-type progs:
- nothing

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

I wonder if the 'Before' behavior can be kept as is.

The bpf prog that runs in both xdp and bpf should be the one always expecting the data-slice will be invalidated and it has to call the bpf_dynptr_data() again after writing. Yes, it is unnecessary for xdp but the bpf prog needs to the same anyway if the verifier was the one enforcing it for both skb and xdp dynptr type.

Quoted 3 lines

If the bpf prog is written for xdp alone, then it has no need to re-get the bpf_dynptr_data() after writing.

Yeah, compared to the alternative, I guess it's better how it is right now. It doesn't seem possible to meaningfully hide the differences without penalizing either case. It also wouldn't be good to make it less useful for XDP, since the whole point of this and the previous effort was exposing bpf_xdp_pointer to the user.

#30 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 65 lines · Kumar Kartikeya Dwivedi
Quoted 59 lines · Martin KaFai Lau
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Joanne Koong

I'm more and more liking the idea of limiting xdp to match the constraints of skb given that both you, Kumar, and Jakub have mentioned that portability between xdp and skb would be useful for users :)

What are your thoughts on this API:

1) bpf_dynptr_data()

Before:
for skb-type progs:
- data slices in fragments is not supported

Quoted 7 lines
Quoted 7 lines

for xdp-type progs:
- data slices in fragments is supported as long as it is in a
contiguous frag (eg not across frags)

Now:
for skb + xdp type progs:
- data slices in fragments is not supported

I don't think it is necessary (or help) to restrict xdp slice from getting a fragment. In any case, the xdp prog has to deal with the case that bpf_dynptr_data(xdp_dynptr, offset, len) is across two fragments. Although unlikely, it still need to handle it (dynptr_data returns NULL) properly by using bpf_dynptr_read(). The same that the skb case also needs to handle dynptr_data returning NULL.

Something like Kumar's sample in [0] should work for both xdp and skb dynptr but replace the helpers with bpf_dynptr_{data,read,write}().

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

Quoted 8 lines
Quoted 8 lines

2) bpf_dynptr_write()

Before:
for skb-type progs:
- all data slices are invalidated after a write

Quoted 6 lines
Quoted 6 lines

for xdp-type progs:
- nothing

Now:
for skb + xdp type progs:
- all data slices are invalidated after a write

I wonder if the 'Before' behavior can be kept as is.

The bpf prog that runs in both xdp and bpf should be the one always expecting the data-slice will be invalidated and it has to call the bpf_dynptr_data() again after writing. Yes, it is unnecessary for xdp but the bpf prog needs to the same anyway if the verifier was the one enforcing it for both skb and xdp dynptr type.

Quoted 3 lines

If the bpf prog is written for xdp alone, then it has no need to re-get the bpf_dynptr_data() after writing.

Quoted 5 lines

Yeah, compared to the alternative, I guess it's better how it is right now. It doesn't seem possible to meaningfully hide the differences without penalizing either case. It also wouldn't be good to make it less useful for XDP, since the whole point of this and the previous effort was exposing bpf_xdp_pointer to the user.

I'll keep it as is for v5.

#31 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

In reply to Joanne Koong
Quoted 61 lines · Joanne Koong
Quoted 42 lines · Kumar Kartikeya Dwivedi

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7450

Quoted 7 lines

[...]

                if (func_id == BPF_FUNC_dynptr_data &&
kernel/bpf/verifier.c +2 -1
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

Quoted 2 lines

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

Ooh interesting, I hadn't previously taken a look at try_match_pkt_pointers(), thanks for mentioning it :)

The cleanest solution to me is to add the flag "DYNPTR_TYPE_{SKB/XDP}" to PTR_TO_PACKET and change reg_is_init_pkt_pointer() to return false if the DYNPTR_TYPE_{SKB/XDP} flag is present. I prefer this over returning PTR_TO_MEM because it seems more robust (eg if in the future we reject x behavior on the packet data reg types, this will automatically apply to the data slices), and because it'll keep the logic more efficient/simpler for the case when the pkt pointer has to be cleared after any helper that changes pkt data is called (aka the case where the data slice gets invalidated).

Quoted 2 lines

What are your thoughts?

Thinking more deeply about it, probably not, we need more work here. I remember _now_ why I chose the pkt_uid approach (and this tells us my commit log lacks all the details about the motivation :( ).

Consider how equivalency checking for packet pointers works in regsafe. It is checking type, then if old range > cur range, then offs, etc.

The problem is, while we now don't prune on access to ptr_to_pkt vs ptr_to_pkt | dynptr_pkt types in same reg (since type differs we return false), we still prune if old range of ptr_to_pkt | dynptr_pkt

Quoted 1 line

cur range of ptr_to_pkt | dynptr_pkt. Both could be pointing into

separate frags, so this assumption would be incorrect. I would be able to trick the verifier into accessing data beyond the length of a different frag, by first making sure one line of exploration is verified, and then changing the register in another branch reaching the same branch target. Helpers can take packet pointers so the access can become a pruning point. It would think the rest of the stuff is safe, while they are not equivalent at all. It is ok if they are bit by bit equivalent (same type, range, off, etc.).

If you start returning false whenever you see this type tag set, it will become too conservative (it considers reg copies of the same dynptr_data lookup as distinct). So you need some kind of id assigned during dynptr_data lookup to distinguish them.

#32 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 78 lines · Kumar Kartikeya Dwivedi
Quoted 61 lines · Joanne Koong
Quoted 42 lines · Kumar Kartikeya Dwivedi

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7450

Quoted 7 lines

[...]

                if (func_id == BPF_FUNC_dynptr_data &&
kernel/bpf/verifier.c +2 -1
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

Quoted 2 lines

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

Ooh interesting, I hadn't previously taken a look at try_match_pkt_pointers(), thanks for mentioning it :)

The cleanest solution to me is to add the flag "DYNPTR_TYPE_{SKB/XDP}" to PTR_TO_PACKET and change reg_is_init_pkt_pointer() to return false if the DYNPTR_TYPE_{SKB/XDP} flag is present. I prefer this over returning PTR_TO_MEM because it seems more robust (eg if in the future we reject x behavior on the packet data reg types, this will automatically apply to the data slices), and because it'll keep the logic more efficient/simpler for the case when the pkt pointer has to be cleared after any helper that changes pkt data is called (aka the case where the data slice gets invalidated).

Quoted 2 lines

What are your thoughts?

Thinking more deeply about it, probably not, we need more work here. I remember _now_ why I chose the pkt_uid approach (and this tells us my commit log lacks all the details about the motivation :( ).

Consider how equivalency checking for packet pointers works in regsafe. It is checking type, then if old range > cur range, then offs, etc.

The problem is, while we now don't prune on access to ptr_to_pkt vs ptr_to_pkt | dynptr_pkt types in same reg (since type differs we return false), we still prune if old range of ptr_to_pkt | dynptr_pkt

Quoted 1 line

cur range of ptr_to_pkt | dynptr_pkt. Both could be pointing into

Quoted 8 lines

separate frags, so this assumption would be incorrect. I would be able to trick the verifier into accessing data beyond the length of a different frag, by first making sure one line of exploration is verified, and then changing the register in another branch reaching the same branch target. Helpers can take packet pointers so the access can become a pruning point. It would think the rest of the stuff is safe, while they are not equivalent at all. It is ok if they are bit by bit equivalent (same type, range, off, etc.).

Thanks for the explanation. To clarify, if old range of ptr_to_pkt > cur range of ptr_to_pkt, what gets pruned? Is it access to cur range of ptr_to_pkt since if old range > cur range, then if old range is acceptable cur range must definitely be acceptable?

Quoted 5 lines

If you start returning false whenever you see this type tag set, it will become too conservative (it considers reg copies of the same dynptr_data lookup as distinct). So you need some kind of id assigned during dynptr_data lookup to distinguish them.

What about if the dynptr_pkt type tag is set, then we compare the ranges as well? If the ranges are the same, then we return true, else false. Does that work?

#33 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 90 lines · Joanne Koong
Quoted 78 lines · Kumar Kartikeya Dwivedi
Quoted 61 lines · Joanne Koong
Quoted 42 lines · Kumar Kartikeya Dwivedi

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7450

Quoted 7 lines

[...]

                if (func_id == BPF_FUNC_dynptr_data &&
kernel/bpf/verifier.c +2 -1
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

Quoted 2 lines

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

Ooh interesting, I hadn't previously taken a look at try_match_pkt_pointers(), thanks for mentioning it :)

The cleanest solution to me is to add the flag "DYNPTR_TYPE_{SKB/XDP}" to PTR_TO_PACKET and change reg_is_init_pkt_pointer() to return false if the DYNPTR_TYPE_{SKB/XDP} flag is present. I prefer this over returning PTR_TO_MEM because it seems more robust (eg if in the future we reject x behavior on the packet data reg types, this will automatically apply to the data slices), and because it'll keep the logic more efficient/simpler for the case when the pkt pointer has to be cleared after any helper that changes pkt data is called (aka the case where the data slice gets invalidated).

Quoted 2 lines

What are your thoughts?

Thinking more deeply about it, probably not, we need more work here. I remember _now_ why I chose the pkt_uid approach (and this tells us my commit log lacks all the details about the motivation :( ).

Consider how equivalency checking for packet pointers works in regsafe. It is checking type, then if old range > cur range, then offs, etc.

The problem is, while we now don't prune on access to ptr_to_pkt vs ptr_to_pkt | dynptr_pkt types in same reg (since type differs we return false), we still prune if old range of ptr_to_pkt | dynptr_pkt

Quoted 1 line

cur range of ptr_to_pkt | dynptr_pkt. Both could be pointing into

Quoted 8 lines

separate frags, so this assumption would be incorrect. I would be able to trick the verifier into accessing data beyond the length of a different frag, by first making sure one line of exploration is verified, and then changing the register in another branch reaching the same branch target. Helpers can take packet pointers so the access can become a pruning point. It would think the rest of the stuff is safe, while they are not equivalent at all. It is ok if they are bit by bit equivalent (same type, range, off, etc.).

Quoted 4 lines

Thanks for the explanation. To clarify, if old range of ptr_to_pkt > cur range of ptr_to_pkt, what gets pruned? Is it access to cur range of ptr_to_pkt since if old range > cur range, then if old range is acceptable cur range must definitely be acceptable?

No, my description was bad :).
We return false when old_range > cur_range, i.e. the path is considered safe and not explored again when old_range <= cur_range (pruning), otherwise we continue verifying.
Consider if it was doing pkt[cur_range + 1] access in the path we are about to explore again (already verified for old_range). That is <= old_range, but > cur_range, so it would be problematic if we pruned search for old_range > cur_range.

So maybe it won't be a problem here, and just the current range checks for pkt pointer slices is fine even if they belong to different frags. I didn't craft any test case when writing my previous reply. Especially since you will disable comparisons, one cannot relearn range again using var_off + comparison, which closes another loophole.

It just seems simpler to me to be a bit more conservative, since it is only an optimization. There might be some corner case lurking we can't think of right now. But I leave the judgement up to you if you can reason about it. In either case it would be good to include some comments in the commit log about all this.

Meanwhile, looking at the current code, I'm more inclined to suggest PTR_TO_MEM (and handle invalidation specially), but again, I will leave it up to you to decide.

When we do += var_off to a pkt reg, its range is reset to zero, compared to PTR_TO_MEM, where off + var_off (smin/umax) is used to check against the actual size for an access, which is a bit more flexible. The reason to reset range is that it will be relearned using comparisons and transferred to copies (reg->id is assigned for each += var_off), which doesn't apply to slice pointers (essentially the only reason to keep them is being able to pick them for invalidation), we try to disable the rest of the pkt pointer magic in the verifier, anyway.

pkt_reg->umax_value influences the prog->aux->max_pkt_offset (and iiuc it can reach that point with range == 0 after += var_off, and zero_size_allowed == true), only seems to be used by netronome's ebpf offload for now, but still a bit confusing if slice pkt pointers cause this to change.

Quoted 10 lines
Quoted 6 lines

If you start returning false whenever you see this type tag set, it will become too conservative (it considers reg copies of the same dynptr_data lookup as distinct). So you need some kind of id assigned during dynptr_data lookup to distinguish them.

What about if the dynptr_pkt type tag is set, then we compare the ranges as well? If the ranges are the same, then we return true, else false. Does that work?

Seems like it, and true part is already covered by memcmp at the start of the function, I think.

#34 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 97 lines · Kumar Kartikeya Dwivedi
Quoted 90 lines · Joanne Koong
Quoted 78 lines · Kumar Kartikeya Dwivedi
Quoted 61 lines · Joanne Koong
Quoted 42 lines · Kumar Kartikeya Dwivedi

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7450

Quoted 7 lines

[...]

                if (func_id == BPF_FUNC_dynptr_data &&
kernel/bpf/verifier.c +2 -1
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

Quoted 2 lines

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

Ooh interesting, I hadn't previously taken a look at try_match_pkt_pointers(), thanks for mentioning it :)

The cleanest solution to me is to add the flag "DYNPTR_TYPE_{SKB/XDP}" to PTR_TO_PACKET and change reg_is_init_pkt_pointer() to return false if the DYNPTR_TYPE_{SKB/XDP} flag is present. I prefer this over returning PTR_TO_MEM because it seems more robust (eg if in the future we reject x behavior on the packet data reg types, this will automatically apply to the data slices), and because it'll keep the logic more efficient/simpler for the case when the pkt pointer has to be cleared after any helper that changes pkt data is called (aka the case where the data slice gets invalidated).

Quoted 2 lines

What are your thoughts?

Thinking more deeply about it, probably not, we need more work here. I remember _now_ why I chose the pkt_uid approach (and this tells us my commit log lacks all the details about the motivation :( ).

Consider how equivalency checking for packet pointers works in regsafe. It is checking type, then if old range > cur range, then offs, etc.

The problem is, while we now don't prune on access to ptr_to_pkt vs ptr_to_pkt | dynptr_pkt types in same reg (since type differs we return false), we still prune if old range of ptr_to_pkt | dynptr_pkt

Quoted 1 line

cur range of ptr_to_pkt | dynptr_pkt. Both could be pointing into

Quoted 8 lines

separate frags, so this assumption would be incorrect. I would be able to trick the verifier into accessing data beyond the length of a different frag, by first making sure one line of exploration is verified, and then changing the register in another branch reaching the same branch target. Helpers can take packet pointers so the access can become a pruning point. It would think the rest of the stuff is safe, while they are not equivalent at all. It is ok if they are bit by bit equivalent (same type, range, off, etc.).

Quoted 4 lines

Thanks for the explanation. To clarify, if old range of ptr_to_pkt > cur range of ptr_to_pkt, what gets pruned? Is it access to cur range of ptr_to_pkt since if old range > cur range, then if old range is acceptable cur range must definitely be acceptable?

Quoted 8 lines

No, my description was bad :).
We return false when old_range > cur_range, i.e. the path is considered safe and not explored again when old_range <= cur_range (pruning), otherwise we continue verifying.
Consider if it was doing pkt[cur_range + 1] access in the path we are about to explore again (already verified for old_range). That is <= old_range, but > cur_range, so it would be problematic if we pruned search for old_range > cur_range.

Does "old_range" here refer to the range that was already previously verified as safe by the verifier? And "cur_range" is the new range that we are trying to figure out is safe or not?

When you say "we return false when old_range > cur_range", what function are we returning false from?

Quoted 17 lines

So maybe it won't be a problem here, and just the current range checks for pkt pointer slices is fine even if they belong to different frags. I didn't craft any test case when writing my previous reply. Especially since you will disable comparisons, one cannot relearn range again using var_off + comparison, which closes another loophole.

It just seems simpler to me to be a bit more conservative, since it is only an optimization. There might be some corner case lurking we can't think of right now. But I leave the judgement up to you if you can reason about it. In either case it would be good to include some comments in the commit log about all this.

Meanwhile, looking at the current code, I'm more inclined to suggest PTR_TO_MEM (and handle invalidation specially), but again, I will leave it up to you to decide.

Quoted 5 lines

When we do += var_off to a pkt reg, its range is reset to zero, compared to PTR_TO_MEM, where off + var_off (smin/umax) is used to check against the actual size for an access, which is a bit more flexible. The reason to reset range is that it will be relearned using comparisons and transferred to copies (reg->id is assigned for each +=

Can you direct me to the function where this relearning happens? thanks!

Quoted 10 lines

var_off), which doesn't apply to slice pointers (essentially the only reason to keep them is being able to pick them for invalidation), we try to disable the rest of the pkt pointer magic in the verifier, anyway.

pkt_reg->umax_value influences the prog->aux->max_pkt_offset (and iiuc it can reach that point with range == 0 after += var_off, and zero_size_allowed == true), only seems to be used by netronome's ebpf offload for now, but still a bit confusing if slice pkt pointers cause this to change.

My major takeaway from this discussion is that there's a lot of extra subtleties when reg is PTR_TO_PACKET :) I'm going to delve deeper into the source code, but from a glance, I think you're right that just assigning PTR_TO_MEM for the data slice will probably make things a lot more straightforward. thanks for the discussion!

Quoted 14 lines
Quoted 11 lines
Quoted 7 lines

If you start returning false whenever you see this type tag set, it will become too conservative (it considers reg copies of the same dynptr_data lookup as distinct). So you need some kind of id assigned during dynptr_data lookup to distinguish them.

What about if the dynptr_pkt type tag is set, then we compare the ranges as well? If the ranges are the same, then we return true, else false. Does that work?

Seems like it, and true part is already covered by memcmp at the start of the function, I think.

#35 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

Quoted 109 lines · Joanne Koong
Quoted 97 lines · Kumar Kartikeya Dwivedi
Quoted 90 lines · Joanne Koong
Quoted 78 lines · Kumar Kartikeya Dwivedi
Quoted 61 lines · Joanne Koong
Quoted 42 lines · Kumar Kartikeya Dwivedi

Review

Quoted 7 lines

[...]

                if (func_id == BPF_FUNC_dynptr_data &&
(quoted diff) +2 -1
-                   dynptr_type == BPF_DYNPTR_TYPE_SKB) {
+                   (dynptr_type == BPF_DYNPTR_TYPE_SKB ||
+                    dynptr_type == BPF_DYNPTR_TYPE_XDP)) {
                        regs[BPF_REG_0].type = PTR_TO_PACKET | ret_flag;
                        regs[BPF_REG_0].range = meta.mem_size;

It doesn't seem like this is safe. Since PTR_TO_PACKET's range can be modified by comparisons with packet pointers loaded from the xdp/skb ctx, how do we distinguish e.g. between a pkt slice obtained from some frag in a multi-buff XDP vs pkt pointer from a linear area?

Someone can compare data_meta from ctx with PTR_TO_PACKET from bpf_dynptr_data on xdp dynptr (which might be pointing to a xdp mb frag). While MAX_PACKET_OFF is 0xffff, it can still be used to do OOB access for the linear area. reg_is_init_pkt_pointer will return true as modified range is not considered for it. Same kind of issues when doing comparison with data_end from ctx (though maybe you won't be able to do incorrect data access at runtime using that).

I had a pkt_uid field in my patch [0] which disallowed comparisons among bpf_packet_pointer slices. Each call assigned a fresh pkt_uid, and that disabled comparisons for them. reg->id is used for var_off range propagation so it cannot be reused.

Coming back to this: What we really want here is a PTR_TO_MEM with a mem_size, so maybe you should go that route instead of PTR_TO_PACKET (and add a type tag to maybe pretty print it also as a packet pointer in verifier log), or add some way to distinguish slice vs non-slice pkt pointers like I did in my patch. You might also want to add some tests for this corner case (there are some later in [0] if you want to reuse them).

So TBH, I kinda dislike my own solution in [0] :). The complexity does not seem worth it. The pkt_uid distinction is more useful (and actually would be needed) in Toke's xdp queueing series, where in a dequeue program you have multiple xdp_mds and want scoped slice invalidations (i.e. adjust_head on one xdp_md doesn't invalidate slices of some other xdp_md). Here we can just get away with normal PTR_TO_MEM.

Quoted 2 lines

... Or just let me know if you handle this correctly already, or if this won't be an actual problem :).

Ooh interesting, I hadn't previously taken a look at try_match_pkt_pointers(), thanks for mentioning it :)

The cleanest solution to me is to add the flag "DYNPTR_TYPE_{SKB/XDP}" to PTR_TO_PACKET and change reg_is_init_pkt_pointer() to return false if the DYNPTR_TYPE_{SKB/XDP} flag is present. I prefer this over returning PTR_TO_MEM because it seems more robust (eg if in the future we reject x behavior on the packet data reg types, this will automatically apply to the data slices), and because it'll keep the logic more efficient/simpler for the case when the pkt pointer has to be cleared after any helper that changes pkt data is called (aka the case where the data slice gets invalidated).

Quoted 2 lines

What are your thoughts?

Thinking more deeply about it, probably not, we need more work here. I remember _now_ why I chose the pkt_uid approach (and this tells us my commit log lacks all the details about the motivation :( ).

Consider how equivalency checking for packet pointers works in regsafe. It is checking type, then if old range > cur range, then offs, etc.

The problem is, while we now don't prune on access to ptr_to_pkt vs ptr_to_pkt | dynptr_pkt types in same reg (since type differs we return false), we still prune if old range of ptr_to_pkt | dynptr_pkt

Quoted 1 line

cur range of ptr_to_pkt | dynptr_pkt. Both could be pointing into

Quoted 8 lines

separate frags, so this assumption would be incorrect. I would be able to trick the verifier into accessing data beyond the length of a different frag, by first making sure one line of exploration is verified, and then changing the register in another branch reaching the same branch target. Helpers can take packet pointers so the access can become a pruning point. It would think the rest of the stuff is safe, while they are not equivalent at all. It is ok if they are bit by bit equivalent (same type, range, off, etc.).

Quoted 4 lines

Thanks for the explanation. To clarify, if old range of ptr_to_pkt > cur range of ptr_to_pkt, what gets pruned? Is it access to cur range of ptr_to_pkt since if old range > cur range, then if old range is acceptable cur range must definitely be acceptable?

Quoted 8 lines

No, my description was bad :).
We return false when old_range > cur_range, i.e. the path is considered safe and not explored again when old_range <= cur_range (pruning), otherwise we continue verifying.
Consider if it was doing pkt[cur_range + 1] access in the path we are about to explore again (already verified for old_range). That is <= old_range, but > cur_range, so it would be problematic if we pruned search for old_range > cur_range.

Quoted 3 lines

Does "old_range" here refer to the range that was already previously verified as safe by the verifier? And "cur_range" is the new range that we are trying to figure out is safe or not?

Yes, it's the range of the same reg in the already safe verified state from the point we are about to explore again (and considering 'pruning' the search if the current state would satisfy the safety properties as well).

Quoted 4 lines

When you say "we return false when old_range > cur_range", what function are we returning false from?

We return false from the regsafe function, i.e. it isn't safe if the old range was greater than the current range, otherwise we match other stuff before we return true (like off being equal, and var_off of old being within cur's).

Quoted 17 lines
Quoted 17 lines

So maybe it won't be a problem here, and just the current range checks for pkt pointer slices is fine even if they belong to different frags. I didn't craft any test case when writing my previous reply. Especially since you will disable comparisons, one cannot relearn range again using var_off + comparison, which closes another loophole.

It just seems simpler to me to be a bit more conservative, since it is only an optimization. There might be some corner case lurking we can't think of right now. But I leave the judgement up to you if you can reason about it. In either case it would be good to include some comments in the commit log about all this.

Meanwhile, looking at the current code, I'm more inclined to suggest PTR_TO_MEM (and handle invalidation specially), but again, I will leave it up to you to decide.

Quoted 8 lines
Quoted 5 lines

When we do += var_off to a pkt reg, its range is reset to zero, compared to PTR_TO_MEM, where off + var_off (smin/umax) is used to check against the actual size for an access, which is a bit more flexible. The reason to reset range is that it will be relearned using comparisons and transferred to copies (reg->id is assigned for each +=

Can you direct me to the function where this relearning happens? thanks!

See the code around the comment:
/* something was added to pkt_ptr, set range to zero */ where it memsets the range to 0,
then in adjust_ptr_min_max_vals,
and then you see how find_good_pkt_pointers also takes into account reg->umax_value + off before setting reg->range after your next comparison (because at runtime the variable offset is already added to the pointer).

Quoted 11 lines
Quoted 10 lines

var_off), which doesn't apply to slice pointers (essentially the only reason to keep them is being able to pick them for invalidation), we try to disable the rest of the pkt pointer magic in the verifier, anyway.

pkt_reg->umax_value influences the prog->aux->max_pkt_offset (and iiuc it can reach that point with range == 0 after += var_off, and zero_size_allowed == true), only seems to be used by netronome's ebpf offload for now, but still a bit confusing if slice pkt pointers cause this to change.

Quoted 5 lines

My major takeaway from this discussion is that there's a lot of extra subtleties when reg is PTR_TO_PACKET :) I'm going to delve deeper into the source code, but from a glance, I think you're right that just assigning PTR_TO_MEM for the data slice will probably make things a lot more straightforward. thanks for the discussion!

Exactly. I think this is an internal verifier detail so we can definitely go back and change this later, but IMO we'd avoid a lot of headache if we just choose PTR_TO_MEM, since PTR_TO_PACKET has a lot of special semantics.

Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
Quoted 8 lines
Quoted 4 lines

If you start returning false whenever you see this type tag set, it will become too conservative (it considers reg copies of the same dynptr_data lookup as distinct). So you need some kind of id assigned during dynptr_data lookup to distinguish them.

What about if the dynptr_pkt type tag is set, then we compare the ranges as well? If the ranges are the same, then we return true, else false. Does that work?

Seems like it, and true part is already covered by memcmp at the start of the function, I think.

#36 lore raw

Re: [PATCH bpf-next v4 2/3] bpf: Add xdp dynptrs

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/Add-skb-xdp-dynptrs/20220823-080022
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: microblaze-buildonly-randconfig-r003-20220823 (https://download.01.org/0day-ci/archive/20220824/[email protected]/config)
compiler: microblaze-linux-gcc (GCC) 12.1.0

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/6d832ed17cea3ede1cd48f885a73144d9c4d800a
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/Add-skb-xdp-dynptrs/20220823-080022
git checkout 6d832ed17cea3ede1cd48f885a73144d9c4d800a
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=microblaze SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

microblaze-linux-ld: kernel/bpf/helpers.o: in function `____bpf_dynptr_read':
kernel/bpf/helpers.c:1543: undefined reference to `__bpf_skb_load_bytes'

>> microblaze-linux-ld: kernel/bpf/helpers.c:1545: undefined reference to `__bpf_xdp_load_bytes'
   microblaze-linux-ld: kernel/bpf/helpers.o: in function `____bpf_dynptr_write':
   kernel/bpf/helpers.c:1586: undefined reference to `__bpf_skb_store_bytes'
>> microblaze-linux-ld: kernel/bpf/helpers.c:1591: undefined reference to `__bpf_xdp_store_bytes'

microblaze-linux-ld: kernel/bpf/helpers.o: in function `____bpf_dynptr_data':
kernel/bpf/helpers.c:1653: undefined reference to `bpf_xdp_pointer'
pahole: .tmp_vmlinux.btf: Invalid argument
.btf.vmlinux.bin.o: file not recognized: file format not recognized

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#37 lore raw

PATCH v4 3/3 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, and speed up execution times.

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"), there was a 2x speed-up:
original version: 0.053 sec
with dynptrs: 0.025 sec

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

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t xdp_bpf2bpf"):
original version: 0.0449 sec
with dynptrs: 0.0429 sec

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

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t l4lb_all"):
original version: 0.0502 sec
with dynptrs: 0.055 sec

For number of processed verifier instructions:
original version: 6284 insns
with dynptrs: 2538 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.

5) progs/dynptr_success.c
* Add test case "test_skb_readonly" for testing attempts at writes / data slices on a prog type with read-only skb ctx.

6) progs/dynptr_fail.c
* Add test cases "skb_invalid_data_slice{1,2}" and "xdp_invalid_data_slice" 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 "skb_invalid_write" for testing that read-only skb dynptrs can't be written to through data slices.

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

---
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c | 132 ++-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  85 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |   9 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 111 ++
 .../selftests/bpf/progs/dynptr_success.c      |  29 +
 .../bpf/progs/test_cls_redirect_dynptr.c      | 968 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 468 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 110 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 240 +++++
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 13 files changed, 2255 insertions(+), 44 deletions(-)
 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/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 +90 -42

169 lines not on this page · raw message

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

85 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/xdp_attach.c +7 -2
index 62aa3edda5e6..40d0d61af9e6 100644
@@ -4,11 +4,10 @@
 #define IFINDEX_LO 1
 #define XDP_FLAGS_REPLACE		(1U << 4)
 
-void serial_test_xdp_attach(void)
+static void serial_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.o";
 	struct bpf_prog_info info = {};
 	int err, fd1, fd2, fd3;
 	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
@@ -85,3 +84,9 @@ void serial_test_xdp_attach(void)
 out_1:
 	bpf_object__close(obj1);
 }
+
+void test_xdp_attach(void)
+{
+	serial_test_xdp_attach("./test_xdp.o");
+	serial_test_xdp_attach("./test_xdp_dynptr.o");
+}
tools/testing/selftests/bpf/progs/dynptr_fail.c +111 -0

120 lines not on this page · raw message

tools/testing/selftests/bpf/progs/dynptr_success.c +29 -0
index a3a6103c8569..d0e504d7aae7 100644
@@ -162,3 +162,32 @@ int test_ringbuf(void *ctx)
 	bpf_ringbuf_discard_dynptr(&ptr, 0);
 	return 0;
 }
+
+SEC("cgroup_skb/egress")
+int test_skb_readonly(struct __sk_buff *skb)
+{
+	__u8 write_data[2] = {1, 2};
+	struct bpf_dynptr ptr;
+	__u64 *data;
+	int ret;
+
+	if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
+		err = 1;
+		return 0;
+	}
+
+	data = bpf_dynptr_data(&ptr, 0, sizeof(*data));
+	if (!data) {
+		err = 2;
+		return 0;
+	}
+
+	/* since cgroup skbs are read only, writes should fail */
+	ret = bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data), 0);
+	if (ret != -EINVAL) {
+		err = 3;
+		return 0;
+	}
+
+	return 0;
+}
tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c new file +968 -0

968 lines not on this page · raw message

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

468 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 +110 -0

110 lines not on this page · raw message

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

240 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.30.2
Acked-by on v4 3/3
#38 lore raw

Re: [PATCH bpf-next v4 3/3] selftests/bpf: tests for using dynptrs to parse skb and xdp buffers

Quoted 78 lines · 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, and speed up execution times.

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"), there was a 2x speed-up:
original version: 0.053 sec
with dynptrs: 0.025 sec

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

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t xdp_bpf2bpf"):
original version: 0.0449 sec
with dynptrs: 0.0429 sec

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

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t l4lb_all"):
original version: 0.0502 sec
with dynptrs: 0.055 sec

For number of processed verifier instructions:
original version: 6284 insns
with dynptrs: 2538 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.

5) progs/dynptr_success.c
* Add test case "test_skb_readonly" for testing attempts at writes / data slices on a prog type with read-only skb ctx.

6) progs/dynptr_fail.c
* Add test cases "skb_invalid_data_slice{1,2}" and "xdp_invalid_data_slice" 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 "skb_invalid_write" for testing that read-only skb dynptrs can't be written to through data slices.

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

---
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c | 132 ++-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  85 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |   9 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 111 ++
 .../selftests/bpf/progs/dynptr_success.c      |  29 +
 .../bpf/progs/test_cls_redirect_dynptr.c      | 968 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 468 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 110 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 240 +++++
Quoted 9 lines
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 13 files changed, 2255 insertions(+), 44 deletions(-)
 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

Massive work on adding lots of selftests, thanks! Left few nits, but looks good anyways:

[...]

tools/testing/selftests/bpf/prog_tests/dynptr.c line 61

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +7 -4
-       /* success cases */
-       {"test_read_write", NULL},
-       {"test_data_slice", NULL},
-       {"test_ringbuf", NULL},
+               "Unsupported reg type fp for bpf_dynptr_from_mem data", SETUP_NONE},
+       {"skb_invalid_data_slice1", "invalid mem access 'scalar'", SETUP_NONE},
+       {"skb_invalid_data_slice2", "invalid mem access 'scalar'", SETUP_NONE},
+       {"xdp_invalid_data_slice", "invalid mem access 'scalar'", SETUP_NONE},
+       {"skb_invalid_ctx", "unknown func bpf_dynptr_from_skb", SETUP_NONE},
+       {"xdp_invalid_ctx", "unknown func bpf_dynptr_from_xdp", SETUP_NONE},
+       {"skb_invalid_write", "cannot write into packet", SETUP_NONE},

nit: given SETUP_NONE is zero, you can just leave it out to make this table a bit cleaner; bit no big deal having it explicitly as well

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +6 -0
+
+       /* these tests should be run and should succeed */
+       {"test_read_write", NULL, SETUP_SYSCALL_SLEEP},
+       {"test_data_slice", NULL, SETUP_SYSCALL_SLEEP},
+       {"test_ringbuf", NULL, SETUP_SYSCALL_SLEEP},
+       {"test_skb_readonly", NULL, SETUP_SKB_PROG},
 };

[...]

Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c new file +48 -0
+static void test_parsing(bool use_dynptr)
+{
+       char buf[128];
+       struct bpf_program *prog;
+       void *skel_ptr;
+       int err;
+
+       LIBBPF_OPTS(bpf_test_run_opts, topts,
+                   .data_in = &pkt,
+                   .data_size_in = sizeof(pkt),
+                   .data_out = buf,
+                   .data_size_out = sizeof(buf),
+                   .repeat = 3,
+       );
+
+       if (use_dynptr) {
+               struct test_parse_tcp_hdr_opt_dynptr *skel;
+
+               skel = test_parse_tcp_hdr_opt_dynptr__open_and_load();
+               if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+                       return;
+
+               pkt.options[6] = skel->rodata->tcp_hdr_opt_kind_tpr;
+               prog = skel->progs.xdp_ingress_v6;
+               skel_ptr = skel;
+       } else {
+               struct test_parse_tcp_hdr_opt *skel;
+
+               skel = test_parse_tcp_hdr_opt__open_and_load();
+               if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+                       return;
+
+               pkt.options[6] = skel->rodata->tcp_hdr_opt_kind_tpr;
+               prog = skel->progs.xdp_ingress_v6;
+               skel_ptr = skel;
+       }
+
+       err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
+       ASSERT_OK(err, "ipv6 test_run");
+       ASSERT_EQ(topts.retval, XDP_PASS, "ipv6 test_run retval");
+
+       if (use_dynptr) {
+               struct test_parse_tcp_hdr_opt_dynptr *skel = skel_ptr;
+
+               ASSERT_EQ(skel->bss->server_id, 0x9000000, "server id");
+               test_parse_tcp_hdr_opt_dynptr__destroy(skel);
+       } else {
+               struct test_parse_tcp_hdr_opt *skel = skel_ptr;

tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c line 84

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c new file +10 -0
+
+               ASSERT_EQ(skel->bss->server_id, 0x9000000, "server id");
+               test_parse_tcp_hdr_opt__destroy(skel);
+       }
+}
+
+void test_parse_tcp_hdr_opt(void)
+{
+       test_parsing(false);
+       test_parsing(true);

given this false/true argument is very non-descriptive and you basically only share few lines of code inside test_parsing, why not have two dedicated test_parsing_wo_dynptr and test_parsing_with_dynptr? And probably makes sense to make those two as two subtests?

Quoted 19 lines
(quoted diff) +1 -0
+}
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +1 -2
index 62aa3edda5e6..40d0d61af9e6 100644
@@ -4,11 +4,10 @@
 #define IFINDEX_LO 1
 #define XDP_FLAGS_REPLACE              (1U << 4)
 
-void serial_test_xdp_attach(void)
+static void serial_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.o";
        struct bpf_prog_info info = {};
        int err, fd1, fd2, fd3;
        LIBBPF_OPTS(bpf_xdp_attach_opts, opts);

tools/testing/selftests/bpf/prog_tests/xdp_attach.c void serial_test_xdp_attach(void) · line 91

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +5 -0
@@ -85,3 +84,9 @@ void serial_test_xdp_attach(void)
 out_1:
        bpf_object__close(obj1);
 }
+
+void test_xdp_attach(void)
+{
+       serial_test_xdp_attach("./test_xdp.o");
+       serial_test_xdp_attach("./test_xdp_dynptr.o");

nit: make into subtests?

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +1 -0
+}

[...]

Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +15 -0
+/* The data slice is invalidated whenever a helper changes packet data */
+SEC("?xdp")
+int xdp_invalid_data_slice(struct xdp_md *xdp)
+{
+       struct bpf_dynptr ptr;
+       struct ethhdr *hdr;
+
+       bpf_dynptr_from_xdp(xdp, 0, &ptr);
+       hdr = bpf_dynptr_data(&ptr, 0, sizeof(*hdr));
+       if (!hdr)
+               return SK_DROP;
+
+       hdr->h_proto = 9;
+
+       if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(*hdr)))

tools/testing/selftests/bpf/prog_tests/xdp_attach.c

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +10 -0
+               return XDP_DROP;
+
+       /* this should fail */
+       hdr->h_proto = 1;
+
+       return XDP_PASS;
+}
+
+/* Only supported prog type can create skb-type dynptrs */
+SEC("?raw_tp/sys_nanosleep")

nit: there is no sys_nanosleep raw tracepoint, is there? Just SEC("?raw_tp") maybe, like you did in recent refactoring?

Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +22 -0
+int skb_invalid_ctx(void *ctx)
+{
+       struct bpf_dynptr ptr;
+
+       /* this should fail */
+       bpf_dynptr_from_skb(ctx, 0, &ptr);
+
+       return 0;
+}
+
+/* Only supported prog type can create xdp-type dynptrs */
+SEC("?raw_tp/sys_nanosleep")
+int xdp_invalid_ctx(void *ctx)
+{
+       struct bpf_dynptr ptr;
+
+       /* this should fail */
+       bpf_dynptr_from_xdp(ctx, 0, &ptr);
+
+       return 0;
+}
+

[...]

Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +14 -0
+
+/* Global metrics, per CPU
+ */
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, unsigned int);
+       __type(value, metrics_t);
+} metrics_map SEC(".maps");
+
+static metrics_t *get_global_metrics(void)
+{
+       uint64_t key = 0;
+       return bpf_map_lookup_elem(&metrics_map, &key);

tools/testing/selftests/bpf/prog_tests/xdp_attach.c

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +10 -0
+}
+
+static ret_t accept_locally(struct __sk_buff *skb, encap_headers_t *encap)
+{
+       const int payload_off =
+               sizeof(*encap) +
+               sizeof(struct in_addr) * encap->unigue.hop_count;
+       int32_t encap_overhead = payload_off - sizeof(struct ethhdr);
+
+       // Changing the ethertype if the encapsulated packet is ipv6

nit: could be copy/paste from original, but let's not add C++ comments?

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +12 -0
+       if (encap->gue.proto_ctype == IPPROTO_IPV6)
+               encap->eth.h_proto = bpf_htons(ETH_P_IPV6);
+
+       if (bpf_skb_adjust_room(skb, -encap_overhead, BPF_ADJ_ROOM_MAC,
+                               BPF_F_ADJ_ROOM_FIXED_GSO |
+                               BPF_F_ADJ_ROOM_NO_CSUM_RESET) ||
+           bpf_csum_level(skb, BPF_CSUM_LEVEL_DEC))
+               return TC_ACT_SHOT;
+
+       return bpf_redirect(skb->ifindex, BPF_F_INGRESS);
+}
+

[...]

tools/testing/selftests/bpf/prog_tests/xdp_attach.c

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +13 -0
+       iph->version = 4;
+       iph->ihl = iphdr_sz >> 2;
+       iph->frag_off = 0;
+       iph->protocol = IPPROTO_IPIP;
+       iph->check = 0;
+       iph->tos = 0;
+       iph->tot_len = bpf_htons(payload_len + iphdr_sz);
+       iph->daddr = tnl->daddr.v4;
+       iph->saddr = tnl->saddr.v4;
+       iph->ttl = 8;
+
+       next_iph = (__u16 *)iph;
+#pragma clang loop unroll(full)

nit: probably don't need unroll?

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +9 -0
+       for (i = 0; i < iphdr_sz >> 1; i++)
+               csum += *next_iph++;
+
+       iph->check = ~((csum & 0xffff) + (csum >> 16));
+
+       count_tx(vip.protocol);
+
+       return XDP_TX;
+}

[...]

#39 lore raw
In reply to Joanne Koong
Quoted 10 lines · Joanne Koong

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)

Just curious: so would you be adding a dynptr interface for obtaining data_meta slices as well in the future? Since the same manual bounds checking is needed for data_meta vs data. How would that look in the generic dynptr interface of data/read/write this set is trying to fit things in?

Quoted 10 lines

When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs for the more simple cases, there is no noticeable difference. For the more complex cases where lengths are non-statically known at compile time, there can be a significant speed-up when using dynptrs (eg a 2x speed up for cls redirection). Patch 3 contains more details as well as examples of how to use skb and xdp dynptrs.

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

--

#40 lore raw
Quoted 13 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines · Joanne Koong

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)

Quoted 5 lines

Just curious: so would you be adding a dynptr interface for obtaining data_meta slices as well in the future? Since the same manual bounds checking is needed for data_meta vs data. How would that look in the generic dynptr interface of data/read/write this set is trying to fit things in?

Oh cool, I didn't realize there is also a data_meta used in packet parsing - thanks for bringing this up. I think there are 2 options for how data_meta can be incorporated into the dynptr interface:

1) have a separate api "bpf_dynptr_from_{skb/xdp}_meta. We'll have to have a function in the verifier that does something similar to 'may_access_direct_pkt_data' but for pkt data meta, since skb progs can have different access restrictions for data vs. data_meta.

2) ideally, the flags arg would be used to indicate whether the parsing should be for data_meta. To support this though, I think we'd need to do access type checking within the helper instead of at the verifier level. One idea is to pass in the env->ops ptr as a 4th arg (manually patching it from the verifier) to the helper, which can be used to determine if data_meta access is permitted.

In both options, there'll be a new BPF_DYNPTR_{SKB/XDP}_META dynptr type and data/read/write will be supported for it.

What are your thoughts?

Quoted 13 lines
Quoted 13 lines

When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs for the more simple cases, there is no noticeable difference. For the more complex cases where lengths are non-statically known at compile time, there can be a significant speed-up when using dynptrs (eg a 2x speed up for cls redirection). Patch 3 contains more details as well as examples of how to use skb and xdp dynptrs.

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

--

#41 lore raw
Quoted 38 lines · Joanne Koong
Quoted 13 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines · Joanne Koong

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)

Quoted 5 lines

Just curious: so would you be adding a dynptr interface for obtaining data_meta slices as well in the future? Since the same manual bounds checking is needed for data_meta vs data. How would that look in the generic dynptr interface of data/read/write this set is trying to fit things in?

Oh cool, I didn't realize there is also a data_meta used in packet parsing - thanks for bringing this up. I think there are 2 options for how data_meta can be incorporated into the dynptr interface:

1) have a separate api "bpf_dynptr_from_{skb/xdp}_meta. We'll have to have a function in the verifier that does something similar to 'may_access_direct_pkt_data' but for pkt data meta, since skb progs can have different access restrictions for data vs. data_meta.

2) ideally, the flags arg would be used to indicate whether the parsing should be for data_meta. To support this though, I think we'd need to do access type checking within the helper instead of at the verifier level. One idea is to pass in the env->ops ptr as a 4th arg (manually patching it from the verifier) to the helper, which can be used to determine if data_meta access is permitted.

Quoted 4 lines

In both options, there'll be a new BPF_DYNPTR_{SKB/XDP}_META dynptr type and data/read/write will be supported for it.

What are your thoughts?

I think separate bpf_dynptr_from_skb_meta() and bpf_dynptr_from_xdp_meta() is cleaner than a flag. Also having a separate helper would make it easier to disable this helper for program types that don't have access to ctx->data_meta, right?

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs for the more simple cases, there is no noticeable difference. For the more complex cases where lengths are non-statically known at compile time, there can be a significant speed-up when using dynptrs (eg a 2x speed up for cls redirection). Patch 3 contains more details as well as examples of how to use skb and xdp dynptrs.

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

--

#42 lore raw
Quoted 43 lines · Andrii Nakryiko
Quoted 41 lines · Joanne Koong
Quoted 13 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines · Joanne Koong

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)

Quoted 5 lines

Just curious: so would you be adding a dynptr interface for obtaining data_meta slices as well in the future? Since the same manual bounds checking is needed for data_meta vs data. How would that look in the generic dynptr interface of data/read/write this set is trying to fit things in?

Oh cool, I didn't realize there is also a data_meta used in packet parsing - thanks for bringing this up. I think there are 2 options for how data_meta can be incorporated into the dynptr interface:

1) have a separate api "bpf_dynptr_from_{skb/xdp}_meta. We'll have to have a function in the verifier that does something similar to 'may_access_direct_pkt_data' but for pkt data meta, since skb progs can have different access restrictions for data vs. data_meta.

2) ideally, the flags arg would be used to indicate whether the parsing should be for data_meta. To support this though, I think we'd need to do access type checking within the helper instead of at the verifier level. One idea is to pass in the env->ops ptr as a 4th arg (manually patching it from the verifier) to the helper, which can be used to determine if data_meta access is permitted.

In both options, there'll be a new BPF_DYNPTR_{SKB/XDP}_META dynptr type and data/read/write will be supported for it.

Quoted 7 lines
Quoted 1 line

What are your thoughts?

I think separate bpf_dynptr_from_skb_meta() and bpf_dynptr_from_xdp_meta() is cleaner than a flag. Also having a separate helper would make it easier to disable this helper for program types that don't have access to ctx->data_meta, right?

Agreed, and with flags then you also need to force them to be constant (to be able to distinguish the return type from the flag's value), which might be too restrictive.