Message ID | 20221031114340.4185587-1-cuigaosheng1@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2261708wru; Mon, 31 Oct 2022 04:45:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4RKYLtXPYlIBLraG8n5+WfiWTmQ3UdspfcAxd1Ic0DrXVF3aPKx4eLTExpulOmYwsN9/OX X-Received: by 2002:a05:6402:51d0:b0:462:847f:212b with SMTP id r16-20020a05640251d000b00462847f212bmr13098450edd.397.1667216714750; Mon, 31 Oct 2022 04:45:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667216714; cv=none; d=google.com; s=arc-20160816; b=OI5W8ANGpbPwl2BVhP0ufaSnUb80PR7yPTAMxSWZt05VGe/EznG7R7EzxtrqrSQpo5 DfxOG4Zmy2mfx/C3n3KP8DesRW+oto2YQdddCrTPuXd3DavWWi/d2E0Y6AgEXIIFofRk Ri1gWwfRclI5J0jsZTR1Xu5sBUxWg9++nR03ieXBYyxKGwdFj4x365sLqcjz0+ChPc6A UI61hKo3lESJLeun340wrqBbw3Sg/NCSgi1wRgOluzaA0Xx3bkZ4O9omfwuQj6l0Eg+U w4USgGzEoXPjhgnDXvS5SHhb1zKhZ5kBJrauid49fSTZD5k9wg6uYrTe69T8B+WrO4T3 1+cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=7pw0zVBS+aRF1cIc7j78dio4ucR9kq30QXUq/KFjpuY=; b=GxZ3Y+gcHd0bOlinD8FS3VU8Etz+C1ROvaJhf1klsF/ue4KgWliOsXSBLgtZu9uPZV PPVznaBDeFhls+L2GclhxOQzqWv8noqNYqFRvd/NsQ1l9gzD61igAdcwkEuz0Wa3Ptlf 1zAk5qSVaDBv8v5gfnRHywUjWBGRTuZR0wua4LX70sFzq+Mbm1hvRJo5pYhynrnhXTzg LyT04mIP1V+Adglf3vT9w+EGe/4rNN7BCvKlTmTGcI+lVR+Nf8xkHYP52zrAq5X8Z3C8 TyegykSic1UzkwrRF95vwGHhdwnXiv0OY80prgNtQDMtoEzMt/E1k5P0R2WWMPyOfMsX cGQg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x3-20020a170906148300b007a4feae7adfsi6204519ejc.565.2022.10.31.04.44.50; Mon, 31 Oct 2022 04:45:14 -0700 (PDT) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231131AbiJaLn7 (ORCPT <rfc822;kartikey406@gmail.com> + 99 others); Mon, 31 Oct 2022 07:43:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230401AbiJaLno (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 31 Oct 2022 07:43:44 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D232203 for <linux-kernel@vger.kernel.org>; Mon, 31 Oct 2022 04:43:43 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.56]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4N1B4L1Rqyz15MHL; Mon, 31 Oct 2022 19:38:42 +0800 (CST) Received: from cgs.huawei.com (10.244.148.83) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 31 Oct 2022 19:43:41 +0800 From: Gaosheng Cui <cuigaosheng1@huawei.com> To: <tglx@linutronix.de>, <mingo@redhat.com>, <bp@alien8.de>, <dave.hansen@linux.intel.com>, <x86@kernel.org>, <hpa@zytor.com>, <tony.luck@intel.com>, <pawan.kumar.gupta@linux.intel.com>, <pbonzini@redhat.com>, <chenyi.qiang@intel.com>, <jithu.joseph@intel.com>, <alexs@kernel.org>, <cuigaosheng1@huawei.com> CC: <linux-kernel@vger.kernel.org> Subject: [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb Date: Mon, 31 Oct 2022 19:43:40 +0800 Message-ID: <20221031114340.4185587-1-cuigaosheng1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.244.148.83] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748203433536690785?= X-GMAIL-MSGID: =?utf-8?q?1748203433536690785?= |
Series |
x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb
|
|
Commit Message
Gaosheng Cui
Oct. 31, 2022, 11:43 a.m. UTC
Shifting signed 32-bit value by 31 bits is undefined, so changing
significant bit to unsigned. The UBSAN warning calltrace like below:
UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21
left shift of 1 by 31 places cannot be represented in type 'int'
Call Trace:
<TASK>
dump_stack_lvl+0x7d/0xa5
dump_stack+0x15/0x1b
ubsan_epilogue+0xe/0x4e
__ubsan_handle_shift_out_of_bounds+0x1e7/0x20c
intel_detect_tlb+0x114/0xbd0
identify_boot_cpu+0x29/0x9e
check_bugs+0x2f/0x15a5
start_kernel+0xc3f/0xc78
x86_64_start_reservations+0x24/0x2a
x86_64_start_kernel+0xed/0xf8
secondary_startup_64_no_verify+0xe5/0xeb
</TASK>
Fixes: e0ba94f14f74 ("x86/tlb_info: get last level TLB entry number of CPU")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
---
arch/x86/kernel/cpu/intel.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Mon, Oct 31, 2022 at 7:43 PM Gaosheng Cui <cuigaosheng1@huawei.com> wrote: > > Shifting signed 32-bit value by 31 bits is undefined, so changing > significant bit to unsigned. The UBSAN warning calltrace like below: > > UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21 > left shift of 1 by 31 places cannot be represented in type 'int' > Call Trace: > <TASK> > dump_stack_lvl+0x7d/0xa5 > dump_stack+0x15/0x1b > ubsan_epilogue+0xe/0x4e > __ubsan_handle_shift_out_of_bounds+0x1e7/0x20c > intel_detect_tlb+0x114/0xbd0 > identify_boot_cpu+0x29/0x9e > check_bugs+0x2f/0x15a5 > start_kernel+0xc3f/0xc78 > x86_64_start_reservations+0x24/0x2a > x86_64_start_kernel+0xed/0xf8 > secondary_startup_64_no_verify+0xe5/0xeb > </TASK> > > Fixes: e0ba94f14f74 ("x86/tlb_info: get last level TLB entry number of CPU") > Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com> Reviewed-by: Alex Shi <alexs@kernel.org> > --- > arch/x86/kernel/cpu/intel.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c > index 2d7ea5480ec3..121c1c38162a 100644 > --- a/arch/x86/kernel/cpu/intel.c > +++ b/arch/x86/kernel/cpu/intel.c > @@ -945,7 +945,7 @@ static void intel_detect_tlb(struct cpuinfo_x86 *c) > > /* If bit 31 is set, this is an unknown format */ > for (j = 0 ; j < 3 ; j++) > - if (regs[j] & (1 << 31)) > + if (regs[j] & (1U << 31)) > regs[j] = 0; > > /* Byte 0 is level count, not a descriptor */ > -- > 2.25.1 >
On Mon, Oct 31, 2022 at 07:43:40PM +0800, Gaosheng Cui wrote: > Shifting signed 32-bit value by 31 bits is undefined, so changing > significant bit to unsigned. The UBSAN warning calltrace like below: > > UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21 > left shift of 1 by 31 places cannot be represented in type 'int' Is it really? Shouldn't -fstrict-overflow define this case?
On Mon, Oct 31, 2022 at 01:30:57PM +0100, Peter Zijlstra wrote: > On Mon, Oct 31, 2022 at 07:43:40PM +0800, Gaosheng Cui wrote: > > Shifting signed 32-bit value by 31 bits is undefined, so changing > > significant bit to unsigned. The UBSAN warning calltrace like below: > > > > UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21 > > left shift of 1 by 31 places cannot be represented in type 'int' > > Is it really? Shouldn't -fstrict-overflow define this case? -fno-strict-overflow that is, which implies -fwrapv which ensures 2s complement, at which point shifting signed numbers is fully defined.
We don't need to add UBSAN calltrace, so I have merged the patch with another patch("x86/cpu: replacing the open-coded shift with BIT(x)"), please ignore this patch,thanks for review this patch! On 2022/10/31 20:32, Peter Zijlstra wrote: > On Mon, Oct 31, 2022 at 01:30:57PM +0100, Peter Zijlstra wrote: >> On Mon, Oct 31, 2022 at 07:43:40PM +0800, Gaosheng Cui wrote: >>> Shifting signed 32-bit value by 31 bits is undefined, so changing >>> significant bit to unsigned. The UBSAN warning calltrace like below: >>> >>> UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21 >>> left shift of 1 by 31 places cannot be represented in type 'int' >> Is it really? Shouldn't -fstrict-overflow define this case? > -fno-strict-overflow that is, which implies -fwrapv which ensures 2s > complement, at which point shifting signed numbers is fully defined. > > .
diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 2d7ea5480ec3..121c1c38162a 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -945,7 +945,7 @@ static void intel_detect_tlb(struct cpuinfo_x86 *c) /* If bit 31 is set, this is an unknown format */ for (j = 0 ; j < 3 ; j++) - if (regs[j] & (1 << 31)) + if (regs[j] & (1U << 31)) regs[j] = 0; /* Byte 0 is level count, not a descriptor */