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 */ |
66 | extern void fslog_extmod_msgtracer(void *, void *); |
67 | |
68 | /* local routines */ |
69 | static void |
70 | extmod_statistics_log(task_t current_task, task_t target); |
71 | |
72 | void |
73 | extmod_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 | |
81 | if (target != ctask) { |
82 | ctask->extmod_statistics.task_for_pid_caller_count++; |
83 | target->extmod_statistics.task_for_pid_count++; |
84 | OSIncrementAtomic64(address: &host_extmod_statistics.task_for_pid_count); |
85 | } |
86 | } |
87 | |
88 | void |
89 | extmod_statistics_incr_thread_set_state(thread_t target) |
90 | { |
91 | task_t ctask = current_task(); |
92 | task_t ttask; |
93 | |
94 | if ((ctask == kernel_task) || (target == THREAD_NULL)) { |
95 | return; |
96 | } |
97 | |
98 | ttask = get_threadtask(target); |
99 | |
100 | if (ttask == TASK_NULL) { |
101 | return; |
102 | } |
103 | |
104 | if (ttask != ctask) { |
105 | ctask->extmod_statistics.thread_set_state_caller_count++; |
106 | ttask->extmod_statistics.thread_set_state_count++; |
107 | OSIncrementAtomic64(address: &host_extmod_statistics.thread_set_state_count); |
108 | } |
109 | } |
110 | |
111 | void |
112 | extmod_statistics_incr_thread_create(task_t target) |
113 | { |
114 | task_t ctask = current_task(); |
115 | |
116 | if ((ctask == kernel_task) || (target == TASK_NULL)) { |
117 | return; |
118 | } |
119 | |
120 | if (target != ctask) { |
121 | ctask->extmod_statistics.thread_creation_caller_count++; |
122 | target->extmod_statistics.thread_creation_count++; |
123 | OSIncrementAtomic64(address: &host_extmod_statistics.thread_creation_count); |
124 | |
125 | extmod_statistics_log(current_task: ctask, target); |
126 | } |
127 | } |
128 | |
129 | static void |
130 | extmod_statistics_log(task_t current_task, task_t target) |
131 | { |
132 | void *c_proc; |
133 | void *t_proc; |
134 | |
135 | c_proc = get_bsdtask_info(current_task); |
136 | t_proc = get_bsdtask_info(target); |
137 | if (c_proc && t_proc) { |
138 | fslog_extmod_msgtracer(c_proc, t_proc); |
139 | } |
140 | } |
141 | |