forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_TypeConfusion_bugs.js
More file actions
400 lines (359 loc) · 14.8 KB
/
Array_TypeConfusion_bugs.js
File metadata and controls
400 lines (359 loc) · 14.8 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
//Note: see function ArraySpliceHelper of JavascriptArray.cpp
if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
}
var tests = [
{
name: "OS7342663:OOB writes using type confusion in InternalCopyArrayElements",
body: function ()
{
function test() {
var arr1 = [0xdead, 0xbabe, 0xdead, 0xbabe];
class MyArray extends Uint32Array { }
Object.defineProperty(MyArray, Symbol.species, { value: function() { return arr1; } });
var float_val = 0xdaddeadbabe * 4.9406564584124654E-324;
var test = [float_val, float_val, float_val, float_val];
test.length = 0x1000;
test.__proto__ = new MyArray(0);
var res = Array.prototype.slice.apply(test, []); // OOB write
assert.areEqual(0x1000, res.length, "res.length == 0x1000");
assert.areEqual(float_val, res[0], "res[0] == float_val");
assert.areEqual(float_val, res[1], "res[1] == float_val");
assert.areEqual(float_val, res[2], "res[2] == float_val");
assert.areEqual(float_val, res[3], "res[3] == float_val");
assert.areEqual(undefined, res[4], "res[4] == float_val");
assert.areEqual(undefined, res[0xfff], "res[0xfff] == undefined");
}
test();
test();
test();
}
},
{
name: "OS7342689:OOB writes using type confusion in InternalFillFromPrototypes",
body: function ()
{
function test() {
var arr1 = [0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead,
0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe,
0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead,
0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe];
class MyArray extends Uint32Array { }
Object.defineProperty(MyArray, Symbol.species, { value: function() { return arr1; } });
var float_val = 0xdaddeadbabe * 4.9406564584124654E-324;
var test = [{}];
delete test[0];
test.length = 0x1000;
var src = [float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val,
float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val,
float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val,
float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val,
float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val, float_val];
test.__proto__ = src;
test.__proto__.__proto__ = new MyArray(0);
//this will write 0xfffc0daddeadbabe to [arr1] + 0x1D8
var res = Array.prototype.slice.apply(test, [])
assert.areEqual(0x1000, res.length, "res.length == 0x1000");
assert.areEqual(float_val, res[0], "res[0] == float_val");
assert.areEqual(float_val, res[1], "res[1] == float_val");
assert.areEqual(float_val, res[2], "res[2] == float_val");
assert.areEqual(float_val, res[src.length-1], "res[src.length-1] == float_val");
assert.areEqual(undefined, res[src.length], "res[src] == undefined");
assert.areEqual(undefined, res[0xfff], "res[0xfff] == undefined");
}
test();
test();
test();
}
},
{
name: "OS7307908:type confusion in Array.prototype.slice",
body: function ()
{
function test() {
var arr = [1, 2]
//Our species function will get called during chakra!Js::JavascriptArray::SliceHelper<unsigned int>
Object.defineProperty(
arr.constructor,
Symbol.species,
{
value : function()
{
//change 'arr' from TypeIds_NativeIntArray to TypeIds_Array
arr[0] = WScript;
//return a TypeIds_NativeIntArray so we can read back out the 64 bit pointer as two 32bit ints.
return [];
}
}
);
//trigger the bug and retrieve a TypeIds_NativeIntArray array containing a pointer.
var brr = arr.slice();
assert.areEqual(2, brr.length, "brr.length == 2");
assert.areEqual(WScript, brr[0], "brr[0] == WScript");
assert.areEqual(2, brr[1], "brr[0] == WScript");
}
test();
test();
test();
}
},
{
name: "OS7342791:type confusion in Array.from",
body: function ()
{
function test() {
var arr1 = [0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe, 0xdead, 0xbabe];
var float_val = 0xdaddeadbabe * 4.9406564584124654E-324;
var test = [float_val, float_val, float_val, float_val];
delete test[0];
delete test[1];
delete test[2];
var res = Array.from.apply(function(){return arr1}, [test]);
assert.areEqual(4, res.length, "res.length == 4");
assert.areEqual(undefined, res[0], "res[0] == undefined");
assert.areEqual(undefined, res[1], "res[1] == undefined");
assert.areEqual(undefined, res[2], "res[2] == undefined");
assert.areEqual(float_val, res[3], "res[3] == float_val");
assert.areEqual(['1','2','3'], Array.from.apply(()=>new Array(), ["123"]), "Array.from on iterable");
assert.areEqual([1,2,3], Array.from.apply(()=>new Array(), [{"0":1, "1":2, "2":3, "length":3}]), "Array.from on non-iterable");
}
test();
test();
test();
}
},
{
name: "OS7342844:type confusion in Array.of",
body: function ()
{
function test() {
var brr = Array.of.call(()=>[ 1, 2, 3, 4 ],
WScript, // supply 2 copies of target so the brr array will have a length of 2 and we can read the 64bit pointer.
WScript
);
assert.areEqual(2, brr.length, "brr.length == 2");
assert.areEqual(WScript, brr[0], "res[0] == WScript");
assert.areEqual(WScript, brr[1], "res[1] == WScript");
assert.areEqual(undefined, brr[2], "res[2] == undefined");
assert.areEqual(undefined, brr[3], "res[3] == undefined");
}
test();
test();
test();
}
},
{
name: "OS7342907:type confusion in Array.prototype.map",
body: function ()
{
function test() {
var arr = [ 1, 2 ];
Object.defineProperty(
arr.constructor,
Symbol.species,
{
value : function()
{
return [];
}
}
);
//The value returned from our callback is directly set into the array whose type we create via the species.
var brr = arr.map( function( v )
{
if( v == 1 )
return WScript;
}
);
assert.areEqual(2, brr.length, "brr.length == 2");
assert.areEqual(WScript, brr[0], "brr[0] == WScript");
assert.areEqual(undefined, brr[1], "brr[1] == undefined");
}
test();
test();
test();
}
},
{
name: "type confusion in Array.prototype.map with Proxy",
body: function ()
{
function test() {
var d = [1,2,3];
class dummy {
constructor() {
return d;
}
}
var handler = {
get: function(target, name) {
if(name == "length") {
return 0x100;
}
return {[Symbol.species] : dummy};
},
has: function(target, name) {
return true;
}
};
var p = new Proxy([], handler);
var a = new Array(1,2,3);
function test(){
return 0x777777777777;
}
var o = a.map.call(p, test);
assert.areEqual(Array(0x100).fill(0x777777777777), o);
}
test();
test();
test();
}
},
{
name: "OS7342965:type confusion in Array.prototype.splice",
body: function ()
{
function test() {
//create a TypeIds_Array holding two 64 bit values (The same amount of space for four 32 bit values).
var arr = [ WScript, WScript ];
//Our species function will get called during chakra!Js::JavascriptArray::EntrySplice
Object.defineProperty(
arr.constructor,
Symbol.species,
{
value : function()
{
//return a TypeIds_NativeIntArray so we can read back out a 64 bit pointer as two 32bit ints.
return [ 1, 2, 3, 4 ];
}
}
);
//trigger the bug and retrieve a TypeIds_NativeIntArray array containing a pointer. The helper
//method ArraySegmentSpliceHelper<Var> will directly copy over the TypeIds_Array segment data
//into the TypeIds_NativeIntArray segment.
var brr = arr.splice( 0, 2 );
assert.areEqual(2, brr.length, "brr.length == 2");
assert.areEqual(WScript, brr[0], "brr[0] == WScript");
assert.areEqual(WScript, brr[1], "brr[1] == WScript");
assert.areEqual(undefined, brr[2], "brr[2] == undefined");
assert.areEqual(undefined, brr[3], "brr[3] == undefined");
}
test();
test();
test();
}
},
{
name: "type confusion in Array.prototype.join",
body: function ()
{
function test() {
var a = [0, 1, 2, 3];
var b = [];
delete a[0];
Object.setPrototypeOf(a, b)
Object.defineProperty(b, "0",
{
get: function() {
a[2] = "abc";
return -1;
}
});
assert.areEqual("-1,1,abc,3", a.join());
}
test();
test();
test();
}
},
{
name: "type confusion in Array.prototype.indexOf",
body: function ()
{
function test() {
var float_val = 0xdaddeadbabe * 4.9406564584124654E-324;
var a = [0, 1, 2, 3];
var b = [];
delete a[1];
Object.setPrototypeOf(a, b);
Object.defineProperty(b, "1",
{
get: function() {
a[2] = float_val; //"abc";
return -1;
}
});
assert.areEqual(3, a.indexOf(3));
}
test();
test();
test();
}
},
{
name: "type confusion in Array.prototype.lastIndexOf",
body: function ()
{
function test() {
var float_val = 0xdaddeadbabe * 4.9406564584124654E-324;
var a = [3, 2, 1, 0];
var b = [];
delete a[3];
Object.setPrototypeOf(a, b);
Object.defineProperty(b, "3",
{
get: function() {
a[1] = float_val; //"abc";
return -1;
}
});
assert.areEqual(0, a.lastIndexOf(3));
}
test();
test();
test();
}
},
{
name: "type confusion in Function.prototype.apply",
body: function ()
{
function test() {
var t = [1,2,3];
function f(){
var h = [];
var a = [...arguments]
for(item in a){
var n = new Number(a[item]);
if( n < 0) {
n = n + 0x100000000;
}
h.push(n.toString(16));
}
return h;
}
var q = f;
t.length = 20;
var o = {};
Object.defineProperty(o, '3', {
get: function() {
var ta = [];
ta.fill.call(t, "natalie");
return 5;
}
});
t.__proto__ = o;
var j = [];
assert.areEqual("1,2,3,5,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN", f.apply(null, t).toString());
}
test();
test();
test();
}
},
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });