Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Helsley | caf3c9d | 2006-01-09 20:52:40 -0800 | [diff] [blame] | 27 | #include <linux/ktime.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 28 | #include <linux/init.h> |
Matt Helsley | 1d31a4e | 2006-06-23 02:05:42 -0700 | [diff] [blame] | 29 | #include <linux/connector.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/gfp.h> |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 31 | #include <linux/ptrace.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 32 | #include <linux/atomic.h> |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 33 | #include <linux/pid_namespace.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 34 | |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 35 | #include <asm/unaligned.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 36 | |
| 37 | #include <linux/cn_proc.h> |
| 38 | |
| 39 | #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event)) |
| 40 | |
| 41 | static atomic_t proc_event_num_listeners = ATOMIC_INIT(0); |
| 42 | static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC }; |
| 43 | |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 44 | /* proc_event_counts is used as the sequence number of the netlink message */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 45 | static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 }; |
| 46 | |
| 47 | static inline void get_seq(__u32 *ts, int *cpu) |
| 48 | { |
Christoph Lameter | 3ea9f68 | 2010-12-08 17:42:23 +0100 | [diff] [blame] | 49 | preempt_disable(); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 50 | *ts = __this_cpu_inc_return(proc_event_counts) - 1; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 51 | *cpu = smp_processor_id(); |
Christoph Lameter | 3ea9f68 | 2010-12-08 17:42:23 +0100 | [diff] [blame] | 52 | preempt_enable(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 60 | struct timespec ts; |
Oleg Nesterov | 9e8f90d | 2011-07-28 18:26:32 -0700 | [diff] [blame] | 61 | struct task_struct *parent; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 62 | |
| 63 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 64 | return; |
| 65 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 66 | msg = (struct cn_msg *)buffer; |
| 67 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 68 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 69 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 70 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 71 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 72 | ev->what = PROC_EVENT_FORK; |
Oleg Nesterov | 9e8f90d | 2011-07-28 18:26:32 -0700 | [diff] [blame] | 73 | 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 Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 78 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 84 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 85 | /* If cn_netlink_send() failed, the data is not sent */ |
| 86 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 87 | } |
| 88 | |
| 89 | void proc_exec_connector(struct task_struct *task) |
| 90 | { |
| 91 | struct cn_msg *msg; |
| 92 | struct proc_event *ev; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 93 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 94 | __u8 buffer[CN_PROC_MSG_SIZE]; |
| 95 | |
| 96 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 97 | return; |
| 98 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 99 | msg = (struct cn_msg *)buffer; |
| 100 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 101 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 102 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 103 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 104 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 105 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 112 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 113 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 114 | } |
| 115 | |
| 116 | void 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 121 | struct timespec ts; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 122 | const struct cred *cred; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 123 | |
| 124 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 125 | return; |
| 126 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 127 | msg = (struct cn_msg *)buffer; |
| 128 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 129 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 130 | ev->what = which_id; |
| 131 | ev->event_data.id.process_pid = task->pid; |
| 132 | ev->event_data.id.process_tgid = task->tgid; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 133 | rcu_read_lock(); |
| 134 | cred = __task_cred(task); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 135 | if (which_id == PROC_EVENT_UID) { |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 136 | 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 Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 138 | } else if (which_id == PROC_EVENT_GID) { |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 139 | 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 Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 141 | } else { |
| 142 | rcu_read_unlock(); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 143 | return; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 144 | } |
| 145 | rcu_read_unlock(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 146 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 147 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 148 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 149 | |
| 150 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 151 | msg->ack = 0; /* not used */ |
| 152 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 153 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 154 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 155 | } |
| 156 | |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 157 | void 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 169 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 170 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 180 | msg->flags = 0; /* not used */ |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 181 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 182 | } |
| 183 | |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 184 | void 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 Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 190 | |
| 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 196 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 197 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 215 | msg->flags = 0; /* not used */ |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 216 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 217 | } |
| 218 | |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 219 | void 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 231 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 232 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 243 | msg->flags = 0; /* not used */ |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 244 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 245 | } |
| 246 | |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 247 | void 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 259 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 260 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 270 | msg->flags = 0; /* not used */ |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 271 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 272 | } |
| 273 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 274 | void 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 279 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 280 | |
| 281 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 282 | return; |
| 283 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 284 | msg = (struct cn_msg *)buffer; |
| 285 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 286 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 287 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 288 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 289 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 290 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 299 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 300 | 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 | */ |
| 311 | static 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 316 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 317 | |
| 318 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 319 | return; |
| 320 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 321 | msg = (struct cn_msg *)buffer; |
| 322 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 323 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 324 | msg->seq = rcvd_seq; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 325 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 326 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 327 | 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 Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame^] | 333 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 334 | 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 Boyd | f0b2593 | 2009-10-06 01:39:51 -0700 | [diff] [blame] | 341 | static void cn_proc_mcast_ctl(struct cn_msg *msg, |
| 342 | struct netlink_skb_parms *nsp) |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 343 | { |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 344 | 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. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 350 | /* |
| 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 Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 359 | /* Can only change if privileged. */ |
| 360 | if (!capable(CAP_NET_ADMIN)) { |
| 361 | err = EPERM; |
| 362 | goto out; |
| 363 | } |
| 364 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 365 | mc_op = (enum proc_cn_mcast_op *)msg->data; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 366 | 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 Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 377 | |
| 378 | out: |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 379 | 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 | */ |
| 387 | static int __init cn_proc_init(void) |
| 388 | { |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 389 | 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 Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 394 | return err; |
| 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | module_init(cn_proc_init); |