Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6351693rdb; Thu, 14 Dec 2023 16:17:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IHxbzB/BppcXfx0nBoX9X7sf7okB05MQF9Emv7hFAhaRkRTkMtzbWNMC28Nj6sxwC02dEh9 X-Received: by 2002:a17:906:2d9:b0:a1f:9c0b:18b3 with SMTP id 25-20020a17090602d900b00a1f9c0b18b3mr3758519ejk.3.1702599430780; Thu, 14 Dec 2023 16:17:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702599430; cv=none; d=google.com; s=arc-20160816; b=I2KuURwEYBctaIo5vdROmgdXqyR3/kvn/r2yYzct0acNJoCz+4+TTLLmjIUGJ/ERWy m3vx0/EJxNqfxXxJagAdbY/tEWK1K3bcMWmYHEYupGSG4IgjqYbutyOKwrLPyqEptEiV pLBKXfW/pT57J4CsBh5Q8vnaqOrO25xuAE0ACYVUe6wVZpwU9O9cw6HPbXv7tvVycUhD LZ9VmLyeWXWtaK9+F+biXWmBKj6FhQB8uPdTRzxnL6OJUQ7y4zlKxm/KiFcICrrXT754 FLOkuZrCI7wm+L1YrcrRzuMGK6Hws6dCEtQyk4sPL6Ae9PQSjbRgynly4zYvB136cFeu fqqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:content-transfer-encoding:autocrypt:references :in-reply-to:date:cc:to:from:subject:message-id:dkim-signature; bh=S2lp22ZpE1Er+qtE5Fj0a/2zBr3ofAMY8jmSn3bat1c=; fh=nQ9gCoVOgTX+nKL2EWvE6Z3UYlb+dJxelCnXv4/64NE=; b=QGh1VkRSvhllyZXNI2c8H/ms8yC2G3TMZG1k20PrQC/9A2sFnDBCIUXMpefdgYF5dl IZPn3H/50gMINdrTGyEKVel+ChcS3v+i9wzpQaDwPrs9AsX6n49NhRIivY/27wAjX/2f dZJ8rL/qIWbpFgkaXpvXB1r51TGi8Lld7/lE5KMu4iL4YVStOalzdKlhuLtNtGX/Or6v lX/4XOqa9g2qpDuxRpylseDnTZ8U3WFATcdvVIjf63oXGwF8sK1sMSTIJNAgrW+rpwhZ QgFzwxv1eaUxiSFEpKcB0EGVvBrt7a9bc00kzI40TAP1zQVlz86kDCr9u+tgwXXM1r+F 3WLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="cRDo/JUd"; spf=pass (google.com: domain of linux-kernel+bounces-281-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-281-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r23-20020a1709067fd700b00a2318ee50fesi51932ejs.320.2023.12.14.16.17.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 16:17:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-281-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="cRDo/JUd"; spf=pass (google.com: domain of linux-kernel+bounces-281-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-281-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 55F911F2283E for ; Fri, 15 Dec 2023 00:17:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5DB537EA; Fri, 15 Dec 2023 00:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cRDo/JUd" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 E8B2D363; Fri, 15 Dec 2023 00:16:58 +0000 (UTC) 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 Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-333536432e0so61916f8f.3; Thu, 14 Dec 2023 16:16:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702599417; x=1703204217; 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=S2lp22ZpE1Er+qtE5Fj0a/2zBr3ofAMY8jmSn3bat1c=; b=cRDo/JUd4nDPIAg6hvFrShevi+9/iXait5t+wUEy1y+fhHolTCsjpPDwOMk3/TF1Uj ZS0xP4vMxDQ//YQuwOeAVCjN6Wow0W+kj28WSOlQ8DsjSHVneSH7B1SxJFQJNq47RcpD Tzkzf7Fi2/h009ghpW95f1QkE7MS8fZmt84ZWH2C2paTKJYJoQwfPKeD0jtWnzCpyaIJ FE54yF/dWfQxLee7hI8vQFhFblJo0edUD9h8BjuPpoW2jFccLn0t0hS8JL64eBxk27ng isJ4iP1vMoNzPh+HcxX/eG4K3SHRBFkLVjyKiTfd7lPZaU00lBZG7spGntZEGpfvfHcF 7SFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702599417; x=1703204217; 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=S2lp22ZpE1Er+qtE5Fj0a/2zBr3ofAMY8jmSn3bat1c=; b=BVR8pAoK7S4WJ9oSkXcCkMX4CvLqn7DhMp2+fN22pqNXmtHOFUwZ2Pyzv5zOJq2czD VeZ8ry30bDsdiUJaUKKtfY2iccPRpXBRWA64nKkw4AdJquWMlMh6i/4GWRtmSXLDFRdy FnlVgRAlJC4XA9s2rOrH/synJmn327dFcnwA0pfKj36woazEUI9ihr48oIPged/C3AN5 7mVXLaqoNsd6LNGcts7kSgSFkwnf78IDUW7RW/3JSLK4PmTNL6Ku8H+3JNpeHpu7QVwG DeUV7j3vNn0LXtevOuUATBU0zUWGjNE2KCGrmUJnT1owSC35a60mx8nC5nv+X2YMW3dT 451Q== X-Gm-Message-State: AOJu0YxMVsE876F9d178xOu5V4NYDT0rkDRNW976gtuBlQcmJhGKfC00 CviyelBExT7ECWQkjI8rLMk= X-Received: by 2002:a5d:5682:0:b0:336:4a5c:e130 with SMTP id f2-20020a5d5682000000b003364a5ce130mr721054wrv.1.1702599416874; Thu, 14 Dec 2023 16:16:56 -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 z10-20020adfe54a000000b00336491ed33asm2707329wrm.99.2023.12.14.16.16.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 16:16:56 -0800 (PST) Message-ID: <07f0eb0f01b7e02ab5896f804359785bfa0e716f.camel@gmail.com> Subject: Re: [Bug Report] bpf: incorrectly pruning runtime execution path From: Eduard Zingerman To: Andrii Nakryiko Cc: Hao Sun , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , bpf , Linux Kernel Mailing List Date: Fri, 15 Dec 2023 02:16:55 +0200 In-Reply-To: References: <480a5cfefc23446f7c82c5b87eef6306364132b9.camel@gmail.com> <917DAD9F-8697-45B8-8890-D33393F6CDF1@gmail.com> <9dee19c7d39795242c15b2f7aa56fb4a6c3ebffa.camel@gmail.com> <73d021e3f77161668aae833e478b210ed5cd2f4d.camel@gmail.com> Autocrypt: addr=eddyz87@gmail.com; prefer-encrypt=mutual; keydata=mQGNBGKNNQEBDACwcUNXZOGTzn4rr7Sd18SA5Wv0Wna/ONE0ZwZEx+sIjyGrPOIhR14/DsOr3ZJer9UJ/WAJwbxOBj6E5Y2iF7grehljNbLr/jMjzPJ+hJpfOEAb5xjCB8xIqDoric1WRcCaRB+tDSk7jcsIIiMish0diTK3qTdu4MB6i/sh4aeFs2nifkNi3LdBuk8Xnk+RJHRoKFJ+C+EoSmQPuDQIRaF9N2m4yO0eG36N8jLwvUXnZzGvHkphoQ9ztbRJp58oh6xT7uH62m98OHbsVgzYKvHyBu/IU2ku5kVG9pLrFp25xfD4YdlMMkJH6l+jk+cpY0cvMTS1b6/g+1fyPM+uzD8Wy+9LtZ4PHwLZX+t4ONb/48i5AKq/jSsb5HWdciLuKEwlMyFAihZamZpEj+9n91NLPX4n7XeThXHaEvaeVVl4hfW/1Qsao7l1YjU/NCHuLaDeH4U1P59bagjwo9d1n5/PESeuD4QJFNqW+zkmE4tmyTZ6bPV6T5xdDRHeiITGc00AEQEAAbQkRWR1YXJkIFppbmdlcm1hbiA8ZWRkeXo4N0BnbWFpbC5jb20+iQHUBBMBCgA+FiEEx+6LrjApQyqnXCYELgxleklgRAkFAmKNNQECGwMFCQPCZwAFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQLgxleklgRAlWZAv/cJ5v3zlEyP0/jMKQBqbVCCHTirPEw+nqxbkeSO6r2FUds0NnGA9a6NPOpBH+qW7a6+n6q3sIbvH7jlss4pzLI7LYlDC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V7WfSBz712eFAynEzyeezPbwU8QBiTpZcHXwQni3IYKvsh7s t1iq+gsfnXbPz5AnS598ScZI1oP7OrPSFJkt/z4acEbOQDQs8aUqrd46PV jsdqGvKnXZxzylux29UTNby4jTlz9pNJM+wPrDRmGfchLDUmf6CffaUYCbu4FiId+9+dcTCDvxbABRy1C3OJ8QY7cxfJ+pEZW18fRJ0XCl/fiV/ecAOfB3HsqgTzAn555h0rkFgay0hAvMU/mAW/CFNSIxV397zm749ZNLA0L2dMy1AKuOqH+/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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Thu, 2023-12-14 at 16:06 -0800, Andrii Nakryiko wrote: > On Thu, Dec 14, 2023 at 8:26=E2=80=AFAM Eduard Zingerman wrote: > >=20 > > On Thu, 2023-12-14 at 17:10 +0200, Eduard Zingerman wrote: > > > [...] > > > > The reason why retval checks fails is that the way you disable dead > > > > code removal pass is not complete. Disable opt_remove_dead_code() > > > > just prevent the instruction #30 from being removed, but also note > > > > opt_hard_wire_dead_code_branches(), which convert conditional jump > > > > into unconditional one, so #30 is still skipped. > > > >=20 > > > > > Note that I tried this test with two functions: > > > > > - bpf_get_current_cgroup_id, with this function I get retval 2, n= ot 4 :) > > > > > - bpf_get_prandom_u32, with this function I get a random retval e= ach time. > > > > >=20 > > > > > What is the expectation when 'bpf_get_current_cgroup_id' is used? > > > > > That it is some known (to us) number, but verifier treats it as u= nknown scalar? > > > > >=20 > > > >=20 > > > > Either one would work, but to make #30 always taken, r0 should be > > > > non-zero. > > >=20 > > > Oh, thank you, I made opt_hard_wire_dead_code_branches() a noop, > > > replaced r0 =3D 0x4 by r0 /=3D 0 and see "divide error: 0000 [#1] PRE= EMPT SMP NOPTI" > > > error in the kernel log on every second or third run of the test > > > (when using prandom). > > >=20 > > > Working to minimize the test case will share results a bit later. > >=20 > > Here is the minimized version of the test: > > https://gist.github.com/eddyz87/fb4d3c7d5aabdc2ae247ed73fefccd32 > >=20 > > If executed several times: ./test_progs -vvv -a verifier_and/pruning_te= st > > it eventually crashes VM with the following error: > >=20 > > [ 2.039066] divide error: 0000 [#1] PREEMPT SMP NOPTI > > ... > > [ 2.039987] Call Trace: > > [ 2.039987] > > [ 2.039987] ? die+0x36/0x90 > > [ 2.039987] ? do_trap+0xdb/0x100 > > [ 2.039987] ? bpf_prog_32cfdb2c00b08250_pruning_test+0x4d/0x60 > > [ 2.039987] ? do_error_trap+0x7d/0x110 > > [ 2.039987] ? bpf_prog_32cfdb2c00b08250_pruning_test+0x4d/0x60 > > [ 2.039987] ? exc_divide_error+0x38/0x50 > > [ 2.039987] ? bpf_prog_32cfdb2c00b08250_pruning_test+0x4d/0x60 > > [ 2.039987] ? asm_exc_divide_error+0x1a/0x20 > > [ 2.039987] ? bpf_prog_32cfdb2c00b08250_pruning_test+0x4d/0x60 > > [ 2.039987] bpf_test_run+0x1b5/0x350 > > [ 2.039987] ? bpf_test_run+0x115/0x350 > > ... > >=20 > > I'll continue debugging this a bit later today. > >=20 >=20 > Great, thanks a lot, Eduard. Let's paste the program here for discussion: >=20 > ... >=20 I managed to minimize it a bit more, getting rid of r5, (not that it changes anything): SEC("socket") __success __flag(BPF_F_TEST_STATE_FREQ) __retval(42) __naked void pruning_test(void) { asm volatile ( " call %[bpf_get_prandom_u32];\n" " r7 =3D r0;\n" " r8 =3D r0;\n" " call %[bpf_get_prandom_u32];\n" " if r0 > 1 goto +0;\n" " if r8 >=3D r0 goto 1f;\n" " r8 +=3D r8;\n" " if r7 =3D=3D 0 goto 1f;\n" " r0 /=3D 0;\n" "1: r0 =3D 42;\n" " exit;\n" : : __imm(bpf_get_prandom_u32) : __clobber_all); } > If you agree with the analysis, we can start discussing what's the > best way to fix this. Please give me some more time, I'm adding some prints do understand why current logic does not mark r8 for state that has "if r8 >=3D r0 goto 1= f;\n" as it's first instruction, on a surface it should.