FYI: Remerge SecurityManager with Classpath

Mark Wielaard mark@klomp.org
Sun Dec 1 08:15:00 GMT 2002


Hi,

The following remerges java.lang.SecurityManager with GNU Classpath.
It is not a complete remerge, only the comments and indenting are
updated (see attached diff 1) and all checkXXX methods are updated (see
attached diff 2). The methods currentClassLoader(), currentLoadedClass()
and classLoaderDepth() are not merged. In the future these methods will
have to be changed in libgcj to  use Andrew his new StackStrace class.
And for GNU Classpath I am currently looking at a patch from the JRVM
developers to change these methods. Also note that the code still
contains a couple of XXXs. These XXXs are very essential and must be
fixed before our permission framework will actually work. But again by
using the new StackTrace class this should not be that hard.

2002-12-01  Mark Wielaard  <mark@klomp.org>

        * java/lang/SecurityManager.java: Remerge comments, indenting and 
        checkXXX methods with Classpath.

I have attached two diffs to make it easier to see what actually
changed, but it will be just one commit.

Cheers,

Mark
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SecurityManager.indent-comment.diff
Type: text/x-patch
Size: 62969 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20021201/d34f5d56/attachment.bin>
-------------- next part --------------
--- java/lang/SecurityManager.java.indented	2002-12-01 16:27:22.000000000 +0100
+++ java/lang/SecurityManager.java	2002-12-01 16:37:35.000000000 +0100
@@ -140,8 +140,9 @@
    */
   public SecurityManager()
   {
-    if (System.getSecurityManager() != null)
-      throw new SecurityException();
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null)
+      sm.checkPermission(new RuntimePermission("createSecurityManager"));
   }
 
   /**
@@ -352,7 +353,7 @@
    */
   public void checkCreateClassLoader()
   {
-    throw new SecurityException("Cannot create new ClassLoaders.");
+    checkPermission(new RuntimePermission("createClassLoader"));
   }
 
   /**
@@ -381,7 +382,8 @@
    */
   public void checkAccess(Thread t)
   {
-    throw new SecurityException("Cannot modify Threads.");
+    if (t.group != null && t.group.getParent() != null)
+      checkPermission(new RuntimePermission("modifyThread"));
   }
 
   /**
@@ -413,7 +415,8 @@
    */
   public void checkAccess(ThreadGroup g)
   {
-    throw new SecurityException("Cannot modify ThreadGroups.");
+    if (g.getParent() != null)
+      checkPermission(new RuntimePermission("modifyThreadGroup"));
   }
 
   /**
@@ -430,7 +433,7 @@
    */
   public void checkExit(int status)
   {
-    throw new SecurityException("Cannot exit JVM.");
+    checkPermission(new RuntimePermission("exitVM"));
   }
 
   /**
@@ -449,7 +452,9 @@
    */
   public void checkExec(String program)
   {
-    throw new SecurityException("Cannot execute programs.");
+    if (! program.equals(new File(program).getAbsolutePath()))
+      program = "<<ALL FILES>>";
+    checkPermission(new FilePermission(program, "execute"));
   }
 
   /**
@@ -467,7 +472,9 @@
    */
   public void checkLink(String filename)
   {
-    throw new SecurityException("Cannot link native libraries.");
+    // Use the toString() hack to do the null check.
+    checkPermission(new RuntimePermission("loadLibrary."
+                                          + filename.toString()));
   }
 
   /**
@@ -485,7 +492,9 @@
    */
   public void checkRead(FileDescriptor desc)
   {
-    throw new SecurityException("Cannot read files via file descriptors.");
+    if (desc == null)
+      throw new NullPointerException();
+    checkPermission(new RuntimePermission("readFileDescriptor"));
   }
 
   /**
@@ -506,7 +515,7 @@
    */
   public void checkRead(String filename)
   {
-    throw new SecurityException("Cannot read files via file names.");
+    checkPermission(new FilePermission(filename, "read"));
   }
 
   /**
@@ -550,7 +559,9 @@
    */
   public void checkWrite(FileDescriptor desc)
   {
-    throw new SecurityException("Cannot write files via file descriptors.");
+    if (desc == null)
+      throw new NullPointerException();
+    checkPermission(new RuntimePermission("writeFileDescriptor"));
   }
 
   /**
@@ -573,7 +584,7 @@
    */
   public void checkWrite(String filename)
   {
-    throw new SecurityException("Cannot write files via file names.");
+    checkPermission(new FilePermission(filename, "write"));
   }
 
   /**
@@ -589,7 +600,7 @@
    */
   public void checkDelete(String filename)
   {
-    throw new SecurityException("Cannot delete files.");
+    checkPermission(new FilePermission(filename, "delete"));
   }
 
   /**
@@ -611,7 +622,12 @@
    */
   public void checkConnect(String host, int port)
   {
-    throw new SecurityException("Cannot make network connections.");
+    if (port == -1)
+      checkPermission(new SocketPermission(host, "resolve"));
+    else
+      // Use the toString() hack to do the null check.
+      checkPermission(new SocketPermission(host.toString() + ":" + port,
+                                           "connect"));
   }
 
   /**
@@ -664,7 +680,9 @@
    */
   public void checkListen(int port)
   {
-    throw new SecurityException("Cannot listen for connections.");
+    checkPermission(new SocketPermission("localhost:"
+                                         + (port == 0 ? "1024-" : "" +port),
+                                         "listen"));
   }
 
   /**
@@ -683,7 +701,9 @@
    */
   public void checkAccept(String host, int port)
   {
-    throw new SecurityException("Cannot accept connections.");
+    // Use the toString() hack to do the null check.
+    checkPermission(new SocketPermission(host.toString() + ":" + port,
+                                         "accept"));
   }
 
   /**
@@ -700,7 +720,8 @@
    */
   public void checkMulticast(InetAddress addr)
   {
-    throw new SecurityException("Cannot read or write multicast.");
+    checkPermission(new SocketPermission(addr.getHostAddress(),
+                                         "accept,connect"));
   }
 
   /**
@@ -720,7 +741,8 @@
    */
   public void checkMulticast(InetAddress addr, byte ttl)
   {
-    throw new SecurityException("Cannot read or write multicast.");
+    checkPermission(new SocketPermission(addr.getHostAddress(),
+                                         "accept,connect"));
   }
 
   /**
@@ -737,7 +759,7 @@
    */
   public void checkPropertiesAccess()
   {
-    throw new SecurityException("Cannot access all system properties at once.");
+    checkPermission(new PropertyPermission("*", "read,write"));
   }
 
   /**
@@ -755,7 +777,7 @@
    */
   public void checkPropertyAccess(String key)
   {
-    throw new SecurityException("Cannot access individual system properties.");
+    checkPermission(new PropertyPermission(key, "read"));
   }
 
   /**
@@ -776,7 +798,17 @@
    */
   public boolean checkTopLevelWindow(Object window)
   {
-    return false;
+    if (window == null)
+      throw new NullPointerException();
+    try
+      {
+        checkPermission(new AWTPermission("showWindowWithoutWarningBanner"));
+        return true;
+      }
+    catch (SecurityException e)
+      {
+        return false;
+      }
   }
 
   /**
@@ -792,7 +824,7 @@
    */
   public void checkPrintJobAccess()
   {
-    throw new SecurityException("Cannot create print jobs.");
+    checkPermission(new RuntimePermission("queuePrintJob"));
   }
 
   /**
@@ -808,7 +840,7 @@
    */
   public void checkSystemClipboardAccess()
   {
-    throw new SecurityException("Cannot access the system clipboard.");
+    checkPermission(new AWTPermission("accessClipboard"));
   }
 
   /**
@@ -846,7 +878,7 @@
    */
   public void checkPackageAccess(String packageName)
   {
-    throw new SecurityException("Cannot access packages via the ClassLoader.");
+    checkPackageList(packageName, "access", "accessClassInPackage.");
   }
 
   /**
@@ -868,7 +900,7 @@
    */
   public void checkPackageDefinition(String packageName)
   {
-    throw new SecurityException("Cannot load classes into any packages via the ClassLoader.");
+    checkPackageList(packageName, "definition", "defineClassInPackage.");
   }
 
   /**
@@ -886,7 +918,7 @@
    */
   public void checkSetFactory()
   {
-    throw new SecurityException("Cannot set the socket factory.");
+    checkPermission(new RuntimePermission("setFactory"));
   }
 
   /**
@@ -914,7 +946,13 @@
    */
   public void checkMemberAccess(Class c, int memberType)
   {
-    throw new SecurityException("Cannot access members of classes.");
+    if (c == null)
+      throw new NullPointerException();
+    if (memberType == Member.PUBLIC)
+      return;
+    // XXX Allow access to classes created by same classloader before next
+    // check.
+    checkPermission(new RuntimePermission("accessDeclaredMembers"));
   }
 
   /**
@@ -949,7 +987,48 @@
   {
     return Thread.currentThread().getThreadGroup();
   }
-}
+
+  /**
+   * Helper that checks a comma-separated list of restricted packages, from
+   * <code>Security.getProperty("package.definition")</code>, for the given
+   * package access permission. If packageName starts with or equals any
+   * restricted package, it checks
+   * <code>RuntimePermission(permission + packageName)</code>.
+   *
+   * @param packageName the package name to check access to
+   * @param restriction the list of restrictions, after "package."
+   * @param permission the base permission, including the '.'
+   * @throws SecurityException if permission is denied
+   * @throws NullPointerException if packageName is null
+   * @see #checkPackageAccess(String)
+   * @see #checkPackageDefinition(String)
+   */
+  void checkPackageList(String packageName, String restriction,
+                        String permission)
+  {
+    // Use the toString() hack to do the null check.
+    Permission p = new RuntimePermission(permission + packageName.toString());
+    String list = Security.getProperty("package." + restriction);
+    if (list == null)
+      return;
+    while (! "".equals(packageName))
+      {
+        for (int index = list.indexOf(packageName);
+             index != -1; index = list.indexOf(packageName, index + 1))
+          {
+	    int packageNameCount = packageName.length();
+            if (index + packageNameCount == list.length()
+                || list.charAt(index + packageNameCount) == ',')
+              {
+                checkPermission(p);
+                return;
+              }
+          }
+        int index = packageName.lastIndexOf('.');
+        packageName = index < 0 ? "" : packageName.substring(0, index);
+      }
+  }
+} // class SecurityManager
 
 // XXX This class is unnecessary.
 class SecurityContext {


More information about the Java-patches mailing list