GeezerVison is showing github.com — src: perform integrity checks on built-in code cache · lufzle/node@7cbbb27
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/cache.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ module.exports = { | 48 | 48 | ), | 49 | 49 | builtinSource: Object.assign({}, NativeModule._source), | 50 | 50 | getCodeCache, | | 51 | + getSource: NativeModule.getSource, | 51 | 52 | codeCache: internalBinding('code_cache'), | 52 | 53 | compiledWithoutCache: NativeModule.compiledWithoutCache, | 53 | 54 | compiledWithCache: NativeModule.compiledWithCache, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/loaders.js +37-11Lines changed: 37 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 127 | 127 | const config = getBinding('config'); | 128 | 128 |
| 129 | 129 | const codeCache = getInternalBinding('code_cache'); | | 130 | + const codeCacheHash = getInternalBinding('code_cache_hash'); | | 131 | + const sourceHash = getInternalBinding('natives_hash'); | 130 | 132 | const compiledWithoutCache = NativeModule.compiledWithoutCache = []; | 131 | 133 | const compiledWithCache = NativeModule.compiledWithCache = []; | 132 | 134 |
| | 232 | 234 | }; | 233 | 235 |
| 234 | 236 | NativeModule.prototype.compile = function() { | 235 | | - let source = NativeModule.getSource(this.id); | | 237 | + const id = this.id; | | 238 | + let source = NativeModule.getSource(id); | 236 | 239 | source = NativeModule.wrap(source); | 237 | 240 |
| 238 | 241 | this.loading = true; | 239 | 242 |
| 240 | 243 | try { | | 244 | + // Currently V8 only checks that the length of the source code is the | | 245 | + // same as the code used to generate the hash, so we add an additional | | 246 | + // check here: | | 247 | + // 1. During compile time, when generating node_javascript.cc and | | 248 | + // node_code_cache.cc, we compute and include the hash of the | | 249 | + // (unwrapped) JavaScript source in both. | | 250 | + // 2. At runtime, we check that the hash of the code being compiled | | 251 | + // and the hash of the code used to generate the cache | | 252 | + // (inside the wrapper) is the same. | | 253 | + // This is based on the assumptions: | | 254 | + // 1. `internalBinding('code_cache_hash')` must be in sync with | | 255 | + // `internalBinding('code_cache')` (same C++ file) | | 256 | + // 2. `internalBinding('natives_hash')` must be in sync with | | 257 | + // `process.binding('natives')` (same C++ file) | | 258 | + // 3. If `internalBinding('natives_hash')` is in sync with | | 259 | + // `internalBinding('natives_hash')`, then the (unwrapped) | | 260 | + // code used to generate `internalBinding('code_cache')` | | 261 | + // should be in sync with the (unwrapped) code in | | 262 | + // `process.binding('natives')` | | 263 | + // There will be, however, false positives if the wrapper used | | 264 | + // to generate the cache is different from the one used at run time, | | 265 | + // and the length of the wrapper somehow stays the same. | | 266 | + // But that should be rare and can be eased once we make the | | 267 | + // two bootstrappers cached and checked as well. | | 268 | + const cache = codeCacheHash[id] && | | 269 | + (codeCacheHash[id] === sourceHash[id]) ? codeCache[id] : undefined; | | 270 | + | 241 | 271 | // (code, filename, lineOffset, columnOffset | 242 | 272 | // cachedData, produceCachedData, parsingContext) | 243 | 273 | const script = new ContextifyScript( | 244 | 274 | source, this.filename, 0, 0, | 245 | | - codeCache[this.id], false, undefined | | 275 | + cache, false, undefined | 246 | 276 | ); | 247 | 277 |
| | 278 | + // This will be used to create code cache in tools/generate_code_cache.js | 248 | 279 | this.script = script; | 249 | 280 |
| 250 | 281 | // One of these conditions may be false when any of the inputs | 251 | 282 | // of the `node_js2c` target in node.gyp is modified. | 252 | | - // FIXME(joyeecheung): | 253 | | - // 1. Figure out how to resolve the dependency issue. When the | 254 | | - // code cache was introduced we were at a point where refactoring | 255 | | - // node.gyp may not be worth the effort. | 256 | | - // 2. Calculate checksums in both js2c and generate_code_cache.js | 257 | | - // and compare them before compiling the native modules since | 258 | | - // V8 only checks the length of the source to decide whether to | 259 | | - // reject the cache. | 260 | | - if (!codeCache[this.id] || script.cachedDataRejected) { | | 283 | + // FIXME(joyeecheung): Figure out how to resolve the dependency issue. | | 284 | + // When the code cache was introduced we were at a point where refactoring | | 285 | + // node.gyp may not be worth the effort. | | 286 | + if (!cache || script.cachedDataRejected) { | 261 | 287 | compiledWithoutCache.push(this.id); | 262 | 288 | } else { | 263 | 289 | compiledWithCache.push(this.id); | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1731,6 +1731,14 @@ static void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { | 1731 | 1731 | // internalBinding('code_cache') | 1732 | 1732 | exports = Object::New(env->isolate()); | 1733 | 1733 | DefineCodeCache(env, exports); | | 1734 | + } else if (!strcmp(*module_v, "code_cache_hash")) { | | 1735 | + // internalBinding('code_cache_hash') | | 1736 | + exports = Object::New(env->isolate()); | | 1737 | + DefineCodeCacheHash(env, exports); | | 1738 | + } else if (!strcmp(*module_v, "natives_hash")) { | | 1739 | + // internalBinding('natives_hash') | | 1740 | + exports = Object::New(env->isolate()); | | 1741 | + DefineJavaScriptHash(env, exports); | 1734 | 1742 | } else { | 1735 | 1743 | return ThrowIfNoSuchModule(env, *module_v); | 1736 | 1744 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/node_code_cache.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | namespace node { | 9 | 9 |
| 10 | 10 | void DefineCodeCache(Environment* env, v8::Local<v8::Object> target); | | 11 | +void DefineCodeCacheHash(Environment* env, v8::Local<v8::Object> target); | 11 | 12 |
| 12 | 13 | } // namespace node | 13 | 14 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_code_cache_stub.cc +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,4 +11,10 @@ void DefineCodeCache(Environment* env, v8::Local<v8::Object> target) { | 11 | 11 | // (here as `target`) so this is a noop. | 12 | 12 | } | 13 | 13 |
| | 14 | +void DefineCodeCacheHash(Environment* env, v8::Local<v8::Object> target) { | | 15 | + // When we do not produce code cache for builtin modules, | | 16 | + // `internalBinding('code_cache_hash')` returns an empty object | | 17 | + // (here as `target`) so this is a noop. | | 18 | +} | | 19 | + | 14 | 20 | } // namespace node |
Collapse file Copy file name to clipboardExpand all lines: src/node_javascript.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 29 | 29 | namespace node { | 30 | 30 |
| 31 | 31 | void DefineJavaScript(Environment* env, v8::Local<v8::Object> target); | | 32 | +void DefineJavaScriptHash(Environment* env, v8::Local<v8::Object> target); | 32 | 33 | v8::Local<v8::String> NodePerContextSource(v8::Isolate* isolate); | 33 | 34 | v8::Local<v8::String> LoadersBootstrapperSource(Environment* env); | 34 | 35 | v8::Local<v8::String> NodeBootstrapperSource(Environment* env); | |
Collapse file Copy file name to clipboardExpand all lines: tools/generate_code_cache.js +29-3Lines changed: 29 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | const { | 11 | 11 | getCodeCache, | | 12 | + getSource, | 12 | 13 | cachableBuiltins | 13 | 14 | } = require('internal/bootstrap/cache'); | 14 | 15 |
| | 16 | +function hash(str) { | | 17 | + if (process.versions.openssl) { | | 18 | + return require('crypto').createHash('sha256').update(str).digest('hex'); | | 19 | + } | | 20 | + return ''; | | 21 | +} | | 22 | + | 15 | 23 | const fs = require('fs'); | 16 | 24 |
| 17 | 25 | const resultPath = process.argv[2]; | @@ -51,6 +59,8 @@ function getInitalizer(key, cache) { | 51 | 59 | const defName = key.replace(/\//g, '_').replace(/-/g, '_'); | 52 | 60 | const definition = `static uint8_t ${defName}_raw[] = {\n` + | 53 | 61 | `${cache.join(',')}\n};`; | | 62 | + const source = getSource(key); | | 63 | + const sourceHash = hash(source); | 54 | 64 | const initializer = ` | 55 | 65 | v8::Local<v8::ArrayBuffer> ${defName}_ab = | 56 | 66 | v8::ArrayBuffer::New(isolate, ${defName}_raw, ${cache.length}); | @@ -60,13 +70,19 @@ function getInitalizer(key, cache) { | 60 | 70 | FIXED_ONE_BYTE_STRING(isolate, "${key}"), | 61 | 71 | ${defName}_array).FromJust(); | 62 | 72 | `; | | 73 | + const hashIntializer = ` | | 74 | + target->Set(context, | | 75 | + FIXED_ONE_BYTE_STRING(isolate, "${key}"), | | 76 | + OneByteString(isolate, "${sourceHash}")).FromJust(); | | 77 | + `; | 63 | 78 | return { | 64 | | - definition, initializer | | 79 | + definition, initializer, hashIntializer, sourceHash | 65 | 80 | }; | 66 | 81 | } | 67 | 82 |
| 68 | 83 | const cacheDefinitions = []; | 69 | 84 | const cacheInitializers = []; | | 85 | +const cacheHashInitializers = []; | 70 | 86 | let totalCacheSize = 0; | 71 | 87 |
| 72 | 88 |
| @@ -79,11 +95,14 @@ for (const key of cachableBuiltins) { | 79 | 95 |
| 80 | 96 | const length = cachedData.length; | 81 | 97 | totalCacheSize += length; | 82 | | - const { definition, initializer } = getInitalizer(key, cachedData); | | 98 | + const { | | 99 | + definition, initializer, hashIntializer, sourceHash | | 100 | + } = getInitalizer(key, cachedData); | 83 | 101 | cacheDefinitions.push(definition); | 84 | 102 | cacheInitializers.push(initializer); | | 103 | + cacheHashInitializers.push(hashIntializer); | 85 | 104 | console.log(`Generated cache for '${key}', size = ${formatSize(length)}` + | 86 | | - `, total = ${formatSize(totalCacheSize)}`); | | 105 | + `, hash = ${sourceHash}, total = ${formatSize(totalCacheSize)}`); | 87 | 106 | } | 88 | 107 |
| 89 | 108 | const result = `#include "node.h" | @@ -106,6 +125,13 @@ void DefineCodeCache(Environment* env, v8::Local<v8::Object> target) { | 106 | 125 | ${cacheInitializers.join('\n')} | 107 | 126 | } | 108 | 127 |
| | 128 | +// The target here will be returned as \`internalBinding('code_cache_hash')\` | | 129 | +void DefineCodeCacheHash(Environment* env, v8::Local<v8::Object> target) { | | 130 | + v8::Isolate* isolate = env->isolate(); | | 131 | + v8::Local<v8::Context> context = env->context(); | | 132 | + ${cacheHashInitializers.join('\n')} | | 133 | +} | | 134 | +
| 109 | 135 | } // namespace node | 110 | 136 | `; | 111 | 137 |
| |
Collapse file Copy file name to clipboardExpand all lines: tools/js2c.py +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 35 | 35 | import re | 36 | 36 | import sys | 37 | 37 | import string | | 38 | +import hashlib | 38 | 39 |
| 39 | 40 |
| 40 | 41 | def ToCArray(elements, step=10): | @@ -205,6 +206,10 @@ def ReadMacros(lines): | 205 | 206 | {initializers} | 206 | 207 | }} | 207 | 208 |
| | 209 | +void DefineJavaScriptHash(Environment* env, v8::Local<v8::Object> target) {{ | | 210 | + {hash_initializers} | | 211 | +}} | | 212 | +
| 208 | 213 | }} // namespace node | 209 | 214 | """ | 210 | 215 |
| @@ -240,6 +245,12 @@ def ReadMacros(lines): | 240 | 245 | {value}.ToStringChecked(env->isolate())).FromJust()); | 241 | 246 | """ | 242 | 247 |
| | 248 | +HASH_INITIALIZER = """\ | | 249 | +CHECK(target->Set(env->context(), | | 250 | + FIXED_ONE_BYTE_STRING(env->isolate(), "{key}"), | | 251 | + FIXED_ONE_BYTE_STRING(env->isolate(), "{value}")).FromJust()); | | 252 | +""" | | 253 | + | 243 | 254 | DEPRECATED_DEPS = """\ | 244 | 255 | 'use strict'; | 245 | 256 | process.emitWarning( | @@ -280,6 +291,7 @@ def JS2C(source, target): | 280 | 291 | # Build source code lines | 281 | 292 | definitions = [] | 282 | 293 | initializers = [] | | 294 | + hash_initializers = []; | 283 | 295 |
| 284 | 296 | for name in modules: | 285 | 297 | lines = ReadFile(str(name)) | @@ -309,10 +321,12 @@ def JS2C(source, target): | 309 | 321 | var = name.replace('-', '_').replace('/', '_') | 310 | 322 | key = '%s_key' % var | 311 | 323 | value = '%s_value' % var | | 324 | + hash_value = hashlib.sha256(lines).hexdigest() | 312 | 325 |
| 313 | 326 | definitions.append(Render(key, name)) | 314 | 327 | definitions.append(Render(value, lines)) | 315 | 328 | initializers.append(INITIALIZER.format(key=key, value=value)) | | 329 | + hash_initializers.append(HASH_INITIALIZER.format(key=name, value=hash_value)) | 316 | 330 |
| 317 | 331 | if deprecated_deps is not None: | 318 | 332 | name = '/'.join(deprecated_deps) | @@ -324,11 +338,13 @@ def JS2C(source, target): | 324 | 338 | definitions.append(Render(key, name)) | 325 | 339 | definitions.append(Render(value, DEPRECATED_DEPS.format(module=name))) | 326 | 340 | initializers.append(INITIALIZER.format(key=key, value=value)) | | 341 | + hash_initializers.append(HASH_INITIALIZER.format(key=name, value=hash_value)) | 327 | 342 |
| 328 | 343 | # Emit result | 329 | 344 | output = open(str(target[0]), "w") | 330 | 345 | output.write(TEMPLATE.format(definitions=''.join(definitions), | 331 | | - initializers=''.join(initializers))) | | 346 | + initializers=''.join(initializers), | | 347 | + hash_initializers=''.join(hash_initializers))) | 332 | 348 | output.close() | 333 | 349 |
| 334 | 350 | def main(): | |
|