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