From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 0B902313273 for ; Mon, 3 Nov 2025 18:09:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762193369; cv=none; b=RYQ2SWiWUDbcX421Th0kfeaWB/XF9ZBuFDxptlNl3VRuSiUxkl5rXuzX63FwaMpC7qtl4OfAeO9JQ3dCGmumFQh3b07KKOmZ6xmnh5iUaDCm7iiYwObSN2eClmb0/fH6Jd97cbzs5XZYER4S3xq9IXh6SyySBrYDNdUPk9cH4CQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762193369; c=relaxed/simple; bh=dSZOek6ygkyM8kMLRCDVZKMdCPYWkaopn/XnCYI/Vb8=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=WUavSwq0lvwC9faf/rpbgLaWMtLMteda7r96R4QKuf8wLUgYjI3TFc+jtJ5NZk8D4lCdLrfN9J1tbHUvWUwSqq22BvceS1PAYBjSaSs7Dv21a6xtVYhUiD+pTUP5LVFGVVRpNHh+3Qj8Fgm98FcYCKdSNC1yKTbFaqa7YVlMXTM= 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=UnKrqmyv; arc=none smtp.client-ip=209.85.214.170 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="UnKrqmyv" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2951a817541so48437215ad.2 for ; Mon, 03 Nov 2025 10:09:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762193367; x=1762798167; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=6GMq24IVk2F3a28aOoFMSN6wPIMIYmOKkHFdluZNlp8=; b=UnKrqmyvy6DUp++piPtr188PfaI7oNGWMAM7vbHHUoa8Z9eZ0AnIrrEfDDpRdehICM SoAstZCyWWNpzruPp7eFAMTwRquJwBlqtZL56KudJ8l8Kb3l90Nkx4vxII++gEAjpC90 wkr+DtJr11CPMuL+LYZFP70v+H3ETgpNRqooepuWnrSZlFGu3OuFUkfcYWLnM9hNW+QU S8XAKoQ9pJq4TB+PeDtcBwoAZGrKV9T6HI3AJx2PPG1HqBz4Yhd8M0NDjAeDYdhdFIqr wJBRtfK87pOqOYL5EnjZWVoBQrCIMbKE/PacszEDIpNKzu2Yrs7zQOMPSNXizjJtOZQB mwJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762193367; x=1762798167; h=mime-version:user-agent:content-transfer-encoding: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=6GMq24IVk2F3a28aOoFMSN6wPIMIYmOKkHFdluZNlp8=; b=F7mr16AyxS0YH47KE76j4GesZxhffJSRuCu3r41BxWjUUDVaB/tPb0RqDH8oF5kpGN hIQp1i43wLEy7+EvAJ60iH1YKPiMnKuoKkYxS15NhJN82+olcNJrZ/D+Ht9RKke60pTD 0YNZyy32lwguRRkmnbt8Aty15Qkuqvmi5IPUHnknmxsfZb37LzVvKwSI+BaSajrEM3iD 9IMn03jtAgYn8anxjWePG57q9FZrTs/DDmNVTZI+U9MDnfegvTejgINtkY0n2vmlmdi1 F8PkIFUt/Abykc5HCKR01vdB6/XztTfTCR0/Ug51qUVFsTS0Fyn+IESvmlc3OrCzt7q9 GBow== X-Forwarded-Encrypted: i=1; AJvYcCX7gLCqHxql8NSLQ5Dg827SME6HUVKxol1iemykdSjwM24mNgVPPxlXD9WpbqswKFx85oc6+Iprcv1HkPc=@vger.kernel.org X-Gm-Message-State: AOJu0YxWYycLEEre9OcJrEfVzq08wbstdAu/WjJfGxX4IZTNyZmj0g78 yahpO7dpLtvPZSJ9tOh+JP7rxJzQXY38cpI0P/4DlBmowMajQNDwDK0+ X-Gm-Gg: ASbGnctUFkH+a3I2GFBIOhkHvzZRvPmwTuI04va4/qmeDM7oE5olJZ3U8opED2GocDJ jLfxCA43LBb14YDlKq3MvwnK0CItVhR+tPxaEPqypIQIgn/chLjvXAf/jP6SWeNaZlEv8btpNHh QL4i4iNa2FWjPlVw0pWoIPOc6jWiGBmmtLlI0aJSuqr9zqylHGQWJfn2s5sIX8X9xbuMIkbgkf6 YJj9oxees+/EMKC23iQ2p94SXhoaonqv3VNq8kh/qXoxwIlwiOqjz5Rzs8kea8VxMznkhwNAGk4 yK5RXGMFCUyed+mhSD1spP8mx0marLScbIMVWdORW5PyYYz0UHHy1iVvK93ZUwca0RdK363JGFN HEO0WLZg/ACDZv6NonUM8yjFZ9w2jR9xciUBTDPBlDpwhpmpD391S50A8yzp9avzkR7NzpXTY1N PtfEj22kG9aAeXnVYgjdUgAk0yag== X-Google-Smtp-Source: AGHT+IGZrfXVBo54SYedR/Q9MDC4tdP/p2CLo6tbKfTLBQnHa03b/gxt/4AS9fp/eW4cLQ/lJm333A== X-Received: by 2002:a17:902:da87:b0:290:c94b:8381 with SMTP id d9443c01a7336-2951a3905e9mr148631275ad.7.1762193367176; Mon, 03 Nov 2025 10:09:27 -0800 (PST) Received: from ?IPv6:2a03:83e0:115c:1:3eb6:963c:67a2:5992? ([2620:10d:c090:500::5:d721]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-341760c9476sm14136a91.4.2025.11.03.10.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Nov 2025 10:09:26 -0800 (PST) Message-ID: Subject: Re: [PATCH bpf-next v4 1/2] bpf: Skip bounds adjustment for conditional jumps on same scalar register From: Eduard Zingerman To: KaFai Wan , ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, shuah@kernel.org, paul.chaignon@gmail.com, m.shachnai@gmail.com, harishankar.vishwanathan@gmail.com, colin.i.king@gmail.com, luis.gerhorst@fau.de, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Kaiyan Mei , Yinhao Hu Date: Mon, 03 Nov 2025 10:09:23 -0800 In-Reply-To: <20251103063108.1111764-2-kafai.wan@linux.dev> References: <20251103063108.1111764-1-kafai.wan@linux.dev> <20251103063108.1111764-2-kafai.wan@linux.dev> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-1.fc42) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Mon, 2025-11-03 at 14:31 +0800, KaFai Wan wrote: > When conditional jumps are performed on the same scalar register > (e.g., r0 <=3D r0, r0 > r0, r0 < r0), the BPF verifier incorrectly > attempts to adjust the register's min/max bounds. This leads to > invalid range bounds and triggers a BUG warning. >=20 > The problematic BPF program: > 0: call bpf_get_prandom_u32 > 1: w8 =3D 0x80000000 > 2: r0 &=3D r8 > 3: if r0 > r0 goto >=20 > The instruction 3 triggers kernel warning: > 3: if r0 > r0 goto > true_reg1: range bounds violation u64=3D[0x1, 0x0] s64=3D[0x1, 0x0] u3= 2=3D[0x1, 0x0] s32=3D[0x1, 0x0] var_off=3D(0x0, 0x0) > true_reg2: const tnum out of sync with range bounds u64=3D[0x0, 0xffff= ffffffffffff] s64=3D[0x8000000000000000, 0x7fffffffffffffff] var_off=3D(0x0= , 0x0) >=20 > Comparing a register with itself should not change its bounds and > for most comparison operations, comparing a register with itself has > a known result (e.g., r0 =3D=3D r0 is always true, r0 < r0 is always fals= e). >=20 > Fix this by: > 1. Enhance is_scalar_branch_taken() to properly handle branch direction > computation for same register comparisons across all BPF jump operatio= ns > 2. Adds early return in reg_set_min_max() to avoid bounds adjustment > for unknown branch directions (e.g., BPF_JSET) on the same register >=20 > The fix ensures that unnecessary bounds adjustments are skipped, preventi= ng > the verifier bug while maintaining correct branch direction analysis. >=20 > Reported-by: Kaiyan Mei > Reported-by: Yinhao Hu > Closes: https://lore.kernel.org/all/1881f0f5.300df.199f2576a01.Coremail.k= aiyanm@hust.edu.cn/ > Signed-off-by: KaFai Wan > --- Acked-by: Eduard Zingerman [...]