Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2111431pxm; Fri, 4 Mar 2022 09:34:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJy0gQJOFtplO2LxtNlkwrJuOiT9BUZPV4oF1AQCVxz2x05I+nNMZT3rsgSVjEqBcL6uIYz7 X-Received: by 2002:a05:6402:40c4:b0:412:d697:187 with SMTP id z4-20020a05640240c400b00412d6970187mr40703329edb.66.1646415250842; Fri, 04 Mar 2022 09:34:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646415250; cv=none; d=google.com; s=arc-20160816; b=Ftug9dWKi06NHPNIK8ZWCOC9gBCd9MVnzAsJvHuoGmfc6MrcXftt1/YnUg+MniJxjH zL816xgm+wPN4K5SM1cBtHug1ZvaNUlQWah/dvcnq+hEk+kmQ9Jx2xdy3bjEn/dRJrDN of/jttfTF7QzPm0hHG//vU1ZxMudXbQBYkZjWYCalafLEOGGJ3yk3qGpmZUTIi/p8SgZ LBiD75IEjnuCmGsx0+AT+i9sbHtuu9w7q2OBa3C3xtD1xOAoOON4q9HVzfkcxJAAnMdX KvqOozghMHML0cR35jqhWaGE2Eqf+Dp2TQ0uGa8sERC+0RH0+O2ik7Sml2+wL7OguRAB slMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:references :cc:to:from:content-language:subject:user-agent:mime-version:date :message-id; bh=cM5C1RhGiHsgxdqjVC6tI44IPoia+WSPtUx53TUs7x0=; b=zUOS3Aq5Vv1jZ3pKd0cqwPj16ePLMhukDO8F/xBrn43lyUDNYMlnR0MRJ14/8ORiku zvHxbrXIFlJEX/e6vKBWPFNf4FBcid4ubu7axHZ8CPC808WGQT1lgg4rSEfjtg1OZEAR nVLtjqKX6Ron4HqzjalVpjhwrOxoHo5snbHPCvVvihvlfikXmHgp/9mx+b4swXPO2Tug H2pwcNeb0VqtjGsp2kqS2/kTCDguYanf9PgBDKUb+XyR8hgWmyrB0/vRnVx8Ujouaqp5 Dhi7f5UV1EP2u8Re5XmHkXd/N+2P7XkWiKrDc0x6JmCF/96KKi6HZUbvNRGV1DpTSu2K 8RSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o26-20020a1709061d5a00b006d13c83d8b8si3119673ejh.700.2022.03.04.09.33.47; Fri, 04 Mar 2022 09:34:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239994AbiCDPCP (ORCPT + 99 others); Fri, 4 Mar 2022 10:02:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229934AbiCDPCO (ORCPT ); Fri, 4 Mar 2022 10:02:14 -0500 Received: from out199-12.us.a.mail.aliyun.com (out199-12.us.a.mail.aliyun.com [47.90.199.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48FCA177D24; Fri, 4 Mar 2022 07:01:25 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R841e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04407;MF=ashimida@linux.alibaba.com;NM=1;PH=DS;RN=23;SR=0;TI=SMTPD_---0V6CkI3R_1646406078; Received: from 192.168.193.155(mailfrom:ashimida@linux.alibaba.com fp:SMTPD_---0V6CkI3R_1646406078) by smtp.aliyun-inc.com(127.0.0.1); Fri, 04 Mar 2022 23:01:19 +0800 Message-ID: <48268e7c-a912-c648-be69-b5e41639bf3e@linux.alibaba.com> Date: Fri, 4 Mar 2022 07:01:18 -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 From: Dan Li 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> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/4/22 06:54, Dan Li wrote: > > > On 3/3/22 11:09, Kees Cook wrote: >> 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: >>> >>> >>> 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; > > From the assembly code, it seems that "&&normal" does't always equal > to the address of label "normal" when we use clang with -O2. > >> redirected: >>          printf("Redirected\n"); >>      } while (0); >> > > The address of "&&redirected" may appear in the middle of the assembly > instructions of the printf. If we unconditionally jump to "&&normal",> it may crash directly because x0 is not set correctly. Sorry, it should be: The address of "&&redirected" may appear in the middle of the assembly instructions of the printf. If we unconditionally jump to "&&redirected", it may crash directly because x0 of printf is not set correctly. Thanks, Dan. > >>      return 0; >> } >> >> >> It does _not_ work under Clang, though, which I'm still looking at. >> > > AFAICT, maybe we could specify -O0 optimization to bypass this. > > > BTW: > Occasionally found, the following code works correctly, but i think > it doesn't solve the issue :) > > #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"); >         printf("\n");                //add a new printf >     } while (0); > >     return 0; > }