Skip to content

Commit 2500ad1

Browse files
committed
ptrace: Don't change __state
Stop playing with tsk->__state to remove TASK_WAKEKILL while a ptrace command is executing. Instead remove TASK_WAKEKILL from the definition of TASK_TRACED, and implement a new jobctl flag TASK_PTRACE_FROZEN. This new flag is set in jobctl_freeze_task and cleared when ptrace_stop is awoken or in jobctl_unfreeze_task (when ptrace_stop remains asleep). In signal_wake_up add __TASK_TRACED to state along with TASK_WAKEKILL when the wake up is for a fatal signal. Skip adding __TASK_TRACED when TASK_PTRACE_FROZEN is not set. This has the same effect as changing TASK_TRACED to __TASK_TRACED as all of the wake_ups that use TASK_KILLABLE go through signal_wake_up. Handle a ptrace_stop being called with a pending fatal signal. Previously it would have been handled by schedule simply failing to sleep. As TASK_WAKEKILL is no longer part of TASK_TRACED schedule will sleep with a fatal_signal_pending. The code in signal_wake_up guarantees that the code will be awaked by any fatal signal that codes after TASK_TRACED is set. Previously the __state value of __TASK_TRACED was changed to TASK_RUNNING when woken up or back to TASK_TRACED when the code was left in ptrace_stop. Now when woken up ptrace_stop now clears JOBCTL_PTRACE_FROZEN and when left sleeping ptrace_unfreezed_traced clears JOBCTL_PTRACE_FROZEN. Tested-by: Kees Cook <[email protected]> Reviewed-by: Oleg Nesterov <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent 57b6de0 commit 2500ad1

File tree

6 files changed

+21
-28
lines changed

6 files changed

+21
-28
lines changed

include/linux/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct task_group;
103103
/* Convenience macros for the sake of set_current_state: */
104104
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
105105
#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
106-
#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
106+
#define TASK_TRACED __TASK_TRACED
107107

108108
#define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
109109

include/linux/sched/jobctl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct task_struct;
1919
#define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */
2020
#define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */
2121
#define JOBCTL_TRAP_FREEZE_BIT 23 /* trap for cgroup freezer */
22+
#define JOBCTL_PTRACE_FROZEN_BIT 24 /* frozen for ptrace */
2223

2324
#define JOBCTL_STOP_DEQUEUED (1UL << JOBCTL_STOP_DEQUEUED_BIT)
2425
#define JOBCTL_STOP_PENDING (1UL << JOBCTL_STOP_PENDING_BIT)
@@ -28,6 +29,7 @@ struct task_struct;
2829
#define JOBCTL_TRAPPING (1UL << JOBCTL_TRAPPING_BIT)
2930
#define JOBCTL_LISTENING (1UL << JOBCTL_LISTENING_BIT)
3031
#define JOBCTL_TRAP_FREEZE (1UL << JOBCTL_TRAP_FREEZE_BIT)
32+
#define JOBCTL_PTRACE_FROZEN (1UL << JOBCTL_PTRACE_FROZEN_BIT)
3133

3234
#define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
3335
#define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)

include/linux/sched/signal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,10 @@ extern void calculate_sigpending(void);
435435

436436
extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
437437

438-
static inline void signal_wake_up(struct task_struct *t, bool resume)
438+
static inline void signal_wake_up(struct task_struct *t, bool fatal)
439439
{
440-
signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
440+
fatal = fatal && !(t->jobctl & JOBCTL_PTRACE_FROZEN);
441+
signal_wake_up_state(t, fatal ? TASK_WAKEKILL | __TASK_TRACED : 0);
441442
}
442443
static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
443444
{

kernel/ptrace.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static bool ptrace_freeze_traced(struct task_struct *task)
197197
spin_lock_irq(&task->sighand->siglock);
198198
if (task_is_traced(task) && !looks_like_a_spurious_pid(task) &&
199199
!__fatal_signal_pending(task)) {
200-
WRITE_ONCE(task->__state, __TASK_TRACED);
200+
task->jobctl |= JOBCTL_PTRACE_FROZEN;
201201
ret = true;
202202
}
203203
spin_unlock_irq(&task->sighand->siglock);
@@ -207,23 +207,19 @@ static bool ptrace_freeze_traced(struct task_struct *task)
207207

208208
static void ptrace_unfreeze_traced(struct task_struct *task)
209209
{
210-
if (READ_ONCE(task->__state) != __TASK_TRACED)
211-
return;
212-
213-
WARN_ON(!task->ptrace || task->parent != current);
210+
unsigned long flags;
214211

215212
/*
216-
* PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely.
217-
* Recheck state under the lock to close this race.
213+
* The child may be awake and may have cleared
214+
* JOBCTL_PTRACE_FROZEN (see ptrace_resume). The child will
215+
* not set JOBCTL_PTRACE_FROZEN or enter __TASK_TRACED anew.
218216
*/
219-
spin_lock_irq(&task->sighand->siglock);
220-
if (READ_ONCE(task->__state) == __TASK_TRACED) {
217+
if (lock_task_sighand(task, &flags)) {
218+
task->jobctl &= ~JOBCTL_PTRACE_FROZEN;
221219
if (__fatal_signal_pending(task))
222220
wake_up_state(task, __TASK_TRACED);
223-
else
224-
WRITE_ONCE(task->__state, TASK_TRACED);
221+
unlock_task_sighand(task, &flags);
225222
}
226-
spin_unlock_irq(&task->sighand->siglock);
227223
}
228224

229225
/**
@@ -256,7 +252,6 @@ static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
256252
*/
257253
read_lock(&tasklist_lock);
258254
if (child->ptrace && child->parent == current) {
259-
WARN_ON(READ_ONCE(child->__state) == __TASK_TRACED);
260255
/*
261256
* child->sighand can't be NULL, release_task()
262257
* does ptrace_unlink() before __exit_signal().

kernel/sched/core.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6304,10 +6304,7 @@ static void __sched notrace __schedule(unsigned int sched_mode)
63046304

63056305
/*
63066306
* We must load prev->state once (task_struct::state is volatile), such
6307-
* that:
6308-
*
6309-
* - we form a control dependency vs deactivate_task() below.
6310-
* - ptrace_{,un}freeze_traced() can change ->state underneath us.
6307+
* that we form a control dependency vs deactivate_task() below.
63116308
*/
63126309
prev_state = READ_ONCE(prev->__state);
63136310
if (!(sched_mode & SM_MASK_PREEMPT) && prev_state) {

kernel/signal.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,14 +2209,12 @@ static int ptrace_stop(int exit_code, int why, unsigned long message,
22092209
}
22102210

22112211
/*
2212-
* schedule() will not sleep if there is a pending signal that
2213-
* can awaken the task.
2214-
*
2215-
* After this point ptrace_signal_wake_up will clear TASK_TRACED
2216-
* if ptrace_unlink happens. Handle previous ptrace_unlinks
2217-
* here to prevent ptrace_stop sleeping in schedule.
2212+
* After this point ptrace_signal_wake_up or signal_wake_up
2213+
* will clear TASK_TRACED if ptrace_unlink happens or a fatal
2214+
* signal comes in. Handle previous ptrace_unlinks and fatal
2215+
* signals here to prevent ptrace_stop sleeping in schedule.
22182216
*/
2219-
if (!current->ptrace)
2217+
if (!current->ptrace || __fatal_signal_pending(current))
22202218
return exit_code;
22212219

22222220
set_special_state(TASK_TRACED);
@@ -2305,7 +2303,7 @@ static int ptrace_stop(int exit_code, int why, unsigned long message,
23052303
current->exit_code = 0;
23062304

23072305
/* LISTENING can be set only during STOP traps, clear it */
2308-
current->jobctl &= ~JOBCTL_LISTENING;
2306+
current->jobctl &= ~(JOBCTL_LISTENING | JOBCTL_PTRACE_FROZEN);
23092307

23102308
/*
23112309
* Queued signals ignored us while we were stopped for tracing.

0 commit comments

Comments
 (0)