From patchwork Fri Sep 27 10:14:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juerg Haefliger X-Patchwork-Id: 1990211 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XFRD81W6Wz1xtK for ; Fri, 27 Sep 2024 20:15:03 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1su7zy-0003gr-O3; Fri, 27 Sep 2024 10:14:54 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1su7zw-0003g2-TG for kernel-team@lists.ubuntu.com; Fri, 27 Sep 2024 10:14:52 +0000 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (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 smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 7B5BE3F135 for ; Fri, 27 Sep 2024 10:14:49 +0000 (UTC) Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-42cb89fbb8bso11572785e9.3 for ; Fri, 27 Sep 2024 03:14:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727432088; x=1728036888; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nsp/BkMcDGSmf+vGleTffOEvDVG4H+4H5mhZaVoC1Ak=; b=qulGEqMGzmII+tx9CJcpz+XFwpP0+D13PrUZw1klt1wTWKwnAcuIop7aUR8/ax4n6Y fiZyWnzjFXUETQ5fUtGqcXTgT45hllQhKWQg06mMBxVchb88Oy9IigTU592a7lGk4dMx EPbz6wB58HHfsjLrJwVJu5VuTZVyk2PNT0kzluXyrqQLnUjXPQxFhZ+GM2IFAwTzA9Mi g4ygBfsQEeTUF3V4Pj6B6+4+e0ZcGtvin99XLtZ17TmQjEeOFIoBfsf7xWaZdX7dsDVl DKATZNpSnXK0pZWRfcR5xupZLJwB4i3qLs742pRXr8DDx5uPnEKu3kEJGT7OGAixrt4i 9V6Q== X-Gm-Message-State: AOJu0YyojWr6EM2rDHeG+0OTkcJMZ6PxRHCgefR+8rt2+q70Yd3Y3MRh 5xvRKejeKuVsNcl3KIOY2sdMVLKMnn2o7HHCzMPg6+Fe+/NhWivZv4YgGDeWP/09kSFiutoG9Jm IdPfxl2sNyaI6zv/k9skygjJpVxr52Us0ZZVLNVCdGJHuE3ta02Jx+b0ILTHbUxBpUH+KWIJGjs Dq1kir6dfRrQ== X-Received: by 2002:a05:600c:4747:b0:426:6379:3b4f with SMTP id 5b1f17b1804b1-42f5849092dmr18790865e9.31.1727432087899; Fri, 27 Sep 2024 03:14:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKXNMq6qFAWM/7mqio1UBBtDVL1GCC0aMeRfTRUMsyD4n5ixjpsZy8J7zQFa/HODibNvOIpw== X-Received: by 2002:a05:600c:4747:b0:426:6379:3b4f with SMTP id 5b1f17b1804b1-42f5849092dmr18790615e9.31.1727432087422; Fri, 27 Sep 2024 03:14:47 -0700 (PDT) Received: from localhost ([81.221.247.52]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42e969ddadcsm69803455e9.7.2024.09.27.03.14.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 03:14:46 -0700 (PDT) From: Juerg Haefliger To: kernel-team@lists.ubuntu.com Subject: [SRU][F][PATCH 1/1] KVM: x86/mmu: make apf token non-zero to fix bug Date: Fri, 27 Sep 2024 12:14:43 +0200 Message-ID: <7f795a27ba199316d0e5e1bb843540a39301c6e5.1727431769.git.juerg.haefliger@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Liang Zhang In current async pagefault logic, when a page is ready, KVM relies on kvm_arch_can_dequeue_async_page_present() to determine whether to deliver a READY event to the Guest. This function test token value of struct kvm_vcpu_pv_apf_data, which must be reset to zero by Guest kernel when a READY event is finished by Guest. If value is zero meaning that a READY event is done, so the KVM can deliver another. But the kvm_arch_setup_async_pf() may produce a valid token with zero value, which is confused with previous mention and may lead the loss of this READY event. This bug may cause task blocked forever in Guest: INFO: task stress:7532 blocked for more than 1254 seconds. Not tainted 5.10.0 #16 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. task:stress state:D stack: 0 pid: 7532 ppid: 1409 flags:0x00000080 Call Trace: __schedule+0x1e7/0x650 schedule+0x46/0xb0 kvm_async_pf_task_wait_schedule+0xad/0xe0 ? exit_to_user_mode_prepare+0x60/0x70 __kvm_handle_async_pf+0x4f/0xb0 ? asm_exc_page_fault+0x8/0x30 exc_page_fault+0x6f/0x110 ? asm_exc_page_fault+0x8/0x30 asm_exc_page_fault+0x1e/0x30 RIP: 0033:0x402d00 RSP: 002b:00007ffd31912500 EFLAGS: 00010206 RAX: 0000000000071000 RBX: ffffffffffffffff RCX: 00000000021a32b0 RDX: 000000000007d011 RSI: 000000000007d000 RDI: 00000000021262b0 RBP: 00000000021262b0 R08: 0000000000000003 R09: 0000000000000086 R10: 00000000000000eb R11: 00007fefbdf2baa0 R12: 0000000000000000 R13: 0000000000000002 R14: 000000000007d000 R15: 0000000000001000 Signed-off-by: Liang Zhang Message-Id: <20220222031239.1076682-1-zhangliang5@huawei.com> Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini (backported from commit 6f3c1fc53d86d580d8d6d749c4af23705e4f6f79) [juergh: Adjusted context.] CVE-2022-48943 Signed-off-by: Juerg Haefliger --- arch/x86/kvm/mmu.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 305381a8f101..8370a81bc771 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -4183,12 +4183,23 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, error_code, gfn, prefault); } +static u32 alloc_apf_token(struct kvm_vcpu *vcpu) +{ + /* make sure the token value is not 0 */ + u32 id = vcpu->arch.apf.id; + + if (id << 12 == 0) + vcpu->arch.apf.id = 1; + + return (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; +} + static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, gfn_t gfn) { struct kvm_arch_async_pf arch; - arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; + arch.token = alloc_apf_token(vcpu); arch.gfn = gfn; arch.direct_map = vcpu->arch.mmu->direct_map; arch.cr3 = vcpu->arch.mmu->get_cr3(vcpu);