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