linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Luis Gerhorst <luis.gerhorst@fau.de>,
	Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	 Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend	 <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev	 <sdf@fomichev.me>,
	Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>,
	 Puranjay Mohan <puranjay@kernel.org>,
	Xu Kuohai <xukuohai@huaweicloud.com>,
	Catalin Marinas	 <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Hari Bathini	 <hbathini@linux.ibm.com>,
	Christophe Leroy <christophe.leroy@csgroup.eu>,
	 Naveen N Rao <naveen@kernel.org>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman	 <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	Mykola Lysenko	 <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>,
	Henriette Herzog	 <henriette.herzog@rub.de>,
	Saket Kumar Bhaskar <skb99@linux.ibm.com>,
	 Cupertino Miranda <cupertino.miranda@oracle.com>,
	Jiayuan Chen <mrpre@163.com>,
	Matan Shachnai <m.shachnai@gmail.com>,
	 Dimitar Kanaliev <dimitar.kanaliev@siteground.com>,
	Shung-Hsi Yu <shung-hsi.yu@suse.com>, Daniel Xu <dxu@dxuuu.xyz>,
		bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
		linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
		linux-kselftest@vger.kernel.org
Cc: Maximilian Ott <ott@cs.fau.de>, Milan Stephan <milan.stephan@fau.de>
Subject: Re: [PATCH bpf-next v3 02/11] bpf: Move insn if/else into do_check_insn()
Date: Thu, 01 May 2025 11:22:53 -0700	[thread overview]
Message-ID: <15294d369d94cf005c9aa722967e5ddb1fa8cee3.camel@gmail.com> (raw)
In-Reply-To: <20250501073603.1402960-3-luis.gerhorst@fau.de>

On Thu, 2025-05-01 at 09:35 +0200, Luis Gerhorst wrote:
> This is required to catch the errors later and fall back to a nospec if
> on a speculative path.
> 
> Eliminate the regs variable as it is only used once and insn_idx is not
> modified in-between the definition and usage.
> 
> Still pass insn simply to match the other check_*() functions. As Eduard
> points out [1], insn is assumed to correspond to env->insn_idx in many
> places (e.g, __check_reg_arg()).
> 
> Move code into do_check_insn(), replace
> * "continue" with "return 0" after modifying insn_idx
> * "goto process_bpf_exit" with "return PROCESS_BPF_EXIT"
> * "do_print_state = " with "*do_print_state = "
> 
> [1] https://lore.kernel.org/all/293dbe3950a782b8eb3b87b71d7a967e120191fd.camel@gmail.com/
> 
> Signed-off-by: Luis Gerhorst <luis.gerhorst@fau.de>
> Acked-by: Henriette Herzog <henriette.herzog@rub.de>
> Cc: Maximilian Ott <ott@cs.fau.de>
> Cc: Milan Stephan <milan.stephan@fau.de>
> ---

Except two notes below, I think this patch looks good.
Thank you, this is a good refactoring.

[...]

> +static int do_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn,
> +			 bool *do_print_state)
> +{

[...]

> +	} else if (class == BPF_ST) {
> +		enum bpf_reg_type dst_reg_type;
> +
> +		if (BPF_MODE(insn->code) != BPF_MEM ||
> +		    insn->src_reg != BPF_REG_0) {
> +			verbose(env, "BPF_ST uses reserved fields\n");
> +			return -EINVAL;
> +		}
> +		/* check src operand */
> +		err = check_reg_arg(env, insn->dst_reg, SRC_OP);
> +		if (err)
> +			return err;
> +
> +		dst_reg_type = cur_regs(env)[insn->dst_reg].type;

Implicitly relying on `insn == &env->prog->insnsi[env->cur_idx]`
is weird. Still think that `insn` parameter should be dropped and
computed inside this function instead.

> +
> +		/* check that memory (dst_reg + off) is writeable */
> +		err = check_mem_access(env, env->insn_idx, insn->dst_reg,
> +				       insn->off, BPF_SIZE(insn->code),
> +				       BPF_WRITE, -1, false, false);
> +		if (err)
> +			return err;
> +
> +		err = save_aux_ptr_type(env, dst_reg_type, false);
> +		if (err)
> +			return err;
> +	} else if (class == BPF_JMP || class == BPF_JMP32) {

[...]

> +		} else if (opcode == BPF_EXIT) {
> +			if (BPF_SRC(insn->code) != BPF_K ||
> +			    insn->imm != 0 ||
> +			    insn->src_reg != BPF_REG_0 ||
> +			    insn->dst_reg != BPF_REG_0 ||
> +			    class == BPF_JMP32) {
> +				verbose(env, "BPF_EXIT uses reserved fields\n");
> +				return -EINVAL;
> +			}
> +process_bpf_exit_full:

Nit: since we are refactoring I'd extract this as a function instead of goto.

> +			/* We must do check_reference_leak here before
> +			 * prepare_func_exit to handle the case when
> +			 * state->curframe > 0, it may be a callback function,
> +			 * for which reference_state must match caller reference
> +			 * state when it exits.
> +			 */
> +			err = check_resource_leak(env, exception_exit, !env->cur_state->curframe,
> +						  "BPF_EXIT instruction in main prog");
> +			if (err)
> +				return err;
> +
> +			/* The side effect of the prepare_func_exit which is
> +			 * being skipped is that it frees bpf_func_state.
> +			 * Typically, process_bpf_exit will only be hit with
> +			 * outermost exit. copy_verifier_state in pop_stack will
> +			 * handle freeing of any extra bpf_func_state left over
> +			 * from not processing all nested function exits. We
> +			 * also skip return code checks as they are not needed
> +			 * for exceptional exits.
> +			 */
> +			if (exception_exit)
> +				return PROCESS_BPF_EXIT;
> +
> +			if (env->cur_state->curframe) {
> +				/* exit from nested function */
> +				err = prepare_func_exit(env, &env->insn_idx);
> +				if (err)
> +					return err;
> +				*do_print_state = true;
> +				return 0;
> +			}
> +
> +			err = check_return_code(env, BPF_REG_0, "R0");
> +			if (err)
> +				return err;
> +			return PROCESS_BPF_EXIT;

[...]


  reply	other threads:[~2025-05-01 18:22 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-01  7:35 [PATCH bpf-next v3 00/11] bpf: Mitigate Spectre v1 using barriers Luis Gerhorst
2025-05-01  7:35 ` [PATCH bpf-next v3 01/11] selftests/bpf: Fix caps for __xlated/jited_unpriv Luis Gerhorst
2025-05-01 16:56   ` Kumar Kartikeya Dwivedi
2025-05-01 17:45   ` Eduard Zingerman
2025-05-01  7:35 ` [PATCH bpf-next v3 02/11] bpf: Move insn if/else into do_check_insn() Luis Gerhorst
2025-05-01 18:22   ` Eduard Zingerman [this message]
2025-05-05 18:31     ` Luis Gerhorst
2025-05-01 22:06   ` Kumar Kartikeya Dwivedi
2025-05-01  7:35 ` [PATCH bpf-next v3 03/11] bpf: Return -EFAULT on misconfigurations Luis Gerhorst
2025-05-01 22:06   ` Kumar Kartikeya Dwivedi
2025-05-01  7:35 ` [PATCH bpf-next v3 04/11] bpf: Return -EFAULT on internal errors Luis Gerhorst
2025-05-01 22:07   ` Kumar Kartikeya Dwivedi
2025-05-01  7:35 ` [PATCH bpf-next v3 05/11] bpf, arm64, powerpc: Add bpf_jit_bypass_spec_v1/v4() Luis Gerhorst
2025-05-01 22:14   ` Kumar Kartikeya Dwivedi
2025-05-18 10:38   ` Hari Bathini
2025-05-01  7:35 ` [PATCH bpf-next v3 06/11] bpf, arm64, powerpc: Change nospec to include v1 barrier Luis Gerhorst
2025-05-19  7:01   ` Hari Bathini
2025-05-01  7:35 ` [PATCH bpf-next v3 07/11] bpf: Rename sanitize_stack_spill to nospec_result Luis Gerhorst
2025-05-01 22:30   ` Kumar Kartikeya Dwivedi
2025-05-01  7:35 ` [PATCH bpf-next v3 08/11] bpf: Fall back to nospec for Spectre v1 Luis Gerhorst
2025-05-01 23:55   ` Kumar Kartikeya Dwivedi
2025-05-02 18:57     ` Luis Gerhorst
2025-05-14  5:38   ` Kumar Kartikeya Dwivedi
2025-05-01  7:36 ` [PATCH bpf-next v3 09/11] selftests/bpf: Add test for Spectre v1 mitigation Luis Gerhorst
2025-05-14  6:24   ` Kumar Kartikeya Dwivedi
2025-05-01  7:36 ` [PATCH bpf-next v3 10/11] bpf: Allow nospec-protected var-offset stack access Luis Gerhorst
2025-05-02  0:03   ` Kumar Kartikeya Dwivedi
2025-06-03 21:07     ` Luis Gerhorst
2025-05-14  6:28   ` Kumar Kartikeya Dwivedi
2025-05-01  7:36 ` [PATCH bpf-next v3 11/11] bpf: Fall back to nospec for sanitization-failures Luis Gerhorst
2025-05-14  6:47   ` Kumar Kartikeya Dwivedi
2025-05-14 17:30     ` Luis Gerhorst
2025-05-14 17:34       ` Kumar Kartikeya Dwivedi
2025-05-09 18:40 ` [PATCH bpf-next v3 00/11] bpf: Mitigate Spectre v1 using barriers patchwork-bot+netdevbpf
2025-05-09 18:43   ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15294d369d94cf005c9aa722967e5ddb1fa8cee3.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=cupertino.miranda@oracle.com \
    --cc=daniel@iogearbox.net \
    --cc=dimitar.kanaliev@siteground.com \
    --cc=dxu@dxuuu.xyz \
    --cc=haoluo@google.com \
    --cc=hbathini@linux.ibm.com \
    --cc=henriette.herzog@rub.de \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=luis.gerhorst@fau.de \
    --cc=m.shachnai@gmail.com \
    --cc=maddy@linux.ibm.com \
    --cc=martin.lau@linux.dev \
    --cc=milan.stephan@fau.de \
    --cc=mpe@ellerman.id.au \
    --cc=mrpre@163.com \
    --cc=mykolal@fb.com \
    --cc=naveen@kernel.org \
    --cc=npiggin@gmail.com \
    --cc=ott@cs.fau.de \
    --cc=puranjay@kernel.org \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=shung-hsi.yu@suse.com \
    --cc=skb99@linux.ibm.com \
    --cc=song@kernel.org \
    --cc=will@kernel.org \
    --cc=xukuohai@huaweicloud.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).