1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package ao486.test.io;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit;
|
30 |
|
|
import ao486.test.layers.FlagsLayer;
|
31 |
|
|
import ao486.test.layers.GeneralRegisterLayer;
|
32 |
|
|
import ao486.test.layers.HandleModeChangeLayer;
|
33 |
|
|
import ao486.test.layers.IOLayer;
|
34 |
|
|
import ao486.test.layers.InstructionLayer;
|
35 |
|
|
import ao486.test.layers.Layer;
|
36 |
|
|
import ao486.test.layers.MemoryLayer;
|
37 |
|
|
import ao486.test.layers.MemoryPatchLayer;
|
38 |
|
|
import ao486.test.layers.OtherLayer;
|
39 |
|
|
import ao486.test.layers.Pair;
|
40 |
|
|
import ao486.test.layers.SegmentLayer;
|
41 |
|
|
import ao486.test.layers.StackLayer;
|
42 |
|
|
import java.io.*;
|
43 |
|
|
import java.util.LinkedList;
|
44 |
|
|
import java.util.Random;
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
public class TestIN_protected extends TestUnit implements Serializable {
|
48 |
|
|
public static void main(String args[]) throws Exception {
|
49 |
|
|
run_test(TestIN_protected.class);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
//--------------------------------------------------------------------------
|
53 |
|
|
@Override
|
54 |
|
|
public int get_test_count() throws Exception {
|
55 |
|
|
return 100;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
@Override
|
59 |
|
|
public void init() throws Exception {
|
60 |
|
|
|
61 |
|
|
random = new Random(105 + index);
|
62 |
|
|
|
63 |
|
|
String instruction;
|
64 |
|
|
while(true) {
|
65 |
|
|
layers.clear();
|
66 |
|
|
|
67 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
68 |
|
|
|
69 |
|
|
//-----
|
70 |
|
|
|
71 |
|
|
/* type:
|
72 |
|
|
* 0 - no io_allow
|
73 |
|
|
* 1 - no valid 32-bit TSS
|
74 |
|
|
* 2 - TR.limit < 103
|
75 |
|
|
* 3 - port permission bits >= limit
|
76 |
|
|
* 4 - port permission bits invalid
|
77 |
|
|
* 5 - all ok
|
78 |
|
|
*/
|
79 |
|
|
|
80 |
|
|
int type = random.nextInt(6);
|
81 |
|
|
|
82 |
|
|
int next_port = 0;
|
83 |
|
|
int next_rpl = 0;
|
84 |
|
|
boolean next_cr0_pe = true;
|
85 |
|
|
boolean next_tr_valid = true;
|
86 |
|
|
int next_tr_base = 0;
|
87 |
|
|
int next_tr_limit = 0;
|
88 |
|
|
int next_tr_type = 0;
|
89 |
|
|
boolean next_vm = false;
|
90 |
|
|
int next_iopl = 0;
|
91 |
|
|
|
92 |
|
|
if(type == 0) {
|
93 |
|
|
do {
|
94 |
|
|
next_rpl = random.nextInt(4);
|
95 |
|
|
next_cr0_pe = random.nextBoolean();
|
96 |
|
|
next_tr_valid = random.nextBoolean();
|
97 |
|
|
next_tr_base = random.nextInt();
|
98 |
|
|
next_tr_limit = random.nextInt(1048576);
|
99 |
|
|
next_tr_type = random.nextInt(16);
|
100 |
|
|
next_vm = random.nextBoolean();
|
101 |
|
|
next_iopl = random.nextInt(4);
|
102 |
|
|
}while(next_cr0_pe && (next_vm || next_rpl > next_iopl));
|
103 |
|
|
|
104 |
|
|
next_port = random.nextInt(65536);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
if(type == 1) {
|
108 |
|
|
do {
|
109 |
|
|
next_rpl = random.nextInt(4);
|
110 |
|
|
next_cr0_pe = random.nextBoolean();
|
111 |
|
|
next_vm = random.nextBoolean();
|
112 |
|
|
next_iopl = random.nextInt(4);
|
113 |
|
|
}while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
|
114 |
|
|
|
115 |
|
|
do {
|
116 |
|
|
next_tr_valid = random.nextBoolean();
|
117 |
|
|
next_tr_type = random.nextInt(16);
|
118 |
|
|
}while( !(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11)));
|
119 |
|
|
|
120 |
|
|
next_tr_base = random.nextInt();
|
121 |
|
|
next_tr_limit = random.nextInt(1048576);
|
122 |
|
|
next_port = random.nextInt();
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
if(type == 2) {
|
126 |
|
|
do {
|
127 |
|
|
next_rpl = random.nextInt(4);
|
128 |
|
|
next_cr0_pe = random.nextBoolean();
|
129 |
|
|
next_vm = random.nextBoolean();
|
130 |
|
|
next_iopl = random.nextInt(4);
|
131 |
|
|
}while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
|
132 |
|
|
|
133 |
|
|
do {
|
134 |
|
|
next_tr_valid = random.nextBoolean();
|
135 |
|
|
next_tr_type = random.nextInt(16);
|
136 |
|
|
}while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
|
137 |
|
|
|
138 |
|
|
do {
|
139 |
|
|
next_tr_limit = random.nextInt(103);
|
140 |
|
|
}while( !(next_tr_limit < 103));
|
141 |
|
|
|
142 |
|
|
next_tr_base = random.nextInt();
|
143 |
|
|
next_port = random.nextInt();
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
if(type == 3) {
|
147 |
|
|
do {
|
148 |
|
|
next_rpl = random.nextInt(4);
|
149 |
|
|
next_cr0_pe = random.nextBoolean();
|
150 |
|
|
next_vm = random.nextBoolean();
|
151 |
|
|
next_iopl = random.nextInt(4);
|
152 |
|
|
}while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
|
153 |
|
|
|
154 |
|
|
do {
|
155 |
|
|
next_tr_valid = random.nextBoolean();
|
156 |
|
|
next_tr_type = random.nextInt(16);
|
157 |
|
|
}while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
|
158 |
|
|
|
159 |
|
|
do {
|
160 |
|
|
next_tr_limit = random.nextInt(1048576);
|
161 |
|
|
}while(next_tr_limit < 103);
|
162 |
|
|
|
163 |
|
|
int permission_base = 0;
|
164 |
|
|
do {
|
165 |
|
|
permission_base = random.nextInt(65536);
|
166 |
|
|
next_port = random.nextInt(65536);
|
167 |
|
|
next_tr_limit = random.nextInt(1048576-103); next_tr_limit += 103;
|
168 |
|
|
}while( !(permission_base + (next_port/8) >= next_tr_limit));
|
169 |
|
|
|
170 |
|
|
next_tr_base = random.nextInt();
|
171 |
|
|
|
172 |
|
|
MemoryPatchLayer tss_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + 102),
|
173 |
|
|
(byte)(permission_base & 0xFF), ((permission_base >> 8) & 0xFF));
|
174 |
|
|
layers.addFirst(tss_patch);
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
if(type == 4) {
|
178 |
|
|
do {
|
179 |
|
|
next_rpl = random.nextInt(4);
|
180 |
|
|
next_cr0_pe = random.nextBoolean();
|
181 |
|
|
next_vm = random.nextBoolean();
|
182 |
|
|
next_iopl = random.nextInt(4);
|
183 |
|
|
}while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
|
184 |
|
|
|
185 |
|
|
do {
|
186 |
|
|
next_tr_valid = random.nextBoolean();
|
187 |
|
|
next_tr_type = random.nextInt(16);
|
188 |
|
|
}while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
|
189 |
|
|
|
190 |
|
|
do {
|
191 |
|
|
next_tr_limit = random.nextInt(1048576);
|
192 |
|
|
}while(next_tr_limit < 103);
|
193 |
|
|
|
194 |
|
|
int permission_base = 0;
|
195 |
|
|
do {
|
196 |
|
|
permission_base = random.nextInt(65536);
|
197 |
|
|
next_port = random.nextInt(65536);
|
198 |
|
|
next_tr_limit = random.nextInt(1048576-103); next_tr_limit += 103;
|
199 |
|
|
}while(permission_base + (next_port/8) >= next_tr_limit);
|
200 |
|
|
|
201 |
|
|
next_tr_base = random.nextInt();
|
202 |
|
|
|
203 |
|
|
MemoryPatchLayer tss_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + 102),
|
204 |
|
|
(byte)(permission_base & 0xFF), ((permission_base >> 8) & 0xFF));
|
205 |
|
|
layers.addFirst(tss_patch);
|
206 |
|
|
|
207 |
|
|
int permission_bits = 0;
|
208 |
|
|
if(random.nextInt(4) == 0) permission_bits = random.nextInt();
|
209 |
|
|
|
210 |
|
|
MemoryPatchLayer perm_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + permission_base + next_port/8 + 0),
|
211 |
|
|
(byte)(permission_bits & 0xFF), (permission_bits >> 8) & 0xFF);
|
212 |
|
|
layers.addFirst(perm_patch);
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
if(type == 5) {
|
216 |
|
|
do {
|
217 |
|
|
next_rpl = random.nextInt(4);
|
218 |
|
|
next_cr0_pe = random.nextBoolean();
|
219 |
|
|
next_vm = random.nextBoolean();
|
220 |
|
|
next_iopl = random.nextInt(4);
|
221 |
|
|
}while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
|
222 |
|
|
|
223 |
|
|
do {
|
224 |
|
|
next_tr_valid = random.nextBoolean();
|
225 |
|
|
next_tr_type = random.nextInt(16);
|
226 |
|
|
}while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
|
227 |
|
|
|
228 |
|
|
do {
|
229 |
|
|
next_tr_limit = random.nextInt(1048576);
|
230 |
|
|
}while(next_tr_limit < 103);
|
231 |
|
|
|
232 |
|
|
int permission_base = 0;
|
233 |
|
|
do {
|
234 |
|
|
permission_base = random.nextInt(65536);
|
235 |
|
|
next_port = random.nextInt(65536);
|
236 |
|
|
next_tr_limit = random.nextInt(1048576-103); next_tr_limit += 103;
|
237 |
|
|
}while(permission_base + (next_port/8) >= next_tr_limit);
|
238 |
|
|
|
239 |
|
|
next_tr_base = random.nextInt();
|
240 |
|
|
|
241 |
|
|
MemoryPatchLayer tss_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + 102),
|
242 |
|
|
(byte)(permission_base & 0xFF), ((permission_base >> 8) & 0xFF));
|
243 |
|
|
layers.addFirst(tss_patch);
|
244 |
|
|
|
245 |
|
|
int permission_bits = random.nextInt();
|
246 |
|
|
permission_bits |= random.nextInt();
|
247 |
|
|
|
248 |
|
|
MemoryPatchLayer perm_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + permission_base + next_port/8 + 0),
|
249 |
|
|
(byte)(permission_bits & 0xFF), (permission_bits >> 8) & 0xFF);
|
250 |
|
|
layers.addFirst(perm_patch);
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
254 |
|
|
layers.add(instr);
|
255 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
256 |
|
|
layers.add(stack);
|
257 |
|
|
layers.add(new OtherLayer(next_cr0_pe? OtherLayer.Type.PROTECTED_OR_V8086 : OtherLayer.Type.REAL, random));
|
258 |
|
|
layers.add(new FlagsLayer(next_vm? FlagsLayer.Type.V8086 : FlagsLayer.Type.NOT_V8086, random));
|
259 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
260 |
|
|
layers.add(new SegmentLayer(random));
|
261 |
|
|
layers.add(new MemoryLayer(random));
|
262 |
|
|
layers.add(new IOLayer(random));
|
263 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
264 |
|
|
getInput("cr0_pe"),
|
265 |
|
|
getInput("vmflag"),
|
266 |
|
|
next_rpl, //getInput("cs_rpl"),
|
267 |
|
|
getInput("cs_p"),
|
268 |
|
|
getInput("cs_s"),
|
269 |
|
|
getInput("cs_type")
|
270 |
|
|
));
|
271 |
|
|
|
272 |
|
|
// instruction size
|
273 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
274 |
|
|
|
275 |
|
|
boolean a32 = random.nextBoolean();
|
276 |
|
|
boolean o32 = random.nextBoolean();
|
277 |
|
|
|
278 |
|
|
long cs_limit = getInput("cs_limit");
|
279 |
|
|
long cs_rpl = getInput("cs_rpl");
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
final int final_port = (random.nextInt() & 0xFFFF0000) | (next_port & 0xFFFF);
|
283 |
|
|
final long final_rpl = cs_rpl;
|
284 |
|
|
final boolean final_tr_valid = next_tr_valid;
|
285 |
|
|
final int final_tr_base = next_tr_base;
|
286 |
|
|
final int final_tr_limit = next_tr_limit;
|
287 |
|
|
final int final_tr_type = next_tr_type;
|
288 |
|
|
final int final_iopl = next_iopl;
|
289 |
|
|
|
290 |
|
|
Layer layer = new Layer() {
|
291 |
|
|
long edx() { return final_port; }
|
292 |
|
|
|
293 |
|
|
long iopl() { return final_iopl; }
|
294 |
|
|
|
295 |
|
|
long cs_rpl() { return final_rpl; }
|
296 |
|
|
|
297 |
|
|
long tr_valid() { return final_tr_valid? 1 : 0; }
|
298 |
|
|
long tr_base() { return final_tr_base; }
|
299 |
|
|
|
300 |
|
|
long tr_limit() { return final_tr_limit; }
|
301 |
|
|
|
302 |
|
|
long tr_type() { return final_tr_type; }
|
303 |
|
|
};
|
304 |
|
|
layers.addFirst(layer);
|
305 |
|
|
|
306 |
|
|
//-----------
|
307 |
|
|
|
308 |
|
|
String instruction_string = prepare_instr(cs_d_b, a32, o32);
|
309 |
|
|
|
310 |
|
|
// add instruction
|
311 |
|
|
instruction = instruction_string + instruction_string + "9090900F0F";
|
312 |
|
|
|
313 |
|
|
instr.add_instruction(instruction);
|
314 |
|
|
|
315 |
|
|
// end condition
|
316 |
|
|
break;
|
317 |
|
|
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
|
324 |
|
|
int opcodes[] = {
|
325 |
|
|
0xE4,0xE5,0xEC,0xED
|
326 |
|
|
};
|
327 |
|
|
|
328 |
|
|
String prefix = "";
|
329 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
330 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
331 |
|
|
|
332 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
333 |
|
|
|
334 |
|
|
int len = (opcode == 0xE4 || opcode == 0xE5)? 2 : 1;
|
335 |
|
|
|
336 |
|
|
byte instr[] = new byte[len];
|
337 |
|
|
instr[0] = (byte)opcode;
|
338 |
|
|
if(len >= 2) instr[1] = (byte)random.nextInt();
|
339 |
|
|
|
340 |
|
|
return prefix + bytesToHex(instr);
|
341 |
|
|
}
|
342 |
|
|
}
|