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
|
/** \file
* \brief Wrappers for Workers
*
* \author Copyright (C) 2019-2022 Libor Polcak
* \author Copyright (C) 2020 Peter Hornak
* \author Copyright (C) 2021 Matus Svancar
*
* \license SPDX-License-Identifier: GPL-3.0-or-later
*/
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
/** \file
* \ingroup wrappers
*
* This wrapper aims on prevention of microarchitectural attacks. This
* code was originally a part of [ChromeZero](https://github.com/IAIK/ChromeZero).
*
* The wrappers support the following behaviour:
*
* * Polyfill: Completely eliminates the paralelism.
* * Randomly slow messages: Add noise to the `postMessage` method execution.
*
* \see Lipp, M., Gruss, D., Schwarz, M., Bidner, D., Maurice, C. et al. Practical
Keystroke Timing Attacks in Sandboxed JavaScript. In:. August 2017, s. 191–209.
ISBN 978-3-319-66398-2.
*
* \see Schwarz, M., Lipp, M. a Gruss, D. JavaScript Zero: Real JavaScript and Zero
* Side-Channel Attacks. NDSS'18.
*
* \see <https://www.fit.vut.cz/study/thesis/22374/?year=0&sup=Pol%C4%8D%C3%A1k>
*/
/*
* Create private namespace
*/
(function() {
var strictWorkerWrapperBody = `
// We create a Worker to limit fingerprinting but we intentionally use a non-existing URL.
return new originalF("https://[ff00::]/worker.js");
`;
var slowBody = `
let _data = new originalF(path, ...args);
let _old = _data.postMessage;
_data.postMessage = function(message) {
let delay = Math.floor(Math.random() * 10**9)
let j;
for (let i = 0; i < delay;) {
j = i;
i = j + 1;
}
return _old.call(_data, message);
}
return _data;
`;
var wrappers = [
{
parent_object: "Navigator.prototype",
parent_object_property: "hardwareConcurrency",
wrapped_objects: [],
helping_code: `
var hw_prng = alea(domainHash, "Navigator.prototype.hardwareConcurrency");
var ret = 2;
if(args[0]==0){
var realValue = navigator.hardwareConcurrency;
ret = Math.floor(2+hw_prng()*(realValue-2));
}
else if(args[0]==1){
ret = Math.floor(2+(hw_prng()*6));
}
`,
post_wrapping_code: [
{
code_type: "object_properties",
wrapped_name: "origConcurrency",
wrapped_objects: [],
parent_object: "Navigator.prototype",
parent_object_property: "hardwareConcurrency",
/** \brief replaces navigator.hardwareConcurrency getter
*
* Depending on level chosen this property returns:
* * (0) - random valid value from range [2 - real value]
* * (1) - random valid value from range [2 - 8]
* * (2) - 2
*/
wrapped_properties: [
{
property_name: "get",
property_value: `
function() {
return ret;
}`,
},
],
}
],
},
{
parent_object: "window",
parent_object_property: "Worker",
original_function: "window.Worker",
wrapped_objects: [],
helping_code: `
let strictWrappers = args[0];
let removeWorkers = args[1];
`,
wrapping_function_args: `path, ...args`,
wrapping_function_body: `
if (!removeWorkers && strictWrappers) {
${strictWorkerWrapperBody}
} else {
${slowBody}
}
`,
post_wrapping_code: [
{
code_type: "delete_properties",
parent_object: "window",
apply_if: "removeWorkers",
delete_properties: ["Worker"],
}
],
}
]
add_wrappers(wrappers);
})();
|