jdk/src/java.base/share/classes/java/security/AccessControlContext.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
/*
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 29027
diff changeset
     2
 * Copyright (c) 1997, 2015, 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: 476
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: 476
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: 476
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 476
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 476
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 31538
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.Debug;
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 31538
diff changeset
    32
import sun.security.util.FilePermCompat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.SecurityConstants;
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An AccessControlContext is used to make system resource access decisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * based on the context it encapsulates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>More specifically, it encapsulates a context and
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    41
 * has a single method, {@code checkPermission},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    42
 * that is equivalent to the {@code checkPermission} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * in the AccessController class, with one difference: The AccessControlContext
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    44
 * {@code checkPermission} method makes access decisions based on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * context it encapsulates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * rather than that of the current execution thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Thus, the purpose of AccessControlContext is for those situations where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a security check that should be made within a given context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * actually needs to be done from within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <i>different</i> context (for example, from within a worker thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> An AccessControlContext is created by calling the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    54
 * {@code AccessController.getContext} method.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    55
 * The {@code getContext} method takes a "snapshot"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * of the current calling context, and places
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * it in an AccessControlContext object, which it returns. A sample call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   AccessControlContext acc = AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Code within a different context can subsequently call the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    66
 * {@code checkPermission} method on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * previously-saved AccessControlContext object. A sample call is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   acc.checkPermission(permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author Roland Schemers
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 41377
diff changeset
    77
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public final class AccessControlContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
    82
    private ProtectionDomain[] context;
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
    83
    // isPrivileged and isAuthorized are referenced by the VM - do not remove
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
    84
    // or change their names
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private boolean isPrivileged;
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
    86
    private boolean isAuthorized = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Note: This field is directly used by the virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // native codes. Don't touch it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private AccessControlContext privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private DomainCombiner combiner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
    94
    // limited privilege scope
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
    95
    private Permission[] permissions;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
    96
    private AccessControlContext parent;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
    97
    private boolean isWrapped;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
    98
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
    99
    // is constrained by limited privilege scope?
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   100
    private boolean isLimited;
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   101
    private ProtectionDomain[] limitedContext;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static boolean debugInit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static Debug debug = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static Debug getDebug()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (debugInit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (Policy.isSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                debug = Debug.getInstance("access");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                debugInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
476
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   120
     * Create an AccessControlContext with the given array of ProtectionDomains.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Context must not be null. Duplicate domains will be removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param context the ProtectionDomains associated with this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The non-duplicate domains are copied from the array. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * changes to the array will not affect this AccessControlContext.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   127
     * @throws NullPointerException if {@code context} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   129
    public AccessControlContext(ProtectionDomain[] context)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (context.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            this.context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else if (context.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (context[0] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                this.context = context.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                this.context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   140
            List<ProtectionDomain> v = new ArrayList<>(context.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            for (int i =0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if ((context[i] != null) &&  (!v.contains(context[i])))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    v.add(context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
476
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   145
            if (!v.isEmpty()) {
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   146
                this.context = new ProtectionDomain[v.size()];
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   147
                this.context = v.toArray(this.context);
b23646c026ed 6631361: Spec of AccessControlContext constructor is not complete
mullan
parents: 97
diff changeset
   148
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   153
     * Create a new {@code AccessControlContext} with the given
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   154
     * {@code AccessControlContext} and {@code DomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * This constructor associates the provided
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   156
     * {@code DomainCombiner} with the provided
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   157
     * {@code AccessControlContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   159
     * @param acc the {@code AccessControlContext} associated
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   160
     *          with the provided {@code DomainCombiner}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   162
     * @param combiner the {@code DomainCombiner} to be associated
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   163
     *          with the provided {@code AccessControlContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @exception NullPointerException if the provided
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   166
     *          {@code context} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception SecurityException if a security manager is installed and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *          caller does not have the "createAccessControlContext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *          {@link SecurityPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public AccessControlContext(AccessControlContext acc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                DomainCombiner combiner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   176
        this(acc, combiner, false);
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   177
    }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   178
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   179
    /**
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   180
     * package private to allow calls from ProtectionDomain without performing
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 31538
diff changeset
   181
     * the security check for {@linkplain SecurityConstants#CREATE_ACC_PERMISSION}
29027
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   182
     * permission
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   183
     */
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   184
    AccessControlContext(AccessControlContext acc,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   185
                        DomainCombiner combiner,
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   186
                        boolean preauthorized) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   187
        if (!preauthorized) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   188
            SecurityManager sm = System.getSecurityManager();
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   189
            if (sm != null) {
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   190
                sm.checkPermission(SecurityConstants.CREATE_ACC_PERMISSION);
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   191
                this.isAuthorized = true;
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   192
            }
2df5737b2c8a 8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents: 25859
diff changeset
   193
        } else {
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   194
            this.isAuthorized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.context = acc.context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // we do not need to run the combine method on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // provided ACC.  it was already "combined" when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // context was originally retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // at this point in time, we simply throw away the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // combiner and use the newly provided one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.combiner = combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * package private for AccessController
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   210
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   211
     * This "argument wrapper" context will be passed as the actual context
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   212
     * parameter on an internal doPrivileged() call used in the implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   214
    AccessControlContext(ProtectionDomain caller, DomainCombiner combiner,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   215
        AccessControlContext parent, AccessControlContext context,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   216
        Permission[] perms)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   217
    {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   218
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   219
         * Combine the domains from the doPrivileged() context into our
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   220
         * wrapper context, if necessary.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   221
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   222
        ProtectionDomain[] callerPDs = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   223
        if (caller != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   224
             callerPDs = new ProtectionDomain[] { caller };
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   225
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (context != null) {
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   227
            if (combiner != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   228
                this.context = combiner.combine(callerPDs, context.context);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   229
            } else {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   230
                this.context = combine(callerPDs, context.context);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   231
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   232
        } else {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   233
            /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   234
             * Call combiner even if there is seemingly nothing to combine.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   235
             */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   236
            if (combiner != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   237
                this.context = combiner.combine(callerPDs, null);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   238
            } else {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   239
                this.context = combine(callerPDs, null);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   240
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        this.combiner = combiner;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   243
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   244
        Permission[] tmp = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   245
        if (perms != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   246
            tmp = new Permission[perms.length];
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   247
            for (int i=0; i < perms.length; i++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   248
                if (perms[i] == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   249
                    throw new NullPointerException("permission can't be null");
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   250
                }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   251
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   252
                /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   253
                 * An AllPermission argument is equivalent to calling
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   254
                 * doPrivileged() without any limit permissions.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   255
                 */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   256
                if (perms[i].getClass() == AllPermission.class) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   257
                    parent = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   258
                }
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 31538
diff changeset
   259
                // Add altPath into permission for compatibility.
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 31538
diff changeset
   260
                tmp[i] = FilePermCompat.newPermPlusAltPath(perms[i]);
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   261
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   262
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   263
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   264
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   265
         * For a doPrivileged() with limited privilege scope, initialize
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   266
         * the relevant fields.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   267
         *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   268
         * The limitedContext field contains the union of all domains which
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   269
         * are enclosed by this limited privilege scope. In other words,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   270
         * it contains all of the domains which could potentially be checked
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   271
         * if none of the limiting permissions implied a requested permission.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   272
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   273
        if (parent != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   274
            this.limitedContext = combine(parent.context, parent.limitedContext);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   275
            this.isLimited = true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   276
            this.isWrapped = true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   277
            this.permissions = tmp;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   278
            this.parent = parent;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   279
            this.privilegedContext = context; // used in checkPermission2()
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   280
        }
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   281
        this.isAuthorized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   284
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * package private constructor for AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   289
    AccessControlContext(ProtectionDomain[] context,
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   290
                         boolean isPrivileged)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        this.isPrivileged = isPrivileged;
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 10336
diff changeset
   294
        this.isAuthorized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   298
     * Constructor for JavaSecurityAccess.doIntersectionPrivilege()
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   299
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   300
    AccessControlContext(ProtectionDomain[] context,
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   301
                         AccessControlContext privilegedContext)
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   302
    {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   303
        this.context = context;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   304
        this.privilegedContext = privilegedContext;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   305
        this.isPrivileged = true;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   306
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   307
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   308
    /**
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   309
     * Returns this context's context.
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   310
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   311
    ProtectionDomain[] getContext() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   312
        return context;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   313
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   314
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7970
diff changeset
   315
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns true if this context is privileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    boolean isPrivileged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return isPrivileged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * get the assigned combiner from the privileged or inherited context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    DomainCombiner getAssignedCombiner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (isPrivileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            acc = privilegedContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            acc = AccessController.getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (acc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return acc.combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   340
     * Get the {@code DomainCombiner} associated with this
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   341
     * {@code AccessControlContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   343
     * @return the {@code DomainCombiner} associated with this
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   344
     *          {@code AccessControlContext}, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @exception SecurityException if a security manager is installed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *          the caller does not have the "getDomainCombiner"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *          {@link SecurityPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public DomainCombiner getDomainCombiner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            sm.checkPermission(SecurityConstants.GET_COMBINER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   358
        return getCombiner();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   359
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   360
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   361
    /**
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   362
     * package private for AccessController
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   363
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   364
    DomainCombiner getCombiner() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return combiner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 18579
diff changeset
   368
    boolean isAuthorized() {
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 18579
diff changeset
   369
        return isAuthorized;
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 18579
diff changeset
   370
    }
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 18579
diff changeset
   371
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Determines whether the access request indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * specified permission should be allowed or denied, based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * the security policy currently in effect, and the context in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * this object. The request is allowed only if every ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * in the context implies the permission. Otherwise the request is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * This method quietly returns if the access request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * is permitted, or throws a suitable AccessControlException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param perm the requested permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @exception AccessControlException if the specified permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * is not permitted, based on the current security policy and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * context encapsulated by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception NullPointerException if the permission to check for is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public void checkPermission(Permission perm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        throws AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        boolean dumpDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (perm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throw new NullPointerException("permission can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (getDebug() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            // If "codebase" is not specified, we dump the info by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            dumpDebug = !Debug.isOn("codebase=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (!dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                // If "codebase" is specified, only dump if the specified code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                // value is in the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                for (int i = 0; context != null && i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    if (context[i].getCodeSource() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        context[i].getCodeSource().getLocation() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        Debug.isOn("codebase=" + context[i].getCodeSource().getLocation().toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        dumpDebug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            dumpDebug &= !Debug.isOn("permission=") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                Debug.isOn("permission=" + perm.getClass().getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (dumpDebug && Debug.isOn("stack")) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   419
                Thread.dumpStack();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (dumpDebug && Debug.isOn("domain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    debug.println("domain (context is null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    for (int i=0; i< context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        debug.println("domain "+i+" "+context[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * iterate through the ProtectionDomains in the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * Stop at the first one that doesn't allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * requested permission (throwing an exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /* if ctxt is null, all we had on the stack were system domains,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
           or the first domain was a Privileged system domain. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
           is to make the common case for system code very fast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   444
        if (context == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   445
            checkPermission2(perm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   447
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        for (int i=0; i< context.length; i++) {
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 31538
diff changeset
   450
            if (context[i] != null && !context[i].impliesWithAltFilePerm(perm)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    debug.println("access denied " + perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
97
5c029f131927 6648816: REGRESSION: setting -Djava.security.debug=failure result in NPE in ACC
xuelei
parents: 2
diff changeset
   455
                if (Debug.isOn("failure") && debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    // Want to make sure this is always displayed for failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    // but do not want to display again if already displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    // above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    if (!dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        debug.println("access denied " + perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   462
                    Thread.dumpStack();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    final ProtectionDomain pd = context[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    final Debug db = debug;
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 29492
diff changeset
   465
                    AccessController.doPrivileged (new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            db.println("domain that failed "+pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                throw new AccessControlException("access denied "+perm, perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // allow if all of them allowed access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            debug.println("access allowed "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   481
        checkPermission2(perm);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   482
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   483
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   484
    /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   485
     * Check the domains associated with the limited privilege scope.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   486
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   487
    private void checkPermission2(Permission perm) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   488
        if (!isLimited) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   489
            return;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   490
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   491
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   492
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   493
         * Check the doPrivileged() context parameter, if present.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   494
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   495
        if (privilegedContext != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   496
            privilegedContext.checkPermission2(perm);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   497
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   498
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   499
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   500
         * Ignore the limited permissions and parent fields of a wrapper
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   501
         * context since they were already carried down into the unwrapped
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   502
         * context.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   503
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   504
        if (isWrapped) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   505
            return;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   506
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   507
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   508
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   509
         * Try to match any limited privilege scope.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   510
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   511
        if (permissions != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   512
            Class<?> permClass = perm.getClass();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   513
            for (int i=0; i < permissions.length; i++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   514
                Permission limit = permissions[i];
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   515
                if (limit.getClass().equals(permClass) && limit.implies(perm)) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   516
                    return;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   517
                }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   518
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   519
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   520
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   521
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   522
         * Check the limited privilege scope up the call stack or the inherited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   523
         * parent thread call stack of this ACC.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   524
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   525
        if (parent != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   526
            /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   527
             * As an optimization, if the parent context is the inherited call
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   528
             * stack context from a parent thread then checking the protection
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   529
             * domains of the parent context is redundant since they have
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   530
             * already been merged into the child thread's context by
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   531
             * optimize(). When parent is set to an inherited context this
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   532
             * context was not directly created by a limited scope
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   533
             * doPrivileged() and it does not have its own limited permissions.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   534
             */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   535
            if (permissions == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   536
                parent.checkPermission2(perm);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   537
            } else {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   538
                parent.checkPermission(perm);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   539
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   540
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Take the stack-based context (this) and combine it with the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   545
     * privileged or inherited context, if need be. Any limited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   546
     * privilege scope is flagged regardless of whether the assigned
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   547
     * context comes from an immediately enclosing limited doPrivileged().
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   548
     * The limited privilege scope can indirectly flow from the inherited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   549
     * parent thread or an assigned context previously captured by getContext().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    AccessControlContext optimize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // the assigned (privileged or inherited) context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        AccessControlContext acc;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   554
        DomainCombiner combiner = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   555
        AccessControlContext parent = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   556
        Permission[] permissions = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   557
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (isPrivileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            acc = privilegedContext;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   560
            if (acc != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   561
                /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   562
                 * If the context is from a limited scope doPrivileged() then
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   563
                 * copy the permissions and parent fields out of the wrapper
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   564
                 * context that was created to hold them.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   565
                 */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   566
                if (acc.isWrapped) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   567
                    permissions = acc.permissions;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   568
                    parent = acc.parent;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   569
                }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   570
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            acc = AccessController.getInheritedAccessControlContext();
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   573
            if (acc != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   574
                /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   575
                 * If the inherited context is constrained by a limited scope
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   576
                 * doPrivileged() then set it as our parent so we will process
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   577
                 * the non-domain-related state.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   578
                 */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   579
                if (acc.isLimited) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   580
                    parent = acc;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   581
                }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   582
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // this.context could be null if only system code is on the stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // in that case, ignore the stack context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        boolean skipStack = (context == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // acc.context could be null if only system code was involved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // in that case, ignore the assigned context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        boolean skipAssigned = (acc == null || acc.context == null);
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   592
        ProtectionDomain[] assigned = (skipAssigned) ? null : acc.context;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   593
        ProtectionDomain[] pd;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   594
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   595
        // if there is no enclosing limited privilege scope on the stack or
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   596
        // inherited from a parent thread
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   597
        boolean skipLimited = ((acc == null || !acc.isWrapped) && parent == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (acc != null && acc.combiner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            // let the assigned acc's combiner do its thing
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   601
            if (getDebug() != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   602
                debug.println("AccessControlContext invoking the Combiner");
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   603
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   604
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   605
            // No need to clone current and assigned.context
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   606
            // combine() will not update them
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   607
            combiner = acc.combiner;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   608
            pd = combiner.combine(context, assigned);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   609
        } else {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   610
            if (skipStack) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   611
                if (skipAssigned) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   612
                    calculateFields(acc, parent, permissions);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   613
                    return this;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   614
                } else if (skipLimited) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   615
                    return acc;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   616
                }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   617
            } else if (assigned != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   618
                if (skipLimited) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   619
                    // optimization: if there is a single stack domain and
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   620
                    // that domain is already in the assigned context; no
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   621
                    // need to combine
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   622
                    if (context.length == 1 && context[0] == assigned[0]) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   623
                        return acc;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   624
                    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   625
                }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   626
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   627
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   628
            pd = combine(context, assigned);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   629
            if (skipLimited && !skipAssigned && pd == assigned) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   630
                return acc;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   631
            } else if (skipAssigned && pd == context) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   632
                calculateFields(acc, parent, permissions);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   633
                return this;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   634
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   637
        // Reuse existing ACC
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   638
        this.context = pd;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   639
        this.combiner = combiner;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   640
        this.isPrivileged = false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   641
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   642
        calculateFields(acc, parent, permissions);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   643
        return this;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   644
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   645
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   647
    /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   648
     * Combine the current (stack) and assigned domains.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   649
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   650
    private static ProtectionDomain[] combine(ProtectionDomain[] current,
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   651
        ProtectionDomain[] assigned) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   653
        // current could be null if only system code is on the stack;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   654
        // in that case, ignore the stack context
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   655
        boolean skipStack = (current == null);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   656
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   657
        // assigned could be null if only system code was involved;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   658
        // in that case, ignore the assigned context
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   659
        boolean skipAssigned = (assigned == null);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   660
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   661
        int slen = (skipStack) ? 0 : current.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        // optimization: if there is no assigned context and the stack length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // is less then or equal to two; there is no reason to compress the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // stack context, it already is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (skipAssigned && slen <= 2) {
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   667
            return current;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   670
        int n = (skipAssigned) ? 0 : assigned.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // now we combine both of them, and create a new context
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   673
        ProtectionDomain[] pd = new ProtectionDomain[slen + n];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // first copy in the assigned context domains, no need to compress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (!skipAssigned) {
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   677
            System.arraycopy(assigned, 0, pd, 0, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        // now add the stack context domains, discarding nulls and duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    outer:
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   682
        for (int i = 0; i < slen; i++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   683
            ProtectionDomain sd = current[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (sd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                for (int j = 0; j < n; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    if (sd == pd[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        continue outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                pd[n++] = sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // if length isn't equal, we need to shorten the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (n != pd.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            // optimization: if we didn't really combine anything
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   697
            if (!skipAssigned && n == assigned.length) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   698
                return assigned;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            } else if (skipAssigned && n == slen) {
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   700
                return current;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30033
diff changeset
   702
            ProtectionDomain[] tmp = new ProtectionDomain[n];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            System.arraycopy(pd, 0, tmp, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            pd = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   707
        return pd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   711
    /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   712
     * Calculate the additional domains that could potentially be reached via
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   713
     * limited privilege scope. Mark the context as being subject to limited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   714
     * privilege scope unless the reachable domains (if any) are already
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   715
     * contained in this domain context (in which case any limited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   716
     * privilege scope checking would be redundant).
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   717
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   718
    private void calculateFields(AccessControlContext assigned,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   719
        AccessControlContext parent, Permission[] permissions)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   720
    {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   721
        ProtectionDomain[] parentLimit = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   722
        ProtectionDomain[] assignedLimit = null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   723
        ProtectionDomain[] newLimit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   725
        parentLimit = (parent != null)? parent.limitedContext: null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   726
        assignedLimit = (assigned != null)? assigned.limitedContext: null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   727
        newLimit = combine(parentLimit, assignedLimit);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   728
        if (newLimit != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   729
            if (context == null || !containsAllPDs(newLimit, context)) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   730
                this.limitedContext = newLimit;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   731
                this.permissions = permissions;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   732
                this.parent = parent;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   733
                this.isLimited = true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   734
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   735
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   736
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * Checks two AccessControlContext objects for equality.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 29027
diff changeset
   741
     * Checks that {@code obj} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * an AccessControlContext and has the same set of ProtectionDomains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * as this context.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 29027
diff changeset
   744
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param obj the object we are testing for equality with this object.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 29027
diff changeset
   746
     * @return true if {@code obj} is an AccessControlContext, and has the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * same set of ProtectionDomains as this context, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if (! (obj instanceof AccessControlContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        AccessControlContext that = (AccessControlContext) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   758
        if (!equalContext(that))
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   759
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   761
        if (!equalLimitedContext(that))
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   762
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   763
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   764
        return true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   765
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   767
    /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   768
     * Compare for equality based on state that is free of limited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   769
     * privilege complications.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   770
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   771
    private boolean equalContext(AccessControlContext that) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   772
        if (!equalPDs(this.context, that.context))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   775
        if (this.combiner == null && that.combiner != null)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   776
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   777
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   778
        if (this.combiner != null && !this.combiner.equals(that.combiner))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   781
        return true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   782
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   784
    private boolean equalPDs(ProtectionDomain[] a, ProtectionDomain[] b) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   785
        if (a == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   786
            return (b == null);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   787
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   788
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   789
        if (b == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   792
        if (!(containsAllPDs(a, b) && containsAllPDs(b, a)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   798
    /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   799
     * Compare for equality based on state that is captured during a
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   800
     * call to AccessController.getContext() when a limited privilege
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   801
     * scope is in effect.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   802
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   803
    private boolean equalLimitedContext(AccessControlContext that) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   804
        if (that == null)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   805
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   806
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   807
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   808
         * If neither instance has limited privilege scope then we're done.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   809
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   810
        if (!this.isLimited && !that.isLimited)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   811
            return true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   812
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   813
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   814
         * If only one instance has limited privilege scope then we're done.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   815
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   816
         if (!(this.isLimited && that.isLimited))
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   817
             return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   818
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   819
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   820
         * Wrapped instances should never escape outside the implementation
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   821
         * this class and AccessController so this will probably never happen
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   822
         * but it only makes any sense to compare if they both have the same
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   823
         * isWrapped state.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   824
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   825
        if ((this.isWrapped && !that.isWrapped) ||
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   826
            (!this.isWrapped && that.isWrapped)) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   827
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   828
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   829
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   830
        if (this.permissions == null && that.permissions != null)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   831
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   832
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   833
        if (this.permissions != null && that.permissions == null)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   834
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   835
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   836
        if (!(this.containsAllLimits(that) && that.containsAllLimits(this)))
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   837
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   838
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   839
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   840
         * Skip through any wrapped contexts.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   841
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   842
        AccessControlContext thisNextPC = getNextPC(this);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   843
        AccessControlContext thatNextPC = getNextPC(that);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   844
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   845
        /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   846
         * The protection domains and combiner of a privilegedContext are
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   847
         * not relevant because they have already been included in the context
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   848
         * of this instance by optimize() so we only care about any limited
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   849
         * privilege state they may have.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   850
         */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   851
        if (thisNextPC == null && thatNextPC != null && thatNextPC.isLimited)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   852
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   853
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   854
        if (thisNextPC != null && !thisNextPC.equalLimitedContext(thatNextPC))
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   855
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   856
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   857
        if (this.parent == null && that.parent != null)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   858
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   859
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   860
        if (this.parent != null && !this.parent.equals(that.parent))
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   861
            return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   862
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   863
        return true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   864
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   865
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   866
    /*
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   867
     * Follow the privilegedContext link making our best effort to skip
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   868
     * through any wrapper contexts.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   869
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   870
    private static AccessControlContext getNextPC(AccessControlContext acc) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   871
        while (acc != null && acc.privilegedContext != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   872
            acc = acc.privilegedContext;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   873
            if (!acc.isWrapped)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   874
                return acc;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   875
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   876
        return null;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   877
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   878
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   879
    private static boolean containsAllPDs(ProtectionDomain[] thisContext,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   880
        ProtectionDomain[] thatContext) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        boolean match = false;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   882
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        // ProtectionDomains within an ACC currently cannot be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        // and this is enforced by the constructor and the various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        // optimize methods. However, historically this logic made attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        // to support the notion of a null PD and therefore this logic continues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        // to support that notion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        ProtectionDomain thisPd;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   890
        for (int i = 0; i < thisContext.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            match = false;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   892
            if ((thisPd = thisContext[i]) == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   893
                for (int j = 0; (j < thatContext.length) && !match; j++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   894
                    match = (thatContext[j] == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   897
                Class<?> thisPdClass = thisPd.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                ProtectionDomain thatPd;
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   899
                for (int j = 0; (j < thatContext.length) && !match; j++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   900
                    thatPd = thatContext[j];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    // Class check required to avoid PD exposure (4285406)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    match = (thatPd != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                        thisPdClass == thatPd.getClass() && thisPd.equals(thatPd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        return match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   911
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   912
    private boolean containsAllLimits(AccessControlContext that) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   913
        boolean match = false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   914
        Permission thisPerm;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   915
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   916
        if (this.permissions == null && that.permissions == null)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   917
            return true;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   918
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   919
        for (int i = 0; i < this.permissions.length; i++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   920
            Permission limit = this.permissions[i];
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   921
            Class <?> limitClass = limit.getClass();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   922
            match = false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   923
            for (int j = 0; (j < that.permissions.length) && !match; j++) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   924
                Permission perm = that.permissions[j];
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   925
                match = (limitClass.equals(perm.getClass()) &&
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   926
                    limit.equals(perm));
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   927
            }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   928
            if (!match) return false;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   929
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   930
        return match;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   931
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   932
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   933
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * Returns the hash code value for this context. The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * is computed by exclusive or-ing the hash code of all the protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * domains in the context together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @return a hash code value for this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        for (int i =0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            if (context[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                hashCode ^= context[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 10336
diff changeset
   952
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
}