Received: by 2002:a17:90a:88:0:0:0:0 with SMTP id a8csp58335pja; Fri, 22 Nov 2019 03:25:32 -0800 (PST) X-Google-Smtp-Source: APXvYqweUS8uhsMCTWcTQvlNpud80lwaIvvGOPrhbdBezikdl8lGji3kCgdtSO8GE+G+AQIlytRH X-Received: by 2002:aa7:c990:: with SMTP id c16mr417600edt.91.1574421931892; Fri, 22 Nov 2019 03:25:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574421931; cv=none; d=google.com; s=arc-20160816; b=cSF7fspkNB/B+yV0agj9sW3q8+PHODgW+t3jcQ1NQwFcsahxpjOn65WPG9rsyuTxPt 9Zv3IClKWgmEfa74w8z3J4rdpXgqv+V7+bzzWhoRYWsUh4i9qawKh6jjo0WnFWKY0EdX OpPZzonG7tUIdxcEy7oZC2cl62aZe44ClSLK1lRS35SRzCZTojEvWSuNiZRf0/8SIgQ4 pMv/Ht2oglNF+DB73WK7LWvAv+5qU3WVVK1PQ1bfbfeoreoh5IstaKyH8qWyNLWqztFr DAqzDT0BTu3ZvWa0riZaJ+ElWFwAjq/c9tzC0SBHhiERd8sNEQbeid+ztMjIAqs73DRE whXw== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=vjVR6e2OyRKKRhLRxEndi8EdCWB3uslsJdypLedgMAo=; b=gS47d9bD+5yS8p67dKoQSWS+5LDZ1RmoP9We61FSDWXGnt2CEvyoGhLcQuze/zk8Xb P5OPFqQB4F0J+GdqukIy3iV2UapAU7kI+DvrTSbJHAMwzzRfvtegx5sM1V3QOrODjkY7 p8zynOkh3w64f3u+uZj/dSUjNn/WVRDNblyRigeel5oFjTsFro+g2b8m3ym3XwVod4Tm k0ZjkUN+mMbLd1oXKUsFdhTnsLeOSBACTkJrRA6ASqLCYxoj1cnxHbVGoG2l3KBz684N WBsmJmCtARqlvnWM3cS8JYsLJLp9XfIdRsKtVPQjMUO7qKgSvZpNUJLwQfu+zrx4sHOl sffA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yeyHu9jY; 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 e10si3878999ejq.413.2019.11.22.03.25.07; Fri, 22 Nov 2019 03:25:31 -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=@kernel.org header.s=default header.b=yeyHu9jY; 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 S1729567AbfKVLUb (ORCPT + 99 others); Fri, 22 Nov 2019 06:20:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:52558 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727944AbfKVKpy (ORCPT ); Fri, 22 Nov 2019 05:45:54 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E4EA320715; Fri, 22 Nov 2019 10:45:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574419553; bh=cr3IUombf9EhZ71/I7ho4fT1dibD1bo/zsPTJKKMmA8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yeyHu9jYJwigy7B3F99Wz2oytgOFIUcON5aQJDObFdyvScsqWe0usVWk/jfaZQrSG efuTaRqMKxLU/bd0qAkPa/h4H+b338Jtz0m5Hd2TMOsZCBTtLtVNcYIlPMalFMYAQi xKIefxytrPafGOEvxCMdaZLEXH4y3JPAUwbp0l7U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Masami Hiramatsu , Thomas Gleixner , Ricardo Neri , Francis Deslauriers , Oleg Nesterov , Alexei Starovoitov , Steven Rostedt , Andy Lutomirski , "H . Peter Anvin" , Yonghong Song , Borislav Petkov , Linus Torvalds , "David S . Miller" Subject: [PATCH 4.9 150/222] kprobes/x86: Prohibit probing on exception masking instructions Date: Fri, 22 Nov 2019 11:28:10 +0100 Message-Id: <20191122100913.582883301@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191122100830.874290814@linuxfoundation.org> References: <20191122100830.874290814@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 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 From: Masami Hiramatsu commit ee6a7354a3629f9b65bc18dbe393503e9440d6f5 upstream. Since MOV SS and POP SS instructions will delay the exceptions until the next instruction is executed, single-stepping on it by kprobes must be prohibited. However, kprobes usually executes those instructions directly on trampoline buffer (a.k.a. kprobe-booster), except for the kprobes which has post_handler. Thus if kprobe user probes MOV SS with post_handler, it will do single-stepping on the MOV SS. This means it is safe that if it is used via ftrace or perf/bpf since those don't use the post_handler. Anyway, since the stack switching is a rare case, it is safer just rejecting kprobes on such instructions. Signed-off-by: Masami Hiramatsu Signed-off-by: Thomas Gleixner Cc: Ricardo Neri Cc: Francis Deslauriers Cc: Oleg Nesterov Cc: Alexei Starovoitov Cc: Steven Rostedt Cc: Andy Lutomirski Cc: "H . Peter Anvin" Cc: Yonghong Song Cc: Borislav Petkov Cc: Linus Torvalds Cc: "David S . Miller" Link: https://lkml.kernel.org/r/152587069574.17316.3311695234863248641.stgit@devbox Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/insn.h | 18 ++++++++++++++++++ arch/x86/kernel/kprobes/core.c | 4 ++++ 2 files changed, 22 insertions(+) --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -208,4 +208,22 @@ static inline int insn_offset_immediate( return insn_offset_displacement(insn) + insn->displacement.nbytes; } +#define POP_SS_OPCODE 0x1f +#define MOV_SREG_OPCODE 0x8e + +/* + * Intel SDM Vol.3A 6.8.3 states; + * "Any single-step trap that would be delivered following the MOV to SS + * instruction or POP to SS instruction (because EFLAGS.TF is 1) is + * suppressed." + * This function returns true if @insn is MOV SS or POP SS. On these + * instructions, single stepping is suppressed. + */ +static inline int insn_masking_exception(struct insn *insn) +{ + return insn->opcode.bytes[0] == POP_SS_OPCODE || + (insn->opcode.bytes[0] == MOV_SREG_OPCODE && + X86_MODRM_REG(insn->modrm.bytes[0]) == 2); +} + #endif /* _ASM_X86_INSN_H */ --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -376,6 +376,10 @@ int __copy_instruction(u8 *dest, u8 *src return 0; memcpy(dest, insn.kaddr, length); + /* We should not singlestep on the exception masking instructions */ + if (insn_masking_exception(&insn)) + return 0; + #ifdef CONFIG_X86_64 if (insn_rip_relative(&insn)) { s64 newdisp;