linux/drivers/connector/cn_proc.c
<<
>>
Prefs
   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
  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. */
  32static 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}
  37
  38static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
  39static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
  40
  41/* proc_event_counts is used as the sequence number of the netlink message */
  42static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
  43
  44static inline void send_msg(struct cn_msg *msg)
  45{
  46        preempt_disable();
  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
  59        preempt_enable();
  60}
  61
  62void proc_fork_connector(struct task_struct *task)
  63{
  64        struct cn_msg *msg;
  65        struct proc_event *ev;
  66        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  67        struct task_struct *parent;
  68
  69        if (atomic_read(&proc_event_num_listeners) < 1)
  70                return;
  71
  72        msg = buffer_to_cn_msg(buffer);
  73        ev = (struct proc_event *)msg->data;
  74        memset(&ev->event_data, 0, sizeof(ev->event_data));
  75        ev->timestamp_ns = ktime_get_ns();
  76        ev->what = PROC_EVENT_FORK;
  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();
  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);
  88        msg->flags = 0; /* not used */
  89        send_msg(msg);
  90}
  91
  92void proc_exec_connector(struct task_struct *task)
  93{
  94        struct cn_msg *msg;
  95        struct proc_event *ev;
  96        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
  97
  98        if (atomic_read(&proc_event_num_listeners) < 1)
  99                return;
 100
 101        msg = buffer_to_cn_msg(buffer);
 102        ev = (struct proc_event *)msg->data;
 103        memset(&ev->event_data, 0, sizeof(ev->event_data));
 104        ev->timestamp_ns = ktime_get_ns();
 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);
 112        msg->flags = 0; /* not used */
 113        send_msg(msg);
 114}
 115
 116void proc_id_connector(struct task_struct *task, int which_id)
 117{
 118        struct cn_msg *msg;
 119        struct proc_event *ev;
 120        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
 121        const struct cred *cred;
 122
 123        if (atomic_read(&proc_event_num_listeners) < 1)
 124                return;
 125
 126        msg = buffer_to_cn_msg(buffer);
 127        ev = (struct proc_event *)msg->data;
 128        memset(&ev->event_data, 0, sizeof(ev->event_data));
 129        ev->what = which_id;
 130        ev->event_data.id.process_pid = task->pid;
 131        ev->event_data.id.process_tgid = task->tgid;
 132        rcu_read_lock();
 133        cred = __task_cred(task);
 134        if (which_id == PROC_EVENT_UID) {
 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);
 137        } else if (which_id == PROC_EVENT_GID) {
 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);
 140        } else {
 141                rcu_read_unlock();
 142                return;
 143        }
 144        rcu_read_unlock();
 145        ev->timestamp_ns = ktime_get_ns();
 146
 147        memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
 148        msg->ack = 0; /* not used */
 149        msg->len = sizeof(*ev);
 150        msg->flags = 0; /* not used */
 151        send_msg(msg);
 152}
 153
 154void proc_sid_connector(struct task_struct *task)
 155{
 156        struct cn_msg *msg;
 157        struct proc_event *ev;
 158        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
 159
 160        if (atomic_read(&proc_event_num_listeners) < 1)
 161                return;
 162
 163        msg = buffer_to_cn_msg(buffer);
 164        ev = (struct proc_event *)msg->data;
 165        memset(&ev->event_data, 0, sizeof(ev->event_data));
 166        ev->timestamp_ns = ktime_get_ns();
 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);
 174        msg->flags = 0; /* not used */
 175        send_msg(msg);
 176}
 177
 178void proc_ptrace_connector(struct task_struct *task, int ptrace_id)
 179{
 180        struct cn_msg *msg;
 181        struct proc_event *ev;
 182        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
 183
 184        if (atomic_read(&proc_event_num_listeners) < 1)
 185                return;
 186
 187        msg = buffer_to_cn_msg(buffer);
 188        ev = (struct proc_event *)msg->data;
 189        memset(&ev->event_data, 0, sizeof(ev->event_data));
 190        ev->timestamp_ns = ktime_get_ns();
 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);
 206        msg->flags = 0; /* not used */
 207        send_msg(msg);
 208}
 209
 210void proc_comm_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_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);
 231        msg->flags = 0; /* not used */
 232        send_msg(msg);
 233}
 234
 235void proc_coredump_connector(struct task_struct *task)
 236{
 237        struct cn_msg *msg;
 238        struct proc_event *ev;
 239        struct task_struct *parent;
 240        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
 241
 242        if (atomic_read(&proc_event_num_listeners) < 1)
 243                return;
 244
 245        msg = buffer_to_cn_msg(buffer);
 246        ev = (struct proc_event *)msg->data;
 247        memset(&ev->event_data, 0, sizeof(ev->event_data));
 248        ev->timestamp_ns = ktime_get_ns();
 249        ev->what = PROC_EVENT_COREDUMP;
 250        ev->event_data.coredump.process_pid = task->pid;
 251        ev->event_data.coredump.process_tgid = task->tgid;
 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();
 260
 261        memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
 262        msg->ack = 0; /* not used */
 263        msg->len = sizeof(*ev);
 264        msg->flags = 0; /* not used */
 265        send_msg(msg);
 266}
 267
 268void proc_exit_connector(struct task_struct *task)
 269{
 270        struct cn_msg *msg;
 271        struct proc_event *ev;
 272        struct task_struct *parent;
 273        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
 274
 275        if (atomic_read(&proc_event_num_listeners) < 1)
 276                return;
 277
 278        msg = buffer_to_cn_msg(buffer);
 279        ev = (struct proc_event *)msg->data;
 280        memset(&ev->event_data, 0, sizeof(ev->event_data));
 281        ev->timestamp_ns = ktime_get_ns();
 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;
 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();
 295
 296        memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
 297        msg->ack = 0; /* not used */
 298        msg->len = sizeof(*ev);
 299        msg->flags = 0; /* not used */
 300        send_msg(msg);
 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 */
 311static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
 312{
 313        struct cn_msg *msg;
 314        struct proc_event *ev;
 315        __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8);
 316
 317        if (atomic_read(&proc_event_num_listeners) < 1)
 318                return;
 319
 320        msg = buffer_to_cn_msg(buffer);
 321        ev = (struct proc_event *)msg->data;
 322        memset(&ev->event_data, 0, sizeof(ev->event_data));
 323        msg->seq = rcvd_seq;
 324        ev->timestamp_ns = ktime_get_ns();
 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);
 331        msg->flags = 0; /* not used */
 332        send_msg(msg);
 333}
 334
 335/**
 336 * cn_proc_mcast_ctl
 337 * @data: message sent from userspace via the connector
 338 */
 339static void cn_proc_mcast_ctl(struct cn_msg *msg,
 340                              struct netlink_skb_parms *nsp)
 341{
 342        enum proc_cn_mcast_op *mc_op = NULL;
 343        int err = 0;
 344
 345        if (msg->len != sizeof(*mc_op))
 346                return;
 347
 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
 357        /* Can only change if privileged. */
 358        if (!__netlink_ns_capable(nsp, &init_user_ns, CAP_NET_ADMIN)) {
 359                err = EPERM;
 360                goto out;
 361        }
 362
 363        mc_op = (enum proc_cn_mcast_op *)msg->data;
 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        }
 375
 376out:
 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 */
 385static int __init cn_proc_init(void)
 386{
 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");
 392                return err;
 393        }
 394        return 0;
 395}
 396device_initcall(cn_proc_init);
 397