Received: by 10.213.65.68 with SMTP id h4csp275507imn; Wed, 21 Mar 2018 18:39:00 -0700 (PDT) X-Google-Smtp-Source: AG47ELu7whuQogUaYU3tPDEZ8POuHvON9Px6JgDplCS9yGUoTt8Xevm+u8YeZ0baZh76x+cpSwhj X-Received: by 2002:a17:902:ac1:: with SMTP id 59-v6mr22880210plp.228.1521682740000; Wed, 21 Mar 2018 18:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521682739; cv=none; d=google.com; s=arc-20160816; b=Lg5dlwYjPY2Owg5N/xecXe5PuZzqXHq5pjvGbEzAZ1rJK2PJcaKN1/nTOxEnR0NA5t VrwdYN4hReOlThlDnpvivdt8DxbGlUlWjENXjU5MxdDfHnworIfIhxLUWshYAGj9kbij e/3aSKD9yu82rl5nDQtZuahI/LX5LGYHkxcIY2ZwuEB+FSiqV+nQgnK+Er5tewc8zq3y aqmPcSn4w6iVeydoIN/GCQ3J0c9FLHYcryBCcNJwISwlaY3WpVIQ4k56wfRQ4LT/tjYP 7WD4Z0SaZaRAimVDosWrmri5yrdAE262jfQxZB0IuU1HQguy5D5bbxeX9m7a4G3VbN7K zqQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dmarc-filter:arc-authentication-results; bh=ikg2sHnXFMIc++yunr11w9wq/NCqAKhmxcG96zgZXlY=; b=N+Rkn6VyUehFY1uuRXx7wQ/1LtIcMUXtqy/0DLfnKSQcDCQxWemlZuYAIjWxo7I84j qiqmJOEHtpO3aCn0Fm2favAb+5L/8qk407qAG5Hl094ud1bUDPLGFs1eXjCh7fNFM6se hQKduXeqk9PHF5Ejds3S9iGD/dDzCdGnEpVn4tgAza5zLaqY28BPYoyEr6pHM148UWIa ZVmPCi1qVKlHzxcwsUr/oRSbdF8cjdzmHbck5R6Ikz2yoE7jHlZVWSfybxOQeZImqP5k 5a+LnJeQ4V7gAJrsdXu8eimVKg9LPn1GjwczkE4jlZgxqdPXvCjTKnuW+E3USZxjrPPX 0TEw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z62si3613851pgd.819.2018.03.21.18.38.45; Wed, 21 Mar 2018 18:38:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753342AbeCVBhz convert rfc822-to-8bit (ORCPT + 99 others); Wed, 21 Mar 2018 21:37:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:52542 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751604AbeCVBhx (ORCPT ); Wed, 21 Mar 2018 21:37:53 -0400 Received: from mail-io0-f177.google.com (mail-io0-f177.google.com [209.85.223.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E58B42183A for ; Thu, 22 Mar 2018 01:37:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E58B42183A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=luto@kernel.org Received: by mail-io0-f177.google.com with SMTP id g14so8947442iob.13 for ; Wed, 21 Mar 2018 18:37:52 -0700 (PDT) X-Gm-Message-State: AElRT7FsXmtljVzsbK/lPEO8NliNFEzSjnukRmri8mP94/4MzVGiBlAe lQ6NDk1ZjhMyb/wNaXpsUA7QsvzJ8Il10msWytQYQQ== X-Received: by 10.107.40.73 with SMTP id o70mr23050255ioo.6.1521682672217; Wed, 21 Mar 2018 18:37:52 -0700 (PDT) MIME-Version: 1.0 Received: by 10.2.137.70 with HTTP; Wed, 21 Mar 2018 18:37:31 -0700 (PDT) In-Reply-To: References: <1521481767-22113-1-git-send-email-chang.seok.bae@intel.com> <1521481767-22113-14-git-send-email-chang.seok.bae@intel.com> From: Andy Lutomirski Date: Thu, 22 Mar 2018 01:37:31 +0000 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 13/15] x86/fsgsbase/64: With FSGSBASE, compare GS bases on paranoid_entry To: "H. Peter Anvin" Cc: Andy Lutomirski , "Chang S. Bae" , X86 ML , Andi Kleen , "Metzger, Markus T" , Tony Luck , "Ravi V. Shankar" , LKML , Dave Hansen Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 21, 2018 at 10:03 PM, H. Peter Anvin wrote: > On 03/20/18 07:58, Andy Lutomirski wrote: >>> On Mar 19, 2018, at 10:49 AM, Chang S. Bae wrote: >>> >>> When FSGSBASE is enabled, SWAPGS needs if and only if (current) >>> GS base is not the kernel's. >>> >>> FSGSBASE instructions allow user to write any value on GS base; >>> even negative. Sign check on the current GS base is not >>> sufficient. Fortunately, reading GS base is fast. Kernel GS >>> base is also known from the offset table with the CPU number. >> >> The original version of these patches (mine and Andi’s) didn’t have >> this comparison, didn’t need RDMSR, and didn’t allow malicious user >> programs to cause the kernel to run decently large chunks of code with >> the reverse of the expected GS convention. Why did you change it? >> >> I really really don't like having a corner case like this that can and >> will be triggered by malicious user code but that is hard to write a >> self-test for because it involves guessing a 64-bit magic number. >> Untestable corner cases in the x86 entry code are bad. >> > > What corner case are you talking about? > > If user GS_BASE and kernel GS_BASE happen to be identical, then SWAPGS > is a nop and it does not matter one iota which is is user space and > which is kernel space. They are just numbers, and swapping one number > with itself doesn't do anything (in fact, opcode 0x90 was used for NOP > because it aliased to xchg [e]ax,[e]ax on pre-64-bit hardware.) > On current kernels, MSR_GS_BASE points to kernel percpu data and MSR_KERNEL_GS_BASE is the user's GSBASE value. If you *write* to MSR_KERNEL_GS_BASE, you modify the user's value. With Andi's/my patches, it works exactly the same way on !FSGSBASE and, if FSGSBASE is on, then, when we're in a paranoid entry, MSR_GS_BASE is the live kernel value, the value upon return is stashed in an entry asm register, and MSR_KERNEL_GS_BASE is whatever it was before we entered. Sure, it's more complicated, but if something starts writing to MSR_KERNEL_GS_BASE in the context of a paranoid entry, the behavior will at least be consistently screwy. With these patches, MSR_GS_BASE points to the kernel percpu data and MSR_KERNEL_GS_BASE is the user's value, but writing to MSR_KERNEL_GS_BASE will change the *kernel* value if we happen to be in a paranoid context while running malicious user code. But only when running malicious user code. In the absence of some compelling reason why #3 is better than #2, I don't like it. If you want to argue for using rdpid or lsl to find the kernel gs base and then load it unconditionally with wrgsbase, I'd be fine with that. But this compare-and-swapgs just seems unnecessarily subject to manipulation.