1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * cn_proc.c - process events connector
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.
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>
20 #include <linux/cn_proc.h>
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.
29 #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event) + 4)
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
)
34 BUILD_BUG_ON(sizeof(struct cn_msg
) != 20);
35 return (struct cn_msg
*)(buffer
+ 4);
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
};
41 /* proc_event_counts is used as the sequence number of the netlink message */
42 static DEFINE_PER_CPU(__u32
, proc_event_counts
) = { 0 };
44 static inline void send_msg(struct cn_msg
*msg
)
48 msg
->seq
= __this_cpu_inc_return(proc_event_counts
) - 1;
49 ((struct proc_event
*)msg
->data
)->cpu
= smp_processor_id();
52 * Preemption remains disabled during send to ensure the messages are
53 * ordered according to their sequence numbers.
55 * If cn_netlink_send() fails, the data is not sent.
57 cn_netlink_send(msg
, 0, CN_IDX_PROC
, GFP_NOWAIT
);
62 void proc_fork_connector(struct task_struct
*task
)
65 struct proc_event
*ev
;
66 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
67 struct task_struct
*parent
;
69 if (atomic_read(&proc_event_num_listeners
) < 1)
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
;
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
;
82 ev
->event_data
.fork
.child_pid
= task
->pid
;
83 ev
->event_data
.fork
.child_tgid
= task
->tgid
;
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 */
92 void proc_exec_connector(struct task_struct
*task
)
95 struct proc_event
*ev
;
96 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
98 if (atomic_read(&proc_event_num_listeners
) < 1)
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
;
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 */
116 void proc_id_connector(struct task_struct
*task
, int which_id
)
119 struct proc_event
*ev
;
120 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
121 const struct cred
*cred
;
123 if (atomic_read(&proc_event_num_listeners
) < 1)
126 msg
= buffer_to_cn_msg(buffer
);
127 ev
= (struct proc_event
*)msg
->data
;
128 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
130 ev
->event_data
.id
.process_pid
= task
->pid
;
131 ev
->event_data
.id
.process_tgid
= task
->tgid
;
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
);
145 ev
->timestamp_ns
= ktime_get_ns();
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 */
154 void proc_sid_connector(struct task_struct
*task
)
157 struct proc_event
*ev
;
158 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
160 if (atomic_read(&proc_event_num_listeners
) < 1)
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
;
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 */
178 void proc_ptrace_connector(struct task_struct
*task
, int ptrace_id
)
181 struct proc_event
*ev
;
182 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
184 if (atomic_read(&proc_event_num_listeners
) < 1)
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;
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 */
210 void proc_comm_connector(struct task_struct
*task
)
213 struct proc_event
*ev
;
214 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
216 if (atomic_read(&proc_event_num_listeners
) < 1)
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
);
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 */
235 void proc_coredump_connector(struct task_struct
*task
)
238 struct proc_event
*ev
;
239 struct task_struct
*parent
;
240 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
242 if (atomic_read(&proc_event_num_listeners
) < 1)
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
;
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
;
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 */
268 void proc_exit_connector(struct task_struct
*task
)
271 struct proc_event
*ev
;
272 struct task_struct
*parent
;
273 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
275 if (atomic_read(&proc_event_num_listeners
) < 1)
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
;
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
;
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 */
304 * Send an acknowledgement message to userspace
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
311 static void cn_proc_ack(int err
, int rcvd_seq
, int rcvd_ack
)
314 struct proc_event
*ev
;
315 __u8 buffer
[CN_PROC_MSG_SIZE
] __aligned(8);
317 if (atomic_read(&proc_event_num_listeners
) < 1)
320 msg
= buffer_to_cn_msg(buffer
);
321 ev
= (struct proc_event
*)msg
->data
;
322 memset(&ev
->event_data
, 0, sizeof(ev
->event_data
));
324 ev
->timestamp_ns
= ktime_get_ns();
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 */
337 * @data: message sent from userspace via the connector
339 static void cn_proc_mcast_ctl(struct cn_msg
*msg
,
340 struct netlink_skb_parms
*nsp
)
342 enum proc_cn_mcast_op
*mc_op
= NULL
;
345 if (msg
->len
!= sizeof(*mc_op
))
349 * Events are reported with respect to the initial pid
350 * and user namespaces so ignore requestors from
353 if ((current_user_ns() != &init_user_ns
) ||
354 (task_active_pid_ns(current
) != &init_pid_ns
))
357 /* Can only change if privileged. */
358 if (!__netlink_ns_capable(nsp
, &init_user_ns
, CAP_NET_ADMIN
)) {
363 mc_op
= (enum proc_cn_mcast_op
*)msg
->data
;
365 case PROC_CN_MCAST_LISTEN
:
366 atomic_inc(&proc_event_num_listeners
);
368 case PROC_CN_MCAST_IGNORE
:
369 atomic_dec(&proc_event_num_listeners
);
377 cn_proc_ack(err
, msg
->seq
, msg
->ack
);
381 * cn_proc_init - initialization entry point
383 * Adds the connector callback to the connector driver.
385 static int __init
cn_proc_init(void)
387 int err
= cn_add_callback(&cn_proc_event_id
,
391 pr_warn("cn_proc failed to register\n");
396 device_initcall(cn_proc_init
);