Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A142EC433E2 for ; Mon, 31 Aug 2020 11:19:31 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 722A02068E for ; Mon, 31 Aug 2020 11:19:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="Cb4tNrNZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 722A02068E Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kChpt-0007tL-LG; Mon, 31 Aug 2020 11:18:53 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kChps-0007tG-NM for xen-devel@lists.xenproject.org; Mon, 31 Aug 2020 11:18:52 +0000 X-Inumbo-ID: 531aaf37-2d4b-4068-836d-ca499a889f76 Received: from esa5.hc3370-68.iphmx.com (unknown [216.71.155.168]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 531aaf37-2d4b-4068-836d-ca499a889f76; Mon, 31 Aug 2020 11:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1598872731; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=e99vmX6iMfIx7xndNdCIeZco3G9FNaD/HMx64qOrfMs=; b=Cb4tNrNZOox4u6OAc6WllA/50XpeFgKKSeBsg1IsF5wjk/Z2vI9xzB5k bWaKrHDLd4G/81f1NOuiojBKc/FrYglN/4Q+SCzlhVDlPQmyRVmmT2L/L x2vNc7cwZUneKZVyU3JfHHrD9GmVLjkIAtJdhJsjnmgDYEqNTqc8DaKYk o=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: xTfKJcjBTZu+2/wGL/MeG3Q3akHG1Br7YDSx0IxHhLtwiWMbrez1ed/T6+9E1QuzaqkILML7Zx ni/bo+DVbOcUwalzKEcbcZr2Z/TVXnvrMuEdtiLPcLrZUBsfv3LLNAf1i+ip1QW1iezkNY6rer 7RwD0IGALjQaqpachLdJ2S5tpJ/uuhDnckOZ1z2ZdxeIVe/Q6pVWOSaQ7KHoyo6cv5j/cofpMM tkue37FzIPHxi4nmdUE9p0Hu6To5rhpstc7+Qz/L5YW6U444d80OlPCMBKmafU5Bzo1RynpHVx Mvc= X-SBRS: 2.7 X-MesageID: 25770395 X-Ironport-Server: esa5.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.76,375,1592884800"; d="scan'208";a="25770395" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Andy Lutomirski Subject: [PATCH] x86/pv: Fix multiple bugs with SEGBASE_GS_USER_SEL Date: Mon, 31 Aug 2020 12:18:32 +0100 Message-ID: <20200831111832.25275-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" The logic takes the segment selector unmodified from guest context. This allowed the guest to load DPL0 descriptors into %gs. Fix up the RPL for non-NUL selectors to be 3. Xen's context switch logic skips saving the inactive %gs base, as it cannot be modified by the guest behind Xen's back. This depends on Xen caching updates to the inactive base, which is was missing from this path. The consequence is that, following SEGBASE_GS_USER_SEL, the next context switch will restore the stale inactive %gs base, and corrupt vcpu state. Rework the hypercall to update the cached idea of gs_base_user, and fix the behaviour in the case of the AMD NUL selector bug to always zero the segment base. Reported-by: Andy Lutomirski Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Andy Lutomirski --- xen/arch/x86/x86_64/mm.c | 56 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index 29048d34dc..95ee05cd5d 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -1059,16 +1059,52 @@ long do_set_segment_base(unsigned int which, unsigned long base) break; case SEGBASE_GS_USER_SEL: - __asm__ __volatile__ ( - " swapgs \n" - "1: movl %k0,%%gs \n" - " "safe_swapgs" \n" - ".section .fixup,\"ax\" \n" - "2: xorl %k0,%k0 \n" - " jmp 1b \n" - ".previous \n" - _ASM_EXTABLE(1b, 2b) - : "+r" (base) ); + /* + * We wish to update the user %gs from the GDT/LDT. Currently, the + * guest kernel's GS_BASE is in context. + */ + asm volatile ( "swapgs" ); + + if ( base <= 3 ) + { + /* Work around NUL segment behaviour on AMD hardware. */ + if ( boot_cpu_data.x86_vendor & + (X86_VENDOR_AMD | X86_VENDOR_HYGON) ) + asm volatile ( "mov %[sel], %%gs" + :: [sel] "rm" (FLAT_USER_DS32) ); + } + else + /* Fix up RPL. */ + base |= 3; + + /* + * Load the chosen selector, with fault handling. + * + * Errors ought to fail the hypercall, but that was never built in + * originally, and Linux will BUG() if this call fails. + * + * NUL the selector in the case of an error. This too needs to deal + * with the AMD NUL segment behaviour, but it is already a slowpath in + * #GP context so perform the flat load unconditionally to avoid + * complicated logic. + * + * Anyone wanting to check for errors from this hypercall should + * re-read %gs and compare against the input 'base' selector. + */ + asm volatile ( "1: mov %k[sel], %%gs\n\t" + ".section .fixup, \"ax\", @progbits\n\t" + "2: mov %k[flat], %%gs\n\t" + " xor %k[sel], %k[sel]\n\t" + " jmp 1b\n\t" + ".previous\n\t" + _ASM_EXTABLE(1b, 2b) + : [sel] "+r" (base) + : [flat] "rm" (FLAT_USER_DS32) ); + + /* Update the cache of the inactive base, as read from the GDT/LDT. */ + v->arch.pv.gs_base_user = rdgsbase(); + + asm volatile ( safe_swapgs ); break; default: -- 2.11.0