Skip to contentSkip to conversation

bpf: allow wide (u64) aligned stores for some fields of bpf_sock_addr

PATCH Superseded Stanislav Fomichev 4 participants 11 messages Jun 28, 2019 – Jul 1, 2019

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bpf: allow wide (u64) aligned stores for some fields of bpf_sock_addr

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

 #  17: (18) r2 = 0x100000000000000
 #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
 #  19: (7b) *(u64 *)(r1 +16) = r2
 #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing to do, so let's support it on the kernel side.

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")
---
 include/linux/filter.h |  6 ++++++
 net/core/filter.c      | 22 ++++++++++++++--------
 2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
 	return size <= size_default && (size & (size - 1)) == 0;
 }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)			\
+	(size == sizeof(__u64) &&					\
+	off >= offsetof(type, field) &&					\
+	off + sizeof(__u64) <= offsetofend(type, field) &&		\
+	off % sizeof(__u64) == 0)
+
 #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
 			if (!bpf_ctx_narrow_access_ok(off, size, size_default))
 				return false;
 		} else {
+			if (bpf_ctx_wide_store_ok(off, size,
+						  struct bpf_sock_addr,
+						  user_ip6))
+				return true;
+
+			if (bpf_ctx_wide_store_ok(off, size,
+						  struct bpf_sock_addr,
+						  msg_src_ip6))
+				return true;
+
 			if (size != size_default)
 				return false;
 		}
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
  * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
  *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
  * In addition it uses Temporary Field TF (member of struct S) as the 3rd
  * "register" since two registers available in convert_ctx_access are not
  * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  * instructions. But we need a temporary place to save pointer to nested
  * structure whose field we want to store to.
  */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)		       \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)	       \
 	do {								       \
 		int tmp_reg = BPF_REG_9;				       \
 		if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)	       \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
 				      offsetof(S, TF));			       \
 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,	       \
 				      si->dst_reg, offsetof(S, F));	       \
-		*insn++ = BPF_STX_MEM(					       \
-			BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,	       \
+		*insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,	       \
 			bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),	       \
 				       target_size)			       \
 				+ OFF);					       \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
 						      TF)		       \
 	do {								       \
 		if (type == BPF_WRITE) {				       \
-			SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-							 TF);		       \
+			SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+							 OFF, TF);	       \
 		} else {						       \
 			SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(		       \
 				S, NS, F, NF, SIZE, OFF);  \
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#1 lore raw

PATCH 2/2 selftests/bpf: add verifier tests for wide stores

Make sure that wide stores are allowed at proper (aligned) addresses. Note that user_ip6 is naturally aligned on 8-byte boundary, so correct addresses are user_ip6[0] and user_ip6[2]. msg_src_ip6 is, however, aligned on a 4-byte bondary, so only msg_src_ip6[1] can be wide-stored.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
---
 tools/testing/selftests/bpf/test_verifier.c   | 17 ++++++--
 .../selftests/bpf/verifier/wide_store.c       | 40 +++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c
tools/testing/selftests/bpf/test_verifier.c +14 -3
index c5514daf8865..b0773291012a 100644
@@ -105,6 +105,7 @@ struct bpf_test {
 			__u64 data64[TEST_DATA_LEN / 8];
 		};
 	} retvals[MAX_TEST_RUNS];
+	enum bpf_attach_type expected_attach_type;
 };
 
 /* Note we want this to be 64 bit aligned so that the end of our array is
@@ -850,6 +851,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
 	int fd_prog, expected_ret, alignment_prevented_execution;
 	int prog_len, prog_type = test->prog_type;
 	struct bpf_insn *prog = test->insns;
+	struct bpf_load_program_attr attr;
 	int run_errs, run_successes;
 	int map_fds[MAX_NR_MAPS];
 	const char *expected_err;
@@ -881,8 +883,17 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
 		pflags |= BPF_F_STRICT_ALIGNMENT;
 	if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
 		pflags |= BPF_F_ANY_ALIGNMENT;
-	fd_prog = bpf_verify_program(prog_type, prog, prog_len, pflags,
-				     "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 4);
+
+	memset(&attr, 0, sizeof(attr));
+	attr.prog_type = prog_type;
+	attr.expected_attach_type = test->expected_attach_type;
+	attr.insns = prog;
+	attr.insns_cnt = prog_len;
+	attr.license = "GPL";
+	attr.log_level = 4;
+	attr.prog_flags = pflags;
+
+	fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
 	if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) {
 		printf("SKIP (unsupported program type %d)\n", prog_type);
 		skips++;
@@ -912,7 +923,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
 			printf("FAIL\nUnexpected success to load!\n");
 			goto fail_log;
 		}
-		if (!strstr(bpf_vlog, expected_err)) {
+		if (!expected_err || !strstr(bpf_vlog, expected_err)) {
 			printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
 			      expected_err, bpf_vlog);
 			goto fail_log;
tools/testing/selftests/bpf/verifier/wide_store.c new file +40 -0
new file mode 100644
index 000000000000..c6385f45b114
@@ -0,0 +1,40 @@
+#define BPF_SOCK_ADDR(field, off, res, err) \
+{ \
+	"wide store to bpf_sock_addr." #field "[" #off "]", \
+	.insns = { \
+	BPF_MOV64_IMM(BPF_REG_0, 1), \
+	BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
+		    offsetof(struct bpf_sock_addr, field[off])), \
+	BPF_EXIT_INSN(), \
+	}, \
+	.result = res, \
+	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
+	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
+	.errstr = err, \
+}
+
+/* user_ip6[0] is u64 aligned */
+BPF_SOCK_ADDR(user_ip6, 0, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(user_ip6, 1, REJECT,
+	      "invalid bpf_context access off=12 size=8"),
+BPF_SOCK_ADDR(user_ip6, 2, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(user_ip6, 3, REJECT,
+	      "invalid bpf_context access off=20 size=8"),
+BPF_SOCK_ADDR(user_ip6, 4, REJECT,
+	      "invalid bpf_context access off=24 size=8"),
+
+/* msg_src_ip6[0] is _not_ u64 aligned */
+BPF_SOCK_ADDR(msg_src_ip6, 0, REJECT,
+	      "invalid bpf_context access off=44 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 1, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(msg_src_ip6, 2, REJECT,
+	      "invalid bpf_context access off=52 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 3, REJECT,
+	      "invalid bpf_context access off=56 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 4, REJECT,
+	      "invalid bpf_context access off=60 size=8"),
+
+#undef BPF_SOCK_ADDR
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#2 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add verifier tests for wide stores

Quoted 86 lines · Stanislav Fomichev

Make sure that wide stores are allowed at proper (aligned) addresses. Note that user_ip6 is naturally aligned on 8-byte boundary, so correct addresses are user_ip6[0] and user_ip6[2]. msg_src_ip6 is, however, aligned on a 4-byte bondary, so only msg_src_ip6[1] can be wide-stored.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
---
  tools/testing/selftests/bpf/test_verifier.c   | 17 ++++++--
  .../selftests/bpf/verifier/wide_store.c       | 40 +++++++++++++++++++
  2 files changed, 54 insertions(+), 3 deletions(-)

create mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c

tools/testing/selftests/bpf/test_verifier.c +14 -3
index c5514daf8865..b0773291012a 100644
@@ -105,6 +105,7 @@ struct bpf_test {
  			__u64 data64[TEST_DATA_LEN / 8];
  		};
  	} retvals[MAX_TEST_RUNS];
+	enum bpf_attach_type expected_attach_type;
  };
 
  /* Note we want this to be 64 bit aligned so that the end of our array is
@@ -850,6 +851,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
  	int fd_prog, expected_ret, alignment_prevented_execution;
  	int prog_len, prog_type = test->prog_type;
  	struct bpf_insn *prog = test->insns;
+	struct bpf_load_program_attr attr;
  	int run_errs, run_successes;
  	int map_fds[MAX_NR_MAPS];
  	const char *expected_err;
@@ -881,8 +883,17 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
  		pflags |= BPF_F_STRICT_ALIGNMENT;
  	if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
  		pflags |= BPF_F_ANY_ALIGNMENT;
-	fd_prog = bpf_verify_program(prog_type, prog, prog_len, pflags,
-				     "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 4);
+
+	memset(&attr, 0, sizeof(attr));
+	attr.prog_type = prog_type;
+	attr.expected_attach_type = test->expected_attach_type;
+	attr.insns = prog;
+	attr.insns_cnt = prog_len;
+	attr.license = "GPL";
+	attr.log_level = 4;
+	attr.prog_flags = pflags;
+
+	fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
  	if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) {
  		printf("SKIP (unsupported program type %d)\n", prog_type);
  		skips++;
@@ -912,7 +923,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
  			printf("FAIL\nUnexpected success to load!\n");
  			goto fail_log;
  		}
-		if (!strstr(bpf_vlog, expected_err)) {
+		if (!expected_err || !strstr(bpf_vlog, expected_err)) {
  			printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
  			      expected_err, bpf_vlog);
  			goto fail_log;
tools/testing/selftests/bpf/verifier/wide_store.c new file +16 -0
new file mode 100644
index 000000000000..c6385f45b114
@@ -0,0 +1,40 @@
+#define BPF_SOCK_ADDR(field, off, res, err) \
+{ \
+	"wide store to bpf_sock_addr." #field "[" #off "]", \
+	.insns = { \
+	BPF_MOV64_IMM(BPF_REG_0, 1), \
+	BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
+		    offsetof(struct bpf_sock_addr, field[off])), \
+	BPF_EXIT_INSN(), \
+	}, \
+	.result = res, \
+	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
+	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
+	.errstr = err, \
+}
+
+/* user_ip6[0] is u64 aligned */

tools/testing/selftests/bpf/verifier/wide_store.c line 26

Quoted 10 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +10 -0
+BPF_SOCK_ADDR(user_ip6, 0, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(user_ip6, 1, REJECT,
+	      "invalid bpf_context access off=12 size=8"),
+BPF_SOCK_ADDR(user_ip6, 2, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(user_ip6, 3, REJECT,
+	      "invalid bpf_context access off=20 size=8"),
+BPF_SOCK_ADDR(user_ip6, 4, REJECT,
+	      "invalid bpf_context access off=24 size=8"),

With offset 4, we have
#968/p wide store to bpf_sock_addr.user_ip6[4] OK

This test case can be removed. user code typically won't write bpf_sock_addr.user_ip6[4], and compiler typically will give a warning since it is out of array bound. Any particular reason you want to include this one?

tools/testing/selftests/bpf/verifier/wide_store.c line 38

Quoted 12 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +12 -0
+
+/* msg_src_ip6[0] is _not_ u64 aligned */
+BPF_SOCK_ADDR(msg_src_ip6, 0, REJECT,
+	      "invalid bpf_context access off=44 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 1, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(msg_src_ip6, 2, REJECT,
+	      "invalid bpf_context access off=52 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 3, REJECT,
+	      "invalid bpf_context access off=56 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 4, REJECT,
+	      "invalid bpf_context access off=60 size=8"),

The same as above, offset=4 case can be removed?

Quoted 3 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +2 -0
+
+#undef BPF_SOCK_ADDR
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add verifier tests for wide stores

Quoted 104 lines · Yonghong Song
Quoted 86 lines · Stanislav Fomichev

Make sure that wide stores are allowed at proper (aligned) addresses. Note that user_ip6 is naturally aligned on 8-byte boundary, so correct addresses are user_ip6[0] and user_ip6[2]. msg_src_ip6 is, however, aligned on a 4-byte bondary, so only msg_src_ip6[1] can be wide-stored.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
---
  tools/testing/selftests/bpf/test_verifier.c   | 17 ++++++--
  .../selftests/bpf/verifier/wide_store.c       | 40 +++++++++++++++++++
  2 files changed, 54 insertions(+), 3 deletions(-)

create mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c

tools/testing/selftests/bpf/test_verifier.c +14 -3
index c5514daf8865..b0773291012a 100644
@@ -105,6 +105,7 @@ struct bpf_test {
                      __u64 data64[TEST_DATA_LEN / 8];
              };
      } retvals[MAX_TEST_RUNS];
+     enum bpf_attach_type expected_attach_type;
  };
 
  /* Note we want this to be 64 bit aligned so that the end of our array is
@@ -850,6 +851,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
      int fd_prog, expected_ret, alignment_prevented_execution;
      int prog_len, prog_type = test->prog_type;
      struct bpf_insn *prog = test->insns;
+     struct bpf_load_program_attr attr;
      int run_errs, run_successes;
      int map_fds[MAX_NR_MAPS];
      const char *expected_err;
@@ -881,8 +883,17 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
              pflags |= BPF_F_STRICT_ALIGNMENT;
      if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
              pflags |= BPF_F_ANY_ALIGNMENT;
-     fd_prog = bpf_verify_program(prog_type, prog, prog_len, pflags,
-                                  "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 4);
+
+     memset(&attr, 0, sizeof(attr));
+     attr.prog_type = prog_type;
+     attr.expected_attach_type = test->expected_attach_type;
+     attr.insns = prog;
+     attr.insns_cnt = prog_len;
+     attr.license = "GPL";
+     attr.log_level = 4;
+     attr.prog_flags = pflags;
+
+     fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
      if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) {
              printf("SKIP (unsupported program type %d)\n", prog_type);
              skips++;
@@ -912,7 +923,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
                      printf("FAIL\nUnexpected success to load!\n");
                      goto fail_log;
              }
-             if (!strstr(bpf_vlog, expected_err)) {
+             if (!expected_err || !strstr(bpf_vlog, expected_err)) {
                      printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
                            expected_err, bpf_vlog);
                      goto fail_log;
tools/testing/selftests/bpf/verifier/wide_store.c new file +16 -0
new file mode 100644
index 000000000000..c6385f45b114
@@ -0,0 +1,40 @@
+#define BPF_SOCK_ADDR(field, off, res, err) \
+{ \
+     "wide store to bpf_sock_addr." #field "[" #off "]", \
+     .insns = { \
+     BPF_MOV64_IMM(BPF_REG_0, 1), \
+     BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
+                 offsetof(struct bpf_sock_addr, field[off])), \
+     BPF_EXIT_INSN(), \
+     }, \
+     .result = res, \
+     .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
+     .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
+     .errstr = err, \
+}
+
+/* user_ip6[0] is u64 aligned */

tools/testing/selftests/bpf/verifier/wide_store.c line 26

Quoted 10 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +10 -0
+BPF_SOCK_ADDR(user_ip6, 0, ACCEPT,
+           NULL),
+BPF_SOCK_ADDR(user_ip6, 1, REJECT,
+           "invalid bpf_context access off=12 size=8"),
+BPF_SOCK_ADDR(user_ip6, 2, ACCEPT,
+           NULL),
+BPF_SOCK_ADDR(user_ip6, 3, REJECT,
+           "invalid bpf_context access off=20 size=8"),
+BPF_SOCK_ADDR(user_ip6, 4, REJECT,
+           "invalid bpf_context access off=24 size=8"),

With offset 4, we have
#968/p wide store to bpf_sock_addr.user_ip6[4] OK

Quoted 5 lines

This test case can be removed. user code typically won't write bpf_sock_addr.user_ip6[4], and compiler typically will give a warning since it is out of array bound. Any particular reason you want to include this one?

I agree, user_ip6[4] is essentially 8-byte write to user_port field.

Quoted 16 lines
Quoted 4 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +2 -0
+
+/* msg_src_ip6[0] is _not_ u64 aligned */

tools/testing/selftests/bpf/verifier/wide_store.c line 38

Quoted 10 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +10 -0
+BPF_SOCK_ADDR(msg_src_ip6, 0, REJECT,
+           "invalid bpf_context access off=44 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 1, ACCEPT,
+           NULL),
+BPF_SOCK_ADDR(msg_src_ip6, 2, REJECT,
+           "invalid bpf_context access off=52 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 3, REJECT,
+           "invalid bpf_context access off=56 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 4, REJECT,
+           "invalid bpf_context access off=60 size=8"),

The same as above, offset=4 case can be removed?

And this one is a write into a struct hole, which should be rejected even without wide-store check, right?

Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +2 -0
+
+#undef BPF_SOCK_ADDR
#4 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add verifier tests for wide stores

In reply to Yonghong Song
Quoted 103 lines · Yonghong Song
Quoted 86 lines · Stanislav Fomichev

Make sure that wide stores are allowed at proper (aligned) addresses. Note that user_ip6 is naturally aligned on 8-byte boundary, so correct addresses are user_ip6[0] and user_ip6[2]. msg_src_ip6 is, however, aligned on a 4-byte bondary, so only msg_src_ip6[1] can be wide-stored.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
---
  tools/testing/selftests/bpf/test_verifier.c   | 17 ++++++--
  .../selftests/bpf/verifier/wide_store.c       | 40 +++++++++++++++++++
  2 files changed, 54 insertions(+), 3 deletions(-)

create mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c

tools/testing/selftests/bpf/test_verifier.c +14 -3
index c5514daf8865..b0773291012a 100644
@@ -105,6 +105,7 @@ struct bpf_test {
  			__u64 data64[TEST_DATA_LEN / 8];
  		};
  	} retvals[MAX_TEST_RUNS];
+	enum bpf_attach_type expected_attach_type;
  };
 
  /* Note we want this to be 64 bit aligned so that the end of our array is
@@ -850,6 +851,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
  	int fd_prog, expected_ret, alignment_prevented_execution;
  	int prog_len, prog_type = test->prog_type;
  	struct bpf_insn *prog = test->insns;
+	struct bpf_load_program_attr attr;
  	int run_errs, run_successes;
  	int map_fds[MAX_NR_MAPS];
  	const char *expected_err;
@@ -881,8 +883,17 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
  		pflags |= BPF_F_STRICT_ALIGNMENT;
  	if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
  		pflags |= BPF_F_ANY_ALIGNMENT;
-	fd_prog = bpf_verify_program(prog_type, prog, prog_len, pflags,
-				     "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 4);
+
+	memset(&attr, 0, sizeof(attr));
+	attr.prog_type = prog_type;
+	attr.expected_attach_type = test->expected_attach_type;
+	attr.insns = prog;
+	attr.insns_cnt = prog_len;
+	attr.license = "GPL";
+	attr.log_level = 4;
+	attr.prog_flags = pflags;
+
+	fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
  	if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) {
  		printf("SKIP (unsupported program type %d)\n", prog_type);
  		skips++;
@@ -912,7 +923,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
  			printf("FAIL\nUnexpected success to load!\n");
  			goto fail_log;
  		}
-		if (!strstr(bpf_vlog, expected_err)) {
+		if (!expected_err || !strstr(bpf_vlog, expected_err)) {
  			printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
  			      expected_err, bpf_vlog);
  			goto fail_log;
tools/testing/selftests/bpf/verifier/wide_store.c new file +16 -0
new file mode 100644
index 000000000000..c6385f45b114
@@ -0,0 +1,40 @@
+#define BPF_SOCK_ADDR(field, off, res, err) \
+{ \
+	"wide store to bpf_sock_addr." #field "[" #off "]", \
+	.insns = { \
+	BPF_MOV64_IMM(BPF_REG_0, 1), \
+	BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
+		    offsetof(struct bpf_sock_addr, field[off])), \
+	BPF_EXIT_INSN(), \
+	}, \
+	.result = res, \
+	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
+	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
+	.errstr = err, \
+}
+
+/* user_ip6[0] is u64 aligned */

tools/testing/selftests/bpf/verifier/wide_store.c line 26

Quoted 10 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +10 -0
+BPF_SOCK_ADDR(user_ip6, 0, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(user_ip6, 1, REJECT,
+	      "invalid bpf_context access off=12 size=8"),
+BPF_SOCK_ADDR(user_ip6, 2, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(user_ip6, 3, REJECT,
+	      "invalid bpf_context access off=20 size=8"),
+BPF_SOCK_ADDR(user_ip6, 4, REJECT,
+	      "invalid bpf_context access off=24 size=8"),

With offset 4, we have
#968/p wide store to bpf_sock_addr.user_ip6[4] OK

Quoted 5 lines

This test case can be removed. user code typically won't write bpf_sock_addr.user_ip6[4], and compiler typically will give a warning since it is out of array bound. Any particular reason you want to include this one?

Agreed on both, I'm being overly cautious here. They should be caught by the outer switch and be rejected because of other reasons.

Quoted 18 lines

tools/testing/selftests/bpf/verifier/wide_store.c line 38

Quoted 12 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +12 -0
+
+/* msg_src_ip6[0] is _not_ u64 aligned */
+BPF_SOCK_ADDR(msg_src_ip6, 0, REJECT,
+	      "invalid bpf_context access off=44 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 1, ACCEPT,
+	      NULL),
+BPF_SOCK_ADDR(msg_src_ip6, 2, REJECT,
+	      "invalid bpf_context access off=52 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 3, REJECT,
+	      "invalid bpf_context access off=56 size=8"),
+BPF_SOCK_ADDR(msg_src_ip6, 4, REJECT,
+	      "invalid bpf_context access off=60 size=8"),

The same as above, offset=4 case can be removed?

Quoted 3 lines
tools/testing/selftests/bpf/verifier/wide_store.c new file +2 -0
+
+#undef BPF_SOCK_ADDR
Acked-by on v1 1/2
#5 lore raw
In reply to Stanislav Fomichev
Quoted 12 lines · Stanislav Fomichev

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

  #  17: (18) r2 = 0x100000000000000
  #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
  #  19: (7b) *(u64 *)(r1 +16) = r2
  #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing
to do, so let's support it on the kernel side.

Quoted 8 lines

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")

The change looks good to me with the following nits:
1. could you add a cover letter for the patch set? typically if the number of patches is more than one, it would be a good practice with a cover letter. See bpf_devel_QA.rst .
2. with this change, the comments in uapi bpf.h
are not accurate any more.
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order.

*/
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order. */
now for stores, aligned 8-byte write is permitted. could you update this as well?

From the typical usage pattern, I did not see a need
for 8-tye read of user_ip6 and msg_src_ip6 yet. So let us just deal with write for now.

With the above two nits,

Quoted 84 lines
---
  include/linux/filter.h |  6 ++++++
  net/core/filter.c      | 22 ++++++++++++++--------
  2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
  	return size <= size_default && (size & (size - 1)) == 0;
  }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)			\
+	(size == sizeof(__u64) &&					\
+	off >= offsetof(type, field) &&					\
+	off + sizeof(__u64) <= offsetofend(type, field) &&		\
+	off % sizeof(__u64) == 0)
+
  #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
  static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
  			if (!bpf_ctx_narrow_access_ok(off, size, size_default))
  				return false;
  		} else {
+			if (bpf_ctx_wide_store_ok(off, size,
+						  struct bpf_sock_addr,
+						  user_ip6))
+				return true;
+
+			if (bpf_ctx_wide_store_ok(off, size,
+						  struct bpf_sock_addr,
+						  msg_src_ip6))
+				return true;
+
  			if (size != size_default)
  				return false;
  		}
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
   * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
   *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
   * In addition it uses Temporary Field TF (member of struct S) as the 3rd
   * "register" since two registers available in convert_ctx_access are not
   * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
   * instructions. But we need a temporary place to save pointer to nested
   * structure whose field we want to store to.
   */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)		       \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)	       \
  	do {								       \
  		int tmp_reg = BPF_REG_9;				       \
  		if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)	       \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  				      offsetof(S, TF));			       \
  		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,	       \
  				      si->dst_reg, offsetof(S, F));	       \
-		*insn++ = BPF_STX_MEM(					       \
-			BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,	       \
+		*insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,	       \
  			bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),	       \
  				       target_size)			       \
  				+ OFF);					       \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  						      TF)		       \
  	do {								       \
  		if (type == BPF_WRITE) {				       \
-			SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-							 TF);		       \
+			SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+							 OFF, TF);	       \
  		} else {						       \
  			SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(		       \
  				S, NS, F, NF, SIZE, OFF);  \
#6 lore raw
Quoted 42 lines · Yonghong Song
Quoted 12 lines · Stanislav Fomichev

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

  #  17: (18) r2 = 0x100000000000000
  #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
  #  19: (7b) *(u64 *)(r1 +16) = r2
  #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing
to do, so let's support it on the kernel side.

Quoted 8 lines

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")

The change looks good to me with the following nits:
1. could you add a cover letter for the patch set? typically if the number of patches is more than one, it would be a good practice with a cover letter. See bpf_devel_QA.rst .
2. with this change, the comments in uapi bpf.h
are not accurate any more.
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order.

*/
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order. */
now for stores, aligned 8-byte write is permitted. could you update this as well?

Quoted 3 lines

From the typical usage pattern, I did not see a need
for 8-tye read of user_ip6 and msg_src_ip6 yet. So let us just deal with write for now.

But I guess it's still possible for clang to optimize two consecutive 4-byte reads into single 8-byte read in some circumstances? If that's the case, maybe it's a good idea to have corresponding read checks as well?

But overall this looks good to me:

Quoted 88 lines

With the above two nits,

Quoted 84 lines
---
  include/linux/filter.h |  6 ++++++
  net/core/filter.c      | 22 ++++++++++++++--------
  2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
      return size <= size_default && (size & (size - 1)) == 0;
  }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)                        \
+     (size == sizeof(__u64) &&                                       \
+     off >= offsetof(type, field) &&                                 \
+     off + sizeof(__u64) <= offsetofend(type, field) &&              \
+     off % sizeof(__u64) == 0)
+
  #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
  static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
                      if (!bpf_ctx_narrow_access_ok(off, size, size_default))
                              return false;
              } else {
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               user_ip6))
+                             return true;
+
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               msg_src_ip6))
+                             return true;
+
                      if (size != size_default)
                              return false;
              }
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
   * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
   *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
   * In addition it uses Temporary Field TF (member of struct S) as the 3rd
   * "register" since two registers available in convert_ctx_access are not
   * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
   * instructions. But we need a temporary place to save pointer to nested
   * structure whose field we want to store to.
   */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)                     \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)               \
      do {                                                                   \
              int tmp_reg = BPF_REG_9;                                       \
              if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)          \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                    offsetof(S, TF));                        \
              *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,         \
                                    si->dst_reg, offsetof(S, F));            \
-             *insn++ = BPF_STX_MEM(                                         \
-                     BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,        \
+             *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,              \
                      bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),           \
                                     target_size)                            \
                              + OFF);                                        \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                                    TF)                      \
      do {                                                                   \
              if (type == BPF_WRITE) {                                       \
-                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-                                                      TF);                  \
+                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+                                                      OFF, TF);             \
              } else {                                                       \
                      SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(                  \
                              S, NS, F, NF, SIZE, OFF);  \
#7 lore raw
Quoted 43 lines · Andrii Nakryiko
Quoted 42 lines · Yonghong Song
Quoted 12 lines · Stanislav Fomichev

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

  #  17: (18) r2 = 0x100000000000000
  #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
  #  19: (7b) *(u64 *)(r1 +16) = r2
  #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing
to do, so let's support it on the kernel side.

Quoted 8 lines

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")

The change looks good to me with the following nits:
1. could you add a cover letter for the patch set? typically if the number of patches is more than one, it would be a good practice with a cover letter. See bpf_devel_QA.rst .
2. with this change, the comments in uapi bpf.h
are not accurate any more.
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order.

*/
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order. */
now for stores, aligned 8-byte write is permitted. could you update this as well?

Quoted 8 lines
Quoted 3 lines

From the typical usage pattern, I did not see a need
for 8-tye read of user_ip6 and msg_src_ip6 yet. So let us just deal with write for now.

But I guess it's still possible for clang to optimize two consecutive 4-byte reads into single 8-byte read in some circumstances? If that's the case, maybe it's a good idea to have corresponding read checks as well?

I guess clang can do those kinds of optimizations. I can put it on my todo and address later (or when we actually see it out in the wild).

Quoted 3 lines

But overall this looks good to me:

Thanks for a review!

Quoted 88 lines
Quoted 88 lines

With the above two nits,

Quoted 84 lines
---
  include/linux/filter.h |  6 ++++++
  net/core/filter.c      | 22 ++++++++++++++--------
  2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
      return size <= size_default && (size & (size - 1)) == 0;
  }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)                        \
+     (size == sizeof(__u64) &&                                       \
+     off >= offsetof(type, field) &&                                 \
+     off + sizeof(__u64) <= offsetofend(type, field) &&              \
+     off % sizeof(__u64) == 0)
+
  #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
  static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
                      if (!bpf_ctx_narrow_access_ok(off, size, size_default))
                              return false;
              } else {
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               user_ip6))
+                             return true;
+
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               msg_src_ip6))
+                             return true;
+
                      if (size != size_default)
                              return false;
              }
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
   * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
   *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
   * In addition it uses Temporary Field TF (member of struct S) as the 3rd
   * "register" since two registers available in convert_ctx_access are not
   * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
   * instructions. But we need a temporary place to save pointer to nested
   * structure whose field we want to store to.
   */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)                     \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)               \
      do {                                                                   \
              int tmp_reg = BPF_REG_9;                                       \
              if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)          \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                    offsetof(S, TF));                        \
              *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,         \
                                    si->dst_reg, offsetof(S, F));            \
-             *insn++ = BPF_STX_MEM(                                         \
-                     BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,        \
+             *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,              \
                      bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),           \
                                     target_size)                            \
                              + OFF);                                        \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                                    TF)                      \
      do {                                                                   \
              if (type == BPF_WRITE) {                                       \
-                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-                                                      TF);                  \
+                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+                                                      OFF, TF);             \
              } else {                                                       \
                      SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(                  \
                              S, NS, F, NF, SIZE, OFF);  \
#8 lore raw
Quoted 48 lines · Stanislav Fomichev
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Yonghong Song
Quoted 12 lines · Stanislav Fomichev

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

   #  17: (18) r2 = 0x100000000000000
   #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
   #  19: (7b) *(u64 *)(r1 +16) = r2
   #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing
to do, so let's support it on the kernel side.

Quoted 8 lines

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")

The change looks good to me with the following nits:
1. could you add a cover letter for the patch set? typically if the number of patches is more than one, it would be a good practice with a cover letter. See bpf_devel_QA.rst .
2. with this change, the comments in uapi bpf.h
are not accurate any more.
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order.

*/
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order. */
now for stores, aligned 8-byte write is permitted. could you update this as well?

From the typical usage pattern, I did not see a need
for 8-tye read of user_ip6 and msg_src_ip6 yet. So let us just deal with write for now.

Quoted 6 lines
Quoted 4 lines

But I guess it's still possible for clang to optimize two consecutive 4-byte reads into single 8-byte read in some circumstances? If that's the case, maybe it's a good idea to have corresponding read checks as well?

I guess clang can do those kinds of optimizations. I can put it on my todo and address later (or when we actually see it out in the wild).

Okay, I find a Facebook internal app. does trying to read the 4 bytes and compare to a predefined loopback address. We may need to handle read cases as well. But this can be a followup after actual tryout.

Quoted 94 lines
Quoted 4 lines

But overall this looks good to me:

Thanks for a review!

Quoted 88 lines
Quoted 88 lines

With the above two nits,

Quoted 84 lines
---
   include/linux/filter.h |  6 ++++++
   net/core/filter.c      | 22 ++++++++++++++--------
   2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
       return size <= size_default && (size & (size - 1)) == 0;
   }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)                        \
+     (size == sizeof(__u64) &&                                       \
+     off >= offsetof(type, field) &&                                 \
+     off + sizeof(__u64) <= offsetofend(type, field) &&              \
+     off % sizeof(__u64) == 0)
+
   #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
   static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
                       if (!bpf_ctx_narrow_access_ok(off, size, size_default))
                               return false;
               } else {
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               user_ip6))
+                             return true;
+
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               msg_src_ip6))
+                             return true;
+
                       if (size != size_default)
                               return false;
               }
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
   /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
    * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
    *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
    * In addition it uses Temporary Field TF (member of struct S) as the 3rd
    * "register" since two registers available in convert_ctx_access are not
    * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
    * instructions. But we need a temporary place to save pointer to nested
    * structure whose field we want to store to.
    */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)                     \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)               \
       do {                                                                   \
               int tmp_reg = BPF_REG_9;                                       \
               if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)          \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                     offsetof(S, TF));                        \
               *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,         \
                                     si->dst_reg, offsetof(S, F));            \
-             *insn++ = BPF_STX_MEM(                                         \
-                     BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,        \
+             *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,              \
                       bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),           \
                                      target_size)                            \
                               + OFF);                                        \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                                     TF)                      \
       do {                                                                   \
               if (type == BPF_WRITE) {                                       \
-                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-                                                      TF);                  \
+                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+                                                      OFF, TF);             \
               } else {                                                       \
                       SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(                  \
                               S, NS, F, NF, SIZE, OFF);  \
#9 lore raw
Quoted 55 lines · Yonghong Song
Quoted 52 lines · Stanislav Fomichev
Quoted 51 lines · Andrii Nakryiko
Quoted 42 lines · Yonghong Song
Quoted 12 lines · Stanislav Fomichev

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

   #  17: (18) r2 = 0x100000000000000
   #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
   #  19: (7b) *(u64 *)(r1 +16) = r2
   #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing
to do, so let's support it on the kernel side.

Quoted 8 lines

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")

The change looks good to me with the following nits:
1. could you add a cover letter for the patch set? typically if the number of patches is more than one, it would be a good practice with a cover letter. See bpf_devel_QA.rst .
2. with this change, the comments in uapi bpf.h
are not accurate any more.
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order.

*/
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order. */
now for stores, aligned 8-byte write is permitted. could you update this as well?

Quoted 3 lines

From the typical usage pattern, I did not see a need
for 8-tye read of user_ip6 and msg_src_ip6 yet. So let us just deal with write for now.

But I guess it's still possible for clang to optimize two consecutive 4-byte reads into single 8-byte read in some circumstances? If that's the case, maybe it's a good idea to have corresponding read checks as well?

Quoted 6 lines
Quoted 2 lines

I guess clang can do those kinds of optimizations. I can put it on my todo and address later (or when we actually see it out in the wild).

Okay, I find a Facebook internal app. does trying to read the 4 bytes and compare to a predefined loopback address. We may need to handle read cases as well. But this can be a followup after actual tryout.

Sounds good, will follow up on that.

Quoted 94 lines
Quoted 94 lines
Quoted 4 lines

But overall this looks good to me:

Thanks for a review!

Quoted 88 lines
Quoted 88 lines

With the above two nits,

Quoted 84 lines
---
   include/linux/filter.h |  6 ++++++
   net/core/filter.c      | 22 ++++++++++++++--------
   2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
       return size <= size_default && (size & (size - 1)) == 0;
   }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)                        \
+     (size == sizeof(__u64) &&                                       \
+     off >= offsetof(type, field) &&                                 \
+     off + sizeof(__u64) <= offsetofend(type, field) &&              \
+     off % sizeof(__u64) == 0)
+
   #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
   static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
                       if (!bpf_ctx_narrow_access_ok(off, size, size_default))
                               return false;
               } else {
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               user_ip6))
+                             return true;
+
+                     if (bpf_ctx_wide_store_ok(off, size,
+                                               struct bpf_sock_addr,
+                                               msg_src_ip6))
+                             return true;
+
                       if (size != size_default)
                               return false;
               }
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
   /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
    * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
    *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
    * In addition it uses Temporary Field TF (member of struct S) as the 3rd
    * "register" since two registers available in convert_ctx_access are not
    * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
    * instructions. But we need a temporary place to save pointer to nested
    * structure whose field we want to store to.
    */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)                     \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)               \
       do {                                                                   \
               int tmp_reg = BPF_REG_9;                                       \
               if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)          \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                     offsetof(S, TF));                        \
               *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,         \
                                     si->dst_reg, offsetof(S, F));            \
-             *insn++ = BPF_STX_MEM(                                         \
-                     BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,        \
+             *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,              \
                       bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),           \
                                      target_size)                            \
                               + OFF);                                        \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
                                                     TF)                      \
       do {                                                                   \
               if (type == BPF_WRITE) {                                       \
-                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-                                                      TF);                  \
+                     SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+                                                      OFF, TF);             \
               } else {                                                       \
                       SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(                  \
                               S, NS, F, NF, SIZE, OFF);  \
#10 lore raw
In reply to Yonghong Song
Quoted 41 lines · Yonghong Song
Quoted 12 lines · Stanislav Fomichev

Since commit cd17d7770578 ("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones:

  #  17: (18) r2 = 0x100000000000000
  #  ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2);
  #  19: (7b) *(u64 *)(r1 +16) = r2
  #  invalid bpf_context access off=16 size=8

From the compiler point of view it does look like a correct thing
to do, so let's support it on the kernel side.

Quoted 8 lines

Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok.

Cc Andrii Nakryiko <[email protected]>
Cc Yonghong Song <[email protected]>
Fixes cd17d7770578 ("bpf/tools: sync bpf.h")

The change looks good to me with the following nits:
1. could you add a cover letter for the patch set? typically if the number of patches is more than one, it would be a good practice with a cover letter. See bpf_devel_QA.rst .
2. with this change, the comments in uapi bpf.h
are not accurate any more.
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order.

*/
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
* Stored in network byte order. */
now for stores, aligned 8-byte write is permitted. could you update this as well?

Quoted 6 lines

From the typical usage pattern, I did not see a need
for 8-tye read of user_ip6 and msg_src_ip6 yet. So let us just deal with write for now.

With the above two nits,

Thank you for a review, will follow up with a v2 shortly with both things addressed!

Quoted 84 lines
Quoted 84 lines
---
  include/linux/filter.h |  6 ++++++
  net/core/filter.c      | 22 ++++++++++++++--------
  2 files changed, 20 insertions(+), 8 deletions(-)
include/linux/filter.h +6 -0
index 340f7d648974..3901007e36f1 100644
@@ -746,6 +746,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
  	return size <= size_default && (size & (size - 1)) == 0;
  }
 
+#define bpf_ctx_wide_store_ok(off, size, type, field)			\
+	(size == sizeof(__u64) &&					\
+	off >= offsetof(type, field) &&					\
+	off + sizeof(__u64) <= offsetofend(type, field) &&		\
+	off % sizeof(__u64) == 0)
+
  #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
 
  static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
net/core/filter.c +14 -8
index dc8534be12fc..5d33f2146dab 100644
@@ -6849,6 +6849,16 @@ static bool sock_addr_is_valid_access(int off, int size,
  			if (!bpf_ctx_narrow_access_ok(off, size, size_default))
  				return false;
  		} else {
+			if (bpf_ctx_wide_store_ok(off, size,
+						  struct bpf_sock_addr,
+						  user_ip6))
+				return true;
+
+			if (bpf_ctx_wide_store_ok(off, size,
+						  struct bpf_sock_addr,
+						  msg_src_ip6))
+				return true;
+
  			if (size != size_default)
  				return false;
  		}
@@ -7689,9 +7699,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
   * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
   *
- * It doesn't support SIZE argument though since narrow stores are not
- * supported for now.
- *
   * In addition it uses Temporary Field TF (member of struct S) as the 3rd
   * "register" since two registers available in convert_ctx_access are not
   * enough: we can't override neither SRC, since it contains value to store, nor
@@ -7699,7 +7706,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
   * instructions. But we need a temporary place to save pointer to nested
   * structure whose field we want to store to.
   */
-#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF)		       \
+#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF)	       \
  	do {								       \
  		int tmp_reg = BPF_REG_9;				       \
  		if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg)	       \
@@ -7710,8 +7717,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  				      offsetof(S, TF));			       \
  		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg,	       \
  				      si->dst_reg, offsetof(S, F));	       \
-		*insn++ = BPF_STX_MEM(					       \
-			BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg,	       \
+		*insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg,	       \
  			bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF),	       \
  				       target_size)			       \
  				+ OFF);					       \
@@ -7723,8 +7729,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
  						      TF)		       \
  	do {								       \
  		if (type == BPF_WRITE) {				       \
-			SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF,    \
-							 TF);		       \
+			SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE,   \
+							 OFF, TF);	       \
  		} else {						       \
  			SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(		       \
  				S, NS, F, NF, SIZE, OFF);  \