From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 77FE5421F17 for ; Fri, 1 May 2026 20:35:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777667744; cv=none; b=UQjRVLVX4ifYx6I8g+0NvYA+tNyMbIBpJn3d78p9yk7ZxtpL6FpFJNRrU/vvox/h9TsbsiFQt01XXc6AcnVkft9a3DJRenUJrabvdgOBsdhUmLA1ttGERcG7KxywASgosUbCtAPk/wI1m9b9gewdUTbaFo3orUgPZFtSpvjpxhs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777667744; c=relaxed/simple; bh=SAroxcNKrFSAqe7ITVNTDHFdcLEWgqlAkm56XUN81KE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bOSmFqHYGvQhfQrUYPTwrQaaLk32DHDOIdHsh9AyGwHnpyUFkjLXNWe+fP/XJ8FNJ0C6pdJ7xhm5sExkxvKNz5V/eamDUSpx1fHURB5xzPLJkpd1IMjqgqLnQPyQqbO9PSo7CvNbOpxztEmNgTef872HARLFq2ni2JZpP+kEINQ= 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=onpZAtcL; arc=none smtp.client-ip=209.85.216.73 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="onpZAtcL" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-35da97f6a6dso2877498a91.0 for ; Fri, 01 May 2026 13:35:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777667743; x=1778272543; 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=pYJswTuz+l+2YoiILOIj0xmNxa9rLIxr+BM7pEgolkw=; b=onpZAtcL8IHQbFnSemTLo3ZcEe84BtOrqVKt8V9WYoBDcZ2ixuYdsh4b4iuaog0MdA wazu4OEAQ63iLa1fOVi4ol3jFufnigWPcSGgOmeVW2/VO6NZ8+BIZNA0cvyktv/jVDf3 TFtrvEn7I5L5KYWIN/Y8hqWlU2KFELJy7CqDb/eWVh5Pz64cMHe1+oRKZ+7doylAbTiA 5i6K7RXFKm7iB2BoQtftiSuBRvJPeZ08p+Hw5bCVCp+esXafhZDZ2UgpRv7fid1QT9yK wRyzf/MsYQdDF+P6zcWhQooeO2yUD74u1VzlLw6r4D5HVBgVa/evfMguaYyaC4+c/EVL /AEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777667743; x=1778272543; 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=pYJswTuz+l+2YoiILOIj0xmNxa9rLIxr+BM7pEgolkw=; b=p7dv3A5sIN6OoRemo/2aFg5gMQCu3kqtjVA462GsdAw2bOOeNq3ROYRx00Ad/3SFTz 9j5/UZAFD4xNk/boTW6/fqMmIEbAxnNgTq7HFEA6anDqLkJF4mCJKx8ZSsrTQsx+rRuT CHIBpm1jUKeJcb+SI7qg1LMQIiIpXr/YaATa9w5j/Wp4+FtF1DUQLStag0KC9sq+5+NJ +gt6/YzltjxrfladvHeT1XcmFXLEAzbVyIgBNxAt6TJbD/a8IglheugJbjlOAPC+kE1p 6buC8TtOM6UXkL2Byp8mBy1EqYtQgvPv/I2apI6uB9MiE5+dXlFq9N6sOfaMASu9fuVY cN4w== X-Forwarded-Encrypted: i=1; AFNElJ/yOAVgOugB8CPmL20o7js5ZVhGjPHUbZiZBAJkJsAeDqx0HK/1k7njDp4sxXDrPYPx4DXUoos41rSuIqM=@vger.kernel.org X-Gm-Message-State: AOJu0YyppEpdP73nnu6Hywtz3WeTUX/eyl1hdrbq/wux7PiswHjvLckt BI7z50byMtPjQOm/y3H/0Fs20oCVbob+5d2cErx9yZrff+zFWbBvVuCqYAOjDMlPzS9iYym1dsa l1mVTrA== X-Received: from pjbdw12.prod.google.com ([2002:a17:90b:94c:b0:35f:b193:747d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e18c:b0:35d:a4c0:a0ac with SMTP id 98e67ed59e1d1-3650ccefc6cmr735595a91.3.1777667742797; Fri, 01 May 2026 13:35:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 May 2026 13:35:32 -0700 In-Reply-To: <20260501203537.2120074-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: <20260501203537.2120074-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260501203537.2120074-2-seanjc@google.com> Subject: [PATCH v2 1/6] KVM: SVM: Fix page overflow in sev_dbg_crypt() for ENCRYPT path From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ashutosh Desai Content-Type: text/plain; charset="UTF-8" From: Ashutosh Desai In sev_dbg_crypt(), the per-iteration transfer length is bounded by the source page offset (PAGE_SIZE - s_off) but not by the destination page offset (PAGE_SIZE - d_off). When d_off > s_off, the encrypt path (__sev_dbg_encrypt_user) performs a read-modify-write using a single-page intermediate buffer (dst_tpage): 1. __sev_dbg_decrypt() expands the size to round_up(len + (d_off & 15), 16) before issuing the PSP command. If len + (d_off & 15) > PAGE_SIZE, the PSP writes beyond the end of the 4096-byte dst_tpage allocation. 2. The subsequent memcpy()/copy_from_user() into page_address(dst_tpage) + (d_off & 15) of 'len' bytes overflows by up to 15 bytes under the same condition. Trigger example: s_off = 0, d_off = 1, debug.len = PAGE_SIZE - the PSP is instructed to write round_up(4097, 16) = 4112 bytes to a 4096-byte buffer. Fix by also bounding len by (PAGE_SIZE - d_off), the same check that sev_send_update_data() already performs for its single-page guest region. ================================================================== BUG: KASAN: slab-use-after-free in sev_dbg_crypt+0x993/0xd10 [kvm_amd] Write of size 4095 at addr ff110062293bb009 by task sev_dbg_test/228214 CPU: 96 UID: 0 PID: 228214 Comm: sev_dbg_test Tainted: G U W 7.0.0-smp--5ce9b0c48211-dbg #156 PREEMPTLAZY Tainted: [U]=USER, [W]=WARN Hardware name: Google Astoria/astoria, BIOS 0.20250817.1-0 08/25/2025 Call Trace: dump_stack_lvl+0x54/0x70 print_report+0xbc/0x260 kasan_report+0xa2/0xd0 kasan_check_range+0x25f/0x2c0 __asan_memcpy+0x40/0x70 sev_dbg_crypt+0x993/0xd10 [kvm_amd] sev_mem_enc_ioctl+0x33c/0x450 [kvm_amd] kvm_vm_ioctl+0x65d/0x6d0 [kvm] __se_sys_ioctl+0xb2/0x100 do_syscall_64+0xe8/0x870 entry_SYSCALL_64_after_hwframe+0x4b/0x53 The buggy address belongs to the physical page: page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x7fe72b6a0 pfn:0x62293bb memcg:ff11000112827d82 flags: 0x1400000000000000(node=1|zone=1) raw: 1400000000000000 0000000000000000 dead000000000122 0000000000000000 raw: 00000007fe72b6a0 0000000000000000 00000001ffffffff ff11000112827d82 page dumped because: kasan: bad access detected Memory state around the buggy address: ff110062293bbf00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff110062293bbf80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >ff110062293bc000: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc ^ ff110062293bc080: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc ff110062293bc100: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc ================================================================== Disabling lock debugging due to kernel taint Fixes: 24f41fb23a39 ("KVM: SVM: Add support for SEV DEBUG_DECRYPT command") Fixes: 7d1594f5d94b ("KVM: SVM: Add support for SEV DEBUG_ENCRYPT command") Cc: stable@vger.kernel.org Signed-off-by: Ashutosh Desai [sean: add sample KASAN splat, Fixes, and stable@] Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c2126b3c3072..b9d7bd868e0b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1396,6 +1396,7 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) s_off = vaddr & ~PAGE_MASK; d_off = dst_vaddr & ~PAGE_MASK; len = min_t(size_t, (PAGE_SIZE - s_off), size); + len = min_t(size_t, len, PAGE_SIZE - d_off); if (dec) ret = __sev_dbg_decrypt_user(kvm, -- 2.54.0.545.g6539524ca2-goog