Received: by 2002:ab2:7104:0:b0:1f7:f6c3:9cb1 with SMTP id z4csp57233lql; Tue, 7 May 2024 09:36:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWAykpU89DSHv31wAySsiY4y7bxM1coPpQPK/lKDCl8FqJwoq26iQXWPZjge+I5lSonAMVW+hZmDpnoRGLkwm4dDXsEkoQMlt4QTfq/Vg== X-Google-Smtp-Source: AGHT+IEND1CFGRbB9qzkQ+yhh9r/vFZeKlHoRnEXarnOetFVLgJaeCbNc5+TW1tX/r2iHfYr3Obv X-Received: by 2002:a17:902:cf0d:b0:1eb:dae:bd98 with SMTP id d9443c01a7336-1eeb078e58cmr2109425ad.51.1715099807699; Tue, 07 May 2024 09:36:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715099807; cv=pass; d=google.com; s=arc-20160816; b=uN0HKEZPMb7WdfRwrccPf2ezwL0Q8lnuTiBCSo8cWPc56dnHPnyutJDfYKIkfMciVZ wvLk8MWiKjFliqKRKtOmkJvfp0m5rcBJZbHZVUkPB3rqCwxlrkomINYvPSwqJ8exHI5R ZYz28dZJqVElTqeiANm2fCLYwFgX3g/3a8HMIrxQukPXY1IYlMsUYFV//QurMdaBqXuA NVb4UoWnCQF76QPChLo6P7mMeEsewjxA1XnRhZQDR5MHAuiaiQLg/5ZzX+2gIvOmEDLM XvAvCMh4O+Zhk0+rQ/naBb8/sn7aFX/zclZdUbXLTzLBdZcc4ulZlyfj4ZycvANMl9yM R9rQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=QeJwneIQoupo8QkN2Q+WsjpfxqenpfhUm2BwJMPTxBw=; fh=j7ar0DwGbfrM6tDH5Zj71+LiDQ1D8Q4THBzHWJw0cb4=; b=F1kvLHBKYpDxUk2K9MPxKpbT6Z8RDrjB8lXeOlX4rZm/4jBebsfg0NVFD5SslGZhsC /lEq/RMgMgWSdmGPN2IJI1GBioKjD9fK7yntbCDJWGMAdFRcDWFBQohG7hDlqUZ4n6ia PVTm05dDUqJy1nD5XL0PocfG4fIe4R1yvkbqRXgpjJUjsQOUAIiH7hp32M5vOH2XUXac 1vfP3b3yPJ4L8UmEGWbevS9EVCr8UAjeFCHjgPM5wFuypHCH/L44tICATZri2s99F5ty 0gdKc3Z98Mj1+7YrhWDXhT/cBkxanIQchHGgbLxc8p9MgIiTj552uvJT4RIYrhL27sL8 Hdmg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=V5OPKhW1; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-171754-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171754-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id m12-20020a170902d18c00b001ebe3f1ede0si10200738plb.402.2024.05.07.09.36.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 09:36:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171754-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=V5OPKhW1; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-171754-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171754-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id E79CCB28F1E for ; Tue, 7 May 2024 16:03:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1EF6B16F858; Tue, 7 May 2024 15:58:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V5OPKhW1" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4B4216C6AA for ; Tue, 7 May 2024 15:58:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715097508; cv=none; b=WmkekErJueof+C7Tk2wbzfQAgcDa1RCw1G+KY4g2aDMa+VEB42F5tB/zlPtnVeeClRgIE4l4mA98H4Js8i5waItpjS2Dq7AXxm3DaouoNc4Ptvkz/MDikPbpRycRghXF5IX17UgmKk4YKkPJQmwJy2eT5E9XpqH5JxU9EyaxuRI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715097508; c=relaxed/simple; bh=aWR8vi3ctb5xuNS5aLdoGpoQVQX3R/AX8tzJO890OYU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ur1cjyfReOrIKeVT9+nfFFU800Ni9Ew5im/sSmTpq32MWoABpuRCVztm5BlkDd+WnNf0aF3eE2caEPIDrJlPJFufN57mbzas4/QA0P0Wh3kds3w70xCcROLOQPApPu6o9zwaVvQx8QotpC0UlATgK3OrZh2VsCKaSGI4ukClgIY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=V5OPKhW1; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715097503; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QeJwneIQoupo8QkN2Q+WsjpfxqenpfhUm2BwJMPTxBw=; b=V5OPKhW1+BWcqS23/1fFUjMKvZbeGipkIER0abPetR8nVDGU8UBa5b+oa4j47y7hXipvqr +8OCxdnhet5u+OXsS9mZWfEIYHn4ecOOy89O91FxPtA8zmBndIexvXijEX+oWwRePHjfO4 nlkWuyC2Nnjtktzq8F7FRP7yKIPdTfM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-32-EwPh8TxkNUSzca5Oyj3J0w-1; Tue, 07 May 2024 11:58:21 -0400 X-MC-Unique: EwPh8TxkNUSzca5Oyj3J0w-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1B6073802AC9; Tue, 7 May 2024 15:58:21 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id E866A401441; Tue, 7 May 2024 15:58:20 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , David Matlack , Kai Huang Subject: [PATCH 17/17] KVM: x86/mmu: Sanity check that __kvm_faultin_pfn() doesn't create noslot pfns Date: Tue, 7 May 2024 11:58:17 -0400 Message-ID: <20240507155817.3951344-18-pbonzini@redhat.com> In-Reply-To: <20240507155817.3951344-1-pbonzini@redhat.com> References: <20240507155817.3951344-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 From: Sean Christopherson WARN if __kvm_faultin_pfn() generates a "no slot" pfn, and gracefully handle the unexpected behavior instead of continuing on with dangerous state, e.g. tdp_mmu_map_handle_target_level() _only_ checks fault->slot, and so could install a bogus PFN into the guest. The existing code is functionally ok, because kvm_faultin_pfn() pre-checks all of the cases that result in KVM_PFN_NOSLOT, but it is unnecessarily unsafe as it relies on __gfn_to_pfn_memslot() getting the _exact_ same memslot, i.e. not a re-retrieved pointer with KVM_MEMSLOT_INVALID set. And checking only fault->slot would fall apart if KVM ever added a flag or condition that forced emulation, similar to how KVM handles writes to read-only memslots. Cc: David Matlack Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Message-ID: <20240228024147.41573-17-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index d717d60c6f19..510eb1117012 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4425,7 +4425,7 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, if (unlikely(is_error_pfn(fault->pfn))) return kvm_handle_error_pfn(vcpu, fault); - if (WARN_ON_ONCE(!fault->slot)) + if (WARN_ON_ONCE(!fault->slot || is_noslot_pfn(fault->pfn))) return kvm_handle_noslot_fault(vcpu, fault, access); /* -- 2.43.0