Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2971467yba; Mon, 22 Apr 2019 16:54:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqy0LX2DCr1efbysyZATy2v7DrEzHRj5+JfbNNm+/6mJg508zjkTELF544i+odircAZedyIr X-Received: by 2002:a63:de0a:: with SMTP id f10mr11944966pgg.418.1555977281031; Mon, 22 Apr 2019 16:54:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555977281; cv=none; d=google.com; s=arc-20160816; b=wHmSMi32lWguU2/RB+OmChVa912nbeduh4/G5iQ4iGN6xvGdslEV+a+KjvmFE/3URb oXsasTThkFihISGn/kzO3hHNCZa+PwchwKTnIUQOIvDrnYG+5XnQIO+BUggRSQfYt3OO HOXV5dtl0FeMfDKbcyp1+KqTd5iKHbSUBWrEDinu7uzE+Fun+G5AGzkqoGOR96L1bqA6 MeKwAyrRw5Y8ealUwy69jyLd28L0MI+Fz7lEJypgU9+u9R8Yf3GZYMXeMaSdx/enlQXf Z8TdzXWarrfK8KEVmYd5G2OvE58zlZFTm4b09qJZggJUSp485OBkMP5fPMtd7o95UeXm ulRQ== 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=88Bj51RaecmgXgwYORMyomb4duMSMRCrVLXQDX7GQKc=; b=dHGlNfUiIdmXyQS9h60Q1UpRIH9z6sn5RoeZ7HaQ+1Q8jk3ArlRGpvuVz0bmyIQ+zY T5Obz44j17ntY4zjISTJ57rKT2QN0X7WX1cEob21wk7+824g9BvntVkg8WeAE0zjrbsg Ak6CINQ9rWIzc7WrgMDJHvw7MtadAinB2EHPl0PTgpWID267mi/T21klOPA2va9wfzD5 rh7ijemfYUhHmnHZAGWW0Kx84YpfPoFwPHy4aBrCVMhmbw6YvjqRFlL7yW2tfU+gFV1h ZaB4JENou/1BDt95ChPDOtkf/b6ILySm0ADfwEEdRrMfOf/EQ6hDAvYCt9dBbW3JhRm+ BeJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=W+QpX3nJ; 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 a23si14115137pls.188.2019.04.22.16.54.25; Mon, 22 Apr 2019 16:54:41 -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=W+QpX3nJ; 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 S1731891AbfDVUAR (ORCPT + 99 others); Mon, 22 Apr 2019 16:00:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:50160 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730256AbfDVTrY (ORCPT ); Mon, 22 Apr 2019 15:47:24 -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 A1A3A218FE; Mon, 22 Apr 2019 19:47:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555962443; bh=Pk2TCZFd3id7asy2ygO9WBQMFe1AwuqYHUOgGArDcNI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W+QpX3nJJx8/jL5BAjOYebFzcAQE3o+Cfu6Ne7f0wI5qOkoGmZCg7lkLnYeFmfSC8 QutBeFssqydCAjLS56MYvwKSwpwY6K98Q+9ZyaKTOcT6ptkoDgji4QQbLY+buqMbYV keITF9Z9z3WVBRPl50/xGcwyezdcpswWAeoBRrqc= 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 4.19 66/68] ptrace: take into account saved_sigmask in PTRACE{GET,SET}SIGMASK Date: Mon, 22 Apr 2019 15:45:14 -0400 Message-Id: <20190422194516.11634-66-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190422194516.11634-1-sashal@kernel.org> References: <20190422194516.11634-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 1be35729c2c5..660d78c9af6c 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 21fec73d45d4..fc0d667f5792 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -29,6 +29,7 @@ #include #include #include +#include /* * Access another process' address space via ptrace. @@ -925,18 +926,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; @@ -962,6 +971,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