Codemesh Runtime v3 C++ API Reference  3.9.205
Security

Overview

In general, an integrated Java/C++ solution is as secure as a pure C++ or pure Java solution. Some problems can arise if you're going to use Java platform security with your C++ application. In general, you will do this by configuring a security manager via the xmog_jvm_options::setSecurityManager(const char*) method and by configuring a security policy via the xmog_jvm_options::setSecurityPolicy(const char*) method.
Documenting how these two settings work would go too far, please consult the Java documentation for more details.

One problem that you encounter in a native application after you have configured a security manager is that the runtime will not initialize correctly when you load the JVM, at least it wouldn't unless you or we take special steps.
In order to make the Java/C++ interaction work transparently to a native developer, the
native runtime has to perform some operations that require privileges that are usually not granted by most security policies. To get things working, you can either explicitly grant the required privileges or you can use Codemesh's delegating security manager.

If you choose to go with Codemesh's delegating security manager (the default), it is installed as the system security manager in place of the one that you configured.
Codemesh's security manager will grant the required privileges and delegate all other checks to your configured security manager. What are the security consequences?

On the plus side, this is much easier than trying to figure out, which privileges are required by the runtime and then having your application broken by the next version of our runtime because it requries an additional privilege.

There are of course run-modes that leave you entirely unprotected or with the above mentioned problems. If you choose to allow a pre-loaded JVM (for example because you're also using raw JNI in other places and can't be sure who gets to load the JVM), you might be running without a security manager or with an incorrectly configured one. If our runtime cannot initialize Java, you're on your own in getting things configured correctly.

An Example

So how do you configure Java security? You will almost invariably use the following snippet:

loader.setSecurityManager( "java.lang.SecurityManager" );
loader.setSecurityPolicy( "./app.policy" );
// the following line is redundant because this is the default
loader.setUseCodemeshSecurityManager( true );

The concrete privilege grants reside in the policy file, for example:

grant
{
permission java.io.FilePermission "c:\\temp\\JC2.1\\XMOG\\staging\\examples-c++\\-", "read";
}

The above snippet will grant the application read privileges to all files in the specified directory and all its subdirectories. Incendentially, this is one of the permissions that you will have to grant your application codebase to be able to load classes from disk.

Runtime Privileges granted by the Codemesh security manager

grant
{
permission java.util.PropertyPermission "java.protocol.handler.pkgs", "read,write";
permission java.util.PropertyPermission "networkaddress.cache.ttl", "read";
permission java.util.PropertyPermission "networkaddress.cache.negative.ttl", "read";
permission java.util.PropertyPermission "sun.net.inetaddr.ttl", "read";
permission java.util.PropertyPermission "user.dir", "read";
permission java.security.SecurityPermission "getProperty.networkaddress.cache.ttl";
permission java.security.SecurityPermission "getProperty.networkaddress.cache.negative.ttl";
permission java.lang.RuntimePermission "getClassLoader";
permission java.lang.RuntimePermission "setContextClassLoader";
};
xmog_jvm_options::setSecurityManager
virtual void setSecurityManager(const char *sm)
Sets the class that is the security manager for the application.
xmog_jvm_loader
The class that acts as a factory for xmog_jvm_loaders and thereby for our JVM abstraction.
Definition: xmog_jvm_loader.h:325
xmog_jvm_options::setSecurityPolicy
virtual void setSecurityPolicy(const char *policyFile)
Sets the policy file containing security settings for the application.
xmog_jvm_loader::get_jvm_loader
static xmog_jvm_loader & get_jvm_loader(bool bEnvOverrides=true, bool bDefaultJvm=true, xmog_trace_facility fac=TraceAll, xmog_trace_level trace_level=TraceErrors)
The "default" factory method that creates the JVM loader instance that is to be used by the proxy cla...

Copyright (c) 1999-2020 by Codemesh, Inc., ALL RIGHTS RESERVED.