Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1869487pxu; Tue, 24 Nov 2020 10:46:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbDCvv8JxwME0FHPT0VHb3jYXtvB3ABGM/BARtidL7C0Y0UpHynFIpOrxjB8VxuediH6iE X-Received: by 2002:a05:6402:2d7:: with SMTP id b23mr5080888edx.196.1606243593629; Tue, 24 Nov 2020 10:46:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606243593; cv=none; d=google.com; s=arc-20160816; b=AObx/i/0i6R5sUDyhADjrhmEj6VWXTOPAT8hWkxDqvp/W5sWJ41MMPxMcgGX0O9cUf 6LLQJNnpOr9xSOB2e+vmRePsil4fsZ77VPA2mCnSItPcT9E01zkfclwLGpXer7fBf89y MXSz0fsSbKl35pU4kf2CIxrvqNZLDLdH2U1ddHuBBuRuX3sO8dspAJ73gzQkLV4jT/l6 iXmOqV9EHqAmRFh/o3LfKhkHvHI1ldCnY8y6rRuxKm2/y5EILGX9uhAW+GiRNEZxgAOt qYyOSYdjOIcYG2HV8yZQuPsTK5HzOcQ6IQu5ZTYFYunjiatPaF38Di4HBfDEHHBQ5S4b W3uw== 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=7bYxDHbnmMeqeAaCeZZRsCP/3hYPYLY12AMSLIFZUIg=; b=oTNO+NK0yjAJjx8if0PJ45Z3LViVKwbcKFBSTC42xWIO2WMCi74PGJ8vMmBDQENecw w0zOiGm7mvGwp4ZtHaxUIgwsVwAF88FF4Ept1gHxy8HcVj0d0lcH+24N4jDDxXDURZsU lxvzNDW+3T8vNnivhkx/qhZCdh/PGo1y/+OWgd3X0Z+4DWJJb2JPiOclJDDy75DGbe8A ybB46v2dJ+ihM+ixNXT4wyVCANt9zFN4WLlX12z/nYdX9ibQoUhxuuXw58TBGJ+6menU 5EXfjBzPjZstp/88AUMzKcEXGvjA5f1i0z+XaDwS4/0hRCSBjDYzB+wV/PDu+1qiCw36 idAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VKlK3+QL; 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 d9si9078195edv.558.2020.11.24.10.46.09; Tue, 24 Nov 2020 10:46:33 -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=VKlK3+QL; 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 S2390933AbgKXSmU (ORCPT + 99 others); Tue, 24 Nov 2020 13:42:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387989AbgKXSmR (ORCPT ); Tue, 24 Nov 2020 13:42:17 -0500 Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE7E9C061A4D for ; Tue, 24 Nov 2020 10:42:14 -0800 (PST) Received: by mail-lj1-x241.google.com with SMTP id i17so23246324ljd.3 for ; Tue, 24 Nov 2020 10:42:14 -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=7bYxDHbnmMeqeAaCeZZRsCP/3hYPYLY12AMSLIFZUIg=; b=VKlK3+QLeMWZ1q3zj34Obxbm2JFZ/yNE7EaUqGdyv8+Yu+bCV6Z/PFlAyqKMwkXK8U J79+sAAVRwvpqkxh6lly891xvx0PSQoJ3eyq6/0uoZzOzDmuUZJ+PH/ysn/byJDernYi /19Lq2qs6y22Py0nYJKyE9aw1W+qCTJBHiEJg5u11ljE6vVgJguOFDp6PRiZh8KuInWw 4OuUW90DHsKAEaiErfNGx9NGVhLLVB0yyPWFWbHSLwxpqSFuDe41XpqzvdozjnQaL4Cc RckVQWC58sV9mGG1MuHm4eHGG+YEhHqWH/5Uq1ZYweG2RSE+icNSDdwDCqgl1fwINGDn sKSg== 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=7bYxDHbnmMeqeAaCeZZRsCP/3hYPYLY12AMSLIFZUIg=; b=ZprcBJ6biyRu6Fy2obwD0CJwZZqOl1inVH+El1D8JYJFJ2syHLB9zq/J0ym3oXn28n JP0xG9NgJ5WNf2xPKpoJp5tTQgLvzf9cvVLw3O7KfG4rhyeWwSq2clyxTITFVcOI0H6P ej3WQyhQKeGqNGzdXDPZjeXjUHEFZvPOPrfiA8yGD0WKMpka/wfVA4xyI34yRQpk1cwD YFyFR6MpiMxTGYTevq4bkdk5yfhqFOuQ6iB6HxxCWi5DQNUXf4JZklLSgObt6E1h8Sl3 C9kWXODi0J8JDaD+gIYjHdrPSVR5oAy7uZulK5qsy/1mfJC0rQoKxemtvsWPTcLafsVy U4qA== X-Gm-Message-State: AOAM531S/x/gmbEd/QHWhC2ypmTfF8VJBd2F6ZIa6AFAwp8BggNsGy9N TsA7al+LP3pqrU1s/SXFNSn3GsZlfq+ZMZ3cZ/cb+g== X-Received: by 2002:a05:651c:1035:: with SMTP id w21mr209576ljm.326.1606243332802; Tue, 24 Nov 2020 10:42:12 -0800 (PST) MIME-Version: 1.0 References: <20201119190237.626-1-chang.seok.bae@intel.com> <20201119190237.626-4-chang.seok.bae@intel.com> In-Reply-To: From: Jann Horn Date: Tue, 24 Nov 2020 19:41:46 +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 , Roland McGrath 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 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 &&