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 8F472C433F5 for ; Thu, 3 Mar 2022 19:10:52 +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:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fMDgIgsP8ogJzcg4NdnQ1cRnezy4SQOZ72nAIc6DL6Q=; b=Uy4LWLZ0x5tdG4 +a9ONMxxhveDVnBIWam8H0uUG8qGVxReBng4oHLfol1+oNh4+0nwwSNTQzP81OfVlcRI6Z7yWyyUL 2PtTuWUCm4Zok7RNEWgRSnO+kxMboeCqgfPSmXOJ40L+bEqakqMpMRhM+yNsSsftOz528lB9PxY5k YIbnD++pj789cNpiKXHf29/Cn+YJAIWGAkOOTg75abRTeEc2hRaTPuGnWBem4X7UqI+GEiQHJS80g zJV0mVEkon2rUo61sO7IIAntPE5cJcX7hdrfzi9Y8VB4gyNL7VDxDGxUFXgUW1JTTjOZsgPOMbELe R2+YhYxRBBhYFAIwiLjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nPqpY-007XY9-8F; Thu, 03 Mar 2022 19:09:40 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nPqpU-007XWy-KL for linux-arm-kernel@lists.infradead.org; Thu, 03 Mar 2022 19:09:38 +0000 Received: by mail-pj1-x102b.google.com with SMTP id v4so5432429pjh.2 for ; Thu, 03 Mar 2022 11:09:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=j7TJLOnN6hgODDuo1callp0lhdyoclIZFm0WJljlY8s=; b=fVnhMSDNmlqJL+mlE4nr2PgQuPT9LGKEI5pLg6UMKecnlAeYN/FVWHDwnXJhWjrE/n DmINVFplNUArYTWf3s3r3hSQ8aSUka6JL/mICjXhADVYWnkAJ2AMABSG1FR+9aEbBXaQ MW2bjvj5LntyCBXcjwdKceOmtzdFX2X3FBxps= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=j7TJLOnN6hgODDuo1callp0lhdyoclIZFm0WJljlY8s=; b=1EVLEjb0gweqAoo8EwmxRVwdckxjieRp8gWqLxTRETkkLNLFBuhUE14Z+W1AvCDdCn 1tU2kVTUDD4cIbT9DHf4yw5QahfemtgABwGFYnt5707JXueM7MCMPdpTf6WdujjNKktQ N+NMcczMwW6nx7Ntc38hMOOUz3TyQyc3odH1Jel/u3uBQdguxpQ8y0vpEh/HA2PFW0gV IoVKvfUlsUIrEsRXmE4yJq3/veWLmUIlsf2NJspheAojvwX+ATW3udOGpxg5q6fFy+Ug qlCxgwmqbLCU0W1dNhDN+7imKwZPmAQzzpb5oNWZneOT11cfYwXYo6rD3iKH9LMWwbz1 wB2g== X-Gm-Message-State: AOAM5322dZenMZ3MVk0BBUsCb9VEBJfeuGiM/gi/l64W7PFRKkSaXt29 XO4pzA4xiybcp0Gb55sKG6ol7A== X-Google-Smtp-Source: ABdhPJyfvwYzCoBtdXSSCDsYpSdESFLlaFH3e43QTFvMWUmnNTc6eJPUCpEnqo8wvQrcrYNq7n2KPQ== X-Received: by 2002:a17:90b:1c11:b0:1bf:c13:e4a3 with SMTP id oc17-20020a17090b1c1100b001bf0c13e4a3mr5279740pjb.188.1646334575652; Thu, 03 Mar 2022 11:09:35 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id d3-20020a056a00244300b004bc9397d3d0sm3198347pfj.103.2022.03.03.11.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 11:09:35 -0800 (PST) Date: Thu, 3 Mar 2022 11:09:34 -0800 From: Kees Cook To: Dan Li 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 Subject: Re: [PATCH v3 2/2] lkdtm: Add Shadow Call Stack tests Message-ID: <202203031105.A1B4CAE6@keescook> References: <20220303073340.86008-1-ashimida@linux.alibaba.com> <20220303074339.86337-1-ashimida@linux.alibaba.com> <202203031010.0A492D114@keescook> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <202203031010.0A492D114@keescook> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220303_110936_715463_675466CC X-CRM114-Status: GOOD ( 17.66 ) 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 On Thu, Mar 03, 2022 at 10:42:45AM -0800, Kees Cook wrote: > Though, having the IS_ENABLED in there makes me wonder if this test > should instead be made _survivable_ on failure. Something like this, > completely untested: > > > #ifdef CONFIG_ARM64 > static noinline void lkdtm_scs_set_lr(unsigned long *addr) > { > unsigned long **lr = (unsigned long **)__builtin_frame_address(0) + 1; > *lr = addr; > } > > /* Function with __noscs attribute clears its return address. */ > static noinline void __noscs lkdtm_noscs_set_lr(unsigned long *addr) > { > unsigned long **lr = (unsigned long **)__builtin_frame_address(0) + 1; > *lr = addr; > } > #endif > > > void lkdtm_CFI_BACKWARD_SHADOW(void) > { > #ifdef CONFIG_ARM64 > > /* Verify the "normal" condition of LR corruption working. */ > do { > /* Keep label in scope to avoid compiler warning. */ > if ((volatile int)0) > goto unexpected; > > pr_info("Trying to corrupt lr in a function without scs protection ...\n"); > lkdtm_noscs_set_lr(&&expected); > > unexpected: > pr_err("XPASS: Unexpectedly survived lr corruption without scs?!\n"); > break; > > expected: > pr_err("ok: lr corruption redirected without scs.\n"); > } while (0); > > > do { > /* Keep labe in scope to avoid compiler warning. */ > if ((volatile int)0) > goto good_scs; > > pr_info("Trying to corrupt lr in a function with scs protection ...\n"); > lkdtm_scs_set_lr(&&bad_scs); > > good_scs: > pr_info("ok: scs takes effect.\n"); > break; > > bad_scs: > pr_err("FAIL: return address rewritten!\n"); > pr_expected_config(CONFIG_SHADOW_CALL_STACK); > } while (0); > #else > pr_err("XFAIL: this test is arm64-only\n"); > #endif > } > > 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. -- Kees Cook _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel