Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5046382rdb; Tue, 12 Dec 2023 18:11:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4eyotKOVpO8kdCMOqtXux3nKMWCqjMwwSVea4UlgGQpNQL5PM7b/IM0Snj+veM8d4uoW3 X-Received: by 2002:a17:90b:4b4b:b0:286:6cc1:2cb8 with SMTP id mi11-20020a17090b4b4b00b002866cc12cb8mr3528015pjb.66.1702433516230; Tue, 12 Dec 2023 18:11:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702433516; cv=none; d=google.com; s=arc-20160816; b=EUGx/dSuFItnmm3s3dxPxgRI9LNKzrwRJbedTCnnqR9tCU8Mf1syqwPLHtrwJV1aeV dpTOI4QdGdxXms9IPxDHdZfPlJttSoBtGvAJRBCP8QvXGvaK85IpGsyk7QDkqEeB96wt nryJ8h8Cgbqu3hI3b0p3fm6NZcZF15gJYGyA+XP0PcwnQb7uyEOGuPmrCSKOfkaKO+5F VZwhHbztWCVhpn+DFe/lnyJ5ts8CVL11RT5qkiO2EgowDn4Nt4HmIMN+x4nNknVe5Qts 9qRbh/cgqkx7ANzFKkGt3UpGQ1BW5Gi5ZQzO3tM2pPnJWNRMkCW6+N0ByKb0hZQDgMhM T6rg== 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=NjrnQeaFObARf6rt/zsrrtTxUbhD/ynal+Up2fZC/58=; fh=Y+2n8KeSOn2tlvxAhXci8Z1zD5CwrbX7pW90DcyfFuo=; b=G9q11CiA54jRcSFNyK1MDJ8UWpQdyljGelZS+Jg05qxu4Vt/qDYeqnYbqoNpQ4ScEy Zr9MZG1Kv4FV3Hfi78R3DtGz2qgqLneex6mVdjVUs9Wf8QHGACZjdh9SbzISIHOBLAdR pKNwrnH1n3ZFWzt0yx/dtG4omqxBORe/1fU2CVE1WVQKx1FQQvFtQ33X9uMQv3zfFktK mehyn6aG94jd0OxHbhYT8N+zG0J+ZGc6H+QjF21UBBqictF4nlOJ2mFoqHoa0xAaxR+T 7EuIC2Z7mspNKnjMz4UBCWoTL8iLJsrkTYlzcFUil2ZzwSVeGN1gOZ3WwwJzo+ojfF2+ 6dUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="f/R/JS8z"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t11-20020a17090abc4b00b0028ad5886876si963092pjv.83.2023.12.12.18.11.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 18:11:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="f/R/JS8z"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 30D198040EBE; Tue, 12 Dec 2023 18:11:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378252AbjLMCLo (ORCPT + 99 others); Tue, 12 Dec 2023 21:11:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378253AbjLMCLn (ORCPT ); Tue, 12 Dec 2023 21:11:43 -0500 Received: from mail-yw1-x1141.google.com (mail-yw1-x1141.google.com [IPv6:2607:f8b0:4864:20::1141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 693B8CD; Tue, 12 Dec 2023 18:11:49 -0800 (PST) Received: by mail-yw1-x1141.google.com with SMTP id 00721157ae682-5c85e8fdd2dso62594137b3.2; Tue, 12 Dec 2023 18:11:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702433508; x=1703038308; 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=NjrnQeaFObARf6rt/zsrrtTxUbhD/ynal+Up2fZC/58=; b=f/R/JS8z5Tg5/my9XH70L3izyHbk5vk3FS1jmCqlHmM2DwWNODb3Tq4ZIww7UW4thY fhnkz7qfL5p9XbkT70WEwAWR+5V9hHzfPiD29my7bQH/sGor26vKuQC+RItKT4KS9UM+ us7L2NVLRsvhFG7YH/UuVg+qcvYBB3VfrtGI4DwCJw0BqCSe3HKIvrTSCOLzHwXE3EDc rd9ktstl+PomEBK70NI7cI9Mv8IfFiTqper8QBL/xM831sWgnl5g0HfVTECTJhGs7g9F YGxoa27jHatb6DUxdjhk2BjSHDdrHDLNNWcMtEaitu8ZdFEFpLeWVDqCk7OzL9jtELoH pw5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702433508; x=1703038308; 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=NjrnQeaFObARf6rt/zsrrtTxUbhD/ynal+Up2fZC/58=; b=h5lZHY22oNkJJLMtiW6rlLfadRqV4wiuO79/wqt+PPc7EV0js6cLMTyVO3KFkHg24p ntDureyg7nfVKxrRnCV674PtDSCYy3/EuBzWRzEg2vadWwhqiRVGFUOPZO7Igpb9dPyE prhWj8/HM7rQPEholRQP4Ax9QxTug2wWuryB/f5ODenaZ4xDRao3skMlA4xDUV398mKr 8yS0YA9SwcEEKzzV8dvY7sYHxIW3ZPG8iVOw6oGiEVnmrttoBhDBPGmuE5+YS+rgDpqY dkpi9FrsSVbHyQgvhnSW1ZUw2MC3APAgySxIwKaSwRcIfhK21XAeVkf75M0AsFxm+4m9 qONg== X-Gm-Message-State: AOJu0YxSz4C7lXyTEuIGC79Au1tqzBGYtkdd2vmWq/rFZXt4g6rfZYel S5na78jbcrJMvJF089bRd/uaF7gPlMQn/+WwGTY= X-Received: by 2002:a0d:d8d6:0:b0:5e2:cfac:6492 with SMTP id a205-20020a0dd8d6000000b005e2cfac6492mr58513ywe.41.1702433508532; Tue, 12 Dec 2023 18:11:48 -0800 (PST) MIME-Version: 1.0 References: <20231212131031.3088661-1-menglong8.dong@gmail.com> <20231212131031.3088661-2-menglong8.dong@gmail.com> In-Reply-To: From: Menglong Dong Date: Wed, 13 Dec 2023 10:11:37 +0800 Message-ID: Subject: Re: [PATCH net-next v2 1/2] bpf: make the verifier trace the "not qeual" for regs To: Eduard Zingerman Cc: andrii@kernel.org, yonghong.song@linux.dev, 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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 18:11:55 -0800 (PST) On Wed, Dec 13, 2023 at 7:23=E2=80=AFAM Eduard Zingerman wrote: > > On Tue, 2023-12-12 at 21:10 +0800, 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); > > } > > > > 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 > > --- > > Acked-by: Eduard Zingerman > > > kernel/bpf/verifier.c | 29 ++++++++++++++++++++++++++++- > > 1 file changed, 28 insertions(+), 1 deletion(-) > > > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > > index 727a59e4a647..08ee0e02df96 100644 > > --- a/kernel/bpf/verifier.c > > +++ b/kernel/bpf/verifier.c > > @@ -14332,7 +14332,34 @@ 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= */ > > + if (!is_reg_const(reg2, is_jmp32)) > > + swap(reg1, reg2); > > + if (!is_reg_const(reg2, is_jmp32)) > > + break; > > + > > + /* try to recompute the bound of reg1 if reg2 is a const = and > > + * is exactly the edge of reg1. > > + */ > > + val =3D reg_const_value(reg2, is_jmp32); > > + if (is_jmp32) { > > + if (reg1->u32_min_value =3D=3D (u32)val) > > + reg1->u32_min_value++; > > Nit: I spent an unreasonable amount of time trying to figure out if > overflow might be an issue here. Would it be helpful to add a > comment like below? (not sure, maybe it's obvious and I'm being slow= ) > > /* u32_min_value is not equal to 0xffffffff at this point, > * because otherwise u32_max_value is 0xffffffff as well, > * in such a case both reg1 and reg2 would be constants, > * jump would be predicted and reg_set_min_max() won't > * be called. > * Same reasoning works for all {u,s}{min,max}{32,64} cases below. > */ Okay, I'll add this comment in the next version. Thanks! Menglong Dong