Skip to content

Commit a62279c

Browse files
committed
8344235: Revisit SecurityManager usage in java.logging after JEP 486 and JEP 491 integration
Reviewed-by: jpai
1 parent 18df6fd commit a62279c

File tree

17 files changed

+206
-907
lines changed

17 files changed

+206
-907
lines changed

src/java.base/share/classes/jdk/internal/logger/BootstrapLogger.java

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,9 +25,6 @@
2525

2626
package jdk.internal.logger;
2727

28-
import java.security.AccessControlContext;
29-
import java.security.AccessController;
30-
import java.security.PrivilegedAction;
3128
import java.util.HashMap;
3229
import java.util.Iterator;
3330
import java.util.Map;
@@ -131,15 +128,8 @@ private static ExecutorService getExecutor() {
131128
@Override
132129
public Thread newThread(Runnable r) {
133130
ExecutorService owner = getExecutor();
134-
@SuppressWarnings("removal")
135-
Thread thread = AccessController.doPrivileged(new PrivilegedAction<Thread>() {
136-
@Override
137-
public Thread run() {
138-
Thread t = InnocuousThread.newThread(new BootstrapMessageLoggerTask(owner, r));
139-
t.setName("BootstrapMessageLoggerTask-"+t.getName());
140-
return t;
141-
}
142-
}, null, new RuntimePermission("enableContextClassLoaderOverride"));
131+
Thread thread = InnocuousThread.newThread(new BootstrapMessageLoggerTask(owner, r));
132+
thread.setName("BootstrapMessageLoggerTask-" + thread.getName());
143133
thread.setDaemon(true);
144134
return thread;
145135
}
@@ -269,8 +259,6 @@ static final class LogEvent {
269259
// the parameters etc... we need to store the context of the
270260
// caller who logged the message - so that we can reuse it when
271261
// we finally log the message.
272-
@SuppressWarnings("removal")
273-
final AccessControlContext acc;
274262

275263
// The next event in the queue
276264
LogEvent next;
@@ -279,7 +267,6 @@ static final class LogEvent {
279267
private LogEvent(BootstrapLogger bootstrap, Level level,
280268
ResourceBundle bundle, String msg,
281269
Throwable thrown, Object[] params) {
282-
this.acc = AccessController.getContext();
283270
this.timeMillis = System.currentTimeMillis();
284271
this.nanoAdjustment = VM.getNanoTimeAdjustment(timeMillis);
285272
this.level = level;
@@ -298,7 +285,6 @@ private LogEvent(BootstrapLogger bootstrap, Level level,
298285
private LogEvent(BootstrapLogger bootstrap, Level level,
299286
Supplier<String> msgSupplier,
300287
Throwable thrown, Object[] params) {
301-
this.acc = AccessController.getContext();
302288
this.timeMillis = System.currentTimeMillis();
303289
this.nanoAdjustment = VM.getNanoTimeAdjustment(timeMillis);
304290
this.level = level;
@@ -319,7 +305,6 @@ private LogEvent(BootstrapLogger bootstrap,
319305
String sourceClass, String sourceMethod,
320306
ResourceBundle bundle, String msg,
321307
Throwable thrown, Object[] params) {
322-
this.acc = AccessController.getContext();
323308
this.timeMillis = System.currentTimeMillis();
324309
this.nanoAdjustment = VM.getNanoTimeAdjustment(timeMillis);
325310
this.level = null;
@@ -340,7 +325,6 @@ private LogEvent(BootstrapLogger bootstrap,
340325
String sourceClass, String sourceMethod,
341326
Supplier<String> msgSupplier,
342327
Throwable thrown, Object[] params) {
343-
this.acc = AccessController.getContext();
344328
this.timeMillis = System.currentTimeMillis();
345329
this.nanoAdjustment = VM.getNanoTimeAdjustment(timeMillis);
346330
this.level = null;
@@ -444,20 +428,12 @@ static LogEvent valueOf(BootstrapLogger bootstrap, Level level,
444428
Objects.requireNonNull(level),
445429
Objects.requireNonNull(msgSupplier), null, null);
446430
}
447-
@SuppressWarnings("removal")
431+
448432
static void log(LogEvent log, Logger logger) {
449-
final SecurityManager sm = System.getSecurityManager();
450433
// not sure we can actually use lambda here. We may need to create
451434
// an anonymous class. Although if we reach here, then it means
452435
// the VM is booted.
453-
if (sm == null || log.acc == null) {
454-
BootstrapExecutors.submit(() -> log.log(logger));
455-
} else {
456-
BootstrapExecutors.submit(() ->
457-
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
458-
log.log(logger); return null;
459-
}, log.acc));
460-
}
436+
BootstrapExecutors.submit(() -> log.log(logger));
461437
}
462438

463439
// non default methods from PlatformLogger.Bridge interface
@@ -510,20 +486,9 @@ static LogEvent valueOf(BootstrapLogger bootstrap, PlatformLogger.Level level,
510486
Objects.requireNonNull(level), sourceClass,
511487
sourceMethod, msgSupplier, thrown, null);
512488
}
513-
@SuppressWarnings("removal")
489+
514490
static void log(LogEvent log, PlatformLogger.Bridge logger) {
515-
final SecurityManager sm = System.getSecurityManager();
516-
if (sm == null || log.acc == null) {
517-
BootstrapExecutors.submit(() -> log.log(logger));
518-
} else {
519-
// not sure we can actually use lambda here. We may need to create
520-
// an anonymous class. Although if we reach here, then it means
521-
// the VM is booted.
522-
BootstrapExecutors.submit(() ->
523-
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
524-
log.log(logger); return null;
525-
}, log.acc));
526-
}
491+
BootstrapExecutors.submit(() -> log.log(logger));
527492
}
528493

529494
static void log(LogEvent event) {
@@ -897,37 +862,32 @@ private LoggingBackend(boolean useLoggerFinder) {
897862
// We do not want this field to get initialized if VM.isBooted() is false.
898863
@SuppressWarnings("removal")
899864
private static final class DetectBackend {
900-
static final LoggingBackend detectedBackend;
901-
static {
902-
detectedBackend = AccessController.doPrivileged(new PrivilegedAction<LoggingBackend>() {
903-
@Override
904-
public LoggingBackend run() {
905-
final Iterator<LoggerFinder> iterator =
906-
ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader())
865+
static final LoggingBackend detectedBackend = detectBackend();
866+
867+
static LoggingBackend detectBackend() {
868+
final Iterator<LoggerFinder> iterator =
869+
ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader())
907870
.iterator();
908-
if (iterator.hasNext()) {
909-
return LoggingBackend.CUSTOM; // Custom Logger Provider is registered
910-
}
911-
// No custom logger provider: we will be using the default
912-
// backend.
913-
final Iterator<DefaultLoggerFinder> iterator2 =
914-
ServiceLoader.loadInstalled(DefaultLoggerFinder.class)
871+
if (iterator.hasNext()) {
872+
return LoggingBackend.CUSTOM; // Custom Logger Provider is registered
873+
}
874+
// No custom logger provider: we will be using the default
875+
// backend.
876+
final Iterator<DefaultLoggerFinder> iterator2 =
877+
ServiceLoader.loadInstalled(DefaultLoggerFinder.class)
915878
.iterator();
916-
if (iterator2.hasNext()) {
917-
// LoggingProviderImpl is registered. The default
918-
// implementation is java.util.logging
919-
String cname = System.getProperty("java.util.logging.config.class");
920-
String fname = System.getProperty("java.util.logging.config.file");
921-
return (cname != null || fname != null)
922-
? LoggingBackend.JUL_WITH_CONFIG
923-
: LoggingBackend.JUL_DEFAULT;
924-
} else {
925-
// SimpleConsoleLogger is used
926-
return LoggingBackend.NONE;
927-
}
928-
}
929-
});
930-
879+
if (iterator2.hasNext()) {
880+
// LoggingProviderImpl is registered. The default
881+
// implementation is java.util.logging
882+
String cname = System.getProperty("java.util.logging.config.class");
883+
String fname = System.getProperty("java.util.logging.config.file");
884+
return (cname != null || fname != null)
885+
? LoggingBackend.JUL_WITH_CONFIG
886+
: LoggingBackend.JUL_DEFAULT;
887+
} else {
888+
// SimpleConsoleLogger is used
889+
return LoggingBackend.NONE;
890+
}
931891
}
932892
}
933893

src/java.base/share/classes/jdk/internal/logger/DefaultLoggerFinder.java

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,8 +36,6 @@
3636
import java.lang.System.LoggerFinder;
3737
import java.lang.System.Logger;
3838
import java.lang.ref.ReferenceQueue;
39-
import java.security.AccessController;
40-
import java.security.PrivilegedAction;
4139
import java.util.Collection;
4240
import java.util.ResourceBundle;
4341

@@ -70,7 +68,7 @@
7068
* that provides the necessary configuration.
7169
*
7270
* @apiNote Programmers are not expected to call this class directly.
73-
* Instead they should rely on the static methods defined by {@link
71+
* Instead, they should rely on the static methods defined by {@link
7472
* java.lang.System java.lang.System} or {@link sun.util.logging.PlatformLogger
7573
* sun.util.logging.PlatformLogger}.
7674
*
@@ -81,30 +79,12 @@
8179
*/
8280
public class DefaultLoggerFinder extends LoggerFinder {
8381

84-
static final RuntimePermission LOGGERFINDER_PERMISSION =
85-
new RuntimePermission("loggerFinder");
86-
8782
/**
8883
* Creates a new instance of DefaultLoggerFinder.
89-
* @throws SecurityException if the calling code does not have the
90-
* {@code RuntimePermission("loggerFinder")}
9184
*/
9285
protected DefaultLoggerFinder() {
93-
this(checkPermission());
94-
}
95-
96-
private DefaultLoggerFinder(Void unused) {
97-
// nothing to do.
9886
}
9987

100-
private static Void checkPermission() {
101-
@SuppressWarnings("removal")
102-
final SecurityManager sm = System.getSecurityManager();
103-
if (sm != null) {
104-
sm.checkPermission(LOGGERFINDER_PERMISSION);
105-
}
106-
return null;
107-
}
10888

10989
// SharedLoggers is a default cache of loggers used when JUL is not
11090
// present - in that case we use instances of SimpleConsoleLogger which
@@ -139,23 +119,14 @@ synchronized Logger get(Function<String, Logger> loggerSupplier, final String na
139119
static final SharedLoggers application = new SharedLoggers();
140120
}
141121

142-
@SuppressWarnings("removal")
143122
public static boolean isSystem(Module m) {
144-
return AccessController.doPrivileged(new PrivilegedAction<>() {
145-
@Override
146-
public Boolean run() {
147-
// returns true if moduleCL is the platform class loader
148-
// or one of its ancestors.
149-
return VM.isSystemDomainLoader(m.getClassLoader());
150-
}
151-
});
123+
return VM.isSystemDomainLoader(m.getClassLoader());
152124
}
153125

154126
@Override
155127
public final Logger getLogger(String name, Module module) {
156128
Objects.requireNonNull(name, "name");
157129
Objects.requireNonNull(module, "module");
158-
checkPermission();
159130
return demandLoggerFor(name, module);
160131
}
161132

@@ -176,11 +147,8 @@ public final Logger getLocalizedLogger(String name, ResourceBundle bundle,
176147
* @param name The name of the logger.
177148
* @param module The module on behalf of which the logger is created.
178149
* @return A {@link Logger logger} suitable for the application usage.
179-
* @throws SecurityException if the calling code does not have the
180-
* {@code RuntimePermission("loggerFinder")}.
181150
*/
182151
protected Logger demandLoggerFor(String name, Module module) {
183-
checkPermission();
184152
if (isSystem(module)) {
185153
return SharedLoggers.system.get(SimpleConsoleLogger::makeSimpleLogger, name);
186154
} else {

src/java.base/share/classes/jdk/internal/logger/LazyLoggers.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,6 @@
2525

2626
package jdk.internal.logger;
2727

28-
import java.security.AccessController;
29-
import java.security.PrivilegedAction;
3028
import java.util.function.BiFunction;
3129
import java.lang.System.LoggerFinder;
3230
import java.lang.System.Logger;
@@ -44,9 +42,6 @@
4442
*/
4543
public final class LazyLoggers {
4644

47-
static final RuntimePermission LOGGERFINDER_PERMISSION =
48-
new RuntimePermission("loggerFinder");
49-
5045
private LazyLoggers() {
5146
throw new InternalError();
5247
}
@@ -341,7 +336,6 @@ PlatformLogger.Bridge platformProxy() {
341336

342337
// Do not expose this outside of this package.
343338
private static volatile LoggerFinder provider;
344-
@SuppressWarnings("removal")
345339
private static LoggerFinder accessLoggerFinder() {
346340
LoggerFinder prov = provider;
347341
if (prov == null) {
@@ -350,10 +344,7 @@ private static LoggerFinder accessLoggerFinder() {
350344
// the result.
351345
// This is just an optimization to avoid the cost of calling
352346
// doPrivileged every time.
353-
final SecurityManager sm = System.getSecurityManager();
354-
prov = sm == null ? LoggerFinder.getLoggerFinder() :
355-
AccessController.doPrivileged(
356-
(PrivilegedAction<LoggerFinder>)LoggerFinder::getLoggerFinder);
347+
prov = LoggerFinder.getLoggerFinder();
357348
if (prov instanceof TemporaryLoggerFinder) return prov;
358349
provider = prov;
359350
}
@@ -403,17 +394,9 @@ static Logger makeLazyLogger(String name, Module module, BooleanSupplier isLoadi
403394
* @param module module on behalf of which the logger is created
404395
* @return The logger returned by the LoggerFinder.
405396
*/
406-
@SuppressWarnings("removal")
407397
static Logger getLoggerFromFinder(String name, Module module) {
408-
final SecurityManager sm = System.getSecurityManager();
409-
if (sm == null) {
410-
return accessLoggerFinder().getLogger(name, module);
411-
} else {
412-
return AccessController.doPrivileged((PrivilegedAction<Logger>)
413-
() -> {return accessLoggerFinder().getLogger(name, module);},
414-
null, LOGGERFINDER_PERMISSION);
415-
}
416-
}
398+
return accessLoggerFinder().getLogger(name, module);
399+
}
417400

418401
/**
419402
* Returns a (possibly lazy) Logger for the caller.

0 commit comments

Comments
 (0)