Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp3852297rdb; Mon, 11 Dec 2023 01:40:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IEibRtZaivB5L55HfIVHKxz66JdLlQSPiplgcRpvcflb6WB6vW52NjmYHprWxTzRq9zBLJg X-Received: by 2002:a17:903:1104:b0:1d3:16a6:d498 with SMTP id n4-20020a170903110400b001d316a6d498mr2368469plh.60.1702287606382; Mon, 11 Dec 2023 01:40:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702287606; cv=none; d=google.com; s=arc-20160816; b=oYpnFcAqwR6x8cmnQYYgXd0RK7no6yVPx7IOPkAPjEnAIVog4baFvX7fscP/NrcoYQ 3of7fk6BmS/f9vmmXOvONudPzKvxYiBQKPJa6OGPi8gcD0G4r+Cv8LIrOfWhJSdwLhTA u0FzUl0lvZBQAz89WFVqUAaWv5cRN3KGEvQTGFoEKSq0RDSEtYPBKZvqy7RjvkbDzuws QVNT/Mg5QzgSh8rpyihG3aNzHaz35p8sskDvbysePjZf2sRN3WGbF3lW0L25jgj8lhfH 7gTmWzH5JES+kBxcIFkDqqjmXlSOqCDt3CWy2k8xKIJQ6pmCqriwFv13cWx0BUfODI4v IOHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=2/PILXw+sfhUyZ9V8GULOr4bqu2Prc7VK3XM3govllA=; fh=KXJHFA0kxdecE/+R//AT9w6lBrOA0MSnP3a3O7yW2Kg=; b=TF3cz/yCNMNVR3QBKycdHsITq5BzZC9+k7uawOLvJow2es1OlKrNP2k2Bn1PfjWT4v RRtIAFo/vAumzN0/ahMAQJ9V7x0NEproP6WbZcT2TuctEp188yAdeZDhNqNPVOL8H+xi dQ1Wd5teJTUqq+h9MkJ3HndZNKkyezMFPNd/Cr6ZbFDvWfvRDqyGeX7CPcyELK8/7bhc p/G5h1KKeR8lvTgR1rOghRbDFaMZcNnz9buCkHult7N1V7fVT4xyhI5DecrJ5NGcQAck xc3GraGafKqwpoUzc98pnW+ZdonSQ2WdMiDS82pmlE0iRYMSeWHa+0lFXxRldckv19WP kUlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Abto7qIi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id a18-20020a170902ecd200b001cffbe81706si6047799plh.354.2023.12.11.01.40.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 01:40:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Abto7qIi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 79A6C8055AF5; Mon, 11 Dec 2023 01:40:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234468AbjLKJjr (ORCPT + 99 others); Mon, 11 Dec 2023 04:39:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbjLKJjq (ORCPT ); Mon, 11 Dec 2023 04:39:46 -0500 Received: from mail-yw1-x1144.google.com (mail-yw1-x1144.google.com [IPv6:2607:f8b0:4864:20::1144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58637CE; Mon, 11 Dec 2023 01:39:52 -0800 (PST) Received: by mail-yw1-x1144.google.com with SMTP id 00721157ae682-5d40c728fc4so30926507b3.1; Mon, 11 Dec 2023 01:39:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702287591; x=1702892391; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2/PILXw+sfhUyZ9V8GULOr4bqu2Prc7VK3XM3govllA=; b=Abto7qIif+7Nnjp5qHSn01Rv/nK1cidsFPW8W5thIf4wEleR/BirTS1K5NJ8ZSFXop jFodXpz5zTwEAqW+P1/Q+rqYbiBhF/ssTLS57H+leiRvtQ+qFWbQSPeztnhP2CTzfbJu A628P9ZyK9fBIkxU6+p1l1WvxuB50Jm5xeZ+89ktAXDSBzu/CPZI45/W4kUaskobbnlK GgPYXtuBkfRIajIWB8zJTDaSxB1heNf/aX+se/9n0ovFEq+LmTj1Et6A++TT822GiaZk qo2arHkJpiHcNUjTgEaE8/JKIQFVOrzgyHYgWtOYFP8QSUtjnoM+42fCQn1NUiaLVHiP pdvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702287591; x=1702892391; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2/PILXw+sfhUyZ9V8GULOr4bqu2Prc7VK3XM3govllA=; b=lgrvRDshJWaCk1Izho5KvPbk3wV2DYFwn2tX7JY7rsXx4BYq9PSeO3YQQOK44vGNDV 9VnxGwSWcXHoJMrkNvNP+YaIjWiAJ6jTRMOZDL5Q7GL4Bin7ylQ92O8bniUNvqG0WhT5 j/46065p+vrfqeIfalOl4RmvmWXxvFiItVnfZP0/BT2XoNxPfMQXCKGcAZycX39Lz1EQ IfYfugMbAxnwrbuSgt+6Xeuk0TyTBMQAaBQa0Ts7Tergz+Q0ZCJZDvdzsWnXmU+LnzRb rOP/J5Y1+IkgvV9ekpjM5OHudRU17FbIG1JwqrA5MVBxGlWTTkgSZcG4DpKp7/Yr0eVm sQdg== X-Gm-Message-State: AOJu0YwyP6Ks+qAy8cCWaZVYPY+3YPI74nxaaZw3W6D5MVvN12YycfL2 sE66fzJxjLzryg6R0i6khgvoGRSpjdp156m8D2Y= X-Received: by 2002:a0d:d743:0:b0:5d3:6042:ced9 with SMTP id z64-20020a0dd743000000b005d36042ced9mr2712396ywd.5.1702287591324; Mon, 11 Dec 2023 01:39:51 -0800 (PST) MIME-Version: 1.0 References: <20231210130001.2050847-1-menglong8.dong@gmail.com> <4457e84f-4417-4a60-a814-9288b0756d91@linux.dev> In-Reply-To: <4457e84f-4417-4a60-a814-9288b0756d91@linux.dev> From: Menglong Dong Date: Mon, 11 Dec 2023 17:39:40 +0800 Message-ID: Subject: Re: [PATCH bpf-next] bpf: make the verifier trace the "not qeual" for regs To: Yonghong Song Cc: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, song@kernel.org, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 11 Dec 2023 01:40:03 -0800 (PST) Hello, On Mon, Dec 11, 2023 at 1:09=E2=80=AFPM Yonghong Song wrote: > > > On 12/10/23 5:00 AM, Menglong Dong wrote: > > We can derive some new information for BPF_JNE in regs_refine_cond_op()= . > > Take following code for example: > > > > /* The type of "a" is u16 */ > > if (a > 0 && a < 100) { > > /* the range of the register for a is [0, 99], not [1, 99], > > * and will cause the following error: > > * > > * invalid zero-sized read > > * > > * as a can be 0. > > */ > > bpf_skb_store_bytes(skb, xx, xx, a, 0); > > } > > Could you have a C test to demonstrate this example? > Also, you should have a set of inline asm code (progs/verifier*.c) > to test various cases as in mark_reg32_not_equal() and > mark_reg_not_equal(). > Yeah! I found that this part is tested in the test_progs/reg_bounds_crafted too, and this commit failed that test case, which I should fix in the next version. > > > > In the code above, "a > 0" will be compiled to "jmp xxx if a =3D=3D 0".= In the > > TRUE branch, the dst_reg will be marked as known to 0. However, in the > > fallthrough(FALSE) branch, the dst_reg will not be handled, which makes > > the [min, max] for a is [0, 99], not [1, 99]. > > > > For BPF_JNE, we can reduce the range of the dst reg if the src reg is a > > const and is exactly the edge of the dst reg. > > > > Signed-off-by: Menglong Dong > > --- > > kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++++++++++++++++= - > > 1 file changed, 44 insertions(+), 1 deletion(-) > > > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > > index 727a59e4a647..7b074ac93190 100644 > > --- a/kernel/bpf/verifier.c > > +++ b/kernel/bpf/verifier.c > > @@ -1764,6 +1764,40 @@ static void __mark_reg_const_zero(struct bpf_reg= _state *reg) > > reg->type =3D SCALAR_VALUE; > > } > > > > +#define CHECK_REG_MIN(value) \ > > +do { \ > > + if ((value) =3D=3D (typeof(value))imm) \ > > + value++; \ > > +} while (0) > > + > > +#define CHECK_REG_MAX(value) \ > > +do { \ > > + if ((value) =3D=3D (typeof(value))imm) \ > > + value--; \ > > +} while (0) > > + > > +static void mark_reg32_not_equal(struct bpf_reg_state *reg, u64 imm) > > +{ > > What if reg->s32_min_value =3D=3D imm and reg->s32_max_value =3D=3D imm? > Has this been handled in previous verifier logic? Will such a case happen? In current code path, the src reg is a const, and the is_branch_taken() will return 0 or 1 if the dst_reg->s32_min_value =3D=3D dst_reg->s32_max_value. Enn......maybe we can do more checking here in case that someone calls this function in another place. Thanks! Menglong Dong > > > + CHECK_REG_MIN(reg->s32_min_value); > > + CHECK_REG_MAX(reg->s32_max_value); > > + CHECK_REG_MIN(reg->u32_min_value); > > + CHECK_REG_MAX(reg->u32_max_value); > > +} > > + > > +static void mark_reg_not_equal(struct bpf_reg_state *reg, u64 imm) > > +{ > > + CHECK_REG_MIN(reg->smin_value); > > + CHECK_REG_MAX(reg->smax_value); > > + > > + CHECK_REG_MIN(reg->umin_value); > > + CHECK_REG_MAX(reg->umax_value); > > + > > + CHECK_REG_MIN(reg->s32_min_value); > > + CHECK_REG_MAX(reg->s32_max_value); > > + CHECK_REG_MIN(reg->u32_min_value); > > + CHECK_REG_MAX(reg->u32_max_value); > > +} > > + > > static void mark_reg_known_zero(struct bpf_verifier_env *env, > > struct bpf_reg_state *regs, u32 regno) > > { > > @@ -14332,7 +14366,16 @@ static void regs_refine_cond_op(struct bpf_reg= _state *reg1, struct bpf_reg_state > > } > > break; > > case BPF_JNE: > > - /* we don't derive any new information for inequality yet= */ > > + /* try to recompute the bound of reg1 if reg2 is a const = and > > + * is exactly the edge of reg1. > > + */ > > + if (is_reg_const(reg2, is_jmp32)) { > > + val =3D reg_const_value(reg2, is_jmp32); > > + if (is_jmp32) > > + mark_reg32_not_equal(reg1, val); > > + else > > + mark_reg_not_equal(reg1, val); > > + } > > break; > > case BPF_JSET: > > if (!is_reg_const(reg2, is_jmp32))