Skip to content

Commit f701e5b

Browse files
Vladimir Zapolskiyoleg-nesterov
authored andcommitted
connector: add an event for monitoring process tracers
This change adds a procfs connector event, which is emitted on every successful process tracer attach or detach. If some process connects to other one, kernelspace connector reports process id and thread group id of both these involved processes. On disconnection null process id is returned. Such an event allows to create a simple automated userspace mechanism to be aware about processes connecting to others, therefore predefined process policies can be applied to them if needed. Note, a detach signal is emitted only in case, if a tracer process explicitly executes PTRACE_DETACH request. In other cases like tracee or tracer exit detach event from proc connector is not reported. Signed-off-by: Vladimir Zapolskiy <vzapolskiy@gmail.com> Acked-by: Evgeniy Polyakov <zbr@ioremap.net> Cc: David S. Miller <davem@davemloft.net> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
1 parent d184d6e commit f701e5b

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎drivers/connector/cn_proc.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/init.h>
2929
#include <linux/connector.h>
3030
#include <linux/gfp.h>
31+
#include <linux/ptrace.h>
3132
#include <asm/atomic.h>
3233
#include <asm/unaligned.h>
3334

@@ -166,6 +167,40 @@ void proc_sid_connector(struct task_struct *task)
166167
cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
167168
}
168169

170+
void proc_ptrace_connector(struct task_struct *task, int ptrace_id)
171+
{
172+
struct cn_msg *msg;
173+
struct proc_event *ev;
174+
struct timespec ts;
175+
__u8 buffer[CN_PROC_MSG_SIZE];
176+
struct task_struct *tracer;
177+
178+
if (atomic_read(&proc_event_num_listeners) < 1)
179+
return;
180+
181+
msg = (struct cn_msg *)buffer;
182+
ev = (struct proc_event *)msg->data;
183+
get_seq(&msg->seq, &ev->cpu);
184+
ktime_get_ts(&ts); /* get high res monotonic timestamp */
185+
put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
186+
ev->what = PROC_EVENT_PTRACE;
187+
ev->event_data.ptrace.process_pid = task->pid;
188+
ev->event_data.ptrace.process_tgid = task->tgid;
189+
if (ptrace_id == PTRACE_ATTACH) {
190+
ev->event_data.ptrace.tracer_pid = current->pid;
191+
ev->event_data.ptrace.tracer_tgid = current->tgid;
192+
} else if (ptrace_id == PTRACE_DETACH) {
193+
ev->event_data.ptrace.tracer_pid = 0;
194+
ev->event_data.ptrace.tracer_tgid = 0;
195+
} else
196+
return;
197+
198+
memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
199+
msg->ack = 0; /* not used */
200+
msg->len = sizeof(*ev);
201+
cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
202+
}
203+
169204
void proc_exit_connector(struct task_struct *task)
170205
{
171206
struct cn_msg *msg;

‎include/linux/cn_proc.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct proc_event {
5353
PROC_EVENT_UID = 0x00000004,
5454
PROC_EVENT_GID = 0x00000040,
5555
PROC_EVENT_SID = 0x00000080,
56+
PROC_EVENT_PTRACE = 0x00000100,
5657
/* "next" should be 0x00000400 */
5758
/* "last" is the last process event: exit */
5859
PROC_EVENT_EXIT = 0x80000000
@@ -95,6 +96,13 @@ struct proc_event {
9596
__kernel_pid_t process_tgid;
9697
} sid;
9798

99+
struct ptrace_proc_event {
100+
__kernel_pid_t process_pid;
101+
__kernel_pid_t process_tgid;
102+
__kernel_pid_t tracer_pid;
103+
__kernel_pid_t tracer_tgid;
104+
} ptrace;
105+
98106
struct exit_proc_event {
99107
__kernel_pid_t process_pid;
100108
__kernel_pid_t process_tgid;
@@ -109,6 +117,7 @@ void proc_fork_connector(struct task_struct *task);
109117
void proc_exec_connector(struct task_struct *task);
110118
void proc_id_connector(struct task_struct *task, int which_id);
111119
void proc_sid_connector(struct task_struct *task);
120+
void proc_ptrace_connector(struct task_struct *task, int which_id);
112121
void proc_exit_connector(struct task_struct *task);
113122
#else
114123
static inline void proc_fork_connector(struct task_struct *task)
@@ -124,6 +133,10 @@ static inline void proc_id_connector(struct task_struct *task,
124133
static inline void proc_sid_connector(struct task_struct *task)
125134
{}
126135

136+
static inline void proc_ptrace_connector(struct task_struct *task,
137+
int ptrace_id)
138+
{}
139+
127140
static inline void proc_exit_connector(struct task_struct *task)
128141
{}
129142
#endif /* CONFIG_PROC_EVENTS */

‎kernel/ptrace.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/uaccess.h>
2424
#include <linux/regset.h>
2525
#include <linux/hw_breakpoint.h>
26+
#include <linux/cn_proc.h>
2627

2728

2829
static int ptrace_trapping_sleep_fn(void *flags)
@@ -305,9 +306,12 @@ static int ptrace_attach(struct task_struct *task, long request,
305306
unlock_creds:
306307
mutex_unlock(&task->signal->cred_guard_mutex);
307308
out:
308-
if (!retval)
309+
if (!retval) {
309310
wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
310311
ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
312+
proc_ptrace_connector(task, PTRACE_ATTACH);
313+
}
314+
311315
return retval;
312316
}
313317

@@ -415,6 +419,7 @@ static int ptrace_detach(struct task_struct *child, unsigned int data)
415419
}
416420
write_unlock_irq(&tasklist_lock);
417421

422+
proc_ptrace_connector(child, PTRACE_DETACH);
418423
if (unlikely(dead))
419424
release_task(child);
420425

0 commit comments

Comments
 (0)