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