Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4978254rdb; Tue, 12 Dec 2023 15:23:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IFT5l/YL2/MMYScRnVsBtUDNMmr0HaAZI85qdNBkdRLIrs4wxEMzI44I9OrdjzaTQqxYyx7 X-Received: by 2002:a05:6a20:13d9:b0:190:e402:8bc6 with SMTP id ho25-20020a056a2013d900b00190e4028bc6mr4594891pzc.43.1702423431348; Tue, 12 Dec 2023 15:23:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702423431; cv=none; d=google.com; s=arc-20160816; b=nj6bEzYBIodg23kIOxL50nbgeJm4qdDG+bVonnBUE8iR4kAL5hGTKXSdEeoQd++7jb F90wjK4b4kzdPJ4x9CpEW5b8zBbGQ6ft1LmBRxUv2zRpIE7onzwW2uDTLbqZvuEZ4TtR EgOY7v0JuHJET78qXhYYAtCHo3sLHQhVUqh1e2ETHOHgx+6vezGCRKPP6jYfE2eLCqf+ 2IsKU6yjmGuKXMKNcn+8bRlyEJ3XKjxxpBJ/1WR/tw65rmp7HYqVMTCBfAca+B2O8mjm rrGHgjVtKEQxX9ODTv1X7pVoCto9uqOk8W+SVn1Gy/Jav0hQHnN7QKALkAIPm52UAqT3 Aegg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:autocrypt:references:in-reply-to:date:cc :to:from:subject:message-id:dkim-signature; bh=hMwDgbTyxwfql3aF4QbaoSSK24jbTU8Cu669x3rTF7M=; fh=0JeQhKyWHMSbsAVUtm4jhmzLTkmKL/a3ojFihpTNslc=; b=Aon80oJWpWayTWZ/7Iae5oeFZV4J06tkDmK2cy4h0JQ+EgEi7f2wsRBGN+NVWyCCjP kSwSmsOz9FC/HIhJzwmm5T0v2HTcrqBhPicz4HRKXKd2KYuOl4ogJSDALfCw9tskMkHt aQxJ3a3F5LinlPXKOIwCJ8f7S8I9ApxUmhc++1zcH8PVB4OLDFWB5IpMy1eFWMs1tSgu Dvj85wXzx7bswSkxyN/HpTDrXCpz9IjcvSEojvnf5oxvlKFPgYIC1vY058zcooBuWyll NMVGia3wcRk0XOFhED7/DUIPf/Ft180IY7RXqFojBhSwCrCBDM11JfPCh/i1gOmn48rI +P2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AAU6orwM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q28-20020a056a0002bc00b006ce7ff254c2si8417191pfs.368.2023.12.12.15.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 15:23:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AAU6orwM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id B393E804C23C; Tue, 12 Dec 2023 15:23:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378014AbjLLXXc (ORCPT + 99 others); Tue, 12 Dec 2023 18:23:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377895AbjLLXXb (ORCPT ); Tue, 12 Dec 2023 18:23:31 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE180AA; Tue, 12 Dec 2023 15:23:37 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-40c4846847eso25918195e9.1; Tue, 12 Dec 2023 15:23:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702423416; x=1703028216; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to; bh=hMwDgbTyxwfql3aF4QbaoSSK24jbTU8Cu669x3rTF7M=; b=AAU6orwMPWc+NXbuZN1x717j65GSCgTkWsm2mLUWQK1aYJBcRJQNPUSkjynDWTchA5 gwDez6vYV0EvkXZBMyt1txTMZ6F+eoewKe+n6rSZIymXV6U8RVS0o2X8+j7kxqWN1MEX i+fRMYQ/abV8H73oEduhgGcn+s4IMiTs2kDC0xoTfqzL/1HJtzdX4FIXiBPWhabsGGOV bHWjl9g9cdutnGZcaI2NunyEpXeMsn+5nFShOwdWA7VoSUUZvP8nMpAyF7MSNpH82QRY lMGlWL20CE1tyOJk3I1DKVhHZN23VO3RQWABSVjHze4vbQVlBrDTDpMoHodSMswdiYCw BHtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702423416; x=1703028216; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hMwDgbTyxwfql3aF4QbaoSSK24jbTU8Cu669x3rTF7M=; b=nCoam/pcXd+DDBW1J2STdcZYobt2rAfMQqKn6jFnyZrNLLBMYBdVlZPB4XhZIXtvqL prylJXcpMyaBIz+9lngu8XY1L3tkBK8lOe7gQBWEUZ2f8FhSXgc6pFz8WaBic9mbMPqE 8H530XR2tOkXs14XCZRABNk/xhar9LsX3EmiXME0o2UsvzzKvCNWQIeD7MkPxYIrnwnF 0C5TSJkahTw/+QHyWB04l3pFEeSyptoW3atWtmg+sh76XutHhExXoxD/d5eDO+JcY4f3 1jaorCOk2OWwl4qQ9ADsxqgBU1d3GgzW+ipQJ8KRA0uBqZBppP/0a+4NKZ4vYgi6FUAA kK6g== X-Gm-Message-State: AOJu0Yxg2EXLPGipxRCWbrlrCpF9XR4PZeVvn0r6ESUH/mbhGB9z976W 6tGzY2cx5H6rpfyKnZOkBqY= X-Received: by 2002:a05:600c:2a43:b0:40c:30e8:fc5f with SMTP id x3-20020a05600c2a4300b0040c30e8fc5fmr3764410wme.90.1702423416089; Tue, 12 Dec 2023 15:23:36 -0800 (PST) Received: from [192.168.1.95] (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id h12-20020a05600c314c00b0040c45cabc34sm9792660wmo.17.2023.12.12.15.23.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 15:23:35 -0800 (PST) Message-ID: Subject: Re: [PATCH net-next v2 1/2] bpf: make the verifier trace the "not qeual" for regs From: Eduard Zingerman To: Menglong Dong , andrii@kernel.org, yonghong.song@linux.dev Cc: 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 Date: Wed, 13 Dec 2023 01:23:34 +0200 In-Reply-To: <20231212131031.3088661-2-menglong8.dong@gmail.com> References: <20231212131031.3088661-1-menglong8.dong@gmail.com> <20231212131031.3088661-2-menglong8.dong@gmail.com> Autocrypt: addr=eddyz87@gmail.com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m749ZNLA0L2dMy1AKuOqH+/B+/ImBfJMDjmdyJQ8WU/OFRuGLdqOd2oZrA1iuPIa+yUYyZkaZfz/emQwpIL1+Q4p1R/OplA4yc301AqruXXUcVDbEB+joHW3hy5FwK5t5OwTKatrSJBkydSF9zdXy98fYzGniRyRA65P0Ix/8J3BYB4edY2/w0Ip/mdYsYQljBY0A== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.50.1 MIME-Version: 1.0 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 groat.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 (groat.vger.email [0.0.0.0]); Tue, 12 Dec 2023 15:23:48 -0800 (PST) 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: >=20 > /* 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); > } >=20 > In the code above, "a > 0" will be compiled to "jmp xxx if a =3D=3D 0". I= n 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]. >=20 > 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. >=20 > Signed-off-by: Menglong Dong > --- Acked-by: Eduard Zingerman > kernel/bpf/verifier.c | 29 ++++++++++++++++++++++++++++- > 1 file changed, 28 insertions(+), 1 deletion(-) >=20 > 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_s= tate *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) =20 /* 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. */