Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2616118E02A for ; Tue, 30 Sep 2025 12:45:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759236339; cv=none; b=V08SFPBa85yKGgJhInQH0Ak9cIEYjMUK1fYgA15i7YJ5tPpzXayikz784yyBC0wOFnGxiBWI2b6/mif/gltJB2PlDFzkWzB+r+CJtn3Jz+eLj/Xm8PscEwbl4IGdl2C4NGSXOfIdCDX03XXRCah3HWrTn5aRu2BgvOOtYEE360A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759236339; c=relaxed/simple; bh=NZstfcKqFCojDVsDG7bY7Yl7vr5ZjkW4ktXKSzgi0jg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V9RJrCz7Az068zlKCd8P6cX193eY2jdSHm4l6C1b1XJsKPt0ZhfIFATZaqQzlbJsurqu9cku24dfBm/KTXx4EP4JnAxa6FfnYyBzGA9BI63830hvOhUhPMXVV+A4fGfL7fVb+N0ylyic1AlwrMqfhzJ66i9K8iuzO1+vY0PmpdU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MOt4X7J5; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MOt4X7J5" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-46e504975dbso18900065e9.1 for ; Tue, 30 Sep 2025 05:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759236335; x=1759841135; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=prAkz5BfXsovh++mXOYoPFLJhohSYMfnLpUEodwBANs=; b=MOt4X7J5EQ6tu4t5O84BgIAuCzFTwCNZBs8dhGPXuCS6sEuoME7aCsUdH5UM94pHcI W0V39LTQj1im85A+mwgCyBxEaf9MG+Iz2ig1oQMRlZs46rOClR6De1zodploTtXGUJkI JZTr6Wh/71y9C1Wyr3Nf1ABa3fpMabOW/b1p7fUqrlP1Kf4pZinBG3EZ0uq6E1IPZXrn PxTlbFUPj8vFpANPXDk3abk8XvW+q1p+DA53fucdlhsZ4jNwmrBMVz/oAxK1qlpfSg3V 9X+FxeWHzz3nKJ/Kl6VqPHOdrjOXccOR9lGD/pUTMuAe61Qa0N1DAreZtrkhi4KPCqJE 26xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759236335; x=1759841135; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=prAkz5BfXsovh++mXOYoPFLJhohSYMfnLpUEodwBANs=; b=oS9wFxIgHPYtSDuye2KlVsTQqcoaGWOBeFgjeOYJGtlDlKFjQ0h5c9mHT1/zoSxO6j xaIvGjXZV/owj5cfSUGvqPRIMSnSZxIVrr7gW+DtqlUEBK3JWBbZk7wBDuJqaCtwxc1O C7wtzg0wAfn0cPsKkNj3s9HpkGwipU3RE2OSJWQp6I+uTCYlAjlvruCSbGTD9Q/zGDai v3et+FfU5b4XGVhhWMkUDxp61o4fNvtIgPgGTDYbyPqeCW94knVkPEC0IGvT6koC335a 8AoUdWLhiq56E/pqxMQQNymgEtciLzxyNLOiPFhgo9roNW55IsEc1030o3Yku465BzvH xQow== X-Gm-Message-State: AOJu0YwM/Zek7Yu8ssnO/maNwJYb2PnHOfUQvLzHniV5wtkMwKtjNfSs apZaflEEGKtKxxRwa7K47ovB8aXwSxLvmcb+4XRwnaTyely+IQ7wherQ5Ygqew== X-Gm-Gg: ASbGncvhM0Mpmu6nVSHgFIzFNYJFh9R1dcQ8d5IU//HJ9sJp/J0IQhv6I1AsP6XqENx gc4pu79R9n4D9xW4fFhquASJYsIcggEvVOMBaA7RyUsxzmBAUbphhahD8KJWAt3LoPhOHLX8lIJ INnXEH89w0mFB6qYznDNPG5BVVQLOWBc3Whbl/Zab82HQNwmpiRGq021MtLDPDwFTH6dIz9BGQD LMfPlMhr/BGBGH+KY97BLm0J05SOczcjPIFmD89Y4xRazv98XB5HJl9T1vL1gLp8MuOY+osadpX DToyLNVY+bQAefLh/rbCdohhjTPmG+iwEckicfLBWQmUbnSqvoWilB5ngY+4chQApRC4uBto/Kh XoVimH8LnQqQCSFaHyWx1nsdshKEzGIohh6pik80jSKTBjpByI7tVdmEPhQzHFUZQaXdKYelJTd Yf X-Google-Smtp-Source: AGHT+IH/eM1rTmBA3X5X1GC2I0SJI1YoYBRQKYGfDfHPLAJsgx+LvAU2y993rY61oQ1XblrF/Ad6Kg== X-Received: by 2002:a05:600c:608b:b0:46e:5a5b:db60 with SMTP id 5b1f17b1804b1-46e5a5bdd24mr32571065e9.31.1759236334810; Tue, 30 Sep 2025 05:45:34 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-40fc8aa0078sm22392586f8f.59.2025.09.30.05.45.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Sep 2025 05:45:34 -0700 (PDT) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Anton Protopopov , Daniel Borkmann , Eduard Zingerman , Quentin Monnet , Yonghong Song Cc: Anton Protopopov Subject: [PATCH v5 bpf-next 01/15] bpf: fix the return value of push_stack Date: Tue, 30 Sep 2025 12:50:57 +0000 Message-Id: <20250930125111.1269861-2-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250930125111.1269861-1-a.s.protopopov@gmail.com> References: <20250930125111.1269861-1-a.s.protopopov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In [1] Eduard mentioned that on push_stack failure verifier code should return -ENOMEM instead of -EFAULT. After checking with the other call sites I've found that code randomly returns either -ENOMEM or -EFAULT. This patch unifies the return values for the push_stack (and similar push_async_cb) functions such that error codes are always assigned properly. [1] https://lore.kernel.org/bpf/20250615085943.3871208-1-a.s.protopopov@gmail.com Signed-off-by: Anton Protopopov Acked-by: Eduard Zingerman --- kernel/bpf/verifier.c | 80 +++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index e892df386eed..5313ed905dc4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2093,7 +2093,7 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL_ACCOUNT); if (!elem) - return NULL; + return ERR_PTR(-ENOMEM); elem->insn_idx = insn_idx; elem->prev_insn_idx = prev_insn_idx; @@ -2103,12 +2103,12 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, env->stack_size++; err = copy_verifier_state(&elem->st, cur); if (err) - return NULL; + return ERR_PTR(-ENOMEM); elem->st.speculative |= speculative; if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { verbose(env, "The sequence of %d jumps is too complex.\n", env->stack_size); - return NULL; + return ERR_PTR(-E2BIG); } if (elem->st.parent) { ++elem->st.parent->branches; @@ -2903,7 +2903,7 @@ static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL_ACCOUNT); if (!elem) - return NULL; + return ERR_PTR(-ENOMEM); elem->insn_idx = insn_idx; elem->prev_insn_idx = prev_insn_idx; @@ -2915,7 +2915,7 @@ static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, verbose(env, "The sequence of %d jumps is too complex for async cb.\n", env->stack_size); - return NULL; + return ERR_PTR(-E2BIG); } /* Unlike push_stack() do not copy_verifier_state(). * The caller state doesn't matter. @@ -2926,7 +2926,7 @@ static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, elem->st.in_sleepable = is_sleepable; frame = kzalloc(sizeof(*frame), GFP_KERNEL_ACCOUNT); if (!frame) - return NULL; + return ERR_PTR(-ENOMEM); init_func_state(env, frame, BPF_MAIN_FUNC /* callsite */, 0 /* frameno within this callchain */, @@ -9014,8 +9014,8 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, prev_st = find_prev_entry(env, cur_st->parent, insn_idx); /* branch out active iter state */ queued_st = push_stack(env, insn_idx + 1, insn_idx, false); - if (!queued_st) - return -ENOMEM; + if (IS_ERR(queued_st)) + return PTR_ERR(queued_st); queued_iter = get_iter_from_state(queued_st, meta); queued_iter->iter.state = BPF_ITER_STATE_ACTIVE; @@ -10588,8 +10588,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins insn_idx, subprog, is_bpf_wq_set_callback_impl_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm)); - if (!async_cb) - return -EFAULT; + if (IS_ERR(async_cb)) + return PTR_ERR(async_cb); callee = async_cb->frame[0]; callee->async_entry_cnt = caller->async_entry_cnt + 1; @@ -10605,8 +10605,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins * proceed with next instruction within current frame. */ callback_state = push_stack(env, env->subprog_info[subprog].start, insn_idx, false); - if (!callback_state) - return -ENOMEM; + if (IS_ERR(callback_state)) + return PTR_ERR(callback_state); err = setup_func_entry(env, subprog, insn_idx, set_callee_state_cb, callback_state); @@ -13827,9 +13827,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, struct bpf_reg_state *regs; branch = push_stack(env, env->insn_idx + 1, env->insn_idx, false); - if (!branch) { + if (IS_ERR(branch)) { verbose(env, "failed to push state for failed lock acquisition\n"); - return -ENOMEM; + return PTR_ERR(branch); } regs = branch->frame[branch->curframe]->regs; @@ -14280,16 +14280,15 @@ struct bpf_sanitize_info { bool mask_to_left; }; -static struct bpf_verifier_state * -sanitize_speculative_path(struct bpf_verifier_env *env, - const struct bpf_insn *insn, - u32 next_idx, u32 curr_idx) +static int sanitize_speculative_path(struct bpf_verifier_env *env, + const struct bpf_insn *insn, + u32 next_idx, u32 curr_idx) { struct bpf_verifier_state *branch; struct bpf_reg_state *regs; branch = push_stack(env, next_idx, curr_idx, true); - if (branch && insn) { + if (!IS_ERR(branch) && insn) { regs = branch->frame[branch->curframe]->regs; if (BPF_SRC(insn->code) == BPF_K) { mark_reg_unknown(env, regs, insn->dst_reg); @@ -14298,7 +14297,7 @@ sanitize_speculative_path(struct bpf_verifier_env *env, mark_reg_unknown(env, regs, insn->src_reg); } } - return branch; + return PTR_ERR_OR_ZERO(branch); } static int sanitize_ptr_alu(struct bpf_verifier_env *env, @@ -14317,7 +14316,6 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env, u8 opcode = BPF_OP(insn->code); u32 alu_state, alu_limit; struct bpf_reg_state tmp; - bool ret; int err; if (can_skip_alu_sanitation(env, insn)) @@ -14390,11 +14388,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env, tmp = *dst_reg; copy_register_state(dst_reg, ptr_reg); } - ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, - env->insn_idx); - if (!ptr_is_dst_reg && ret) + err = sanitize_speculative_path(env, NULL, env->insn_idx + 1, env->insn_idx); + if (err < 0) + return REASON_STACK; + if (!ptr_is_dst_reg) *dst_reg = tmp; - return !ret ? REASON_STACK : 0; + return 0; } static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) @@ -16713,8 +16712,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, /* branch out 'fallthrough' insn as a new state to explore */ queued_st = push_stack(env, idx + 1, idx, false); - if (!queued_st) - return -ENOMEM; + if (IS_ERR(queued_st)) + return PTR_ERR(queued_st); queued_st->may_goto_depth++; if (prev_st) @@ -16792,10 +16791,11 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, * the fall-through branch for simulation under speculative * execution. */ - if (!env->bypass_spec_v1 && - !sanitize_speculative_path(env, insn, *insn_idx + 1, - *insn_idx)) - return -EFAULT; + if (!env->bypass_spec_v1) { + err = sanitize_speculative_path(env, insn, *insn_idx + 1, *insn_idx); + if (err < 0) + return err; + } if (env->log.level & BPF_LOG_LEVEL) print_insn_state(env, this_branch, this_branch->curframe); *insn_idx += insn->off; @@ -16805,11 +16805,12 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, * program will go. If needed, push the goto branch for * simulation under speculative execution. */ - if (!env->bypass_spec_v1 && - !sanitize_speculative_path(env, insn, - *insn_idx + insn->off + 1, - *insn_idx)) - return -EFAULT; + if (!env->bypass_spec_v1) { + err = sanitize_speculative_path(env, insn, *insn_idx + insn->off + 1, + *insn_idx); + if (err < 0) + return err; + } if (env->log.level & BPF_LOG_LEVEL) print_insn_state(env, this_branch, this_branch->curframe); return 0; @@ -16830,10 +16831,9 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, return err; } - other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, - false); - if (!other_branch) - return -EFAULT; + other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, false); + if (IS_ERR(other_branch)) + return PTR_ERR(other_branch); other_branch_regs = other_branch->frame[other_branch->curframe]->regs; if (BPF_SRC(insn->code) == BPF_X) { -- 2.34.1