BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps
@ 2025-11-28  6:32 Anton Protopopov
  2025-11-28  6:32 ` [PATCH bpf-next 1/2] bpf: force BPF_F_RDONLY_PROG on insn array creation Anton Protopopov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Anton Protopopov @ 2025-11-28  6:32 UTC (permalink / raw)
  To: bpf; +Cc: Anton Protopopov

Two fixes suggested by Alexei in [1]. Resending as a series,
as the second patch depends on the first.

  [1] https://lore.kernel.org/bpf/CAADnVQK3piReoo1ja=9hgz7aJ60Y_Jjur_JMOaYV8-Mn_VyE4A@mail.gmail.com/#R

Anton Protopopov (2):
  bpf: force BPF_F_RDONLY_PROG on insn array creation
  bpf: check for insn arrays in check_ptr_alignment

 kernel/bpf/bpf_insn_array.c                   |  3 +++
 kernel/bpf/verifier.c                         | 19 +++++++++----------
 .../selftests/bpf/progs/verifier_gotox.c      |  2 +-
 3 files changed, 13 insertions(+), 11 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 1/2] bpf: force BPF_F_RDONLY_PROG on insn array creation
  2025-11-28  6:32 [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps Anton Protopopov
@ 2025-11-28  6:32 ` Anton Protopopov
  2025-11-28  6:32 ` [PATCH bpf-next 2/2] bpf: check for insn arrays in check_ptr_alignment Anton Protopopov
  2025-11-28 23:20 ` [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Anton Protopopov @ 2025-11-28  6:32 UTC (permalink / raw)
  To: bpf; +Cc: Anton Protopopov, Alexei Starovoitov

The original implementation added a hack to check_mem_access()
to prevent programs from writing into insn arrays. To get rid
of this hack, enforce BPF_F_RDONLY_PROG on map creation.

Also fix the corresponding selftest, as the error message changes
with this patch.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
 kernel/bpf/bpf_insn_array.c                        |  3 +++
 kernel/bpf/verifier.c                              | 13 ++++++-------
 tools/testing/selftests/bpf/progs/verifier_gotox.c |  2 +-
 3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c
index 61ce52882632..c96630cb75bf 100644
--- a/kernel/bpf/bpf_insn_array.c
+++ b/kernel/bpf/bpf_insn_array.c
@@ -55,6 +55,9 @@ static struct bpf_map *insn_array_alloc(union bpf_attr *attr)
 
 	bpf_map_init_from_attr(&insn_array->map, attr);
 
+	/* BPF programs aren't allowed to write to the map */
+	insn_array->map.map_flags |= BPF_F_RDONLY_PROG;
+
 	return &insn_array->map;
 }
 
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d865848b789d..58f99557ba38 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -7565,11 +7565,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 			verbose(env, "R%d leaks addr into map\n", value_regno);
 			return -EACCES;
 		}
-		if (t == BPF_WRITE && insn_array) {
-			verbose(env, "writes into insn_array not allowed\n");
-			return -EACCES;
-		}
-
 		err = check_map_access_type(env, regno, off, size, t);
 		if (err)
 			return err;
@@ -7584,10 +7579,14 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 		} else if (t == BPF_READ && value_regno >= 0) {
 			struct bpf_map *map = reg->map_ptr;
 
-			/* if map is read-only, track its contents as scalars */
+			/*
+			 * If map is read-only, track its contents as scalars,
+			 * unless it is an insn array (see the special case below)
+			 */
 			if (tnum_is_const(reg->var_off) &&
 			    bpf_map_is_rdonly(map) &&
-			    map->ops->map_direct_value_addr) {
+			    map->ops->map_direct_value_addr &&
+			    map->map_type != BPF_MAP_TYPE_INSN_ARRAY) {
 				int map_off = off + reg->var_off.value;
 				u64 val = 0;
 
diff --git a/tools/testing/selftests/bpf/progs/verifier_gotox.c b/tools/testing/selftests/bpf/progs/verifier_gotox.c
index 536c9f3e2170..607dad058ca1 100644
--- a/tools/testing/selftests/bpf/progs/verifier_gotox.c
+++ b/tools/testing/selftests/bpf/progs/verifier_gotox.c
@@ -244,7 +244,7 @@ jt0_%=:								\
 }
 
 SEC("socket")
-__failure __msg("writes into insn_array not allowed")
+__failure __msg("write into map forbidden, value_size=16 off=8 size=8")
 __naked void jump_table_no_writes(void)
 {
 	asm volatile ("						\
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 2/2] bpf: check for insn arrays in check_ptr_alignment
  2025-11-28  6:32 [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps Anton Protopopov
  2025-11-28  6:32 ` [PATCH bpf-next 1/2] bpf: force BPF_F_RDONLY_PROG on insn array creation Anton Protopopov
@ 2025-11-28  6:32 ` Anton Protopopov
  2025-11-28 23:20 ` [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Anton Protopopov @ 2025-11-28  6:32 UTC (permalink / raw)
  To: bpf; +Cc: Anton Protopopov, Alexei Starovoitov

Do not abuse the strict_alignment_once flag, and check if the map is
an instruction array inside the check_ptr_alignment() function.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
 kernel/bpf/verifier.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 58f99557ba38..ddc68273d29f 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6482,6 +6482,8 @@ static int check_ptr_alignment(struct bpf_verifier_env *env,
 		break;
 	case PTR_TO_MAP_VALUE:
 		pointer_desc = "value ";
+		if (reg->map_ptr->map_type == BPF_MAP_TYPE_INSN_ARRAY)
+			strict = true;
 		break;
 	case PTR_TO_CTX:
 		pointer_desc = "context ";
@@ -7529,8 +7531,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 {
 	struct bpf_reg_state *regs = cur_regs(env);
 	struct bpf_reg_state *reg = regs + regno;
-	bool insn_array = reg->type == PTR_TO_MAP_VALUE &&
-			  reg->map_ptr->map_type == BPF_MAP_TYPE_INSN_ARRAY;
 	int size, err = 0;
 
 	size = bpf_size_to_bytes(bpf_size);
@@ -7538,7 +7538,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 		return size;
 
 	/* alignment checks will add in reg->off themselves */
-	err = check_ptr_alignment(env, reg, off, size, strict_alignment_once || insn_array);
+	err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
 	if (err)
 		return err;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps
  2025-11-28  6:32 [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps Anton Protopopov
  2025-11-28  6:32 ` [PATCH bpf-next 1/2] bpf: force BPF_F_RDONLY_PROG on insn array creation Anton Protopopov
  2025-11-28  6:32 ` [PATCH bpf-next 2/2] bpf: check for insn arrays in check_ptr_alignment Anton Protopopov
@ 2025-11-28 23:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-11-28 23:20 UTC (permalink / raw)
  To: Anton Protopopov; +Cc: bpf

Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Fri, 28 Nov 2025 06:32:22 +0000 you wrote:
> Two fixes suggested by Alexei in [1]. Resending as a series,
> as the second patch depends on the first.
> 
>   [1] https://lore.kernel.org/bpf/CAADnVQK3piReoo1ja=9hgz7aJ60Y_Jjur_JMOaYV8-Mn_VyE4A@mail.gmail.com/#R
> 
> Anton Protopopov (2):
>   bpf: force BPF_F_RDONLY_PROG on insn array creation
>   bpf: check for insn arrays in check_ptr_alignment
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/2] bpf: force BPF_F_RDONLY_PROG on insn array creation
    https://git.kernel.org/bpf/bpf-next/c/7feff23cdf2e
  - [bpf-next,2/2] bpf: check for insn arrays in check_ptr_alignment
    https://git.kernel.org/bpf/bpf-next/c/e3ea26add687

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-11-28 23:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-28  6:32 [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps Anton Protopopov
2025-11-28  6:32 ` [PATCH bpf-next 1/2] bpf: force BPF_F_RDONLY_PROG on insn array creation Anton Protopopov
2025-11-28  6:32 ` [PATCH bpf-next 2/2] bpf: check for insn arrays in check_ptr_alignment Anton Protopopov
2025-11-28 23:20 ` [PATCH bpf-next 0/2] A pair of follow ups for indirect jumps patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox