Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp40944rdb; Wed, 1 Nov 2023 16:16:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/V3ImGJjm1u+mpVtB739+O6k3xhf1ml54/8ejKirU78/v/bsrVv0SVnRmLXVZ9s3P33Uq X-Received: by 2002:a17:903:32cc:b0:1cc:45d0:46fc with SMTP id i12-20020a17090332cc00b001cc45d046fcmr10625681plr.54.1698880577467; Wed, 01 Nov 2023 16:16:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698880577; cv=none; d=google.com; s=arc-20160816; b=Wbedx9fQGBTUAm1FtnV6aAYJ+A3JZZtDOVxn/PiXe0mHs/ZdVUJODigUZQRyBORQH6 a7vvAjW9+sMWr+djxxBsioL4f7PaLchXH8Np/+JXacwTZhMCFDOO7InpuodylM4BENQq codgYghrB3W0K2WJppbNQgcqc7RQlWKK4AbXfK2KIb6Qr9S1OCyJ99Kwv1eStCAVc2yk ibk2JhvRtyNbsAwxbNwkCaS32NhEkJmVVORcPSuQORI+6BMdZmUQ0CJ6kB3f0WOGueUw GEyymGSbP/HjyudVJeVIS4I/CBaJf1mLqYld+AxV256NpBqyTxFDT2aU+W110h80StgQ +gWg== 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=ptbNpH+AcVqxM1M4NGsECpNzHfwuOHCRxY6vQN87uCg=; fh=+p9Nwp+M2CdkPkArpDUMw71B96tsADLKHlXx47HtQA4=; b=JsaDUhc4zThOnC9WCQ9t8rdCuTyRfF8avSrU8g2JmWz0sJuH2EKFTMI8X3YG+CL/lY Ec2/bplIT+CTRYvjjQv8Rp4JWbSWiVtIqOencrc7Px+jcZshia3E5b2Y+/ZKpBWRV7dN IYtFdkFStjl/Iq4Yj9tYdD2+FUZ2QqnyK4NYLfo6xLn8ir0bRpda0CgH6yfvmEFVGPp1 CcLrFLAF6B1t28e3HDYlCCVQGmCt44t/CHw0FupTjy1ZjZ77JzwKYKgdITnP+sV1pqqX FDOJO0msU04TgbuWMMBUOR5XzxNkhS9C0dNjF5PZylzNfe5y5gFC6rLbEhM22QeaByUk WdWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=CHJNXqiY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id t5-20020a170902dcc500b001cc5420241bsi3908283pll.413.2023.11.01.16.16.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 16:16:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=CHJNXqiY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 5EDA78026D95; Wed, 1 Nov 2023 16:16:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233267AbjKAXPz (ORCPT + 99 others); Wed, 1 Nov 2023 19:15:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346285AbjKAWpX (ORCPT ); Wed, 1 Nov 2023 18:45:23 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C76FC10F; Wed, 1 Nov 2023 15:45:20 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-9adb9fa7200so67955466b.0; Wed, 01 Nov 2023 15:45:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698878719; x=1699483519; 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=ptbNpH+AcVqxM1M4NGsECpNzHfwuOHCRxY6vQN87uCg=; b=CHJNXqiYcQyYl4riPXtWDy3vryJWplZrWb4eZY4h8m45gHevbClTolEGIg0sYsq2Wx urECiZ3mqW1iLNtrj6moPXvcw70/F0OqCU8bN7g8ajMAMr+M0L90BZ8JknGmSBkhIRiD +NzfO8NuYoCxcIwuDKnSF0uBMMnAiIoWwJczVbr90y0YS6zB1wuTgkCbXWVon7NHk1Ne ghs7gELZEEJevw825Awc91+yNWNqAiiCSRszdy610VZ2j1OWnnGqhQ3shnGU3FQx7KUj OUAOM95sK+reZKajlDUmj9vDAz0bMPv0xv1unnIFmHmgq35bqh9aujxvXEFH9vp5tVR+ lBzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698878719; x=1699483519; 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=ptbNpH+AcVqxM1M4NGsECpNzHfwuOHCRxY6vQN87uCg=; b=xEKFX4Zi8PEPTf/Op7h4193oOwL5fH1ix7kIFjysXT1G4uaSqiJetH99FbnfYWfu1n Y59ZxFP/Xr3wiW+kpDcwnLKrVKWKKIloLzOm6+JAiW3juMDrMGB0xM2IG7fzPsjjqujY R5+N5hOUw4jVsBbh9bQ/LkEC1bjueJb+AMZCT0Yygdm6Lh7MGnT/heVT/IYXIbTW86sL BHIKYCO03G2LzlOal/yd61xbg2ljmPBBQy65shI9IdL47OO8KsurhKcsEoEsEv0BEj5l hQ8QjP7liapVHKjfKIfsnputDxt/nkZUo42hnTjIwK3kv1SAix+nJLUiwlMFN0jM5hqY p+1A== X-Gm-Message-State: AOJu0Yz2D0ymrcPdJLorPlhks+KBnmJM2MmmmvMHE7WYOnBkmAbwwzJw z3/hJjT7rmw8J7HQNu0brR2WAEx5xmjZYez5qLw= X-Received: by 2002:a17:906:ef09:b0:9bd:a5a9:34de with SMTP id f9-20020a170906ef0900b009bda5a934demr3350099ejs.23.1698878718702; Wed, 01 Nov 2023 15:45:18 -0700 (PDT) MIME-Version: 1.0 References: <4b354d05b1bb4aa681fff5baca3455d90233951d.camel@gmail.com> In-Reply-To: From: Andrii Nakryiko Date: Wed, 1 Nov 2023 15:45:07 -0700 Message-ID: Subject: Re: bpf: shift-out-of-bounds in tnum_rshift() To: Hao Sun Cc: Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf , Linux Kernel Mailing List 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 agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 01 Nov 2023 16:16:05 -0700 (PDT) On Wed, Nov 1, 2023 at 2:53=E2=80=AFAM Hao Sun wrote: > > On Fri, Oct 27, 2023 at 7:51=E2=80=AFPM Andrii Nakryiko > wrote: > > > > On Wed, Oct 25, 2023 at 10:34=E2=80=AFAM Eduard Zingerman wrote: > > > > > > On Tue, 2023-10-24 at 14:40 +0200, Hao Sun wrote: > > > > Hi, > > > > > > > > The following program can trigger a shift-out-of-bounds in > > > > tnum_rshift(), called by scalar32_min_max_rsh(): > > > > > > > > 0: (bc) w0 =3D w1 > > > > 1: (bf) r2 =3D r0 > > > > 2: (18) r3 =3D 0xd > > > > 4: (bc) w4 =3D w0 > > > > 5: (bf) r5 =3D r0 > > > > 6: (bf) r7 =3D r3 > > > > 7: (bf) r8 =3D r4 > > > > 8: (2f) r8 *=3D r5 > > > > 9: (cf) r5 s>>=3D r5 > > > > 10: (a6) if w8 < 0xfffffffb goto pc+10 > > > > 11: (1f) r7 -=3D r5 > > > > 12: (71) r6 =3D *(u8 *)(r1 +17) > > > > 13: (5f) r3 &=3D r8 > > > > 14: (74) w2 >>=3D 30 > > > > 15: (1f) r7 -=3D r5 > > > > 16: (5d) if r8 !=3D r6 goto pc+4 > > > > 17: (c7) r8 s>>=3D 5 > > > > 18: (cf) r0 s>>=3D r0 > > > > 19: (7f) r0 >>=3D r0 > > > > 20: (7c) w5 >>=3D w8 # shift-out-bounds here > > > > 21: exit > > > > > > Here is a simplified example: > > > > > > SEC("?tp") > > > __success __retval(0) > > > __naked void large_shifts(void) > > > { > > > asm volatile (" \ > > > call %[bpf_get_prandom_u32]; \n\ > > > r8 =3D r0; \n\ > > > r6 =3D r0; \n\ > > > r6 &=3D 0xf; \n\ > > > if w8 < 0xffffffff goto +2; \n\ > > > if r8 !=3D r6 goto +1; \n\ > > > w0 >>=3D w8; /* shift-out-bounds here */ \n\ > > > exit; \n\ > > > " : > > > : __imm(bpf_get_prandom_u32) > > > : __clobber_all); > > > } > > > > > > > With my changes the verifier does correctly derive that r8 !=3D r6 will > > always happen, and thus skips w0 >>=3D w8. But the test itself with > > A similar issue can be triggered after your patch for JNE/JEQ. > > For the following case, the verifier would shift out of bound: > // 0: r0 =3D -2 > BPF_MOV64_IMM(BPF_REG_0, -2), > // 1: r0 /=3D 1 > BPF_ALU64_IMM(BPF_DIV, BPF_REG_0, 1), > // 2: r8 =3D r0 > BPF_MOV64_REG(BPF_REG_8, BPF_REG_0), > // 3: if w8 !=3D 0xfffffffe goto+4 > BPF_JMP32_IMM(BPF_JNE, BPF_REG_8, 0xfffffffe, 4), > // 4: if r8 s> 0xd goto+3 > BPF_JMP_IMM(BPF_JSGT, BPF_REG_8, 0xd, 3), > // 5: r4 =3D 0x2 > BPF_MOV64_IMM(BPF_REG_4, 0x2), > // 6: if r8 s<=3D r4 goto+1 > BPF_JMP_REG(BPF_JSLE, BPF_REG_8, BPF_REG_4, 1), > // 7: w8 s>>=3D w0 # shift out of bound here > BPF_ALU32_REG(BPF_ARSH, BPF_REG_8, BPF_REG_0), > // 8: exit > BPF_EXIT_INSN(), > > -------- Verifier Log -------- > func#0 @0 > 0: R1=3Dctx(off=3D0,imm=3D0) R10=3Dfp0 > 0: (b7) r0 =3D -2 ; R0_w=3D-2 > 1: (37) r0 /=3D 1 ; R0_w=3Dscalar() > 2: (bf) r8 =3D r0 ; R0_w=3Dscalar(id=3D1) R8_w=3Ds= calar(id=3D1) > 3: (56) if w8 !=3D 0xfffffffe goto pc+4 ; > R8_w=3Dscalar(id=3D1,smin=3D-9223372032559808514,smax=3D92233720368547758= 06,umin=3Dumin32=3D4294967294,umax=3D18446744073709551614,smin32=3D-2,smax3= 2=3D-2, > umax32=3D4294967294,var_off=3D(0xfffffffe; 0xffffffff00000000)) > 4: (65) if r8 s> 0xd goto pc+3 ; > R8_w=3Dscalar(id=3D1,smin=3D-9223372032559808514,smax=3D13,umin=3Dumin32= =3D4294967294,umax=3D18446744073709551614,smin32=3D-2,smax32=3D-2,umax32=3D= 4294967294, > var_off=3D(0xfffffffe; 0xffffffff00000000)) > 5: (b7) r4 =3D 2 ; R4_w=3D2 > 6: (dd) if r8 s<=3D r4 goto pc+1 ; R4_w=3D2 R8_w=3D4294967294 > 7: (cc) w8 s>>=3D w0 ; R0=3D4294967294 R8=3D429496729= 5 > 8: (95) exit > > Here, after #6, reg range is incorrect, seems to be an issue in JSLE case > in is_branch_taken(). Is this issue fixed in your patch series? I don't know, but you can easily check by applying my patches on top of bpf-next and then trying your change. > > > __retval(0) is not a valid test, so it would be good to construct > > something that will correctly return 0 at runtime (or use some other > > check). So I won't put this test into my patch set and will live it as > > a follow up for someone. But here's the log for anyone curious: > > > > VERIFIER LOG: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > func#0 @0 > > 0: R1=3Dctx(off=3D0,imm=3D0) R10=3Dfp0 > > ; asm volatile (" \ > > 0: (85) call bpf_get_prandom_u32#7 ; R0_w=3Dscalar() > > 1: (bf) r8 =3D r0 ; R0_w=3Dscalar(id=3D1) R8_w=3D= scalar(id=3D1) > > 2: (bf) r6 =3D r0 ; R0_w=3Dscalar(id=3D1) R6_w=3D= scalar(id=3D1) > > 3: (57) r6 &=3D 15 ; > > R6_w=3Dscalar(smin=3Dsmin32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D15,var_= off=3D(0x0; > > 0xf)) > > 4: (a6) if w8 < 0xffffffff goto pc+2 ; > > R8_w=3Dscalar(id=3D1,smin=3D-9223372032559808513,umin=3Dumin32=3D429496= 7295,smin32=3D-1,smax32=3D-1,var_off=3D(0xffffffff; > > 0xffffffff00000000)) > > 5: (5d) if r8 !=3D r6 goto pc+1 > > mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1 > > mark_precise: frame0: regs=3Dr0,r8 stack=3D before 4: (a6) if w8 < > > 0xffffffff goto pc+2 > > mark_precise: frame0: regs=3Dr0,r8 stack=3D before 3: (57) r6 &=3D 15 > > mark_precise: frame0: regs=3Dr0,r8 stack=3D before 2: (bf) r6 =3D r0 > > mark_precise: frame0: regs=3Dr0,r8 stack=3D before 1: (bf) r8 =3D r0 > > mark_precise: frame0: regs=3Dr0 stack=3D before 0: (85) call bpf_get_pr= andom_u32#7 > > mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1 > > mark_precise: frame0: regs=3Dr6 stack=3D before 4: (a6) if w8 < 0xfffff= fff goto pc+2 > > mark_precise: frame0: regs=3Dr6 stack=3D before 3: (57) r6 &=3D 15 > > mark_precise: frame0: regs=3Dr6 stack=3D before 2: (bf) r6 =3D r0 > > mark_precise: frame0: regs=3Dr0 stack=3D before 1: (bf) r8 =3D r0 > > mark_precise: frame0: regs=3Dr0 stack=3D before 0: (85) call bpf_get_pr= andom_u32#7 > > 5: R6_w=3Dscalar(smin=3Dsmin32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D15,v= ar_off=3D(0x0; > > 0xf)) R8_w=3Dscalar(id=3D1,smin=3D-9223372032559808513,umin=3Dumin32=3D= 4294967295,smin32=3D-1,smax32=3D-1,var_off=3D(0xffffffff; > > 0xffffffff00000000)) > > 7: (95) exit > > > > from 4 to 7: R0=3Dscalar(id=3D1,smax=3D9223372036854775806,umax=3D18446= 744073709551614,umax32=3D4294967294) > > R6=3Dscalar(smin=3Dsmin32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D15,var_of= f=3D(0x0; 0xf)) > > R8=3Dscalar(id=3D1,smax=3D9223372036854775806,umax=3D184467440737095516= 14,umax32=3D4294967294) > > R10=3Dfp0 > > 7: R0=3Dscalar(id=3D1,smax=3D9223372036854775806,umax=3D184467440737095= 51614,umax32=3D4294967294) > > R6=3Dscalar(smin=3Dsmin32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D15,var_of= f=3D(0x0; 0xf)) > > R8=3Dscalar(id=3D1,smax=3D9223372036854775806,umax=3D184467440737095516= 14,umax32=3D4294967294) > > R10=3Dfp0 > > 7: (95) exit > > processed 8 insns (limit 1000000) max_states_per_insn 0 total_states 1 > > peak_states 1 mark_read 1 > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > at insn #4, simulating a FALSE condition, verifier knows that r6 is > > [0, 15], while w8 is exactly 0xffffffff, so at insn #5 it can tell > > that 0xffffffff can never be equal to a value in [0, 15] range, and > > thus skips the shift instruction. > >