Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp622053iog; Fri, 17 Jun 2022 09:54:31 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uDev0mZ0UUmmLkNEgJrlV4zkb+J1qmGzz9wg3qV8vM0P/htMbf+SineQLD6IwUHFp0lw8I X-Received: by 2002:a05:6a00:1513:b0:51c:4b66:a93d with SMTP id q19-20020a056a00151300b0051c4b66a93dmr10919495pfu.27.1655484871503; Fri, 17 Jun 2022 09:54:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655484871; cv=none; d=google.com; s=arc-20160816; b=BPvZCYUVhaeMJU5l/8BujCAPDnZHxIDe8JKLfx2uvZN9/yqgur+4dPS/ISYh0rCUf2 GTbGjVwxUuPeJ+Bz4m9zz0jXDpvdjnevOJn2SqEYvAtF6DUfqwLzWmR8Gu5M33yE6OIY Qo8Vp/Dnj4FbpL0lf8ilG9z6Z8HGrZjPzfWvym2uJPPgokmbdg7FMrxg4cUG6Mt5qil8 mMxb/rsIF1kKi277qX+36h30C2jBugQTy/xeb7Dole2DZ90q9s7GUgbmSBm6EstPlsbW suGWc8oQ7nUqx8Blxmx49z3oyzIbjNpOGfoFrCIIRoLRFU43X1VP1vsP3jp7CUhea6V2 n6nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=9u51CE6MuvCEUihOmshcU90j7YwKJB4q4TkXfgvMtC0=; b=dHvqhVMsork2A97/9RO7tUCN2r8We3d5di11QiAujbGwee5250DQLxEVyLw1qACx+m +wrM08cIwKnbYgqiOzKmyFOzSB9E9VVjOxm993ROsh60hog7H4JdBpusDeFKmVsG4TeK p5ttoEMJppMVMso5tT47nLK9PWth5kEhpfeiI50447HJf99FKAcPnkgpXgTGp6FnkWR8 VbRHGgqaPh0OFJVEdF+n886lz3l7wrx4D1wYxnsmwC+r+w2tMGAyyKnjzTS723lxRCiV dykabeRPdxk/auoVLdA7kHak44P9kx9/kyASzIcVbOgSfhMPzCZ3O2Y7NRVhCfHxtTF+ JzrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=h3N1IudI; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x22-20020a17090a789600b001dfa74a82fasi8273876pjk.176.2022.06.17.09.54.19; Fri, 17 Jun 2022 09:54:31 -0700 (PDT) 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; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=h3N1IudI; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383317AbiFQQcz (ORCPT + 99 others); Fri, 17 Jun 2022 12:32:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383300AbiFQQcy (ORCPT ); Fri, 17 Jun 2022 12:32:54 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7075CB848 for ; Fri, 17 Jun 2022 09:32:52 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id o6so4318818plg.2 for ; Fri, 17 Jun 2022 09:32:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9u51CE6MuvCEUihOmshcU90j7YwKJB4q4TkXfgvMtC0=; b=h3N1IudI9o50D0w+VbgATrzUm5tzxG8m5/3TIip9oD12Q5E2hSppZ8I3Ao/7MHZf3r GSGupEluqHTR9AxlhXqS4ak/vmOIqx5dzkTrmQJNcfNmygbJ8LZ0iHf/qodAaRl9yY62 Ds0bfB68mVpCbq5/QCsnTbps1RUPWyspfVItNBtc2D68V7KC4ydDei4dmGTDGj1CO5SR fHr2I7WDXpZsqM8hTWO7JcJmwiUeJTwEMtJsb/uoRIvHAAPafc6Gq04hi4GOuNSpXTom m8ugJOGII/Icyer8wllOpTsgl/X2ilNol3br2svETk6je0Bnncg7LRPnHf9Fii3izYDf aGuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9u51CE6MuvCEUihOmshcU90j7YwKJB4q4TkXfgvMtC0=; b=mk2Ct8wPtq8BiXT7EtjPrTf92LZMZBiCDxaPe7lwBXCG77T15xpq1mGXzCws743ye2 Uf/Un1o14kZc18pY/I5e1XxqYMbL4wYB2w5oHI4qMNSSuH2LJFW2Cnrzodfkzh/3tWO7 SqyqY0SE13k2t9kMGtm8aigLyystvMvukVpmG3uQmw1J+ywzOKs7MfWzYPzZ8/5oeRUo P3H6oE3JMUkl7kHNwi0YuSwNY1f0syBMXMyM7xf5gawCfB8rvI0qqQA6kQKQg5JD16yX D/LEscqQ9Qo6InDq7XXUdaI1PG5WVQ/SJGAfte9p/i1nji82/HlscI/HaCYEL6GKxmUF n4Sw== X-Gm-Message-State: AJIora84L5JAkDG+Lv1KgPrNxVwnqDnNUyvK8UbHSF7dI67JmWlbQJJi DOG4Q5III8WWD1z9sBfNDl//z2lyem+DizDtfO0RsA== X-Received: by 2002:a17:902:e552:b0:163:6a5e:4e08 with SMTP id n18-20020a170902e55200b001636a5e4e08mr10171956plf.130.1655483571756; Fri, 17 Jun 2022 09:32:51 -0700 (PDT) MIME-Version: 1.0 References: <20220615213834.3116135-1-daolu@rivosinc.com> <202206152234.DE7FC51F9@keescook> In-Reply-To: <202206152234.DE7FC51F9@keescook> From: Dao Lu Date: Fri, 17 Jun 2022 09:32:41 -0700 Message-ID: Subject: Re: [PATCH] arch/riscv: Add support for STACKLEAK gcc plugin To: Kees Cook Cc: linux-kernel@vger.kernel.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Ard Biesheuvel , Guo Ren , Geert Uytterhoeven , Arnd Bergmann , Tong Tiangen , Mark Rutland , Changbin Du , Vincent Chen , "open list:RISC-V ARCHITECTURE" , "open list:EXTENSIBLE FIRMWARE INTERFACE (EFI)" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 Thanks Kees. > stackleak_check_alloca does not exist. Was this maybe from an older commit log? Yeah it was from an older version and was later removed, I will take it away from the commit message in the next version. Thanks, Dao On Wed, Jun 15, 2022 at 10:36 PM Kees Cook wrote: > > On Wed, Jun 15, 2022 at 02:38:29PM -0700, Dao Lu wrote: > > This adds support for STACKLEAK gcc plugin to ricv by implementing > > typo: riscv > > > stackleak_check_alloca, based heavily on the arm64 version, and adding > > stackleak_check_alloca does not exist. Was this maybe from an older > commit log? > > > the two helpers used by the stackleak common code: > > current_top_of_stack() and on_thread_stack(). This also adds the missing > > helper functions for riscv, on_stack() and on_task_stack(). > > Additionally, this disables the plugin for EFI stub code for riscv. > > I can't speak to the arch-specific bits here, but if this passes the > current LKDTM tests, then that should be a good indication that it's > working. :) > > > > > Signed-off-by: Dao Lu > > --- > > arch/riscv/Kconfig | 1 + > > arch/riscv/include/asm/processor.h | 18 ++++++++++++++++++ > > arch/riscv/include/asm/stacktrace.h | 27 +++++++++++++++++++++++++++ > > arch/riscv/kernel/entry.S | 3 +++ > > drivers/firmware/efi/libstub/Makefile | 2 +- > > 5 files changed, 50 insertions(+), 1 deletion(-) > > > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > > index c22f58155948..22aa146acd25 100644 > > --- a/arch/riscv/Kconfig > > +++ b/arch/riscv/Kconfig > > @@ -80,6 +80,7 @@ config RISCV > > select HAVE_ARCH_MMAP_RND_BITS if MMU > > select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT > > select HAVE_ARCH_SECCOMP_FILTER > > + select HAVE_ARCH_STACKLEAK > > select HAVE_ARCH_TRACEHOOK > > select HAVE_ARCH_TRANSPARENT_HUGEPAGE if 64BIT && MMU > > select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE > > diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h > > index 21c8072dce17..3a7505ab7f58 100644 > > --- a/arch/riscv/include/asm/processor.h > > +++ b/arch/riscv/include/asm/processor.h > > @@ -85,6 +85,24 @@ int riscv_of_parent_hartid(struct device_node *node); > > extern void riscv_fill_hwcap(void); > > extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src); > > > > +/* > > + * For CONFIG_GCC_PLUGIN_STACKLEAK > > + * > > + * These need to be macros because otherwise we get stuck in a nightmare > > + * of header definitions for the use of task_stack_page. > > + */ > > + > > +struct stack_info { > > + unsigned long low; > > + unsigned long high; > > +}; > > + > > +/* > > + * The top of the current task's task stack > > + */ > > +#define current_top_of_stack() ((unsigned long)current->stack + THREAD_SIZE) > > +#define on_thread_stack() (on_task_stack(current, current_stack_pointer, 1, NULL)) > > + > > #endif /* __ASSEMBLY__ */ > > > > #endif /* _ASM_RISCV_PROCESSOR_H */ > > diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/stacktrace.h > > index 3450c1912afd..afb66b677b6a 100644 > > --- a/arch/riscv/include/asm/stacktrace.h > > +++ b/arch/riscv/include/asm/stacktrace.h > > @@ -16,4 +16,31 @@ extern void notrace walk_stackframe(struct task_struct *task, struct pt_regs *re > > extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task, > > const char *loglvl); > > > > +static inline bool on_stack(unsigned long sp, unsigned long size, > > + unsigned long low, unsigned long high, > > + struct stack_info *info) > > +{ > > + if (!low) > > + return false; > > + > > + if (sp < low || sp + size < sp || sp + size > high) > > + return false; > > + > > + if (info) { > > + info->low = low; > > + info->high = high; > > + } > > + return true; > > +} > > + > > +static inline bool on_task_stack(const struct task_struct *tsk, > > + unsigned long sp, unsigned long size, > > + struct stack_info *info) > > +{ > > + unsigned long low = (unsigned long)task_stack_page(tsk); > > + unsigned long high = low + THREAD_SIZE; > > + > > + return on_stack(sp, size, low, high, info); > > +} > > + > > #endif /* _ASM_RISCV_STACKTRACE_H */ > > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > > index 2e5b88ca11ce..65d441cb560f 100644 > > --- a/arch/riscv/kernel/entry.S > > +++ b/arch/riscv/kernel/entry.S > > @@ -264,6 +264,9 @@ ret_from_exception: > > bnez s0, resume_kernel > > > > resume_userspace: > > +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK > > + call stackleak_erase > > +#endif > > /* Interrupts must be disabled here so flags are checked atomically */ > > REG_L s0, TASK_TI_FLAGS(tp) /* current_thread_info->flags */ > > andi s1, s0, _TIF_WORK_MASK > > diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile > > index d0537573501e..5e1fc4f82883 100644 > > --- a/drivers/firmware/efi/libstub/Makefile > > +++ b/drivers/firmware/efi/libstub/Makefile > > @@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ > > -fno-builtin -fpic \ > > $(call cc-option,-mno-single-pic-base) > > cflags-$(CONFIG_RISCV) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ > > - -fpic > > + -fpic $(DISABLE_STACKLEAK_PLUGIN) > > > > cflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt > > > > -- > > 2.25.1 > > > > -- > Kees Cook