1/*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <mach/mach_types.h>
30
31#include <kern/host.h>
32#include <kern/thread.h>
33#include <kern/task.h>
34#include <kern/extmod_statistics.h>
35#include <libkern/OSAtomic.h>
36
37#include <uuid/uuid.h>
38
39/*
40 * This code module adds statistics to track when
41 * a userspace task is modified by another userspace
42 * task. This can facilitate triage of crashes
43 * and abberant behavior, which are not expected
44 * to occur when the program is running in its
45 * qualified environment.
46 *
47 * We assume the target task has a lifecycle lock
48 * that will prevent it from exiting
49 * (task_reference/task_reference_internal), which
50 * should be called either explicitly, or implicitly
51 * via MIG glue code (convert_port_to_task).
52 *
53 * Host-wide statistics don't asssume any locks are
54 * held, and use atomic operations.
55 *
56 * If we can detect that the kernel proper is
57 * performing these operations, don't count
58 * it as an external modification. Some of the
59 * external modification routines are called
60 * by the kernel during thread setup, in which
61 * case we rename the userspace entrypoint called
62 * by the MIG demuxer to have a "_from_user" suffix.
63 */
64
65/* externs for BSD kernel */
66extern void fslog_extmod_msgtracer(void *, void *);
67
68/* local routines */
69static void
70extmod_statistics_log(task_t current_task, task_t target);
71
72void
73extmod_statistics_incr_task_for_pid(task_t target)
74{
75 task_t ctask = current_task();
76
77 if ((ctask == kernel_task) || (target == TASK_NULL))
78 return;
79
80 if (target != ctask) {
81 ctask->extmod_statistics.task_for_pid_caller_count++;
82 target->extmod_statistics.task_for_pid_count++;
83 OSIncrementAtomic64(&host_extmod_statistics.task_for_pid_count);
84 }
85}
86
87void
88extmod_statistics_incr_thread_set_state(thread_t target)
89{
90 task_t ctask = current_task();
91 task_t ttask;
92
93 if ((ctask == kernel_task) || (target == THREAD_NULL))
94 return;
95
96 ttask = get_threadtask(target);
97
98 if (ttask == TASK_NULL)
99 return;
100
101 if (ttask != ctask) {
102 ctask->extmod_statistics.thread_set_state_caller_count++;
103 ttask->extmod_statistics.thread_set_state_count++;
104 OSIncrementAtomic64(&host_extmod_statistics.thread_set_state_count);
105 }
106}
107
108void
109extmod_statistics_incr_thread_create(task_t target)
110{
111 task_t ctask = current_task();
112
113 if ((ctask == kernel_task) || (target == TASK_NULL))
114 return;
115
116 if (target != ctask) {
117 ctask->extmod_statistics.thread_creation_caller_count++;
118 target->extmod_statistics.thread_creation_count++;
119 OSIncrementAtomic64(&host_extmod_statistics.thread_creation_count);
120
121 extmod_statistics_log(ctask, target);
122 }
123}
124
125static void
126extmod_statistics_log(task_t current_task, task_t target)
127{
128 void *c_proc;
129 void *t_proc;
130
131 c_proc = get_bsdtask_info(current_task);
132 t_proc = get_bsdtask_info(target);
133 if (c_proc && t_proc) {
134 fslog_extmod_msgtracer(c_proc, t_proc);
135 }
136}
137