Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3645131imm; Mon, 18 Jun 2018 01:27:56 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJc1qnXT1SVOpUBX/1jArBCUQF0UsdO8py9BvBMwF3hIKo9W+SxVHef6dbQkWGVbTPX28Ov X-Received: by 2002:a17:902:b48f:: with SMTP id y15-v6mr13049308plr.261.1529310475992; Mon, 18 Jun 2018 01:27:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529310475; cv=none; d=google.com; s=arc-20160816; b=NT8AtJVelAE8FmHREecrnNaQEdYPfLw1HBtF38g3g21upweNh3d8gtzzqDjKdUF5V3 HAlyh6xTDsoX4x5MgaIB1FtTUeC3bmn2BLkubI+kIguj12ZjqHVBtUBjrMm8r2owKZea VsCivn2ecmb2XgPthQQqb4+cGw8FRk/NPY1zgH6QoJp9PiPnjbhaPNxb+GmSm4DZpISu Y0/xJgILnLjpQzy9PJkJnydgUvwEglhyRqPhzRhJSosguUxgLrpFPxfnqLbPctVHH6pt wKtiZkrOdLtzmD7ifBswUDUdQXgBpbFZOA6zDXnpljYuMxWI3ObZeLfpQWrT1Kq7Mfdm RSWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=7enAXEC1wK/ktRs9v7bMYqy4B2E1DLS5dG3JERAOFpk=; b=Trl55yBJjytgUaMUEpctVhFTAaPo2wxS+NET+7QExCG1aZIp3l8A3I5EOBkmQJ23Qr 9AeWTUiDpsJATlJYy/2SlhJQWQ5WCBpc4p2MaJ6J1ehit0AI8Fh3zBmiaHgk+d75l1/Z YvbiC+MvBHTaNY833mM/Zzq6z27oqivPxowYmYkBUrmtDLQwteYNRc2c3J9rA41h1LFV qTEgE7TMBe6nvbGZ87TRW+d2YbvYvssFh8RYm/zq0AAHK335Fgmlo1xH6U6fmwDR8EZJ 1tCmgQf8AUxHebkIYcp8ltuYmPE7l9k1qINk4C9vZt1hJ9TGrqPlTQxoTjiPIB6FLXnD SRRA== 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 z8-v6si11940034pgs.533.2018.06.18.01.27.42; Mon, 18 Jun 2018 01:27:55 -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 S966070AbeFRI0K (ORCPT + 99 others); Mon, 18 Jun 2018 04:26:10 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:57154 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966044AbeFRI0H (ORCPT ); Mon, 18 Jun 2018 04:26:07 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 9A2BEC8D; Mon, 18 Jun 2018 08:26:06 +0000 (UTC) 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" , Sasha Levin Subject: [PATCH 4.16 231/279] kprobes/x86: Prohibit probing on exception masking instructions Date: Mon, 18 Jun 2018 10:13:36 +0200 Message-Id: <20180618080618.376827758@linuxfoundation.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180618080608.851973560@linuxfoundation.org> References: <20180618080608.851973560@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Masami Hiramatsu [ Upstream commit ee6a7354a3629f9b65bc18dbe393503e9440d6f5 ] 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: Sasha Levin 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 @@ -370,6 +370,10 @@ int __copy_instruction(u8 *dest, u8 *src if (insn->opcode.bytes[0] == BREAKPOINT_INSTRUCTION) return 0; + /* We should not singlestep on the exception masking instructions */ + if (insn_masking_exception(insn)) + return 0; + #ifdef CONFIG_X86_64 /* Only x86_64 has RIP relative instructions */ if (insn_rip_relative(insn)) {