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