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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3CE4DC433FE for ; Mon, 3 Oct 2022 21:01:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 638446B0071; Mon, 3 Oct 2022 17:01:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BFDD6B0073; Mon, 3 Oct 2022 17:01:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C4EA6B0074; Mon, 3 Oct 2022 17:01:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CE7736B0071 for ; Mon, 3 Oct 2022 17:01:33 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E6F1D140145 for ; Mon, 3 Oct 2022 20:52:34 +0000 (UTC) X-FDA: 79980836628.07.D45C84B Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf24.hostedemail.com (Postfix) with ESMTP id 7A126180017 for ; Mon, 3 Oct 2022 20:52:34 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id fw14so3862016pjb.3 for ; Mon, 03 Oct 2022 13:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=8ffUlHkSjB1PAHGl9/RU9+eAvU4W3MS9YfacwqGrUYs=; b=R8SPL4pbrpY73g71S77B4NcBTS41NGBC4RYk2iZ49PCLH1gtakir+cCS5l0lIgw9iv Xxoo8zueXn6IyedMrA0nwnXjtNWYE1TlfblH23f/SyGZTcNI8eTt7gfTQDmO/w6xx7hv XaO0n42ruxYNS9W2k5Y7IaK+KigjLUiZdFSa0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=8ffUlHkSjB1PAHGl9/RU9+eAvU4W3MS9YfacwqGrUYs=; b=lgNpuLegCSanRVME/jtWW/yCKa6rshGOAyH9FeYVtooqBR/E7OxOSXhobe9VTZxQW5 X+54F5re+mqR9/bWrd9gF5j7mJUHZ8dFOzXsERDeQcSgQluqQGkZ3QLpuQ3eIE3d/XEh bpezxPhUlF6FmbcZvjtgBXrSdPuPdcaqqXRzDbzo48tiPoJRlamjcZtmJTO2+wJpSH3G FWoeOWionGfAIj818gceLRr64xZm3jbovwkp1pzZ3Ag+ILv2erRIwOdA+1oW2gwg4TxP Le3nzQKTqOLTYxVKG4aGTFbUO2UsJK+dtt6F/Q+Snr98XuOpfPIeJYsodRJd76YJQVdh yPYQ== X-Gm-Message-State: ACrzQf23RjUzMOdqq/9FEQfrZ7Pv3qd42Osny6JWftpqBjR3kSxnPN5P YStv/T7gd50DUH6mjOtFB9d9nw== X-Google-Smtp-Source: AMsMyM5ap3cu1yiQByhDqspB/OFSaaducCy6qGDxDqArY/L8+b0bcwim0v3UCdiAZIBDDd6WZTlMiA== X-Received: by 2002:a17:902:da8a:b0:17b:df43:9235 with SMTP id j10-20020a170902da8a00b0017bdf439235mr20146511plx.137.1664830353447; Mon, 03 Oct 2022 13:52:33 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p28-20020a634f5c000000b0044db4f3f7ecsm2979268pgl.20.2022.10.03.13.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 13:52:32 -0700 (PDT) Date: Mon, 3 Oct 2022 13:52:31 -0700 From: Kees Cook To: Rick Edgecombe Cc: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, Yu-cheng Yu Subject: Re: [PATCH v2 27/39] x86/cet/shstk: Handle signals for shadow stack Message-ID: <202210031347.6DBE61199@keescook> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-28-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220929222936.14584-28-rick.p.edgecombe@intel.com> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664830354; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8ffUlHkSjB1PAHGl9/RU9+eAvU4W3MS9YfacwqGrUYs=; b=tl9d+b2gyPpP8lzcc2rQK6rh3HLi5VUxvolEABCVZyHHM/s6tsDpn3JOL2FVafFH75viD8 3EpntBhK5MgJnhIBor1+6uJ3LcJgy7GzW0jdi+1vnTiLZyLhdTt39MSKNDrSXZf37AUgoy pJvOwVch1xGMZyVXGQJLPW7yGA8a0/E= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=R8SPL4pb; spf=pass (imf24.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.42 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664830354; a=rsa-sha256; cv=none; b=WhyB3mbSoktYcgGIAg63K9Rvux/pzI7s0vcgNdB76E7hLYiaSz2UpIZB1d4W7CrjzfXEU4 9Po4bB4EfLL5tsOlkDV457Ave/wpkEfKjvgjKIIvMTeINfEPeAiK73vUBsFPSlGMrxSOTO Old/gljezSx8jM5M5vXpPLFniz+quHs= Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=R8SPL4pb; spf=pass (imf24.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.42 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 7A126180017 X-Rspam-User: X-Stat-Signature: u7pcfxh1m56qi8ruu4fpsh8h9rbwzeu6 X-HE-Tag: 1664830354-878714 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Sep 29, 2022 at 03:29:24PM -0700, Rick Edgecombe wrote: > From: Yu-cheng Yu > > When a signal is handled normally the context is pushed to the stack > before handling it. For shadow stacks, since the shadow stack only track's > return addresses, there isn't any state that needs to be pushed. However, > there are still a few things that need to be done. These things are > userspace visible and which will be kernel ABI for shadow stacks. > > One is to make sure the restorer address is written to shadow stack, since > the signal handler (if not changing ucontext) returns to the restorer, and > the restorer calls sigreturn. So add the restorer on the shadow stack > before handling the signal, so there is not a conflict when the signal > handler returns to the restorer. > > The other thing to do is to place some type of checkable token on the > thread's shadow stack before handling the signal and check it during > sigreturn. This is an extra layer of protection to hamper attackers > calling sigreturn manually as in SROP-like attacks. > > For this token we can use the shadow stack data format defined earlier. > Have the data pushed be the previous SSP. In the future the sigreturn > might want to return back to a different stack. Storing the SSP (instead > of a restore offset or something) allows for future functionality that > may want to restore to a different stack. > > So, when handling a signal push > - the SSP pointing in the shadow stack data format > - the restorer address below the restore token. > > In sigreturn, verify SSP is stored in the data format and pop the shadow > stack. > > Signed-off-by: Yu-cheng Yu > Co-developed-by: Rick Edgecombe > Signed-off-by: Rick Edgecombe > Cc: Andy Lutomirski > Cc: Cyrill Gorcunov > Cc: Florian Weimer > Cc: H. Peter Anvin > Cc: Kees Cook > > --- > > v2: > - Switch to new shstk signal format > > v1: > - Use xsave helpers. > - Expand commit log. > > Yu-cheng v27: > - Eliminate saving shadow stack pointer to signal context. > > Yu-cheng v25: > - Update commit log/comments for the sc_ext struct. > - Use restorer address already calculated. > - Change CONFIG_X86_CET to CONFIG_X86_SHADOW_STACK. > - Change X86_FEATURE_CET to X86_FEATURE_SHSTK. > - Eliminate writing to MSR_IA32_U_CET for shadow stack. > - Change wrmsrl() to wrmsrl_safe() and handle error. > > arch/x86/ia32/ia32_signal.c | 1 + > arch/x86/include/asm/cet.h | 5 ++ > arch/x86/kernel/shstk.c | 126 ++++++++++++++++++++++++++++++------ > arch/x86/kernel/signal.c | 10 +++ > 4 files changed, 123 insertions(+), 19 deletions(-) > > diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c > index c9c3859322fa..88d71b9de616 100644 > --- a/arch/x86/ia32/ia32_signal.c > +++ b/arch/x86/ia32/ia32_signal.c > @@ -34,6 +34,7 @@ > #include > #include > #include > +#include > > static inline void reload_segments(struct sigcontext_32 *sc) > { > diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h > index 924de99e0c61..8c6fab9f402a 100644 > --- a/arch/x86/include/asm/cet.h > +++ b/arch/x86/include/asm/cet.h > @@ -6,6 +6,7 @@ > #include > > struct task_struct; > +struct ksignal; > > struct thread_shstk { > u64 base; > @@ -22,6 +23,8 @@ int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, > void shstk_free(struct task_struct *p); > int shstk_disable(void); > void reset_thread_shstk(void); > +int setup_signal_shadow_stack(struct ksignal *ksig); > +int restore_signal_shadow_stack(void); > #else > static inline long cet_prctl(struct task_struct *task, int option, > unsigned long features) { return -EINVAL; } > @@ -33,6 +36,8 @@ static inline int shstk_alloc_thread_stack(struct task_struct *p, > static inline void shstk_free(struct task_struct *p) {} > static inline int shstk_disable(void) { return -EOPNOTSUPP; } > static inline void reset_thread_shstk(void) {} > +static inline int setup_signal_shadow_stack(struct ksignal *ksig) { return 0; } > +static inline int restore_signal_shadow_stack(void) { return 0; } > #endif /* CONFIG_X86_SHADOW_STACK */ > > #endif /* __ASSEMBLY__ */ > diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c > index 8904aef487bf..04442134aadd 100644 > --- a/arch/x86/kernel/shstk.c > +++ b/arch/x86/kernel/shstk.c > @@ -227,41 +227,129 @@ static int get_shstk_data(unsigned long *data, unsigned long __user *addr) > } > > /* > - * Verify the user shadow stack has a valid token on it, and then set > - * *new_ssp according to the token. > + * Create a restore token on shadow stack, and then push the user-mode > + * function return address. > */ > -static int shstk_check_rstor_token(unsigned long *new_ssp) > +static int shstk_setup_rstor_token(unsigned long ret_addr, unsigned long *new_ssp) Oh, hrm. Prior patch defines shstk_check_rstor_token() and doesn't call it. This patch removes it. :P Can you please remove shstk_check_rstor_token() from the prior patch? > { > - unsigned long token_addr; > - unsigned long token; > + unsigned long ssp, token_addr; > + int err; > + > + if (!ret_addr) > + return -EINVAL; > + > + ssp = get_user_shstk_addr(); > + if (!ssp) > + return -EINVAL; > + > + err = create_rstor_token(ssp, &token_addr); > + if (err) > + return err; > + > + ssp = token_addr - sizeof(u64); > + err = write_user_shstk_64((u64 __user *)ssp, (u64)ret_addr); > + > + if (!err) > + *new_ssp = ssp; > + > + return err; > +} > + > +static int shstk_push_sigframe(unsigned long *ssp) > +{ > + unsigned long target_ssp = *ssp; > + > + /* Token must be aligned */ > + if (!IS_ALIGNED(*ssp, 8)) > + return -EINVAL; > > - token_addr = get_user_shstk_addr(); > - if (!token_addr) > + if (!IS_ALIGNED(target_ssp, 8)) > return -EINVAL; > > - if (get_user(token, (unsigned long __user *)token_addr)) > + *ssp -= SS_FRAME_SIZE; > + if (put_shstk_data((void *__user)*ssp, target_ssp)) > return -EFAULT; > > - /* Is mode flag correct? */ > - if (!(token & BIT(0))) > + return 0; > +} > + > + > +static int shstk_pop_sigframe(unsigned long *ssp) > +{ > + unsigned long token_addr; > + int err; > + > + err = get_shstk_data(&token_addr, (unsigned long __user *)*ssp); > + if (unlikely(err)) > + return err; > + > + /* Restore SSP aligned? */ > + if (unlikely(!IS_ALIGNED(token_addr, 8))) > return -EINVAL; Why doesn't this always fail, given BIT(0) being set? I don't see it getting cleared until the end of this function. > > - /* Is busy flag set? */ > - if (token & BIT(1)) > + /* SSP in userspace? */ > + if (unlikely(token_addr >= TASK_SIZE_MAX)) > return -EINVAL; BIT(63) already got cleared by here (in get_shstk_data(), but yes, this is still a reasonable check. > > - /* Mask out flags */ > - token &= ~3UL; > + *ssp = token_addr; > + > + return 0; > +} -- Kees Cook