Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could WASM be used to check integrity of a JS method?

I'm experimenting with webAssembly and trying to figure out a way of validating the integrity of a JS method used by the webAssembly module.

For the purpose of the discussion, let's assume that the binary module is not hackable (I know it is not the case), but the JS side is.

Given the following C code:

#include <emscripten.h>

//js method to validate
void validateMe();

int validateMethods(){
    // check validateMe integrity.
    // return 1 if validation succeeded.
}

EMSCRIPTEN_KEEPALIVE
void doStuff(){
    if (validateMethods()){
       // do stuff
    }
}

I would like to call doStuff() from the JS side, and doStuff() will operate only if the integrity check succeeded. I thought of doing some sort of integrity check, similar to Subresource, checking the toString representation of the method. However, If I want to get the current (in-memory) JS method toString, I'll have to call JS which can be compromised.

Q: Can I somehow get the toString in a different way? Any other approach would also be appreciated.

Update: after digging a bit deeper, reading this article, it seems that there is no way to access the JS memory other than the shared array. So any validation technic would be appreciated.


Update 2 (Goal): My ultimate goal is to make sure the WASM part will only work with a specific JS, or at least make it harder to interact with manipulated JS.

Fiddle Example: The following fiddle, is a naive function validation, comparing the toString of the function char by char. If you alter the validateMe function, the validation fails. I'm trying to "bulletproof" it.

like image 321
Shlomi Schwartz Avatar asked May 03 '18 07:05

Shlomi Schwartz


People also ask

Does Wasm use JavaScript?

With around 40 languages that can compile to WebAssembly, developers can finally use their favorite language on the Web. WebAssembly does not replace JavaScript; in fact, some JavaScript code is required to load WebAssembly modules. WebAssembly runs in all major browsers and in all platforms.

How does WebAssembly work with JavaScript?

WebAssembly modules can be imported into a web (or Node. js) app, exposing WebAssembly functions for use via JavaScript. JavaScript frameworks could make use of WebAssembly to confer massive performance advantages and new features while still making functionality easily available to web developers.

Is WebAssembly more secure than JavaScript?

Furthermore, WASM provides a secure approach to deploy innovation in the browser runtime quickly. While Javascript has been helpful as a dynamic language, its security has long been a source of worry. WASM developers can use Sandboxes as security measures to execute secure and high-performance applications.

What is WebAssembly used for?

WebAssembly is a binary instruction format and virtual machine that brings near-native performance to web browser applications, and allows developers to build high-speed web apps in the language of their choice.


1 Answers

JS is a dynamic language and you can override (almost) everything. You can get the body of the function as string and hash it to generate a "snapshot" of it and later on check against this snapshot, but one can override one of the inner functions independently.

var getA = function() { return 1; };

var myFunc = function() {
  var a = getA();
  return a * 2;
};

WebAssembly.instantiate(wasmBytes, { myFunc });

// ... later on ...
getA = function() { return 5; };
like image 157
Boyan Mihaylovv Avatar answered Oct 05 '22 22:10

Boyan Mihaylovv