From mboxrd@z Thu Jan 1 00:00:00 1970 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 C5A35C4345F for ; Mon, 15 Apr 2024 13:44:20 +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=CQouV5dlynaX0OTIGcHN94J4K1bMnREpAAuGeM+rbd4=; b=ih2trRrqAIQ8QS Ep5xbt2ZjuO8Pofa4MHuRfWCWWUPObAq6v01XMlKiRGAMeTOIaRMNMdvAcbDgZlTwh9xwzavn3h8h Hkk9nJRbLOe9odJ/7PZErxXUvzKUAhlIl/Z/tOL+C2IemvWTzb62Ec2q9l639R0LHWB14t64SLgQp +Q5Dq53Qkeqt8DapXmrtwcTwA+KDgN14572p4fwHnmC0y8Fv+gTf5TpSvE3xfxM7XORJzHQPM6PlQ aJSB0OK7zfWgIgWlZH3vF8tGNi8L4EifG5OvxU6YIx7swsAG9pg9f7rDNj/j07X2lJ7eadASdur2K Wns8DZQ+Pjeh8zBE+k8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rwMd1-00000008Xhi-10SJ; Mon, 15 Apr 2024 13:44:11 +0000 Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rwMcc-00000008XSb-1oAj for linux-arm-kernel@lists.infradead.org; Mon, 15 Apr 2024 13:43:48 +0000 Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2d9fe2b37acso39825051fa.2 for ; Mon, 15 Apr 2024 06:43:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713188624; x=1713793424; 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=UwkkSUi1b63q//pF6FQeQvdmd+qn9ODc8sgiig3GYAI=; b=hcapRwh5qUUgtlw7jXgB2uuEoEHOvr/owi9QZOFuM4qoRDEVMU7Qx4JHiTNfdaCgEJ hCUlaoSMnsLQTL+fUUgDX8J5OZeJzKdldVsR/ZXa6HL7zpQ9frsUgDZM2mP6ROApSxC2 kEI2N0ZoTVurWCo/DQYJv+CM+nT6SNkJVbbAqgsDJZJ+kn5UYFCzzFq4vQfcBVGo7Bv+ W6l8QfFwl8OABkuyhSjYHOyn0iLk72YqRaWs8neclBpp6Olx8HUHUt6BtHH/MupLX+Of knS9pJxigVxQ9/ZQNuak2Oke4HwTbf+gAuOzvs6Tg0hKfd1b4oryvB3GZZ4NPWDigYSe y+cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713188624; x=1713793424; 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=UwkkSUi1b63q//pF6FQeQvdmd+qn9ODc8sgiig3GYAI=; b=WVGls7tYLbb5Fn+fgVKV3CbaF6fAXOVup9D2OC0Z8oyNGyXpZnXjtANtHTtLtcacJT OZpi+g553T2bMOfA5R5ZnTjBpz03qib3DUUOjf3Qc54sXnKZ0MbkPQVyJGrai2WuzygO LMQgh3LNGKnBH08wSoMeU+POiVWOot9pDFvdfyIbn2RH4qjbfjDjprSZIOfvCLgnoNym n7EPZ4fD3u7LoIdb0fI4hzkAAsE27ZkaUV2iEf4VMMZbjV6bgCfacohJShiTIGhRol08 cgM8N0rkNBsvIACPeCM4vWHxIy8XwrZB9Yia4EpZVYGIRYE4vb6hyei+EzdH2iGz9xwx ssjw== X-Gm-Message-State: AOJu0YxUxwtAOt8E537mpLryKiWtUAY5GJW6qZcc1xkVY+JU8VTJLs7l uvnmjwzrhP5rJj2iLk5QDQFCxz6AOxWMoOVY17LlOh+QkCat5HgJjKiJSKpomlZU7UMeQn9kZxR x X-Google-Smtp-Source: AGHT+IHCajOj0FuGEo04PJviti82GD44O7FVKp5cCtY8Jd4x5CQ6Bg6wQtmRbFu2KIZ47oJzjEbhUA== X-Received: by 2002:a05:651c:231:b0:2d8:36ee:350d with SMTP id z17-20020a05651c023100b002d836ee350dmr6285867ljn.16.1713188624523; Mon, 15 Apr 2024 06:43:44 -0700 (PDT) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id p11-20020a2eb98b000000b002d43737e2d7sm1297988ljp.30.2024.04.15.06.43.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 06:43:44 -0700 (PDT) From: Linus Walleij Date: Mon, 15 Apr 2024 15:43:28 +0200 Subject: [PATCH v5 09/10] ARM: hw_breakpoint: Handle CFI breakpoints MIME-Version: 1.0 Message-Id: <20240415-arm32-cfi-v5-9-ff11093eeccc@linaro.org> References: <20240415-arm32-cfi-v5-0-ff11093eeccc@linaro.org> In-Reply-To: <20240415-arm32-cfi-v5-0-ff11093eeccc@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-20240415_064346_659010_54D3833F X-CRM114-Status: GOOD ( 18.05 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 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..ce7c152dd6e9 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,32 @@ 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 requires compiler work */ + unsigned long target = 0; + u32 type = 0; + + switch (report_cfi_failure(regs, instruction_pointer(regs), &target, type)) { + 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 +959,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. */ } -- 2.44.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel