From patchwork Wed Jun 20 16:36:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 139367 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp1000745lji; Wed, 20 Jun 2018 09:35:29 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLY1JwrLQ/DkCNbribqGB1yKXxTPSArBSoNzG/19M11R/XAKLV24nqNRdOLGJlehFTEHF7d X-Received: by 2002:a65:660c:: with SMTP id w12-v6mr19141707pgv.404.1529512529458; Wed, 20 Jun 2018 09:35:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529512529; cv=none; d=google.com; s=arc-20160816; b=DOZlLAlUB/fzXRkZ2rHTqbrG6PaMJA/f5Hq+EGOWiwm0MBXDTG7VL7BEdNGhmn/pWA iBKgFPoAZe/HyICoSUIIvTwiTZjnegOpXamgBxjbAdgfwSAsL3ZqPIAAXvOZ3hYqSq7B GGRMa6sLA0DbIn0/1bSPHSAOhXs9cvvz+yFcK6/Yo6ZvSb+t18o3VnnucjT8dDl+Ip4t MQBvz6/m7GFcvArT6DtIpe3ASbLaGtLaYWXvSQZ/+kyoD5Fr/QDMfXVsygIjRWINlFPn dOnya3iXNRVG85i0l/0UmXo/FRt9s6odDide+f/GoqQpoP9nicLF98V0Y4nJs1QqRce5 9X1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=v6roasjT5UPolGobX+eqw+aZlLjObZL5HkTSpxC/6lQ=; b=hrHO8w7/cLybVQPHYxVXZ0y0UNGfpbZSFA0QK49ZTsT9K1YOLXK7TYDPxK89dn5e0z jCzvvsXXI9Xtrcl6VCneUS9Bv15AE6vxysd0o2v2fw73euT+QKyM7u5d56WONUjfCY93 A1o5/OW71csnHcGihjEbSI6FroO3K1717NZkTrM0QoaGptCow3VfMK9BJ52HJtdwGHHg 3c6Cfi6yvoHGPkI5Q2k9vL6W+QVmdA9sJ+bIB4dPiwrJGhUuWCVGzAiVf7eKiuGgUnjI H2DHhrfC2wXY31fxsDy92asxz/CQ9L2+Y2ByM6Sjq6j6alpdXr8G1JXb44DcptuOZfxC D7rg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w134-v6si2680310pfd.313.2018.06.20.09.35.29; Wed, 20 Jun 2018 09:35:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932422AbeFTQf1 (ORCPT + 30 others); Wed, 20 Jun 2018 12:35:27 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:39986 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753960AbeFTQf0 (ORCPT ); Wed, 20 Jun 2018 12:35:26 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C371E1435; Wed, 20 Jun 2018 09:35:25 -0700 (PDT) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9482D3F589; Wed, 20 Jun 2018 09:35:25 -0700 (PDT) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 852D41AE0D56; Wed, 20 Jun 2018 17:36:01 +0100 (BST) From: Will Deacon To: linux-kernel@vger.kernel.org Cc: mathieu.desnoyers@efficios.com, peterz@infradead.org, paulmck@linux.vnet.ibm.com, boqun.feng@gmail.com, Will Deacon Subject: [RFC PATCH] rseq: Avoid infinite recursion when delivering SIGSEGV Date: Wed, 20 Jun 2018 17:36:00 +0100 Message-Id: <1529512560-28007-1-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When delivering a signal to a task that is using rseq, we call into __rseq_handle_notify_resume() so that the registers pushed in the sigframe are updated to reflect the state of the restartable sequence (for example, ensuring that the signal returns to the abort handler if necessary). However, if the rseq management fails due to an unrecoverable fault when accessing userspace or certain combinations of RSEQ_CS_* flags, then we will attempt to deliver a SIGSEGV. This has the potential for infinite recursion if the rseq code continuously fails on signal delivery. Avoid this problem by using force_sigsegv() instead of force_sig(), which is explicitly designed to reset the SEGV handler to SIG_DFL in the case of a recursive fault. Signed-off-by: Will Deacon --- Sending as an RFC since I only spotted this via code inspection and haven't tried to trigger it yet. arch/arm/kernel/signal.c | 2 +- arch/powerpc/kernel/signal.c | 2 +- arch/x86/kernel/signal.c | 2 +- include/linux/sched.h | 20 ++++++++++++++------ kernel/rseq.c | 7 ++++--- 5 files changed, 21 insertions(+), 12 deletions(-) -- 2.1.4 diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c index f09e9d66d605..6434a62591e8 100644 --- a/arch/arm/kernel/signal.c +++ b/arch/arm/kernel/signal.c @@ -544,7 +544,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) * Increment event counter and perform fixup for the pre-signal * frame. */ - rseq_signal_deliver(regs); + rseq_signal_deliver(ksig, regs); /* * Set up the stack frame diff --git a/arch/powerpc/kernel/signal.c b/arch/powerpc/kernel/signal.c index 17fe4339ba59..af822e764569 100644 --- a/arch/powerpc/kernel/signal.c +++ b/arch/powerpc/kernel/signal.c @@ -134,7 +134,7 @@ static void do_signal(struct task_struct *tsk) /* Re-enable the breakpoints for the signal stack */ thread_change_pc(tsk, tsk->thread.regs); - rseq_signal_deliver(tsk->thread.regs); + rseq_signal_deliver(&ksig, tsk->thread.regs); if (is32) { if (ksig.ka.sa.sa_flags & SA_SIGINFO) diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 445ca11ff863..92a3b312a53c 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -692,7 +692,7 @@ setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs) * Increment event counter and perform fixup for the pre-signal * frame. */ - rseq_signal_deliver(regs); + rseq_signal_deliver(ksig, regs); /* Set up the stack frame */ if (is_ia32_frame(ksig)) { diff --git a/include/linux/sched.h b/include/linux/sched.h index 87bf02d93a27..277f8ccb0661 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1799,20 +1799,27 @@ static inline void rseq_set_notify_resume(struct task_struct *t) set_tsk_thread_flag(t, TIF_NOTIFY_RESUME); } -void __rseq_handle_notify_resume(struct pt_regs *regs); +void __rseq_handle_notify_resume(struct ksignal *sig, struct pt_regs *regs); -static inline void rseq_handle_notify_resume(struct pt_regs *regs) +static inline void _rseq_handle_notify_resume(struct ksignal *ksig, + struct pt_regs *regs) { if (current->rseq) - __rseq_handle_notify_resume(regs); + __rseq_handle_notify_resume(ksig, regs); +} + +static inline void rseq_handle_notify_resume(struct pt_regs *regs) +{ + _rseq_handle_notify_resume(NULL, regs); } -static inline void rseq_signal_deliver(struct pt_regs *regs) +static inline void rseq_signal_deliver(struct ksignal *ksig, + struct pt_regs *regs) { preempt_disable(); __set_bit(RSEQ_EVENT_SIGNAL_BIT, ¤t->rseq_event_mask); preempt_enable(); - rseq_handle_notify_resume(regs); + _rseq_handle_notify_resume(ksig, regs); } /* rseq_preempt() requires preemption to be disabled. */ @@ -1867,7 +1874,8 @@ static inline void rseq_set_notify_resume(struct task_struct *t) static inline void rseq_handle_notify_resume(struct pt_regs *regs) { } -static inline void rseq_signal_deliver(struct pt_regs *regs) +static inline void rseq_signal_deliver(struct ksignal *ksig, + struct pt_regs *regs) { } static inline void rseq_preempt(struct task_struct *t) diff --git a/kernel/rseq.c b/kernel/rseq.c index ae306f90c514..22b6acf1ad63 100644 --- a/kernel/rseq.c +++ b/kernel/rseq.c @@ -251,10 +251,10 @@ static int rseq_ip_fixup(struct pt_regs *regs) * respect to other threads scheduled on the same CPU, and with respect * to signal handlers. */ -void __rseq_handle_notify_resume(struct pt_regs *regs) +void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs) { struct task_struct *t = current; - int ret; + int ret, sig; if (unlikely(t->flags & PF_EXITING)) return; @@ -268,7 +268,8 @@ void __rseq_handle_notify_resume(struct pt_regs *regs) return; error: - force_sig(SIGSEGV, t); + sig = ksig ? ksig->sig : 0; + force_sigsegv(sig, t); } #ifdef CONFIG_DEBUG_RSEQ