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 D9F51C433F5 for ; Mon, 7 Mar 2022 15:19:27 +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-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uNxIEepkKsfNGg6wrUyJGKKtheIhX2OY4JDfHP7tlZY=; b=pdAzQxd8jFXn/L H+toujUbGEMdh2joRdPeRwgydELeMR0TF7aO45s6qDwV8LISVV9/46LrAw5DIVkuZsKtepB13mWCe BF74+V/J8mwO4w0NXopbgUWrmvqOrXWNF9L2y3+lDbMGOCjnaytoFSUt5hhdQb9PqPeS65I9roGEH HrjLjrBGGcq3R59BLNcCJXWaIxmtKpw6SV/5hSCIXcanWw8cHanfFSZbLrfxiShwbDRvA9PXPraAq 6Jtcq/w504HDObmSr+2uf6JJLB72kF4I9Eud/HGjxN8cUVa+GA3nTVg/Z1Th8Sl5GPvof/AOkuAVF APguw8vu3N6mi/L1SEyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRF7d-000aIa-Ep; Mon, 07 Mar 2022 15:18:05 +0000 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRF7Z-000aHP-Dt for linux-arm-kernel@lists.infradead.org; Mon, 07 Mar 2022 15:18:03 +0000 X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R971e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=e01e01424; MF=ashimida@linux.alibaba.com; NM=1; PH=DS; RN=23; SR=0; TI=SMTPD_---0V6XN-TY_1646666269; Received: from 192.168.193.155(mailfrom:ashimida@linux.alibaba.com fp:SMTPD_---0V6XN-TY_1646666269) by smtp.aliyun-inc.com(127.0.0.1); Mon, 07 Mar 2022 23:17:51 +0800 Message-ID: <92a767c4-09e1-8783-2581-9848bb72890d@linux.alibaba.com> Date: Mon, 7 Mar 2022 07:16:36 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: [PATCH v3 2/2] lkdtm: Add Shadow Call Stack tests Content-Language: en-US To: Kees Cook Cc: akpm@linux-foundation.org, arnd@arndb.de, catalin.marinas@arm.com, gregkh@linuxfoundation.org, linux@roeck-us.net, luc.vanoostenryck@gmail.com, elver@google.com, mark.rutland@arm.com, masahiroy@kernel.org, ojeda@kernel.org, nathan@kernel.org, npiggin@gmail.com, ndesaulniers@google.com, samitolvanen@google.com, shuah@kernel.org, tglx@linutronix.de, will@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org References: <20220303073340.86008-1-ashimida@linux.alibaba.com> <20220303074339.86337-1-ashimida@linux.alibaba.com> <202203031010.0A492D114@keescook> <202203031105.A1B4CAE6@keescook> From: Dan Li In-Reply-To: <202203031105.A1B4CAE6@keescook> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220307_071801_710320_D7022199 X-CRM114-Status: GOOD ( 20.95 ) 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-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 3/3/22 11:09, Kees Cook wrote: > On Thu, Mar 03, 2022 at 10:42:45AM -0800, Kees Cook wrote: >> And we should, actually, be able to make the "set_lr" functions be >> arch-specific, leaving the test itself arch-agnostic.... > > Yeah, as a tested example, this works for x86_64, and based on what you > had, I'd expect it to work on arm64 too: > > #include > > static __attribute__((noinline)) > void set_return_addr(unsigned long *expected, unsigned long *addr) > { > /* Use of volatile is to make sure final write isn't seen as a dead store. */ > unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1; > > /* Make sure we've found the right place on the stack before writing it. */ > if (*ret_addr == expected) > *ret_addr = addr; > } > > volatile int force_label; > int main(void) > { > do { > /* Keep labels in scope. */ > if (force_label) > goto normal; > if (force_label) > goto redirected; > > set_return_addr(&&normal, &&redirected); > normal: > printf("I should be skipped\n"); > break; > redirected: > printf("Redirected\n"); > } while (0); > > return 0; > } > > > It does _not_ work under Clang, though, which I'm still looking at. > The following code seems to work fine under clang/gcc, x86_64/aarch64 (also tested in lkdtm_CFI_BACKWARD_SHADOW): #include static __attribute__((noinline)) void set_return_addr(unsigned long *expected, unsigned long *addr) { /* Use of volatile is to make sure final write isn't seen as a dead store. */ unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1; /* Make sure we've found the right place on the stack before writing it. */ if(*ret_addr == expected) *ret_addr = (addr); } static volatile int force_label; int main(void) { void *array[] = {0, &&normal, &&redirected}; if (force_label) { /* Call it with a NULL to avoid parameters being treated as constants in -02. */ set_return_addr(NULL, NULL); goto * array[force_label]; } do { set_return_addr(&&normal, &&redirected); normal: printf("I should be skipped\n"); break; redirected: printf("Redirected\n"); } while (0); return 0; } But currently it still crashes when I try to enable "-mbranch-protection=pac-ret+leaf+bti". Because the address of "&&redirected" is not encrypted under pac, the autiasp check will fail when set_return_addr returns, and eventually cause the function to crash when it returns to "&&redirected" ("&&redirected" as a reserved label always seems to start with a bti j insn). For lkdtm, if we're going to handle both cases in one function, maybe it would be better to turn off the -mbranch-protection=pac-ret+leaf+bti and maybe also turn off -O2 options for the function :) Thanks, Dan. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel