Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp600863lqs; Tue, 5 Mar 2024 10:36:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVD+OZAvBAeZdROMaWwcLWeAr+NwjOJx6fbRdO1fptwtY2qITKxYDo9To8vz/Cgo+sLPd4lqOWriR9atcy0voDX7ifG1JybNNpX3SIjdg== X-Google-Smtp-Source: AGHT+IGIZXfmEcV1aqW7OyM0crxoEDlinl4IIWEjmsWFRIz+Mt5dTgJQUFdYbfJ+VIMJNIkyhve5 X-Received: by 2002:a05:6a21:a58f:b0:1a1:4cb5:cac1 with SMTP id gd15-20020a056a21a58f00b001a14cb5cac1mr2814795pzc.6.1709663784361; Tue, 05 Mar 2024 10:36:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709663784; cv=pass; d=google.com; s=arc-20160816; b=TJeGotdP3mPDY8ERxAOEBVVWcrbaECSfgLV4Pqpx7FL9wJ8YOkr6JqCoxw4bGosx2S +0qHepgGM1KvIfZDZQ51azSTArCpZ3XGpaKhoauHe+UdmY5LvLc4SakuRB9u/7rVTAKc o0Claup1Ha4n1igfbOlCi8sBQKsy/1ctHMAjkIV6gZpaxXvJJY4adYAK/jRp0yqUrLLp Aq/DxcGP1zsZR1uASj1/afxtGsMmwOjd2FrlBzkCu8EUJ4lfZ4jnUW3gS3jVziL9EyDo dgLrKtSVWbMg9AqjL9n5Iow3G2F89g5mSdsetXmqL4PaJo8zLeLc1SMtdLIhPF/C98w0 wcSQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=TlOX6/F8b6UkW3S3mSd4t8BdzdGGSllMNgb4BZ8Ecuo=; fh=8TDVEorlwyaAnu4U1lBPMplPTScNRbNuUFeto6bwWv0=; b=kc2AhaTZXTgz8c+vN0IVjEk8u85zPIVmGXVOi2bWtVxep26pfjr7NjEgQKQV/4ZZXN txaWMRJTm6zqMcMxZs01tbNJbThN8hRBuKyViQxE35Zveq9eRqDx6h/Y9DDW+PnIN+GW 2cc7axXdrECz9/80/mpnS3SwOeu4AZpry49Zd6c5IjkjWg9rm16cqdn+L3v0MBvzrUyy lClDMm2XbDeD0ZI1QbJifpCPuLQ0oS/F7MDEnE5mwfztoLfpZvIwOze8vI2m+dm3+z4K knbkUa5CZfUbOt5ESSLxleENMXDp8og84wpY/I9gngzr54u3WgL0ecxQBCGSwNQdkhJe TeRQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WyuOFdij; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-92799-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92799-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id o75-20020a62cd4e000000b006e58d592579si10230950pfg.290.2024.03.05.10.36.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 10:36:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-92799-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WyuOFdij; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-92799-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92799-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 552A7B2AC61 for ; Tue, 5 Mar 2024 17:58:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 08B9017BCE; Tue, 5 Mar 2024 17:58:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WyuOFdij" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00CAB182B3; Tue, 5 Mar 2024 17:58:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709661529; cv=none; b=jC9m2h+eDcw3r14pbA71OWPDrzN8uCX1RnqRH/diARZgZzUFHzKy+m7NiSC5hrVkLuV/YJGg+BDBLtw4UqhyoftYXeRT9IHPoKymobwzHyJtPClBlAhCtmpBd5pSRzXrPpve835guVX3f4PxtZ4Sh6SR8cQrAb2EpWoepL6nKto= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709661529; c=relaxed/simple; bh=n/toLIPHbeaoCIED5/1fJO2jWwcxruDUgbg++Dsbijk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Cq5YhoSgzeYonvlWsLRQHkxsBhrSHj698GUY9Hpr7gSFy72Y262VxZKHhrQ+zJJCGOmNEO3VYDKEhhKEZXzjWv6yU6dV4ptf/xndq5v4qtcdJAlmBtoLaUpRVCIkPHIwpQdTAVPBh1/GJQQP1FpPJGwp9Z3rz+xFzPuWUf1WFuw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WyuOFdij; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D08BBC433C7; Tue, 5 Mar 2024 17:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709661528; bh=n/toLIPHbeaoCIED5/1fJO2jWwcxruDUgbg++Dsbijk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=WyuOFdij78BbskVgyqWFM/kzuBAvozgJDjD+TyHF7hPV9/yM2i3UYBjxrJkgZGhdJ hW0lISaHBCuCz3x58q3LcNLqE0iGTM41jWlFKhXlgzZsm7d6YIn+vslueGkx6QnfSR GWWKGicIcHu12H3OYH9O6JyEOP+wleGX4Ylc32EUYlgbQ5v+9gp+D5Hkroim2V4IW2 wOij1bmoo7i0nbyfE4LownVfl/7lY+dZvCOeF3nG8tj5rWlBvW3fZPAawsdL81QtiU v2w6UlebkcHRtTMGiwSY+kewSqrytcnmtNwKciHGI3gOJ98z/1rPmc1zQ/HBtB3c7J IHyTn8DDq4gew== Date: Tue, 5 Mar 2024 09:58:46 -0800 From: Josh Poimboeuf To: Jiangfeng Xiao Cc: Kees Cook , Jann Horn , gustavoars@kernel.org, akpm@linux-foundation.org, peterz@infradead.org, dave.hansen@linux.intel.com, kirill.shutemov@linux.intel.com, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-mm@kvack.org, nixiaoming@huawei.com, kepler.chenxin@huawei.com, wangbing6@huawei.com, wangfangpeng1@huawei.com, douzhaolei@huawei.com, Russell King , linux-arm-kernel@lists.infradead.org, Ard Biesheuvel Subject: Re: [PATCH] usercopy: delete __noreturn from usercopy_abort Message-ID: <20240305175846.qnyiru7uaa7itqba@treble> References: <1709516385-7778-1-git-send-email-xiaojiangfeng@huawei.com> <202403040938.D770633@keescook> <77bb0d81-f496-7726-9495-57088a4c0bfc@huawei.com> <202403050129.5B72ACAA0D@keescook> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: > >> For the usercopy_abort function, whether '__noreturn' is added > >> does not affect the internal behavior of the usercopy_abort function. > >> Therefore, it is recommended that '__noreturn' be deleted > >> so that backtrace can work properly. > > > > This isn't acceptable. Removing __noreturn this will break > > objtool's processing of execution flow for livepatching, IBT, and > > KCFI instrumentation. These all depend on an accurate control flow > > descriptions, and usercopy_abort is correctly marked __noreturn. __noreturn also has the benefit of enabling the compiler to produce more compact code for callees. > Thank you for providing this information. > I'll go back to further understand how __noreturn is used > in features such as KCFI and livepatching. Adding ARM folks -- see https://lkml.kernel.org/lkml/1709516385-7778-1-git-send-email-xiaojiangfeng@huawei.com for the original bug report. This is an off-by-one bug which is common in unwinders, due to the fact that the address on the stack points to the return address rather than the call address. So, for example, when the last instruction of a function is a function call (e.g., to a noreturn function), it can cause the unwinder to incorrectly try to unwind from the function *after* the callee. For ORC (x86), we fixed this by decrementing the PC for call frames (but not exception frames). I've seen user space unwinders do similar, for non-signal frames. Something like the following might fix your issue (completely untested): diff --git a/arch/arm/include/asm/stacktrace.h b/arch/arm/include/asm/stacktrace.h index 360f0d2406bf..4891e38cdc1f 100644 --- a/arch/arm/include/asm/stacktrace.h +++ b/arch/arm/include/asm/stacktrace.h @@ -21,9 +21,7 @@ struct stackframe { struct llist_node *kr_cur; struct task_struct *tsk; #endif -#ifdef CONFIG_UNWINDER_FRAME_POINTER bool ex_frame; -#endif }; static __always_inline @@ -37,9 +35,8 @@ void arm_get_current_stackframe(struct pt_regs *regs, struct stackframe *frame) frame->kr_cur = NULL; frame->tsk = current; #endif -#ifdef CONFIG_UNWINDER_FRAME_POINTER - frame->ex_frame = in_entry_text(frame->pc); -#endif + frame->ex_frame = !!regs; + } extern int unwind_frame(struct stackframe *frame); diff --git a/arch/arm/kernel/stacktrace.c b/arch/arm/kernel/stacktrace.c index 620aa82e3bdd..caed7436da09 100644 --- a/arch/arm/kernel/stacktrace.c +++ b/arch/arm/kernel/stacktrace.c @@ -154,9 +154,6 @@ static void start_stack_trace(struct stackframe *frame, struct task_struct *task frame->kr_cur = NULL; frame->tsk = task; #endif -#ifdef CONFIG_UNWINDER_FRAME_POINTER - frame->ex_frame = in_entry_text(frame->pc); -#endif } void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, @@ -167,6 +164,7 @@ void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, if (regs) { start_stack_trace(&frame, NULL, regs->ARM_fp, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc); + frame.ex_frame = true; } else if (task != current) { #ifdef CONFIG_SMP /* @@ -180,6 +178,7 @@ void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, thread_saved_sp(task), 0, thread_saved_pc(task)); #endif + frame.ex_frame = false; } else { here: start_stack_trace(&frame, task, @@ -187,6 +186,7 @@ void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, current_stack_pointer, (unsigned long)__builtin_return_address(0), (unsigned long)&&here); + frame.ex_frame = false; /* skip this function */ if (unwind_frame(&frame)) return; diff --git a/arch/arm/kernel/traps.c b/arch/arm/kernel/traps.c index 3bad79db5d6e..46a5b1eb3f0a 100644 --- a/arch/arm/kernel/traps.c +++ b/arch/arm/kernel/traps.c @@ -84,10 +84,10 @@ void dump_backtrace_entry(unsigned long where, unsigned long from, printk("%sFunction entered at [<%08lx>] from [<%08lx>]\n", loglvl, where, from); #elif defined CONFIG_BACKTRACE_VERBOSE - printk("%s[<%08lx>] (%ps) from [<%08lx>] (%pS)\n", + printk("%s[<%08lx>] (%ps) from [<%08lx>] (%pB)\n", loglvl, where, (void *)where, from, (void *)from); #else - printk("%s %ps from %pS\n", loglvl, (void *)where, (void *)from); + printk("%s %ps from %pB\n", loglvl, (void *)where, (void *)from); #endif if (in_entry_text(from) && end <= ALIGN(frame, THREAD_SIZE)) diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c index 9d2192156087..99ded32196af 100644 --- a/arch/arm/kernel/unwind.c +++ b/arch/arm/kernel/unwind.c @@ -407,7 +407,7 @@ int unwind_frame(struct stackframe *frame) { const struct unwind_idx *idx; struct unwind_ctrl_block ctrl; - unsigned long sp_low; + unsigned long sp_low, pc; /* store the highest address on the stack to avoid crossing it*/ sp_low = frame->sp; @@ -417,19 +417,22 @@ int unwind_frame(struct stackframe *frame) pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__, frame->pc, frame->lr, frame->sp); - idx = unwind_find_idx(frame->pc); + pc = frame->ex_frame ? frame->pc : frame->pc - 4; + + idx = unwind_find_idx(pc); if (!idx) { - if (frame->pc && kernel_text_address(frame->pc)) { - if (in_module_plt(frame->pc) && frame->pc != frame->lr) { + if (kernel_text_address(pc)) { + if (in_module_plt(pc) && frame->pc != frame->lr) { /* * Quoting Ard: Veneers only set PC using a * PC+immediate LDR, and so they don't affect * the state of the stack or the register file */ frame->pc = frame->lr; + frame->ex_frame = false; return URC_OK; } - pr_warn("unwind: Index not found %08lx\n", frame->pc); + pr_warn("unwind: Index not found %08lx\n", pc); } return -URC_FAILURE; } @@ -442,7 +445,7 @@ int unwind_frame(struct stackframe *frame) if (idx->insn == 1) /* can't unwind */ return -URC_FAILURE; - else if (frame->pc == prel31_to_addr(&idx->addr_offset)) { + else if (frame->ex_frame && pc == prel31_to_addr(&idx->addr_offset)) { /* * Unwinding is tricky when we're halfway through the prologue, * since the stack frame that the unwinder expects may not be @@ -451,9 +454,10 @@ int unwind_frame(struct stackframe *frame) * a function, we are still effectively in the stack frame of * the caller, and the unwind info has no relevance yet. */ - if (frame->pc == frame->lr) + if (pc == frame->lr) return -URC_FAILURE; frame->pc = frame->lr; + frame->ex_frame = false; return URC_OK; } else if ((idx->insn & 0x80000000) == 0) /* prel31 to the unwind table */ @@ -515,6 +519,7 @@ int unwind_frame(struct stackframe *frame) frame->lr = ctrl.vrs[LR]; frame->pc = ctrl.vrs[PC]; frame->lr_addr = ctrl.lr_addr; + frame->ex_frame = false; return URC_OK; } @@ -544,6 +549,7 @@ void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk, */ here: frame.pc = (unsigned long)&&here; + frame.ex_frame = false; } else { /* task blocked in __switch_to */ frame.fp = thread_saved_fp(tsk); @@ -554,11 +560,12 @@ void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk, */ frame.lr = 0; frame.pc = thread_saved_pc(tsk); + frame.ex_frame = false; } while (1) { int urc; - unsigned long where = frame.pc; + unsigned long where = frame.ex_frame ? frame.pc : frame.pc - 4; urc = unwind_frame(&frame); if (urc < 0)