From patchwork Sun Apr 21 18:28:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 13637484 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 61AF1C07E8D for ; Sun, 21 Apr 2024 18:29:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=J+ffYSf5RvSyep6AjwWRenGOqQ/+ACjX8ofAoOZthww=; b=pDCo7YLbinrSvr GyQgUVn2+8Y9NvWffvKxVuZpJi/OkTkCLHzTUhwkhDyslOJnOfUmoVyESUN2sfJ9Q1hpTgVL/3yzE 6l/f2Za+AI4CK+qTrpAsRSayIhF7wLSowIXKcaqIfJepf697Y6pa4XdxxTw1/pcImFlpQ4yaKhz2G p2r9hiyinSM4byv/lbv8UOp+Hcjp5BUDY12ikZVXicD7YG+6b3WVD4gLcvBxb6RbQXj1M5/eTaGxJ pFCsj+phZnqEMHqeRsR/5vky4A5GpfU2P/Lenp9bsN8pP+e4LqRGNSijGfC8wj3BCD9DMaxZwjfK7 87yU0yccxBX6oEGpTcoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rybvy-0000000B4Dp-3FkD; Sun, 21 Apr 2024 18:29:02 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rybvd-0000000B43D-3IOh for linux-arm-kernel@lists.infradead.org; Sun, 21 Apr 2024 18:28:44 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-5193363d255so4835162e87.3 for ; Sun, 21 Apr 2024 11:28:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713724119; x=1714328919; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ecw0uWRNm3i8fpJCtFnOl72Sdbbv98c8IiHQj1PGVlc=; b=aW1tDkkDnaeO+8pYv0VqY/MXEInh7/ntHvFB/qgH7OHvDjt2x2ZcsxdoJKWz+22a16 mzOHm33GoSpwZu+xqF6RQRqUM3QJ0IWiuBm1HniqGw91t6RK7cUd3UlOArbMIY3Rttmb 5ukl1ctnmKrqxUNYByk5eGjP0mffFUfzAXuOrzvT4ospdfr78nU2VLkfXpUWx8AX4dAQ 44k7ytHt64GWx9P0l8qxl9EdG1b9b2rVOMAemdy+mmnMQ0QII4fZH0Xe61xNyXjJuR7V YxQ8DnAmrATZsPVtJBMNILinmcuCZEGK3V7uf6t1AbBiLZKHcMa2KAm6uzgIbUCyl4oL UjFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713724119; x=1714328919; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ecw0uWRNm3i8fpJCtFnOl72Sdbbv98c8IiHQj1PGVlc=; b=gVebIzoVj3O42TFBPNEUqEOTuAGq8j/eG2VuArm2OvSSksg3d3zxciaBbbfxgnDxcV xVSFnIpuwRBTyK+aWSiWGj7oHLXusWPcoh+fHw+eTv6Pde05o9YQPjbje6XF2GHC/mJP qDTPmQXFB2oS71fkWl0G4KnxqzUVorDe/U9gSaThM6H2a09AiGK7cilGqovbHdEPaJuH CZSq7JnpTN6Tn5uUXw5JKRifz0QUyUpxtcMc8RgT8HmgQNOTb+XtFSJiH0p9mRWHCkNE EfvOdjUxdTXHyArDCGdqKibRsMcvx2Aqz0hqkIj5ZOtrrzNo8DFWJ8EeacjdbW7ZmE+A GyJQ== X-Gm-Message-State: AOJu0Yy+cFoW52Qu3eLhj/hx142hwYc8RT3+dJGA0oPyX2r30h6NE5iH VvlDsMwVuSUa2psmb69kiEbqfJTJN8oxAqFirEonHUAM4hak7c/r/O73AxRjWMc= X-Google-Smtp-Source: AGHT+IFQEQOCTGYb+sj338RlX5DuJLbp/3EO6h8z6/lx8OWssMUSvRxd4tjvA+IzpvfzYdtFnvy/vw== X-Received: by 2002:ac2:41d6:0:b0:515:a417:334 with SMTP id d22-20020ac241d6000000b00515a4170334mr4597907lfi.46.1713724118796; Sun, 21 Apr 2024 11:28:38 -0700 (PDT) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id s14-20020a19770e000000b005188ea5d2b5sm1549641lfc.303.2024.04.21.11.28.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Apr 2024 11:28:38 -0700 (PDT) From: Linus Walleij Date: Sun, 21 Apr 2024 20:28:36 +0200 Subject: [PATCH v7 10/11] ARM: hw_breakpoint: Handle CFI breakpoints MIME-Version: 1.0 Message-Id: <20240421-arm32-cfi-v7-10-6e132a948cc8@linaro.org> References: <20240421-arm32-cfi-v7-0-6e132a948cc8@linaro.org> In-Reply-To: <20240421-arm32-cfi-v7-0-6e132a948cc8@linaro.org> To: Russell King , Sami Tolvanen , Kees Cook , Nathan Chancellor , Nick Desaulniers , Ard Biesheuvel , Arnd Bergmann Cc: linux-arm-kernel@lists.infradead.org, llvm@lists.linux.dev, Linus Walleij X-Mailer: b4 0.13.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240421_112842_152863_978886D9 X-CRM114-Status: GOOD ( 18.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This registers a breakpoint handler for the new breakpoint type (0x03) inserted by LLVM CLANG for CFI breakpoints. If we are in permissive mode, just print a backtrace and continue. Example with CONFIG_CFI_PERMISSIVE enabled: > echo CFI_FORWARD_PROTO > /sys/kernel/debug/provoke-crash/DIRECT lkdtm: Performing direct entry CFI_FORWARD_PROTO lkdtm: Calling matched prototype ... lkdtm: Calling mismatched prototype ... CFI failure at lkdtm_indirect_call+0x40/0x4c (target: 0x0; expected type: 0x00000000) WARNING: CPU: 1 PID: 112 at lkdtm_indirect_call+0x40/0x4c CPU: 1 PID: 112 Comm: sh Not tainted 6.8.0-rc1+ #150 Hardware name: ARM-Versatile Express (...) lkdtm: FAIL: survived mismatched prototype function call! lkdtm: Unexpected! This kernel (6.8.0-rc1+ armv7l) was built with CONFIG_CFI_CLANG=y As you can see the LKDTM test fails, but I expect that this would be expected behaviour in the permissive mode. We are currently not implementing target and type for the CFI breakpoint as this requires additional operand bundling compiler extensions. CPUs without breakpoint support cannot handle breakpoints naturally, in these cases the permissive mode will not work, CFI will fall over on an undefined instruction: Internal error: Oops - undefined instruction: 0 [#1] PREEMPT ARM CPU: 0 PID: 186 Comm: ash Tainted: G W 6.9.0-rc1+ #7 Hardware name: Gemini (Device Tree) PC is at lkdtm_indirect_call+0x38/0x4c LR is at lkdtm_CFI_FORWARD_PROTO+0x30/0x6c This is reasonable I think: it's the best CFI can do to ascertain the the control flow is not broken on these CPUs. Reviewed-by: Kees Cook Tested-by: Kees Cook Signed-off-by: Linus Walleij --- arch/arm/include/asm/hw_breakpoint.h | 1 + arch/arm/kernel/hw_breakpoint.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/arch/arm/include/asm/hw_breakpoint.h b/arch/arm/include/asm/hw_breakpoint.h index 62358d3ca0a8..e7f9961c53b2 100644 --- a/arch/arm/include/asm/hw_breakpoint.h +++ b/arch/arm/include/asm/hw_breakpoint.h @@ -84,6 +84,7 @@ static inline void decode_ctrl_reg(u32 reg, #define ARM_DSCR_MOE(x) ((x >> 2) & 0xf) #define ARM_ENTRY_BREAKPOINT 0x1 #define ARM_ENTRY_ASYNC_WATCHPOINT 0x2 +#define ARM_ENTRY_CFI_BREAKPOINT 0x3 #define ARM_ENTRY_SYNC_WATCHPOINT 0xa /* DSCR monitor/halting bits. */ diff --git a/arch/arm/kernel/hw_breakpoint.c b/arch/arm/kernel/hw_breakpoint.c index dc0fb7a81371..93f9034e4e72 100644 --- a/arch/arm/kernel/hw_breakpoint.c +++ b/arch/arm/kernel/hw_breakpoint.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -903,6 +904,37 @@ static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs) watchpoint_single_step_handler(addr); } +#ifdef CONFIG_CFI_CLANG +static void hw_breakpoint_cfi_handler(struct pt_regs *regs) +{ + /* + * TODO: implementing target and type to pass to CFI using the more + * elaborate report_cfi_failure() requires compiler work. To be able + * to properly extract target information the compiler needs to + * emit a stable instructions sequence for the CFI checks so we can + * decode the instructions preceding the trap and figure out which + * registers were used. + */ + + switch (report_cfi_failure_noaddr(regs, instruction_pointer(regs))) { + case BUG_TRAP_TYPE_BUG: + die("Oops - CFI", regs, 0); + break; + case BUG_TRAP_TYPE_WARN: + /* Skip the breaking instruction */ + instruction_pointer(regs) += 4; + break; + default: + die("Unknown CFI error", regs, 0); + break; + } +} +#else +static void hw_breakpoint_cfi_handler(struct pt_regs *regs) +{ +} +#endif + /* * Called from either the Data Abort Handler [watchpoint] or the * Prefetch Abort Handler [breakpoint] with interrupts disabled. @@ -932,6 +964,9 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr, case ARM_ENTRY_SYNC_WATCHPOINT: watchpoint_handler(addr, fsr, regs); break; + case ARM_ENTRY_CFI_BREAKPOINT: + hw_breakpoint_cfi_handler(regs); + break; default: ret = 1; /* Unhandled fault. */ }