Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1923076rdb; Tue, 20 Feb 2024 10:57:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXFayNUGPZWEjgxFvtIJ8aA+I/hHfbL7P6WK+PJURFI9xwxSjRJQ7uJ/c4PhE+4OmrSrLT/52zwyZcpLv1iqocsrGTEbHkiPXS35TTXYg== X-Google-Smtp-Source: AGHT+IHsMXQyBY0eOxpjY8PguN+WNkT8pjP0Cb04Kjw3BpV0OhYmzDXAKzU/I9tzaGZq9jz8phNT X-Received: by 2002:a05:622a:1885:b0:42c:7dee:ce8 with SMTP id v5-20020a05622a188500b0042c7dee0ce8mr19915862qtc.65.1708455444668; Tue, 20 Feb 2024 10:57:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708455444; cv=pass; d=google.com; s=arc-20160816; b=alLle/kBB1BLjL6lNG6P2gHOvyjspiLYN5JILGxr4ExVsi1vKSN/G7NwJSvzOl/rEs ca8H0RDKnK4cmCiI4cKIY2j25d3Ll9e/hAw3Y/oAcej36Y2vs7KuktQPCiRNW2VqHIMo R7eZuL9594pER1NCm2PzyF7P2QwsEnXJ35u0Q5x9vzvCiqaaFZlEfwoBuwx391jybfWN vdCCNPphDe20T91MtPxAde+k1+FUl7d91JhWyBfNL14wKhf2BGmLkNHJBmbtl3MHY8gh rf9J3D8/wuiAGafN9tblvUZuuExc1CJLEDap8ev9g7y3x43qiMlmMiNxhsVBjyCgyoQE ioaA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:references:message-id:subject:cc:to:from:date; bh=6mM33Jkn1BipsBWTmgMAWz6Dc1aKF1QRK2SYMEn5EW0=; fh=/UwxPRSrxxqNHLonbaog2SS0jVqzNPU1QF/RR2dX4Jk=; b=heva/MiUAqOqcW/vcwY0ewuqVpVzFA5eF/DLgDIr7vsjrYoSJ6dDGqzgAnCd7OtV1L 48jsqTzF6UyfD1B4GpttlJF8NMtp1uGvpL2iFQEiLj+P/MAcV3MRfgyfJgfVIIIACMbJ /H9ENTx490qfAp5e0k2NodtQSPULGpAYgoBX4OXVEb16cLrK1tPHsf4vkHwod2ZHHPeN J04jkogLUU/+HPPFpPbzHaeBOUQGj3T5Ae4XMVKWMx9UkGGE6JCazVEZYkcx7h2D9FQ2 FD+sKlYBDcj9gq31d9PRzwZogAON0Vr7W+awqXobn9+W2opjWXVUyoMc3sWNUpS4iVma MmKg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=libc.org); spf=pass (google.com: domain of linux-kernel+bounces-73554-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73554-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id f7-20020ac85d07000000b0042df2a9bab9si9092629qtx.512.2024.02.20.10.57.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 10:57:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73554-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=libc.org); spf=pass (google.com: domain of linux-kernel+bounces-73554-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73554-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5DA101C22748 for ; Tue, 20 Feb 2024 18:57:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0A801134CC0; Tue, 20 Feb 2024 18:57:15 +0000 (UTC) Received: from brightrain.aerifal.cx (brightrain.aerifal.cx [104.156.224.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8916212EBCA for ; Tue, 20 Feb 2024 18:57:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.156.224.86 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708455434; cv=none; b=FfVF+ja+EksC72/4VVi/nwh0AWAYQyrHvC3fQ/tDsibZRCSr4Jex5IfLK/RATtPnwQBjiHDgfFpF+tNraH3svQBEK+7SvPCLfbS9F8Zq7Fg1cjAmV0xL44C9IKowD905778uSEB6fpqe/18WasWSU8gp6MveRO44CnNC7A/85Ss= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708455434; c=relaxed/simple; bh=2AJMM3Na6vVTLu4s6ylsknzzYTfPF0vFcwg0MnVpps4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=R1HCdLorff6TwYtx7Ou1ZsB9MR7hGeMZ0FBIimhJ3sFAGuJ+fPj14o40AHRqrP72e4k02852HMb6hD26vrPGRw9QCIWsQUM4pSNKRIKyVH5Ppk7Zl+rXl657VsOPeEFeh/68wXl1rIiJrTxLExf7w2vC8/59F65LwU3VxS1+ApM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=libc.org; spf=pass smtp.mailfrom=libc.org; arc=none smtp.client-ip=104.156.224.86 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=libc.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=libc.org Date: Tue, 20 Feb 2024 13:57:15 -0500 From: Rich Felker To: "Edgecombe, Rick P" Cc: "corbet@lwn.net" , "ardb@kernel.org" , "maz@kernel.org" , "shuah@kernel.org" , "Szabolcs.Nagy@arm.com" , "keescook@chromium.org" , "james.morse@arm.com" , "debug@rivosinc.com" , "akpm@linux-foundation.org" , "catalin.marinas@arm.com" , "oleg@redhat.com" , "arnd@arndb.de" , "ebiederm@xmission.com" , "will@kernel.org" , "suzuki.poulose@arm.com" , "sorear@fastmail.com" , "oliver.upton@linux.dev" , "broonie@kernel.org" , "brauner@kernel.org" , "fweimer@redhat.com" , "aou@eecs.berkeley.edu" , "linux-kernel@vger.kernel.org" , "paul.walmsley@sifive.com" , "hjl.tools@gmail.com" , "linux-mm@kvack.org" , "palmer@dabbelt.com" , "kvmarm@lists.linux.dev" , "linux-arch@vger.kernel.org" , "thiago.bauermann@linaro.org" , "linux-doc@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-kselftest@vger.kernel.org" , "musl@lists.openwall.com" , "linux-riscv@lists.infradead.org" Subject: Re: [musl] Re: [PATCH v8 00/38] arm64/gcs: Provide support for GCS in userspace Message-ID: <20240220185714.GO4163@brightrain.aerifal.cx> References: <20240203-arm64-gcs-v8-0-c9fec77673ef@kernel.org> <22a53b78-10d7-4a5a-a01e-b2f3a8c22e94@app.fastmail.com> <4c7bdf8fde9cc45174f10b9221fa58ffb450b755.camel@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4c7bdf8fde9cc45174f10b9221fa58ffb450b755.camel@intel.com> User-Agent: Mutt/1.5.21 (2010-09-15) On Tue, Feb 20, 2024 at 06:41:05PM +0000, Edgecombe, Rick P wrote: > Hi, > > I worked on the x86 kernel shadow stack support. I think it is an > interesting suggestion. Some questions below, and I will think more on > it. > > On Tue, 2024-02-20 at 11:36 -0500, Stefan O'Rear wrote: > > While discussing the ABI implications of shadow stacks in the context > > of > > Zicfiss and musl a few days ago, I had the following idea for how to > > solve > > the source compatibility problems with shadow stacks in POSIX.1-2004 > > and > > POSIX.1-2017: > > > > 1. Introduce a "flexible shadow stack handling" option.  For what > > follows, > >    it doesn't matter if this is system-wide, per-mm, or per-vma. > > > > 2. Shadow stack faults on non-shadow stack pages, if flexible shadow > > stack > >    handling is in effect, cause the affected page to become a shadow > > stack > >    page.  When this happens, the page filled with invalid address > > tokens. > > Hmm, could the shadow stack underflow onto the real stack then? Not > sure how bad that is. INCSSP (incrementing the SSP register on x86) > loops are not rare so it seems like something that could happen. Shadow stack underflow should fault on attempt to access non-shadow-stack memory as shadow-stack, no? > >    Faults from non-shadow-stack accesses to a shadow-stack page which > > was > >    created by the previous paragraph will cause the page to revert to > >    non-shadow-stack usage, with or without clearing. > > Won't this prevent catching stack overflows when they happen? An > overflow will just turn the shadow stack into normal stack and only get > detected when the shadow stack unwinds? I don't think that's as big a problem as it sounds like. It might make pinpointing the spot at which things went wrong take a little bit more work, but it should not admit any wrong-execution. > A related question would be how to handle the expanding nature of the > initial stack. I guess the initial stack could be special and have a > separate shadow stack. That seems fine. > >    Important: a shadow stack operation can only load a valid address > > from > >    a page if that page has been in continuous shadow stack use since > > the > >    address was written by another shadow stack operation; the > > flexibility > >    delays error reporting in cases of stray writes but it never > > allows for > >    corruption of shadow stack operation. > > Shadow stacks currently have automatic guard gaps to try to prevent one > thread from overflowing onto another thread's shadow stack. This would > somewhat opens that up, as the stack guard gaps are usually maintained > by userspace for new threads. It would have to be thought through if > these could still be enforced with checking at additional spots. I would think the existing guard pages would already do that if a thread's shadow stack is contiguous with its own data stack. > > 3. Standards-defined operations which use a user-provided stack > >    (makecontext, sigaltstack, pthread_attr_setstack) use a subrange > > of the > >    provided stack for shadow stack storage.  I propose to use a > > shadow > >    stack size of 1/32 of the provided stack size, rounded up to a > > positive > >    integer number of pages, and place the shadow stack allocation at > > the > >    lowest page-aligned address inside the provided stack region. > > > >    Since page usage is flexible, no change in page permissions is > >    immediately needed; this merely sets the initial shadow stack > > pointer for > >    the new context. > > > >    If the shadow stack grew in the opposite direction to the > > architectural > >    stack, it would not be necessary to pick a fixed direction. > > > > 4. SIGSTKSZ and MINSIGSTKSZ are increased by 2 pages to provide > > sufficient > >    space for a minimum-sized shadow stack region and worst case > > alignment. > > Do all makecontext() callers ensure the size is greater than this? > > I guess glibc's makecontext() could do this scheme to prevent leaking > without any changes to the kernel. Basically steal a little of the > stack address range and overwrite it with a shadow stack mapping. But > only if the apps leave enough room. If they need to be updated, then > they could be updated to manage their own shadow stacks too I think. From the musl side, I have always looked at the entirely of shadow stack stuff with very heavy skepticism, and anything that breaks existing interface contracts, introduced places where apps can get auto-killed because a late resource allocation fails, or requires applications to code around the existence of something that should be an implementation detail, is a non-starter. To even consider shadow stack support, it must truely be fully non-breaking. > > _Without_ doing this, sigaltstack cannot be used to recover from > > stack > > overflows if the shadow stack limit is reached first, and makecontext > > cannot be supported without memory leaks and unreportable error > > conditions. > > FWIW, I think the makecontext() shadow stack leaking is a bad idea. I > would prefer the existing makecontext() interface just didn't support > shadow stack, rather than the leaking solution glibc does today. AIUI the proposal by Stefan makes it non-leaking because it's just using normal memory that reverts to normal usage on any non-shadow-stack access. Rich