We are going to design a runtime policy enforcement mesh for WebAssembly (Wasm) components.
The system will use capability tokens for authorization, maintain audit trails, and provide isolation guarantees.
Key components:
Capability Tokens: Represent permissions to access resources (e.g., network, files, other components).
Policy Enforcement Point (PEP): Intercepts requests from Wasm components to the host or other components.
Policy Decision Point (PDP): Evaluates policies against the capability token and the request context.
Audit Trail: Logs all access attempts (both allowed and denied) for security auditing.
Isolation Guarantees: Ensure that components cannot bypass the enforcement mesh.
We assume that each Wasm component is instantiated with a set of capability tokens (or references to tokens) that it holds.
Steps:
Instantiation:
When a Wasm component is instantiated, the host (or a trusted component) provides it with a set of capability tokens.
These tokens are stored in a way that the component can present them when making a request.
Request Interception:
The host environment (or a Wasm runtime with support for interception) must intercept all calls that could lead to side effects (e.g., host functions, imports, or inter-component calls).
We can achieve this by having the host functions wrapped by the PEP.
Policy Evaluation:
For each intercepted request, the PEP collects:
a. The capability token(s) presented by the component (if any).
b. The requested action and resource (e.g., read file /etc/passwd).
c. Additional context (e.g., component identity, time, etc.).
The PEP then queries the PDP with this information.
The PDP returns a decision (allow/deny) and possibly obligations.
Audit Logging:
Regardless of the decision, the PEP logs the request and decision to an audit trail.
The audit trail must be tamper-evident (e.g., using cryptographic hashing or secure logging to a remote system).
Isolation:
The Wasm runtime must enforce that components cannot directly access the host or other components without going through the PEP.
This can be achieved by only allowing components to call host functions that are wrapped by the PEP.
Additionally, the capability tokens must be unforgeable. We can use cryptographic signatures for tokens.
Token Management:
Tokens are issued by a trusted authority (could be the host or a token service).
Tokens are bound to a component (or a set of components) and a set of permissions.
Tokens may have expiration and revocation.
Implementation Considerations:
We need to extend the Wasm runtime (or use a runtime that supports custom host function wrapping) to intercept all relevant calls.
Alternatively, we can design a system where the component explicitly presents a token for each sensitive operation. But then we must ensure that the component cannot bypass the token check.
Proposed flow for a component making a request:
Component calls a host function (e.g., open_file).
The host function is actually a wrapper provided by the PEP.
The wrapper expects the component to provide a capability token (or uses a token that was associated with the component at instantiation).
The wrapper builds a request context and sends it to the PDP (which might be in-process or out-of-process).
If allowed, the wrapper calls the actual host function and returns the result to the component.
If denied, the wrapper returns an error (or a default value) and logs the attempt.
However, note that the component must be prevented from calling the actual host function directly. Therefore, the host must only expose the wrapper functions and not the raw ones.
For inter-component calls, we can have a similar mechanism: the component must present a token to call a function of another component. The callee's PEP (or a central PEP) checks the token.
Alternatively, we can have a single PEP that mediates all inter-component and host calls.
Given the complexity, we might design:
A central Policy Enforcement Mesh (PEM) that runs as a separate component or in the host.
All components are configured to use the PEM for any sensitive operation.
But then how do we prevent a component from bypassing the PEM? We must ensure that the only way to perform an operation is through the PEM.
This requires that the host only allows components to call functions that are provided by the PEM (and the PEM then calls the actual host functions). The PEM must be the sole mediator.
Steps for implementation:
Define an interface for the PEM that components must use for sensitive operations.
The host, when instantiating a component, only provides imports that are from the PEM (and possibly some safe, non-sensitive functions).
The PEM, when it receives a request from a component, checks the component's capability token (which it must have presented) and then decides whether to perform the operation on behalf of the component.
However, this requires that the component is written to use the PEM. Alternatively, we can have a system where the host function imports are actually provided by the PEM, but the component thinks it's calling the host directly. This is possible if the host runtime allows us to override the host functions.
In practice, with WebAssembly, we can:
At instantiation, we provide a set of host functions (the ones that the component expects). We can replace each host function with a wrapper that does the policy check and then calls the original (if allowed).
So the plan:
We have a set of original host functions (e.g., fs_open, net_connect).
We create a wrapper for each that:
a. Extracts the token from the component's memory (or from a known location) or uses the token associated with the component instance.
b. Sends a request to the PDP (which could be in-process or out-of-process) with the token and the operation details.
c. If allowed, calls the original host function and returns.
d. If denied, returns an error.
But note: the component must not be able to call the original host function directly. Therefore, we must not expose the original host function to the component. Instead, we only expose the wrapper.
How to do this in practice:
When creating the Wasm instance, we can provide a custom set of host functions (the wrappers) and keep the original host functions in a separate module that is not exposed to the component.
Alternatively, we can have a two-layer system:
Layer 1 (the component's view): The component sees host functions that are actually our wrappers.
Layer 2 (the host): The wrappers have access to the real host functions (because they are part of the host) and can call them only after policy check.
This is feasible if we are embedding the Wasm runtime in a host application that we control.
For a more generic solution, we might need to modify the Wasm runtime to support this interception.
Given the constraints, we design:
A Policy Enforcement Mesh (PEM) that is part of the host.
The host, when instantiating a Wasm module, will:
a. Create a new instance of the module with a set of host functions that are actually wrappers provided by the PEM.
b. The PEM maintains a mapping from Wasm instance to its capability tokens (or the tokens are stored in a known location in the instance's memory).
How does the component pass the token? We have two options:
Option 1: The token is passed as an extra argument to every host function. This requires modifying the component's code to include the token in every call. Not ideal.
Option 2: The token is stored in a well-known memory location (or in a global variable) of the Wasm instance. The wrapper can read it.
But then, a malicious component could change its token? We must ensure that the token is stored in a way that the component cannot tamper with it.
We can store the token in the host's metadata for the instance, not in the component's memory. The wrapper, when called, can look up the token by the instance's ID (which is known because the wrapper is called in the context of that instance).
How to get the instance ID in the wrapper? The wrapper is a host function and when the Wasm component calls it, the runtime provides the calling instance? This is runtime-specific.
Alternatively, we can have the token passed as a parameter to the host function, but then the host function must be called with the token. How does the component get the token initially?
We can provide an initialization function that the component must call at the start, which sets the token for the instance. But then we must ensure that the component cannot make sensitive calls before initialization.
Alternatively, we can have the token provided at instantiation time (as part of the instantiation arguments) and stored by the host in the instance's metadata.
Let's assume our host runtime allows us to store metadata per instance. Then:
At instantiation, the host (or the PEM) stores the token (or tokens) in the metadata for that instance.
When a host function (wrapper) is called, the runtime provides the instance (or we can get it from the current execution context). Then the wrapper retrieves the token from the metadata.
This way, the component does not need to know about the token at all. The token is managed by the host and the PEM.
Steps:
Host instantiates the Wasm module.
Host (or PEM) associates a set of capability tokens with the instance (based on the policy of who instantiated it and for what purpose).
Host provides the instance with a set of host functions that are actually wrappers from the PEM.
When the component calls a host function (wrapper):
a. The wrapper uses the current instance's ID to look up the token(s) from the host's metadata.
b. The wrapper sends a request to the PDP (which may be the same as the PEM or a separate service) with:
- The token(s)
- The operation (e.g., "open_file") and arguments (e.g., path)
- Instance ID (for audit)
c. If the PDP allows, the wrapper calls the real host function (which is only accessible to the host, not the component) and returns the result.
d. If denied, the wrapper returns an error.
This design ensures that the component cannot bypass the PEM because it only has access to the wrapper functions, and the wrapper functions are the only way to call the real host functions.
For inter-component calls:
We can have a similar mechanism. Suppose component A wants to call a function exported by component B.
How is this set up?
In Wasm, components can export functions that other components can import. But the call is direct: the caller's runtime will call the callee's function.
We want to intercept these calls as well.
We can:
When component A is instantiated, we don't give it the actual function pointer for component B's export. Instead, we give it a wrapper function (provided by the PEM) that will perform the policy check and then call the real function.
How does the PEM know which function is being called and by which component?
We can have a two-level indirection:
The PEM provides a generic "call_export" function that takes:
a. The component ID of the callee (or the export name)
b. The arguments
But then the component must use this generic function, which changes the component's code.
Alternatively, we can have the host runtime, when setting up the imports for component A, replace each imported function (which comes from component B) with a wrapper that does:
code
wrapper(args) {
token = get_token_for_current_instance(); // current instance is A
callee = get_callee_from_original_import(); // the original function from B
// Send request to PDP with token, operation (which is the callee's name or ID), and arguments.
if allowed:
call callee with args
else:
return error
}
This requires that the host runtime supports replacing the import table with wrappers. Some Wasm runtimes (like Wasmtime) allow customizing the linkage.
Given the complexity and the fact that we are designing a mesh for WebAssembly components, we assume we are using a runtime that allows us to:
Store metadata per instance.
Provide custom host functions that are called instead of the original ones.
Intercept and wrap imported functions (for inter-component calls).
If we cannot intercept inter-component calls, then we must rely on components using the PEM explicitly for cross-component calls, which is less transparent.
Given the goal of transparency, we assume the host runtime can wrap both host functions and imported functions.
Summary of the design:
Capability Tokens:
Issued by a trusted authority.
Cryptographically signed (e.g., JWT or similar) so that the PDP can verify them without contacting the issuer every time.
Contain: subject (component ID or a set of component IDs), permissions (list of allowed operations), expiration, etc.
Policy Enforcement Mesh (PEM):
Provides wrappers for every sensitive host function and every imported function (from other components).
Maintains a mapping of instance ID to its tokens (set at instantiation).
For each call, it collects:
Token(s) from the metadata (or if multiple tokens, it may try each until one allows? or combine? Usually, a token is presented and it has a set of permissions. We might allow multiple tokens, but then we need a policy for how to combine them.)
Operation (function name or ID) and arguments.
Instance ID (caller) and maybe callee (for inter-component).
Sends a request to the PDP.
Policy Decision Point (PDP):
Receives a request with token and operation context.
Verifies the token signature (if not done by PEM).
Extracts the permissions from the token.
Checks if the operation is allowed by the token's permissions and any additional policy (e.g., time of day, etc.).
Returns allow/deny.
Audit Trail:
The PEM logs every request (with instance ID, operation, arguments, decision, and timestamp) to a secure log.
The log is written in a way that prevents tampering (e.g., append-only, signed, or sent to a remote audit server).
Isolation Guarantees:
The host runtime only exposes the wrapper functions to the component. The real host functions are kept internal.
The component cannot obtain the real function pointers because they are not in its import table.
The tokens are stored in host metadata and cannot be accessed by the component.
Implementation Steps:
We'll outline the steps for a host that uses Wasmtime (as an example) to illustrate:
Define the set of sensitive host functions and the operations they represent.
When instantiating a Wasm module:
a. Create a new instance of the module with a custom Linker that only provides the wrapper functions.
b. Store the capability tokens for this instance in a HashMap<Instance, Vec<Token>> (or similar) in the host.
c. For each import that the module expects, if it is a sensitive function, we provide a wrapper. For non-sensitive, we can provide the original function (if safe) or also wrap for audit?
The wrapper function:
fn wrapper(instance: &Instance, args: ...) -> Result<..., Trap> {
let tokens = host_metadata.get_tokens(instance);
let operation = get_operation_name_from_wrapper(); // e.g., "fs_open"
let context = RequestContext {
caller: instance.id(),
operation: operation,
arguments: args,
tokens: tokens,
// maybe more context
};
let decision = pdp.evaluate(&context);
audit_log(&context, decision);
match decision {
Allow => {
// call the real host function (which we have stored somewhere) with the same args
real_host_function(args)
}
Deny => {
// return an appropriate error (e.g., permission denied)
Err(Trap::new("permission denied"))
}
}
}
For inter-component calls:
We need to wrap the imported functions as well. When we instantiate component A, we set up its imports. If one of the imports comes from component B, we don't give it the actual function from B. Instead, we give it a wrapper that:
a. Knows the callee (component B's function) and the caller (component A's instance).
b. Does a similar policy check, but now the operation is the callee's function name and the callee's ID.
How do we know which function is being called? We can create a wrapper for each imported function that is specific to that function. So when we add the import for component_b::foo, we provide a wrapper that knows it is for foo.
The PDP:
We need a way to define policies. The tokens contain permissions, but we might have additional policies (like time-based). The PDP can be a separate service or in-process.
We can start with a simple PDP that just checks the token's permission list for the required operation.
The PDP checks:
- token is valid (signature, expiration)
- the required operation (e.g., "fs_open") is in the perms list.
We can also support wildcards or more complex policies (e.g., only allow fs_open with path starting with /data).
The token might also include constraints (like allowed paths) and the PDP checks those.
For simplicity, we assume the token contains a list of allowed operations and maybe some constraints.
Challenges:
Performance: Every sensitive call goes through the PEM and PDP. We might cache decisions (with care for revocation) or have the PDP in-process for low latency.
Token revocation: If a token is revoked, we must ensure that the PDP knows about it. We can have a short token lifetime and require re-authentication, or have the PDP check a revocation list (which might be updated asynchronously).
Complexity of wrapping all functions: We must identify all sensitive functions and wrap them. This is error-prone.
Alternative approach:
Instead of wrapping every function, we can have the component called through a single entry point (like a syscall interface) and then the PEM dispatches. But that requires the component to use a single function for all operations, which is not standard.
Given the constraints of WebAssembly, the wrapper approach is more feasible.
Conclusion:
We have designed a runtime policy enforcement mesh that uses capability tokens, a PEP (the wrappers), a PDP, audit logging, and isolation by wrapping host and imported functions.
The next steps would be to implement this in a specific Wasm runtime (like Wasmtime) and test with example components.
I'll design a comprehensive runtime policy enforcement mesh for WebAssembly components. This system combines capability-based security with mandatory access control and continuous auditing.
// Wasmtime embedding example
struct SecureWasmtime {
engine: Engine,
linker: Linker,
pem: PolicyEnforcementPoint,
isolation: IsolationEnforcer,
store: Store<SecureContext>,
}
impl SecureWasmtime {
fn instantiate_with_policy(
&mut self,
module: &Module,
tokens: Vec<CapabilityToken>,
policy_config: PolicyConfig
) -> Result<Instance, Error> {
// 1. Validate tokens against policy
for token in &tokens {
self.pem.validate_token(token)?;
}
// 2. Create instance with secure context
let mut store = Store::new(
&self.engine,
SecureContext {
component_id: generate_component_id(),
capabilities: tokens,
isolation_state: IsolationState::default(),
audit_handle: self.audit_logger.handle(),
}
);
// 3. Wrap all host functions
let mut linker = Linker::new(&self.engine);
for (name, _) in module.imports() {
if let Some(host_func) = self.get_host_function(name) {
let wrapped = self.pem.wrap_host_function(host_func);
linker.func_wrap(name, wrapped)?;
}
}
// 4. Instantiate
let instance = linker.instantiate_async(&mut store, module).await?;
// 5. Register with isolation enforcer
let component_id = store.data().component_id.clone();
self.isolation.register_component(component_id, policy_config.isolation);
Ok(instance)
}
}
Performance Optimizations
rust
// Caching strategy
struct SmartCache {
// Multi-level caching:
// 1. Component-local (fastest)
// 2. Node-local (shared between components on same host)
// 3. Distributed (Redis/etcd for cluster-wide)
local_cache: LruCache<CacheKey, CacheEntry>,
node_cache: RwLock<HashMap<CacheKey, CacheEntry>>,
distributed_cache: Option<RedisCache>,
// Cache invalidation on:
// - Token revocation
// - Policy changes
// - Time-based TTL
// - Component restart
}
// Batch evaluation for startup
impl PolicyEnforcementPoint {
async fn batch_validate_startup(
&self,
components: Vec<(ComponentId, Vec<CapabilityToken>)>
) -> HashMap<ComponentId, StartupValidation> {
// Validate all tokens in parallel
let futures = components.iter().map(|(id, tokens)| {
async move {
let mut valid = true;
let mut errors = Vec::new();
for token in tokens {
if let Err(e) = self.pdp.validate_token(token).await {
valid = false;
errors.push(e);
}
}
(*id, StartupValidation { valid, errors })
}
});
futures::future::join_all(futures)
.await
.into_iter()
.collect()
}
}
6. Security Properties
Formal Guarantees
rust
// Using verification conditions
trait SecurityGuarantees {
// 1. Non-bypassability: All host accesses go through PEP
fn verify_no_bypass(&self) -> bool;
// 2. Confinement: Components cannot exceed their capabilities
fn verify_confinement(&self, component: &ComponentId) -> bool;
// 3. Least privilege: Components have minimal necessary rights
fn verify_least_privilege(&self) -> bool;
// 4. Audit completeness: All access attempts logged
fn verify_audit_completeness(&self) -> bool;
}
// Proof sketch:
// By induction on execution steps:
// Base: Initial state - all components have only issued tokens
// Induction: If all host calls go through PEP, and PEP consults PDP,
// and PDP only allows what tokens permit, then no unauthorized access.
Quantum resistance - Post-quantum crypto for long-lived tokens
Multi-tenancy - Tenant isolation in shared clusters
This design provides a robust foundation for enterprise-grade WebAssembly security, balancing strong guarantees with practical performance considerations. The mesh approach ensures that security policies are uniformly enforced across all components regardless of their origin or trust level.
The PDP evaluates the request against the token and returns allow/deny.