Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2433161rwb; Mon, 7 Nov 2022 13:15:32 -0800 (PST) X-Google-Smtp-Source: AMsMyM5I1rCmArv3kXtdE6xAVlzAcM3ejvZGA/xPQhCoMjcW1cJRzyGY2wOIIHmTLWn9LH208mEh X-Received: by 2002:a17:907:162a:b0:7a9:9875:3147 with SMTP id hb42-20020a170907162a00b007a998753147mr49912042ejc.546.1667855732624; Mon, 07 Nov 2022 13:15:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667855732; cv=none; d=google.com; s=arc-20160816; b=VsYj9yFFwO5FbgoMj0xX5bW9bNqQzLIzOH0FAbqdyfNAzPzeATuZ+jqWeBnbGioC9M f+oPOvfTxCRTK09ckeaEgv8mVpz7Be6nUJYVC7BIVvNz1TfA6Wl4W5M0jX7lB16XwwQB N7Ishu+j/HbiUTeut7nHP155bDOM1c3WEuPe/ftGkqZA/pUBThLm3KX3UVFiriP1nIab F/t1BgOhdlVO86rQDYhPYmjgyfCy9vmDrrAz9XGEmIO2++PSrbejDmbNaH8SRphLw993 yvGr/lnPFkxa433MBDhNHSrxZJ4R/RXwPoKJMVfDObk00ns6W2cMRvCNEVOcz3SNVAJo N2Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Uw4Vs7V4/db0GHoTeanpNzIRHKp6KNfFs3tBhi1t9+c=; b=E5mrA3zEBGufL6vkZolyRHyAP8NfKjg9n/BV5jnhvRCtFIGpYbmo++hxr+OoJhNAH3 lcQ+48t/m5VabmISbEfi3BrcY3Q55p3NCGidXGhhIvMnjRWrziBMhtqh/brAUwtmroTI dyeP5lPxuoBIcBxlxa3s9NFlO0pN0AuXMVeHwAkXn7wJ6hiktDIE/GZnRtRQEo0AI/0B n6AKgY3v5GgZKDhRlzq42sYH6HH7zf88A4iLi5MSN+u08vAcOGZXunsJvuOI5OaguUAo 5+dybE85jetKj2Z8MvorcvqD8FcWpt7Lqzg12X4YMBulrHVWUzwxHfsNSkboSgUrfon+ ajEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Y22KOS3q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g16-20020a1709065d1000b007ae566d2641si6436415ejt.566.2022.11.07.13.15.09; Mon, 07 Nov 2022 13:15:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Y22KOS3q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233077AbiKGUwP (ORCPT + 93 others); Mon, 7 Nov 2022 15:52:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233011AbiKGUwN (ORCPT ); Mon, 7 Nov 2022 15:52:13 -0500 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 8551427176; Mon, 7 Nov 2022 12:52:12 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id r14so19534159edc.7; Mon, 07 Nov 2022 12:52:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Uw4Vs7V4/db0GHoTeanpNzIRHKp6KNfFs3tBhi1t9+c=; b=Y22KOS3q31AOjnyWsz4u2EVdUM/ELgOvBXEQihO6s1ZqHGew1qTCJ2e+breRRHvTVz BlwSf4IYUMpSbfNC0fYx0kdH1gwCRVAPpb5D8iOlMZJUElgQYYSdpx9kB04z8ua3Hkdu D06KrvPUcx6ywKBPxTLvxTDkzyw2agjQBEi97QGZqsfsP9O0HxKBh3Ryl1WaDwpwV+xM QiTM4sGvCThd9BKrubYMaeIy5P6VkkIM4szsATpZh7ThC+9YH/GdU/aoGzfgkhSO9B4f lEMDkiv2xYX3PXfGG76mz7JNmw576050FLsNbdHt4cP6eOjRTGPDqaX6nr+SorrLuVFs afwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=Uw4Vs7V4/db0GHoTeanpNzIRHKp6KNfFs3tBhi1t9+c=; b=Sq36mjWReRXtdeo1WfUPbdv1CKonObAPIxC4Bpt6h9/2oX7SCRulKiyFVZFt4pNr1M sXizvHqBScCgahKx77H3lTFB3d4jxQnfl7psjgosg5ZRxHQp4QngPaIrnkKlqhKLEby7 FdUq8sF56z7gvEnpoWH6BZh9sJNkElyj0wuvCZ8nw0ePAQd4/iHQLlWUELI7sZ8Et5jb 19fsvqbKHIKghm/vFTqYhEdCvaOtzowWuL5lujDoh2U8bbruOk27je4GYsocXsFmBe2B K9FNJLGsYWXzLYvwSWEdG778Xgas45xhGk0Q3J2QknQsluCjlVcvO+wX1+3othOMvR5J 1KwA== X-Gm-Message-State: ACrzQf3AnJ+809q0hTIYFDfxe6rh0Ap5/cD16wHjAaskRVqlvwqDmAZJ rrqpG0LYyUiuG+lYTDyGHn5lHzVtujGWFidwRLY= X-Received: by 2002:a05:6402:2791:b0:461:c5b4:d114 with SMTP id b17-20020a056402279100b00461c5b4d114mr51485078ede.357.1667854330945; Mon, 07 Nov 2022 12:52:10 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Alexei Starovoitov Date: Mon, 7 Nov 2022 12:51:59 -0800 Message-ID: Subject: Re: BUG in BPF verifier, 10 insns costs 2 mins+ to load To: Hao Sun Cc: Linux Kernel Mailing List , bpf , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , ong@kernel.org, Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Content-Type: text/plain; charset="UTF-8" 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_NONE,SPF_HELO_NONE,SPF_PASS 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 On Sun, Nov 6, 2022 at 8:26 PM Hao Sun wrote: > > Hi, > > I've just written a BPF verifier fuzzer, targeting logic bugs in the > BPF verifier. > The following is an abnormal case it generated. The case only contains 10 > BPF instructions but costs more than 2 mins to load on : with full verbose verifier logging, right? That is expected for any prog that is going to hit the 1M insn limit. > HEAD commit: f0c4d9fc9cc9 Linux 6.1-rc4 > git tree: upstream > kernel config: https://pastebin.com/raw/SBxaikiG > C reproducer: https://pastebin.com/raw/HsDXdraZ > verifier log: https://pastebin.com/raw/sNmSsVxs > > Ideally, the verifier should exit quickly in this case, since R2=42 > always holds. > The behaviour of the verifier does not make sense to me, seems it lost > the range information of R2. > > Please point out if I missed anything, the C reproducer in the link > (https://pastebin.com/raw/HsDXdraZ) > essentially loads the following case into `test_verifier.c`: > { > "BVF verifier test", > .insns = { > BPF_MOV64_IMM(BPF_REG_1, 42), > BPF_MOV64_IMM(BPF_REG_2, 0), > BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1), > BPF_EXIT_INSN(), > BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1), > BPF_ALU32_IMM(BPF_DIV, BPF_REG_2, 1), > BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 108), > BPF_JMP32_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, -3), > BPF_MOV64_REG(BPF_REG_0, BPF_REG_2), > BPF_EXIT_INSN(), > }, > .prog_type = BPF_PROG_TYPE_XDP, > }, > > The verifier's log is more then 4M, but essentially is: > 0: R1=ctx(off=0,imm=0) R10=fp0 > 0: (b7) r1 = 42 ; R1_w=P42 > 1: (b7) r2 = 0 ; R2_w=P0 > 2: (85) call pc+1 > caller: > R10=fp0 > callee: > frame1: R1_w=P42 R2_w=P0 R10=fp0 > 4: (57) r2 &= -52 ; frame1: R2_w=P0 > 5: (0f) r2 += r1 ; frame1: R1_w=P42 R2_w=P42 > 6: (34) w2 /= 1 ; frame1: > R2_w=Pscalar(umax=4294967295,var_off=(0x0; 0xffffffff)) > 7: (17) r1 -= 108 ; frame1: R1_w=P-66 > 8: (2e) if w1 > w2 goto pc-3 6: frame1: R1_w=P-66 > R2_w=Pscalar(umax=4294967229,var_off=(0x0; 0xffffffff)) R10=fp0 > 6: (34) w2 /= 1 ; frame1: > R2_w=Pscalar(umax=4294967295,var_off=(0x0; 0xffffffff)) > 7: (17) r1 -= 108 ; frame1: R1_w=P-174 > 8: (2e) if w1 > w2 goto pc-3 6: frame1: R1_w=P-174 > R2_w=Pscalar(umax=4294967121,var_off=(0x0; 0xffffffff)) R10=fp0 > 6: (34) w2 /= 1 ; frame1: > R2_w=Pscalar(umax=4294967295,var_off=(0x0; 0xffffffff)) > 7: (17) r1 -= 108 ; frame1: R1=P-282 > 8: (2e) if w1 > w2 goto pc-3 6: frame1: R1=P-282 > R2=Pscalar(umax=4294967013,var_off=(0x0; 0xffffffff)) R10=fp0 > ... > 6: (34) w2 /= 1 ; frame1: > R2_w=Pscalar(umax=4294967295,var_off=(0x0; 0xffffffff)) > 7: (17) r1 -= 108 ; frame1: R1_w=P-6342690 > 8: (2e) if w1 > w2 goto pc-3 6: frame1: R1_w=P-6342690 > R2_w=Pscalar(umax=4288624605,var_off=(0x0; 0xffffffff)) R10=fp0 > 6: (34) w2 /= 1 ; frame1: > R2_w=Pscalar(umax=4294967295,var_off=(0x0; 0xffffffff)) > 7: (17) r1 -= 108 ; frame1: R1_w=P-6342798 > 8: (2e) if w1 > w2 goto pc-3 ; frame1: R1_w=P-6342798 > R2_w=Pscalar(umin=4288624498,umax=4294967295,var_off=(0xff800000; > 0x7fffff),s32_min=-6342798,s32_max=-1) > 9: (bf) r0 = r2 ; frame1: > R0_w=Pscalar(id=58730,umin=4288624498,umax=4294967295,var_off=(0xff800000; > 0x7fffff),s32_min=-6342798,s32_max=-1) > R2_w=Pscalar(id=58730,umin=4288624498,umax=4294967295,var_off=(0xff800000; > 0x7fffff),s32_min=-6342798,s32_max=-1) > 10: (95) exit > returning from callee: > frame1: R0_w=Pscalar(id=58730,umin=4288624498,umax=4294967295,var_off=(0xff800000; > 0x7fffff),s32_min=-6342798,s32_max=-1) R1_w=P-6342798 > R2_w=Pscalar(id=58730,umin=4288624498,umax=4294967295,var_off=(0xff800000; > 0x7fffff),s32_min=-6342798,s32_max=-1) R10=fp0 > to caller at 3: > R0_w=Pscalar(id=58730,umin=4288624498,umax=429496