Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752851AbcKUH1Z (ORCPT ); Mon, 21 Nov 2016 02:27:25 -0500 Received: from mail-wj0-f194.google.com ([209.85.210.194]:34389 "EHLO mail-wj0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750944AbcKUH1X (ORCPT ); Mon, 21 Nov 2016 02:27:23 -0500 X-Greylist: delayed 817 seconds by postgrey-1.27 at vger.kernel.org; Mon, 21 Nov 2016 02:27:23 EST Date: Mon, 21 Nov 2016 08:13:42 +0100 From: Ingo Molnar To: Andy Lutomirski Cc: Brian Gerst , Andy Lutomirski , tedheadster@gmail.com, Linus Torvalds , "H. Peter Anvin" , George Spelvin , "linux-kernel@vger.kernel.org" , X86 ML Subject: Re: What exactly do 32-bit x86 exceptions push on the stack in the CS slot? Message-ID: <20161121071342.GA16999@gmail.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2669 Lines: 62 * Andy Lutomirski wrote: > On Sat, Nov 19, 2016 at 6:11 PM, Brian Gerst wrote: > > On Sat, Nov 19, 2016 at 8:52 PM, Andy Lutomirski wrote: > >> This is a question for the old-timers here, since I can't find > >> anything resembling an answer in the SDM. > >> > >> Suppose an exception happens (#UD in this case, but I assume it > >> doesn't really matter). We're not in long mode, and the IDT is set up > >> to deliver to a normal 32-bit kernel code segment. We're running in > >> that very same code segment when the exception hits, so no CPL change > >> occurs and the TSS doesn't particularly matter. > >> > >> The CPU will push EFLAGS, CS, and RIP. Here's the question: what > >> happens to the high word of CS on the stack? > >> > >> The SDM appears to say nothing at all about this. Modern systems > >> (e.g. my laptop running in 32-bit legacy mode under KVM) appear to > >> zero-extend CS. But Matthew's 486DX appears to put garbage in the > >> high bits (or maybe just leave whatever was already on the stack in > >> place). > >> > >> Do any of you happen to know what's going on and when the behavior > >> changed? I'd like to know just how big of a problem this is. Because > >> if lots of CPUs work like Matthew's, we have lots of subtle bugs on > >> them. > >> > >> --Andy > > > > This came up a while back, and we was determined that we can't assume > > zero-extension in 32-bit mode because older processors only do a > > 16-bit write even on a 32-bit push. So all segments have to be > > treated as 16-bit values, or we have to explicitly zero-extend them. > > > > All 64-bit capable processors do zero-extend segments, even in 32-bit mode. > > This almost makes me want to change the definition of pt_regs on > 32-bit rather than fixing all the entry code. So I have applied your fix that addresses the worst fallout directly: fc0e81b2bea0 x86/traps: Ignore high word of regs->cs in early_fixup_exception() ... but otherwise we might be better off zeroing out the high bits of segment registers stored on the stack, in all entry code pathways - maybe using a single function and conditional on