After implementing my platform to have the ability to load jar files dynamically using reflection, I come across a security issue. The jar file could be 'dangerous' and contain code that could effect things, therefore I come across on SO the use of using a SecurityManager.
I am loading the jar file like this (example of hard coded file):
URLClassLoader clsLoader = URLClassLoader
.newInstance(new URL[] { new URL("file:/C://Temp/SettingsApp.jar") });
Class<?> cls = clsLoader.loadClass("iezon.app.SettingsApp");
JPanel settingsApp = (JPanel) cls.newInstance();
I have tried to use the SecurityManager like so:
try {
SecurityManager sm = new SecurityManager();
Object context = sm.getSecurityContext();
sm.checkPermission(new AllPermission(), context);
settingsAppPanel = (JPanel) cls.newInstance();
} catch (AccessControlException e) {
e.printStackTrace();
}
The test with AllPermission() works and gives me this exception:
java.security.AccessControlException: access denied ("java.security.AllPermission" "" "")
However, I am unable to find any information on how to bind the jar file to this security manager and not the current platform. I just don't want the jar file to be able to open sockets, etc.. without firstly requesting permissions that the user can then accept.
I stumbled across BasicPermission, however, this is an abstract class and cannot be instanced like the AllPermission and likewise, I am unable to find any information on approaching this. Any help would be appreciated!
As #Holger mentioned in the comments, the only way to implement using a Security Manager is to dynamically create policies. Therefore, when a developer uploads a new app to the appstore, he is given a set of policies to declare that his app uses. On the app, this is then fed to the user asking them to confirm this policy (at their own risk if it involves sockets etc...) and then a policy is created and the app can run:
if (!app.policyIsAgreedTo()) {
if(readPolicy(app).get(0).equalsIgnoreCase("grant {")) {
app.setPolicy(true);
} else {
throw new Exception("App policy has not been agreed to.");
}
}
if (app.policyIsAgreedTo()) {
System.setProperty("java.security.policy", "file:/C:/Temp/" + app.getName() + ".policy");
System.setSecurityManager(new SecurityManager());
JPanel panel = (JPanel) app.getObject().newInstance();
System.setSecurityManager(null);
return panel;
}
throw new Exception("App policy has not been agreed to.");
The policy gets created like so:
public static void createPolicy(ArrayList<String> policies, App app) {
try {
FileWriter fileWriter =
new FileWriter("C:/Temp/" + app.getName() + ".policy");
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
bufferedWriter.write("grant {");
bufferedWriter.newLine();
for(String policy : policies) {
bufferedWriter.write(" permission java.lang.RuntimePermission \"" + policy + "\";");
bufferedWriter.newLine();
}
bufferedWriter.write("};");
bufferedWriter.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
The app makes an API call to the app store gathering the permissions the app needs, then a simple createPolicy() is called with the permissions in an ArrayList ie:
new ArrayList<String>() {{ add("setSecurityManager"); }}; // Etc...
Related
Please let me know if this question has been asked before.
The Goal
In my android application when a user launches the App it loads the Login.class first. This class checks to see if a local file (in the included file path of the App) called app_prefs.prop exists (which it does) and then it checks the following fields structured like so:
username=
user_hash=
saved_email=
email_hash=
Those fields are blank by default just like so. I am reading them using the following code:
public static String getConfigValue(Context context, String name) {
Resources resources = context.getResources();
String TAG = "Retrieve";
try {
InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
Properties properties = new Properties();
properties.load(rawResource);
return properties.getProperty(name);
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to find the config file: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "Failed to open config file.");
}
return null;
}
If they return empty values, which by default they will, then the login screen is showed. If they do not, the login is attempted by default and if successful it will continue to the App, if not, login is shown again of course.
The Issue
When they sign in, I want to write the data into those fields. Currently its being sent to and from the server using JSON and works awesome. I am able to extract this data as well to a string variable which I am then passing to my save to config file after logging the user in but before continuing to the next App screen. This is where the problem lies, I have enabled the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have also passed all the values to it, but they are not being written to the lines I want them too. Here is the code I am using to write the file with:
private void commitUserInfotoFile(Context context, String username, String passhash, String rmemail, String rmemailhash) {
Resources resources = context.getResources();
String TAG = "Store";
try {
InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
Properties properties = new Properties();
properties.load(rawResource);
//tried using setProperty as well as put but neither works
properties.setProperty("username", username);
properties.put("username", username);
properties.setProperty("user_hash", passhash);
properties.setProperty("saved_email", rmemail);
properties.setProperty("email_hash", rmemailhash);
Log.e(TAG, "Wrote the values to the stored file");
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to find the config file: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IO Exception loading file.");
}
}
Yet its not storing those values to the file even tho I get the message Wrote the values to the stored file in my console. I am a little confused as to the writing of properties using this method so any help would be appreciated. Thanks
You never store the result of your edits back into the resource. setProperty() just updates some internal key-value pair in the Properties object, it does not update it's source. You need to call Properties.store(OutputStream, String) when you are done with your edits. See here:
https://developer.android.com/reference/java/util/Properties.html#store(java.io.OutputStream, java.lang.String)
I have a simple plugin system, that loads external JAR plugins into main application. I am using Mountainblade Modular to do so. Not sure how they do it "under the hood" but probably it's something standard.
This works fine, I instantiate classes from external jar and it all works. Except that some plugins come with icons/images. I am a bit unsure on how do I load/refer to images from that external JAR (with code inside that external JAR, as it is ran in context of the main JAR, kind of)
How should I approach this?
This issue is not as straightforward as it seems to be.
When you load classes from external jar, they are "loaded" into JVM. By "loading" into JVM I mean that JVM is responsible for their storage in memory. Usually it is done like this:
ClassLoader myClassLoader = new MyClassLoader(jarFileContent);
Class myExtClass = myClassLoader.loadClass(myClassName);
Resources from classpath jars can be accessed easily with
InputStream resourceStream = myClass.getResourceAsStream("/myFile.txt");
You can do that, because these jars are in classpath, I mean their location is known. These files are not stored in memory. When resource is accessed, JVM can search for it in classpath jars (for example on file system).
But for external jars it is completely different: jar comes from nowhere, is once processed and forgotten. JVM does not load resources from it in memory. In order to access these files, you have to manually organize their storage. I've done this once so I can share the code. It will help you to understand the basic idea (but probably won't help you with your specific library).
// Method from custom UrlClassLoader class.
// jarContent here is byte array of loaded jar file.
// important notes:
// resources can be accesed only with this custom class loader
// resource content is provided with the help of custom URLStreamHandler
#Override
protected URL findResource(String name) {
JarInputStream jarInputStream;
try {
jarInputStream = new JarInputStream(new ByteArrayInputStream(jarContent));
JarEntry jarEntry;
while (true) {
jarEntry = jarInputStream.getNextJarEntry();
if (jarEntry == null) {
break;
}
if (name.equals(jarEntry.getName())) {
final byte[] bytes = IOUtils.toByteArray(jarInputStream);
return new URL(null, "in-memory-bytes", new URLStreamHandler() {
#Override
protected URLConnection openConnection(URL u) throws IOException {
return new URLConnection(u) {
#Override
public void connect() throws IOException {
// nothing to do here
}
#Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
};
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I'm quite confused on how to properly setup a logger in an Java EE OSGi environment. Here are my requirements:
Only 1 log file per EBA (grouping of bundles)
Multiple log files per application server (due to multiple EBAs)
Do not want to perform ClassLoader magic (its fine if a library I use does this, I just don't want to have to write it)
Must rotate the log file at the end of the day and only maintain 7 log files at once
Preferably does not require creating a logging bundle per EBA. No other developer will buy in if I tell them to write their own logging interop for each application.
Must work with WebSphere Application Server 8.5.5
First I tried to use SLF4j on top of log4j like our other Java EE applications do, however nothing could find my log4j.properties. I tried variations of importing SLF4j, and also had issues where loading it in 1 bundle prevented it from loading in another.
Next I looked into PAX logger, but it appears to log globally, not per EBA.
Attempting to use the OSGi LogService prevents my bundle from deploying to WebSphere, plus I'm not sure how I could get it to meet my requirements anyway.
I'm at the point where the only option I can see is to write my own bundle that keeps a registry for bundle -> log file (using FrameworkUtil.getBundle on the client class) and implement a full logging framework within it. If that has classloader isolation issues then possibly push to an EJB to do the actual logging. I'm really hoping that's not my only solution.
Can anyone point me to some documentation that will help me out??
Thank you!
People are in general confused about the Log Service ... The Log Service does not store any logs, it just acts as a dispatcher. I understand the confusion since the Log Service is mandated to have a small buffer for the initial start up and provides an API to get the buffer.
For what you want, you should add a Log Listener with the Log Reader services. What you want is quite easy with Declarative Services. This is a component that implements your requirements :
#Component(provide = {}, immediate = true) public class Logger extends Thread
implements LogListener {
final BlockingQueue<LogEntry> queue = new ArrayBlockingQueue<LogEntry>(1000);
File root;
#Reference void setLR(LogReaderService lr) {
lr.addLogListener(this);
}
#Activate void activate(BundleContext context, Map<String,Object> props) {
if ( props.containsKey("root"))
root = new File((String) props.get("root"));
else
root = context.getDataFile("logs");
root.mkdirs();
start();
}
#Deactivate void deactivate() {
interrupt();
}
#Override public void logged(LogEntry entry) {
queue.offer(entry); // ignore full silently
}
public void run() {
while (true)
try {
LogEntry entry = queue.take();
File file = getPath(entry);
if (file.isFile()) {
long days = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis()
- file.lastModified());
if (days > 2) file.delete();
}
try (OutputStream raf = new FileOutputStream(file, true)) {
String s = String.format("%tT [%03d] %s%n", entry.getTime(), entry
.getBundle().getBundleId(), entry.getMessage());
raf.write(s.getBytes("UTF-8"));
}
} catch (InterruptedException ie) {
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private File getPath(LogEntry entry) {
long rollover = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis()) % 7;
String eba = "eba"; // fill in eba from entry.getBundle()?
return new File(root, "log-" + eba + "-" + rollover + ".msg");
}
}
This could of course be done a bit more efficient, but that is left as an exercise.
First off, I want to specify that I do have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
specified in my manifest, and I do check Environment.MEDIA_MOUNTED.
The really strange thing about this, in my opinion, is that it returns true, but it doesn't actually create the directories.
public static void downloadFiles(ArrayList<FileList> list) {
for (FileList file: list) {
try {
// This will be the download directory
File download = new File(downloadDirPatch.getCanonicalPath(), file.getPath());
// downloadDirPatch is defined as follows in a different class:
//
// private static String updateDir = "CognitionUpdate";
// private static File sdcard = Environment.getExternalStorageDirectory();
// final public static File downloadDir = new File(sdcard, updateDir);
// final public static File downloadDirPatch = new File(downloadDir, "patch");
// final public static File downloadDirFile = new File(downloadDir, "file");
if (DEV_MODE)
Log.i(TAG, "Download file: " + download.getCanonicalPath());
// Check if the directory already exists or not
if (!download.exists())
// The directory doesn't exist, so attempt to create it
if (download.mkdirs()) {
// Directory created successfully
Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
} else {
throw new ExternalStorageSetupFailedException("Download sub-directories could not be created");
}
else {
// Directory already exists
Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} catch (ExternalStorageSetupFailedException essfe) {
essfe.printStackTrace();
}
}
}
"if (download.mkdirs())" returns true, but when the app goes to actually download the file it throws a
FileNotFoundException: open failed: ENOENT (No such file or directory)
exception, and when I check for the directory afterwards on my phone, it doesn't exist.
Earlier in the program, the app sets up the parent download directory, and that all works fine using File.mkdir(), but File.mkdirs() doesn't seem to be working properly for me.
Your question does not give much detail about the FileNotFoundException. Check the path that triggers this. Forget what you think the path is, log it or run it through the debugger to see what it really is.
As per the directories not created correctly, verify (with your eyes) that the path is really what you think it is. I see you are already logging download.getCanonicalPath, do check in your logs what it really is.
Finally, is Download.download really saving stuff where you think it does? Before you call it you are preparing and verifying a directory using download, but then you are not using download when you call Download.download, so it's impossible to tell.
Btw, don't repeat yourself, you can rewrite without repeating the Download.download line:
if (!download.exists())
if (!download.mkdirs()) {
throw new ExternalStorageSetupFailedException("Download sub-directories could not be created");
}
}
Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
I'm working on an Java applet that prints a file.
The applet is "self-signed".
The print function is:
//argFilePath : path to file (http://localhost/Teste/pdf1.pdf)
//argPrintService : something like PrintServiceLookup.lookupDefaultPrintService()
private int print(String argFilePath, PrintService argPrintService){
try
{
DocPrintJob printJob = argPrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(argFilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
I get this exception when trying to open the file:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)
HTML/JavaScrip
<input onclick="alert(document.getElementById('xpto').print('http://localhost/Teste/pdf1.pdf'));" type="button"/>
<applet width="180" height="120" code="printers.class" id="xpto" archive="printerAPI.jar"></applet>
is correct to use:
DocFlavor.INPUT_STREAM.AUTOSENSE
The idea seems to be to print as many file type as possible - pdf, docx, jpg, etc.
How can you fix the exception?
Found the answer (on stackoverflow lol :D)!
It looks like the problem was:
"javascript does not have file access permissions"
so the applet is blocked. we have to use
AccessController.doPrivileged()
doPrivileged
Here is my implementation:
private int print(String argFilePath, PrintService argPrintService){
cPrint cP = new cPrint(argFilePath, argPrintService);
return (Integer) AccessController.doPrivileged(cP);
}
class cPrint implements PrivilegedAction<Object> {
String FilePath;
PrintService PrintService;
public cPrint(String argFilePath, PrintService argPrintService) {
this.FilePath = argFilePath;
this.PrintService = argPrintService;
};
public Object run() {
// privileged code goes here
try
{
DocPrintJob printJob = PrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(FilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
}
You probably got this:
java.security.AccessControlException: access denied (java.net.SocketPermission
127.0.0.1:80 connect,resolve)
because applets can't make connections to websites, other than the one they came from. Now, this is terribly silly because one would think localhost is not another website, but the Java SecurityManager must only look at IP address. Therefore, if the browser is connected to 74.125.224.224 then the Java applet must connect to that address—which is different from localhost, whose address is 127.0.0.1.
This will just take care of the Socket Permission error. But, you'll probably run into something else if you're trying to access the user's hardware. In which case, you'll need to make a certificate and the user will choose whether or not to run your applet.
If you just want to run this on your home computer then, you need a plain-text java.policy file in your home directory. (~/.java.policy for Unix people.) In that file you'll type:
grant{
permission java.security.AllPermission;
};
After you save this file in your home directory, all java applets will be given full permission to run—anything. It'll be like the SecurityManager doesn't exist, so try to be a bit careful. After you're done with testing, I'd recommend to delete this file.