From patchwork Thu Jul 17 18:22:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Lutomirski X-Patchwork-Id: 4578081 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5DC559F26C for ; Thu, 17 Jul 2014 18:23:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7599C201CD for ; Thu, 17 Jul 2014 18:23:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 31F02201C0 for ; Thu, 17 Jul 2014 18:23:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755296AbaGQSWw (ORCPT ); Thu, 17 Jul 2014 14:22:52 -0400 Received: from mail-pd0-f173.google.com ([209.85.192.173]:51407 "EHLO mail-pd0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753179AbaGQSWs (ORCPT ); Thu, 17 Jul 2014 14:22:48 -0400 Received: by mail-pd0-f173.google.com with SMTP id w10so3600298pde.32 for ; Thu, 17 Jul 2014 11:22:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=P2xrjgtrt4b0uhXsFm9fmU1psBBias+4GuZXKnQUBe8=; b=DqBC1D98XrTQmZdnCV1ySqUS5QCrbrsYzzbedc6CbcvLEnILiy3y4FEmrfOwmEi6Y2 dpQ/acvjciMHr6Nd8/zCkNXRXO4cxpusfrZ6grBe8VRl86F3nhbQiV/wuagVxmyXBjPP Iq2LiMMkSL4jbCAMZl13t2jfPSZ73s+pqHGzqtpbrhhTwsaq6xQ+XQajp+nzvjemqY1t Nr4AdSJJ3Z28hkBkD1WhqOs1zdyMVk9Z/okF6JFswZzaFQ1yBK6rOS3mqzdxOYJpVtOQ LTQPq6xfE9Ck8jc5zPGrZWN7GQq0sjtfbSw/3k96rhxpoRtqsch5sQNg2+AKJFnZGRqd SkMQ== X-Gm-Message-State: ALoCoQkAp1HV2rcF/v5LVuUfXtjx6cUK78T+JnZDTxyBP6YFYf1LcFNcuJO9Z5km1obx0n2NB7sj X-Received: by 10.70.41.110 with SMTP id e14mr39051149pdl.15.1405621368122; Thu, 17 Jul 2014 11:22:48 -0700 (PDT) Received: from localhost ([2001:5a8:4:83c0:fccf:79b7:dc5b:f307]) by mx.google.com with ESMTPSA id a12sm3131944pdm.56.2014.07.17.11.22.45 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Jul 2014 11:22:46 -0700 (PDT) From: Andy Lutomirski To: kvm@vger.kernel.org, "H. Peter Anvin" , Theodore Ts'o , linux-kernel@vger.kernel.org, Kees Cook , x86@kernel.org Cc: Daniel Borkmann , Srivatsa Vaddagiri , Raghavendra K T , Gleb Natapov , Paolo Bonzini , bsd@redhat.com, Andrew Honig , Andy Lutomirski Subject: [PATCH v4 5/5] x86, kaslr: Use MSR_KVM_GET_RNG_SEED for KASLR if available Date: Thu, 17 Jul 2014 11:22:20 -0700 Message-Id: <4454941956362c58e676610d3bf489f58c689d46.1405620944.git.luto@amacapital.net> X-Mailer: git-send-email 1.9.3 In-Reply-To: References: In-Reply-To: References: Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It's considerably better than any of the alternatives on KVM. Rather than reinventing all of the cpu feature query code, this fixes native_cpuid to work in PIC objects. I haven't combined it with boot/cpuflags.c's cpuid implementation: including asm/processor.h from boot/cpuflags.c results in a flood of unrelated errors, and fixing it might be messy. Signed-off-by: Andy Lutomirski Reviewed-by: Kees Cook --- arch/x86/boot/compressed/aslr.c | 27 +++++++++++++++++++++++++++ arch/x86/include/asm/processor.h | 21 ++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/aslr.c b/arch/x86/boot/compressed/aslr.c index fc6091a..8583f0e 100644 --- a/arch/x86/boot/compressed/aslr.c +++ b/arch/x86/boot/compressed/aslr.c @@ -5,6 +5,8 @@ #include #include +#include + #include #include #include @@ -15,6 +17,22 @@ static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION; +static bool kvm_para_has_feature(unsigned int feature) +{ + u32 kvm_base; + u32 features; + + if (!has_cpuflag(X86_FEATURE_HYPERVISOR)) + return false; + + kvm_base = hypervisor_cpuid_base("KVMKVMKVM\0\0\0", KVM_CPUID_FEATURES); + if (!kvm_base) + return false; + + features = cpuid_eax(kvm_base | KVM_CPUID_FEATURES); + return features & (1UL << feature); +} + #define I8254_PORT_CONTROL 0x43 #define I8254_PORT_COUNTER0 0x40 #define I8254_CMD_READBACK 0xC0 @@ -81,6 +99,15 @@ static unsigned long get_random_long(void) } } + if (kvm_para_has_feature(KVM_FEATURE_GET_RNG_SEED)) { + u64 seed; + + debug_putstr(" MSR_KVM_GET_RNG_SEED"); + rdmsrl(MSR_KVM_GET_RNG_SEED, seed); + random ^= (unsigned long)seed; + use_i8254 = false; + } + if (has_cpuflag(X86_FEATURE_TSC)) { debug_putstr(" RDTSC"); rdtscll(raw); diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index a4ea023..6096f3c 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -189,10 +189,25 @@ static inline int have_cpuid_p(void) static inline void native_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) { - /* ecx is often an input as well as an output. */ - asm volatile("cpuid" + /* + * This function can be used from the boot code, so it needs + * to avoid using EBX in constraints in PIC mode. + * + * ecx is often an input as well as an output. + */ + asm volatile(".ifnc %%ebx,%1 ; .ifnc %%rbx,%1 \n\t" + "movl %%ebx,%1 \n\t" + ".endif ; .endif \n\t" + "cpuid \n\t" + ".ifnc %%ebx,%1 ; .ifnc %%rbx,%1 \n\t" + "xchgl %%ebx,%1 \n\t" + ".endif ; .endif" : "=a" (*eax), - "=b" (*ebx), +#if defined(__i386__) && defined(__PIC__) + "=r" (*ebx), /* gcc won't let us use ebx */ +#else + "=b" (*ebx), /* ebx is okay */ +#endif "=c" (*ecx), "=d" (*edx) : "0" (*eax), "2" (*ecx)