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