java.security.AccessControlException: Access denied (java.io.FilePermission - java

final File parentDir = new File("S:\\PDSPopulatingProgram");
parentDir.mkdir();
final String hash = "popupateData";
final String fileName = hash + ".txt";
final File file = new File(parentDir, fileName);
file.createNewFile(); // Creates file PDSPopulatingProgram/popupateData.txt
I am trying to create a file in a folder but I am getting exception as
java.security.AccessControlException: Access denied
I am working in windows environment. I can create a folder from the Windows Explorer, but not from the Java Code.
How can I resolve this issue?

Within your <jre location>\lib\security\java.policy try adding:
grant {
permission java.security.AllPermission;
};
And see if it allows you. If so, you will have to add more granular permissions.
See:
Java 8 Documentation for java.policy files
and
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html

Although it is not recommended, but if you really want to let your web application access a folder outside its deployment directory. You need to add following permission in java.policy file (path is as in the reply of Petey B)
permission java.io.FilePermission "your folder path", "write"
In your case it would be
permission java.io.FilePermission "S:/PDSPopulatingProgram/-", "write"
Here /- means any files or sub-folders inside this folder.
Warning: But by doing this, you are inviting some security risk.

Just document it here
on Windows you need to escape the \ character:
"e:\\directory\\-"

Related

Block some permissions and grant other permissions in java security policy

I want to implement a security policy file in the following way :-
Restrict access to all files except for files in 3 directories, i.e. if code accesses files from these 3 directories, it should be allowed but file access for any other directory is restricted.
Grant all other permissions to the code base.
How can I proceed for creating policy file for this requirement.
You need to create next policy file (yourPolicy.policy):
grant codeBase "file:/location_of_your_code/-" {
permission java.io.FilePermission "/tmp/f1/*", "read, write";
permission java.io.FilePermission "/tmp/f2/*", "read, write";
permission java.io.FilePermission "/tmp/f3/*", "read, write";
};
And launch your code with next arguments:
java -Djava.security.manager -Djava.security.policy=yourPolicy.policy YourClassName
It will restrict access of your java program to only these three folders.
About requirement “grant all other permissions” it seems that you can’t grant all permissions and override some specific permissions (grant access to only three folders) using java policy syntax. Thus you need explicitly specify all permissions that you want to grant to your application.

How to grant file read/write permission in a Security Manager policy file for variable parent directories

I'm learning to use Security Manager, and I'm getting this error when I run my unit tests:
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Users\[username]\AppData\Local\NetBeans\Cache\7.4\executor-snippets\junitvmwatcher1469887727677239882.properties" "write")
It's simple enough grant permission to that directory. The problem is I run this code on different computers. Ideally, I'd like to do something like this:
permission java.io.FilePermission "*/NetBeans/Cache/7.4/-", "write";
But apparently SecurityManager doesn't recognize wildcard characters at the beginning of the path. I've tried using both an asterisk and a dash. Neither works.
Basically, I'd like to get my tests to run without needing to hard-code an absolute path. Is there another way to achieve this?
You can use property expansion in policy files:
For example, permission java.io.FilePermission "${user.home}", "read";
will expand "${user.home}" to use the value of the "user.home" system property.
Could you set:
permission java.io.FilePermission "${user.home}/AppData/Local/NetBeans/-", "write";
to get what you wanted?

Java FileOutputStream access denied to a signed applet

I'm developping a Java Applet that must access the visitor's filesystem, so i compressed my .class file to a .jar file with self-signed cert, when I'm opening the call.html file with my browser (file where is located the <applet> HTML tag), I accept the security popup then i'm getting this error in the Java console:
java.security.AccessControlException: access denied (java.io.FilePermission output.txt write)
I'm using a FileInputStream and a FileOutputStream. FileInputStream works but not FileOutputStream, why?
Here's my code:
try {
AccessController.doPrivileged(
new PrivilegedExceptionAction() {
#Override
public Object run() throws FileNotFoundException {
outFile = new FileOutputStream("output.txt");
inFile = new FileInputStream("input.txt");
return "test";
}
}
);
} catch (PrivilegedActionException e) {
throw (FileNotFoundException) e.getException();
}
I've tried many way to make privileged actions, FileInputStream is always working, whereas FileOutputStream isn't. output.txt is not read-only file.
Access permission is granted with a policy file, and appletviewer is launched with the policy file to be used for the applet being viewed.
Creating a Policy File
Policy tool is a Java 2 Platform security tool for creating policy files. The Java Tutorial trail on Controlling Applets explains how to use Policy Tool in good detail. Here is the policy file you need to run the applet. You can use Policy tool to create it or copy the text below into an ASCII file.
grant {
permission java.util.PropertyPermission
"user.home", "read";
permission java.io.FilePermission
"${user.home}/text.txt", "read,write";
};
Here is the full link for applets permission
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/data.html

how to provide file write permission for applet in jdk1.4

I have problem in setup of policy file for applet.I am doing this first time and don't know how to set the policy file for applet in java.Actually I want to give the permission to the applet to write on the file system. for Which I will have to give file permission to the applet
So I make a file named .java.policy and and put the following code in it
grant codeBase "file:/C://res/applet/*" { permission java.io.FilePermission "C:\res\applet\test.txt", "read, write"; };
and save this in users\jindal folder now i set the JAVA_HOME as c:\users\jindal
but still I found the exception that
java.security.AccessControlException: access denied (java.io.FilePermission C:\res\applet\test.txt write)
can any body please help what is wrong or what should i do.And I have to use jdk 1.4
You need to sign your jar file
see also : http://java.sun.com/developer/Books/javaprogramming/JAR/sign/signing.html
You are probably better of signing the jar. Signing the jar elevates the privileges for the applet, which enables file access.
First you need a certificate. You can create a temporary one by:
keytool -genkey -alias certAlias
Sign the jar:
jarsigner -storepass yourPwd -signedjar output.jar in.jar certAlias

Java RMI: Client security policy

grant {
permission java.security.AllPermission;
};
This works.
grant file:///- {
permission java.security.AllPermission;
};
This does not work. Could someone please explain to me why?
The syntax should be:
grant codeBase "file:///-" {
...
};
See the docs. Note the semicolon.
Be very careful assigning permissions to code.
Are you sure the codebase should be a file URL (normal for development, not for production...).
The directive "grant { permission }" means grant the permission to all code no matter where it came from. In other words, when there is no codebase specified, the code could be loaded from the network or the file system.
The second directive (if it worked) would only apply to the local file system. It would be specifying all files (recursively) on the local file system. I'm not sure that "file:///" is a valid URL by itself. I know that file:///tmp/- works.

Categories