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 X-Spam-Level: X-Spam-Status: No, score=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9941C64E75 for ; Tue, 24 Nov 2020 20:48:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7FB1E206E0 for ; Tue, 24 Nov 2020 20:48:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DjCc4MN3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728544AbgKXUsR (ORCPT ); Tue, 24 Nov 2020 15:48:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726505AbgKXUsR (ORCPT ); Tue, 24 Nov 2020 15:48:17 -0500 Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42454C0613D6 for ; Tue, 24 Nov 2020 12:48:15 -0800 (PST) Received: by mail-lj1-x244.google.com with SMTP id j10so11168541lja.5 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=qaTM97NFWc0VgFGCN81z9Gm434ftZZPGRpo0LETi9wu2yvnQTo+3BcySZ0EAyjN3W8 I4OrDd2MZ8yGWHsNPdQmihMrpERszHym68mgC3YfZQWY1HhzouDT8RzfH4u7psbxaUNb RWOUrLCM4ep62u32mIhUGj7v62+twL+us6uMPHAVgRkmvUUTSYW865/Bo9A7SGIUC8Uc c14S5XLnoEJIJ95p14KF4cRFf06g2EESBFMjN1UstlVVe2r0mCa/RHPWepVLv9+iCtBU Vwf2NloQVsumfAswsbvK9JWkUKZ3yPTOip+HCuPI+LLcpYycaz3akh8GnC9RUnozaALe CVdg== X-Gm-Message-State: AOAM53262kKfb/wVdlGGbQ2k0/q29wKZbAvOi0zauhP+1fzF2R+ED8QJ 9MBEwpQFPxT7GpDGbK8A+QxM0abSNyEnj/eAlHdjIQ== X-Google-Smtp-Source: ABdhPJy9rSK07pxI0k30vVunlMZK4763KQjWwx2Esh+gUkxGiy0qRGDBofmooW5ebFiAW4nQaGooCZ0Hj/KyFVPB4UE= 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-arch@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?