blob: c73fc2b74de2a1dd0665bde5693b1fce03f6d6c5 [file] [log] [blame]
Matt Helsley9f460802005-11-07 00:59:16 -08001/*
2 * cn_proc.c - process events connector
3 *
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.
8 *
9 *
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.
14 *
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.
19 *
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
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
Matt Helsleycaf3c9d2006-01-09 20:52:40 -080027#include <linux/ktime.h>
Matt Helsley9f460802005-11-07 00:59:16 -080028#include <linux/init.h>
Matt Helsley1d31a4e2006-06-23 02:05:42 -070029#include <linux/connector.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Vladimir Zapolskiyf701e5b2011-07-15 20:45:18 +030031#include <linux/ptrace.h>
Arun Sharma600634972011-07-26 16:09:06 -070032#include <linux/atomic.h>
Eric W. Biederman9582d902012-02-07 16:48:16 -080033#include <linux/pid_namespace.h>
Arun Sharma600634972011-07-26 16:09:06 -070034
Erik Jacobsonaf3e0952007-01-05 16:37:05 -080035#include <asm/unaligned.h>
Matt Helsley9f460802005-11-07 00:59:16 -080036
37#include <linux/cn_proc.h>
38
39#define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
40
41static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
42static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
43
David S. Millercc398c22006-01-08 01:03:34 -080044/* proc_event_counts is used as the sequence number of the netlink message */
Matt Helsley9f460802005-11-07 00:59:16 -080045static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
46
47static inline void get_seq(__u32 *ts, int *cpu)
48{
Christoph Lameter3ea9f682010-12-08 17:42:23 +010049 preempt_disable();
Valentin Ilief3c48ec2012-07-14 13:08:29 +000050 *ts = __this_cpu_inc_return(proc_event_counts) - 1;
Matt Helsley9f460802005-11-07 00:59:16 -080051 *cpu = smp_processor_id();
Christoph Lameter3ea9f682010-12-08 17:42:23 +010052 preempt_enable();
Matt Helsley9f460802005-11-07 00:59:16 -080053}
54
55void proc_fork_connector(struct task_struct *task)
56{
57 struct cn_msg *msg;
58 struct proc_event *ev;
59 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070060 struct timespec ts;
Oleg Nesterov9e8f90d2011-07-28 18:26:32 -070061 struct task_struct *parent;
Matt Helsley9f460802005-11-07 00:59:16 -080062
63 if (atomic_read(&proc_event_num_listeners) < 1)
64 return;
65
Valentin Ilief3c48ec2012-07-14 13:08:29 +000066 msg = (struct cn_msg *)buffer;
67 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +020068 memset(&ev->event_data, 0, sizeof(ev->event_data));
Matt Helsley9f460802005-11-07 00:59:16 -080069 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070070 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -080071 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -080072 ev->what = PROC_EVENT_FORK;
Oleg Nesterov9e8f90d2011-07-28 18:26:32 -070073 rcu_read_lock();
74 parent = rcu_dereference(task->real_parent);
75 ev->event_data.fork.parent_pid = parent->pid;
76 ev->event_data.fork.parent_tgid = parent->tgid;
77 rcu_read_unlock();
Matt Helsley9f460802005-11-07 00:59:16 -080078 ev->event_data.fork.child_pid = task->pid;
79 ev->event_data.fork.child_tgid = task->tgid;
80
81 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
82 msg->ack = 0; /* not used */
83 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +020084 msg->flags = 0; /* not used */
Matt Helsley9f460802005-11-07 00:59:16 -080085 /* If cn_netlink_send() failed, the data is not sent */
86 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
87}
88
89void proc_exec_connector(struct task_struct *task)
90{
91 struct cn_msg *msg;
92 struct proc_event *ev;
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070093 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -080094 __u8 buffer[CN_PROC_MSG_SIZE];
95
96 if (atomic_read(&proc_event_num_listeners) < 1)
97 return;
98
Valentin Ilief3c48ec2012-07-14 13:08:29 +000099 msg = (struct cn_msg *)buffer;
100 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200101 memset(&ev->event_data, 0, sizeof(ev->event_data));
Matt Helsley9f460802005-11-07 00:59:16 -0800102 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700103 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800104 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800105 ev->what = PROC_EVENT_EXEC;
106 ev->event_data.exec.process_pid = task->pid;
107 ev->event_data.exec.process_tgid = task->tgid;
108
109 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
110 msg->ack = 0; /* not used */
111 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200112 msg->flags = 0; /* not used */
Matt Helsley9f460802005-11-07 00:59:16 -0800113 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
114}
115
116void proc_id_connector(struct task_struct *task, int which_id)
117{
118 struct cn_msg *msg;
119 struct proc_event *ev;
120 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700121 struct timespec ts;
David Howellsc69e8d92008-11-14 10:39:19 +1100122 const struct cred *cred;
Matt Helsley9f460802005-11-07 00:59:16 -0800123
124 if (atomic_read(&proc_event_num_listeners) < 1)
125 return;
126
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000127 msg = (struct cn_msg *)buffer;
128 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200129 memset(&ev->event_data, 0, sizeof(ev->event_data));
Matt Helsley9f460802005-11-07 00:59:16 -0800130 ev->what = which_id;
131 ev->event_data.id.process_pid = task->pid;
132 ev->event_data.id.process_tgid = task->tgid;
David Howellsc69e8d92008-11-14 10:39:19 +1100133 rcu_read_lock();
134 cred = __task_cred(task);
Matt Helsley9f460802005-11-07 00:59:16 -0800135 if (which_id == PROC_EVENT_UID) {
Eric W. Biederman9582d902012-02-07 16:48:16 -0800136 ev->event_data.id.r.ruid = from_kuid_munged(&init_user_ns, cred->uid);
137 ev->event_data.id.e.euid = from_kuid_munged(&init_user_ns, cred->euid);
Matt Helsley9f460802005-11-07 00:59:16 -0800138 } else if (which_id == PROC_EVENT_GID) {
Eric W. Biederman9582d902012-02-07 16:48:16 -0800139 ev->event_data.id.r.rgid = from_kgid_munged(&init_user_ns, cred->gid);
140 ev->event_data.id.e.egid = from_kgid_munged(&init_user_ns, cred->egid);
David Howellsc69e8d92008-11-14 10:39:19 +1100141 } else {
142 rcu_read_unlock();
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000143 return;
David Howellsc69e8d92008-11-14 10:39:19 +1100144 }
145 rcu_read_unlock();
Matt Helsley9f460802005-11-07 00:59:16 -0800146 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700147 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800148 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800149
150 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
151 msg->ack = 0; /* not used */
152 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200153 msg->flags = 0; /* not used */
Matt Helsley9f460802005-11-07 00:59:16 -0800154 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
155}
156
Scott James Remnant02b51df2009-09-22 16:43:44 -0700157void proc_sid_connector(struct task_struct *task)
158{
159 struct cn_msg *msg;
160 struct proc_event *ev;
161 struct timespec ts;
162 __u8 buffer[CN_PROC_MSG_SIZE];
163
164 if (atomic_read(&proc_event_num_listeners) < 1)
165 return;
166
167 msg = (struct cn_msg *)buffer;
168 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200169 memset(&ev->event_data, 0, sizeof(ev->event_data));
Scott James Remnant02b51df2009-09-22 16:43:44 -0700170 get_seq(&msg->seq, &ev->cpu);
171 ktime_get_ts(&ts); /* get high res monotonic timestamp */
172 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
173 ev->what = PROC_EVENT_SID;
174 ev->event_data.sid.process_pid = task->pid;
175 ev->event_data.sid.process_tgid = task->tgid;
176
177 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
178 msg->ack = 0; /* not used */
179 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200180 msg->flags = 0; /* not used */
Scott James Remnant02b51df2009-09-22 16:43:44 -0700181 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
182}
183
Vladimir Zapolskiyf701e5b2011-07-15 20:45:18 +0300184void proc_ptrace_connector(struct task_struct *task, int ptrace_id)
185{
186 struct cn_msg *msg;
187 struct proc_event *ev;
188 struct timespec ts;
189 __u8 buffer[CN_PROC_MSG_SIZE];
Vladimir Zapolskiyf701e5b2011-07-15 20:45:18 +0300190
191 if (atomic_read(&proc_event_num_listeners) < 1)
192 return;
193
194 msg = (struct cn_msg *)buffer;
195 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200196 memset(&ev->event_data, 0, sizeof(ev->event_data));
Vladimir Zapolskiyf701e5b2011-07-15 20:45:18 +0300197 get_seq(&msg->seq, &ev->cpu);
198 ktime_get_ts(&ts); /* get high res monotonic timestamp */
199 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
200 ev->what = PROC_EVENT_PTRACE;
201 ev->event_data.ptrace.process_pid = task->pid;
202 ev->event_data.ptrace.process_tgid = task->tgid;
203 if (ptrace_id == PTRACE_ATTACH) {
204 ev->event_data.ptrace.tracer_pid = current->pid;
205 ev->event_data.ptrace.tracer_tgid = current->tgid;
206 } else if (ptrace_id == PTRACE_DETACH) {
207 ev->event_data.ptrace.tracer_pid = 0;
208 ev->event_data.ptrace.tracer_tgid = 0;
209 } else
210 return;
211
212 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
213 msg->ack = 0; /* not used */
214 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200215 msg->flags = 0; /* not used */
Vladimir Zapolskiyf701e5b2011-07-15 20:45:18 +0300216 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
217}
218
Vladimir Zapolskiyf786ecb2011-09-21 09:26:44 +0000219void proc_comm_connector(struct task_struct *task)
220{
221 struct cn_msg *msg;
222 struct proc_event *ev;
223 struct timespec ts;
224 __u8 buffer[CN_PROC_MSG_SIZE];
225
226 if (atomic_read(&proc_event_num_listeners) < 1)
227 return;
228
229 msg = (struct cn_msg *)buffer;
230 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200231 memset(&ev->event_data, 0, sizeof(ev->event_data));
Vladimir Zapolskiyf786ecb2011-09-21 09:26:44 +0000232 get_seq(&msg->seq, &ev->cpu);
233 ktime_get_ts(&ts); /* get high res monotonic timestamp */
234 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
235 ev->what = PROC_EVENT_COMM;
236 ev->event_data.comm.process_pid = task->pid;
237 ev->event_data.comm.process_tgid = task->tgid;
238 get_task_comm(ev->event_data.comm.comm, task);
239
240 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
241 msg->ack = 0; /* not used */
242 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200243 msg->flags = 0; /* not used */
Vladimir Zapolskiyf786ecb2011-09-21 09:26:44 +0000244 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
245}
246
Jesper Derehag2b5faa42013-03-19 20:50:05 +0000247void proc_coredump_connector(struct task_struct *task)
248{
249 struct cn_msg *msg;
250 struct proc_event *ev;
251 __u8 buffer[CN_PROC_MSG_SIZE];
252 struct timespec ts;
253
254 if (atomic_read(&proc_event_num_listeners) < 1)
255 return;
256
257 msg = (struct cn_msg *)buffer;
258 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200259 memset(&ev->event_data, 0, sizeof(ev->event_data));
Jesper Derehag2b5faa42013-03-19 20:50:05 +0000260 get_seq(&msg->seq, &ev->cpu);
261 ktime_get_ts(&ts); /* get high res monotonic timestamp */
262 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
263 ev->what = PROC_EVENT_COREDUMP;
264 ev->event_data.coredump.process_pid = task->pid;
265 ev->event_data.coredump.process_tgid = task->tgid;
266
267 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
268 msg->ack = 0; /* not used */
269 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200270 msg->flags = 0; /* not used */
Jesper Derehag2b5faa42013-03-19 20:50:05 +0000271 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
272}
273
Matt Helsley9f460802005-11-07 00:59:16 -0800274void proc_exit_connector(struct task_struct *task)
275{
276 struct cn_msg *msg;
277 struct proc_event *ev;
278 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700279 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800280
281 if (atomic_read(&proc_event_num_listeners) < 1)
282 return;
283
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000284 msg = (struct cn_msg *)buffer;
285 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200286 memset(&ev->event_data, 0, sizeof(ev->event_data));
Matt Helsley9f460802005-11-07 00:59:16 -0800287 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700288 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800289 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800290 ev->what = PROC_EVENT_EXIT;
291 ev->event_data.exit.process_pid = task->pid;
292 ev->event_data.exit.process_tgid = task->tgid;
293 ev->event_data.exit.exit_code = task->exit_code;
294 ev->event_data.exit.exit_signal = task->exit_signal;
295
296 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
297 msg->ack = 0; /* not used */
298 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200299 msg->flags = 0; /* not used */
Matt Helsley9f460802005-11-07 00:59:16 -0800300 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
301}
302
303/*
304 * Send an acknowledgement message to userspace
305 *
306 * Use 0 for success, EFOO otherwise.
307 * Note: this is the negative of conventional kernel error
308 * values because it's not being returned via syscall return
309 * mechanisms.
310 */
311static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
312{
313 struct cn_msg *msg;
314 struct proc_event *ev;
315 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700316 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800317
318 if (atomic_read(&proc_event_num_listeners) < 1)
319 return;
320
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000321 msg = (struct cn_msg *)buffer;
322 ev = (struct proc_event *)msg->data;
Mathias Krausee727ca82013-09-30 22:03:06 +0200323 memset(&ev->event_data, 0, sizeof(ev->event_data));
Matt Helsley9f460802005-11-07 00:59:16 -0800324 msg->seq = rcvd_seq;
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700325 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800326 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800327 ev->cpu = -1;
328 ev->what = PROC_EVENT_NONE;
329 ev->event_data.ack.err = err;
330 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
331 msg->ack = rcvd_ack + 1;
332 msg->len = sizeof(*ev);
Mathias Krausee727ca82013-09-30 22:03:06 +0200333 msg->flags = 0; /* not used */
Matt Helsley9f460802005-11-07 00:59:16 -0800334 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
335}
336
337/**
338 * cn_proc_mcast_ctl
339 * @data: message sent from userspace via the connector
340 */
Stephen Boydf0b25932009-10-06 01:39:51 -0700341static void cn_proc_mcast_ctl(struct cn_msg *msg,
342 struct netlink_skb_parms *nsp)
Matt Helsley9f460802005-11-07 00:59:16 -0800343{
Matt Helsley9f460802005-11-07 00:59:16 -0800344 enum proc_cn_mcast_op *mc_op = NULL;
345 int err = 0;
346
347 if (msg->len != sizeof(*mc_op))
348 return;
349
Eric W. Biederman9582d902012-02-07 16:48:16 -0800350 /*
351 * Events are reported with respect to the initial pid
352 * and user namespaces so ignore requestors from
353 * other namespaces.
354 */
355 if ((current_user_ns() != &init_user_ns) ||
356 (task_active_pid_ns(current) != &init_pid_ns))
357 return;
358
Kees Cooke70ab972013-02-25 21:32:25 +0000359 /* Can only change if privileged. */
360 if (!capable(CAP_NET_ADMIN)) {
361 err = EPERM;
362 goto out;
363 }
364
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000365 mc_op = (enum proc_cn_mcast_op *)msg->data;
Matt Helsley9f460802005-11-07 00:59:16 -0800366 switch (*mc_op) {
367 case PROC_CN_MCAST_LISTEN:
368 atomic_inc(&proc_event_num_listeners);
369 break;
370 case PROC_CN_MCAST_IGNORE:
371 atomic_dec(&proc_event_num_listeners);
372 break;
373 default:
374 err = EINVAL;
375 break;
376 }
Kees Cooke70ab972013-02-25 21:32:25 +0000377
378out:
Matt Helsley9f460802005-11-07 00:59:16 -0800379 cn_proc_ack(err, msg->seq, msg->ack);
380}
381
382/*
383 * cn_proc_init - initialization entry point
384 *
385 * Adds the connector callback to the connector driver.
386 */
387static int __init cn_proc_init(void)
388{
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000389 int err = cn_add_callback(&cn_proc_event_id,
390 "cn_proc",
391 &cn_proc_mcast_ctl);
392 if (err) {
393 pr_warn("cn_proc failed to register\n");
Matt Helsley9f460802005-11-07 00:59:16 -0800394 return err;
395 }
396 return 0;
397}
398
399module_init(cn_proc_init);