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