Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753647AbcCJMJP (ORCPT ); Thu, 10 Mar 2016 07:09:15 -0500 Received: from smtp33.i.mail.ru ([94.100.177.93]:42189 "EHLO smtp33.i.mail.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753849AbcCJMJB (ORCPT ); Thu, 10 Mar 2016 07:09:01 -0500 Subject: Re: [PATCH] [Cleanup] x86: signal: unify the sigaltstack check with other arches To: Andy Lutomirski , Ingo Molnar References: <1456095685-23857-1-git-send-email-stsp@list.ru> <20160225082524.GA12294@gmail.com> <56CEF02C.7050906@list.ru> <20160308162046.GA30211@gmail.com> Cc: "linux-kernel@vger.kernel.org" , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , X86 ML , Borislav Petkov , Brian Gerst , Oleg Nesterov , Richard Weinberger , Stas Sergeev From: Stas Sergeev Message-ID: <56E163CE.1010206@list.ru> Date: Thu, 10 Mar 2016 15:08:46 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Mras: Ok Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4687 Lines: 106 10.03.2016 03:02, Andy Lutomirski пишет: > On Tue, Mar 8, 2016 at 8:20 AM, Ingo Molnar wrote: >> >> * Stas Sergeev wrote: >> >>> 25.02.2016 11:25, Ingo Molnar пишет: >>>> >>>> * Stas Sergeev wrote: >>>> >>>>> Currently x86's get_sigframe() checks for "current->sas_ss_size" >>>>> to determine whether there is a need to switch to sigaltstack. >>>>> The common practice used by all other arches is to check for >>>>> sas_ss_flags(sp) == 0 >>>>> >>>>> This patch makes the code consistent with other arches. >>>>> The slight complexity of the patch is added by the optimization on >>>>> !sigstack check that was requested by Andy Lutomirski: sas_ss_flags(sp)==0 >>>>> already implies that we are not on a sigstack, so the code is shuffled >>>>> to avoid the duplicate checking. >>>> >>>> So this changelog is missing an analysis about what effect this change will have >>>> on applications. Can any type of user-space code see a change in behavior? If yes, >>>> what will happen and is that effect desirable? >>> This is a clean-up, and as such, there is no visible effect. >>> If there is - it is a bug. >>> The purpose of this patch is only to unify the x86 code with >>> what all the other arches do. It was initially the part of the >>> rejected series, but now it is just a clean-up. >> >> Ok, so AFAICS the relevant change is: >> >> - if (current->sas_ss_size) >> - sp = current->sas_ss_sp + current->sas_ss_size; >> + if (sas_ss_flags(sp) == 0) >> + sp = current->sas_ss_sp + current->sas_ss_size; >> >> and since sas_ss_flags() is defined as: >> >> static inline int sas_ss_flags(unsigned long sp) >> { >> if (!current->sas_ss_size) >> return SS_DISABLE; >> >> return on_sig_stack(sp) ? SS_ONSTACK : 0; >> } >> >> sas_ss_flags() returns 0 iff current->sas_ss_size && !on_sig_stack(). >> >> But we already have on_sig_stack(sp) calculated. Why not write that as: >> >> + if (current->sas_ss_size && !onsigstack) >> + sp = current->sas_ss_sp + current->sas_ss_size; >> >> and since we check '!onsigstack' in both branches, we might as well factor it out >> into a single condition ... and arrive to the exact code that we began with. > > ISTM it's silly for us to be unconditionally computing onsigstack. > We're doing it because we need it later for this: > > /* > * 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; But there is also the SA_RESTORER check that needs this as well: } else if (config_enabled(CONFIG_X86_32) && !onsigstack && (regs->ss & 0xffff) != __USER_DS && !(ka->sa.sa_flags & SA_RESTORER) && ka->sa.sa_restorer) { /* This is the legacy signal stack switching. */ sp = (unsigned long) ka->sa.sa_restorer; So I don't think you can that easily get rid of calling on_sig_stack(), but definitely we can structure the code so that gcc can better optimize it. > Anyway, I think we should make two changes to the sig_on_stack thing: > > 1. If SS_AUTODISARM, then we're not on the stack, regardless of what sp says. I think Ingo replied to the stand-alone cleanup I sent a month ago. Now I have also included it in an SS_AUTODISARM series, and that led to confusion... Sorry about that. But I think the above is not correct: if SS_AUTODISARM is used, we should still look into sp, or sa_restorer will not work. And in fact, someone could even call sigaltstack(SS_ONSTACK|SS_AUTODISARM) inside the sighandler itself! In this case we still have to look into sp as we did before. So I don't think the optimization you suggest above, can work. > 2. If !user_64bit_mode(regs) && (regs->ss & 0x4), then we're not on > the signal stack. This will happen if we're running on an LDT stack > and we coincidentally have the ESP part of SS:ESP matching the signal > stack. Yes, but let me please do that later. :) If I start merging also that into an existing series, we'll never get any progress. So can we just get applied at least something? :) > In general, the existing design is crap and it should always have > worked the way that Stas is proposing with SS_AUTODISARM. But how would you then return oss->ss_flags to user? I think returning oss->ss_flags to user requires the current design, and by specifying SS_AUTODISARM, the user accepts the fact that oss->ss_flags is no longer reliable (never returns SS_ONSTACK).