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 5DCE5C433EF for ; Sat, 5 Feb 2022 20:21:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACC4C6B00A0; Sat, 5 Feb 2022 15:21:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A54266B00A1; Sat, 5 Feb 2022 15:21:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A7756B00A2; Sat, 5 Feb 2022 15:21:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 7539E6B00A0 for ; Sat, 5 Feb 2022 15:21:50 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3596B8249980 for ; Sat, 5 Feb 2022 20:21:50 +0000 (UTC) X-FDA: 79109847180.08.8F424D0 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf21.hostedemail.com (Postfix) with ESMTP id D41F01C000B for ; Sat, 5 Feb 2022 20:21:49 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id t9so5881161plg.13 for ; Sat, 05 Feb 2022 12:21:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=BelwKvGiNngYmwb1phL2hLEVwXRxowUsIgvaDY3aEhc=; b=S8VIICqQ4Esj/K7s636jZYgXjT+rBr1k8//aD6RwoFudz36QJzH7ba1Gv5Pig6JtFB 57bZR+Rxw+cOERbx3Bmkw/vRZnd91e2qlHN3w4nP1cJJY6yLoajMqahDoeL+TXAfHxb4 v+oBUIIp4GE383R4w56a8alreRrd95AtXjs+XUSBYG032joUj7m+qRKnWSS31AomhqE6 WNpbt1gEnUR2LbNhzUyj+TzwzFYzoftixchVYyShJthta7LEND3i7IumHeSH5gxuuquF yvGdB9yZBWnyPPLm3lfp37YASgToGwcW9UqUO/YfaKyUNnsvz6r6USo6aQ/JG4bAjdyi x9Lg== 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:content-transfer-encoding; bh=BelwKvGiNngYmwb1phL2hLEVwXRxowUsIgvaDY3aEhc=; b=s5lmUFrQm2zNXMZt1hrX5FoYPFBU2n+IHbpYVu1AlPl806ZdbLbFv0StqQ53xZUEic SOQk0bTAlFpCcDE8xR+1zTlg0iKU1IRXijm5hGVj7y4xqf04CwH+Is6pxukQFmGyTWLW Y1Va62jPjqSFic3RJgadnue5F+utbltKpwfIC5XPhFXhCtEtlCck5VTtPPHTF61p2yvi W3P2YBzBlcfTNbMbBA5Vm1W0uxT7wQg2EI8aZBVz1odvD5bQAK7CjjFAtUXNhTYptUAH zH6vPNDPNjuEOSyH96XROLAjXCJwsE+7K3YZSOxfsvhVMYZQ0LYe6u6dNgAzC0SIRzj0 xNAg== X-Gm-Message-State: AOAM532L0AV/CWBmqWIzO4noBH1kRstTSLtdeMcW6BCxi4z4tl2q85kq ZiUpKpsWaXNX5f5L/fznO3i7BzL2qkYUsloD6nU= X-Google-Smtp-Source: ABdhPJxjjaAO4+JvF6zsK509l0ZyRJEcv9J8F7QurQJrAt2Z309n50xEgtBefayuJQd5KG4TJ/898MwjPME4pGTMEPc= X-Received: by 2002:a17:903:2350:: with SMTP id c16mr9700042plh.4.1644092508735; Sat, 05 Feb 2022 12:21:48 -0800 (PST) MIME-Version: 1.0 References: <87fsozek0j.ffs@tglx> <3421da7fc8474b6db0e265b20ffd28d0@AcuMS.aculab.com> <9f948745435c4c9273131146d50fe6f328b91a78.camel@intel.com> In-Reply-To: <9f948745435c4c9273131146d50fe6f328b91a78.camel@intel.com> From: "H.J. Lu" Date: Sat, 5 Feb 2022 12:21:12 -0800 Message-ID: Subject: Re: [PATCH 00/35] Shadow stacks for userspace To: "Edgecombe, Rick P" Cc: "David.Laight@aculab.com" , "bsingharora@gmail.com" , "hpa@zytor.com" , "Syromiatnikov, Eugene" , "peterz@infradead.org" , "rdunlap@infradead.org" , "keescook@chromium.org" , "Eranian, Stephane" , "kirill.shutemov@linux.intel.com" , "dave.hansen@linux.intel.com" , "linux-mm@kvack.org" , "fweimer@redhat.com" , "nadav.amit@gmail.com" , "jannh@google.com" , "kcc@google.com" , "linux-arch@vger.kernel.org" , "pavel@ucw.cz" , "oleg@redhat.com" , "Yang, Weijiang" , "Lutomirski, Andy" , "bp@alien8.de" , "arnd@arndb.de" , "Moreira, Joao" , "tglx@linutronix.de" , "mike.kravetz@oracle.com" , "x86@kernel.org" , "linux-doc@vger.kernel.org" , "Dave.Martin@arm.com" , "john.allen@amd.com" , "mingo@redhat.com" , "Shankar, Ravi V" , "corbet@lwn.net" , "linux-kernel@vger.kernel.org" , "linux-api@vger.kernel.org" , "gorcunov@gmail.com" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=S8VIICqQ; spf=pass (imf21.hostedemail.com: domain of hjl.tools@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=hjl.tools@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: nil X-Rspamd-Queue-Id: D41F01C000B X-Stat-Signature: h7tjfqteo3stfiq1s996ip5rsamnnakm X-Rspamd-Server: rspam12 X-HE-Tag: 1644092509-836607 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 Sat, Feb 5, 2022 at 12:15 PM Edgecombe, Rick P wrote: > > On Sat, 2022-02-05 at 05:29 -0800, H.J. Lu wrote: > > On Sat, Feb 5, 2022 at 5:27 AM David Laight > > wrote: > > > > > > From: Edgecombe, Rick P > > > > Sent: 04 February 2022 01:08 > > > > Hi Thomas, > > > > > > > > Thanks for feedback on the plan. > > > > > > > > On Thu, 2022-02-03 at 22:07 +0100, Thomas Gleixner wrote: > > > > > > Until now, the enabling effort was trying to support both > > > > > > Shadow > > > > > > Stack and IBT. > > > > > > This history will focus on a few areas of the shadow stack > > > > > > development history > > > > > > that I thought stood out. > > > > > > > > > > > > Signals > > > > > > ------- > > > > > > Originally signals placed the location of the shadow > > > > > > stack > > > > > > restore > > > > > > token inside the saved state on the stack. This was > > > > > > problematic from a > > > > > > past ABI promises perspective. So the restore location > > > > > > was > > > > > > instead just > > > > > > assumed from the shadow stack pointer. This works > > > > > > because in > > > > > > normal > > > > > > allowed cases of calling sigreturn, the shadow stack > > > > > > pointer > > > > > > should be > > > > > > right at the restore token at that time. There is no > > > > > > alternate shadow > > > > > > stack support. If an alt shadow stack is added later > > > > > > we > > > > > > would > > > > > > need to > > > > > > > > > > So how is that going to work? altstack is not an esoteric > > > > > corner > > > > > case. > > > > > > > > My understanding is that the main usages for the signal stack > > > > were > > > > handling stack overflows and corruption. Since the shadow stack > > > > only > > > > contains return addresses rather than large stack allocations, > > > > and is > > > > not generally writable or pivotable, I thought there was a good > > > > possibility an alt shadow stack would not end up being especially > > > > useful. Does it seem like reasonable guesswork? > > > > > > The other 'problem' is that it is valid to longjump out of a signal > > > handler. > > > These days you have to use siglongjmp() not longjmp() but it is > > > still used. > > > > > > It is probably also valid to use siglongjmp() to jump from a nested > > > signal handler into the outer handler. > > > Given both signal handlers can have their own stack, there can be > > > three > > > stacks involved. > > So the scenario is? > > 1. Handle signal 1 > 2. sigsetjmp() > 3. signalstack() > 4. Handle signal 2 on alt stack > 5. siglongjmp() > > I'll check that it is covered by the tests, but I think it should work > in this series that has no alt shadow stack. I have only done a high > level overview of how the shadow stack stuff, that doesn't involve the > kernel, works in glibc. Sounds like I'll need to do a deeper dive. > > > > > > > I think the shadow stack pointer has to be in ucontext - which also > > > means the application can change it before returning from a signal. > > Yes we might need to change it to support alt shadow stacks. Can you > elaborate why you think it has to be in ucontext? I was thinking of > looking at three options for storing the ssp: > - Stored in the shadow stack like a token using WRUSS from the kernel. > - Stored on the kernel side using a hashmap that maps ucontext or > sigframe userspace address to ssp (this is of course similar to > storing in ucontext, except that the user can=E2=80=99t change the ssp= ). > - Stored writable in userspace in ucontext. > > But in this version, without alt shadow stacks, the shadow stack > pointer is not stored in ucontext. This causes the limitation that > userspace can only call sigreturn when it has returned back to a point > where there is a restore token on the shadow stack (which was placed > there by the kernel). This doesn=E2=80=99t mean it can=E2=80=99t switch t= o a different > shadow stack or handle a nested signal, but it limits the possibility > for calling sigreturn with a totally different sigframe (like CRIU and > SROP attacks do). It should hopefully be a helpful, protective > limitation for most apps and I'm hoping CRIU can be fixed without > removing it. > > I am not aware of other limitations to signals (besides normal shadow > stack enforcement), but I could be missing it. And people's skepticism > is making me want to go back over it with more scrutiny. > > > > In much the same way as all the segment registers can be changed > > > leading to all the nasty bugs when the final 'return to user' code > > > traps in kernel when loading invalid segment registers or executing > > > iret. > > I don't think this is as difficult to avoid because userspace ssp has > its own register that should not be accessed at that point, but I have > not given this aspect enough analysis. Thanks for bringing it up. > > > > > > > Hmmm... do shadow stacks mean that longjmp() has to be a system > > > call? > > > > No. setjmp/longjmp save and restore shadow stack pointer. > > > > It sounds like it would help to write up in a lot more detail exactly > how all the signal and specialer stack manipulation scenarios work in > glibc. > setjmp/longjmp work on the same sigjmp_buf. Shadow stack pointer is saved and restored, just like any other callee-saved registers. --=20 H.J.