Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp430676pxk; Thu, 1 Oct 2020 06:06:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTormQq8iB6KA7xgxyiIloZygq+0G0k84f5S8lX8RySm3y2aB3A/cMm69nR8sJJUbgv2/S X-Received: by 2002:a17:906:c2d2:: with SMTP id ch18mr8455592ejb.79.1601557606379; Thu, 01 Oct 2020 06:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601557606; cv=none; d=google.com; s=arc-20160816; b=sIt90D+51wEPh9o7y9AE2Blkdub5beGmszJfGdtaX422KZ0R2JoibDoiIeY/gDzwgx AN3ipK6C7RcuMFXOe+NxFfntdD4XRrZlbw0wMB7nIieYh4r/liuuQtXQcl3NqV+qULqc mUPCWdvgb47q31T+Fr1RodT0uIITxONwN6b5Hud609xx2EhaKsd2Jr2CVd+FyR1g0mMB 6Hept0zgP8JPB7unF/bwifeTwB7wvq/tnIzfN8yFIV3xsbsVSH7fvNhVgGYVXpn24ZNh KoGmffFBxseVtRiuwpHdctW9BGyN1wjzm9XVqpH+eAvV4MWdce9pWdSDcf9J5ri/WC+E Jlpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tc5uVwrXTmnAKpdzTTt3ZNORqzG7l9kwmUbcnLDV6EM=; b=02j/ahCI3l7hbzmPnx7UZVD0ZDvvnFe0oq7hO2HlsEDqFz4yQ0KRoN9+K4mk46kt/k U0tZwIHheaIH0m43gSHJGOGUlp+7Y1NUAoXFrRf6AoWLdehGyeGAXiM9a5q1T0HQzRNV MCjossXkiKZIgnJwaiu22fGs7QivTp7EewLDXVwoCLUi4zh6W6Mcpv+lWz44nZl0qs5u UdLEGQZiW13mOpVw+eW+35vdvsYHSpn9dOr1uj1u0OCDLQOBWECJin0np9qagD3iXfC0 yWoyjfwKILm0WRS5cyFU1VCsVB2+MRfDVujIdZxdLfhQ0vZiQe5eCQhdtcrb5AYFFVFi Rp/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BqowF1t5; 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 a7si3343525ejy.600.2020.10.01.06.06.19; Thu, 01 Oct 2020 06:06:46 -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=BqowF1t5; 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 S1732018AbgJANED (ORCPT + 99 others); Thu, 1 Oct 2020 09:04:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:57186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731952AbgJANEC (ORCPT ); Thu, 1 Oct 2020 09:04:02 -0400 Received: from localhost (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6ED5C207FB; Thu, 1 Oct 2020 13:04:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601557441; bh=jv+eF8Q2btStYdRPVW0DsLC9P3eBMViIk17Uwk3FEc8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BqowF1t5A5t0LeZbkZhqNFujAECPtAY7YX9872OUb5Ve8uYAkY/1dbMWMjKBi+ehX e+QewqcKFVprGK/Dju6vBqHZsJF6oi+4teTwoFk+q5Kty5UPfYX0YjmNHl4EKeG/8j zj/cvltNUCIN39WPyCtVBZnAEQbNo/pKi2l3zUEY= Date: Thu, 1 Oct 2020 09:04:00 -0400 From: Sasha Levin To: Wei Liu Cc: Vitaly Kuznetsov , tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, mikelley@microsoft.com, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, stable@kernel.org, kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com Subject: Re: [PATCH] x86/hyper-v: guard against cpu mask changes in hyperv_flush_tlb_others() Message-ID: <20201001130400.GE2415204@sasha-vm> References: <20201001013814.2435935-1-sashal@kernel.org> <87o8lm9te3.fsf@vitty.brq.redhat.com> <20201001115359.6jhhrybemnhizgok@liuwe-devbox-debian-v2> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20201001115359.6jhhrybemnhizgok@liuwe-devbox-debian-v2> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 01, 2020 at 11:53:59AM +0000, Wei Liu wrote: >On Thu, Oct 01, 2020 at 11:40:04AM +0200, Vitaly Kuznetsov wrote: >> Sasha Levin writes: >> >> > 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 | 4 +++- >> > 1 file changed, 3 insertions(+), 1 deletion(-) >> > >> > diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c >> > index 5208ba49c89a9..b1d6afc5fc4a3 100644 >> > --- a/arch/x86/hyperv/mmu.c >> > +++ b/arch/x86/hyperv/mmu.c >> > @@ -109,7 +109,9 @@ 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() && hv_cpu_number_to_vp_number(last) >= 64) >> > goto do_ex_hypercall; >> >> In case 'cpus' can end up being empty (I'm genuinely suprised it can) I was just as surprised as you and spent the good part of a day debugging this. However, a: WARN_ON(cpumask_empty(cpus)); triggers at that line of code even though we check for cpumask_empty() at the entry of the function. >> the check is mandatory indeed. I would, however, just return directly in >> this case: Makes sense. >> if (last < num_possible_cpus()) >> return; > >I think you want > > last >= num_possible_cpus() > >here? > >A more important question is, if the mask can change willy-nilly, what >is stopping it from changing between these checks? I.e. is there still a >windows that hv_cpu_number_to_vp_number(last) can return garbage? It's not that hv_cpu_number_to_vp_number() returns garbage, the issue is that we feed it garbage. hv_cpu_number_to_vp_number() expects that the input would be in the range of 0 <= X < num_possible_cpus(), and here if 'cpus' was empty we would pass in X==num_possible_cpus() making it read out of bound. Maybe it's worthwhile to add a WARN_ON() into hv_cpu_number_to_vp_number() to assert as well. -- Thanks, Sasha