This document describes how to add a security manager to a multi-sandboxed platform. The security manager will allow access to a kernel LED native function to one feature and forbid access to a second feature.
- Check the permission before the native call.
- Add security manager to the kernel.
Important notice, In order to ease the reader’s understanding, the project from this HowTo is released in the final, working state one should obtain when following the instructions below.
This tutorial is built upon the Single-App-to-Multi-App-Platform platform. This platform must:
- Implement LEDs native function in the kernel
- Load at least two features
Import the example projects into the MicroEJ SDK:
Multi-App-Security-Managercontains this READMEHelloWorldis a feature named “Hello” application using the LEDHelloWorld2is the same feature asHelloWorldwith a different kf name “Hello2”NativeAPIsis a project that defines the native functions to manage the LEDKernelis the kernel entry point and function to load the features
- Create an
LedPermission
class that extends
java.security.BasicPermission - In Led, check the
permission before calling
switchLedNative(boolean on);
/**
* Switch the led state.
*
* @param on
* <code>true</code> to switch on the led.
* @throws SecurityException
* when the SecurityManager does not give {@link LedPermission}.
*/
public static void switchLed(boolean on) throws SecurityException {
SecurityManager securityManager = System.getSecurityManager();
// SecurityManager may be null if the Kernel has not yet set it.
if (securityManager != null) {
securityManager.checkPermission(LedPermission.INSTANCE);
}
switchLedNative(on);
}
A simple security manager which does not check permissions when called in Kernel mode and performs the checks otherwise.
- Create
com.microej.kernel.security.KernelSecurityManager
that extends
java.lang.SecurityManager. - Create an interface
com.microej.kernel.security.FeaturePermissionChecker
that defines
public void checkPermission(Permission p, Feature f) throws SecurityException; - In
com.microej.kernel.security.KernelSecurityManager
overrides
public void checkPermission(Permission permission)to delegate the check.
@Override
public void checkPermission(Permission permission) {
if(!Kernel.isInKernelMode()){
// We are not called in kernel mode, so perform the security checks.
Feature feature = (Feature)Kernel.getContextOwner();
Kernel.enter();
FeaturePermissionChecker checker = permissionsMap.get(permission.getClass());
if(checker != null){
checker.checkPermission(permission, feature);
} else {
noCheckerFound(permission, feature);
}
} else {
// We are called in kernel mode, so allow the operation always.
kernelPermission(permission);
}
}
- Create
com.microej.kernel.security.LedPermissionChecker
that implements
com.microej.kernel.security.FeaturePermissionChecker.- The permission will only be granted to the features with the name “Hello”
@Override
public void checkPermission(Permission p, Feature f) throws SecurityException {
String name = f.getName();
if (name == null || !name.equals("Hello")) {
throw new SecurityException();
}
}
Modify the Kernel entry point com.microej.kernel.SimpleKernel.main(String[]) to instantiate the SecurityManager.
KernelSecurityManager securityManager = new KernelSecurityManager(); securityManager.setFeaturePermissionChecker(LedPermission.class, new LedPermissionChecker()); System.setSecurityManager(securityManager);
- In the MicroEJ SDK, generate the microejapp.o file
- Right-click on the
Kernelproject - Select Run-As -> Run Configuration
- Right-click on MicroEJ Application
- Select New
- In Execution tab
- Set your platform
- Check Execute on device
- Set Settings to Build and deploy
- Run
- In the BSP folder the
microejapp.ofile is generated
- Right-click on the
- Use a third party C IDE to build the BSP and flash your board
In the MicroEJ SDK, generate
application_1.foRight-click on the HelloWorld project
Select Run-As -> Run Configuration
Right-click on MicroEJ Application
Select New
In Execution tab
In Configuration tab
Run
In the output folder (by default generated at the root of
HelloWorldproject) anapplication_1.fofile is generated
Generate
application_2.fo- Follow the same steps as 1. with
HelloWorld2and using the nameapplication_2.fo
- Follow the same steps as 1. with
Copy the
application_1.foandapplication_2.fofeature files to atmp/folder at the root of the microSD card.Insert the SD card into the board
Reset the board
The firmware should run the 2 features
HelloWorldshould make the LED blink- A
SecurityExceptionshould be raised inHelloWorld2
- Other
FeaturePermissionCheckercan be added to theKernelSecurityManagerfor other type of Permission.- java.net.SocketPermission
- java.io.FilePermission
- …
Keil strips the debug info from the binary file, a tool is provided in the platform to add them. To generate the object file with the info, follow this steps:
- Run -> Run configuration
- Create a new MicroEJ Tool configuration
- In Execution tab
- Select your platform
- Select the tool Soar debug infos post-linker
- In Configuration tab
- Set the SOAR object file to the .o file generated in the
{kernel project output folder}/soar/ - Set the executable file to the
.axfobject file generated by Keil
- Set the SOAR object file to the .o file generated in the
- Run
- Update the Hello World launch configuration to use the generated file
standalone.axf.soarDebugInfosfor the kernel


