Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp15479798ybl; Tue, 31 Dec 2019 08:34:27 -0800 (PST) X-Google-Smtp-Source: APXvYqzDAjCPdBre4brL30UlH1ASyXJ+ICPYIzTbZcph00pV/fLZlfCuFiTJ/096ktiS4+xnjHzn X-Received: by 2002:a05:6402:1a25:: with SMTP id be5mr75880071edb.220.1577810067749; Tue, 31 Dec 2019 08:34:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577810067; cv=none; d=google.com; s=arc-20160816; b=lzwK2exb9qLfRJi3dnc/5N1fn54Ul548rseLnMjgj3n9Y7Z6BXfAlJkw2Y1YN4SPTu kAeieFw7fuA2NrsyMxe/p94KapTc3jMVPGoSjyiqEmgawQzuePQaoWt/dxtgzr1V+Dv4 73ZudURTPWY/Yvmckoj67Tbs91vM/9n3t+oueDv+yPen8cHuONp/NYfYGR3ycVUOMb6V T6EO4Qn9P9mgIa0/sd9IyhYsB0FF6pot8nyBVIKCciV8KjD+92HsCX7OK08ZdhmElp7K P5Aegozk4gOHJaTPfokHxawiC8yuOSL2lzh/aXjyzMC7bnhjnrFD6epj7m93cQbRNvEv acxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=XLKvpbF/byz3Z6T7cOCCHGN1ZNPGh/rIhiwmf7WR9SA=; b=NDzpp0rJT92g+m/HsxCeRj2M2u5qVirv4cnk43HvlQeD9aI+DKplupSRei4rLEo++u tz6LdCXKMsyJgyL5R1WLtMgjaomfw2iohBKgSv7AND/w3ReAdk6ClzjL4Un2U84WQErV VyrmGjyoxOU0ZOX7cPdDGQmIJMSUQ/FnKXXsBZ2N12tRM+5GjclB/gl+XhSAEePnCvDS V/Fz51maqpiEFwYfK5l5k+Js08kZQm67G40B3j0lh3GkKAa562a+z3nyh3wVqhnwyRq9 nEr8TG2fRPsvwAdhGFZzbgTISJFY3n0H4lIy8X54WshVXweOXKyLGpIQqOtLDiA6fDOc F9sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b="aZh/zyjU"; 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=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g18si31174176ejr.358.2019.12.31.08.33.49; Tue, 31 Dec 2019 08:34:27 -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=@alien8.de header.s=dkim header.b="aZh/zyjU"; 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=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727071AbfLaQbS (ORCPT + 99 others); Tue, 31 Dec 2019 11:31:18 -0500 Received: from mail.skyhub.de ([5.9.137.197]:47296 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726060AbfLaQbS (ORCPT ); Tue, 31 Dec 2019 11:31:18 -0500 Received: from zn.tnic (p200300EC2F00E7007CEDBF47C01C0A42.dip0.t-ipconnect.de [IPv6:2003:ec:2f00:e700:7ced:bf47:c01c:a42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 823371EC0273; Tue, 31 Dec 2019 17:31:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1577809876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=XLKvpbF/byz3Z6T7cOCCHGN1ZNPGh/rIhiwmf7WR9SA=; b=aZh/zyjUpkE7oqr4WKclNJ6Hjkima9b/OTjhub45rFI7P/lCOjAS24rd3915L8lBoel76q hSsCNMhJGYzkhFEU6zGOTXLka+L75QOvunZ2WbenwRuifOre1FlO8gmNEesbFnAxqkK2uC 22trVeJGKL1draP8JmLvw1ROYNprE3o= Date: Tue, 31 Dec 2019 17:31:08 +0100 From: Borislav Petkov To: Jann Horn Cc: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, Andrey Konovalov , Andy Lutomirski , Sean Christopherson Subject: [PATCH] x86/traps: Cleanup do_general_protection() Message-ID: <20191231163108.GC13549@zn.tnic> References: <20191218231150.12139-1-jannh@google.com> <20191218231150.12139-3-jannh@google.com> <20191231121121.GA13549@zn.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191231121121.GA13549@zn.tnic> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ... and a cleanup ontop: --- From: Borislav Petkov Date: Tue, 31 Dec 2019 17:15:35 +0100 Hoist the user_mode() case up because it is less code and can be dealt with up-front like the other special cases UMIP and vm86. This saves an indentation level for the kernel-mode #GP case and allows to "unfold" the code more so that it is more readable. No functional changes. Signed-off-by: Borislav Petkov Cc: Jann Horn Cc: x86@kernel.org --- arch/x86/kernel/traps.c | 79 +++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 2afd7d8d4007..ca395ad28b4e 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -567,7 +567,10 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs, dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code) { char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR; + enum kernel_gp_hint hint = GP_NO_HINT; struct task_struct *tsk; + unsigned long gp_addr; + int ret; RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); cond_local_irq_enable(regs); @@ -584,58 +587,56 @@ dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code) } tsk = current; - if (!user_mode(regs)) { - enum kernel_gp_hint hint = GP_NO_HINT; - unsigned long gp_addr; - - if (fixup_exception(regs, X86_TRAP_GP, error_code, 0)) - return; + if (user_mode(regs)) { tsk->thread.error_code = error_code; tsk->thread.trap_nr = X86_TRAP_GP; - /* - * To be potentially processing a kprobe fault and to - * trust the result from kprobe_running(), we have to - * be non-preemptible. - */ - if (!preemptible() && kprobe_running() && - kprobe_fault_handler(regs, X86_TRAP_GP)) - return; + show_signal(tsk, SIGSEGV, "", desc, regs, error_code); + force_sig(SIGSEGV); - if (notify_die(DIE_GPF, desc, regs, error_code, - X86_TRAP_GP, SIGSEGV) == NOTIFY_STOP) - return; + return; + } - if (error_code) - snprintf(desc, sizeof(desc), "segment-related " GPFSTR); - else - hint = get_kernel_gp_address(regs, &gp_addr); + if (fixup_exception(regs, X86_TRAP_GP, error_code, 0)) + return; - if (hint != GP_NO_HINT) - snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx", - (hint == GP_NON_CANONICAL) ? - "probably for non-canonical address" : - "maybe for address", - gp_addr); + tsk->thread.error_code = error_code; + tsk->thread.trap_nr = X86_TRAP_GP; - /* - * KASAN is interested only in the non-canonical case, clear it - * otherwise. - */ - if (hint != GP_NON_CANONICAL) - gp_addr = 0; + /* + * To be potentially processing a kprobe fault and to trust the result + * from kprobe_running(), we have to be non-preemptible. + */ + if (!preemptible() && + kprobe_running() && + kprobe_fault_handler(regs, X86_TRAP_GP)) + return; - die_addr(desc, regs, error_code, gp_addr); + ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV); + if (ret == NOTIFY_STOP) return; - } - tsk->thread.error_code = error_code; - tsk->thread.trap_nr = X86_TRAP_GP; + if (error_code) + snprintf(desc, sizeof(desc), "segment-related " GPFSTR); + else + hint = get_kernel_gp_address(regs, &gp_addr); + + if (hint != GP_NO_HINT) + snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx", + (hint == GP_NON_CANONICAL) ? "probably for non-canonical address" + : "maybe for address", + gp_addr); + + /* + * KASAN is interested only in the non-canonical case, clear it + * otherwise. + */ + if (hint != GP_NON_CANONICAL) + gp_addr = 0; - show_signal(tsk, SIGSEGV, "", desc, regs, error_code); + die_addr(desc, regs, error_code, gp_addr); - force_sig(SIGSEGV); } NOKPROBE_SYMBOL(do_general_protection); -- 2.21.0 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette