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.129.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 2DC524219FD for ; Fri, 29 May 2026 18:35:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079762; cv=none; b=oRcwipKzSxe92zutRS6hOo0lQlajOj7S+/wrowV9oFgp7DefLDno998QiNor96EauT2pEyeR+4JRtofnT9L7hyComNnj9Wwh2g6KGoUAbvOKsCFoOQpOii6RsVkKUdmXYTXS5x8codzUo1O4OCNdmN5RuYaYYyF/UaHcUae3TZM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079762; c=relaxed/simple; bh=d9xvjs+tTx/sVZNCwndNXY07gix+BakluyzBTGottoM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gCCQdohzXjcGZA2CU0DUQBb3TykyAjQDdJHM2vWPxctQLViraclDniLgcOSNypJcMDx7+waZT0YN8eJOkDbmEiJNIUIUleAS4+IalV3l3OpsrefkVIPq8tgNc3DLbHWzBtQMcP3RaWvqLSeUpVPwxD46r9jTq6jq9F3oZSoxPPQ= 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=Ka5a/O6h; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=NbgtCbQ0; arc=none smtp.client-ip=170.10.129.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="Ka5a/O6h"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="NbgtCbQ0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780079756; 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=bItx102/eY014jSvfKnVDtrZQixF/uHwn/vTfUZ9ulM=; b=Ka5a/O6hURURRfck86omhX0On33avbU68gRZKtnXdod6jGtm4bJxUDAsh2vMy3NSTKDAev sz22WWxiDZFtEgZeTD81XjkMUtSPC3X2cBi+azFZ1X3SKU6DEStw3214tGMSULhKphJRw4 drZd9dvrQiqrA3fCRTsfqJdBhQNvkpg= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-677-c2UTSeP6OjGGfmdrKijJsg-1; Fri, 29 May 2026 14:35:54 -0400 X-MC-Unique: c2UTSeP6OjGGfmdrKijJsg-1 X-Mimecast-MFC-AGG-ID: c2UTSeP6OjGGfmdrKijJsg_1780079753 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4909c0f0ba2so7785445e9.3 for ; Fri, 29 May 2026 11:35:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780079753; x=1780684553; 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=bItx102/eY014jSvfKnVDtrZQixF/uHwn/vTfUZ9ulM=; b=NbgtCbQ010XyI1gQ6VOPWKSPEcwOXucv/uB6U+6QjR1MsGeSBmCyv4fgwID7Qd9RlC hOeHuKyLTt19NVFFDp0dfnH7pS7IPVzs/U0gCpRLWD469fId6MWP5iA9vC3Nxt+rVpSs X5GtCiTY1Uhx13L5yZESp/2Y/9rvIfetDZAzGTF0f+ypJUWRWPia9WD/2jWqKsyR7T7c 9/KYr8zMNWwhifwJSEZPl94Q5b41J5JX/RkLfJ2mtonRK0VO77suVjZhKaEL+HXRHdDE 0nUukwyQko41cI9h5gj8W0sFvqNolZv9nu4fwoWvLWpZeh9knrCTzed6ghlT9yk99fgx LG4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780079753; x=1780684553; 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=bItx102/eY014jSvfKnVDtrZQixF/uHwn/vTfUZ9ulM=; b=DyAKV0z2IoQZ7KgtOzLa+3lEraT/8Sff2SzSHZdHs60BWv1x1IsN98bHjFF8aconDc LzX9MelO8nkqNJeI7ZqDnScc6cMCV6AFxRJTzsn44EUzckTS14rGgIfY5mqiWZkYKyLm jzMKKX+BQf1HL1nYHsx5wHND4vptzM3IJnoCquzzd49FRbkWIofXYS1etrVhgSOUdAcA Yv9M8mjmK9wcCjD5dwb7gjQy8U9SUZRUa7R16o80l8f7ytnaYO/jVQ7rFd/J1Mt/Hicp 5h701sKUJmrZjAsybHTrTEdYHlSC0iXZbCFj023U0hgClxjF2BQsig4rtLC8PVi1UbCr t57g== X-Gm-Message-State: AOJu0YxQG4/fJL0hyldPDekD38MgZ+numfw4d0YcjyjTURYM8f/zALZg 8zOR7dGuvjd9xkORX/5f9ugTeZWocLuloDOaw+KluFN4Pn1/W+aHT5qhmQ/+HGZ4+CIqHWdA41K fKPzlDD8VET9UkNCuJwMM8zFedusnP2pb883n0XSfJOY3VdvrUTh1tJEeUNM8KHgpuDUXD2Ekh+ QlBlG8YgIEo6Kge82T+OTD+3KUwKiLKLNgLXPrFOYX0XrV96RN1A== X-Gm-Gg: Acq92OEGLtB5ioyElpSS3Xx5YrrS3uPQIiiXmo688PY/8ANN8gK56jD8kGpHVqxLGtg ZKA//BlW4xDfxKU74iCcQ9skpvRBmxI8uEqXCjTCMETOcaoQ7QW/LnWzO6XvmTtMIcEVPEXfK5x ipgAzwrp8w/AJ2KbsgUgLcX/n9Rg7oV4jE/D5qNQpcpT+YWAF0VIWrJ3DJjs+Pjkg1FjvtxeRIm 2/KUXcoK/xGvoKTM6Zsa1NebiVq5HAZqucgKbLyjYzCFHA04yiGOxSfTZoAWO2bR0PhLtYf98Ct VGJfiVXUZn84ZB1mIK4Q3NY7Cdqf/EPPz3GD1ENdIs5goS4XrzGQZV8xPs9i8ecIbapS+asoYoN GbrPV1hgecfRjsluzhY6XwHidaNrso7eAwJzWREMLKOV8D61wnvHfGL180hZ+VTSJnpafFmZiPt Z0Taman+i4laqlxMH9WrKnm1XXVUyzmZZ2pcxV9Q== X-Received: by 2002:a05:600c:6287:b0:490:a1a6:6f24 with SMTP id 5b1f17b1804b1-490a29338a8mr12422105e9.15.1780079753360; Fri, 29 May 2026 11:35:53 -0700 (PDT) X-Received: by 2002:a05:600c:6287:b0:490:a1a6:6f24 with SMTP id 5b1f17b1804b1-490a29338a8mr12421485e9.15.1780079752861; Fri, 29 May 2026 11:35:52 -0700 (PDT) Received: from [192.168.10.48] ([151.49.251.208]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45ef3559645sm5101632f8f.26.2026.05.29.11.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 11:35:52 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Tom Lendacky , Michael Roth , Stan Shaw , Peter Gonda , Jacky Li , stable@vger.kernel.org Subject: [PATCH 01/24] KVM: SEV: Require in-GHCB scratch area if GHCB v2+ is in use Date: Fri, 29 May 2026 20:35:26 +0200 Message-ID: <20260529183549.1104619-2-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: Michael Roth As per the GHCB spec, when using GHCB v2+ require the software scratch area to reside in the GHCB's shared buffer. Note, things like Page State Change (PSC) requests _rely_ on this behavior, as the guest can't provide a length when making the request, i.e. the size of the guest payload is bounded by the size of the shared buffer. Failure to force usage of the GHCB, and a slew of other flaws, lets a malicious SNP guest corrupt host kernel heap memory, and leak host heap layout information. setup_vmgexit_scratch() allocates a buffer via kvzalloc(exit_info_2), where exit_info_2 is guest-controlled. With exit_info_2=24, this yields a 24-byte allocation in kmalloc-cg-32 (32-byte slab objects). The buffer holds an 8-byte psc_hdr followed by 8-byte psc_entry structs, so only entries[0] and entries[1] are in-bounds. snp_begin_psc() validates end_entry against VMGEXIT_PSC_MAX_COUNT (253) but NOT against the actual buffer size: idx_end = hdr->end_entry; if (idx_end >= VMGEXIT_PSC_MAX_COUNT) { // checks 253, not buffer snp_complete_psc(svm, ...); return 1; } for (idx = idx_start; idx <= idx_end; idx++) { entry_start = entries[idx]; // OOB when idx >= 2 The guest sets end_entry=10+, causing the host to iterate entries[2+] which are OOB into adjacent slab objects. For each OOB entry: - The host reads 8 bytes (OOB READ / info leak oracle) - If the data passes PSC validation, __snp_complete_one_psc() writes cur_page = 1 or 512 into the entry (OOB WRITE, sev.c:3806) - If validation fails, the error response reveals whether adjacent memory is zero vs non-zero (information disclosure to guest) The guest controls allocation size (exit_info_2), entry range (cur_entry/end_entry), and can fire unlimited VMGEXITs to repeatedly hit different slab positions. By exploiting the variety of bugs, a malicious SEV-SNP guest can: - OOB read adjacent kmalloc-cg-32 objects (heap layout disclosure) - OOB write cur_page bits into adjacent objects (heap corruption) - Trigger use-after-free conditions across VMGEXITs E.g. with KASAN enabled, a single insmod of the PoC guest module produces 73 KASAN reports: BUG: KASAN: slab-out-of-bounds in snp_begin_psc+0x126/0x890 Read of size 8 at addr ffff888219ffb5e0 by task qemu-system-x86/2199 BUG: KASAN: slab-out-of-bounds in snp_begin_psc+0x468/0x890 Write of size 8 at addr ffff888351566648 by task qemu-system-x86/2199 The buggy address belongs to the object at ffff888XXXXXXXXX which belongs to the cache kmalloc-cg-32 of size 32 The buggy address is located N bytes to the right of allocated 32-byte region [ffff888XXXXXXXXX, ffff888XXXXXXXXX) Breakdown: 62 slab-out-of-bounds (reads + writes past allocation) 7 slab-use-after-free 4 use-after-free All credit to Stan for the wonderful description and reproducer! Reported-by: Stan Shaw Cc: Michael Roth Cc: Tom Lendacky Cc: Peter Gonda Cc: Jacky Li Fixes: 4af663c2f64a ("KVM: SEV: Allow per-guest configuration of GHCB protocol version") Cc: stable@vger.kernel.org Signed-off-by: Michael Roth [sean: write changelog] Reviewed-by: Tom Lendacky Signed-off-by: Sean Christopherson Message-ID: <20260501202250.2115252-2-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c2126b3c3072..23170b64f4a3 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3703,6 +3703,10 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_va = (void *)svm->sev_es.ghcb; scratch_va += (scratch_gpa_beg - control->ghcb_gpa); } else { + /* GHCB v2 requires the scratch area to be within the GHCB. */ + if (to_kvm_sev_info(svm->vcpu.kvm)->ghcb_version >= 2) + goto e_scratch; + /* * The guest memory must be read into a kernel buffer, so * limit the size -- 2.54.0