Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1439760pxb; Thu, 4 Mar 2021 11:17:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJzs4XcuWdss8hVP8EL9+MnAnYdfUQ6yorBQiucpeLaGoD7vue21gTKKbtvXRSutYfFhj6Wl X-Received: by 2002:a17:906:5797:: with SMTP id k23mr6104117ejq.515.1614885437183; Thu, 04 Mar 2021 11:17:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614885437; cv=none; d=google.com; s=arc-20160816; b=nxpCm6RqIfvQwchiNsOOKWMfhaE2gOg8C6Ds5riWyAM2Gz7JsftB4/f9s+NR3M6IA9 WuhOrccapYATw75x9dwg7aeWs1cGy6hs4UUmzYN/ihsb34wqFXCz1StnV8mixqdrenL+ eJTXOkiVy4uLz2/jKNa5tNbkiHWu1ifE/NR3yPLbcIQnpRu/elFpAYqkHx6ERotN/0oh DlwA6jwcG4lh48j1Nnfp9dxVSpqpjrUR91h4293DZx5xQKLuKR4xj1WBkHVcxexClqjg KXl7Xz3ORc9q5amRD3elfqLoiO8FNA7lrm3gsKWQGntGb3Di+HW82FwYjm5Stp6qGnI5 zDJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cVTi4NmBYVyxOc9ESXr3CV2yRtdvfe1qL3UCDdkvwcQ=; b=lpGvbZ5Mj96KtacBz35FLXzj66N3vhkNyTmBzJKey/e+7cmCxbMaQtUzDejIC+czuN 40I0GkJJpgnnkTCKLPv9MR+bsfOOGWZS3ac/vfeND+og/4jOo3hjN+2e1vv0Xa1QljzK fJlIw6CJNlsyw/ALW2WvW8cpeNlUvKT+7auyU5qsVQL7pBxISe3MqOXsswcBrKbmTjHo NUVMHcqGcjR5nwyk9AcMGTAGDL4t+yG7iCj75GZ3GMmwXS0gQje5DmDewELxKPaAiKfK gdyBLUnZMQ6vvSttGHOCEIKKFDGeKwWz9Cv54RmuHOsRa4S2UI9L8qPN5/u8iXxVObSm ln9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jj0Ch+a7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si232122edv.439.2021.03.04.11.16.54; Thu, 04 Mar 2021 11:17:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jj0Ch+a7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238058AbhCDTHk (ORCPT + 99 others); Thu, 4 Mar 2021 14:07:40 -0500 Received: from mail.kernel.org ([198.145.29.99]:39322 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237838AbhCDTHa (ORCPT ); Thu, 4 Mar 2021 14:07:30 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C8E0264F7B; Thu, 4 Mar 2021 19:06:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614884776; bh=NVtpsd7hAnZRNM/yEFr2E7BeAFnMe96ztRP75qp4TFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jj0Ch+a7UoZfXPjktbo9sKI/+znhtZ4FWF2DQSupwlmYnbXDh6Nce4aLHurSp/bdZ 45KmQjmmtadjYDPh7mT4FHd9MFxFgA1ZoGvfF1LrklCsGFsaScNOJn3hh+dlaAmjIK 05aQPb6SNdXK/34OhJmUGYyIwBgCecI0e9bQ9VSDlibbcUOxkRPt+VM4oCgncYHzPD tXLUYeZq0IBhaz5bg1DPBSXpSFol6cQaG2MWO7XueQb/ncEX3YZNhzfwh7Q7yRi5Zs hT0vY20cRyNdBLJ5rk4eCpwpvs/g5dd1Hcbl4659KEaWbPEa+o1sa/ms+9nWsuwUcf B1lqSwDFkH1XQ== From: Andy Lutomirski To: x86@kernel.org Cc: LKML , Mark Rutland , Andy Lutomirski Subject: [PATCH v3 11/11] kentry: Verify kentry state in instrumentation_begin/end() Date: Thu, 4 Mar 2021 11:06:04 -0800 Message-Id: <0749a0732066316c2d048c70bf379b17d067f4d2.1614884673.git.luto@kernel.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Calling instrumentation_begin() and instrumentation_end() when kentry thinks the CPU is in user mode is an error. Verify the kentry state when instrumentation_begin/end() are called. Add _nocheck() variants to skip verification to avoid WARN() generating extra kentry warnings. Signed-off-by: Andy Lutomirski --- arch/x86/kernel/traps.c | 4 ++-- include/asm-generic/bug.h | 8 ++++---- include/linux/instrumentation.h | 25 ++++++++++++++++++++----- kernel/entry/common.c | 7 +++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index be924180005a..983e4be5fdcb 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -229,7 +229,7 @@ static noinstr bool handle_bug(struct pt_regs *regs) /* * All lies, just get the WARN/BUG out. */ - instrumentation_begin(); + instrumentation_begin_nocheck(); /* * Since we're emulating a CALL with exceptions, restore the interrupt * state to what it was at the exception site. @@ -242,7 +242,7 @@ static noinstr bool handle_bug(struct pt_regs *regs) } if (regs->flags & X86_EFLAGS_IF) raw_local_irq_disable(); - instrumentation_end(); + instrumentation_end_nocheck(); return handled; } diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h index 76a10e0dca9f..fc360c463a99 100644 --- a/include/asm-generic/bug.h +++ b/include/asm-generic/bug.h @@ -85,18 +85,18 @@ void warn_slowpath_fmt(const char *file, const int line, unsigned taint, const char *fmt, ...); #define __WARN() __WARN_printf(TAINT_WARN, NULL) #define __WARN_printf(taint, arg...) do { \ - instrumentation_begin(); \ + instrumentation_begin_nocheck(); \ warn_slowpath_fmt(__FILE__, __LINE__, taint, arg); \ - instrumentation_end(); \ + instrumentation_end_nocheck(); \ } while (0) #else extern __printf(1, 2) void __warn_printk(const char *fmt, ...); #define __WARN() __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN)) #define __WARN_printf(taint, arg...) do { \ - instrumentation_begin(); \ + instrumentation_begin_nocheck(); \ __warn_printk(arg); \ __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\ - instrumentation_end(); \ + instrumentation_end_nocheck(); \ } while (0) #define WARN_ON_ONCE(condition) ({ \ int __ret_warn_on = !!(condition); \ diff --git a/include/linux/instrumentation.h b/include/linux/instrumentation.h index 93e2ad67fc10..cdf80454f92a 100644 --- a/include/linux/instrumentation.h +++ b/include/linux/instrumentation.h @@ -4,14 +4,21 @@ #if defined(CONFIG_DEBUG_ENTRY) && defined(CONFIG_STACK_VALIDATION) +extern void kentry_assert_may_instrument(void); + /* Begin/end of an instrumentation safe region */ -#define instrumentation_begin() ({ \ - asm volatile("%c0: nop\n\t" \ +#define instrumentation_begin_nocheck() ({ \ + asm volatile("%c0: nop\n\t" \ ".pushsection .discard.instr_begin\n\t" \ ".long %c0b - .\n\t" \ ".popsection\n\t" : : "i" (__COUNTER__)); \ }) +#define instrumentation_begin() ({ \ + instrumentation_begin_nocheck(); \ + kentry_assert_may_instrument(); \ +}) + /* * Because instrumentation_{begin,end}() can nest, objtool validation considers * _begin() a +1 and _end() a -1 and computes a sum over the instructions. @@ -43,15 +50,23 @@ * To avoid this, have _end() be a NOP instruction, this ensures it will be * part of the condition block and does not escape. */ -#define instrumentation_end() ({ \ +#define instrumentation_end_nocheck() ({ \ asm volatile("%c0: nop\n\t" \ ".pushsection .discard.instr_end\n\t" \ ".long %c0b - .\n\t" \ ".popsection\n\t" : : "i" (__COUNTER__)); \ }) + +#define instrumentation_end() ({ \ + kentry_assert_may_instrument(); \ + instrumentation_end_nocheck(); \ +}) + #else -# define instrumentation_begin() do { } while(0) -# define instrumentation_end() do { } while(0) +# define instrumentation_begin_nocheck() do { } while(0) +# define instrumentation_begin() do { } while(0) +# define instrumentation_end_nocheck() do { } while(0) +# define instrumentation_end() do { } while(0) #endif #endif /* __LINUX_INSTRUMENTATION_H */ diff --git a/kernel/entry/common.c b/kernel/entry/common.c index 9dea411de3b0..870595bdd405 100644 --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -56,6 +56,13 @@ static __always_inline void kentry_cpu_depth_sub(unsigned int n) { this_cpu_sub(kentry_cpu_depth, n); } + +void kentry_assert_may_instrument(void) +{ + DEBUG_ENTRY_WARN_ONCE(this_cpu_read(kentry_cpu_depth) == 0, "instrumentable code is running in the wrong kentry state"); +} +EXPORT_SYMBOL_GPL(kentry_assert_may_instrument); + #else #define DEBUG_ENTRY_WARN_ONCE(condition, format...) do {} while (0) -- 2.29.2