Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2558336rda; Wed, 25 Oct 2023 06:21:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEOQoZ7Oa3houwJU8PSs4jj4qcTjJ/99+jIhfzc6Lx9w0SJr8VhiSE5pHWBz9BEbeEi4UU2 X-Received: by 2002:a25:3252:0:b0:d9b:9f55:b62d with SMTP id y79-20020a253252000000b00d9b9f55b62dmr13869731yby.61.1698240078647; Wed, 25 Oct 2023 06:21:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698240078; cv=none; d=google.com; s=arc-20160816; b=oKHryWZLVKm6snM6BtnPRzL89V5cGzEkBH5AU1LE5+jBi9Yh4BovIx84+jGoWVYrHm 6MIn1o4PXIcp2mvM4YaEizPfA2kEwAUm1oaccmtuhayQ6n7MbWAgPxYquUbzhnPdq3Pq g0LWfg2fxMffJ3RMr4lgER2p8W5QJXwQKsWcUMVtnxY2E45bOIbYxyjMsIRw2pm2GZEa /K6KrrnOP1+StbNAX/eZVekE/cQ7PT9GWVE0seS7b9qxahIY1vwIIZOvk1qLlT3sQV+y OHz51/ON4az8h5/vmDuZ423WWTexJxAiV8AurIv4UUwIibGdAvR1fJdZCpEDCbHprDU/ 1wFg== 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=LPF+5Rsz+apiPCuGIdBu5hhoVuX8IXXK92UDgsE51EY=; fh=9ccdba4B9pCzwT/XaETgFVLiNKqLOxOLCg39JfCsUCE=; b=f6s0BFbE/ib6m4PWi9gK2IqKr4ClUEhslEb1weeLM2wyH/9UhMxUU7SwOKl8OuSNee YFz1Eh/n2821rziNMxu4b3GYvCalp/67q8oNfgS8vyZaSgkp5ebxVBjlkkgWXc0fP6Xu NRigWjcfGGDklW5a7EyhEiNEIFuiwlK+JWdQyal6mqowOW7IGy6mxiTp1NxWp37TXjzq yYN4lOnIamyZprZrH2cQn0G3wAnIW4Z/Sze6Pw9La6ZVBimbeV0SDdu/fL/wmGxJMYiU xK+5pAfXQKEj072OoONQLYwswowqfetD0AD2Bft+sPT2xGFH3SiCCj/kGkT7nTt8t1A/ yU4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="lQ+3z9/Q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id 2-20020a250102000000b00da05c2e1753si3038843ybb.548.2023.10.25.06.21.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 06:21:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="lQ+3z9/Q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 0920E80941E6; Wed, 25 Oct 2023 06:20:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234828AbjJYNUr (ORCPT + 99 others); Wed, 25 Oct 2023 09:20:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234129AbjJYNUo (ORCPT ); Wed, 25 Oct 2023 09:20:44 -0400 Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FEE1111; Wed, 25 Oct 2023 06:20:42 -0700 (PDT) Received: by mail-yb1-xb31.google.com with SMTP id 3f1490d57ef6-d84c24a810dso4894028276.2; Wed, 25 Oct 2023 06:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698240041; x=1698844841; 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=LPF+5Rsz+apiPCuGIdBu5hhoVuX8IXXK92UDgsE51EY=; b=lQ+3z9/Q2uCxd+cHRxB6CqFsfiidtLdbnZy/LXXV6WCOindSSmnv7YOPbgrL7hy02b lGYAOEr+gl/mhsV61L8Atqt0H1FuDIQg5EO9RCOnQPLr368tijIBG1+cNiqOWxiR2npA 8DXqYsEGC0iaHlJGCPsJgBgW4cISVW/BMHNIouoswdsdJ8a2B9uwqRTd83xSrb7a2XBl 6XizrbOo8Go4hJJg8X0Fb+umpOdhJi8Nr8LxXQ+XdYvRsHrNQXlIVyZ6A60tPZyIF3ET gGzft78Z55PDIKi95hzu8VdK8Vohxng93ZPZ9QSk2HxVmWLKH/5UfgXXFaLL43IIPrEq PdJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698240041; x=1698844841; 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=LPF+5Rsz+apiPCuGIdBu5hhoVuX8IXXK92UDgsE51EY=; b=TeVw5nrcnvFwRnkyh8PaA6IyyxdcOSqWO8bAmPr3pW9lw5LpDQHyIdnDRFQ296C0tl kwmEDnLixlSc+esnWkIdL8NCdY89h2PBIVj9naMco5bNBuqGAJZKIKUPL2u0rZ1u7Ujj f05svS0NnzRq4vu+efQytoiQR0OeTAQ26OpTp0wttCyEQnS/1hbFbR7VdtqiQlCyLD7j N2QjX51caMokYn3XqL9xff8f6mtoMIgXCQkrgRcKQf2yVu5sZD+8EoHt29tn5ZGou+E0 mB0r3GYP5G9O939QMfCzP2hnMc8JDCqWvLWaMKC2FCkGpcuPverYghnImVqNY2UIhthI B00w== X-Gm-Message-State: AOJu0Yx+5V0drmcpQiYatuviFaDc7SWB+3EVfV5/AA7VLtELEOKVks6n RyqPUocrgGFy4iPk1OznSKcbPbvw4HutZT+Irg== X-Received: by 2002:a25:aca0:0:b0:d9a:4d90:feda with SMTP id x32-20020a25aca0000000b00d9a4d90fedamr261633ybi.62.1698240041458; Wed, 25 Oct 2023 06:20:41 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Hao Sun Date: Wed, 25 Oct 2023 15:20:29 +0200 Message-ID: Subject: Re: bpf: shift-out-of-bounds in tnum_rshift() To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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_BLOCKED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 25 Oct 2023 06:20:51 -0700 (PDT) On Wed, Oct 25, 2023 at 2:31=E2=80=AFPM Hao Sun wrote= : > > On Tue, Oct 24, 2023 at 2:40=E2=80=AFPM Hao Sun wro= te: > > > > 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 are the c macros for the above program in case anyone needs this: > > // 0: (bc) w0 =3D w1 > BPF_MOV32_REG(BPF_REG_0, BPF_REG_1), > // 1: (bf) r2 =3D r0 > BPF_MOV64_REG(BPF_REG_2, BPF_REG_0), > // 2: (18) r3 =3D 0xd > BPF_LD_IMM64(BPF_REG_3, 0xd), > // 4: (bc) w4 =3D w0 > BPF_MOV32_REG(BPF_REG_4, BPF_REG_0), > // 5: (bf) r5 =3D r0 > BPF_MOV64_REG(BPF_REG_5, BPF_REG_0), > // 6: (bf) r7 =3D r3 > BPF_MOV64_REG(BPF_REG_7, BPF_REG_3), > // 7: (bf) r8 =3D r4 > BPF_MOV64_REG(BPF_REG_8, BPF_REG_4), > // 8: (2f) r8 *=3D r5 > BPF_ALU64_REG(BPF_MUL, BPF_REG_8, BPF_REG_5), > // 9: (cf) r5 s>>=3D 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 -=3D r5 > BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_5), > // 12: (71) r6 =3D *(u8 *)(r1 +17) > BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, 17), > // 13: (5f) r3 &=3D r8 > BPF_ALU64_REG(BPF_AND, BPF_REG_3, BPF_REG_8), > // 14: (74) w2 >>=3D 30 > BPF_ALU32_IMM(BPF_RSH, BPF_REG_2, 30), > // 15: (1f) r7 -=3D r5 > BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_5), > // 16: (5d) if r8 !=3D r6 goto pc+4 > BPF_JMP_REG(BPF_JNE, BPF_REG_8, BPF_REG_6, 4), > // 17: (c7) r8 s>>=3D 5 > BPF_ALU64_IMM(BPF_ARSH, BPF_REG_8, 5), > // 18: (cf) r0 s>>=3D r0 > BPF_ALU64_REG(BPF_ARSH, BPF_REG_0, BPF_REG_0), > // 19: (7f) r0 >>=3D r0 > BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_0), > // 20: (7c) w5 >>=3D w8 > BPF_ALU32_REG(BPF_RSH, BPF_REG_5, BPF_REG_8), > BPF_EXIT_INSN() > > > After load: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D > > 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 > > > > Here is another similar one, which can probably be fixed in the same patch. Build the kernel with UBSAN and run the following repro can easily reproduce this. C reproducer: https://pastebin.com/raw/zNfHaBnj =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D UBSAN: shift-out-of-bounds in kernel/bpf/verifier.c:13049:63 shift exponent 55 is too large for 32-bit type 'int' CPU: 3 PID: 8614 Comm: poc Not tainted 6.6.0-rc5-01400-g7c2f6c9fb91f-dirty = #22 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 scalar32_min_max_arsh kernel/bpf/verifier.c:13049 [inline] adjust_scalar_min_max_vals kernel/bpf/verifier.c:13237 [inline] adjust_reg_min_max_vals.cold+0x116/0x353 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:0x43b0a9 Code: 48 83 c4 28 c3 e8 17 1a 00 00 0f 1f 80 00 00 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 48 RSP: 002b:00007fffec705b18 EFLAGS: 00000202 ORIG_RAX: 0000000000000141 RAX: ffffffffffffffda RBX: 00007fffec705ff8 RCX: 000000000043b0a9 RDX: 0000000000000080 RSI: 00007fffec705b30 RDI: 0000000000000005 RBP: 00007fffec705c00 R08: 0000000400000002 R09: 0000003e00000000 R10: 00000000000000fc R11: 0000000000000202 R12: 0000000000000001 R13: 00007fffec705fe8 R14: 0000000000000001 R15: 0000000000000001