All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access()
@ 2022-03-07  5:29 Shung-Hsi Yu
  2022-03-07 17:07 ` Yonghong Song
  2022-03-08 14:50 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Shung-Hsi Yu @ 2022-03-07  5:29 UTC (permalink / raw)
  To: bpf, linux-kernel
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev

Instead of determining buf_info string in the caller of
check_buffer_access(), we can determine whether the register type is read-only
through type_is_rdonly_mem() helper inside check_buffer_access() and construct
buf_info, making the code slightly cleaner.

Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
---

Initially I tried to remove the buf_info argument from
__check_buffer_access(), however check_tp_buffer_access() uses "tracepoint"
(rather than the usual "rdonly"/"rdwr") as it's buf_info, thus I decide to
leave __check_buffer_access() as-is, and only change check_buffer_access()
instead.

 kernel/bpf/verifier.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index a39eedecc93a..518238029e46 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -4047,9 +4047,9 @@ static int check_buffer_access(struct bpf_verifier_env *env,
 			       const struct bpf_reg_state *reg,
 			       int regno, int off, int size,
 			       bool zero_size_allowed,
-			       const char *buf_info,
 			       u32 *max_access)
 {
+	const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr";
 	int err;
 
 	err = __check_buffer_access(env, buf_info, reg, regno, off, size);
@@ -4543,7 +4543,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 					      value_regno);
 	} else if (base_type(reg->type) == PTR_TO_BUF) {
 		bool rdonly_mem = type_is_rdonly_mem(reg->type);
-		const char *buf_info;
 		u32 *max_access;
 
 		if (rdonly_mem) {
@@ -4552,15 +4551,13 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 					regno, reg_type_str(env, reg->type));
 				return -EACCES;
 			}
-			buf_info = "rdonly";
 			max_access = &env->prog->aux->max_rdonly_access;
 		} else {
-			buf_info = "rdwr";
 			max_access = &env->prog->aux->max_rdwr_access;
 		}
 
 		err = check_buffer_access(env, reg, regno, off, size, false,
-					  buf_info, max_access);
+					  max_access);
 
 		if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ))
 			mark_reg_unknown(env, regs, value_regno);
@@ -4823,7 +4820,6 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
 				   struct bpf_call_arg_meta *meta)
 {
 	struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
-	const char *buf_info;
 	u32 *max_access;
 
 	switch (base_type(reg->type)) {
@@ -4850,15 +4846,13 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
 			if (meta && meta->raw_mode)
 				return -EACCES;
 
-			buf_info = "rdonly";
 			max_access = &env->prog->aux->max_rdonly_access;
 		} else {
-			buf_info = "rdwr";
 			max_access = &env->prog->aux->max_rdwr_access;
 		}
 		return check_buffer_access(env, reg, regno, reg->off,
 					   access_size, zero_size_allowed,
-					   buf_info, max_access);
+					   max_access);
 	case PTR_TO_STACK:
 		return check_stack_range_initialized(
 				env,
-- 
2.35.1


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

* Re: [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access()
  2022-03-07  5:29 [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access() Shung-Hsi Yu
@ 2022-03-07 17:07 ` Yonghong Song
  2022-03-08 14:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Yonghong Song @ 2022-03-07 17:07 UTC (permalink / raw)
  To: Shung-Hsi Yu, bpf, linux-kernel
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, John Fastabend, KP Singh, netdev



On 3/6/22 9:29 PM, Shung-Hsi Yu wrote:
> Instead of determining buf_info string in the caller of
> check_buffer_access(), we can determine whether the register type is read-only
> through type_is_rdonly_mem() helper inside check_buffer_access() and construct
> buf_info, making the code slightly cleaner.
> 
> Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>

LGTM. Thanks.

Acked-by: Yonghong Song <yhs@fb.com>

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

* Re: [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access()
  2022-03-07  5:29 [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access() Shung-Hsi Yu
  2022-03-07 17:07 ` Yonghong Song
@ 2022-03-08 14:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-03-08 14:50 UTC (permalink / raw)
  To: Shung-Hsi Yu
  Cc: bpf, linux-kernel, ast, daniel, andrii, kafai, songliubraving,
	yhs, john.fastabend, kpsingh, netdev

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Mon, 7 Mar 2022 13:29:18 +0800 you wrote:
> Instead of determining buf_info string in the caller of
> check_buffer_access(), we can determine whether the register type is read-only
> through type_is_rdonly_mem() helper inside check_buffer_access() and construct
> buf_info, making the code slightly cleaner.
> 
> Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
> 
> [...]

Here is the summary with links:
  - [bpf-next] bpf: determine buf_info inside check_buffer_access()
    https://git.kernel.org/bpf/bpf-next/c/44e9a741cad8

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] 3+ messages in thread

end of thread, other threads:[~2022-03-08 14:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-07  5:29 [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access() Shung-Hsi Yu
2022-03-07 17:07 ` Yonghong Song
2022-03-08 14:50 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.