From nobody Sun Dec 22 09:35:24 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 ED89C1BF37 for ; Sat, 21 Dec 2024 01:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734744263; cv=none; b=KzOlWQlk+Amd9L8lmmdspc6w4RuhBhoTgwUCSbHBE8xQVbPX6qM9uCHt0t/4EMyYIQt7DV718qn/FSB72lwqfeU6da+9+C2zo3B2tZl6ZD5kAC4COIj2L5hNRvw2S7C53EJ9qbRLGvqj/2HQTmKajJW1nPjlYVINvNTkloRIjv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734744263; c=relaxed/simple; bh=rBG+AsI9uSAETmCL6GbWWVsOK3Oe1VshvCQ+iEEvVBU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ER7brHc3IJKzic1IkduDbWDKcz4orZB8W5hU84uIYAtyrwfBdZ9LQAu0HXO41iW0dHWUSC+0W7INAYTAuMVzsCapeWM3/0fRf9GF36qIMb/3RC9Xpl75RRohODTgBUUUum6Za3diVXW0/j0YYaPY2l8oJxWoILgpnuPyGZQ+afI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yepeilin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VWXxyRsF; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yepeilin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VWXxyRsF" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-725e87a142dso3447359b3a.2 for ; Fri, 20 Dec 2024 17:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734744261; x=1735349061; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Zrn35WjRTAXV/q0fGp4quX7tww+ddKAXEAGON/HplnE=; b=VWXxyRsFQ77P63rS6FmD6NH8FYe8roC65o/wMR6g7XlLtvZWq6yIK9blP9G3h7oB8Q DLk+dR+bLaY/QKHLwcLSE6GungHX7qC0GYyLYHm8XfIlbI/oe+Ri2CvAGx9OaS+jJnVp 9Jx39KbAHOOVHRaMXtVyETrpSFAJ3fVmQ/ydPaCMq1lsif6qCIeN15K0UcYvUbOreg7u ULcrE052dkSE0X4GbVNsq5ks2wjr83XnFqTCyRw5429Kje+TRD5pPbfWg1zf+7ZqWTU8 v+J7FnZwrcRFV4c7LQtyxupW4dr49k7H2amt1yVQa4b24gticCs1wwSIWJEu+KD8m+z4 UQ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734744261; x=1735349061; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Zrn35WjRTAXV/q0fGp4quX7tww+ddKAXEAGON/HplnE=; b=MxMM3fA+5T/NggaS3BlWvlSHJ3rcw9Nv/7AmcmU2/tJtcL6q9lX5QDX/P0vyY+gI1/ cf2qKehhxAAsBA+s49Fj/czzg/Sj0Qkc9j4wNZhRzYf7yT4+OTN966Zs/1xaI+nSBUSC 7t/HqzLoya0JxtF8MA0jAl42cl+8dR+suwc3Aa10MIe/KWAPoeqzHOAbGbiNmkXalC20 5hgLBsn2OYHlbobFNCfiAj3KUewHbCoyFrSXG4KjDe1dkVIY0bV2eQwmMeG/de9Ctf+m Rotj0Bm8CoF/2nmSe28PVqgNGMyhdhNfu+QO+4xyHbuAZ3eukbRX6nBK0Tsp8YPLLGXX v0Aw== X-Forwarded-Encrypted: i=1; AJvYcCUYBgLOJTtdv3hdFkhSHrHOtbIup47loc8Au73PHnTYCyJ7KZ1x72slb1JtivzirzX5U6sW/oAES2P3tsk=@vger.kernel.org X-Gm-Message-State: AOJu0YwiiTPTxNo4+ARmLhoIFqy0cepEFpFAB2IQ5Qe3SiQbJ7vRGL13 SNAmjn4LgdBKEkOccb/3SahXrtWbZhz2RmTJQnVe31lJV4jiY7lhSt/0liJLSyTEFvZwo1B2GTB TpsEkQHJz7w== X-Google-Smtp-Source: AGHT+IFbscAq9apWLBUd7Erh+ffNiilAXLjwg3/pHWhedK7m5HIwODQAO6Bo6uwHyxt7qauW6JqF+Y3GDmamXA== X-Received: from pgum3.prod.google.com ([2002:a65:6a03:0:b0:7fd:4497:f282]) (user=yepeilin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d80d:b0:1e0:d848:9e8f with SMTP id adf61e73a8af0-1e5e046decbmr9957435637.13.1734744261235; Fri, 20 Dec 2024 17:24:21 -0800 (PST) Date: Sat, 21 Dec 2024 01:24:04 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <44fd0483ebdb9f84e6d069fdf890bc5801e0d130.1734742802.git.yepeilin@google.com> Subject: [PATCH RFC bpf-next v1 1/4] bpf/verifier: Factor out check_load() From: Peilin Ye To: bpf@vger.kernel.org Cc: Peilin Ye , Alexei Starovoitov , Eduard Zingerman , Song Liu , Yonghong Song , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "Paul E. McKenney" , Puranjay Mohan , Xu Kuohai , Catalin Marinas , Will Deacon , Quentin Monnet , Mykola Lysenko , Shuah Khan , Josh Don , Barret Rhoden , Neel Natu , Benjamin Segall , David Vernet , Dave Marchevsky , linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No functional changes intended. While we are here, make that comment about "reserved fields" more specific. Reviewed-by: Josh Don Signed-off-by: Peilin Ye --- kernel/bpf/verifier.c | 56 +++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f27274e933e5..fa40a0440590 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -7518,6 +7518,36 @@ static int check_mem_access(struct bpf_verifier_env = *env, int insn_idx, u32 regn static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_ty= pe type, bool allow_trust_mismatch); =20 +static int check_load(struct bpf_verifier_env *env, struct bpf_insn *insn,= const char *ctx) +{ + struct bpf_reg_state *regs =3D cur_regs(env); + enum bpf_reg_type src_reg_type; + int err; + + /* check src operand */ + err =3D check_reg_arg(env, insn->src_reg, SRC_OP); + if (err) + return err; + + err =3D check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); + if (err) + return err; + + src_reg_type =3D regs[insn->src_reg].type; + + /* check that memory (src_reg + off) is readable, + * the state of dst_reg will be updated by this func + */ + err =3D check_mem_access(env, env->insn_idx, insn->src_reg, + insn->off, BPF_SIZE(insn->code), + BPF_READ, insn->dst_reg, false, + BPF_MODE(insn->code) =3D=3D BPF_MEMSX); + err =3D err ?: save_aux_ptr_type(env, src_reg_type, true); + err =3D err ?: reg_bounds_sanity_check(env, ®s[insn->dst_reg], ctx); + + return err; +} + static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct= bpf_insn *insn) { int load_reg; @@ -18945,30 +18975,10 @@ static int do_check(struct bpf_verifier_env *env) return err; =20 } else if (class =3D=3D BPF_LDX) { - enum bpf_reg_type src_reg_type; - - /* check for reserved fields is already done */ - - /* check src operand */ - err =3D check_reg_arg(env, insn->src_reg, SRC_OP); - if (err) - return err; - - err =3D check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); - if (err) - return err; - - src_reg_type =3D regs[insn->src_reg].type; - - /* check that memory (src_reg + off) is readable, - * the state of dst_reg will be updated by this func + /* Check for reserved fields is already done in + * resolve_pseudo_ldimm64(). */ - err =3D check_mem_access(env, env->insn_idx, insn->src_reg, - insn->off, BPF_SIZE(insn->code), - BPF_READ, insn->dst_reg, false, - BPF_MODE(insn->code) =3D=3D BPF_MEMSX); - err =3D err ?: save_aux_ptr_type(env, src_reg_type, true); - err =3D err ?: reg_bounds_sanity_check(env, ®s[insn->dst_reg], "ldx"= ); + err =3D check_load(env, insn, "ldx"); if (err) return err; } else if (class =3D=3D BPF_STX) { --=20 2.47.1.613.gc27f4b7a9f-goog