Skip to content

Commit c70d9d8

Browse files
committed
ptrace: Properly initialize ptracer_cred on fork
When I introduced ptracer_cred I failed to consider the weirdness of fork where the task_struct copies the old value by default. This winds up leaving ptracer_cred set even when a process forks and the child process does not wind up being ptraced. Because ptracer_cred is not set on non-ptraced processes whose parents were ptraced this has broken the ability of the enlightenment window manager to start setuid children. Fix this by properly initializing ptracer_cred in ptrace_init_task This must be done with a little bit of care to preserve the current value of ptracer_cred when ptrace carries through fork. Re-reading the ptracer_cred from the ptracing process at this point is inconsistent with how PT_PTRACE_CAP has been maintained all of these years. Tested-by: Takashi Iwai <tiwai@suse.de> Fixes: 64b875f ("ptrace: Capture the ptracer's creds not PT_PTRACE_CAP") Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 3fd3722 commit c70d9d8

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎include/linux/ptrace.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ extern int ptrace_request(struct task_struct *child, long request,
5454
unsigned long addr, unsigned long data);
5555
extern void ptrace_notify(int exit_code);
5656
extern void __ptrace_link(struct task_struct *child,
57-
struct task_struct *new_parent);
57+
struct task_struct *new_parent,
58+
const struct cred *ptracer_cred);
5859
extern void __ptrace_unlink(struct task_struct *child);
5960
extern void exit_ptrace(struct task_struct *tracer, struct list_head *dead);
6061
#define PTRACE_MODE_READ 0x01
@@ -206,7 +207,7 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
206207

207208
if (unlikely(ptrace) && current->ptrace) {
208209
child->ptrace = current->ptrace;
209-
__ptrace_link(child, current->parent);
210+
__ptrace_link(child, current->parent, current->ptracer_cred);
210211

211212
if (child->ptrace & PT_SEIZED)
212213
task_set_jobctl_pending(child, JOBCTL_TRAP_STOP);
@@ -215,6 +216,8 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
215216

216217
set_tsk_thread_flag(child, TIF_SIGPENDING);
217218
}
219+
else
220+
child->ptracer_cred = NULL;
218221
}
219222

220223
/**

‎kernel/ptrace.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,25 @@ int ptrace_access_vm(struct task_struct *tsk, unsigned long addr,
6060
}
6161

6262

63+
void __ptrace_link(struct task_struct *child, struct task_struct *new_parent,
64+
const struct cred *ptracer_cred)
65+
{
66+
BUG_ON(!list_empty(&child->ptrace_entry));
67+
list_add(&child->ptrace_entry, &new_parent->ptraced);
68+
child->parent = new_parent;
69+
child->ptracer_cred = get_cred(ptracer_cred);
70+
}
71+
6372
/*
6473
* ptrace a task: make the debugger its new parent and
6574
* move it to the ptrace list.
6675
*
6776
* Must be called with the tasklist lock write-held.
6877
*/
69-
void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
78+
static void ptrace_link(struct task_struct *child, struct task_struct *new_parent)
7079
{
71-
BUG_ON(!list_empty(&child->ptrace_entry));
72-
list_add(&child->ptrace_entry, &new_parent->ptraced);
73-
child->parent = new_parent;
7480
rcu_read_lock();
75-
child->ptracer_cred = get_cred(__task_cred(new_parent));
81+
__ptrace_link(child, new_parent, __task_cred(new_parent));
7682
rcu_read_unlock();
7783
}
7884

@@ -386,7 +392,7 @@ static int ptrace_attach(struct task_struct *task, long request,
386392
flags |= PT_SEIZED;
387393
task->ptrace = flags;
388394

389-
__ptrace_link(task, current);
395+
ptrace_link(task, current);
390396

391397
/* SEIZE doesn't trap tracee on attach */
392398
if (!seize)
@@ -459,7 +465,7 @@ static int ptrace_traceme(void)
459465
*/
460466
if (!ret && !(current->real_parent->flags & PF_EXITING)) {
461467
current->ptrace = PT_PTRACED;
462-
__ptrace_link(current, current->real_parent);
468+
ptrace_link(current, current->real_parent);
463469
}
464470
}
465471
write_unlock_irq(&tasklist_lock);

0 commit comments

Comments
 (0)