2013-07-08 40 views
1

我想禁止在AccessController.doPriviliged()方法中創建線程。下面的方法創建並運行線程。我用-Djava.security.manager運行這個。根據這個鏈接,如果modifyThreadGroup沒有被授予,那麼線程創建應該被禁止?AccessController doPrivileged可以在modifyThreadGroup未被授予時創建線程嗎?

http://docs.oracle.com/javase/7/docs/technotes/guides/security/permissions.html

誰能開導我,爲什麼發生這種情況,並使用AccessController的不允許線程創建正確的方法是什麼?

// .java.policy in $UserHome: 

grant codeBase "file:/C:/-" { 
    permission java.security.AllPermission; 
}; 


public class ThreadTest { 
    public void testModifyThreadGroup() { 

    // grant no permissions 
    Permissions perms = new Permissions(); 

    ProtectionDomain domain = new ProtectionDomain(
      new CodeSource(null, (Certificate[]) null), perms); 
    AccessControlContext _accessControlContext = new AccessControlContext(
      new ProtectionDomain[] { domain }); 

    try { 
     AccessController.doPrivileged(new PrivilegedExceptionAction(){ 
      @Override 
      public Object run() { 
       try { 
        // modifyThreadGroup not granted, so should not be able 
        // to call Thread constructor??? 
        Thread t = new Thread(new Runnable() { 
         @Override 
         public void run() { 
          System.out.println("Thread.run()"); 
         } 
        }); 
        t.run(); 
       } catch (Exception ex) { 
        System.out.println("Error running thread: " + ex); 
       } 
       return null; 
     }}, _accessControlContext); 
    } catch(Exception e) { 
     System.out.println("Access Error running doPrivileged: " + e); 
    } 
    } 
} 

回答

3

創建或啓動一個線程時所做的唯一檢查是檢查,看看是否調用線程是否有權修改該線程組。這並不像你想的那樣檢查調用線程是否具有「modifyThreadGroup」權限。

取而代之的是它(by default)總是授予訪問權限,除非有問題的ThreadGroup是系統線程組,在這種情況下,會檢查「modifyThreadGroup」權限。有問題的ThreadGroup幾乎從不是系統線程組。

您將不得不使用自己的實現來擴展SecurityManager,並對自己的實現進行一些檢查。您應該可以編寫自己的新許可。

import java.security.AccessControlContext; 
import java.security.AccessController; 
import java.security.CodeSource; 
import java.security.Permissions; 
import java.security.PrivilegedExceptionAction; 
import java.security.ProtectionDomain; 
import java.security.cert.Certificate; 

public class QuickTest { 

    public static class NoThreadsSecurityManager extends SecurityManager { 

     public void checkAccess(ThreadGroup g) { 
      super.checkAccess(g); 
      checkPermission(new RuntimePermission("exitVM")); 
     } 

    } 

    public static class SimpleRunnable implements PrivilegedExceptionAction<Object> { 
     @Override 
     public Object run() { 
      try { 
       Thread t = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         System.out.println("Thread.run()"); 
        } 
       }); 
       t.start(); 
       t.join(); 
      } catch (Exception ex) { 
       System.out.println("Error running thread: " + ex); 
      } 
      return null; 
     } 
    } 

    public void testModifyThreadGroup() { 

     // grant no permissions 
     Permissions perms = new Permissions(); 

     ProtectionDomain domain = new ProtectionDomain(new CodeSource(null, (Certificate[]) null), perms); 
     AccessControlContext _accessControlContext = new AccessControlContext(new ProtectionDomain[] { domain }); 

     try { 
      AccessController.doPrivileged(new SimpleRunnable(), _accessControlContext); 
     } catch (Exception e) { 
      System.out.println("Access Error running doPrivileged: " + e); 
     } 

     new SimpleRunnable().run(); 

    } 

    public static void main(String[] args) { 
     System.setSecurityManager(new NoThreadsSecurityManager()); 
     new QuickTest().testModifyThreadGroup(); 
    } 

} 
+2

謝謝!從你鏈接的文檔中:「需要更嚴格策略的應用程序應該重寫此方法,如果重寫此方法,則重寫該方法的方法應另外檢查調用線程是否具有RuntimePermission(」modifyThreadGroup「)權限,如果所以,請靜靜地返回。這是爲了確保授予該權限的代碼(如JDK本身)可以處理任何線程。「我發現在相關方法中檢查'modifyThread'和'modifyThreadGroup'(檢查'exitVM'的地方)給出了期望的行爲。 – markt

2

接受佩斯的響應後,我發現一些別人可能會覺得有用:作爲一個例子,我使用給出的默認策略現有權限,我知道的線程將不得不(exitVM)實施的黑客攻擊。從你鏈接的文檔中:「需要更嚴格策略的應用程序應該重寫此方法,如果重寫此方法,則重寫該方法的方法應另外檢查調用線程是否具有RuntimePermission(」modifyThreadGroup「)權限,如果所以,請靜靜地返回,這是爲了確保授予該權限的代碼(如JDK本身)可以處理任何線程。「我發現在相關方法中檢查'modifyThread'和'modifyThreadGroup'(檢查'exitVM'的地方)給出了期望的行爲。

相關問題