1 | /* |
2 | * Copyright (c) 2019 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 <sys/sysctl.h> |
30 | #include <sys/malloc.h> |
31 | #include <pexpert/pexpert.h> |
32 | #include <kern/kalloc.h> |
33 | #include <kern/percpu.h> |
34 | #include <prng/entropy.h> |
35 | #include <libkern/section_keywords.h> |
36 | |
37 | SYSCTL_NODE(_kern, OID_AUTO, entropy, CTLFLAG_RD, 0, NULL); |
38 | SYSCTL_NODE(_kern_entropy, OID_AUTO, health, CTLFLAG_RD, 0, NULL); |
39 | SYSCTL_INT(_kern_entropy_health, OID_AUTO, startup_done, CTLFLAG_RD, &entropy_health_startup_done, 0, NULL); |
40 | |
41 | SYSCTL_NODE(_kern_entropy_health, OID_AUTO, repetition_count_test, CTLFLAG_RD, 0, NULL); |
42 | SYSCTL_UINT(_kern_entropy_health_repetition_count_test, OID_AUTO, reset_count, CTLFLAG_RD, &entropy_health_rct_stats.reset_count, 0, NULL); |
43 | SYSCTL_UINT(_kern_entropy_health_repetition_count_test, OID_AUTO, failure_count, CTLFLAG_RD, &entropy_health_rct_stats.failure_count, 0, NULL); |
44 | SYSCTL_UINT(_kern_entropy_health_repetition_count_test, OID_AUTO, max_observation_count, CTLFLAG_RD, &entropy_health_rct_stats.max_observation_count, 0, NULL); |
45 | |
46 | SYSCTL_NODE(_kern_entropy_health, OID_AUTO, adaptive_proportion_test, CTLFLAG_RD, 0, NULL); |
47 | SYSCTL_UINT(_kern_entropy_health_adaptive_proportion_test, OID_AUTO, reset_count, CTLFLAG_RD, &entropy_health_apt_stats.reset_count, 0, NULL); |
48 | SYSCTL_UINT(_kern_entropy_health_adaptive_proportion_test, OID_AUTO, failure_count, CTLFLAG_RD, &entropy_health_apt_stats.failure_count, 0, NULL); |
49 | SYSCTL_UINT(_kern_entropy_health_adaptive_proportion_test, OID_AUTO, max_observation_count, CTLFLAG_RD, &entropy_health_apt_stats.max_observation_count, 0, NULL); |
50 | |
51 | SYSCTL_NODE(_kern_entropy, OID_AUTO, filter, CTLFLAG_RD, 0, |
52 | "Entropy filter information" ); |
53 | SYSCTL_QUAD(_kern_entropy_filter, OID_AUTO, total_sample_count, CTLFLAG_RD, &entropy_filter_total_sample_count, |
54 | "The total number of samples processed (i.e. the number of interrupts)" ); |
55 | SYSCTL_QUAD(_kern_entropy_filter, OID_AUTO, accepted_sample_count, CTLFLAG_RD, &entropy_filter_accepted_sample_count, |
56 | "The number of samples that passed the filter" ); |
57 | SYSCTL_QUAD(_kern_entropy_filter, OID_AUTO, rejected_sample_count, CTLFLAG_RD, &entropy_filter_rejected_sample_count, |
58 | "The number of samples that were rejected by the filters" ); |
59 | |
60 | SYSCTL_NODE(_kern_entropy, OID_AUTO, analysis, CTLFLAG_RD, 0, |
61 | "Subsystem to collect entropy samples for offline analysis" ); |
62 | |
63 | static int entropy_analysis_supported = ENTROPY_ANALYSIS_SUPPORTED; |
64 | SYSCTL_INT(_kern_entropy_analysis, OID_AUTO, supported, CTLFLAG_RD, &entropy_analysis_supported, 0, |
65 | "1 if the kernel was built with entropy analysis support; 0 otherwise" ); |
66 | |
67 | #if ENTROPY_ANALYSIS_SUPPORTED |
68 | |
69 | SYSCTL_INT(_kern_entropy_analysis, OID_AUTO, enabled, CTLFLAG_RD, &entropy_analysis_enabled, 0, |
70 | "1 if entropy analysis is enabled (via boot arg); 0 otherwise" ); |
71 | SYSCTL_UINT(_kern_entropy_analysis, OID_AUTO, max_sample_count, CTLFLAG_RD | CTLFLAG_NOAUTO, &entropy_analysis_max_sample_count, 0, |
72 | "Target count of samples to be collected" ); |
73 | SYSCTL_UINT(_kern_entropy_analysis, OID_AUTO, sample_count, CTLFLAG_RD | CTLFLAG_NOAUTO, &entropy_analysis_sample_count, 0, |
74 | "Current count of samples collected" ); |
75 | |
76 | static int entropy_analysis_sample_size = sizeof(entropy_sample_t); |
77 | SYSCTL_UINT(_kern_entropy_analysis, OID_AUTO, sample_size, CTLFLAG_RD | CTLFLAG_NOAUTO, &entropy_analysis_sample_size, 0, |
78 | "Size (in bytes) of a single sample" ); |
79 | |
80 | SYSCTL_UINT(_kern_entropy_analysis, OID_AUTO, buffer_size, CTLFLAG_RD | CTLFLAG_NOAUTO, &entropy_analysis_buffer_size, 0, |
81 | "Size (in bytes) of the buffer of samples" ); |
82 | |
83 | SYSCTL_UINT(_kern_entropy_analysis, OID_AUTO, filter_size, CTLFLAG_RD | CTLFLAG_NOAUTO, &entropy_analysis_filter_size, 0, |
84 | "Size (in bytes) of the filter bitmap" ); |
85 | |
86 | static int |
87 | sysctl_entropy_collect SYSCTL_HANDLER_ARGS |
88 | { |
89 | if (req->oldptr == USER_ADDR_NULL) { |
90 | return SYSCTL_OUT(req, NULL, entropy_analysis_buffer_size); |
91 | } |
92 | |
93 | return SYSCTL_OUT(req, entropy_analysis_buffer, entropy_analysis_buffer_size); |
94 | } |
95 | |
96 | SYSCTL_PROC(_kern_entropy_analysis, OID_AUTO, buffer, |
97 | CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NOAUTO, |
98 | NULL, 0, sysctl_entropy_collect, "-" , |
99 | "The buffer of samples" ); |
100 | |
101 | static int |
102 | sysctl_entropy_filter SYSCTL_HANDLER_ARGS |
103 | { |
104 | if (req->oldptr == USER_ADDR_NULL) { |
105 | return SYSCTL_OUT(req, NULL, entropy_analysis_filter_size); |
106 | } |
107 | |
108 | // There is one bit in the bitmap for each sample. |
109 | unsigned filter_nbits = entropy_analysis_max_sample_count; |
110 | |
111 | bitmap_t *filter = bitmap_alloc(filter_nbits); |
112 | if (!filter) { |
113 | return ENOMEM; |
114 | } |
115 | |
116 | entropy_filter(entropy_analysis_max_sample_count, entropy_analysis_buffer, BITMAP_LEN(filter_nbits), filter); |
117 | |
118 | int status = SYSCTL_OUT(req, filter, entropy_analysis_filter_size); |
119 | |
120 | bitmap_free(filter, filter_nbits); |
121 | return status; |
122 | } |
123 | |
124 | SYSCTL_PROC(_kern_entropy_analysis, OID_AUTO, filter, |
125 | CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NOAUTO, |
126 | NULL, 0, sysctl_entropy_filter, "-" , |
127 | "The bitmap of filtered samples" ); |
128 | |
129 | __startup_func |
130 | static void |
131 | entropy_analysis_sysctl_startup(void) |
132 | { |
133 | uint32_t sample_count = 0; |
134 | if (__improbable(PE_parse_boot_argn(ENTROPY_ANALYSIS_BOOTARG, &sample_count, sizeof(sample_count)))) { |
135 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_max_sample_count); |
136 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_sample_count); |
137 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_sample_size); |
138 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_buffer_size); |
139 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_buffer); |
140 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_filter_size); |
141 | sysctl_register_oid_early(&sysctl__kern_entropy_analysis_filter); |
142 | } |
143 | } |
144 | STARTUP(SYSCTL, STARTUP_RANK_MIDDLE, entropy_analysis_sysctl_startup); |
145 | |
146 | #endif // ENTROPY_ANALYSIS_SUPPORTED |
147 | |