Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6568872

BasicPermission.newPermissionCollection() violates general contract specified in Permission class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • security-libs
    • b16
    • x86
    • linux, solaris
    • Verified

      Specification for Permission.newPermissionCollection() reads:
      "Returns:
              a new PermissionCollection object for ***this type of Permission***, or null if one is not defined."

      However PermissionCollection instance which is returned by BasicPermission.newPermissionCollection() can contain permissions of other type, moreover it can reject adding permission of this type. The following example code demonstrates the problem:
      ======================================================================

               Permission perm1 = new RuntimePermission("exitVM");
               Permission perm2 = new PropertyPermission("java.*", "read");
      //First two collections should be for RuntimePermissions according to spec
               PermissionCollection permCollection1 = perm1.newPermissionCollection();
               PermissionCollection permCollection2 = perm1.newPermissionCollection();
               //This collection is for PropertyPermissions
               PermissionCollection permCollection3 = perm2.newPermissionCollection();
                 
               //In this test we add correct permission first
               try {
                  permCollection1.add(perm1);
               } catch (RuntimeException ex) {
                   System.out.println("Not OK. The followng exception was thrown while adding RuntimePermission to RuntimePermission's collection perm1: " + ex);
               }
      // and then permission of other type
               try {
                   permCollection1.add(perm2);
               } catch (RuntimeException ex) {
                   System.out.println("OK. The followng exception was thrown while adding PropertyPermission to RuntimePermission's collection perm1: " + ex);
               }
               
               //And now we add permissions in the reverse order. Problem is here
               try {
                  permCollection2.add(perm2);
               } catch (RuntimeException ex) {
                   System.out.println("OK. The followng exception was thrown while adding PropertyPermission to RuntimePermission's collection perm2: " + ex);
               }
               try {
                   permCollection2.add(perm1);
               } catch (RuntimeException ex) {
                   System.out.println("Not OK. The followng exception was thrown while adding RuntimePermission to RuntimePermission's collection perm2: " + ex);
               }
               
               //Collection for PropertyPermission works correct in similar situation.
               try {
                  permCollection3.add(perm1);
               } catch (RuntimeException ex) {
                   System.out.println("OK. The followng exception was thrown while adding RuntimePermission to PropertyPermission's collection perm2: " + ex);
               }
               try {
                   permCollection3.add(perm2);
               } catch (RuntimeException ex) {
                   System.out.println("Not OK. The followng exception was thrown while adding PropertyPermission to PropertyPermission's collection perm2: " + ex);
               }

      ==========================================================

      Below is the output from the code above:

      ===================== Output =============================
      OK. The followng exception was thrown while adding PropertyPermission to RuntimePermission's collection perm1: java.lang.IllegalArgumentException: invalid permission: (java.util.PropertyPermission java.* read)

      Not OK. The followng exception was thrown while adding RuntimePermission to RuntimePermission's collection perm2: java.lang.IllegalArgumentException: invalid permission: (java.lang.RuntimePermission exitVM)

      OK. The followng exception was thrown while adding RuntimePermission to PropertyPermission's collection perm2: java.lang.IllegalArgumentException: invalid permission: (java.lang.RuntimePermission exitVM)
      ===========================================================

            weijun Weijun Wang
            mismirno Mikhail Smirnov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: