From patchwork Wed Feb 28 02:41:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 207641 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3099845dyb; Tue, 27 Feb 2024 18:46:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWdjSY5z6CyA2yXYe0Ao/DVdvhdNuF+9tC5Ts/a478J+OvzeLgcMsgalP2KqV6NJcvNen0kud/sNIx0htxmXhdGNaygOg== X-Google-Smtp-Source: AGHT+IEQFU04OKD3c4ZvK6K0P7uPT2vzFDFD1SZtAIue5QFgsnFs+2RP3w3Trzn3fIf/Ef8cHx6R X-Received: by 2002:a67:ff8f:0:b0:472:6871:f637 with SMTP id v15-20020a67ff8f000000b004726871f637mr1244128vsq.30.1709088418695; Tue, 27 Feb 2024 18:46:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709088418; cv=pass; d=google.com; s=arc-20160816; b=CpId9ahwHLUjhYlQzEI4rDG/BpEOwMqaIZ75RlaQCcAg9w+DQ7UNWdWCmp0O8qTK6q m16PIsYDxqG9hjcv5rnGKtrx1keFUZv/SYMMzxphocyBEUXbQgSsxovNygAbT5XoGJNv fSvIe2gOYTQShYsqJPx+1Q1rfb1356pxI2QUC5IvEjtWEwNjN08DDAsdzYnNZBieNn/G 8x5mjUzCL2rBHa1NCVe3xfsdCZ7Wnrip6oMN//93hJvFPAwbvk9wXqpjLxwbNrYGBNnY 8zKZf6Jk15nSMdld8uy7LT5GRZDeTPxReLxkbWANGJipRnShjb6jrjl40h40CEXcZd2a GQyw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=fK/e/x3+Tf/3Rz4S7hZymU4b4heBfx/RABet1RXMTfk=; fh=dZJ1NJ4YSYnqptr7PaV6Hk0hOhmiwhx15uE/SqHDNvo=; b=ieI3xnR1IfXskaGxQ+OtxLx+dVgsn+5IA5F/Kv3EtrmvwO3418QJFuANCeDMp7BppG 5uKmVS2041GIsQllO+YQZZxO6mggl5sfNEfeExPQb8tubrMSD+6ifrBdcjJ+SPmaA7aL 1BrVy9UhvjlxnSRvt1DrKgqR7FHvSyivZQHdKa4QsDMzfYrjisfxYx7MKblpbk4jXG9R jVIpVNhTdrUzhf3aQWr+HSgpuhxEdA7LmjLr0uLfuGgHULtYwxcgXpzzs5OugnWqX/xO VWl42stZiZ2rR/9QuhPwdHRniot7YjWgCVU/ymowKb94qEzhHmevmsPaPv7/T/2GexPk cY6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rpsnoFLQ; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-84442-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84442-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g17-20020ac85811000000b0042e8e86362csi4818288qtg.517.2024.02.27.18.46.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 18:46:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84442-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rpsnoFLQ; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-84442-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84442-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 7AF5A1C236E8 for ; Wed, 28 Feb 2024 02:46:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77765200BA; Wed, 28 Feb 2024 02:42:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rpsnoFLQ" Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08EFE2D05D for ; Wed, 28 Feb 2024 02:42:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709088146; cv=none; b=fb1qb5FKxu4jIeUFHcH0/IKMsIEVMFF7lx6NjBURJfrkhEFWVXJUNbs3YGNVUdIWFYz9p0h3h2FSOfpFw5mI9isvWlIBoT2rsU6PTa4q99urS3fX22PWOjhrxZpzI4cX0qbZgZdNQ96M5TaWpqG9bZ4saVQzkXxYNjh/ANTuOqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709088146; c=relaxed/simple; bh=szLQWH4Y5Bzo6Uy7reBTywxc6dUFXHryHkO47zK9DXo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OuL5yGe5S+omMHQDD8/IEq325uE4sjYbFC3Su3B9rhHVNYRJzJ75EkG7pYMFHaIa6W2zrzsNLU9BSOHxqYrXHoO2Ah+042Mr27S3L+LDFv+Jq01aBc0Uuug6/XDvNHeFoFUBcXFxA82/m3N/+cPGbXNDGIOz120sDSdv55ds7sU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rpsnoFLQ; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2993efc802dso2200183a91.1 for ; Tue, 27 Feb 2024 18:42:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709088140; x=1709692940; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=fK/e/x3+Tf/3Rz4S7hZymU4b4heBfx/RABet1RXMTfk=; b=rpsnoFLQv8Hr66kEDU1R/DQUQ6CdvcALLXvKoaMxmlhmQQG0O67AeLnt9n4kiGI3r+ PWR7gPyio7d5kk9PHvrwe7XFTqnGtSBvBBUha/E58qdt+bIRrE6ajvnakAJv8ygpVNm0 ZVB22rxYNL2l03f1fOgK3fKGP2AMjMOFUenCCXtDI1rvr148vHTjobq17YZnIdfdOrty Z7LqtuCCEzC2upLTszhOf9oi1MamnqctqOUGgibMm3WrrOxMYmtZtV/76so88kR7Fwl2 KgJ8DDkgQakRo3CVqGSAEY/9qgpAiz6YRMPVu36M27+i2ImmG5uj2SU1X8ON65jaU2KD OIUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709088140; x=1709692940; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fK/e/x3+Tf/3Rz4S7hZymU4b4heBfx/RABet1RXMTfk=; b=S2jsREXF+16tvZn+wmzZF2taq3zf98Uuu+BKxqGosvPQJsOKVtiLQU8aqW1XeACQrO Ynx8YM97sCZTYroOFyZB0NjgekD248oeBS3qHPDYKfmB168y655p7tCQQTQ8zXUsnmw1 /M3iMU8zD/foTHvlkWS0+dHA165VwvohQ2sle60mkzoyuGvi3zDZ2pyg0Ivg3NFjg08p YYsrkuEQ3AFECkm17PEZXnTawZ/6voYMlVjJQzyvDScWvm5P0BItqbpRnbktV7zc1vjD 4wVWBdT82dyfh9WpAJu1yTCFLHlPyYuOhz8WmQTgmdK3o+ZwPzhPYnp2KLmOLEkUtcjF PekA== X-Forwarded-Encrypted: i=1; AJvYcCV4gy9Ffufa+mXn2Y1GxwyumLJdHGKKAkM6Yo7VNEW4WC9cHPlDr5Mp7MI12v2nPs4QGJhtoNelM9tC0Sdbxb9TNtVHarVuBwCrYelv X-Gm-Message-State: AOJu0YyV9xfHfz/yqbZLOQvuMzia9i8Zs21dBaaVrEDKmhc25jQm6WcR s/jODDAipauaN4uokey+YzmVY9Y8VOfatF1YYSxz2pFbQt13cd5HTmCVE+UwNexWQ5ydUgsuwzo M6w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:2d0b:b0:299:40c3:338f with SMTP id sy11-20020a17090b2d0b00b0029940c3338fmr38855pjb.1.1709088140382; Tue, 27 Feb 2024 18:42:20 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 27 Feb 2024 18:41:47 -0800 In-Reply-To: <20240228024147.41573-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240228024147.41573-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240228024147.41573-17-seanjc@google.com> Subject: [PATCH 16/16] KVM: x86/mmu: Sanity check that __kvm_faultin_pfn() doesn't create noslot pfns From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yan Zhao , Isaku Yamahata , Michael Roth , Yu Zhang , Chao Peng , Fuad Tabba , David Matlack X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792109097580254484 X-GMAIL-MSGID: 1792109097580254484 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 --- 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 43f24a74571a..cedacb1b89c5 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4468,7 +4468,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); /*