Google has released a new experimental browser plugin that allows web applications to securely run native code on the underlying platform. The plugin, which is called Native Client, is distributed under the open source BSD license and is designed to work with all major platforms and browsers.

Native Client provides a sandboxed web-embeddable runtime environment for portable x86 binaries. It also provides a bridge to facilitate communication between JavaScript and Native Client executables. This makes it possible for complex web applications to seamlessly leverage native code for processor-intensive computations.

Allowing web applications to run native code has traditionally posed significant security risks. Microsoft's COM-based ActiveX technology, which aimed to provide developers with similar capabilities, is widely viewed as one of the most egregious security failings of the Windows operating system and it has become a frequent attack vector for malicious code.

Google believes that its security model has the potential to be far more robust and effective than the code-signing system of trust used by ActiveX. Google's engineers explain the differences between the Native Client and ActiveX security models in a paper about the project:

"[ActiveX's] dependency on the user making prudent trust decisions is commonly exploited. ActiveX provides no guarantee that a trusted control is safe, and even when the control itself is not inherently malicious, defects in the control can be exploited, often permitting execution of arbitrary code," the paper explains. "In contrast, NaCl is designed to prevent such exploitation, even for flawed NaCl modules."

The Native Client framework consists of trusted and untrusted modules that run in their own individual processes and communicate through an RPC system. Third-party untrusted modules contain application-specific programming and leverage the Native Client container to interact with trusted modules that perform any activities which impact the underlying platform, including networking operations and file system access. The container will impose security constraints on the behavior of the third-party code and will also give users granular control over the permissions granted to third-party code.

Google supplies a customized GCC build toolchain that can be used to compile portable binaries that are compatible with the Native Client infrastructure. This makes it extremely easy to port existing code. As a test, Google ported an H.264 encoder that is about 11,000 lines of conventional C code. To get the encoder to compile and run in Native Client, they only had to add approximately 20 lines of code and modify the Makefile. The resulting binaries are completely portable and can run without requiring recompilation on any operating system supported by the Native Client.

"Legacy Linux libraries that don't inherently require network and disk generally port to NaCl with minimal effort," Google's paper says. "Performance of the original and NaCl versions [of the H.264 encoder] were comparable and limited by video frame-rate."

Google provides several examples to demonstrate how easy it is to adapt a program so that it can be used with the Native Client system. The simplest example is a little Hello World application that demonstrates how to make a native function that is callable through JavaScript.

#include <stdio.h> #include <string.h> #include <nacl/nacl_srpc.h> /* Return a clever string. */ int HelloWorld(NaClAppArg **in_args, NaClAppArg **out_args) { /* Strdup must be used because the SRPC layer frees the string passed to it. */ out_args[0]->u.sval = strdup("hello, world."); return RPC_OK; } /* Export the method as taking no arguments and returning one integer. */ NACL_SRPC_METHOD("helloworld::s", HelloWorld);

To use this function in a web page, the C file must be compiled into a Native Client executable, which is then embedded in the web page and accessed through JavaScript:

<embed id="pluginobj" type="application/x-nacl-srpc" width="0" height="0" xsrc="srpc_hw.nexe" mce_src="srpc_hw.nexe" />

// helloworld is invoked when its button is pressed. function helloworld() { try { alert(document.getElementById('pluginobj').helloworld()); } catch(e) { alert(e); } }

One of the most impressive examples provided by Google is a patch for the SDL version of Quake, the classic first person shooter. I downloaded the source code and data files from the ID web site and compiled it with the Native Client build tools after applying Google's patch. I tested it extensively (playing Quake is a tough job, but somebody has to do it!) and was pleased with the performance.

Although running a computer game in the browser (where have we seen that before?) may not be particularly thrilling, Native Client opens up a lot of really exciting possibilities for building rich native/web hybrid applications. Along with much-needed improvements in JavaScript performance, support for bridging the gap between JavaScript and native code could really transform the web.

Google has made this early technical demo available with the hope that public exposure will help harden the code. The company has urged security experts and independent developers to rough up the Native Client security model and help find way to improve it so that it can be as safe and secure as possible when it is ready to be deployed to end users.

Further reading