Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp67947pxk; Mon, 5 Oct 2020 18:03:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwWyn4pEeHwG+YnAePZ+wW27BRHk6uF03UKISjrdiduYNMhjwRBFS0cVx+XkBcSRgwiiIC X-Received: by 2002:a17:906:a841:: with SMTP id dx1mr2553543ejb.520.1601946220476; Mon, 05 Oct 2020 18:03:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601946220; cv=none; d=google.com; s=arc-20160816; b=pRj1IsWMd8BwbIFPCab79B/R47qiweCoJtjFjHTsqM9RY6PGDjDUy7GUpOHdWnmIDD ww3vzNVYtichSw4m1eBZbaF1Gvkwzo4Mnw50lPJa/WD3ntvn7DMOyLV+5rm3CQPQOPyj cJqqyWxW30vpq5CmmG04c+jaVUL0X/7x4Q5l1hXQXnigoI/x33XTLA/kSpsO7+fJYU/s U+3bkPwUDgxUxM0u4jZxlOnYUmQkCM9ruWmFLSgkcsZKA4uBnRIP4NNincdMvxTU2Bfs LvKyPAD4Hb8aLUzKaEDes52oDmHUapywbjZfpzYPbk9NtLpGR8D8fgzAOfdrNyKvSsKr 2rWw== 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:dkim-signature; bh=TMFxt6RsQ2m2C++vznm6GcF23LADPUra/RnHHPKG2uI=; b=j5QYD4lzKu+XuDw3F0pM47TwAalW7C8MVu/sv/MowZCvobf7ORphE32hqoWVmGwA9d TvJYsCj7uwobdv7xctOncTseGLyfhJLvMeIYm3DhTcNIQpQTmL3RNpP0nQwB/VMvFZ/q RTGEnMZlLUXqacA4SYG84g34XaiuUpu1kxDfq4pGEhRQclbuFwt7SvmEI2AKjNjpYLYr fbnQZ6883TbpOmc+DukY9+F7WhOQtQNwvms24r2ZaAIprYG6HmfuQDkoDIqgP+b97BP/ GejIkXx880+PHFJNQdgV9Zi2vA9XLAjKvQ7BTTODLB/FMh03LvBNe8N72eESvdzIPYxg 8o7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XSIOLG1I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i27si1155311edb.55.2020.10.05.18.03.17; Mon, 05 Oct 2020 18:03:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XSIOLG1I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727156AbgJEXhp (ORCPT + 99 others); Mon, 5 Oct 2020 19:37:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:37980 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725973AbgJEXhp (ORCPT ); Mon, 5 Oct 2020 19:37:45 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4AAE7206CB; Mon, 5 Oct 2020 23:37:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601941064; bh=E53JolE8ITBFwkvdUaidAw3g9c5lp+aUUbF/TIdypOI=; h=From:To:Cc:Subject:Date:From; b=XSIOLG1I4vM3XlyNhhZYwBvMlCGMCl4fYPHBkR+2E+8/39OXAIl/kqmn62nXoyZTj 3cRDOnBvnaUitDFX4dXeJZiHu/qm+/MeYfQ/eiocLda26V9JC8idUIOGg3EAwbQRn0 eCSceWhVlY/6MXfVxcXn5GVAPYVKHIyAP/sGkUfs= From: Sasha Levin To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, vkuznets@redhat.com, mikelley@microsoft.com, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, Sasha Levin , stable@kernel.org Subject: [PATCH v2 1/2] x86/hyper-v: guard against cpu mask changes in hyperv_flush_tlb_others() Date: Mon, 5 Oct 2020 19:37:38 -0400 Message-Id: <20201005233739.2560641-1-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org cpumask can change underneath us, which is generally safe except when we call into hv_cpu_number_to_vp_number(): if cpumask ends up empty we pass num_cpu_possible() into hv_cpu_number_to_vp_number(), causing it to read garbage. As reported by KASAN: [ 83.504763] BUG: KASAN: slab-out-of-bounds in hyperv_flush_tlb_others (include/asm-generic/mshyperv.h:128 arch/x86/hyperv/mmu.c:112) [ 83.908636] Read of size 4 at addr ffff888267c01370 by task kworker/u8:2/106 [ 84.196669] CPU: 0 PID: 106 Comm: kworker/u8:2 Tainted: G W 5.4.60 #1 [ 84.196669] Hardware name: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS 090008 12/07/2018 [ 84.196669] Workqueue: writeback wb_workfn (flush-8:0) [ 84.196669] Call Trace: [ 84.196669] dump_stack (lib/dump_stack.c:120) [ 84.196669] print_address_description.constprop.0 (mm/kasan/report.c:375) [ 84.196669] __kasan_report.cold (mm/kasan/report.c:507) [ 84.196669] kasan_report (arch/x86/include/asm/smap.h:71 mm/kasan/common.c:635) [ 84.196669] hyperv_flush_tlb_others (include/asm-generic/mshyperv.h:128 arch/x86/hyperv/mmu.c:112) [ 84.196669] flush_tlb_mm_range (arch/x86/include/asm/paravirt.h:68 arch/x86/mm/tlb.c:798) [ 84.196669] ptep_clear_flush (arch/x86/include/asm/tlbflush.h:586 mm/pgtable-generic.c:88) Fixes: 0e4c88f37693 ("x86/hyper-v: Use cheaper HVCALL_FLUSH_VIRTUAL_ADDRESS_{LIST,SPACE} hypercalls when possible") Cc: Vitaly Kuznetsov Cc: stable@kernel.org Signed-off-by: Sasha Levin --- arch/x86/hyperv/mmu.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c index 5208ba49c89a9..6cac9f5857af4 100644 --- a/arch/x86/hyperv/mmu.c +++ b/arch/x86/hyperv/mmu.c @@ -109,7 +109,14 @@ static void hyperv_flush_tlb_others(const struct cpumask *cpus, * must. We will also check all VP numbers when walking the * supplied CPU set to remain correct in all cases. */ - if (hv_cpu_number_to_vp_number(cpumask_last(cpus)) >= 64) + int last = cpumask_last(cpus); + + if (last >= num_possible_cpus()) { + local_irq_restore(flags); + return; + } + + if (hv_cpu_number_to_vp_number(last) >= 64) goto do_ex_hypercall; for_each_cpu(cpu, cpus) { -- 2.25.1