jdk/src/java.base/share/classes/java/security/ProtectionDomain.java
author coleenp
Wed, 30 Aug 2017 19:18:22 -0400
changeset 47098 e704f55561c3
parent 45434 4582657c7260
permissions -rw-r--r--
8164207: Checking missing load-acquire in relation to _pd_set in dictionary.cpp Summary: Use load_acquire for accessing DictionaryEntry::_pd_set since it's accessed outside the SystemDictionary_lock Reviewed-by: zgu, twisti, dholmes, adinn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43803
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
    28
import java.lang.ref.Reference;
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
    29
import java.lang.ref.ReferenceQueue;
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
    30
import java.lang.ref.SoftReference;
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
    31
import java.lang.ref.WeakReference;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    32
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
42153
d57f354dd0ab 8043252: Debug of access control is obfuscated - NullPointerException in ProtectionDomain
jnimeh
parents: 41596
diff changeset
    35
import java.util.Objects;
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
    36
import java.util.concurrent.ConcurrentHashMap;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30687
diff changeset
    37
import jdk.internal.misc.JavaSecurityAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30687
diff changeset
    38
import jdk.internal.misc.JavaSecurityProtectionDomainAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30687
diff changeset
    39
import static jdk.internal.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30687
diff changeset
    40
import jdk.internal.misc.SharedSecrets;
43803
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    41
import sun.security.action.GetPropertyAction;
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
    42
import sun.security.provider.PolicyFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.util.Debug;
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
    44
import sun.security.util.FilePermCompat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
    48
 * The ProtectionDomain class encapsulates the characteristics of a domain,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * which encloses a set of classes whose instances are granted a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * of permissions when being executed on behalf of a given set of Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * A static set of permissions can be bound to a ProtectionDomain when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * constructed; such permissions are granted to the domain regardless of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Policy in force. However, to support dynamic security policies, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * ProtectionDomain can also be constructed such that it is dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * mapped to a set of permissions by the current Policy whenever a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Gary Ellison
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 43803
diff changeset
    62
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class ProtectionDomain {
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
    66
43803
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    67
    /**
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    68
     * If true, {@link #impliesWithAltFilePerm} will try to be compatible on
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    69
     * FilePermission checking even if a 3rd-party Policy implementation is set.
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    70
     */
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    71
    private static final boolean filePermCompatInPD =
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    72
            "true".equals(GetPropertyAction.privilegedGetProperty(
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    73
                "jdk.security.filePermCompat"));
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
    74
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    75
    private static class JavaSecurityAccessImpl implements JavaSecurityAccess {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    76
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    77
        private JavaSecurityAccessImpl() {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    78
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    79
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    80
        @Override
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    81
        public <T> T doIntersectionPrivilege(
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    82
                PrivilegedAction<T> action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    83
                final AccessControlContext stack,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    84
                final AccessControlContext context) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    85
            if (action == null) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    86
                throw new NullPointerException();
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    87
            }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    88
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    89
            return AccessController.doPrivileged(
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    90
                action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    91
                getCombinedACC(context, stack)
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    92
            );
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    93
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    94
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    95
        @Override
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    96
        public <T> T doIntersectionPrivilege(
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    97
                PrivilegedAction<T> action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    98
                AccessControlContext context) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
    99
            return doIntersectionPrivilege(action,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   100
                AccessController.getContext(), context);
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   101
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   102
41596
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41377
diff changeset
   103
        @Override
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41377
diff changeset
   104
        public ProtectionDomain[] getProtectDomains(AccessControlContext context) {
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41377
diff changeset
   105
            return context.getContext();
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41377
diff changeset
   106
        }
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41377
diff changeset
   107
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   108
        private static AccessControlContext getCombinedACC(
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   109
            AccessControlContext context, AccessControlContext stack) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   110
            AccessControlContext acc =
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   111
                new AccessControlContext(context, stack.getCombiner(), true);
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   112
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   113
            return new AccessControlContext(stack.getContext(), acc).optimize();
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   114
        }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   115
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   117
    static {
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   118
        // setup SharedSecrets to allow access to doIntersectionPrivilege
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   119
        // methods and ProtectionDomain cache
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 27181
diff changeset
   120
        SharedSecrets.setJavaSecurityAccess(new JavaSecurityAccessImpl());
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   121
        SharedSecrets.setJavaSecurityProtectionDomainAccess(
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   122
            new JavaSecurityProtectionDomainAccess() {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   123
                @Override
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   124
                public ProtectionDomainCache getProtectionDomainCache() {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   125
                    return new PDCache();
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   126
                }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   127
            });
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   128
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   129
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   130
    /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   131
     * Used for storing ProtectionDomains as keys in a Map.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   132
     */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   133
    static final class Key {}
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /* CodeSource */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private CodeSource codesource ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /* ClassLoader the protection domain was consed from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private ClassLoader classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /* Principals running-as within this protection domain */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private Principal[] principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /* the rights this protection domain is granted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private PermissionCollection permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /* if the permissions object has AllPermission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private boolean hasAllPerm = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /* the PermissionCollection is static (pre 1.4 constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
       or dynamic (via a policy refresh) */
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   152
    private final boolean staticPermissions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   154
    /*
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   155
     * An object used as a key when the ProtectionDomain is stored in a Map.
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   156
     */
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   157
    final Key key = new Key();
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   158
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Creates a new ProtectionDomain with the given CodeSource and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Permissions. If the permissions object is not null, then
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 34956
diff changeset
   162
     *  {@code setReadOnly()} will be called on the passed in
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   163
     * Permissions object.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   164
     * <p>
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   165
     * The permissions granted to this domain are static, i.e.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   166
     * invoking the {@link #staticPermissionsOnly()} method returns true.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   167
     * They contain only the ones passed to this constructor and
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   168
     * the current Policy will not be consulted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param codesource the codesource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            PermissionCollection permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.classloader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.principals = new Principal[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        staticPermissions = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Creates a new ProtectionDomain qualified by the given CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Permissions, ClassLoader and array of Principals. If the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18222
diff changeset
   192
     * permissions object is not null, then {@code setReadOnly()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * will be called on the passed in Permissions object.
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   194
     * <p>
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   195
     * The permissions granted to this domain are dynamic, i.e.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   196
     * invoking the {@link #staticPermissionsOnly()} method returns false.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   197
     * They include both the static permissions passed to this constructor,
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   198
     * and any permissions granted to this domain by the current Policy at the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * time a permission is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * This constructor is typically used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * {@link SecureClassLoader ClassLoaders}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * and {@link DomainCombiner DomainCombiners} which delegate to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18222
diff changeset
   204
     * {@code Policy} to actively associate the permissions granted to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * this domain. This constructor affords the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Policy provider the opportunity to augment the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * PermissionCollection to reflect policy changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param codesource the CodeSource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param classloader the ClassLoader associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param principals the array of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * domain. The contents of the array are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            PermissionCollection permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            ClassLoader classloader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            Principal[] principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this.classloader = classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this.principals = (principals != null ? principals.clone():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                           new Principal[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        staticPermissions = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Returns the CodeSource of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return the CodeSource of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public final CodeSource getCodeSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return this.codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns the ClassLoader of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return the ClassLoader of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public final ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return this.classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns an array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return a non-null array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public final Principal[] getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return this.principals.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the static permissions granted to this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the static set of permissions for this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public final PermissionCollection getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   282
     * Returns true if this domain contains only static permissions
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   283
     * and does not check the current {@code Policy} at the time of
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   284
     * permission checking.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   285
     *
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   286
     * @return true if this domain contains only static permissions.
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   287
     *
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   288
     * @since 9
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   289
     */
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   290
    public final boolean staticPermissionsOnly() {
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   291
        return this.staticPermissions;
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   292
    }
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   293
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   294
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Check and see if this ProtectionDomain implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * expressed in the Permission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * The set of permissions evaluated is a function of whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * ProtectionDomain was constructed with a static set of permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * or it was bound to a dynamically mapped set of permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   302
     * If the {@link #staticPermissionsOnly()} method returns
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   303
     * true, then the permission will only be checked against the
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   304
     * PermissionCollection supplied at construction.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   306
     * Otherwise, the permission will be checked against the combination
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   307
     * of the PermissionCollection supplied at construction and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * the current Policy binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   310
     * @param perm the Permission object to check.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   312
     * @return true if {@code perm} is implied by this ProtectionDomain.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
39747
2d542b973871 8138811: Construction of static protection domains
valeriep
parents: 37882
diff changeset
   314
    public boolean implies(Permission perm) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (hasAllPerm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // internal permission collection already has AllPermission -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // no need to go to policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (!staticPermissions &&
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   323
            Policy.getPolicyNoCheck().implies(this, perm)) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   324
            return true;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   325
        }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   326
        if (permissions != null) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   327
            return permissions.implies(perm);
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   328
        }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   329
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   330
        return false;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   331
    }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   332
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   333
    /**
43803
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   334
     * This method has almost the same logic flow as {@link #implies} but
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   335
     * it ensures some level of FilePermission compatibility after JDK-8164705.
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   336
     *
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   337
     * This method is called by {@link AccessControlContext#checkPermission}
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   338
     * and not intended to be called by an application.
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   339
     */
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   340
    boolean impliesWithAltFilePerm(Permission perm) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   341
43803
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   342
        // If FilePermCompat.compat is set (default value), FilePermission
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   343
        // checking compatibility should be considered.
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   344
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   345
        // If filePermCompatInPD is set, this method checks for alternative
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   346
        // FilePermission to keep compatibility for any Policy implementation.
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   347
        // When set to false (default value), implies() is called since
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   348
        // the PolicyFile implementation already supports compatibility.
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   349
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   350
        // If this is a subclass of ProtectionDomain, call implies()
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   351
        // because most likely user has overridden it.
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   352
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   353
        if (!filePermCompatInPD || !FilePermCompat.compat ||
e1881d258206 8168410: Multiple JCK tests are failing due to SecurityException is not thrown.
weijun
parents: 42153
diff changeset
   354
                getClass() != ProtectionDomain.class) {
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   355
            return implies(perm);
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   356
        }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   357
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   358
        if (hasAllPerm) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   359
            // internal permission collection already has AllPermission -
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   360
            // no need to go to policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return true;
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   362
        }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   363
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   364
        Permission p2 = null;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   365
        boolean p2Calculated = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   367
        if (!staticPermissions) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   368
            Policy policy = Policy.getPolicyNoCheck();
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   369
            if (policy instanceof PolicyFile) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   370
                // The PolicyFile implementation supports compatibility
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   371
                // inside and it also covers the static permissions.
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   372
                return policy.implies(this, perm);
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   373
            } else {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   374
                if (policy.implies(this, perm)) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   375
                    return true;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   376
                }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   377
                p2 = FilePermCompat.newPermUsingAltPath(perm);
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   378
                p2Calculated = true;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   379
                if (p2 != null && policy.implies(this, p2)) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   380
                    return true;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   381
                }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   382
            }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   383
        }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   384
        if (permissions != null) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   385
            if (permissions.implies(perm)) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   386
                return true;
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   387
            } else {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   388
                if (!p2Calculated) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   389
                    p2 = FilePermCompat.newPermUsingAltPath(perm);
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   390
                }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   391
                if (p2 != null) {
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   392
                    return permissions.implies(p2);
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   393
                }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   394
            }
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39747
diff changeset
   395
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   399
    // called by the VM -- do not remove
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   400
    boolean impliesCreateAccessControlContext() {
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   401
        return implies(SecurityConstants.CREATE_ACC_PERMISSION);
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   402
    }
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   403
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Convert a ProtectionDomain to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   407
    @Override public String toString() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        String pals = "<no principals>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (principals != null && principals.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            StringBuilder palBuf = new StringBuilder("(principals ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                palBuf.append(principals[i].getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            " \"" + principals[i].getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if (i < principals.length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    palBuf.append(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    palBuf.append(")\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            pals = palBuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // Check if policy is set; we don't want to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // the policy prematurely here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        PermissionCollection pc = Policy.isSet() && seeAllp() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                      mergePermissions():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                      getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return "ProtectionDomain "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            " "+codesource+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            " "+classloader+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            " "+pals+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            " "+pc+"\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   437
    /*
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   438
     * holder class for the static field "debug" to delay its initialization
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   439
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   440
    private static class DebugHolder {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   441
        private static final Debug debug = Debug.getInstance("domain");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   442
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   443
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Return true (merge policy permissions) in the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * . SecurityManager is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *          debug is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *          SecurityManager impelmentation is in bootclasspath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *          Policy implementation is in bootclasspath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *          (the bootclasspath restrictions avoid recursion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *          debug is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *          caller has Policy.getPolicy permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private static boolean seeAllp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } else {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   465
            if (DebugHolder.debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (sm.getClass().getClassLoader() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    Policy.getPolicyNoCheck().getClass().getClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                                                == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    // fall thru and return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private PermissionCollection mergePermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (staticPermissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        PermissionCollection perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            java.security.AccessController.doPrivileged
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 29492
diff changeset
   490
            (new java.security.PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        Policy p = Policy.getPolicyNoCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        return p.getPermissions(ProtectionDomain.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        Permissions mergedPerms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        int swag = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int vcap = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        Enumeration<Permission> e;
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   501
        List<Permission> pdVector = new ArrayList<>(vcap);
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   502
        List<Permission> plVector = new ArrayList<>(swag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // Build a vector of domain permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    pdVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Build a vector of Policy permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (perms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            synchronized (perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                e = perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    plVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    vcap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (perms != null && permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            // Weed out the duplicates from the policy. Unless a refresh
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18579
diff changeset
   530
            // has occurred since the pd was consed this should result in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // an empty vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                e = permissions.elements();   // domain vs policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    Permission pdp = e.nextElement();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   536
                    Class<?> pdpClass = pdp.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    String pdpActions = pdp.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    String pdpName = pdp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    for (int i = 0; i < plVector.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        Permission pp = plVector.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        if (pdpClass.isInstance(pp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                            // The equals() method on some permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                            // have some side effects so this manual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                            // comparison is sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                            if (pdpName.equals(pp.getName()) &&
42153
d57f354dd0ab 8043252: Debug of access control is obfuscated - NullPointerException in ProtectionDomain
jnimeh
parents: 41596
diff changeset
   546
                                Objects.equals(pdpActions, pp.getActions())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                plVector.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (perms !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // the order of adding to merged perms and permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            // needs to preserve the bugfix 4301064
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            for (int i = plVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                mergedPerms.add(plVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            for (int i = pdVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                mergedPerms.add(pdVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return mergedPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   572
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   573
    /**
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   574
     * A cache of ProtectionDomains and their Permissions.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   575
     *
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   576
     * This class stores ProtectionDomains as weak keys in a ConcurrentHashMap
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   577
     * with additional support for checking and removing weak keys that are no
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   578
     * longer in use. There can be cases where the permission collection may
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   579
     * have a chain of strong references back to the ProtectionDomain, which
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   580
     * ordinarily would prevent the entry from being removed from the map. To
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   581
     * address that, we wrap the permission collection in a SoftReference so
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   582
     * that it can be reclaimed by the garbage collector due to memory demand.
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   583
     */
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   584
    private static class PDCache implements ProtectionDomainCache {
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   585
        private final ConcurrentHashMap<WeakProtectionDomainKey,
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   586
                                        SoftReference<PermissionCollection>>
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   587
                                        pdMap = new ConcurrentHashMap<>();
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   588
        private final ReferenceQueue<Key> queue = new ReferenceQueue<>();
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   589
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   590
        @Override
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   591
        public void put(ProtectionDomain pd, PermissionCollection pc) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   592
            processQueue(queue, pdMap);
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   593
            WeakProtectionDomainKey weakPd =
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   594
                new WeakProtectionDomainKey(pd, queue);
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   595
            pdMap.put(weakPd, new SoftReference<>(pc));
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   596
        }
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   597
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   598
        @Override
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   599
        public PermissionCollection get(ProtectionDomain pd) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   600
            processQueue(queue, pdMap);
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   601
            WeakProtectionDomainKey weakPd = new WeakProtectionDomainKey(pd);
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   602
            SoftReference<PermissionCollection> sr = pdMap.get(weakPd);
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   603
            return (sr == null) ? null : sr.get();
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   604
        }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   605
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   606
        /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   607
         * Removes weak keys from the map that have been enqueued
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   608
         * on the reference queue and are no longer in use.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   609
         */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   610
        private static void processQueue(ReferenceQueue<Key> queue,
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   611
                                         ConcurrentHashMap<? extends
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   612
                                         WeakReference<Key>, ?> pdMap) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   613
            Reference<? extends Key> ref;
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   614
            while ((ref = queue.poll()) != null) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   615
                pdMap.remove(ref);
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   616
            }
27181
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   617
        }
29f9c4f56e80 8058547: Memory leak in ProtectionDomain cache
mullan
parents: 25859
diff changeset
   618
    }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   619
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   620
    /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   621
     * A weak key for a ProtectionDomain.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   622
     */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   623
    private static class WeakProtectionDomainKey extends WeakReference<Key> {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   624
        /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   625
         * Saved value of the referent's identity hash code, to maintain
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   626
         * a consistent hash code after the referent has been cleared
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   627
         */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   628
        private final int hash;
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   629
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   630
        /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   631
         * A key representing a null ProtectionDomain.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   632
         */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   633
        private static final Key NULL_KEY = new Key();
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   634
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   635
        /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   636
         * Create a new WeakProtectionDomain with the specified domain and
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   637
         * registered with a queue.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   638
         */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   639
        WeakProtectionDomainKey(ProtectionDomain pd, ReferenceQueue<Key> rq) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   640
            this((pd == null ? NULL_KEY : pd.key), rq);
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   641
        }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   642
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   643
        WeakProtectionDomainKey(ProtectionDomain pd) {
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   644
            this(pd == null ? NULL_KEY : pd.key);
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   645
        }
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   646
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   647
        private WeakProtectionDomainKey(Key key, ReferenceQueue<Key> rq) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   648
            super(key, rq);
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   649
            hash = key.hashCode();
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   650
        }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   651
34956
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   652
        private WeakProtectionDomainKey(Key key) {
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   653
            super(key);
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   654
            hash = key.hashCode();
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   655
        }
cfa4105ed600 8085903: New fix for memory leak in ProtectionDomain cache
mullan
parents: 32834
diff changeset
   656
30687
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   657
        /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   658
         * Returns the identity hash code of the original referent.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   659
         */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   660
        @Override
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   661
        public int hashCode() {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   662
            return hash;
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   663
        }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   664
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   665
        /**
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   666
         * Returns true if the given object is an identical
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   667
         * WeakProtectionDomainKey instance, or, if this object's referent
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   668
         * has not been cleared and the given object is another
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   669
         * WeakProtectionDomainKey instance with an identical non-null
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   670
         * referent as this one.
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   671
         */
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   672
        @Override
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   673
        public boolean equals(Object obj) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   674
            if (obj == this) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   675
                return true;
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   676
            }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   677
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   678
            if (obj instanceof WeakProtectionDomainKey) {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   679
                Object referent = get();
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   680
                return (referent != null) &&
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   681
                       (referent == ((WeakProtectionDomainKey)obj).get());
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   682
            } else {
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   683
                return false;
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   684
            }
eb40445ce6d7 8055753: Use ConcurrentHashMap to map ProtectionDomain to PermissionCollection
mullan
parents: 30033
diff changeset
   685
        }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   686
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
}