Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4166233ybl; Mon, 9 Dec 2019 06:32:29 -0800 (PST) X-Google-Smtp-Source: APXvYqzpJSxl3VqlDdymRlCoUPJBaA1+H/ox6mQQLc/JiViiptNRicfguQU41hRrg4ylXraSf9+6 X-Received: by 2002:a05:6830:1e75:: with SMTP id m21mr20916361otr.36.1575901949477; Mon, 09 Dec 2019 06:32:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575901949; cv=none; d=google.com; s=arc-20160816; b=kt738zx92RODJTU9jeN4FiQtkEhzxTTlu9pIhcYK7KTf/tTdbKK0ZH2WN63z7IVnEs kJEYCkxzc4O7sissvimriqDkqYWaDcf6RJ8MBx//QWd+fRJTAi692wtNoPfG7EXEe6rB keo+Ypmkhx9iRB0ZWU+0GDJ+G5Kq40dk6Yh1uLI0n7sXP0/KBDAeJiG+6W30hFi96apQ K4n6PCS7vwsemAhuO5ltdkwWiHzhD7msckXucZUdSVdzn8Ns5iXxgVaFCth2CYYsxP+h dVj6D6h5oizDaOvd/YTlzWe3JTG0qa58E0/do7/v/LFnVjq9ESVoTzLN+wlMDCykUVOR QWtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=doQeIN/U+MYu/QD7wqRZp73qNYpHVGdRJwhLC3Y8YdU=; b=JkPCDXbFGdjDF1Xtf+gm2XGYWJTBxekyZa1WVkhYsufz35a/C5dvATxDAXXUBSdAFG 2PpoaRz27FFaBgGwyD97ODSjORo7kSo5bh6cNIXLqc4yfui6Xt6f3TEg+F7KiCfTxPX3 HtYEgl5MjWWNnbhGx3zhvvYn1I30WhZE1yfkF0DVhfVCEcgtZAg2sHeiqkUSgmVu889D f1ju7ydzALSpSJHN49vMjjZXZj15HmxOeQNw9EP7XMk3tc6CAUh7Li0q99KHx8uw6tbC YKAy5D6A9ct5OEF+wWBbGI0AZHJmaCETWFPak5lwQQL5GkG0tB+GuzjbLnYMeFRMlbJK ELCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=l6KcQXNZ; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d13si12340333otp.196.2019.12.09.06.32.17; Mon, 09 Dec 2019 06:32:29 -0800 (PST) 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; dkim=pass header.i=@google.com header.s=20161025 header.b=l6KcQXNZ; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727575AbfLIObr (ORCPT + 99 others); Mon, 9 Dec 2019 09:31:47 -0500 Received: from mail-yw1-f74.google.com ([209.85.161.74]:38709 "EHLO mail-yw1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727038AbfLIObr (ORCPT ); Mon, 9 Dec 2019 09:31:47 -0500 Received: by mail-yw1-f74.google.com with SMTP id 16so11942654ywz.5 for ; Mon, 09 Dec 2019 06:31:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=doQeIN/U+MYu/QD7wqRZp73qNYpHVGdRJwhLC3Y8YdU=; b=l6KcQXNZdQrTJ4jt03R9PLZQsR6z9pOMWHVbbnN5LWKv4ZH+4/SCcBFTPT/ezh5xfg SbxD6vvn3PpYIMKcTJdqVFBze5PC373ztHhwKzzrrJ0Gkz9k4yhme233zPq3nuFGBXUP sz7A5dAo59XswLRwMPRHMn/N6enjv2JbUbiwrh3fQZGkGth/v7LH0vF149MRXSyg4qqt D6UgBOTRnI4mpwftOgR1Gff5p7gzoG8FKcnWB7nUhmMWFx5nYyEiKfSZgn/WKW+TrYFr ZitoFqsKAy7P7gxc9TB9V/WMWora+KHN7IbbH36p5saeQlANViydZBscyHxR7v62Nobl NtBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=doQeIN/U+MYu/QD7wqRZp73qNYpHVGdRJwhLC3Y8YdU=; b=ZlBzk0T3NDkxHZDilCbHL8J1lFn8lFuU5fjhT9+/niWwT8FPymUMIt8vkpA3/tmg9v 88W8iiCTMlVEPdeHzKJ8wVvrb2njeQAyEE3bCwS9y8sRA87pWqp6ymUOz2OCn+IXuV2J IxP705XrkaeHQjQ3hUBuAGWi6VxwdtXCFAtQ28Ob7TBjfwLj8tTLK3g+HjJ1AkaEZ4bq XLOyjmwo5+r21MZixYcs3vy5nFMgvPVkvSItmUFQy3jJ3pR1q073EAV1KrqGR0wGe04/ ZicSuGMQMvmXilrwzIniiOG6hCckqRWiWZ7EHQy9ElsWL1uYp1NZNh+1VZslfWwi+/cP BXRQ== X-Gm-Message-State: APjAAAXyq+O+vWiEG7hgs0JPqUcwqtDigbboFy1gZp/I9g8JsXBmhZ9C pakdvB7mnJDtPNi7vbl9MhNwLKBPog== X-Received: by 2002:a0d:f003:: with SMTP id z3mr19482463ywe.391.1575901905562; Mon, 09 Dec 2019 06:31:45 -0800 (PST) Date: Mon, 9 Dec 2019 15:31:17 +0100 Message-Id: <20191209143120.60100-1-jannh@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.24.0.393.g34dc348eaf-goog Subject: [PATCH v6 1/4] x86/insn-eval: Add support for 64-bit kernel mode From: Jann Horn To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , kasan-dev@googlegroups.com, jannh@google.com Cc: linux-kernel@vger.kernel.org, Andrey Konovalov , Andy Lutomirski , Sean Christopherson Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To support evaluating 64-bit kernel mode instructions: Replace existing checks for user_64bit_mode() with a new helper that checks whether code is being executed in either 64-bit kernel mode or 64-bit user mode. Select the GS base depending on whether the instruction is being evaluated in kernel mode. Signed-off-by: Jann Horn --- Notes: v2-v6: no changes arch/x86/include/asm/ptrace.h | 13 +++++++++++++ arch/x86/lib/insn-eval.c | 26 +++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h index 5057a8ed100b..ac45b06941a5 100644 --- a/arch/x86/include/asm/ptrace.h +++ b/arch/x86/include/asm/ptrace.h @@ -159,6 +159,19 @@ static inline bool user_64bit_mode(struct pt_regs *regs) #endif } +/* + * Determine whether the register set came from any context that is running in + * 64-bit mode. + */ +static inline bool any_64bit_mode(struct pt_regs *regs) +{ +#ifdef CONFIG_X86_64 + return !user_mode(regs) || user_64bit_mode(regs); +#else + return false; +#endif +} + #ifdef CONFIG_X86_64 #define current_user_stack_pointer() current_pt_regs()->sp #define compat_user_stack_pointer() current_pt_regs()->sp diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c index 306c3a0902ba..31600d851fd8 100644 --- a/arch/x86/lib/insn-eval.c +++ b/arch/x86/lib/insn-eval.c @@ -155,7 +155,7 @@ static bool check_seg_overrides(struct insn *insn, int regoff) */ static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off) { - if (user_64bit_mode(regs)) + if (any_64bit_mode(regs)) return INAT_SEG_REG_IGNORE; /* * Resolve the default segment register as described in Section 3.7.4 @@ -266,7 +266,7 @@ static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff) * which may be invalid at this point. */ if (regoff == offsetof(struct pt_regs, ip)) { - if (user_64bit_mode(regs)) + if (any_64bit_mode(regs)) return INAT_SEG_REG_IGNORE; else return INAT_SEG_REG_CS; @@ -289,7 +289,7 @@ static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff) * In long mode, segment override prefixes are ignored, except for * overrides for FS and GS. */ - if (user_64bit_mode(regs)) { + if (any_64bit_mode(regs)) { if (idx != INAT_SEG_REG_FS && idx != INAT_SEG_REG_GS) idx = INAT_SEG_REG_IGNORE; @@ -646,23 +646,27 @@ unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx) */ return (unsigned long)(sel << 4); - if (user_64bit_mode(regs)) { + if (any_64bit_mode(regs)) { /* * Only FS or GS will have a base address, the rest of * the segments' bases are forced to 0. */ unsigned long base; - if (seg_reg_idx == INAT_SEG_REG_FS) + if (seg_reg_idx == INAT_SEG_REG_FS) { rdmsrl(MSR_FS_BASE, base); - else if (seg_reg_idx == INAT_SEG_REG_GS) + } else if (seg_reg_idx == INAT_SEG_REG_GS) { /* * swapgs was called at the kernel entry point. Thus, * MSR_KERNEL_GS_BASE will have the user-space GS base. */ - rdmsrl(MSR_KERNEL_GS_BASE, base); - else + if (user_mode(regs)) + rdmsrl(MSR_KERNEL_GS_BASE, base); + else + rdmsrl(MSR_GS_BASE, base); + } else { base = 0; + } return base; } @@ -703,7 +707,7 @@ static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx) if (sel < 0) return 0; - if (user_64bit_mode(regs) || v8086_mode(regs)) + if (any_64bit_mode(regs) || v8086_mode(regs)) return -1L; if (!sel) @@ -948,7 +952,7 @@ static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs, * following instruction. */ if (*regoff == -EDOM) { - if (user_64bit_mode(regs)) + if (any_64bit_mode(regs)) tmp = regs->ip + insn->length; else tmp = 0; @@ -1250,7 +1254,7 @@ static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs) * After computed, the effective address is treated as an unsigned * quantity. */ - if (!user_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit)) + if (!any_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit)) goto out; /* -- 2.24.0.393.g34dc348eaf-goog