Skip to content

Commit fcfc2aa

Browse files
avagintorvalds
authored andcommitted
ptrace: take into account saved_sigmask in PTRACE{GET,SET}SIGMASK
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: 29000ca ("ptrace: add ability to get/set signal-blocked mask") Signed-off-by: Andrei Vagin <avagin@gmail.com> Acked-by: Oleg Nesterov <oleg@redhat.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent eebf364 commit fcfc2aa

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎include/linux/sched/signal.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,20 @@ static inline void set_restore_sigmask(void)
418418
set_thread_flag(TIF_RESTORE_SIGMASK);
419419
WARN_ON(!test_thread_flag(TIF_SIGPENDING));
420420
}
421+
422+
static inline void clear_tsk_restore_sigmask(struct task_struct *tsk)
423+
{
424+
clear_tsk_thread_flag(tsk, TIF_RESTORE_SIGMASK);
425+
}
426+
421427
static inline void clear_restore_sigmask(void)
422428
{
423429
clear_thread_flag(TIF_RESTORE_SIGMASK);
424430
}
431+
static inline bool test_tsk_restore_sigmask(struct task_struct *tsk)
432+
{
433+
return test_tsk_thread_flag(tsk, TIF_RESTORE_SIGMASK);
434+
}
425435
static inline bool test_restore_sigmask(void)
426436
{
427437
return test_thread_flag(TIF_RESTORE_SIGMASK);
@@ -439,6 +449,10 @@ static inline void set_restore_sigmask(void)
439449
current->restore_sigmask = true;
440450
WARN_ON(!test_thread_flag(TIF_SIGPENDING));
441451
}
452+
static inline void clear_tsk_restore_sigmask(struct task_struct *tsk)
453+
{
454+
tsk->restore_sigmask = false;
455+
}
442456
static inline void clear_restore_sigmask(void)
443457
{
444458
current->restore_sigmask = false;
@@ -447,6 +461,10 @@ static inline bool test_restore_sigmask(void)
447461
{
448462
return current->restore_sigmask;
449463
}
464+
static inline bool test_tsk_restore_sigmask(struct task_struct *tsk)
465+
{
466+
return tsk->restore_sigmask;
467+
}
450468
static inline bool test_and_clear_restore_sigmask(void)
451469
{
452470
if (!current->restore_sigmask)

‎kernel/ptrace.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/hw_breakpoint.h>
3030
#include <linux/cn_proc.h>
3131
#include <linux/compat.h>
32+
#include <linux/sched/signal.h>
3233

3334
/*
3435
* Access another process' address space via ptrace.
@@ -924,18 +925,26 @@ int ptrace_request(struct task_struct *child, long request,
924925
ret = ptrace_setsiginfo(child, &siginfo);
925926
break;
926927

927-
case PTRACE_GETSIGMASK:
928+
case PTRACE_GETSIGMASK: {
929+
sigset_t *mask;
930+
928931
if (addr != sizeof(sigset_t)) {
929932
ret = -EINVAL;
930933
break;
931934
}
932935

933-
if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
936+
if (test_tsk_restore_sigmask(child))
937+
mask = &child->saved_sigmask;
938+
else
939+
mask = &child->blocked;
940+
941+
if (copy_to_user(datavp, mask, sizeof(sigset_t)))
934942
ret = -EFAULT;
935943
else
936944
ret = 0;
937945

938946
break;
947+
}
939948

940949
case PTRACE_SETSIGMASK: {
941950
sigset_t new_set;
@@ -961,6 +970,8 @@ int ptrace_request(struct task_struct *child, long request,
961970
child->blocked = new_set;
962971
spin_unlock_irq(&child->sighand->siglock);
963972

973+
clear_tsk_restore_sigmask(child);
974+
964975
ret = 0;
965976
break;
966977
}

0 commit comments

Comments
 (0)