OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
8 * This is the core of ia32-mode validator. Please note that this file | |
9 * combines ragel machine description and C language actions. Please read | |
10 * validator_internals.html first to understand how the whole thing is built: | |
11 * it explains how the byte sequences are constructed, what constructs like | |
12 * "@{}" or "REX_WRX?" mean, etc. | |
13 */ | |
14 | |
15 #include <assert.h> | |
16 #include <errno.h> | |
17 #include <stddef.h> | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 #include <string.h> | |
21 | |
22 #include "native_client/src/trusted/validator_ragel/bitmap.h" | |
23 #include "native_client/src/trusted/validator_ragel/unreviewed/validator_interna
l.h" | |
24 | |
25 /* Ignore this information: it's not used by security model in IA32 mode. */ | |
26 #undef GET_VEX_PREFIX3 | |
27 #define GET_VEX_PREFIX3 0 | |
28 #undef SET_VEX_PREFIX3 | |
29 #define SET_VEX_PREFIX3(P) | |
30 | |
31 %%{ | |
32 machine x86_32_validator; | |
33 alphtype unsigned char; | |
34 variable p current_position; | |
35 variable pe end_of_bundle; | |
36 variable eof end_of_bundle; | |
37 variable cs current_state; | |
38 | |
39 include byte_machine "byte_machines.rl"; | |
40 | |
41 include prefixes_parsing_validator | |
42 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
43 include vex_actions_ia32 | |
44 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
45 include vex_parsing_ia32 | |
46 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
47 include displacement_fields_actions | |
48 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
49 include displacement_fields_parsing | |
50 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
51 include modrm_parsing_ia32_validator | |
52 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
53 include immediate_fields_actions | |
54 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
55 include immediate_fields_parsing_ia32 | |
56 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
57 include relative_fields_validator_actions | |
58 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
59 include relative_fields_parsing | |
60 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
61 include cpuid_actions | |
62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
63 | |
64 # Action which marks last byte as not immediate. Most 3DNow! instructions, | |
65 # some AVX and XOP instructions have this property. It's referenced by | |
66 # decode_x86_32 machine in [autogenerated] "validator_x86_32_instruction.rl" | |
67 # file. | |
68 action last_byte_is_not_immediate { | |
69 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; | |
70 } | |
71 | |
72 include decode_x86_32 "validator_x86_32_instruction.rl"; | |
73 | |
74 special_instruction = | |
75 # and $~0x1f, %eXX call %eXX | |
76 # vvvvvvvvvv | |
77 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax | |
78 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx | |
79 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx | |
80 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx | |
81 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp | |
82 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp | |
83 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi | |
84 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi | |
85 # ^^^^ ^^^^ | |
86 # and $~0x1f, %eXX jmp %eXX | |
87 @{ | |
88 UnmarkValidJumpTarget((current_position - data) - 1, valid_targets); | |
89 instruction_begin -= 3; | |
90 instruction_info_collected |= SPECIAL_INSTRUCTION; | |
91 } | | |
92 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax | |
93 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) | |
94 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg | |
95 | |
96 # Check if call is properly aligned | |
97 # | |
98 # For direct call we explicitly encode all variations. For indirect call | |
99 # we accept all the special instructions which ends with register-addressed | |
100 # indirect call. | |
101 call_alignment = | |
102 ((one_instruction & | |
103 # Direct call | |
104 ((data16 0xe8 rel16) | | |
105 (0xe8 rel32))) | | |
106 (special_instruction & | |
107 # Indirect call | |
108 (any* data16? 0xff ((opcode_2 | opcode_3) any* & | |
109 modrm_registers)))) | |
110 # Call instruction must aligned to the end of bundle. Previously this was | |
111 # strict requirement, today it's just warning to aid with debugging. | |
112 @{ | |
113 if (((current_position - data) & kBundleMask) != kBundleMask) | |
114 instruction_info_collected |= BAD_CALL_ALIGNMENT; | |
115 }; | |
116 | |
117 # This action calls user's callback (if needed) and cleans up validator's | |
118 # internal state. | |
119 # | |
120 # We call the user callback if there are validation errors or if the | |
121 # CALL_USER_CALLBACK_ON_EACH_INSTRUCTION option is used. | |
122 # | |
123 # After that we move instruction_begin and clean all the variables which | |
124 # only used in the processing of a single instruction (prefixes, operand | |
125 # states and instruction_info_collected). | |
126 action end_of_instruction_cleanup { | |
127 /* Mark start of this instruction as a valid target for jump. */ | |
128 MarkValidJumpTarget(instruction_begin - data, valid_targets); | |
129 | |
130 /* Call user-supplied callback. */ | |
131 instruction_end = current_position + 1; | |
132 if ((instruction_info_collected & VALIDATION_ERRORS_MASK) || | |
133 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { | |
134 result &= user_callback(instruction_begin, instruction_end, | |
135 instruction_info_collected, callback_data); | |
136 } | |
137 | |
138 /* On successful match the instruction_begin must point to the next byte | |
139 * to be able to report the new offset as the start of instruction | |
140 * causing error. */ | |
141 instruction_begin = instruction_end; | |
142 | |
143 /* Clear variables (well, one variable currently). */ | |
144 instruction_info_collected = 0; | |
145 } | |
146 | |
147 # This action reports fatal error detected by DFA. | |
148 action report_fatal_error { | |
149 result &= user_callback(instruction_begin, current_position, | |
150 UNRECOGNIZED_INSTRUCTION, callback_data); | |
151 /* | |
152 * Process the next bundle: "continue" here is for the "for" cycle in | |
153 * the ValidateChunkIA32 function. | |
154 * | |
155 * It does not affect the case which we really care about (when code | |
156 * is validatable), but makes it possible to detect more errors in one | |
157 * run in tools like ncval. | |
158 */ | |
159 continue; | |
160 } | |
161 | |
162 # This is main ragel machine: it does 99% of validation work. There are only | |
163 # one thing to do if this machine accepts the bundles - check that direct | |
164 # jumps are correct. This is done in the following way: | |
165 # * DFA fills two arrays: valid_targets and jump_dests. | |
166 # * ProcessInvalidJumpTargets checks that "jump_dests & !valid_targets == 0". | |
167 # All other checks are done here. | |
168 main := ((call_alignment | one_instruction | special_instruction) | |
169 @end_of_instruction_cleanup)* | |
170 $!report_fatal_error; | |
171 | |
172 }%% | |
173 | |
174 %% write data; | |
175 | |
176 | |
177 Bool ValidateChunkIA32(const uint8_t *data, size_t size, | |
178 uint32_t options, | |
179 const NaClCPUFeaturesX86 *cpu_features, | |
180 ValidationCallbackFunc user_callback, | |
181 void *callback_data) { | |
182 bitmap_word valid_targets_small; | |
183 bitmap_word jump_dests_small; | |
184 bitmap_word *valid_targets; | |
185 bitmap_word *jump_dests; | |
186 const uint8_t *current_position; | |
187 const uint8_t *end_of_bundle; | |
188 int result = TRUE; | |
189 | |
190 CHECK(sizeof valid_targets_small == sizeof jump_dests_small); | |
191 CHECK(size % kBundleSize == 0); | |
192 | |
193 /* For a very small sequences (one bundle) malloc is too expensive. */ | |
194 if (size <= (sizeof valid_targets_small * 8)) { | |
195 valid_targets_small = 0; | |
196 valid_targets = &valid_targets_small; | |
197 jump_dests_small = 0; | |
198 jump_dests = &jump_dests_small; | |
199 } else { | |
200 valid_targets = BitmapAllocate(size); | |
201 jump_dests = BitmapAllocate(size); | |
202 if (!valid_targets || !jump_dests) { | |
203 free(jump_dests); | |
204 free(valid_targets); | |
205 errno = ENOMEM; | |
206 return FALSE; | |
207 } | |
208 } | |
209 | |
210 /* | |
211 * This option is usually used in tests: we will process the whole chunk | |
212 * in one pass. Usually each bundle is processed separately which means | |
213 * instructions (and super-instructions) can not cross borders of the bundle. | |
214 */ | |
215 if (options & PROCESS_CHUNK_AS_A_CONTIGUOUS_STREAM) | |
216 end_of_bundle = data + size; | |
217 else | |
218 end_of_bundle = data + kBundleSize; | |
219 | |
220 /* | |
221 * Main loop. Here we process the data array bundle-after-bundle. | |
222 * Ragel-produced DFA does all the checks with one exception: direct jumps. | |
223 * It collects the two arrays: valid_targets and jump_dests which are used | |
224 * to test direct jumps later. | |
225 */ | |
226 for (current_position = data; | |
227 current_position < data + size; | |
228 current_position = end_of_bundle, | |
229 end_of_bundle = current_position + kBundleSize) { | |
230 /* Start of the instruction being processed. */ | |
231 const uint8_t *instruction_begin = current_position; | |
232 /* Only used locally in the end_of_instruction_cleanup action. */ | |
233 const uint8_t *instruction_end; | |
234 uint32_t instruction_info_collected = 0; | |
235 int current_state; | |
236 | |
237 %% write init; | |
238 %% write exec; | |
239 } | |
240 | |
241 /* | |
242 * Check the direct jumps. All the targets from jump_dests must be in | |
243 * valid_targets. | |
244 */ | |
245 result &= ProcessInvalidJumpTargets(data, size, valid_targets, jump_dests, | |
246 user_callback, callback_data); | |
247 | |
248 /* We only use malloc for a large code sequences */ | |
249 if (jump_dests != &jump_dests_small) free(jump_dests); | |
250 if (valid_targets != &valid_targets_small) free(valid_targets); | |
251 if (!result) errno = EINVAL; | |
252 return result; | |
253 } | |
OLD | NEW |