Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp533252yba; Fri, 3 May 2019 06:28:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyf/n2FHAthBIAWuiAd2LZMQRRg03wx+MNgm44iL6T9RlhGdN8RwIVktmyNF+xwYygfjlL X-Received: by 2002:a62:62c2:: with SMTP id w185mr10724546pfb.237.1556890084083; Fri, 03 May 2019 06:28:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556890084; cv=none; d=google.com; s=arc-20160816; b=uay0RCB9RtwgD2g4RxpFf2t6THTuvRd2V3dbK9/fsVYZEWxzBQzi/tkqarjXHjlQ/D DIxMvafAB7EWPgZBzw2TmQcf1PI8vkKRm+sDSkV8wMpWf/cDJ/FuyWwhsmAmQUvbcPkR 4UCUiXhI/LQc2VmFr3C8DUH+cGnrZKuk6xa/NODJNRZWh6EtmAU6hk6oISGze0VcjSgE K6ks3/El+zEifdlCLEfHaDbZmTg0lJDBXmduuS4oyO+I9I8O5XOEMFsfv2PNvuI1lxEc 2tW5wOFZD5rtbqXhq3vLnm3eMQS6pcvwEZuDOkCSVG3DxEqIdQZQsgNH+SaOUvuGiqHa zU8g== 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 :references:in-reply-to:message-id:subject:cc:to:from:date; bh=4MRBSrmmaSLfg4OmwjrUC4452GBVIRp4PSE1GNM7lJc=; b=eLN4t38++Tf0qQ+djF673G6APZIX281frfpj+Qjfox6zisbJXtykeLUcDC4L5FshEu 95qdtoV/8csRf//xVMijzaVkbi6qw5Llmn4qmyyyE0SJynKHMf8AEia8gAZvkXFRo0Pd rYZbrZskG1y/p/7pzFGpzGXR1D+TPFim3HXpytc6VCObbHdA/J1HuA9hNJnnYTaTFFeB hWeWPelCPD/bQFbGNTyZDLgwWBjrEjWbzRFyPS41P+7almQxRgo6J/FbHzgGD0MY1lQn jrRpaEBXzU7sqNpXAHFLhBB6lAzvnSDGf+VvyHAH6EXqD+ZS9OtcBduTHUrZQwKU+DyM i0Kg== 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 c3si2339879plo.243.2019.05.03.06.27.48; Fri, 03 May 2019 06:28:04 -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 S1727640AbfECNWw (ORCPT + 99 others); Fri, 3 May 2019 09:22:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:60828 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726289AbfECNWw (ORCPT ); Fri, 3 May 2019 09:22:52 -0400 Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (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 A51BC2075E; Fri, 3 May 2019 13:22:48 +0000 (UTC) Date: Fri, 3 May 2019 09:22:47 -0400 From: Steven Rostedt To: Peter Zijlstra Cc: Linus Torvalds , Linux List Kernel Mailing , Ingo Molnar , Andrew Morton , Andy Lutomirski , Nicolai Stange , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , the arch/x86 maintainers , Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Shuah Khan , Konrad Rzeszutek Wilk , Tim Chen , Sebastian Andrzej Siewior , Mimi Zohar , Juergen Gross , Nick Desaulniers , Nayna Jain , Masahiro Yamada , Joerg Roedel , "open list:KERNEL SELFTEST FRAMEWORK" , stable Subject: Re: [RFC][PATCH 1/2] x86: Allow breakpoints to emulate call functions Message-ID: <20190503092247.20cc1ff0@gandalf.local.home> In-Reply-To: <20190503092959.GB2623@hirez.programming.kicks-ass.net> References: <20190501203152.397154664@goodmis.org> <20190501232412.1196ef18@oasis.local.home> <20190502162133.GX2623@hirez.programming.kicks-ass.net> <20190502181811.GY2623@hirez.programming.kicks-ass.net> <20190502202146.GZ2623@hirez.programming.kicks-ass.net> <20190502185225.0cdfc8bc@gandalf.local.home> <20190502193129.664c5b2e@gandalf.local.home> <20190502195052.0af473cf@gandalf.local.home> <20190503092959.GB2623@hirez.programming.kicks-ass.net> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 3 May 2019 11:29:59 +0200 Peter Zijlstra wrote: > OMG, WTF, ARGH... That code is fsck'ing horrible. I'd almost argue to > always do the INT3 thing, just to avoid games like that. Hehe, that's almost the exact same thoughts I had when seeing this code ;-) > > That said; for normal traps ®s->sp is indeed the previous context -- > if it doesn't fall off the stack. Your hack detects the regular INT3 > frame. Howver if regs->sp has been modified (int3_emulate_push, for > example) your detectoring comes unstuck. Yep. I realized the issue as well. But wanted to make sure this did work when sp wasn't changed. > > Now, it is rather unlikely these two code paths interact, but just to be > safe, something like so might be more reliable: > > > diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c > index 4b8ee05dd6ad..aceaad0cc9a9 100644 > --- a/arch/x86/kernel/ptrace.c > +++ b/arch/x86/kernel/ptrace.c > @@ -163,6 +163,9 @@ static inline bool invalid_selector(u16 value) > * stack pointer we fall back to regs as stack if no previous stack > * exists. > * > + * There is a special case for INT3, there we construct a full pt_regs > + * environment. We can detect this case by a high bit in regs->cs > + * > * This is valid only for kernel mode traps. > */ > unsigned long kernel_stack_pointer(struct pt_regs *regs) > @@ -171,6 +174,9 @@ unsigned long kernel_stack_pointer(struct pt_regs *regs) > unsigned long sp = (unsigned long)®s->sp; > u32 *prev_esp; > > + if (regs->__csh & (1 << 13)) /* test CS_FROM_INT3 */ > + return regs->sp; > + Thanks, I was looking into doing something like this (setting a flag in the int3 code), but didn't have the time to see the best way to do this. I'll add this version of the code and run it through my tests. -- Steve > if (context == (sp & ~(THREAD_SIZE - 1))) > return sp; > > --- a/arch/x86/entry/entry_32.S > +++ b/arch/x86/entry/entry_32.S > @@ -388,6 +388,7 @@ > > #define CS_FROM_ENTRY_STACK (1 << 31) > #define CS_FROM_USER_CR3 (1 << 30) > +#define CS_FROM_INT3 (1 << 29) > > .macro SWITCH_TO_KERNEL_STACK > > @@ -1515,6 +1516,9 @@ ENTRY(int3) > > add $16, 12(%esp) # point sp back at the previous context > > + andl $0x0000ffff, 4(%esp) > + orl $CS_FROM_INT3, 4(%esp) > + > pushl $-1 # orig_eax; mark as interrupt > > SAVE_ALL