2 * cn_proc.c - process events connector
4 * Copyright (C) Matt Helsley, IBM Corp. 2005
5 * Based on cn_fork.c by Guillaume Thouvenin <guillaume.thouvenin@bull.net>
6 * Original copyright notice follows:
7 * Copyright (C) 2005 BULL SA.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/ktime.h>
28 #include <linux/init.h>
29 #include <linux/connector.h>
30 #include <linux/gfp.h>
31 #include <linux/ptrace.h>
32 #include <linux/atomic.h>
33 #include <linux/pid_namespace.h>
35 #include <linux/cn_proc.h>
38 * Size of a cn_msg followed by a proc_event structure. Since the
39 * sizeof struct cn_msg is a multiple of 4 bytes, but not 8 bytes, we
40 * add one 4-byte word to the size here, and then start the actual
41 * cn_msg structure 4 bytes into the stack buffer. The result is that
42 * the immediately following proc_event structure is aligned to 8 bytes.
44 #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4)
46 /* See comment above; we test our assumption about sizeof struct cn_msg here. */
47 static inline struct cn_msg
*buffer_to_cn_msg(__u8
*buffer
)
49 BUILD_BUG_ON(sizeof(struct cn_msg
) != 20);
50 return (struct cn_msg
*)(buffer
+ 4);
53 static atomic_t proc_event_num_listeners
= ATOMIC_INIT(0);
54 static struct cb_id cn_proc_event_id
= { CN_IDX_PROC
, CN_VAL_PROC
};
56 /* proc_event_counts is used as the sequence number of the netlink message */
57 static DEFINE_PER_CPU(__u32
, proc_event_counts
) = { 0 };
59 static inline void get_seq(__u32
*ts
, int *cpu
)
62 *ts
= __this_cpu_inc_return(proc_event_counts
) - 1;
63 *cpu
= smp_processor_id();
67 void proc_fork_connector(struct task_struct
*task
)
70 struct proc_event
*ev
;
71 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
73 struct task_struct
*parent
;
75 if (atomic_read(&proc_event_num_listeners
) < 1)
78 msg
= buffer_to_cn_msg(buffer
);
79 ev
= (struct proc_event
*)msg
->data
;
80 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
81 get_seq(&msg
->seq
, &ev
->cpu
);
82 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
83 ev
->timestamp_ns
= timespec_to_ns(&ts
);
84 ev
->what
= PROC_EVENT_FORK
;
86 parent
= rcu_dereference(task
->real_parent
);
87 ev
->event_data
.fork
.parent_pid
= parent
->pid
;
88 ev
->event_data
.fork
.parent_tgid
= parent
->tgid
;
90 ev
->event_data
.fork
.child_pid
= task
->pid
;
91 ev
->event_data
.fork
.child_tgid
= task
->tgid
;
93 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
94 msg
->ack
= 0; /* not used */
95 msg
->len
= sizeof(*ev
);
96 msg
->flags
= 0; /* not used */
97 /* If cn_netlink_send() failed, the data is not sent */
98 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
101 void proc_exec_connector(struct task_struct
*task
)
104 struct proc_event
*ev
;
106 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
108 if (atomic_read(&proc_event_num_listeners
) < 1)
111 msg
= buffer_to_cn_msg(buffer
);
112 ev
= (struct proc_event
*)msg
->data
;
113 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
114 get_seq(&msg
->seq
, &ev
->cpu
);
115 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
116 ev
->timestamp_ns
= timespec_to_ns(&ts
);
117 ev
->what
= PROC_EVENT_EXEC
;
118 ev
->event_data
.exec
.process_pid
= task
->pid
;
119 ev
->event_data
.exec
.process_tgid
= task
->tgid
;
121 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
122 msg
->ack
= 0; /* not used */
123 msg
->len
= sizeof(*ev
);
124 msg
->flags
= 0; /* not used */
125 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
128 void proc_id_connector(struct task_struct
*task
, int which_id
)
131 struct proc_event
*ev
;
132 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
134 const struct cred
*cred
;
136 if (atomic_read(&proc_event_num_listeners
) < 1)
139 msg
= buffer_to_cn_msg(buffer
);
140 ev
= (struct proc_event
*)msg
->data
;
141 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
143 ev
->event_data
.id
.process_pid
= task
->pid
;
144 ev
->event_data
.id
.process_tgid
= task
->tgid
;
146 cred
= __task_cred(task
);
147 if (which_id
== PROC_EVENT_UID
) {
148 ev
->event_data
.id
.r
.ruid
= from_kuid_munged(&init_user_ns
, cred
->uid
);
149 ev
->event_data
.id
.e
.euid
= from_kuid_munged(&init_user_ns
, cred
->euid
);
150 } else if (which_id
== PROC_EVENT_GID
) {
151 ev
->event_data
.id
.r
.rgid
= from_kgid_munged(&init_user_ns
, cred
->gid
);
152 ev
->event_data
.id
.e
.egid
= from_kgid_munged(&init_user_ns
, cred
->egid
);
158 get_seq(&msg
->seq
, &ev
->cpu
);
159 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
160 ev
->timestamp_ns
= timespec_to_ns(&ts
);
162 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
163 msg
->ack
= 0; /* not used */
164 msg
->len
= sizeof(*ev
);
165 msg
->flags
= 0; /* not used */
166 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
169 void proc_sid_connector(struct task_struct
*task
)
172 struct proc_event
*ev
;
174 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
176 if (atomic_read(&proc_event_num_listeners
) < 1)
179 msg
= buffer_to_cn_msg(buffer
);
180 ev
= (struct proc_event
*)msg
->data
;
181 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
182 get_seq(&msg
->seq
, &ev
->cpu
);
183 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
184 ev
->timestamp_ns
= timespec_to_ns(&ts
);
185 ev
->what
= PROC_EVENT_SID
;
186 ev
->event_data
.sid
.process_pid
= task
->pid
;
187 ev
->event_data
.sid
.process_tgid
= task
->tgid
;
189 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
190 msg
->ack
= 0; /* not used */
191 msg
->len
= sizeof(*ev
);
192 msg
->flags
= 0; /* not used */
193 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
196 void proc_ptrace_connector(struct task_struct
*task
, int ptrace_id
)
199 struct proc_event
*ev
;
201 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
203 if (atomic_read(&proc_event_num_listeners
) < 1)
206 msg
= buffer_to_cn_msg(buffer
);
207 ev
= (struct proc_event
*)msg
->data
;
208 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
209 get_seq(&msg
->seq
, &ev
->cpu
);
210 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
211 ev
->timestamp_ns
= timespec_to_ns(&ts
);
212 ev
->what
= PROC_EVENT_PTRACE
;
213 ev
->event_data
.ptrace
.process_pid
= task
->pid
;
214 ev
->event_data
.ptrace
.process_tgid
= task
->tgid
;
215 if (ptrace_id
== PTRACE_ATTACH
) {
216 ev
->event_data
.ptrace
.tracer_pid
= current
->pid
;
217 ev
->event_data
.ptrace
.tracer_tgid
= current
->tgid
;
218 } else if (ptrace_id
== PTRACE_DETACH
) {
219 ev
->event_data
.ptrace
.tracer_pid
= 0;
220 ev
->event_data
.ptrace
.tracer_tgid
= 0;
224 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
225 msg
->ack
= 0; /* not used */
226 msg
->len
= sizeof(*ev
);
227 msg
->flags
= 0; /* not used */
228 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
231 void proc_comm_connector(struct task_struct
*task
)
234 struct proc_event
*ev
;
236 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
238 if (atomic_read(&proc_event_num_listeners
) < 1)
241 msg
= buffer_to_cn_msg(buffer
);
242 ev
= (struct proc_event
*)msg
->data
;
243 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
244 get_seq(&msg
->seq
, &ev
->cpu
);
245 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
246 ev
->timestamp_ns
= timespec_to_ns(&ts
);
247 ev
->what
= PROC_EVENT_COMM
;
248 ev
->event_data
.comm
.process_pid
= task
->pid
;
249 ev
->event_data
.comm
.process_tgid
= task
->tgid
;
250 get_task_comm(ev
->event_data
.comm
.comm
, task
);
252 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
253 msg
->ack
= 0; /* not used */
254 msg
->len
= sizeof(*ev
);
255 msg
->flags
= 0; /* not used */
256 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
259 void proc_coredump_connector(struct task_struct
*task
)
262 struct proc_event
*ev
;
263 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
266 if (atomic_read(&proc_event_num_listeners
) < 1)
269 msg
= buffer_to_cn_msg(buffer
);
270 ev
= (struct proc_event
*)msg
->data
;
271 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
272 get_seq(&msg
->seq
, &ev
->cpu
);
273 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
274 ev
->timestamp_ns
= timespec_to_ns(&ts
);
275 ev
->what
= PROC_EVENT_COREDUMP
;
276 ev
->event_data
.coredump
.process_pid
= task
->pid
;
277 ev
->event_data
.coredump
.process_tgid
= task
->tgid
;
279 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
280 msg
->ack
= 0; /* not used */
281 msg
->len
= sizeof(*ev
);
282 msg
->flags
= 0; /* not used */
283 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
286 void proc_exit_connector(struct task_struct
*task
)
289 struct proc_event
*ev
;
290 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
293 if (atomic_read(&proc_event_num_listeners
) < 1)
296 msg
= buffer_to_cn_msg(buffer
);
297 ev
= (struct proc_event
*)msg
->data
;
298 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
299 get_seq(&msg
->seq
, &ev
->cpu
);
300 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
301 ev
->timestamp_ns
= timespec_to_ns(&ts
);
302 ev
->what
= PROC_EVENT_EXIT
;
303 ev
->event_data
.exit
.process_pid
= task
->pid
;
304 ev
->event_data
.exit
.process_tgid
= task
->tgid
;
305 ev
->event_data
.exit
.exit_code
= task
->exit_code
;
306 ev
->event_data
.exit
.exit_signal
= task
->exit_signal
;
308 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
309 msg
->ack
= 0; /* not used */
310 msg
->len
= sizeof(*ev
);
311 msg
->flags
= 0; /* not used */
312 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
316 * Send an acknowledgement message to userspace
318 * Use 0 for success, EFOO otherwise.
319 * Note: this is the negative of conventional kernel error
320 * values because it's not being returned via syscall return
323 static void cn_proc_ack(int err
, int rcvd_seq
, int rcvd_ack
)
326 struct proc_event
*ev
;
327 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
330 if (atomic_read(&proc_event_num_listeners
) < 1)
333 msg
= buffer_to_cn_msg(buffer
);
334 ev
= (struct proc_event
*)msg
->data
;
335 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
337 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
338 ev
->timestamp_ns
= timespec_to_ns(&ts
);
340 ev
->what
= PROC_EVENT_NONE
;
341 ev
->event_data
.ack
.err
= err
;
342 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
343 msg
->ack
= rcvd_ack
+ 1;
344 msg
->len
= sizeof(*ev
);
345 msg
->flags
= 0; /* not used */
346 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
351 * @data: message sent from userspace via the connector
353 static void cn_proc_mcast_ctl(struct cn_msg
*msg
,
354 struct netlink_skb_parms
*nsp
)
356 enum proc_cn_mcast_op
*mc_op
= NULL
;
359 if (msg
->len
!= sizeof(*mc_op
))
363 * Events are reported with respect to the initial pid
364 * and user namespaces so ignore requestors from
367 if ((current_user_ns() != &init_user_ns
) ||
368 (task_active_pid_ns(current
) != &init_pid_ns
))
371 /* Can only change if privileged. */
372 if (!__netlink_ns_capable(nsp
, &init_user_ns
, CAP_NET_ADMIN
)) {
377 mc_op
= (enum proc_cn_mcast_op
*)msg
->data
;
379 case PROC_CN_MCAST_LISTEN
:
380 atomic_inc(&proc_event_num_listeners
);
382 case PROC_CN_MCAST_IGNORE
:
383 atomic_dec(&proc_event_num_listeners
);
391 cn_proc_ack(err
, msg
->seq
, msg
->ack
);
395 * cn_proc_init - initialization entry point
397 * Adds the connector callback to the connector driver.
399 static int __init
cn_proc_init(void)
401 int err
= cn_add_callback(&cn_proc_event_id
,
405 pr_warn("cn_proc failed to register\n");
411 module_init(cn_proc_init
);