BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 01/21] bpf: Stop a load only when the loader is killed or has to freeze
Date: Mon, 16 Nov 2026 06:49:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-216329@kernel.org> (raw)
In-Reply-To: <20261116055638.4171529-2-alexei.starovoitov@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] bpf: unmasked BPF_COMMON_ATTRS in sys_bpf() skips
  -ERESTARTNOINTR
- [Medium] bpf: stale map->user state across restart on blinding abort
  in bpf_check()
- [Medium] bpf: bpf_log_attr_finalize() overwrites -EINTR on
  bpf_must_abort()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] bpf: out-of-bounds write to core_relo_rec_size in
  bpf_check_core_relo() [1]
--

commit 3d463cd1b62b606f3634c39818c65fed7d7789b2
Author: Alexei Starovoitov <ast@kernel.org>

bpf: Stop a load only when the loader is killed or has to freeze

This commit introduces bpf_must_abort() so that BPF loading only aborts with
-EINTR on fatal signals or freezer requests, and restarts the syscall via
-ERESTARTNOINTR after freezing.

> diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
> index 8a3f2421331e0..d6b930377d753 100644
> --- a/kernel/bpf/check_btf.c
> +++ b/kernel/bpf/check_btf.c
> @@ -402,8 +402,8 @@ int bpf_check_core_relo(struct bpf_verifier_env *env,
>  			break;
>  		}
>  
> -		err = bpf_core_apply(&ctx, &core_relo, i,
> -				     &prog->insnsi[core_relo.insn_off / 8]);
> +		err = bpf_must_abort() ? -EINTR :
> +		      bpf_core_apply(&ctx, &core_relo, i, &prog->insnsi[core_relo.insn_off / 8]);
>  		if (err)
>  			break;
>  		bpfptr_add(&u_core_relo, rec_size);

[Severity: High]
This problem wasn't introduced by this patch, but can the -E2BIG error path
earlier in this loop write past the end of uattr [1]?

__sys_bpf() copies size bytes from uattr into a zero-initialized union
bpf_attr without passing size down to bpf_prog_load() or bpf_check().  On
little-endian architectures, a size that covers the start of
core_relo_rec_size but ends before offsetofend(union bpf_attr,
core_relo_rec_size) still forms a valid attr->core_relo_rec_size.  When
bpf_check_uarg_tail_zero() then returns -E2BIG:

kernel/bpf/check_btf.c:bpf_check_core_relo() {
    ...
		err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size);
		if (err) {
			if (err == -E2BIG) {
				verbose(env, "nonzero tailing record in core_relo");
				if (copy_to_bpfptr_offset(uattr,
							  offsetof(union bpf_attr, core_relo_rec_size),
							  &expected_size, sizeof(expected_size)))
					err = -EFAULT;
			}
			break;
		}
    ...
}

copy_to_bpfptr_offset() writes all 4 bytes of expected_size to uattr without
checking that size covers the full field.  When invoked from a
BPF_PROG_TYPE_SYSCALL program via bpf_sys_bpf(), uattr is a kernel pointer
for which the verifier only validates [attr, attr + size), so this writes up
to three zero bytes out of bounds into adjacent BPF stack or map value
memory.

[ ... ]

> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
> index 1d33a78d93739..735932392a87f 100644
> --- a/kernel/bpf/fixups.c
> +++ b/kernel/bpf/fixups.c
> @@ -165,19 +164,6 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
>  }
>  #endif
>  
> -/*
> - * A post-verification rewriting pass can queue a patch for each instruction
> - * of a large program. Keep the primitives that queue them killable and
> - * preemptible.
> - */
> -static bool bpf_rewrite_must_abort(void)
> -{
> -	if (fatal_signal_pending(current))
> -		return true;
> -	cond_resched();
> -	return false;
> -}
> -
>  /*
>   * Queue the replacement of insn 'off' by 'patch[0..len)' until
>   * bpf_patch_list_commit(), see __bpf_patch_list_add(). Replacing one insn by
> @@ -190,7 +176,7 @@ struct bpf_insn *bpf_patch_list_add(struct bpf_verifier_env *env, u32 off,
>  	struct bpf_patch_list *pl = &env->patch_list;
>  	struct bpf_insn_patch *p = pl->cnt ? &pl->patches[pl->cnt - 1] : NULL;
>  
> -	if (bpf_rewrite_must_abort())
> +	if (bpf_must_abort())
>  		return ERR_PTR(-EINTR);

[Severity: Medium]
Can aborting here on a freezer request leave map->user modified or unreleased
when BPF_PROG_LOAD restarts?

Previously bpf_rewrite_must_abort() only triggered on fatal signals, where
the task exits.  With bpf_must_abort(), a freeze during
__bpf_prog_select_runtime() -> bpf_prog_jit_compile() ->
bpf_jit_blind_constants() -> bpf_patch_list_add() returns -EINTR to
bpf_check() after two state changes have already happened for a
single-subprog program:

1. bpf_fixup_call_args() -> bpf_jit_subprogs() -> resolve_func_ptrs() has
   already set BPF_MAP_USER_PATCHED on ptr->map->user and zeroed *slot in the
   map if static subprogs referenced in a frozen read-only map were removed
   as dead code.
2. env->prog->aux->used_maps has already been populated, so err_release_maps
   skips synchronous release_maps(env) and defers __bpf_free_used_maps() to
   system_wq via schedule_work(&aux->work); __bpf_free_used_maps() also only
   runs cmpxchg(&map->user, (unsigned long)aux, 0), which does not clear
   aux | BPF_MAP_USER_PATCHED.

When the task thaws and restarts BPF_PROG_LOAD via -ERESTARTNOINTR,
bpf_map_claim() or resolve_func_ptrs() can see the stale map->user state and
fail with -EBUSY or treat a private map as BPF_MAP_USER_MANY.

[ ... ]

> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 04a3b3c80c74e..9c55f5ed8ced5 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -6604,7 +6604,13 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
>  SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size,
>  		struct bpf_common_attr __user *, uattr_common, unsigned int, size_common)
>  {
> -	return __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common);
> +	int err = __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common);
> +
> +	/* bpf_must_abort() ended the command, nothing of it is left */
> +	if (err == -EINTR && task_sigpending(current) &&
> +	    (cmd == BPF_PROG_LOAD || cmd == BPF_BTF_LOAD || cmd == BPF_MAP_CREATE))
> +		return -ERESTARTNOINTR;
> +	return err;
>  }

[Severity: Medium]
Does this check miss calls that set BPF_COMMON_ATTRS in cmd?

__sys_bpf() clears BPF_COMMON_ATTRS on its local copy of cmd (cmd &=
~BPF_COMMON_ATTRS), so cmd in SYSCALL_DEFINE5(bpf) still has that bit set and
returns -EINTR instead of -ERESTARTNOINTR to callers usinguattr_common.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index e4fcd1c9242d4..03f8b2f335969 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -21790,11 +21790,8 @@ static int do_check_block(struct bpf_verifier_env *env, int prev_insn_idx)
>  		struct bpf_insn_aux_data *insn_aux;
>  		int err;
>  
> -		if (signal_pending(current))
> -			return -EAGAIN;
> -
> -		if (need_resched())
> -			cond_resched();
> +		if (bpf_must_abort())
> +			return -EINTR;

[Severity: Medium]
Can bpf_log_attr_finalize() overwrite -EINTR with -ENOSPC when a load with a
user log buffer is aborted by bpf_must_abort()?

In both bpf_check() and bpf_btf_load(), the error path runs:

kernel/bpf/verifier.c:bpf_check() {
    ...
err_release_maps:
	err = bpf_log_attr_finalize(attr_log, &env->log);
	if (err)
		ret = err;
    ...
}

If log->len_max > log->len_total when bpf_must_abort() triggers (including
under BPF_LOG_LEVEL1 when ret == -EINTR causes do_check() to run
bpf_flow_print_way() and do_check_common() to skip bpf_vlog_reset(&env->log,
0) before print_verification_stats()), bpf_log_attr_finalize() returns
-ENOSPC and overwrites ret = -EINTR, so SYSCALL_DEFINE5(bpf) returns -ENOSPC
to user space instead of restarting with -ERESTARTNOINTR.

[1] https://sashiko.dev/#/bug/linux-e8be0f6f-dada-49a6-9534-0a5b7e8f9218

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116055638.4171529-1-alexei.starovoitov@gmail.com?part=1

  parent reply	other threads:[~2026-11-16  6:49 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  5:56 [PATCH bpf-next 00/21] bpf: Remove the limits on maps, kfuncs, subprogs and special fields Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 01/21] bpf: Stop a load only when the loader is killed or has to freeze Alexei Starovoitov
2026-11-16  6:39   ` bot+bpf-ci
2026-11-16  6:49   ` sashiko-bot [this message]
2026-11-16  5:56 ` [PATCH bpf-next 02/21] bpf: Fail the load when there is no memory Alexei Starovoitov
2026-11-16  6:29   ` sashiko-bot
2026-11-16  5:56 ` [PATCH bpf-next 03/21] bpf: Give a syscall program the largest context when it does tail calls Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 04/21] bpf: Do not inline bpf_loop() when its callback is dead code Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 05/21] bpf: Dump programs that have more than 524288 insns Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 06/21] bpf: Resolve BTF structs and arrays first Alexei Starovoitov
2026-11-16  6:13   ` sashiko-bot
2026-11-16  5:56 ` [PATCH bpf-next 07/21] bpf: Remove the limits on maps, BTFs and fd_array of a program Alexei Starovoitov
2026-11-16  6:39   ` bot+bpf-ci
2026-11-16  5:56 ` [PATCH bpf-next 08/21] bpf: Do not scan all maps for each insn and each subprog Alexei Starovoitov
2026-11-16  6:16   ` sashiko-bot
2026-11-16  5:56 ` [PATCH bpf-next 09/21] bpf: Remove the limits on kfuncs of a program Alexei Starovoitov
2026-11-16  6:39   ` bot+bpf-ci
2026-11-16  5:56 ` [PATCH bpf-next 10/21] bpf: Remove the limits on tables of functions Alexei Starovoitov
2026-11-16  6:14   ` sashiko-bot
2026-11-16  5:56 ` [PATCH bpf-next 11/21] libbpf: Size the tables of the loader program from the object Alexei Starovoitov
2026-11-16  6:20   ` sashiko-bot
2026-11-16  5:56 ` [PATCH bpf-next 12/21] bpf: Relax jumps that patches push out of range Alexei Starovoitov
2026-11-16  6:52   ` bot+bpf-ci
2026-11-16  5:56 ` [PATCH bpf-next 13/21] bpf: Keep the call graph in lists of edges Alexei Starovoitov
2026-11-16  6:39   ` sashiko-bot
2026-11-16  5:56 ` [PATCH bpf-next 14/21] bpf: Find the effects of callees on the call graph Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 15/21] bpf: Link the decl tags of a type Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 16/21] bpf: Remove BPF_MAX_SUBPROGS Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 17/21] bpf: Walk each type once when looking for special fields Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 18/21] bpf: Raise the limit on the length of BTF names Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 19/21] bpf: Find dominators in O(n log n) Alexei Starovoitov
2026-11-16  5:56 ` [PATCH bpf-next 20/21] selftests/bpf: Test programs above the old limits Alexei Starovoitov
2026-11-16  6:33   ` sashiko-bot
2026-11-16  6:39   ` bot+bpf-ci
2026-11-16  5:56 ` [PATCH bpf-next 21/21] selftests/bpf: Test limits of the load with raw programs Alexei Starovoitov
2026-11-16  6:39   ` sashiko-bot

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=sashiko-outbox-216329@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.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