Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2052852pxu; Tue, 24 Nov 2020 15:59:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJy0bKGFu1+Bh/OJBkRCUjHHnlrmGdn9U/+4g8Q9PucUgXc5tVeA5TGogX6WmeyrubUlvKpK X-Received: by 2002:a17:906:f289:: with SMTP id gu9mr832728ejb.514.1606262398623; Tue, 24 Nov 2020 15:59:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606262398; cv=none; d=google.com; s=arc-20160816; b=v3YPpxoaZsmIQd4XI7rLbyAPXZAUSgYr+hHGkB/5c9noooRPKAwR7uLln7UPoAa7pf +02RCEhZBC6IGGcwGmGI6YUitJsiOfI1tEJiU1UdsfRuNCjoEgjSXT9j11YF20YwdqCu BQzCCjalf7hJGhro9nGdO4AJr9Nog2GZmmCiX6uYQS4V5JncYx/KrMOBJq+HXGBa+m/Z 8AuzW3XItUYchV+MIqEiYez21LdIxeX9LgcHO9LkESDO8D9LNURXgG8qGDZvWLdm+OrS 7RVQZM1gxyfplARSGyEwDDYPoIdYOZqAA4bD74aVDlKBOTdzDMHvvjxkjx4iekH3ZA+a hc6A== 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=3epUm83csRRkQAmpLttIRKsjugTlLX4yDOx4MmdoI9k=; b=RAhSPSco1o4552TucDvAJ4p+rTLUayN7IciQcEYLxqsXYLTevemWiQ9vH9gm5dg54y XdT3ss7LswSZ52c/6fqT8+Q4wk+XEhxsEWNhD+1ODR8Rde0hUW0ybqyUjT8yLQmovqFJ rF5KhYLxjEgy7CXQ/rfVvRq6AzZM6d67FiVEE3t+vStyR5cp3miS/vGXvOhNtMK//HgT J8mbhacYXxsVvD5KZgBkWp7t47JZeTQt98lBev7eIw6tR9Jm620ejAx1SYmQBbiM835s YRoT39r1sF42W9OtDC5KXPlkScGPzC+6SnjUvd0CojIHT+bjA7RvapKn3dj4fVqYLNws QE+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DjCc4MN3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c1si197715edq.286.2020.11.24.15.59.36; Tue, 24 Nov 2020 15:59:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DjCc4MN3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728522AbgKXUsQ (ORCPT + 99 others); Tue, 24 Nov 2020 15:48:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726505AbgKXUsP (ORCPT ); Tue, 24 Nov 2020 15:48:15 -0500 Received: from mail-lj1-x242.google.com (mail-lj1-x242.google.com [IPv6:2a00:1450:4864:20::242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64B42C061A4D for ; Tue, 24 Nov 2020 12:48:15 -0800 (PST) Received: by mail-lj1-x242.google.com with SMTP id f18so5632745ljg.9 for ; Tue, 24 Nov 2020 12:48:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3epUm83csRRkQAmpLttIRKsjugTlLX4yDOx4MmdoI9k=; b=DjCc4MN3vMIBSsP3wMB1bLF2wtdbXdnfZxeQ9qPMcxN/OPEf2DicGu828Nlsf1wu9D 9LH4T8fYP9cfCnOIPGDY5frb0XXy9n/ULjV8In8XV09w7lu7HXPSB29VJLDdQGrCdjL6 p7FbyinBchyxWzJazFdcblKWTiGJEX0HpbhtFjwSd1iHiiupe9yvEETMdkbb24Du3Jax yFSQfuvAPhFUtL2zp7oZRvFxh9BPLBdVrefwdf8xlXfQ6UNUVC0dnwhLBMyxkV1H17ch LWfzRPq7SI5wROFuo961qd1W1GKdYt17MO6YtZ0Feh5a7+5QHGeSMK5lWYOU3nkrCIDM k/xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3epUm83csRRkQAmpLttIRKsjugTlLX4yDOx4MmdoI9k=; b=m4xq48yjZ8AWnfi6bYMXtJ4cdsiKZiLAXR4DyJsz0IqELGPyimRajxYmscFTg2gLGw vEBP3TCHdoh9ji7XkWDc9PPJ2xzGCi5thvOu2WDVEDAOfkoHd9cRgF65ek4hV1VVxFQA IsVpNWCrwSNg64TNaBCbmsDMg93korGGQx/DxPoLUrZHadEd7bIn85U+Oj8v7rSFxOrn oolmsxeJH0MmEWpFTZ+0NLNo6Q8ERJf6f0DXgSYvWP7iuoDIL35PpylosnJTDEIV5Gvl kVNmKt7aDT28XVy3XmuRl0phKt6op05WmBBmmkVzTDxTo6bMQG0ZjILMwegLewXUcAxN iDcg== X-Gm-Message-State: AOAM530HY339jFGSqGfHmQAVLHiV75pEqx6pjdmwvRcqqiNLTS1p4uHm G2w6rDldEAhPkTFkG/A07LrFlwmCk8KlCjtB/g/iIQ== X-Received: by 2002:a2e:6a14:: with SMTP id f20mr29527ljc.377.1606250893439; Tue, 24 Nov 2020 12:48:13 -0800 (PST) MIME-Version: 1.0 References: <20201119190237.626-1-chang.seok.bae@intel.com> <20201119190237.626-4-chang.seok.bae@intel.com> <15AB5469-3DBD-4518-9C15-DDCE7C70B1B5@intel.com> In-Reply-To: <15AB5469-3DBD-4518-9C15-DDCE7C70B1B5@intel.com> From: Jann Horn Date: Tue, 24 Nov 2020 21:47:47 +0100 Message-ID: Subject: Re: [PATCH v2 3/4] x86/signal: Prevent an alternate stack overflow before a signal delivery To: "Bae, Chang Seok" Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Andy Lutomirski , "the arch/x86 maintainers" , "Brown, Len" , "Hansen, Dave" , "H.J. Lu" , Dave Martin , Michael Ellerman , "Luck, Tony" , "Shankar, Ravi V" , "libc-alpha@sourceware.org" , linux-arch , Linux API , kernel list , Hiroshi Shimamoto Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 24, 2020 at 9:43 PM Bae, Chang Seok wrote: > > On Nov 24, 2020, at 10:41, Jann Horn wrote: > > On Tue, Nov 24, 2020 at 7:22 PM Bae, Chang Seok > > wrote: > >>> On Nov 20, 2020, at 15:04, Jann Horn wrote: > >>> On Thu, Nov 19, 2020 at 8:40 PM Chang S. Bae wrote: > >>>> > >>>> diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c > >>>> index ee6f1ceaa7a2..cee41d684dc2 100644 > >>>> --- a/arch/x86/kernel/signal.c > >>>> +++ b/arch/x86/kernel/signal.c > >>>> @@ -251,8 +251,13 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, > >>>> > >>>> /* This is the X/Open sanctioned signal stack switching. */ > >>>> if (ka->sa.sa_flags & SA_ONSTACK) { > >>>> - if (sas_ss_flags(sp) == 0) > >>>> + if (sas_ss_flags(sp) == 0) { > >>>> + /* If the altstack might overflow, die with SIGSEGV: */ > >>>> + if (!altstack_size_ok(current)) > >>>> + return (void __user *)-1L; > >>>> + > >>>> sp = current->sas_ss_sp + current->sas_ss_size; > >>>> + } > >>> > >>> A couple lines further down, we have this (since commit 14fc9fbc700d): > >>> > >>> /* > >>> * If we are on the alternate signal stack and would overflow it, don't. > >>> * Return an always-bogus address instead so we will die with SIGSEGV. > >>> */ > >>> if (onsigstack && !likely(on_sig_stack(sp))) > >>> return (void __user *)-1L; > >>> > >>> Is that not working? > >> > >> onsigstack is set at the beginning here. If a signal hits under normal stack, > >> this flag is not set. Then it will miss the overflow. > >> > >> The added check allows to detect the sigaltstack overflow (always). > > > > Ah, I think I understand what you're trying to do. But wouldn't the > > better approach be to ensure that the existing on_sig_stack() check is > > also used if we just switched to the signal stack? Something like: > > > > diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c > > index be0d7d4152ec..2f57842fb4d6 100644 > > --- a/arch/x86/kernel/signal.c > > +++ b/arch/x86/kernel/signal.c > > @@ -237,7 +237,7 @@ get_sigframe(struct k_sigaction *ka, struct > > pt_regs *regs, size_t frame_size, > > unsigned long math_size = 0; > > unsigned long sp = regs->sp; > > unsigned long buf_fx = 0; > > - int onsigstack = on_sig_stack(sp); > > + bool onsigstack = on_sig_stack(sp); > > int ret; > > > > /* redzone */ > > @@ -246,8 +246,10 @@ get_sigframe(struct k_sigaction *ka, struct > > pt_regs *regs, size_t frame_size, > > > > /* This is the X/Open sanctioned signal stack switching. */ > > if (ka->sa.sa_flags & SA_ONSTACK) { > > - if (sas_ss_flags(sp) == 0) > > + if (sas_ss_flags(sp) == 0) { > > sp = current->sas_ss_sp + current->sas_ss_size; > > + onsigstack = true; > > + } > > } else if (IS_ENABLED(CONFIG_X86_32) && > > !onsigstack && > > regs->ss != __USER_DS && > > Yeah, but wouldn't it better to avoid overwriting user data if we can? The old > check raises segfault *after* overwritten. Where is that overwrite happening? Between the point where your check happens, and the point where the old check is, the only calls are to fpu__alloc_mathframe() and align_sigframe(), right? fpu__alloc_mathframe() just does some size calculations and doesn't write anything. align_sigframe() also just does size calculations. Am I missing something?