Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2598045rda; Wed, 25 Oct 2023 07:19:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHGUBH5+aU4EinB+hEz+ZJ4VyGCndZXadTiTfqj3SUAQytNz/i9MBOMt5PfYt7R/ffQ6dY+ X-Received: by 2002:a05:6902:1890:b0:d9a:d196:67c1 with SMTP id cj16-20020a056902189000b00d9ad19667c1mr18094989ybb.10.1698243542235; Wed, 25 Oct 2023 07:19:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698243542; cv=pass; d=google.com; s=arc-20160816; b=OO5+fzhmI7C053B/s3vHx8kYjEXT0cK0WRs8qkhZsgqcwFsyLKyoIZx4iJHyXKaH03 Ft1zSaeUVSYPbdKars6CaJwUEawbrU2C/Xlbz3lcJspfJyuuh3E0XUxmezt2lMJkPETu 3lfTQOfgddECCiCuWbMNowOcGgv0bp73sTlW9mrY1tWBqXyTgoq6Cn5Zfj8GmB0fPmnz B/U8qHE3MqJHvLNhFGoONZkgw8e3TJaoik7wjE0xuK0FRIAdr8ZKQrjWQiJLNaMPuPWr B6fuRWnL3TvGczZ58vsFqltSFuIczU6mZa/3Ka+8e8aYzS8GCpFWMwNPeYQlqAi9Dd1z fj/w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:in-reply-to :content-transfer-encoding:content-disposition:references:message-id :subject:cc:to:from:date:dkim-signature; bh=hnP7DZlJEDNHh/wMYKyzqYUJTIarAhkTGTGBrM7KENg=; fh=be6bZVz77bL524Gwg2MH8rHSDiLL+XV2ERWUdEbXj6E=; b=qntkuc9PCl7zbP/yOOJGGPCfwO1nfW/mhDRu206o7NL+SaKorw/00CExOhj4aiRjer Ykcn+11rI7I4vMHUeHtbNIeet0gG/i96DYig/HOA+Scc+87/5Z1v7G5c0zUZiIn4kQ2p aKXtnrlJ5JXPnHgQJ6s7VYo2I06Q/GCBkxWaRuPhFoTZNV/9AdVdwQXGMLZLNDMDdAwh Xg/u8sP/byTJUTvLWBiIjaG4d+GPirRCCo/y6TAo7Vcdw7kQuEmv+mn7ELGBi5ef7+kM VoTidmCJ6G06LA7PGBXpDctIoe5nNtVMfS0MNZvXrDqjaxjhDiRMioks6qG5Rt5jJ4vy g6NQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.com header.s=selector1 header.b=pkLB1Jlc; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id j1-20020a252301000000b00da03ba106cdsi4392194ybj.269.2023.10.25.07.19.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 07:19:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=selector1 header.b=pkLB1Jlc; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 9A29280DAC6B; Wed, 25 Oct 2023 07:18:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234520AbjJYOSe (ORCPT + 99 others); Wed, 25 Oct 2023 10:18:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234446AbjJYOSc (ORCPT ); Wed, 25 Oct 2023 10:18:32 -0400 Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03on2049.outbound.protection.outlook.com [40.107.105.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 732E8132; Wed, 25 Oct 2023 07:18:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ahvjxymlZHlWueupc2xRCUuQxEd2xggKOgDCyVlhxtyOnXVUJkobIZIX2vFbT/rVbSDM9trtpLMemijnxT4rdZd4yonWKdu8+SkujDmnyiTmu6wb3P5Y8VEhaxbIZY7XF2SJwP44znS2R0FBT/yjTgZlYTD3l7p+W+u8oe/CNAju2omu3cA1YTnToeTnmrLCtpzdRHXOVpOdhZhcE8/egAfiaetOjE48/sMZDTsB4ZyC7jKhcRx2yRcivRJrlmvMFZbaT/qQBEt+6Oo8Qxf4Yt/cRyKXceR3BRDyFYUIjlEA6ZGQ5NIRkbZtG0A8Mlj/Q5noGYZIOXlUk24CMgvWbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hnP7DZlJEDNHh/wMYKyzqYUJTIarAhkTGTGBrM7KENg=; b=I3l3c8J7/Gz7xAtTyrMnEYQD88EFc3Y5wgso5xiWDEi5GT3hFNKc411XQOf+F9w99AzsfK03sH12hq3JlTmOlJHnc7ii37LbwiiqJ6kVdHlR4VOMBOeEf7m+3RnWfNhKgXcmYiChnCPul4hwmO4E7x1VaUN6I60MhJlEkkCGkEze1eABCpgX0gm3sQCr3DvaRaoU1dVCYgsNc9XIY99A9OUx+jIdDB+5/1gzY+U3uKyyIFg7zNnEM5w/fE9Fd1ikl8BbE8g4Z5gD4uMGSjL/scivmjznTVgejNs5+JJJtOZZaVcZDAr5siakWbU+8F+mvp67mOjONINwTKqA6ZGiXQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hnP7DZlJEDNHh/wMYKyzqYUJTIarAhkTGTGBrM7KENg=; b=pkLB1JlcN/GGOVVvKong/KKDv3KAw2JHSWunYEk2HuQjDKmHUW65Tx/A0mHTqVkHiyShdhb8a5T6Dh2Mpqy5KhIDUIOMMCR/3PaX/OBdUXozs7IrwH0uPYmP6f7+by5IrIfXOeLqSKK3VacBdFbrViT53/gI5HWCD4Nek4n5CpTbAzIY4d0VSEaiR1jrazqGzq1KE5+zp3D+/DCOwoPJHHjbCPZBkjKGMm9cEi0rzUkmvmejNsSp140wjefiKqvnI7rvyoC56AArGpumdUe1TpbyU0HGQqvILmlHKbXB2Lv/CKUF/YR0F+Ks9EfdACgNxDErdxwf5eV8ixVmig5eKA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from AS8PR04MB9510.eurprd04.prod.outlook.com (2603:10a6:20b:44a::11) by AS8PR04MB9061.eurprd04.prod.outlook.com (2603:10a6:20b:444::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.11; Wed, 25 Oct 2023 14:18:26 +0000 Received: from AS8PR04MB9510.eurprd04.prod.outlook.com ([fe80::9f3e:3b47:5ccd:c47c]) by AS8PR04MB9510.eurprd04.prod.outlook.com ([fe80::9f3e:3b47:5ccd:c47c%6]) with mapi id 15.20.6933.011; Wed, 25 Oct 2023 14:18:25 +0000 Date: Wed, 25 Oct 2023 22:18:13 +0800 From: Shung-Hsi Yu 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 , bpf , Linux Kernel Mailing List Subject: Re: bpf: shift-out-of-bounds in tnum_rshift() Message-ID: References: Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-ClientProxiedBy: TY2PR06CA0038.apcprd06.prod.outlook.com (2603:1096:404:2e::26) To AS8PR04MB9510.eurprd04.prod.outlook.com (2603:10a6:20b:44a::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR04MB9510:EE_|AS8PR04MB9061:EE_ X-MS-Office365-Filtering-Correlation-Id: c87b69dc-2e7b-4ca6-7e8a-08dbd56540a4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pfyOPN0FHNGXJvXNr5mUZbTNIpOhmPK89dsEBlIkqoo0ntiZ+tj5cS3O9T9xWkRgLoxE3RENEWM81MWXHQZi76SLaczegOkIde16mPZKHgS7lp0tX7Lr1iFETBrD0lp1qjfl7DFnYfkLprDCUrMMiiBDgkeqj/DdV85OFhzAu87qB0AUXnmhQM2mDJC89phsrFcXKt5wl0LZyeKJ3B2UMAMUvTgEy4agKhEY85FXsxosAO7aQqBTzijOk0rhQqXZTue9l12mgqtreSLDEJ15RgcVsmK3BXqfdtm00WUw47SB+4Wj3oJ8YWRr6NJ1w+NsDTHjONNcKwDf7kvUnSMZsajcOa42G1Si3KxJJELxersK73fji3Ellr3ZUvLKotkweOBNpyLSvn11dLTDy7QmGAFNPGRdVQgaXk+qND6md1fKWSW8NUOzBcmn828QyEY6TDjhKAzVHxQ7978UDPu70lMgzRWsE9g4lkI9qg6nLjam6QXUw5Lm8mI99QDUMeHCYjzoIZRm8U+lUXHT/7s8vHCisbJO4lxIfGw5GyyqrPM= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AS8PR04MB9510.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(7916004)(376002)(366004)(346002)(39860400002)(136003)(396003)(230922051799003)(186009)(1800799009)(64100799003)(451199024)(66556008)(966005)(86362001)(66946007)(478600001)(316002)(6916009)(6486002)(66476007)(54906003)(4326008)(83380400001)(8936002)(8676002)(7416002)(5660300002)(6666004)(9686003)(38100700002)(2906002)(6512007)(53546011)(26005)(6506007)(41300700001)(33716001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?MnlNV1RMdnZUL1pOUlo5eVRSSEpwVUd1cnRtdGZ0RTk5eUhUNVJCbnRLMXZN?= =?utf-8?B?d0dDMFRCNGZydU8zWnQxeXR0cDJub3FFZktrd3M5TEUxRWhCeU9td2NjRUlU?= =?utf-8?B?dWRzWDJ4MmVDV3RUOG80OVhFeWM0aW41NFFGNU1JYVQ5OHZqQlIycmp4MHNZ?= =?utf-8?B?ckNKZVRDejE5MERrYS9uSTJyaGgxWWdVMWprTmxXbjRkTXlNdTJjT3ovSVVH?= =?utf-8?B?a0t1TFpqQUlzNEhxbHZMT0syVVpFL05YVDlxOTlpbGJFbjBhOG1NTEJYSUtn?= =?utf-8?B?MGFwQUhyakRzcFlJOEJqWEQvNUY5WWU5cmd5Q1RKcDlSbnF2VXJhOXBaY1lq?= =?utf-8?B?T1FiN1A5YlVHWlBIU3h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rossTenant-Network-Message-Id: c87b69dc-2e7b-4ca6-7e8a-08dbd56540a4 X-MS-Exchange-CrossTenant-AuthSource: AS8PR04MB9510.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2023 14:18:25.9381 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: dO/AnwsBajAAYSrLiwdJ7PtV3Txhqv/w43Mt6Ac770a0l65ku7yzDmZA8fo/Ypd6fH2rkkzTZlyHYjh7i+AUcQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB9061 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Wed, 25 Oct 2023 07:18:53 -0700 (PDT) On Wed, Oct 25, 2023 at 10:09:27PM +0800, Shung-Hsi Yu wrote: > Hi Hao, > > On Wed, Oct 25, 2023 at 02:31:02PM +0200, Hao Sun wrote: > > On Tue, Oct 24, 2023 at 2:40 PM 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. > = w1 > > > 1: (bf) r2 = r0 > > > 2: (18) r3 = 0xd > > > 4: (bc) w4 = w0 > > > 5: (bf) r5 = r0 > > > 6: (bf) r7 = r3 > > > 7: (bf) r8 = r4 > > > 8: (2f) r8 *= r5 > > > 9: (cf) r5 s>>= r5 > > > 10: (a6) if w8 < 0xfffffffb goto pc+10 > > > 11: (1f) r7 -= r5 > > > 12: (71) r6 = *(u8 *)(r1 +17) > > > 13: (5f) r3 &= r8 > > > 14: (74) w2 >>= 30 > > > 15: (1f) r7 -= r5 > > > 16: (5d) if r8 != r6 goto pc+4 > > > 17: (c7) r8 s>>= 5 > > > 18: (cf) r0 s>>= r0 > > > 19: (7f) r0 >>= r0 > > > 20: (7c) w5 >>= w8 # shift-out-bounds here > > > 21: exit > > > > > > > Here are the c macros for the above program in case anyone needs this: > > > > // 0: (bc) w0 = w1 > > BPF_MOV32_REG(BPF_REG_0, BPF_REG_1), > > // 1: (bf) r2 = r0 > > BPF_MOV64_REG(BPF_REG_2, BPF_REG_0), > > // 2: (18) r3 = 0xd > > BPF_LD_IMM64(BPF_REG_3, 0xd), > > // 4: (bc) w4 = w0 > > BPF_MOV32_REG(BPF_REG_4, BPF_REG_0), > > // 5: (bf) r5 = r0 > > BPF_MOV64_REG(BPF_REG_5, BPF_REG_0), > > // 6: (bf) r7 = r3 > > BPF_MOV64_REG(BPF_REG_7, BPF_REG_3), > > // 7: (bf) r8 = r4 > > BPF_MOV64_REG(BPF_REG_8, BPF_REG_4), > > // 8: (2f) r8 *= r5 > > BPF_ALU64_REG(BPF_MUL, BPF_REG_8, BPF_REG_5), > > // 9: (cf) r5 s>>= r5 > > BPF_ALU64_REG(BPF_ARSH, BPF_REG_5, BPF_REG_5), > > // 10: (a6) if w8 < 0xfffffffb goto pc+10 > > BPF_JMP32_IMM(BPF_JLT, BPF_REG_8, 0xfffffffb, 10), > > // 11: (1f) r7 -= r5 > > BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_5), > > // 12: (71) r6 = *(u8 *)(r1 +17) > > BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, 17), > > // 13: (5f) r3 &= r8 > > BPF_ALU64_REG(BPF_AND, BPF_REG_3, BPF_REG_8), > > // 14: (74) w2 >>= 30 > > BPF_ALU32_IMM(BPF_RSH, BPF_REG_2, 30), > > // 15: (1f) r7 -= r5 > > BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_5), > > // 16: (5d) if r8 != r6 goto pc+4 > > BPF_JMP_REG(BPF_JNE, BPF_REG_8, BPF_REG_6, 4), > > // 17: (c7) r8 s>>= 5 > > BPF_ALU64_IMM(BPF_ARSH, BPF_REG_8, 5), > > // 18: (cf) r0 s>>= r0 > > BPF_ALU64_REG(BPF_ARSH, BPF_REG_0, BPF_REG_0), > > // 19: (7f) r0 >>= r0 > > BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_0), > > // 20: (7c) w5 >>= w8 > > BPF_ALU32_REG(BPF_RSH, BPF_REG_5, BPF_REG_8), > > BPF_EXIT_INSN() > > > > > After load: > > > ================================================================================ > > > UBSAN: shift-out-of-bounds in kernel/bpf/tnum.c:44:9 > > > shift exponent 255 is too large for 64-bit type 'long long unsigned int' > > > CPU: 2 PID: 8574 Comm: bpf-test Not tainted > > > 6.6.0-rc5-01400-g7c2f6c9fb91f-dirty #21 > > > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 > > > Call Trace: > > > > > > __dump_stack lib/dump_stack.c:88 [inline] > > > dump_stack_lvl+0x8e/0xb0 lib/dump_stack.c:106 > > > ubsan_epilogue lib/ubsan.c:217 [inline] > > > __ubsan_handle_shift_out_of_bounds+0x15a/0x2f0 lib/ubsan.c:387 > > > tnum_rshift.cold+0x17/0x32 kernel/bpf/tnum.c:44 > > > scalar32_min_max_rsh kernel/bpf/verifier.c:12999 [inline] > > > adjust_scalar_min_max_vals kernel/bpf/verifier.c:13224 [inline] > > > adjust_reg_min_max_vals+0x1936/0x5d50 kernel/bpf/verifier.c:13338 > > > do_check kernel/bpf/verifier.c:16890 [inline] > > > do_check_common+0x2f64/0xbb80 kernel/bpf/verifier.c:19563 > > > do_check_main kernel/bpf/verifier.c:19626 [inline] > > > bpf_check+0x65cf/0xa9e0 kernel/bpf/verifier.c:20263 > > > bpf_prog_load+0x110e/0x1b20 kernel/bpf/syscall.c:2717 > > > __sys_bpf+0xfcf/0x4380 kernel/bpf/syscall.c:5365 > > > __do_sys_bpf kernel/bpf/syscall.c:5469 [inline] > > > __se_sys_bpf kernel/bpf/syscall.c:5467 [inline] > > > __x64_sys_bpf+0x73/0xb0 kernel/bpf/syscall.c:5467 > > > do_syscall_x64 arch/x86/entry/common.c:50 [inline] > > > do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80 > > > entry_SYSCALL_64_after_hwframe+0x63/0xcd > > > RIP: 0033:0x5610511e23cd > > > Code: 24 80 00 00 00 48 0f 42 d0 48 89 94 24 68 0c 00 00 b8 41 01 00 > > > 00 bf 05 00 00 00 ba 90 00 00 00 48 8d b44 > > > RSP: 002b:00007f5357fc7820 EFLAGS: 00000246 ORIG_RAX: 0000000000000141 > > > RAX: ffffffffffffffda RBX: 0000000000000095 RCX: 00005610511e23cd > > > RDX: 0000000000000090 RSI: 00007f5357fc8410 RDI: 0000000000000005 > > > RBP: 0000000000000000 R08: 00007f5357fca458 R09: 00007f5350005520 > > > R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000002b > > > R13: 0000000d00000000 R14: 000000000000002b R15: 000000000000002b > > > > > > > > > If remove insn #20, the verifier gives: > > > -------- Verifier Log -------- > > > func#0 @0 > > > 0: R1=ctx(off=0,imm=0) R10=fp0 > > > 0: (bc) w0 = w1 ; > > > R0_w=scalar(smin=0,smax=umax=4294967295,var_off=(0x0; 0xffffffff)) > > > R1=ctx(off=0, > > > imm=0) > > > 1: (bf) r2 = r0 ; > > > R0_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; > > > 0xffffffff)) > > > R2_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; 0xffffffff)) > > > 2: (18) r3 = 0xd ; R3_w=13 > > > 4: (bc) w4 = w0 ; > > > R0_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; > > > 0xffffffff)) > > > R4_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; 0xffffffff)) > > > 5: (bf) r5 = r0 ; > > > R0_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; > > > 0xffffffff)) > > > R5_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; 0xffffffff)) > > > 6: (bf) r7 = r3 ; R3_w=13 R7_w=13 > > > 7: (bf) r8 = r4 ; > > > R4_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; > > > 0xffffffff)) > > > R8_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; 0xffffffff)) > > > 8: (2f) r8 *= r5 ; > > > R5_w=scalar(id=1,smin=0,smax=umax=4294967295,var_off=(0x0; > > > 0xffffffff)) > > > R8_w=scalar() > > > 9: (cf) r5 s>>= r5 ; R5_w=scalar() > > > 10: (a6) if w8 < 0xfffffffb goto pc+9 ; > > > R8_w=scalar(smin=-9223372032559808520,umin=4294967288,smin32=-5,smax32=-1, > > > umin32=4294967291,var_off=(0xfffffff8; 0xffffffff00000007)) > > > 11: (1f) r7 -= r5 ; R5_w=scalar() R7_w=scalar() > > > 12: (71) r6 = *(u8 *)(r1 +17) ; R1=ctx(off=0,imm=0) > > > R6_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=255, > > > var_off=(0x0; 0xff)) > > > 13: (5f) r3 &= r8 ; > > > R3_w=scalar(smin=umin=smin32=umin32=8,smax=umax=smax32=umax32=13,var_off=(0x8; > > > 0x5)) R8_w=scalar(smin=-9223372032559808520,umin=4294967288,smin32=-5,smax32=-1,umin32=4294967291,var_off=(0xffff) > > > 14: (74) w2 >>= 30 ; > > > R2_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=3,var_off=(0x0; > > > 0x3)) > > > 15: (1f) r7 -= r5 ; R5_w=scalar() R7_w=scalar() > > > 16: (5d) if r8 != r6 goto pc+3 ; > > > R6_w=scalar(smin=umin=umin32=4294967288,smax=umax=umax32=255,smin32=-8,smax32=-1, > > > var_off=(0xfffffff8; 0x7)) > > > R8_w=scalar(smin=umin=4294967288,smax=umax=255,smin32=-5,smax32=-1,umin32=4294967291) > > Seems like the root cause is a bug with range tracking, before instruction > 16, R8_w was > > R8_w=scalar(smin=-9223372032559808520,umin=4294967288,smin32=-5,smax32=-1,umin32=4294967291,var_off=(0xffff) > > But after instruction 16 it becomes > > R8_w=scalar(smin=umin=4294967288,smax=umax=255,smin32=-5,smax32=-1,umin32=4294967291) > > Where smin_value > smax_value, and umin_value > umax_value (among other > things). This should be the main problem. > > The verifier operates on the assumption that smin_value <= smax_value and > umin_value <= umax_value, and if that assumption is not upheld then all kind > of things can go wrong. > > Maybe Andrii may already has this worked out in the range-vs-range that he > has mentioned[1] he'll be sending soon. > > 1: https://lore.kernel.org/bpf/CAEf4BzbJ3hZCSt4nLCZCV4cxV60+kddiSMsy7-9ou_RaQV7B8A@mail.gmail.com/ > > > > 17: (c7) r8 s>>= 5 ; R8_w=134217727 > > > 18: (cf) r0 s>>= r0 ; R0_w=scalar() > > > 19: (7f) r0 >>= r0 ; R0=scalar() > > > 20: (95) exit > > > > > > from 16 to 20: safe > > > > > > from 10 to 20: safe > > > processed 22 insns (limit 1000000) max_states_per_insn 0 total_states > > > 1 peak_states 1 mark_read 1 > > > -------- End of Verifier Log -------- > > > > > > In adjust_scalar_min_max_vals(), src_reg.umax_value is 7, thus pass > > > the check here: > > > if (umax_val >= insn_bitness) { > > > /* Shifts greater than 31 or 63 are undefined. > > > * This includes shifts by a negative number. > > > */ > > > mark_reg_unknown(env, regs, insn->dst_reg); > > > break; > > > } > > > > > > However in scalar32_min_max_rsh(), both src_reg->u32_min_value and > > > src_reg->u32_max_value is 134217727, causing tnum_rsh() shit by 255. > > > > > > Should we check if(src_reg->u32_max_value < insn_bitness) before calling > > > scalar32_min_max_rsh(), rather than only checking umax_val? Or, is it > > > because issues somewhere else, incorrectly setting u32_min_value to > > > 34217727 > > Checking umax_val alone is be enough and we don't need to add a check for > u32_max_value, because (when we have correct range tracking) u32_max_value > should always be smaller than u32_value. So the fix needed here is to have > correct range tracking. Since you are running fuzzer on BPF verifier, it may be be helpful to check that the following conditions are always met: - umin <= umax - smin <= smax - u32_min <= u32_max - s32_min <= s32_max - u32_max <= umax - smin <= s32_min - s32_max <= smax If any of these condition is not upheld then it should be a range tracking bug. > > > Best > > > Hao Sun