From mboxrd@z Thu Jan 1 00:00:00 1970 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 3D62C439001 for ; Fri, 29 May 2026 18:36:16 +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=1780079782; cv=none; b=RdBgO1/b5/DVm+7viFnhbubWlWFwv8lejKxVyKrKv2PW4U3qFoPHU9gGwHjIwwkmiRqwR1J5NYLD9nJ/uSE1U15OEgn9ecxwRNCQBTkOKGjqAup3Tztn7s3HfS7ShTLZpysOH/pjHYfTVy7WR7C8Jn+LpGWcy+uJL/4ghg/tfdo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079782; c=relaxed/simple; bh=ocrp7UiS1WanCwFhsgzTGR2ip9lAk7udP5dDEIw2HFE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RNS1lBKfSUfrQDSRM0CKCwxUXzS9DVx/KBUrs+l5JmBBFkaNPzom15QlsPnLVzPAxkzqBp9tW+lEhnsWWAxAu3RdDeOsHMNyZKXTOllCOp7ZLxyYYnclb1lSV7Kb7zuDBCxQDh2sgttxbTKHxQda80mls9JFnKVdJscmbfIo4bE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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=e+qttPKx; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=NGH+xCF4; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="e+qttPKx"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="NGH+xCF4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780079775; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ku/ngyswLIZ+ZhY/zg7dChYo6WlT7EL2y3Avs5NIsP0=; b=e+qttPKxT3DW7pTEWTJxL80biBALNS+OPOwi84PGMCjZZOVyz2c2jMAZXtDuTzOkUvcqx0 dnIZhHpy4jeKlGsV6OniCCfc7AZ5pdGKlaTS8H93ZlLRME6SfW/3FilKX4GZknevqaXSpo OJ0NQ8++qG976U/nej7zxP8JaPqSm6k= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-354-MnRhGIXwOOa6IbrCQttQtA-1; Fri, 29 May 2026 14:36:14 -0400 X-MC-Unique: MnRhGIXwOOa6IbrCQttQtA-1 X-Mimecast-MFC-AGG-ID: MnRhGIXwOOa6IbrCQttQtA_1780079773 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-45ef4931de5so410634f8f.1 for ; Fri, 29 May 2026 11:36:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780079773; x=1780684573; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ku/ngyswLIZ+ZhY/zg7dChYo6WlT7EL2y3Avs5NIsP0=; b=NGH+xCF4jFJnHxhlOunyscmGKYEc7DaPRYcJnSW9J5nzB04PqiFqprrTaTDMB/6Xyw 9frEclL06kyjdRtgzhiNPBgtsSEOulkatCPrxI08ETs+Hyuu4CSFS/XAELyfBnWJcbwc rBZyE0TjD4eCGbtPxFkGLPCcZpkWgv2r8E4f5MtG1Zp4zcyVzvTQitjWoOFKkWAJZUip dk0MsMY+kkWj+4A0zXxx8H0SGqTtvyakYWqg9O9DG7XaCjaRb0oKpAZH2eLXo5VZnvAd Hl6UKNnLog8kcp5XMUHW7nRLXL29XblFZkb8NLbPAksgqX4ara1kJ8CItlwOVb6Nbzfh xNFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780079773; x=1780684573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ku/ngyswLIZ+ZhY/zg7dChYo6WlT7EL2y3Avs5NIsP0=; b=AOxW7zOHVtpk05u3tn5c1mcj7HHYPtaVfIPD6oJFwLkt7mj7ZTvFazRoKYt3y8Tnyt n5lNs1S5j+9S7SBr8rj9KS/02CidOTCEY0gFUhHLb5K1n3Hsse+x+Zph4305N7MKYjG/ ZVjwKTHBCNEu3z7Hvn7uPimmcRF1XzSwfW+unvRUy7FWubVsR+IZuDQkTKSDFZwN55im Mq3xFkiAGUnafF8Mo2chP99KIHi/9w+M/bT6Va7wBPmLim0FITqDn+9F8/OKalqT8k/Y nbyb2WwQ6NGyqEFQVoaLlHfPhokveQ2x+yn09vBadxM6fLylooRcyV5LQoV6Y4r3s1bE 0AVA== X-Gm-Message-State: AOJu0YyReSFKpK+vtIezomH6l4NzsbiN+4fHufOxqWSA7rzVhuBbDl2w 9ljBlG++GAlGOMDnV44xI1f7tyiyg9Wsi5wy41ZPEOFqHNXB+tslHGP1rPqkQW4TuEG3zJOziMh 7Ph+v8BNxb1DVI6upT57c3Xzmq7CZoo++kPqhswHU2pqd68joLWpgkkEIUvKQG6qKbfhdEa/30F S4ub90OkjAFncUqidWaK6DE8TXOljPbzK/+YIaeaRLs5RVvh2N8w== X-Gm-Gg: Acq92OGxxi3RrbAMnf1AS1Ya/72dksIo2QsCU6OvF/musRpZ/SZwiIjXr5Gc2G7Gd2x PCNieAcBfJe5CTDxXXt4E7Yrx0Qf8skEirxiMWX9xUmxRHa1HnZZ9CSoUA8v8QjewnwpFNz0Thy +LpveaCkoXpR44HeB6PvSPjo0j48tbLdCsAyqlAp/ENO6HGKr8OsagUUUNhoDO4cduJ6w6uL0Ua 3w5axuMefwyPLC1IPdwRh/BwbUQdsfVo1ini5oG2UstOzDsFu8HcYdXPbjbeH/OLRiFLDyd4KMD 6fmmT16BjM8HkPnNb9khMG0OPJxabYeafuK1sIk1xaCWyfNkox2862UCVfZch69fBraxfrz4MNr qcYz5t4wAhgszCswYn1qoXvUDZjCTDTGTSFRXiMoslCYK1y4JXcCNC+pFaDAcj4yUMeGZ3tDrTJ ezlg13IKBH+nMl2tIbJL9FKYeCE+CZ64NZWOdaHQ== X-Received: by 2002:a05:6000:40c5:b0:45a:5392:3a19 with SMTP id ffacd0b85a97d-45ef13794fcmr7448006f8f.16.1780079772862; Fri, 29 May 2026 11:36:12 -0700 (PDT) X-Received: by 2002:a05:6000:40c5:b0:45a:5392:3a19 with SMTP id ffacd0b85a97d-45ef13794fcmr7447963f8f.16.1780079772417; Fri, 29 May 2026 11:36:12 -0700 (PDT) Received: from [192.168.10.48] ([151.49.251.208]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45ef354b7edsm5207945f8f.22.2026.05.29.11.36.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 11:36:11 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Tom Lendacky , Michael Roth , stable@vger.kernel.org Subject: [PATCH 09/24] KVM: SEV: Check PSC request indices against the actual size of the buffer Date: Fri, 29 May 2026 20:35:34 +0200 Message-ID: <20260529183549.1104619-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260529183549.1104619-1-pbonzini@redhat.com> References: <20260529183549.1104619-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-Transfer-Encoding: 8bit From: Sean Christopherson When processing Page State Change (PSC) requests, validate the PSC buffer against the effective size of the scratch area, which could be less than the maximum size if the guest provided a pointer that isn't exactly at the start of the GHCB shared buffer. Fixes: 9b54e248d264 ("KVM: SEV: Add support to handle Page State Change VMGEXIT") Cc: stable@vger.kernel.org Reviewed-by: Tom Lendacky Reviewed-by: Michael Roth Signed-off-by: Sean Christopherson Message-ID: <20260501202250.2115252-10-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 8577451b82b2..6e8cbae2135a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3903,7 +3903,7 @@ static int snp_begin_psc(struct vcpu_svm *svm) struct kvm_vcpu *vcpu = &svm->vcpu; struct psc_hdr *hdr = &psc->hdr; struct psc_entry entry_start; - u16 idx, idx_start, idx_end; + u16 idx, idx_start, idx_end, max_nr_entries; int npages; bool huge; u64 gfn; @@ -3913,6 +3913,19 @@ static int snp_begin_psc(struct vcpu_svm *svm) return 1; } + /* + * GHCB v2 requires the scratch area to reside within the GHCB itself, + * and PSC requests are only supported for GHCB v2+. Thus it should be + * impossible to exceed the max PSC entry count (which is derived from + * the size of the shared GHCB buffer). + */ + max_nr_entries = (sev_es->ghcb_sa_len - sizeof(struct psc_hdr)) / + sizeof(struct psc_entry); + if (WARN_ON_ONCE(max_nr_entries > VMGEXIT_PSC_MAX_COUNT)) { + snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC); + return 1; + } + next_range: /* There should be no other PSCs in-flight at this point. */ if (WARN_ON_ONCE(svm->sev_es.psc_inflight)) { @@ -3928,7 +3941,7 @@ static int snp_begin_psc(struct vcpu_svm *svm) idx_start = hdr->cur_entry; idx_end = hdr->end_entry; - if (idx_end >= VMGEXIT_PSC_MAX_COUNT) { + if (idx_end >= max_nr_entries) { snp_complete_psc(svm, VMGEXIT_PSC_ERROR_INVALID_HDR); return 1; } -- 2.54.0