Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2778410yba; Mon, 22 Apr 2019 12:48:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqyJkVu+92/dLD9xYxIyX4zczCM6zXuoQTdZRhpnSxAoFjze/+xTMr91PUql4hq8bA+FwvAs X-Received: by 2002:aa7:96eb:: with SMTP id i11mr839951pfq.229.1555962494056; Mon, 22 Apr 2019 12:48:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555962494; cv=none; d=google.com; s=arc-20160816; b=RTlcBzEmwf//bzeeVFQh9EnLW7HKN06UsLBGcaa3OxfMbf28TjxfefxcCWFggbFGz0 TPHNPlQKweMfOapYhIHw2Oy1aI90IgtCwOsdZwUmEU0qjT2x0Cau/ro349xXz4kt4yMr PmcgPwVWm6ZR8hhijMFli5tK6Bz74hxM5VOBctVJzEwDkXCv2SoI1+11Y3S50A4/JK5Q 6MRfWTmAkDapiMSO3yTBX8JxWhi8zVqarmaHmO4kp8mSKkF8og8ooNrB6aj/rX7Ac5a0 HmGKDrbNjfG21ToC3Rq6Wc24S5Q09ELQ5SP4V1jTvs1M4LpnV6l1zCZu6VbCclMEWlgD QRQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=J35xmyTNJgRjBvy9hhwnWqIumlRVKgdLmnlJ+IbGkh0=; b=nekqR2jLhYPFNAKhUimsvieLS3gQDFaWAd8wH7nHzlJN666p165itfEkRV8ZPowuKj /iAdEqV19xFfUxDyuHP+p8MS2uo41QjlL8lE0kv0+cYftKFvcu6e85DSZfU1eRY7pRQW rkPc05hoXMpiL9q42b+mInJStipe57bbNzIA/e5z8hcqNxpRTFINIZgO5anxtxea0jUg Qr9wI+ONOPmqXvsS6Mhr+SR4AUCMPZv64j8L/GXegIkc0WJ61uf6mTL7nZgLMSUTs3Rk qeRoS8c9oCOg/9gfEjyjL73qqxbdGRF9K2Fisec6Zec8SrQ7MxcpbyJYCP8rLtGsdW3/ jKew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UO6wOX3h; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b15si14757396pfb.231.2019.04.22.12.47.51; Mon, 22 Apr 2019 12:48:14 -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; dkim=pass header.i=@kernel.org header.s=default header.b=UO6wOX3h; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729702AbfDVTpQ (ORCPT + 99 others); Mon, 22 Apr 2019 15:45:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:46818 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729684AbfDVTpN (ORCPT ); Mon, 22 Apr 2019 15:45:13 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EFE9B218DA; Mon, 22 Apr 2019 19:45:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555962311; bh=OhJZk7L4zM+/lKoDMmPYjEkP6Af7sIJE3xtBTNGYlg0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UO6wOX3hGUj0k1FgnGQTfsB3iwCy1a0rZDJrp7cwqGK+0qrM5jl4iQzQjQ98pq7uk ospxEMEuvl2GX6ElCmetU5ena9BG7cKt+sMIwMRSzbHsCcckBGH50PCzhKfNzNXqIz 5acZ+AHjEYme1Rn5Q1o1w6IG96D56RCNmDHuDBw8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andrei Vagin , "Eric W. Biederman" , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH AUTOSEL 5.0 96/98] ptrace: take into account saved_sigmask in PTRACE{GET,SET}SIGMASK Date: Mon, 22 Apr 2019 15:42:03 -0400 Message-Id: <20190422194205.10404-96-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190422194205.10404-1-sashal@kernel.org> References: <20190422194205.10404-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrei Vagin [ Upstream commit fcfc2aa0185f4a731d05a21e9f359968fdfd02e7 ] There are a few system calls (pselect, ppoll, etc) which replace a task sigmask while they are running in a kernel-space When a task calls one of these syscalls, the kernel saves a current sigmask in task->saved_sigmask and sets a syscall sigmask. On syscall-exit-stop, ptrace traps a task before restoring the saved_sigmask, so PTRACE_GETSIGMASK returns the syscall sigmask and PTRACE_SETSIGMASK does nothing, because its sigmask is replaced by saved_sigmask, when the task returns to user-space. This patch fixes this problem. PTRACE_GETSIGMASK returns saved_sigmask if it's set. PTRACE_SETSIGMASK drops the TIF_RESTORE_SIGMASK flag. Link: http://lkml.kernel.org/r/20181120060616.6043-1-avagin@gmail.com Fixes: 29000caecbe8 ("ptrace: add ability to get/set signal-blocked mask") Signed-off-by: Andrei Vagin Acked-by: Oleg Nesterov Cc: "Eric W. Biederman" Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin (Microsoft) --- include/linux/sched/signal.h | 18 ++++++++++++++++++ kernel/ptrace.c | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index 13789d10a50e..76b8399b17f6 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -417,10 +417,20 @@ static inline void set_restore_sigmask(void) set_thread_flag(TIF_RESTORE_SIGMASK); WARN_ON(!test_thread_flag(TIF_SIGPENDING)); } + +static inline void clear_tsk_restore_sigmask(struct task_struct *tsk) +{ + clear_tsk_thread_flag(tsk, TIF_RESTORE_SIGMASK); +} + static inline void clear_restore_sigmask(void) { clear_thread_flag(TIF_RESTORE_SIGMASK); } +static inline bool test_tsk_restore_sigmask(struct task_struct *tsk) +{ + return test_tsk_thread_flag(tsk, TIF_RESTORE_SIGMASK); +} static inline bool test_restore_sigmask(void) { return test_thread_flag(TIF_RESTORE_SIGMASK); @@ -438,6 +448,10 @@ static inline void set_restore_sigmask(void) current->restore_sigmask = true; WARN_ON(!test_thread_flag(TIF_SIGPENDING)); } +static inline void clear_tsk_restore_sigmask(struct task_struct *tsk) +{ + tsk->restore_sigmask = false; +} static inline void clear_restore_sigmask(void) { current->restore_sigmask = false; @@ -446,6 +460,10 @@ static inline bool test_restore_sigmask(void) { return current->restore_sigmask; } +static inline bool test_tsk_restore_sigmask(struct task_struct *tsk) +{ + return tsk->restore_sigmask; +} static inline bool test_and_clear_restore_sigmask(void) { if (!current->restore_sigmask) diff --git a/kernel/ptrace.c b/kernel/ptrace.c index 771e93f9c43f..6f357f4fc859 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -29,6 +29,7 @@ #include #include #include +#include /* * Access another process' address space via ptrace. @@ -924,18 +925,26 @@ int ptrace_request(struct task_struct *child, long request, ret = ptrace_setsiginfo(child, &siginfo); break; - case PTRACE_GETSIGMASK: + case PTRACE_GETSIGMASK: { + sigset_t *mask; + if (addr != sizeof(sigset_t)) { ret = -EINVAL; break; } - if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t))) + if (test_tsk_restore_sigmask(child)) + mask = &child->saved_sigmask; + else + mask = &child->blocked; + + if (copy_to_user(datavp, mask, sizeof(sigset_t))) ret = -EFAULT; else ret = 0; break; + } case PTRACE_SETSIGMASK: { sigset_t new_set; @@ -961,6 +970,8 @@ int ptrace_request(struct task_struct *child, long request, child->blocked = new_set; spin_unlock_irq(&child->sighand->siglock); + clear_tsk_restore_sigmask(child); + ret = 0; break; } -- 2.19.1