18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * cn_proc.c - process events connector 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) Matt Helsley, IBM Corp. 2005 68c2ecf20Sopenharmony_ci * Based on cn_fork.c by Guillaume Thouvenin <guillaume.thouvenin@bull.net> 78c2ecf20Sopenharmony_ci * Original copyright notice follows: 88c2ecf20Sopenharmony_ci * Copyright (C) 2005 BULL SA. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/ktime.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/connector.h> 158c2ecf20Sopenharmony_ci#include <linux/gfp.h> 168c2ecf20Sopenharmony_ci#include <linux/ptrace.h> 178c2ecf20Sopenharmony_ci#include <linux/atomic.h> 188c2ecf20Sopenharmony_ci#include <linux/pid_namespace.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <linux/cn_proc.h> 218c2ecf20Sopenharmony_ci#include <linux/local_lock.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* 248c2ecf20Sopenharmony_ci * Size of a cn_msg followed by a proc_event structure. Since the 258c2ecf20Sopenharmony_ci * sizeof struct cn_msg is a multiple of 4 bytes, but not 8 bytes, we 268c2ecf20Sopenharmony_ci * add one 4-byte word to the size here, and then start the actual 278c2ecf20Sopenharmony_ci * cn_msg structure 4 bytes into the stack buffer. The result is that 288c2ecf20Sopenharmony_ci * the immediately following proc_event structure is aligned to 8 bytes. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci#define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4) 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* See comment above; we test our assumption about sizeof struct cn_msg here. */ 338c2ecf20Sopenharmony_cistatic inline struct cn_msg *buffer_to_cn_msg(__u8 *buffer) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(struct cn_msg) != 20); 368c2ecf20Sopenharmony_ci return (struct cn_msg *)(buffer + 4); 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic atomic_t proc_event_num_listeners = ATOMIC_INIT(0); 408c2ecf20Sopenharmony_cistatic struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC }; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/* local_event.count is used as the sequence number of the netlink message */ 438c2ecf20Sopenharmony_cistruct local_event { 448c2ecf20Sopenharmony_ci local_lock_t lock; 458c2ecf20Sopenharmony_ci __u32 count; 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct local_event, local_event) = { 488c2ecf20Sopenharmony_ci .lock = INIT_LOCAL_LOCK(lock), 498c2ecf20Sopenharmony_ci}; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic inline void send_msg(struct cn_msg *msg) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci local_lock(&local_event.lock); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci msg->seq = __this_cpu_inc_return(local_event.count) - 1; 568c2ecf20Sopenharmony_ci ((struct proc_event *)msg->data)->cpu = smp_processor_id(); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci /* 598c2ecf20Sopenharmony_ci * local_lock() disables preemption during send to ensure the messages 608c2ecf20Sopenharmony_ci * are ordered according to their sequence numbers. 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * If cn_netlink_send() fails, the data is not sent. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci cn_netlink_send(msg, 0, CN_IDX_PROC, GFP_NOWAIT); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci local_unlock(&local_event.lock); 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_civoid proc_fork_connector(struct task_struct *task) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci struct cn_msg *msg; 728c2ecf20Sopenharmony_ci struct proc_event *ev; 738c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 748c2ecf20Sopenharmony_ci struct task_struct *parent; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 778c2ecf20Sopenharmony_ci return; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 808c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 818c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 828c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 838c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_FORK; 848c2ecf20Sopenharmony_ci rcu_read_lock(); 858c2ecf20Sopenharmony_ci parent = rcu_dereference(task->real_parent); 868c2ecf20Sopenharmony_ci ev->event_data.fork.parent_pid = parent->pid; 878c2ecf20Sopenharmony_ci ev->event_data.fork.parent_tgid = parent->tgid; 888c2ecf20Sopenharmony_ci rcu_read_unlock(); 898c2ecf20Sopenharmony_ci ev->event_data.fork.child_pid = task->pid; 908c2ecf20Sopenharmony_ci ev->event_data.fork.child_tgid = task->tgid; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 938c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 948c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 958c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 968c2ecf20Sopenharmony_ci send_msg(msg); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_civoid proc_exec_connector(struct task_struct *task) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct cn_msg *msg; 1028c2ecf20Sopenharmony_ci struct proc_event *ev; 1038c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 1068c2ecf20Sopenharmony_ci return; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 1098c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 1108c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 1118c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 1128c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_EXEC; 1138c2ecf20Sopenharmony_ci ev->event_data.exec.process_pid = task->pid; 1148c2ecf20Sopenharmony_ci ev->event_data.exec.process_tgid = task->tgid; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 1178c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 1188c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 1198c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 1208c2ecf20Sopenharmony_ci send_msg(msg); 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_civoid proc_id_connector(struct task_struct *task, int which_id) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct cn_msg *msg; 1268c2ecf20Sopenharmony_ci struct proc_event *ev; 1278c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 1288c2ecf20Sopenharmony_ci const struct cred *cred; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 1318c2ecf20Sopenharmony_ci return; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 1348c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 1358c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 1368c2ecf20Sopenharmony_ci ev->what = which_id; 1378c2ecf20Sopenharmony_ci ev->event_data.id.process_pid = task->pid; 1388c2ecf20Sopenharmony_ci ev->event_data.id.process_tgid = task->tgid; 1398c2ecf20Sopenharmony_ci rcu_read_lock(); 1408c2ecf20Sopenharmony_ci cred = __task_cred(task); 1418c2ecf20Sopenharmony_ci if (which_id == PROC_EVENT_UID) { 1428c2ecf20Sopenharmony_ci ev->event_data.id.r.ruid = from_kuid_munged(&init_user_ns, cred->uid); 1438c2ecf20Sopenharmony_ci ev->event_data.id.e.euid = from_kuid_munged(&init_user_ns, cred->euid); 1448c2ecf20Sopenharmony_ci } else if (which_id == PROC_EVENT_GID) { 1458c2ecf20Sopenharmony_ci ev->event_data.id.r.rgid = from_kgid_munged(&init_user_ns, cred->gid); 1468c2ecf20Sopenharmony_ci ev->event_data.id.e.egid = from_kgid_munged(&init_user_ns, cred->egid); 1478c2ecf20Sopenharmony_ci } else { 1488c2ecf20Sopenharmony_ci rcu_read_unlock(); 1498c2ecf20Sopenharmony_ci return; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci rcu_read_unlock(); 1528c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 1558c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 1568c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 1578c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 1588c2ecf20Sopenharmony_ci send_msg(msg); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_civoid proc_sid_connector(struct task_struct *task) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct cn_msg *msg; 1648c2ecf20Sopenharmony_ci struct proc_event *ev; 1658c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 1688c2ecf20Sopenharmony_ci return; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 1718c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 1728c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 1738c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 1748c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_SID; 1758c2ecf20Sopenharmony_ci ev->event_data.sid.process_pid = task->pid; 1768c2ecf20Sopenharmony_ci ev->event_data.sid.process_tgid = task->tgid; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 1798c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 1808c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 1818c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 1828c2ecf20Sopenharmony_ci send_msg(msg); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_civoid proc_ptrace_connector(struct task_struct *task, int ptrace_id) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci struct cn_msg *msg; 1888c2ecf20Sopenharmony_ci struct proc_event *ev; 1898c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 1928c2ecf20Sopenharmony_ci return; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 1958c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 1968c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 1978c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 1988c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_PTRACE; 1998c2ecf20Sopenharmony_ci ev->event_data.ptrace.process_pid = task->pid; 2008c2ecf20Sopenharmony_ci ev->event_data.ptrace.process_tgid = task->tgid; 2018c2ecf20Sopenharmony_ci if (ptrace_id == PTRACE_ATTACH) { 2028c2ecf20Sopenharmony_ci ev->event_data.ptrace.tracer_pid = current->pid; 2038c2ecf20Sopenharmony_ci ev->event_data.ptrace.tracer_tgid = current->tgid; 2048c2ecf20Sopenharmony_ci } else if (ptrace_id == PTRACE_DETACH) { 2058c2ecf20Sopenharmony_ci ev->event_data.ptrace.tracer_pid = 0; 2068c2ecf20Sopenharmony_ci ev->event_data.ptrace.tracer_tgid = 0; 2078c2ecf20Sopenharmony_ci } else 2088c2ecf20Sopenharmony_ci return; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 2118c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 2128c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 2138c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 2148c2ecf20Sopenharmony_ci send_msg(msg); 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_civoid proc_comm_connector(struct task_struct *task) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci struct cn_msg *msg; 2208c2ecf20Sopenharmony_ci struct proc_event *ev; 2218c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 2248c2ecf20Sopenharmony_ci return; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 2278c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 2288c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 2298c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 2308c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_COMM; 2318c2ecf20Sopenharmony_ci ev->event_data.comm.process_pid = task->pid; 2328c2ecf20Sopenharmony_ci ev->event_data.comm.process_tgid = task->tgid; 2338c2ecf20Sopenharmony_ci get_task_comm(ev->event_data.comm.comm, task); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 2368c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 2378c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 2388c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 2398c2ecf20Sopenharmony_ci send_msg(msg); 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_civoid proc_coredump_connector(struct task_struct *task) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci struct cn_msg *msg; 2458c2ecf20Sopenharmony_ci struct proc_event *ev; 2468c2ecf20Sopenharmony_ci struct task_struct *parent; 2478c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 2508c2ecf20Sopenharmony_ci return; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 2538c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 2548c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 2558c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 2568c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_COREDUMP; 2578c2ecf20Sopenharmony_ci ev->event_data.coredump.process_pid = task->pid; 2588c2ecf20Sopenharmony_ci ev->event_data.coredump.process_tgid = task->tgid; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci rcu_read_lock(); 2618c2ecf20Sopenharmony_ci if (pid_alive(task)) { 2628c2ecf20Sopenharmony_ci parent = rcu_dereference(task->real_parent); 2638c2ecf20Sopenharmony_ci ev->event_data.coredump.parent_pid = parent->pid; 2648c2ecf20Sopenharmony_ci ev->event_data.coredump.parent_tgid = parent->tgid; 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci rcu_read_unlock(); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 2698c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 2708c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 2718c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 2728c2ecf20Sopenharmony_ci send_msg(msg); 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_civoid proc_exit_connector(struct task_struct *task) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci struct cn_msg *msg; 2788c2ecf20Sopenharmony_ci struct proc_event *ev; 2798c2ecf20Sopenharmony_ci struct task_struct *parent; 2808c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 2838c2ecf20Sopenharmony_ci return; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 2868c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 2878c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 2888c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 2898c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_EXIT; 2908c2ecf20Sopenharmony_ci ev->event_data.exit.process_pid = task->pid; 2918c2ecf20Sopenharmony_ci ev->event_data.exit.process_tgid = task->tgid; 2928c2ecf20Sopenharmony_ci ev->event_data.exit.exit_code = task->exit_code; 2938c2ecf20Sopenharmony_ci ev->event_data.exit.exit_signal = task->exit_signal; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci rcu_read_lock(); 2968c2ecf20Sopenharmony_ci if (pid_alive(task)) { 2978c2ecf20Sopenharmony_ci parent = rcu_dereference(task->real_parent); 2988c2ecf20Sopenharmony_ci ev->event_data.exit.parent_pid = parent->pid; 2998c2ecf20Sopenharmony_ci ev->event_data.exit.parent_tgid = parent->tgid; 3008c2ecf20Sopenharmony_ci } 3018c2ecf20Sopenharmony_ci rcu_read_unlock(); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 3048c2ecf20Sopenharmony_ci msg->ack = 0; /* not used */ 3058c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 3068c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 3078c2ecf20Sopenharmony_ci send_msg(msg); 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci/* 3118c2ecf20Sopenharmony_ci * Send an acknowledgement message to userspace 3128c2ecf20Sopenharmony_ci * 3138c2ecf20Sopenharmony_ci * Use 0 for success, EFOO otherwise. 3148c2ecf20Sopenharmony_ci * Note: this is the negative of conventional kernel error 3158c2ecf20Sopenharmony_ci * values because it's not being returned via syscall return 3168c2ecf20Sopenharmony_ci * mechanisms. 3178c2ecf20Sopenharmony_ci */ 3188c2ecf20Sopenharmony_cistatic void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci struct cn_msg *msg; 3218c2ecf20Sopenharmony_ci struct proc_event *ev; 3228c2ecf20Sopenharmony_ci __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci if (atomic_read(&proc_event_num_listeners) < 1) 3258c2ecf20Sopenharmony_ci return; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci msg = buffer_to_cn_msg(buffer); 3288c2ecf20Sopenharmony_ci ev = (struct proc_event *)msg->data; 3298c2ecf20Sopenharmony_ci memset(&ev->event_data, 0, sizeof(ev->event_data)); 3308c2ecf20Sopenharmony_ci msg->seq = rcvd_seq; 3318c2ecf20Sopenharmony_ci ev->timestamp_ns = ktime_get_ns(); 3328c2ecf20Sopenharmony_ci ev->cpu = -1; 3338c2ecf20Sopenharmony_ci ev->what = PROC_EVENT_NONE; 3348c2ecf20Sopenharmony_ci ev->event_data.ack.err = err; 3358c2ecf20Sopenharmony_ci memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 3368c2ecf20Sopenharmony_ci msg->ack = rcvd_ack + 1; 3378c2ecf20Sopenharmony_ci msg->len = sizeof(*ev); 3388c2ecf20Sopenharmony_ci msg->flags = 0; /* not used */ 3398c2ecf20Sopenharmony_ci send_msg(msg); 3408c2ecf20Sopenharmony_ci} 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci/** 3438c2ecf20Sopenharmony_ci * cn_proc_mcast_ctl 3448c2ecf20Sopenharmony_ci * @data: message sent from userspace via the connector 3458c2ecf20Sopenharmony_ci */ 3468c2ecf20Sopenharmony_cistatic void cn_proc_mcast_ctl(struct cn_msg *msg, 3478c2ecf20Sopenharmony_ci struct netlink_skb_parms *nsp) 3488c2ecf20Sopenharmony_ci{ 3498c2ecf20Sopenharmony_ci enum proc_cn_mcast_op *mc_op = NULL; 3508c2ecf20Sopenharmony_ci int err = 0; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci if (msg->len != sizeof(*mc_op)) 3538c2ecf20Sopenharmony_ci return; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci /* 3568c2ecf20Sopenharmony_ci * Events are reported with respect to the initial pid 3578c2ecf20Sopenharmony_ci * and user namespaces so ignore requestors from 3588c2ecf20Sopenharmony_ci * other namespaces. 3598c2ecf20Sopenharmony_ci */ 3608c2ecf20Sopenharmony_ci if ((current_user_ns() != &init_user_ns) || 3618c2ecf20Sopenharmony_ci (task_active_pid_ns(current) != &init_pid_ns)) 3628c2ecf20Sopenharmony_ci return; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci /* Can only change if privileged. */ 3658c2ecf20Sopenharmony_ci if (!__netlink_ns_capable(nsp, &init_user_ns, CAP_NET_ADMIN)) { 3668c2ecf20Sopenharmony_ci err = EPERM; 3678c2ecf20Sopenharmony_ci goto out; 3688c2ecf20Sopenharmony_ci } 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci mc_op = (enum proc_cn_mcast_op *)msg->data; 3718c2ecf20Sopenharmony_ci switch (*mc_op) { 3728c2ecf20Sopenharmony_ci case PROC_CN_MCAST_LISTEN: 3738c2ecf20Sopenharmony_ci atomic_inc(&proc_event_num_listeners); 3748c2ecf20Sopenharmony_ci break; 3758c2ecf20Sopenharmony_ci case PROC_CN_MCAST_IGNORE: 3768c2ecf20Sopenharmony_ci atomic_dec(&proc_event_num_listeners); 3778c2ecf20Sopenharmony_ci break; 3788c2ecf20Sopenharmony_ci default: 3798c2ecf20Sopenharmony_ci err = EINVAL; 3808c2ecf20Sopenharmony_ci break; 3818c2ecf20Sopenharmony_ci } 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ciout: 3848c2ecf20Sopenharmony_ci cn_proc_ack(err, msg->seq, msg->ack); 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci/* 3888c2ecf20Sopenharmony_ci * cn_proc_init - initialization entry point 3898c2ecf20Sopenharmony_ci * 3908c2ecf20Sopenharmony_ci * Adds the connector callback to the connector driver. 3918c2ecf20Sopenharmony_ci */ 3928c2ecf20Sopenharmony_cistatic int __init cn_proc_init(void) 3938c2ecf20Sopenharmony_ci{ 3948c2ecf20Sopenharmony_ci int err = cn_add_callback(&cn_proc_event_id, 3958c2ecf20Sopenharmony_ci "cn_proc", 3968c2ecf20Sopenharmony_ci &cn_proc_mcast_ctl); 3978c2ecf20Sopenharmony_ci if (err) { 3988c2ecf20Sopenharmony_ci pr_warn("cn_proc failed to register\n"); 3998c2ecf20Sopenharmony_ci return err; 4008c2ecf20Sopenharmony_ci } 4018c2ecf20Sopenharmony_ci return 0; 4028c2ecf20Sopenharmony_ci} 4038c2ecf20Sopenharmony_cidevice_initcall(cn_proc_init); 404