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