1 // SPDX-License-Identifier: GPL-2.0-or-later 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. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/ktime.h> 13 #include <linux/init.h> 14 #include <linux/connector.h> 15 #include <linux/gfp.h> 16 #include <linux/ptrace.h> 17 #include <linux/atomic.h> 18 #include <linux/pid_namespace.h> 19 20 #include <linux/cn_proc.h> 21 #include <linux/local_lock.h> 22 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 } 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 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 }; 50 51 static int cn_filter(struct sock *dsk, struct sk_buff *skb, void *data) 52 { 53 __u32 what, exit_code, *ptr; 54 enum proc_cn_mcast_op mc_op; 55 uintptr_t val; 56 57 if (!dsk || !dsk->sk_user_data || !data) 58 return 0; 59 60 ptr = (__u32 *)data; 61 what = *ptr++; 62 exit_code = *ptr; 63 val = ((struct proc_input *)(dsk->sk_user_data))->event_type; 64 mc_op = ((struct proc_input *)(dsk->sk_user_data))->mcast_op; 65 66 if (mc_op == PROC_CN_MCAST_IGNORE) 67 return 1; 68 69 if ((__u32)val == PROC_EVENT_ALL) 70 return 0; 71 72 /* 73 * Drop packet if we have to report only non-zero exit status 74 * (PROC_EVENT_NONZERO_EXIT) and exit status is 0 75 */ 76 if (((__u32)val & PROC_EVENT_NONZERO_EXIT) && 77 (what == PROC_EVENT_EXIT)) { 78 if (exit_code) 79 return 0; 80 } 81 82 if ((__u32)val & what) 83 return 0; 84 85 return 1; 86 } 87 88 static inline void send_msg(struct cn_msg *msg) 89 { 90 __u32 filter_data[2]; 91 92 local_lock(&local_event.lock); 93 94 msg->seq = __this_cpu_inc_return(local_event.count) - 1; 95 ((struct proc_event *)msg->data)->cpu = smp_processor_id(); 96 97 /* 98 * local_lock() disables preemption during send to ensure the messages 99 * are ordered according to their sequence numbers. 100 * 101 * If cn_netlink_send() fails, the data is not sent. 102 */ 103 filter_data[0] = ((struct proc_event *)msg->data)->what; 104 if (filter_data[0] == PROC_EVENT_EXIT) { 105 filter_data[1] = 106 ((struct proc_event *)msg->data)->event_data.exit.exit_code; 107 } else { 108 filter_data[1] = 0; 109 } 110 111 if (cn_netlink_send_mult(msg, msg->len, 0, CN_IDX_PROC, GFP_NOWAIT, 112 cn_filter, (void *)filter_data) == -ESRCH) 113 atomic_set(&proc_event_num_listeners, 0); 114 115 local_unlock(&local_event.lock); 116 } 117 118 void proc_fork_connector(struct task_struct *task) 119 { 120 struct cn_msg *msg; 121 struct proc_event *ev; 122 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 123 struct task_struct *parent; 124 125 if (atomic_read(&proc_event_num_listeners) < 1) 126 return; 127 128 msg = buffer_to_cn_msg(buffer); 129 ev = (struct proc_event *)msg->data; 130 memset(&ev->event_data, 0, sizeof(ev->event_data)); 131 ev->timestamp_ns = ktime_get_ns(); 132 ev->what = PROC_EVENT_FORK; 133 rcu_read_lock(); 134 parent = rcu_dereference(task->real_parent); 135 ev->event_data.fork.parent_pid = parent->pid; 136 ev->event_data.fork.parent_tgid = parent->tgid; 137 rcu_read_unlock(); 138 ev->event_data.fork.child_pid = task->pid; 139 ev->event_data.fork.child_tgid = task->tgid; 140 141 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 142 msg->ack = 0; /* not used */ 143 msg->len = sizeof(*ev); 144 msg->flags = 0; /* not used */ 145 send_msg(msg); 146 } 147 148 void proc_exec_connector(struct task_struct *task) 149 { 150 struct cn_msg *msg; 151 struct proc_event *ev; 152 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 153 154 if (atomic_read(&proc_event_num_listeners) < 1) 155 return; 156 157 msg = buffer_to_cn_msg(buffer); 158 ev = (struct proc_event *)msg->data; 159 memset(&ev->event_data, 0, sizeof(ev->event_data)); 160 ev->timestamp_ns = ktime_get_ns(); 161 ev->what = PROC_EVENT_EXEC; 162 ev->event_data.exec.process_pid = task->pid; 163 ev->event_data.exec.process_tgid = task->tgid; 164 165 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 166 msg->ack = 0; /* not used */ 167 msg->len = sizeof(*ev); 168 msg->flags = 0; /* not used */ 169 send_msg(msg); 170 } 171 172 void proc_id_connector(struct task_struct *task, int which_id) 173 { 174 struct cn_msg *msg; 175 struct proc_event *ev; 176 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 177 const struct cred *cred; 178 179 if (atomic_read(&proc_event_num_listeners) < 1) 180 return; 181 182 msg = buffer_to_cn_msg(buffer); 183 ev = (struct proc_event *)msg->data; 184 memset(&ev->event_data, 0, sizeof(ev->event_data)); 185 ev->what = which_id; 186 ev->event_data.id.process_pid = task->pid; 187 ev->event_data.id.process_tgid = task->tgid; 188 rcu_read_lock(); 189 cred = __task_cred(task); 190 if (which_id == PROC_EVENT_UID) { 191 ev->event_data.id.r.ruid = from_kuid_munged(&init_user_ns, cred->uid); 192 ev->event_data.id.e.euid = from_kuid_munged(&init_user_ns, cred->euid); 193 } else if (which_id == PROC_EVENT_GID) { 194 ev->event_data.id.r.rgid = from_kgid_munged(&init_user_ns, cred->gid); 195 ev->event_data.id.e.egid = from_kgid_munged(&init_user_ns, cred->egid); 196 } else { 197 rcu_read_unlock(); 198 return; 199 } 200 rcu_read_unlock(); 201 ev->timestamp_ns = ktime_get_ns(); 202 203 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 204 msg->ack = 0; /* not used */ 205 msg->len = sizeof(*ev); 206 msg->flags = 0; /* not used */ 207 send_msg(msg); 208 } 209 210 void proc_sid_connector(struct task_struct *task) 211 { 212 struct cn_msg *msg; 213 struct proc_event *ev; 214 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 215 216 if (atomic_read(&proc_event_num_listeners) < 1) 217 return; 218 219 msg = buffer_to_cn_msg(buffer); 220 ev = (struct proc_event *)msg->data; 221 memset(&ev->event_data, 0, sizeof(ev->event_data)); 222 ev->timestamp_ns = ktime_get_ns(); 223 ev->what = PROC_EVENT_SID; 224 ev->event_data.sid.process_pid = task->pid; 225 ev->event_data.sid.process_tgid = task->tgid; 226 227 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 228 msg->ack = 0; /* not used */ 229 msg->len = sizeof(*ev); 230 msg->flags = 0; /* not used */ 231 send_msg(msg); 232 } 233 234 void proc_ptrace_connector(struct task_struct *task, int ptrace_id) 235 { 236 struct cn_msg *msg; 237 struct proc_event *ev; 238 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 239 240 if (atomic_read(&proc_event_num_listeners) < 1) 241 return; 242 243 msg = buffer_to_cn_msg(buffer); 244 ev = (struct proc_event *)msg->data; 245 memset(&ev->event_data, 0, sizeof(ev->event_data)); 246 ev->timestamp_ns = ktime_get_ns(); 247 ev->what = PROC_EVENT_PTRACE; 248 ev->event_data.ptrace.process_pid = task->pid; 249 ev->event_data.ptrace.process_tgid = task->tgid; 250 if (ptrace_id == PTRACE_ATTACH) { 251 ev->event_data.ptrace.tracer_pid = current->pid; 252 ev->event_data.ptrace.tracer_tgid = current->tgid; 253 } else if (ptrace_id == PTRACE_DETACH) { 254 ev->event_data.ptrace.tracer_pid = 0; 255 ev->event_data.ptrace.tracer_tgid = 0; 256 } else 257 return; 258 259 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 260 msg->ack = 0; /* not used */ 261 msg->len = sizeof(*ev); 262 msg->flags = 0; /* not used */ 263 send_msg(msg); 264 } 265 266 void proc_comm_connector(struct task_struct *task) 267 { 268 struct cn_msg *msg; 269 struct proc_event *ev; 270 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 271 272 if (atomic_read(&proc_event_num_listeners) < 1) 273 return; 274 275 msg = buffer_to_cn_msg(buffer); 276 ev = (struct proc_event *)msg->data; 277 memset(&ev->event_data, 0, sizeof(ev->event_data)); 278 ev->timestamp_ns = ktime_get_ns(); 279 ev->what = PROC_EVENT_COMM; 280 ev->event_data.comm.process_pid = task->pid; 281 ev->event_data.comm.process_tgid = task->tgid; 282 get_task_comm(ev->event_data.comm.comm, task); 283 284 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 285 msg->ack = 0; /* not used */ 286 msg->len = sizeof(*ev); 287 msg->flags = 0; /* not used */ 288 send_msg(msg); 289 } 290 291 void proc_coredump_connector(struct task_struct *task) 292 { 293 struct cn_msg *msg; 294 struct proc_event *ev; 295 struct task_struct *parent; 296 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 297 298 if (atomic_read(&proc_event_num_listeners) < 1) 299 return; 300 301 msg = buffer_to_cn_msg(buffer); 302 ev = (struct proc_event *)msg->data; 303 memset(&ev->event_data, 0, sizeof(ev->event_data)); 304 ev->timestamp_ns = ktime_get_ns(); 305 ev->what = PROC_EVENT_COREDUMP; 306 ev->event_data.coredump.process_pid = task->pid; 307 ev->event_data.coredump.process_tgid = task->tgid; 308 309 rcu_read_lock(); 310 if (pid_alive(task)) { 311 parent = rcu_dereference(task->real_parent); 312 ev->event_data.coredump.parent_pid = parent->pid; 313 ev->event_data.coredump.parent_tgid = parent->tgid; 314 } 315 rcu_read_unlock(); 316 317 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 318 msg->ack = 0; /* not used */ 319 msg->len = sizeof(*ev); 320 msg->flags = 0; /* not used */ 321 send_msg(msg); 322 } 323 324 void proc_exit_connector(struct task_struct *task) 325 { 326 struct cn_msg *msg; 327 struct proc_event *ev; 328 struct task_struct *parent; 329 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 330 331 if (atomic_read(&proc_event_num_listeners) < 1) 332 return; 333 334 msg = buffer_to_cn_msg(buffer); 335 ev = (struct proc_event *)msg->data; 336 memset(&ev->event_data, 0, sizeof(ev->event_data)); 337 ev->timestamp_ns = ktime_get_ns(); 338 ev->what = PROC_EVENT_EXIT; 339 ev->event_data.exit.process_pid = task->pid; 340 ev->event_data.exit.process_tgid = task->tgid; 341 ev->event_data.exit.exit_code = task->exit_code; 342 ev->event_data.exit.exit_signal = task->exit_signal; 343 344 rcu_read_lock(); 345 if (pid_alive(task)) { 346 parent = rcu_dereference(task->real_parent); 347 ev->event_data.exit.parent_pid = parent->pid; 348 ev->event_data.exit.parent_tgid = parent->tgid; 349 } 350 rcu_read_unlock(); 351 352 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 353 msg->ack = 0; /* not used */ 354 msg->len = sizeof(*ev); 355 msg->flags = 0; /* not used */ 356 send_msg(msg); 357 } 358 359 /* 360 * Send an acknowledgement message to userspace 361 * 362 * Use 0 for success, EFOO otherwise. 363 * Note: this is the negative of conventional kernel error 364 * values because it's not being returned via syscall return 365 * mechanisms. 366 */ 367 static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack) 368 { 369 struct cn_msg *msg; 370 struct proc_event *ev; 371 __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 372 373 if (atomic_read(&proc_event_num_listeners) < 1) 374 return; 375 376 msg = buffer_to_cn_msg(buffer); 377 ev = (struct proc_event *)msg->data; 378 memset(&ev->event_data, 0, sizeof(ev->event_data)); 379 msg->seq = rcvd_seq; 380 ev->timestamp_ns = ktime_get_ns(); 381 ev->cpu = -1; 382 ev->what = PROC_EVENT_NONE; 383 ev->event_data.ack.err = err; 384 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 385 msg->ack = rcvd_ack + 1; 386 msg->len = sizeof(*ev); 387 msg->flags = 0; /* not used */ 388 send_msg(msg); 389 } 390 391 /** 392 * cn_proc_mcast_ctl 393 * @msg: message sent from userspace via the connector 394 * @nsp: NETLINK_CB of the client's socket buffer 395 */ 396 static void cn_proc_mcast_ctl(struct cn_msg *msg, 397 struct netlink_skb_parms *nsp) 398 { 399 enum proc_cn_mcast_op mc_op = 0, prev_mc_op = 0; 400 struct proc_input *pinput = NULL; 401 enum proc_cn_event ev_type = 0; 402 int err = 0, initial = 0; 403 struct sock *sk = NULL; 404 405 /* 406 * Events are reported with respect to the initial pid 407 * and user namespaces so ignore requestors from 408 * other namespaces. 409 */ 410 if ((current_user_ns() != &init_user_ns) || 411 !task_is_in_init_pid_ns(current)) 412 return; 413 414 if (msg->len == sizeof(*pinput)) { 415 pinput = (struct proc_input *)msg->data; 416 mc_op = pinput->mcast_op; 417 ev_type = pinput->event_type; 418 } else if (msg->len == sizeof(mc_op)) { 419 mc_op = *((enum proc_cn_mcast_op *)msg->data); 420 ev_type = PROC_EVENT_ALL; 421 } else { 422 return; 423 } 424 425 ev_type = valid_event((enum proc_cn_event)ev_type); 426 427 if (ev_type == PROC_EVENT_NONE) 428 ev_type = PROC_EVENT_ALL; 429 430 if (nsp->sk) { 431 sk = nsp->sk; 432 if (sk->sk_user_data == NULL) { 433 sk->sk_user_data = kzalloc(sizeof(struct proc_input), 434 GFP_KERNEL); 435 if (sk->sk_user_data == NULL) { 436 err = ENOMEM; 437 goto out; 438 } 439 initial = 1; 440 } else { 441 prev_mc_op = 442 ((struct proc_input *)(sk->sk_user_data))->mcast_op; 443 } 444 ((struct proc_input *)(sk->sk_user_data))->event_type = 445 ev_type; 446 ((struct proc_input *)(sk->sk_user_data))->mcast_op = mc_op; 447 } 448 449 switch (mc_op) { 450 case PROC_CN_MCAST_LISTEN: 451 if (initial || (prev_mc_op != PROC_CN_MCAST_LISTEN)) 452 atomic_inc(&proc_event_num_listeners); 453 break; 454 case PROC_CN_MCAST_IGNORE: 455 if (!initial && (prev_mc_op != PROC_CN_MCAST_IGNORE)) 456 atomic_dec(&proc_event_num_listeners); 457 ((struct proc_input *)(sk->sk_user_data))->event_type = 458 PROC_EVENT_NONE; 459 break; 460 default: 461 err = EINVAL; 462 break; 463 } 464 465 out: 466 cn_proc_ack(err, msg->seq, msg->ack); 467 } 468 469 /* 470 * cn_proc_init - initialization entry point 471 * 472 * Adds the connector callback to the connector driver. 473 */ 474 static int __init cn_proc_init(void) 475 { 476 int err = cn_add_callback(&cn_proc_event_id, 477 "cn_proc", 478 &cn_proc_mcast_ctl); 479 if (err) { 480 pr_warn("cn_proc failed to register\n"); 481 return err; 482 } 483 return 0; 484 } 485 device_initcall(cn_proc_init); 486