Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp1184354rdb; Wed, 1 Nov 2023 13:58:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGphvCMf7qN3AlrgUThIDVIG3aQ96hnE70C2uftlGpabu54eomX2hRpZXtmCMRluRhCi/Oq X-Received: by 2002:a05:6300:8005:b0:181:1cfa:4120 with SMTP id an5-20020a056300800500b001811cfa4120mr4690197pzc.46.1698872280562; Wed, 01 Nov 2023 13:58:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698872280; cv=none; d=google.com; s=arc-20160816; b=hrY2eS+sQj2K51vPE9fEQiYIqhDbEU7nRE09fCvGjFKTtrmCs2tTYf77i2V1TW5mQE qeZbwKjW3E5LZ8IieLz0T/TsQoR9rU0txi5ONcCp1VsRQSIq8dCa8H/pZvZehTY0l+v5 FCKLHO72/9Homh5SUbSSId+vK7hclHVfE48Gtepj5908WmHvip7Jgow8c4c5lRujJQ06 CL1xGNgHCBONKcYfNNDxK8FqoxL2+ff5MDtl9EIWk7vNK+ZskmwP93KRJMvJGRK7PsXc fg3hfRENyuTkeoGKRBxOLWZJE11RyLqTCFjopEH1KRSvK/mrig8aqLHgTwjJONmUlZcp OAQw== 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=E71E5dF0f7RF1z/b8IaouihYDBRTmJBE+XZef23gOzE=; fh=3H/E+nInebRHBp6Hia+8cAE9ifAE2du5iikCUNcZhIk=; b=qyzBRCZsjgQBTHlbhJbGKea1iBCHI2iOZa4XHG8nh3LXBPjpm92bHdDEJuiIyi9e3u WxL+UmUrIRxlqviEJ6nwy8hW24Dyq30ejVO9pxkkQBxq3axWEiBxU5DCnupqLUe+TBAV G3+r/xHtxhixbYzxGyooMLU/2v5rLYZqUvdEt84z4wPwUM/RpRUnvpBzKgp7kVIOZaCP LX7U4TsMIFNH2Q38NY+e3pwdxE9+m9NKHpN/KAfocze9Tl6xZOXRFfmYEOSWF4Qcy9Vx Hub4OYGuaoqw9agbsXZ2cTqLQfG8Dz5/nYkWT+O8U2TU0uBAg8Rwst/ZvtwSVT9663/b 1OTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TwoK++cf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id s24-20020aa78bd8000000b006be26c1be4bsi2210455pfd.73.2023.11.01.13.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 13:58:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TwoK++cf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 0E7B0801F948; Wed, 1 Nov 2023 13:57:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234974AbjKAU5u (ORCPT + 99 others); Wed, 1 Nov 2023 16:57:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232713AbjKAU5t (ORCPT ); Wed, 1 Nov 2023 16:57:49 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4489E10C; Wed, 1 Nov 2023 13:57:47 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-5437269a661so2636361a12.0; Wed, 01 Nov 2023 13:57:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698872266; x=1699477066; 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=E71E5dF0f7RF1z/b8IaouihYDBRTmJBE+XZef23gOzE=; b=TwoK++cf7p5dhIUPAn9VzTXbcJS3oEaOFlXv+A+gsvb2geNV/gxMjOvLKrg46M/gus wgp98W9xFDCoJDxYazbk0LWvp5XxitHlVehCfZwy2DNZ5Y75WLWiDG1SIXQIJxM6y02f 2sIskL02zMa0ZQw2chJEULrHCi72iOPpiElO/I0789gYWESwnXoFIx0hXe1jPENWQR69 i9OrfAL3BnjJ8NQurB5pFb6LreCSbSvH/w44EFQtLecX2dNh75Nwb2G1hkeWq/nRkqt9 xMRy+6dkW6fjH9eL2uzKG2il00E3T4836Dh9FqLjPQF/yZLhx7gLrzX5s4yOKaibDLVR u3Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698872266; x=1699477066; 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=E71E5dF0f7RF1z/b8IaouihYDBRTmJBE+XZef23gOzE=; b=SWeoZn7efeTuXkGaGMuKwHScQfzqOEY+WPprCSRDR876gCKDUTUvOqBCmzSXqypuHm vYkKQvd2DI4xN70TtxIfCi6Q6agWKeosfnTDrV7SH8JaLh8j3j0oDXcxnltwoAjYxHOB JcE46y5fwGuWDOlts+YfT7mTGz8MLrpt7tVfJ8ePf3RaGnc4q7vqJLPTjtK0LpKqVKVD FD6I7K3PUJV0fCqz6za+GRQ45raZ1TO1t8jiimIo9ing6tSJmFp38vVJL3elhiLhMz6V z6Wz/vyYz9buL98REHzCBAMCTIJ8F30b0J4YKHAFh4QY166rRVVRU9QwzySwlNmnYJzJ 2h+Q== X-Gm-Message-State: AOJu0YziURqTitCx6N6FLISx6Yh4/VLV+FwkXQBMSKgm0L5BwFMBTSxb Hvi9EkXMLMI2cwQ4BpAek/x0tYqRMjaAVqOd/NE= X-Received: by 2002:a17:906:c104:b0:9b2:bdbb:f145 with SMTP id do4-20020a170906c10400b009b2bdbbf145mr2900162ejc.34.1698872265479; Wed, 01 Nov 2023 13:57:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andrii Nakryiko Date: Wed, 1 Nov 2023 13:57:33 -0700 Message-ID: Subject: Re: bpf: incorrectly reject program with `back-edge insn from 7 to 8` To: Hao Sun Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Linux Kernel Mailing List , bpf 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 fry.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 (fry.vger.email [0.0.0.0]); Wed, 01 Nov 2023 13:57:58 -0700 (PDT) On Wed, Nov 1, 2023 at 6:56=E2=80=AFAM Hao Sun wrote: > > Hi, > > The verifier incorrectly rejects the following prog in check_cfg() when > loading with root with confusing log `back-edge insn from 7 to 8`: > /* 0: r9 =3D 2 > * 1: r3 =3D 0x20 > * 2: r4 =3D 0x35 > * 3: r8 =3D r4 > * 4: goto+3 > * 5: r9 -=3D r3 > * 6: r9 -=3D r4 > * 7: r9 -=3D r8 > * 8: r8 +=3D r4 > * 9: if r8 < 0x64 goto-5 > * 10: r0 =3D r9 > * 11: exit > * */ > BPF_MOV64_IMM(BPF_REG_9, 2), > BPF_MOV64_IMM(BPF_REG_3, 0x20), > BPF_MOV64_IMM(BPF_REG_4, 0x35), > BPF_MOV64_REG(BPF_REG_8, BPF_REG_4), > BPF_JMP_IMM(BPF_JA, 0, 0, 3), > BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_3), > BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_4), > BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_8), > BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_4), > BPF_JMP32_IMM(BPF_JLT, BPF_REG_8, 0x68, -5), > BPF_MOV64_REG(BPF_REG_0, BPF_REG_9), > BPF_EXIT_INSN() > > -------- Verifier Log -------- > func#0 @0 > back-edge from insn 7 to 8 > processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 > peak_states 0 mark_read 0 > > This is not intentionally rejected, right? The way you wrote it, with goto +3, yes, it's intentional. Note that you'll get different results in privileged and unprivileged modes. Privileged mode allows "bounded loops" logic, so it doesn't immediately reject this program, and then later sees that r8 is always < 0x64, so program is correct. But in unprivileged mode the rules are different, and this conditional back edge is not allowed, which is probably what you are getting. It's actually confusing and your "back-edge from insn 7 to 8" is out of date and doesn't correspond to your program, you should see "back-edge from insn 11 to 7", please double check. Anyways, while I was looking into this, I realized that ldimm64 isn't handled exactly correctly in check_cfg(), so I just sent a fix. It also adds a nicer detection of jumping into the middle of the ldimm64 instruction, which I believe is something you were advocating for. > > Best > Hao