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