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 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 35 | #include <linux/cn_proc.h> |
| 36 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 37 | /* |
| 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. |
| 43 | */ |
| 44 | #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4) |
| 45 | |
| 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) |
| 48 | { |
| 49 | BUILD_BUG_ON(sizeof(struct cn_msg) != 20); |
| 50 | return (struct cn_msg *)(buffer + 4); |
| 51 | } |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 52 | |
| 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 }; |
| 55 | |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 56 | /* 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] | 57 | static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 }; |
| 58 | |
| 59 | static inline void get_seq(__u32 *ts, int *cpu) |
| 60 | { |
Christoph Lameter | 3ea9f68 | 2010-12-08 17:42:23 +0100 | [diff] [blame] | 61 | preempt_disable(); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 62 | *ts = __this_cpu_inc_return(proc_event_counts) - 1; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 63 | *cpu = smp_processor_id(); |
Christoph Lameter | 3ea9f68 | 2010-12-08 17:42:23 +0100 | [diff] [blame] | 64 | preempt_enable(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void proc_fork_connector(struct task_struct *task) |
| 68 | { |
| 69 | struct cn_msg *msg; |
| 70 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 71 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 72 | struct timespec ts; |
Oleg Nesterov | 9e8f90d | 2011-07-28 18:26:32 -0700 | [diff] [blame] | 73 | struct task_struct *parent; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 74 | |
| 75 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 76 | return; |
| 77 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 78 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 79 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 80 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 81 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 82 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 83 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 84 | ev->what = PROC_EVENT_FORK; |
Oleg Nesterov | 9e8f90d | 2011-07-28 18:26:32 -0700 | [diff] [blame] | 85 | rcu_read_lock(); |
| 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; |
| 89 | rcu_read_unlock(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 90 | ev->event_data.fork.child_pid = task->pid; |
| 91 | ev->event_data.fork.child_tgid = task->tgid; |
| 92 | |
| 93 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 94 | msg->ack = 0; /* not used */ |
| 95 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 96 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 97 | /* If cn_netlink_send() failed, the data is not sent */ |
| 98 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 99 | } |
| 100 | |
| 101 | void proc_exec_connector(struct task_struct *task) |
| 102 | { |
| 103 | struct cn_msg *msg; |
| 104 | struct proc_event *ev; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 105 | struct timespec ts; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 106 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 107 | |
| 108 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 109 | return; |
| 110 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 111 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 112 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 113 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 114 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 115 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 116 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 117 | ev->what = PROC_EVENT_EXEC; |
| 118 | ev->event_data.exec.process_pid = task->pid; |
| 119 | ev->event_data.exec.process_tgid = task->tgid; |
| 120 | |
| 121 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 122 | msg->ack = 0; /* not used */ |
| 123 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 124 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 125 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 126 | } |
| 127 | |
| 128 | void proc_id_connector(struct task_struct *task, int which_id) |
| 129 | { |
| 130 | struct cn_msg *msg; |
| 131 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 132 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 133 | struct timespec ts; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 134 | const struct cred *cred; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 135 | |
| 136 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 137 | return; |
| 138 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 139 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 140 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 141 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 142 | ev->what = which_id; |
| 143 | ev->event_data.id.process_pid = task->pid; |
| 144 | ev->event_data.id.process_tgid = task->tgid; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 145 | rcu_read_lock(); |
| 146 | cred = __task_cred(task); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 147 | if (which_id == PROC_EVENT_UID) { |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 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); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 150 | } else if (which_id == PROC_EVENT_GID) { |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 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); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 153 | } else { |
| 154 | rcu_read_unlock(); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 155 | return; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 156 | } |
| 157 | rcu_read_unlock(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 158 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 159 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 160 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 161 | |
| 162 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 163 | msg->ack = 0; /* not used */ |
| 164 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 165 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 166 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 167 | } |
| 168 | |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 169 | void proc_sid_connector(struct task_struct *task) |
| 170 | { |
| 171 | struct cn_msg *msg; |
| 172 | struct proc_event *ev; |
| 173 | struct timespec ts; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 174 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 175 | |
| 176 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 177 | return; |
| 178 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 179 | msg = buffer_to_cn_msg(buffer); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 180 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 181 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 182 | get_seq(&msg->seq, &ev->cpu); |
| 183 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 184 | ev->timestamp_ns = timespec_to_ns(&ts); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 185 | ev->what = PROC_EVENT_SID; |
| 186 | ev->event_data.sid.process_pid = task->pid; |
| 187 | ev->event_data.sid.process_tgid = task->tgid; |
| 188 | |
| 189 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 190 | msg->ack = 0; /* not used */ |
| 191 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 192 | msg->flags = 0; /* not used */ |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 193 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 194 | } |
| 195 | |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 196 | void proc_ptrace_connector(struct task_struct *task, int ptrace_id) |
| 197 | { |
| 198 | struct cn_msg *msg; |
| 199 | struct proc_event *ev; |
| 200 | struct timespec ts; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 201 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 202 | |
| 203 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 204 | return; |
| 205 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 206 | msg = buffer_to_cn_msg(buffer); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 207 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 208 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 209 | get_seq(&msg->seq, &ev->cpu); |
| 210 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 211 | ev->timestamp_ns = timespec_to_ns(&ts); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 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; |
| 221 | } else |
| 222 | return; |
| 223 | |
| 224 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 225 | msg->ack = 0; /* not used */ |
| 226 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 227 | msg->flags = 0; /* not used */ |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 228 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 229 | } |
| 230 | |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 231 | void proc_comm_connector(struct task_struct *task) |
| 232 | { |
| 233 | struct cn_msg *msg; |
| 234 | struct proc_event *ev; |
| 235 | struct timespec ts; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 236 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 237 | |
| 238 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 239 | return; |
| 240 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 241 | msg = buffer_to_cn_msg(buffer); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 242 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 243 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 244 | get_seq(&msg->seq, &ev->cpu); |
| 245 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 246 | ev->timestamp_ns = timespec_to_ns(&ts); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 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); |
| 251 | |
| 252 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 253 | msg->ack = 0; /* not used */ |
| 254 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 255 | msg->flags = 0; /* not used */ |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 256 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 257 | } |
| 258 | |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 259 | void proc_coredump_connector(struct task_struct *task) |
| 260 | { |
| 261 | struct cn_msg *msg; |
| 262 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 263 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 264 | struct timespec ts; |
| 265 | |
| 266 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 267 | return; |
| 268 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 269 | msg = buffer_to_cn_msg(buffer); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 270 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 271 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 272 | get_seq(&msg->seq, &ev->cpu); |
| 273 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 274 | ev->timestamp_ns = timespec_to_ns(&ts); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 275 | ev->what = PROC_EVENT_COREDUMP; |
| 276 | ev->event_data.coredump.process_pid = task->pid; |
| 277 | ev->event_data.coredump.process_tgid = task->tgid; |
| 278 | |
| 279 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 280 | msg->ack = 0; /* not used */ |
| 281 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 282 | msg->flags = 0; /* not used */ |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 283 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 284 | } |
| 285 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 286 | void proc_exit_connector(struct task_struct *task) |
| 287 | { |
| 288 | struct cn_msg *msg; |
| 289 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 290 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 291 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 292 | |
| 293 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 294 | return; |
| 295 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 296 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 297 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 298 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 299 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 300 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 301 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 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; |
| 307 | |
| 308 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 309 | msg->ack = 0; /* not used */ |
| 310 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 311 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 312 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Send an acknowledgement message to userspace |
| 317 | * |
| 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 |
| 321 | * mechanisms. |
| 322 | */ |
| 323 | static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack) |
| 324 | { |
| 325 | struct cn_msg *msg; |
| 326 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 327 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 328 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 329 | |
| 330 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 331 | return; |
| 332 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 333 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 334 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 335 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 336 | msg->seq = rcvd_seq; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 337 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 338 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 339 | ev->cpu = -1; |
| 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); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 345 | msg->flags = 0; /* not used */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 346 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * cn_proc_mcast_ctl |
| 351 | * @data: message sent from userspace via the connector |
| 352 | */ |
Stephen Boyd | f0b2593 | 2009-10-06 01:39:51 -0700 | [diff] [blame] | 353 | static void cn_proc_mcast_ctl(struct cn_msg *msg, |
| 354 | struct netlink_skb_parms *nsp) |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 355 | { |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 356 | enum proc_cn_mcast_op *mc_op = NULL; |
| 357 | int err = 0; |
| 358 | |
| 359 | if (msg->len != sizeof(*mc_op)) |
| 360 | return; |
| 361 | |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 362 | /* |
| 363 | * Events are reported with respect to the initial pid |
| 364 | * and user namespaces so ignore requestors from |
| 365 | * other namespaces. |
| 366 | */ |
| 367 | if ((current_user_ns() != &init_user_ns) || |
| 368 | (task_active_pid_ns(current) != &init_pid_ns)) |
| 369 | return; |
| 370 | |
Kees Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 371 | /* Can only change if privileged. */ |
| 372 | if (!capable(CAP_NET_ADMIN)) { |
| 373 | err = EPERM; |
| 374 | goto out; |
| 375 | } |
| 376 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 377 | mc_op = (enum proc_cn_mcast_op *)msg->data; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 378 | switch (*mc_op) { |
| 379 | case PROC_CN_MCAST_LISTEN: |
| 380 | atomic_inc(&proc_event_num_listeners); |
| 381 | break; |
| 382 | case PROC_CN_MCAST_IGNORE: |
| 383 | atomic_dec(&proc_event_num_listeners); |
| 384 | break; |
| 385 | default: |
| 386 | err = EINVAL; |
| 387 | break; |
| 388 | } |
Kees Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 389 | |
| 390 | out: |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 391 | cn_proc_ack(err, msg->seq, msg->ack); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * cn_proc_init - initialization entry point |
| 396 | * |
| 397 | * Adds the connector callback to the connector driver. |
| 398 | */ |
| 399 | static int __init cn_proc_init(void) |
| 400 | { |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 401 | int err = cn_add_callback(&cn_proc_event_id, |
| 402 | "cn_proc", |
| 403 | &cn_proc_mcast_ctl); |
| 404 | if (err) { |
| 405 | pr_warn("cn_proc failed to register\n"); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 406 | return err; |
| 407 | } |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | module_init(cn_proc_init); |