Skip to content

Commit 940aa7c

Browse files
committed
8344397: Remove Security Manager dependencies from java.security and sun.security packages
Reviewed-by: rriggs, hchao, weijun, alanb
1 parent 3d0d0e6 commit 940aa7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+407
-1437
lines changed

src/java.base/share/classes/java/lang/Class.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.lang.reflect.TypeVariable;
5454
import java.lang.constant.Constable;
5555
import java.net.URL;
56+
import java.security.AllPermission;
5657
import java.security.Permissions;
5758
import java.security.ProtectionDomain;
5859
import java.util.ArrayList;
@@ -89,7 +90,6 @@
8990
import sun.reflect.generics.repository.MethodRepository;
9091
import sun.reflect.generics.repository.ConstructorRepository;
9192
import sun.reflect.generics.scope.ClassScope;
92-
import sun.security.util.SecurityConstants;
9393
import sun.reflect.annotation.*;
9494
import sun.reflect.misc.ReflectUtil;
9595

@@ -2720,7 +2720,7 @@ private static class Holder {
27202720
private static final ProtectionDomain allPermDomain;
27212721
static {
27222722
Permissions perms = new Permissions();
2723-
perms.add(SecurityConstants.ALL_PERMISSION);
2723+
perms.add(new AllPermission());
27242724
allPermDomain = new ProtectionDomain(null, perms);
27252725
}
27262726
}

src/java.base/share/classes/java/security/AccessControlContext.java

-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@
4444
public final class AccessControlContext {
4545

4646
private ProtectionDomain[] context;
47-
// isPrivileged and isAuthorized are referenced by the VM - do not remove
48-
// or change their names
49-
private boolean isPrivileged;
50-
private boolean isAuthorized = false;
51-
52-
// Note: This field is directly used by the virtual machine
53-
// native codes. Don't touch it.
54-
private AccessControlContext privilegedContext;
5547

5648
@SuppressWarnings("removal")
5749
private DomainCombiner combiner = null;

src/java.base/share/classes/java/security/Provider.java

+18-39
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,6 @@ public String toString() {
345345
return name + " version " + versionStr;
346346
}
347347

348-
/*
349-
* override the following methods to ensure that provider
350-
* information can only be changed if the caller has the appropriate
351-
* permissions.
352-
*/
353-
354348
/**
355349
* Clears this {@code Provider} so that it no longer contains the properties
356350
* used to look up facilities implemented by the {@code Provider}.
@@ -359,7 +353,7 @@ public String toString() {
359353
*/
360354
@Override
361355
public synchronized void clear() {
362-
check("clearProviderProperties."+name);
356+
checkInitialized();
363357
if (debug != null) {
364358
debug.println("Remove " + name + " provider properties");
365359
}
@@ -376,7 +370,7 @@ public synchronized void clear() {
376370
*/
377371
@Override
378372
public synchronized void load(InputStream inStream) throws IOException {
379-
check("putProviderProperty."+name);
373+
checkInitialized();
380374
if (debug != null) {
381375
debug.println("Load " + name + " provider properties");
382376
}
@@ -394,7 +388,7 @@ public synchronized void load(InputStream inStream) throws IOException {
394388
*/
395389
@Override
396390
public synchronized void putAll(Map<?,?> t) {
397-
check("putProviderProperty."+name);
391+
checkInitialized();
398392
if (debug != null) {
399393
debug.println("Put all " + name + " provider properties");
400394
}
@@ -461,7 +455,7 @@ public Collection<Object> values() {
461455
*/
462456
@Override
463457
public synchronized Object put(Object key, Object value) {
464-
check("putProviderProperty."+name);
458+
checkInitialized();
465459
if (debug != null) {
466460
debug.println("Set " + name + " provider property [" +
467461
key + "/" + value +"]");
@@ -478,7 +472,7 @@ public synchronized Object put(Object key, Object value) {
478472
*/
479473
@Override
480474
public synchronized Object putIfAbsent(Object key, Object value) {
481-
check("putProviderProperty."+name);
475+
checkInitialized();
482476
if (debug != null) {
483477
debug.println("Set " + name + " provider property [" +
484478
key + "/" + value +"]");
@@ -494,7 +488,7 @@ public synchronized Object putIfAbsent(Object key, Object value) {
494488
*/
495489
@Override
496490
public synchronized Object remove(Object key) {
497-
check("removeProviderProperty."+name);
491+
checkInitialized();
498492
if (debug != null) {
499493
debug.println("Remove " + name + " provider property " + key);
500494
}
@@ -509,7 +503,7 @@ public synchronized Object remove(Object key) {
509503
*/
510504
@Override
511505
public synchronized boolean remove(Object key, Object value) {
512-
check("removeProviderProperty."+name);
506+
checkInitialized();
513507
if (debug != null) {
514508
debug.println("Remove " + name + " provider property " + key);
515509
}
@@ -525,7 +519,7 @@ public synchronized boolean remove(Object key, Object value) {
525519
@Override
526520
public synchronized boolean replace(Object key, Object oldValue,
527521
Object newValue) {
528-
check("putProviderProperty." + name);
522+
checkInitialized();
529523
if (debug != null) {
530524
debug.println("Replace " + name + " provider property " + key);
531525
}
@@ -540,7 +534,7 @@ public synchronized boolean replace(Object key, Object oldValue,
540534
*/
541535
@Override
542536
public synchronized Object replace(Object key, Object value) {
543-
check("putProviderProperty." + name);
537+
checkInitialized();
544538
if (debug != null) {
545539
debug.println("Replace " + name + " provider property " + key);
546540
}
@@ -558,7 +552,7 @@ public synchronized Object replace(Object key, Object value) {
558552
@Override
559553
public synchronized void replaceAll(BiFunction<? super Object,
560554
? super Object, ? extends Object> function) {
561-
check("putProviderProperty." + name);
555+
checkInitialized();
562556
if (debug != null) {
563557
debug.println("ReplaceAll " + name + " provider property ");
564558
}
@@ -575,8 +569,7 @@ public synchronized void replaceAll(BiFunction<? super Object,
575569
@Override
576570
public synchronized Object compute(Object key, BiFunction<? super Object,
577571
? super Object, ? extends Object> remappingFunction) {
578-
check("putProviderProperty." + name);
579-
check("removeProviderProperty." + name);
572+
checkInitialized();
580573
if (debug != null) {
581574
debug.println("Compute " + name + " provider property " + key);
582575
}
@@ -594,8 +587,7 @@ public synchronized Object compute(Object key, BiFunction<? super Object,
594587
@Override
595588
public synchronized Object computeIfAbsent(Object key,
596589
Function<? super Object, ? extends Object> mappingFunction) {
597-
check("putProviderProperty." + name);
598-
check("removeProviderProperty." + name);
590+
checkInitialized();
599591
if (debug != null) {
600592
debug.println("ComputeIfAbsent " + name + " provider property " +
601593
key);
@@ -613,8 +605,7 @@ public synchronized Object computeIfAbsent(Object key,
613605
public synchronized Object computeIfPresent(Object key,
614606
BiFunction<? super Object, ? super Object, ? extends Object>
615607
remappingFunction) {
616-
check("putProviderProperty." + name);
617-
check("removeProviderProperty." + name);
608+
checkInitialized();
618609
if (debug != null) {
619610
debug.println("ComputeIfPresent " + name + " provider property " +
620611
key);
@@ -635,8 +626,7 @@ public synchronized Object computeIfPresent(Object key,
635626
public synchronized Object merge(Object key, Object value,
636627
BiFunction<? super Object, ? super Object, ? extends Object>
637628
remappingFunction) {
638-
check("putProviderProperty." + name);
639-
check("removeProviderProperty." + name);
629+
checkInitialized();
640630
if (debug != null) {
641631
debug.println("Merge " + name + " provider property " + key);
642632
}
@@ -694,15 +684,6 @@ private void checkInitialized() {
694684
}
695685
}
696686

697-
private void check(String directive) {
698-
checkInitialized();
699-
@SuppressWarnings("removal")
700-
SecurityManager security = System.getSecurityManager();
701-
if (security != null) {
702-
security.checkSecurityAccess(directive);
703-
}
704-
}
705-
706687
// legacyMap changed since last call to getServices()
707688
private transient volatile boolean legacyChanged;
708689
// serviceMap changed since last call to getServices()
@@ -789,8 +770,6 @@ private static boolean checkLegacy(Object key) {
789770

790771
/**
791772
* Copies all the mappings from the specified Map to this provider.
792-
* Internal method to be called AFTER the security check has been
793-
* performed.
794773
*/
795774
private void implPutAll(Map<?,?> t) {
796775
for (Map.Entry<?,?> e : t.entrySet()) {
@@ -1239,7 +1218,7 @@ public Set<Service> getServices() {
12391218
* @since 1.5
12401219
*/
12411220
protected void putService(Service s) {
1242-
check("putProviderProperty." + name);
1221+
checkInitialized();
12431222
if (debug != null) {
12441223
debug.println(name + ".putService(): " + s);
12451224
}
@@ -1303,7 +1282,7 @@ Service getDefaultSecureRandomService() {
13031282
private void putPropertyStrings(Service s) {
13041283
String type = s.getType();
13051284
String algorithm = s.getAlgorithm();
1306-
// use super() to avoid permission check and other processing
1285+
// use super() to avoid other processing
13071286
super.put(type + "." + algorithm, s.getClassName());
13081287
for (String alias : s.getAliases()) {
13091288
super.put(ALIAS_PREFIX + type + "." + alias, algorithm);
@@ -1321,7 +1300,7 @@ private void putPropertyStrings(Service s) {
13211300
private void removePropertyStrings(Service s) {
13221301
String type = s.getType();
13231302
String algorithm = s.getAlgorithm();
1324-
// use super() to avoid permission check and other processing
1303+
// use super() to avoid other processing
13251304
super.remove(type + "." + algorithm);
13261305
for (String alias : s.getAliases()) {
13271306
super.remove(ALIAS_PREFIX + type + "." + alias);
@@ -1346,7 +1325,7 @@ private void removePropertyStrings(Service s) {
13461325
* @since 1.5
13471326
*/
13481327
protected void removeService(Service s) {
1349-
check("removeProviderProperty." + name);
1328+
checkInitialized();
13501329
if (debug != null) {
13511330
debug.println(name + ".removeService(): " + s);
13521331
}

src/java.base/share/classes/java/security/SecureRandom.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,7 @@ private static final class StrongPatternHolder {
942942
public static SecureRandom getInstanceStrong()
943943
throws NoSuchAlgorithmException {
944944

945-
@SuppressWarnings("removal")
946-
String property = AccessController.doPrivileged(
947-
(PrivilegedAction<String>) () -> Security.getProperty(
948-
"securerandom.strongAlgorithms"));
949-
945+
String property = Security.getProperty("securerandom.strongAlgorithms");
950946
if (property == null || property.isEmpty()) {
951947
throw new NoSuchAlgorithmException(
952948
"Null/empty securerandom.strongAlgorithms Security Property");

src/java.base/share/classes/java/security/Security.java

+2-40
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,7 @@ private static void debugLoad(boolean start, Object source) {
312312
}
313313

314314
static {
315-
// doPrivileged here because there are multiple
316-
// things in initialize that might require privs.
317-
// (the FileInputStream call and the File.exists call, etc)
318-
@SuppressWarnings("removal")
319-
var dummy = AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
320-
initialize();
321-
return null;
322-
});
315+
initialize();
323316
// Set up JavaSecurityPropertiesAccess in SharedSecrets
324317
SharedSecrets.setJavaSecurityPropertiesAccess(new JavaSecurityPropertiesAccess() {
325318
@Override
@@ -475,15 +468,13 @@ public static String getAlgorithmProperty(String algName,
475468
*/
476469
public static synchronized int insertProviderAt(Provider provider,
477470
int position) {
478-
String providerName = provider.getName();
479-
checkInsertProvider(providerName);
480471
ProviderList list = Providers.getFullProviderList();
481472
ProviderList newList = ProviderList.insertAt(list, provider, position - 1);
482473
if (list == newList) {
483474
return -1;
484475
}
485476
Providers.setProviderList(newList);
486-
return newList.getIndex(providerName) + 1;
477+
return newList.getIndex(provider.getName()) + 1;
487478
}
488479

489480
/**
@@ -527,7 +518,6 @@ public static int addProvider(Provider provider) {
527518
* @see #addProvider
528519
*/
529520
public static synchronized void removeProvider(String name) {
530-
check("removeProvider." + name);
531521
ProviderList list = Providers.getFullProviderList();
532522
ProviderList newList = ProviderList.remove(list, name);
533523
Providers.setProviderList(newList);
@@ -822,7 +812,6 @@ static Object[] getImpl(String algorithm, String type, Provider provider,
822812
*/
823813
public static String getProperty(String key) {
824814
SecPropLoader.checkReservedKey(key);
825-
check("getProperty." + key);
826815
String name = props.getProperty(key);
827816
if (name != null)
828817
name = name.trim(); // could be a class name with trailing ws
@@ -845,7 +834,6 @@ public static String getProperty(String key) {
845834
*/
846835
public static void setProperty(String key, String datum) {
847836
SecPropLoader.checkReservedKey(key);
848-
check("setProperty." + key);
849837
props.put(key, datum);
850838

851839
SecurityPropertyModificationEvent spe = new SecurityPropertyModificationEvent();
@@ -859,32 +847,6 @@ public static void setProperty(String key, String datum) {
859847
}
860848
}
861849

862-
private static void check(String directive) {
863-
@SuppressWarnings("removal")
864-
SecurityManager security = System.getSecurityManager();
865-
if (security != null) {
866-
security.checkSecurityAccess(directive);
867-
}
868-
}
869-
870-
private static void checkInsertProvider(String name) {
871-
@SuppressWarnings("removal")
872-
SecurityManager security = System.getSecurityManager();
873-
if (security != null) {
874-
try {
875-
security.checkSecurityAccess("insertProvider");
876-
} catch (SecurityException se1) {
877-
try {
878-
security.checkSecurityAccess("insertProvider." + name);
879-
} catch (SecurityException se2) {
880-
// throw first exception, but add second to suppressed
881-
se1.addSuppressed(se2);
882-
throw se1;
883-
}
884-
}
885-
}
886-
}
887-
888850
private static class Criteria {
889851
private final String serviceName;
890852
private final String algName;

0 commit comments

Comments
 (0)