Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp100887iof; Sun, 5 Jun 2022 22:23:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyt0aeCwxp0cU3vJAXhttYGhZxw6Cxh9vIu5eEk8/44QinHLDVabv3tJOgnqZBjGcbs9vmY X-Received: by 2002:a63:4964:0:b0:3fc:a87a:bd8d with SMTP id y36-20020a634964000000b003fca87abd8dmr19813818pgk.257.1654493014582; Sun, 05 Jun 2022 22:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654493014; cv=none; d=google.com; s=arc-20160816; b=MZHB27+ekbGD7sIvEovq7/2A/mScxew69Yebw7CQ9K4AwE94nF+2VyITj5vqH6a/r5 l+Bw84YVD755Qv6cXQsy1fdTkKWZTHqvOfvERs6mLAP1GgrnAfRHsC26/LolYgiJ/skJ oWRqRUn3rRr2CCAVm68bIQWoucKX0mZC8nnFpK2LP5CgbTDw5msnEollJYT0NbhucDzm 0OFoq+huQzII1DFV+SrTFlOGAqjTAdk9BKV67cmO1TYUe62C+U08mL3nU4/hF8ICq1lV NrHIZPeW+4e+7NLQlLgAz2yxfeyeB0ITGUQLzZhjVtxWlCCEYJsJ+wdvolBPRW3h0hZU bJOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wh5rcTmXkfEupUkD4nE+WvWW/nFH3nQVtF26U1hcb8o=; b=TgutYyfTw59QWGa7D4pytqnvTIGEtCmDLK+20FdeDL5VbE08JmpMyCGgEAw3svc6qC 6Sh1cmovjVLE3OapnfDJErxf9Yv9h6gdNJhLuErkTG5BMItwhL9VgjnIjGwF8XZiijOH vGnIAFh1S4lbOADdoIBpvAbCNURRpG4j7il8os8NyY56xNaXNPZG2NYUAkwaUp/PtUtJ 35U6AuP6KxiFqA2ZoVFglqfvBx2tP3OrLqBx/RWVqU5xTWZheJyMblwkQu+918qU3Jov 9x7f3WRT+4N8QVDqUvLMiUtyCRuVovCFDADn07WhV8EA90HKMywT49/TqgMtNkcxImz1 01/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gxtxVgro; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l8-20020a056a00140800b00510a30ef4c6si21814467pfu.370.2022.06.05.22.23.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:23:34 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gxtxVgro; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4A5871A29D7; Sun, 5 Jun 2022 21:26:04 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346868AbiFCSO4 (ORCPT + 99 others); Fri, 3 Jun 2022 14:14:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346581AbiFCSA0 (ORCPT ); Fri, 3 Jun 2022 14:00:26 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 154B55908E; Fri, 3 Jun 2022 10:56:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 4834BB82369; Fri, 3 Jun 2022 17:56:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FC97C385A9; Fri, 3 Jun 2022 17:56:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278997; bh=lNOmLlFQuyuci5IwSKVo0uhTC3PXQ5cfKd6PwOA3j3o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gxtxVgro5XsC1ADaMS7hAUJVUYi12BcG7o0HIWiMPVQKXHdvXoEnhmm3mwJt5eyU1 rQg4FfVJyIzx/I5gfGa2kekbfZzd53+emrECgS0ND7sT1AG5rBSrlSjkVWYIHOOs7N 2MLwO71322nSpHWo6vHAQ2ZjQNRc+jJHnrcfx+2E= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tadeusz Struk , syzbot+6cde2282daa792c49ab8@syzkaller.appspotmail.com, Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.18 20/67] KVM: x86: Use __try_cmpxchg_user() to update guest PTE A/D bits Date: Fri, 3 Jun 2022 19:43:21 +0200 Message-Id: <20220603173821.309910746@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173820.731531504@linuxfoundation.org> References: <20220603173820.731531504@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit f122dfe4476890d60b8c679128cd2259ec96a24c upstream. Use the recently introduced __try_cmpxchg_user() to update guest PTE A/D bits instead of mapping the PTE into kernel address space. The VM_PFNMAP path is broken as it assumes that vm_pgoff is the base pfn of the mapped VMA range, which is conceptually wrong as vm_pgoff is the offset relative to the file and has nothing to do with the pfn. The horrific hack worked for the original use case (backing guest memory with /dev/mem), but leads to accessing "random" pfns for pretty much any other VM_PFNMAP case. Fixes: bd53cb35a3e9 ("X86/KVM: Handle PFNs outside of kernel reach when touching GPTEs") Debugged-by: Tadeusz Struk Tested-by: Tadeusz Struk Reported-by: syzbot+6cde2282daa792c49ab8@syzkaller.appspotmail.com Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Message-Id: <20220202004945.2540433-4-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/mmu/paging_tmpl.h | 38 +------------------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -144,42 +144,6 @@ static bool FNAME(is_rsvd_bits_set)(stru FNAME(is_bad_mt_xwr)(&mmu->guest_rsvd_check, gpte); } -static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, - pt_element_t __user *ptep_user, unsigned index, - pt_element_t orig_pte, pt_element_t new_pte) -{ - signed char r; - - if (!user_access_begin(ptep_user, sizeof(pt_element_t))) - return -EFAULT; - -#ifdef CMPXCHG - asm volatile("1:" LOCK_PREFIX CMPXCHG " %[new], %[ptr]\n" - "setnz %b[r]\n" - "2:" - _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_EFAULT_REG, %k[r]) - : [ptr] "+m" (*ptep_user), - [old] "+a" (orig_pte), - [r] "=q" (r) - : [new] "r" (new_pte) - : "memory"); -#else - asm volatile("1:" LOCK_PREFIX "cmpxchg8b %[ptr]\n" - "setnz %b[r]\n" - "2:" - _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_EFAULT_REG, %k[r]) - : [ptr] "+m" (*ptep_user), - [old] "+A" (orig_pte), - [r] "=q" (r) - : [new_lo] "b" ((u32)new_pte), - [new_hi] "c" ((u32)(new_pte >> 32)) - : "memory"); -#endif - - user_access_end(); - return r; -} - static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, u64 *spte, u64 gpte) @@ -278,7 +242,7 @@ static int FNAME(update_accessed_dirty_b if (unlikely(!walker->pte_writable[level - 1])) continue; - ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index, orig_pte, pte); + ret = __try_cmpxchg_user(ptep_user, &orig_pte, pte, fault); if (ret) return ret;