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 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Matt Helsley | caf3c9d | 2006-01-09 20:52:40 -0800 | [diff] [blame] | 26 | #include <linux/ktime.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 27 | #include <linux/init.h> |
Matt Helsley | 1d31a4e | 2006-06-23 02:05:42 -0700 | [diff] [blame] | 28 | #include <linux/connector.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/gfp.h> |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 30 | #include <linux/ptrace.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 31 | #include <linux/atomic.h> |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 32 | #include <linux/pid_namespace.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 33 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 34 | #include <linux/cn_proc.h> |
| 35 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 36 | /* |
| 37 | * Size of a cn_msg followed by a proc_event structure. Since the |
| 38 | * sizeof struct cn_msg is a multiple of 4 bytes, but not 8 bytes, we |
| 39 | * add one 4-byte word to the size here, and then start the actual |
| 40 | * cn_msg structure 4 bytes into the stack buffer. The result is that |
| 41 | * the immediately following proc_event structure is aligned to 8 bytes. |
| 42 | */ |
| 43 | #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4) |
| 44 | |
| 45 | /* See comment above; we test our assumption about sizeof struct cn_msg here. */ |
| 46 | static inline struct cn_msg *buffer_to_cn_msg(__u8 *buffer) |
| 47 | { |
| 48 | BUILD_BUG_ON(sizeof(struct cn_msg) != 20); |
| 49 | return (struct cn_msg *)(buffer + 4); |
| 50 | } |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 51 | |
| 52 | static atomic_t proc_event_num_listeners = ATOMIC_INIT(0); |
| 53 | static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC }; |
| 54 | |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 55 | /* 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] | 56 | static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 }; |
| 57 | |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 58 | static inline void send_msg(struct cn_msg *msg) |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 59 | { |
Christoph Lameter | 3ea9f68 | 2010-12-08 17:42:23 +0100 | [diff] [blame] | 60 | preempt_disable(); |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 61 | |
| 62 | msg->seq = __this_cpu_inc_return(proc_event_counts) - 1; |
| 63 | ((struct proc_event *)msg->data)->cpu = smp_processor_id(); |
| 64 | |
| 65 | /* |
| 66 | * Preemption remains disabled during send to ensure the messages are |
| 67 | * ordered according to their sequence numbers. |
| 68 | * |
| 69 | * If cn_netlink_send() fails, the data is not sent. |
| 70 | */ |
| 71 | cn_netlink_send(msg, 0, CN_IDX_PROC, GFP_NOWAIT); |
| 72 | |
Christoph Lameter | 3ea9f68 | 2010-12-08 17:42:23 +0100 | [diff] [blame] | 73 | preempt_enable(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void proc_fork_connector(struct task_struct *task) |
| 77 | { |
| 78 | struct cn_msg *msg; |
| 79 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 80 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Oleg Nesterov | 9e8f90d | 2011-07-28 18:26:32 -0700 | [diff] [blame] | 81 | struct task_struct *parent; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 82 | |
| 83 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 84 | return; |
| 85 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 86 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 87 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 88 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 89 | ev->timestamp_ns = ktime_get_ns(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 90 | ev->what = PROC_EVENT_FORK; |
Oleg Nesterov | 9e8f90d | 2011-07-28 18:26:32 -0700 | [diff] [blame] | 91 | rcu_read_lock(); |
| 92 | parent = rcu_dereference(task->real_parent); |
| 93 | ev->event_data.fork.parent_pid = parent->pid; |
| 94 | ev->event_data.fork.parent_tgid = parent->tgid; |
| 95 | rcu_read_unlock(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 96 | ev->event_data.fork.child_pid = task->pid; |
| 97 | ev->event_data.fork.child_tgid = task->tgid; |
| 98 | |
| 99 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 100 | msg->ack = 0; /* not used */ |
| 101 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 102 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 103 | send_msg(msg); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void proc_exec_connector(struct task_struct *task) |
| 107 | { |
| 108 | struct cn_msg *msg; |
| 109 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 110 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 111 | |
| 112 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 113 | return; |
| 114 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 115 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 116 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 117 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 118 | ev->timestamp_ns = ktime_get_ns(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 119 | ev->what = PROC_EVENT_EXEC; |
| 120 | ev->event_data.exec.process_pid = task->pid; |
| 121 | ev->event_data.exec.process_tgid = task->tgid; |
| 122 | |
| 123 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 124 | msg->ack = 0; /* not used */ |
| 125 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 126 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 127 | send_msg(msg); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void proc_id_connector(struct task_struct *task, int which_id) |
| 131 | { |
| 132 | struct cn_msg *msg; |
| 133 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 134 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 135 | const struct cred *cred; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 136 | |
| 137 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 138 | return; |
| 139 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 140 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 141 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 142 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 143 | ev->what = which_id; |
| 144 | ev->event_data.id.process_pid = task->pid; |
| 145 | ev->event_data.id.process_tgid = task->tgid; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 146 | rcu_read_lock(); |
| 147 | cred = __task_cred(task); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 148 | if (which_id == PROC_EVENT_UID) { |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 149 | ev->event_data.id.r.ruid = from_kuid_munged(&init_user_ns, cred->uid); |
| 150 | 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] | 151 | } else if (which_id == PROC_EVENT_GID) { |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 152 | ev->event_data.id.r.rgid = from_kgid_munged(&init_user_ns, cred->gid); |
| 153 | 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] | 154 | } else { |
| 155 | rcu_read_unlock(); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 156 | return; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 157 | } |
| 158 | rcu_read_unlock(); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 159 | ev->timestamp_ns = ktime_get_ns(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 160 | |
| 161 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 162 | msg->ack = 0; /* not used */ |
| 163 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 164 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 165 | send_msg(msg); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 168 | void proc_sid_connector(struct task_struct *task) |
| 169 | { |
| 170 | struct cn_msg *msg; |
| 171 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 172 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 173 | |
| 174 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 175 | return; |
| 176 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 177 | msg = buffer_to_cn_msg(buffer); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 178 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 179 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 180 | ev->timestamp_ns = ktime_get_ns(); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 181 | ev->what = PROC_EVENT_SID; |
| 182 | ev->event_data.sid.process_pid = task->pid; |
| 183 | ev->event_data.sid.process_tgid = task->tgid; |
| 184 | |
| 185 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 186 | msg->ack = 0; /* not used */ |
| 187 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 188 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 189 | send_msg(msg); |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 192 | void proc_ptrace_connector(struct task_struct *task, int ptrace_id) |
| 193 | { |
| 194 | struct cn_msg *msg; |
| 195 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 196 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 197 | |
| 198 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 199 | return; |
| 200 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 201 | msg = buffer_to_cn_msg(buffer); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 202 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 203 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 204 | ev->timestamp_ns = ktime_get_ns(); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 205 | ev->what = PROC_EVENT_PTRACE; |
| 206 | ev->event_data.ptrace.process_pid = task->pid; |
| 207 | ev->event_data.ptrace.process_tgid = task->tgid; |
| 208 | if (ptrace_id == PTRACE_ATTACH) { |
| 209 | ev->event_data.ptrace.tracer_pid = current->pid; |
| 210 | ev->event_data.ptrace.tracer_tgid = current->tgid; |
| 211 | } else if (ptrace_id == PTRACE_DETACH) { |
| 212 | ev->event_data.ptrace.tracer_pid = 0; |
| 213 | ev->event_data.ptrace.tracer_tgid = 0; |
| 214 | } else |
| 215 | return; |
| 216 | |
| 217 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 218 | msg->ack = 0; /* not used */ |
| 219 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 220 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 221 | send_msg(msg); |
Vladimir Zapolskiy | f701e5b | 2011-07-15 20:45:18 +0300 | [diff] [blame] | 222 | } |
| 223 | |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 224 | void proc_comm_connector(struct task_struct *task) |
| 225 | { |
| 226 | struct cn_msg *msg; |
| 227 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 228 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 229 | |
| 230 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 231 | return; |
| 232 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 233 | msg = buffer_to_cn_msg(buffer); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 234 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 235 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 236 | ev->timestamp_ns = ktime_get_ns(); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 237 | ev->what = PROC_EVENT_COMM; |
| 238 | ev->event_data.comm.process_pid = task->pid; |
| 239 | ev->event_data.comm.process_tgid = task->tgid; |
| 240 | get_task_comm(ev->event_data.comm.comm, task); |
| 241 | |
| 242 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 243 | msg->ack = 0; /* not used */ |
| 244 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 245 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 246 | send_msg(msg); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 249 | void proc_coredump_connector(struct task_struct *task) |
| 250 | { |
| 251 | struct cn_msg *msg; |
| 252 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 253 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 254 | |
| 255 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 256 | return; |
| 257 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 258 | msg = buffer_to_cn_msg(buffer); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 259 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 260 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 261 | ev->timestamp_ns = ktime_get_ns(); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 262 | ev->what = PROC_EVENT_COREDUMP; |
| 263 | ev->event_data.coredump.process_pid = task->pid; |
| 264 | ev->event_data.coredump.process_tgid = task->tgid; |
| 265 | |
| 266 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 267 | msg->ack = 0; /* not used */ |
| 268 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 269 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 270 | send_msg(msg); |
Jesper Derehag | 2b5faa4 | 2013-03-19 20:50:05 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 273 | void proc_exit_connector(struct task_struct *task) |
| 274 | { |
| 275 | struct cn_msg *msg; |
| 276 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 277 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 278 | |
| 279 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 280 | return; |
| 281 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 282 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 283 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 284 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 285 | ev->timestamp_ns = ktime_get_ns(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 286 | ev->what = PROC_EVENT_EXIT; |
| 287 | ev->event_data.exit.process_pid = task->pid; |
| 288 | ev->event_data.exit.process_tgid = task->tgid; |
| 289 | ev->event_data.exit.exit_code = task->exit_code; |
| 290 | ev->event_data.exit.exit_signal = task->exit_signal; |
| 291 | |
| 292 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 293 | msg->ack = 0; /* not used */ |
| 294 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 295 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 296 | send_msg(msg); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Send an acknowledgement message to userspace |
| 301 | * |
| 302 | * Use 0 for success, EFOO otherwise. |
| 303 | * Note: this is the negative of conventional kernel error |
| 304 | * values because it's not being returned via syscall return |
| 305 | * mechanisms. |
| 306 | */ |
| 307 | static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack) |
| 308 | { |
| 309 | struct cn_msg *msg; |
| 310 | struct proc_event *ev; |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 311 | __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 312 | |
| 313 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 314 | return; |
| 315 | |
Chris Metcalf | 1ca1a4c | 2013-11-14 12:09:21 -0500 | [diff] [blame] | 316 | msg = buffer_to_cn_msg(buffer); |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 317 | ev = (struct proc_event *)msg->data; |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 318 | memset(&ev->event_data, 0, sizeof(ev->event_data)); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 319 | msg->seq = rcvd_seq; |
Thomas Gleixner | 9e93f21 | 2014-07-16 21:04:40 +0000 | [diff] [blame] | 320 | ev->timestamp_ns = ktime_get_ns(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 321 | ev->cpu = -1; |
| 322 | ev->what = PROC_EVENT_NONE; |
| 323 | ev->event_data.ack.err = err; |
| 324 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 325 | msg->ack = rcvd_ack + 1; |
| 326 | msg->len = sizeof(*ev); |
Mathias Krause | e727ca8 | 2013-09-30 22:03:06 +0200 | [diff] [blame] | 327 | msg->flags = 0; /* not used */ |
Aaron Campbell | ab8ed95 | 2016-06-24 10:05:32 -0300 | [diff] [blame] | 328 | send_msg(msg); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * cn_proc_mcast_ctl |
| 333 | * @data: message sent from userspace via the connector |
| 334 | */ |
Stephen Boyd | f0b2593 | 2009-10-06 01:39:51 -0700 | [diff] [blame] | 335 | static void cn_proc_mcast_ctl(struct cn_msg *msg, |
| 336 | struct netlink_skb_parms *nsp) |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 337 | { |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 338 | enum proc_cn_mcast_op *mc_op = NULL; |
| 339 | int err = 0; |
| 340 | |
| 341 | if (msg->len != sizeof(*mc_op)) |
| 342 | return; |
| 343 | |
Eric W. Biederman | 9582d90 | 2012-02-07 16:48:16 -0800 | [diff] [blame] | 344 | /* |
| 345 | * Events are reported with respect to the initial pid |
| 346 | * and user namespaces so ignore requestors from |
| 347 | * other namespaces. |
| 348 | */ |
| 349 | if ((current_user_ns() != &init_user_ns) || |
| 350 | (task_active_pid_ns(current) != &init_pid_ns)) |
| 351 | return; |
| 352 | |
Kees Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 353 | /* Can only change if privileged. */ |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 354 | if (!__netlink_ns_capable(nsp, &init_user_ns, CAP_NET_ADMIN)) { |
Kees Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 355 | err = EPERM; |
| 356 | goto out; |
| 357 | } |
| 358 | |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 359 | mc_op = (enum proc_cn_mcast_op *)msg->data; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 360 | switch (*mc_op) { |
| 361 | case PROC_CN_MCAST_LISTEN: |
| 362 | atomic_inc(&proc_event_num_listeners); |
| 363 | break; |
| 364 | case PROC_CN_MCAST_IGNORE: |
| 365 | atomic_dec(&proc_event_num_listeners); |
| 366 | break; |
| 367 | default: |
| 368 | err = EINVAL; |
| 369 | break; |
| 370 | } |
Kees Cook | e70ab97 | 2013-02-25 21:32:25 +0000 | [diff] [blame] | 371 | |
| 372 | out: |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 373 | cn_proc_ack(err, msg->seq, msg->ack); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * cn_proc_init - initialization entry point |
| 378 | * |
| 379 | * Adds the connector callback to the connector driver. |
| 380 | */ |
| 381 | static int __init cn_proc_init(void) |
| 382 | { |
Valentin Ilie | f3c48ec | 2012-07-14 13:08:29 +0000 | [diff] [blame] | 383 | int err = cn_add_callback(&cn_proc_event_id, |
| 384 | "cn_proc", |
| 385 | &cn_proc_mcast_ctl); |
| 386 | if (err) { |
| 387 | pr_warn("cn_proc failed to register\n"); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 388 | return err; |
| 389 | } |
| 390 | return 0; |
| 391 | } |
Paul Gortmaker | 8297f2d | 2016-07-04 17:50:58 -0400 | [diff] [blame] | 392 | device_initcall(cn_proc_init); |