Tom
Tom

Reputation: 29

How block the use of java.io.file

I'm creating a webpage on my Linux server for my students to compile Java code online: they write their code in a textarea/html and then press a button to compile/execute their programs

The first thinks I want to do, it's prohibit the usage of "java.io.file" ... Do you have any tips to block this access? I use jdk.8.0_60

I don't know if anyone have some information to up the security of code's execution on my server ...

Upvotes: 2

Views: 1262

Answers (5)

Tom
Tom

Reputation: 29

Thanks for your answer ... I use now this command line

 java -Xmx16M -Xms2M -Xss2M -Djava.security.manager Djava.security.policy=/home/java.policy Main

Thanks Tom

Upvotes: 0

Tom
Tom

Reputation: 29

Really thanks, i think i reached my goal ...

I create a file at the path "/home/policy" which contains this lines : grant { permission java.lang.RuntimePermission "setSecurityManager"; permission java.lang.RuntimePermission "createSecurityManager"; permission java.lang.RuntimePermission "usePolicy"; };

And i try this code which give me error for only read a file with right 777 :

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
public class Main extends SecurityManager {
   public static void main(String[] args) {
   // set the policy file as the system securuty policy
   System.setProperty("java.security.policy", "file:/home/java.policy");

   // create a security manager
   Main sm = new Main();

   // set the system security manager
   System.setSecurityManager(sm);
   System.out.println("Test");

   //lit un fichier
   BufferedReader br = null;
   try {
      String sCurrentLine;
      br = new BufferedReader(new FileReader("/home/a.txt"));
      while ((sCurrentLine = br.readLine()) != null) {
         System.out.println(sCurrentLine);
      }
   } catch (IOException e) {
      e.printStackTrace();
   } finally {
      try {
         if (br != null)br.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }

   // print a message if we passed the check
   System.out.println("Allowed!");
   }
}

I hope that it's the right way ... Thanks for your help Thomas

Upvotes: 0

Andreas
Andreas

Reputation: 159114

You can enable the Java Security Manager.

See this page for guides about security. See section 8 "Access Control" of the Java™ Security Overview link.

With the use of a policy file, you can control which, if any, files are permitted, but there are many other aspects that can be controlled. See the Policy Permissions link for a list. Here are some notable ones:

  • FilePermission represents access to a file or directory.
  • RuntimePermission is for runtime permissions, e.g. prevent System.exit().
  • SocketPermission represents access to a network via sockets.
  • URLPermission represents permission to access a resource defined by a given URL.

Note: Even with a Security Manager, you should still protect your server using the operating system file security.

Upvotes: 4

Riddhesh Sanghvi
Riddhesh Sanghvi

Reputation: 1222

  • Defining and registering your own security manager will allow you to limit what the code does - see oracle documentation for SecurityManager.

  • Have a look at the java-sandbox project which allows you to easily create very flexible sandboxes to run untrusted code.

The java-sandbox allows to securely execute untrusted code, such as third-party or user generated code from within your application. It allows to specify resources and classes that may be used by the code, thus, separating the execution from the application's execution environment.

Upvotes: 1

Ownaginatious
Ownaginatious

Reputation: 797

You can't really restrict usage of standard libraries in the JVM... at least not easily.

If you want to prevent your students from messing with your file system, you should instead leverage the operating system to do that. Execute the code under a user account that is restricted to a specific directory and obviously without sudo privileges.

Your server should already be setup such that sensitive data (e.g. grades) cannot be accessed by any user on the system.

Upvotes: 2

Related Questions