Skip to content

Commit 9b0ab92

Browse files
author
Roger Riggs
committedNov 18, 2024
8344034: Remove security manager dependency in Serialization
Reviewed-by: mullan, alanb
1 parent d52d136 commit 9b0ab92

File tree

7 files changed

+101
-414
lines changed

7 files changed

+101
-414
lines changed
 

‎src/java.base/share/classes/java/io/ObjectInputFilter.java

+2-18
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import jdk.internal.util.StaticProperty;
3030

3131
import java.lang.reflect.InvocationTargetException;
32-
import java.security.AccessController;
33-
import java.security.PrivilegedAction;
3432
import java.security.Security;
3533
import java.util.ArrayList;
3634
import java.util.List;
@@ -630,17 +628,13 @@ final class Config {
630628
configLog = System.getLogger("java.io.serialization");
631629

632630
// Get the values of the system properties, if they are defined
633-
@SuppressWarnings("removal")
634631
String factoryClassName = StaticProperty.jdkSerialFilterFactory() != null
635632
? StaticProperty.jdkSerialFilterFactory()
636-
: AccessController.doPrivileged((PrivilegedAction<String>) () ->
637-
Security.getProperty(SERIAL_FILTER_FACTORY_PROPNAME));
633+
: Security.getProperty(SERIAL_FILTER_FACTORY_PROPNAME);
638634

639-
@SuppressWarnings("removal")
640635
String filterString = StaticProperty.jdkSerialFilter() != null
641636
? StaticProperty.jdkSerialFilter()
642-
: AccessController.doPrivileged((PrivilegedAction<String>) () ->
643-
Security.getProperty(SERIAL_FILTER_PROPNAME));
637+
: Security.getProperty(SERIAL_FILTER_PROPNAME);
644638

645639
// Initialize the static filter if the jdk.serialFilter is present
646640
String filterMessage = null;
@@ -734,11 +728,6 @@ public static ObjectInputFilter getSerialFilter() {
734728
*/
735729
public static void setSerialFilter(ObjectInputFilter filter) {
736730
Objects.requireNonNull(filter, "filter");
737-
@SuppressWarnings("removal")
738-
SecurityManager sm = System.getSecurityManager();
739-
if (sm != null) {
740-
sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
741-
}
742731
if (invalidFilterMessage != null) {
743732
throw new IllegalStateException(invalidFilterMessage);
744733
}
@@ -831,11 +820,6 @@ static BinaryOperator<ObjectInputFilter> getSerialFilterFactorySingleton() {
831820
*/
832821
public static void setSerialFilterFactory(BinaryOperator<ObjectInputFilter> filterFactory) {
833822
Objects.requireNonNull(filterFactory, "filterFactory");
834-
@SuppressWarnings("removal")
835-
SecurityManager sm = System.getSecurityManager();
836-
if (sm != null) {
837-
sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
838-
}
839823
if (filterFactoryNoReplace.getAndSet(true)) {
840824
final String msg = serialFilterFactory != null
841825
? "Cannot replace filter factory: " + serialFilterFactory.getClass().getName()

‎src/java.base/share/classes/java/io/ObjectInputStream.java

+28-98
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@
3434
import java.lang.reflect.Modifier;
3535
import java.lang.reflect.Proxy;
3636
import java.nio.charset.StandardCharsets;
37-
import java.security.AccessControlContext;
38-
import java.security.AccessController;
39-
import java.security.PrivilegedAction;
40-
import java.security.PrivilegedActionException;
41-
import java.security.PrivilegedExceptionAction;
4237
import java.util.Arrays;
43-
import java.util.Map;
4438
import java.util.Objects;
4539

4640
import jdk.internal.access.JavaLangAccess;
@@ -49,8 +43,6 @@
4943
import jdk.internal.misc.Unsafe;
5044
import jdk.internal.util.ByteArray;
5145
import sun.reflect.misc.ReflectUtil;
52-
import sun.security.action.GetBooleanAction;
53-
import sun.security.action.GetIntegerAction;
5446

5547
/**
5648
* An ObjectInputStream deserializes primitive data and objects previously
@@ -278,26 +270,26 @@ protected Boolean computeValue(Class<?> type) {
278270
* have been read.
279271
* See {@link #setObjectInputFilter(ObjectInputFilter)}
280272
*/
281-
static final boolean SET_FILTER_AFTER_READ = GetBooleanAction
282-
.privilegedGetProperty("jdk.serialSetFilterAfterRead");
273+
static final boolean SET_FILTER_AFTER_READ =
274+
Boolean.getBoolean("jdk.serialSetFilterAfterRead");
283275

284276
/**
285277
* Property to control {@link GetField#get(String, Object)} conversion of
286278
* {@link ClassNotFoundException} to {@code null}. If set to {@code true}
287279
* {@link GetField#get(String, Object)} returns null otherwise
288280
* throwing {@link ClassNotFoundException}.
289281
*/
290-
private static final boolean GETFIELD_CNFE_RETURNS_NULL = GetBooleanAction
291-
.privilegedGetProperty("jdk.serialGetFieldCnfeReturnsNull");
282+
private static final boolean GETFIELD_CNFE_RETURNS_NULL =
283+
Boolean.getBoolean("jdk.serialGetFieldCnfeReturnsNull");
292284

293285
/**
294286
* Property to override the implementation limit on the number
295287
* of interfaces allowed for Proxies. The property value is clamped to 0..65535.
296288
* The maximum number of interfaces allowed for a proxy is limited to 65535 by
297289
* {@link java.lang.reflect.Proxy#newProxyInstance(ClassLoader, Class[], InvocationHandler)}.
298290
*/
299-
static final int PROXY_INTERFACE_LIMIT = Math.clamp(GetIntegerAction
300-
.privilegedGetProperty("jdk.serialProxyInterfaceLimit", 65535), 0, 65535);
291+
static final int PROXY_INTERFACE_LIMIT =
292+
Math.clamp(Integer.getInteger("jdk.serialProxyInterfaceLimit", 65535), 0, 65535);
301293
}
302294

303295
/*
@@ -386,7 +378,6 @@ private static class Logging {
386378
*/
387379
@SuppressWarnings("this-escape")
388380
public ObjectInputStream(InputStream in) throws IOException {
389-
verifySubclass();
390381
bin = new BlockDataInputStream(in);
391382
handles = new HandleTable(10);
392383
vlist = new ValidationList();
@@ -416,11 +407,6 @@ public ObjectInputStream(InputStream in) throws IOException {
416407
* fails due to invalid serial filter or serial filter factory properties.
417408
*/
418409
protected ObjectInputStream() throws IOException {
419-
@SuppressWarnings("removal")
420-
SecurityManager sm = System.getSecurityManager();
421-
if (sm != null) {
422-
sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
423-
}
424410
bin = null;
425411
handles = null;
426412
vlist = null;
@@ -907,13 +893,6 @@ protected boolean enableResolveObject(boolean enable) {
907893
if (enable == enableResolve) {
908894
return enable;
909895
}
910-
if (enable) {
911-
@SuppressWarnings("removal")
912-
SecurityManager sm = System.getSecurityManager();
913-
if (sm != null) {
914-
sm.checkPermission(SUBSTITUTION_PERMISSION);
915-
}
916-
}
917896
enableResolve = enable;
918897
return !enableResolve;
919898
}
@@ -1309,11 +1288,6 @@ public final ObjectInputFilter getObjectInputFilter() {
13091288
* @since 9
13101289
*/
13111290
public final void setObjectInputFilter(ObjectInputFilter filter) {
1312-
@SuppressWarnings("removal")
1313-
SecurityManager sm = System.getSecurityManager();
1314-
if (sm != null) {
1315-
sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
1316-
}
13171291
if (totalObjectRefs > 0 && !Caches.SET_FILTER_AFTER_READ) {
13181292
throw new IllegalStateException(
13191293
"filter can not be set after an object has been read");
@@ -1571,58 +1545,29 @@ public abstract boolean get(String name, boolean val)
15711545
public abstract Object get(String name, Object val) throws IOException, ClassNotFoundException;
15721546
}
15731547

1574-
/**
1575-
* Verifies that this (possibly subclass) instance can be constructed
1576-
* without violating security constraints: the subclass must not override
1577-
* security-sensitive non-final methods, or else the
1578-
* "enableSubclassImplementation" SerializablePermission is checked.
1579-
*/
1580-
private void verifySubclass() {
1581-
Class<?> cl = getClass();
1582-
if (cl == ObjectInputStream.class) {
1583-
return;
1584-
}
1585-
@SuppressWarnings("removal")
1586-
SecurityManager sm = System.getSecurityManager();
1587-
if (sm == null) {
1588-
return;
1589-
}
1590-
boolean result = Caches.subclassAudits.get(cl);
1591-
if (!result) {
1592-
sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
1593-
}
1594-
}
1595-
15961548
/**
15971549
* Performs reflective checks on given subclass to verify that it doesn't
15981550
* override security-sensitive non-final methods. Returns TRUE if subclass
15991551
* is "safe", FALSE otherwise.
16001552
*/
1601-
@SuppressWarnings("removal")
16021553
private static Boolean auditSubclass(Class<?> subcl) {
1603-
return AccessController.doPrivileged(
1604-
new PrivilegedAction<Boolean>() {
1605-
public Boolean run() {
1606-
for (Class<?> cl = subcl;
1607-
cl != ObjectInputStream.class;
1608-
cl = cl.getSuperclass())
1609-
{
1610-
try {
1611-
cl.getDeclaredMethod(
1612-
"readUnshared", (Class[]) null);
1613-
return Boolean.FALSE;
1614-
} catch (NoSuchMethodException ex) {
1615-
}
1616-
try {
1617-
cl.getDeclaredMethod("readFields", (Class[]) null);
1618-
return Boolean.FALSE;
1619-
} catch (NoSuchMethodException ex) {
1620-
}
1621-
}
1622-
return Boolean.TRUE;
1623-
}
1554+
for (Class<?> cl = subcl;
1555+
cl != ObjectInputStream.class;
1556+
cl = cl.getSuperclass())
1557+
{
1558+
try {
1559+
cl.getDeclaredMethod(
1560+
"readUnshared", (Class[]) null);
1561+
return Boolean.FALSE;
1562+
} catch (NoSuchMethodException ex) {
16241563
}
1625-
);
1564+
try {
1565+
cl.getDeclaredMethod("readFields", (Class[]) null);
1566+
return Boolean.FALSE;
1567+
} catch (NoSuchMethodException ex) {
1568+
}
1569+
}
1570+
return Boolean.TRUE;
16261571
}
16271572

16281573
/**
@@ -2702,16 +2647,11 @@ private static class Callback {
27022647
final ObjectInputValidation obj;
27032648
final int priority;
27042649
Callback next;
2705-
@SuppressWarnings("removal")
2706-
final AccessControlContext acc;
27072650

2708-
Callback(ObjectInputValidation obj, int priority, Callback next,
2709-
@SuppressWarnings("removal") AccessControlContext acc)
2710-
{
2651+
Callback(ObjectInputValidation obj, int priority, Callback next) {
27112652
this.obj = obj;
27122653
this.priority = priority;
27132654
this.next = next;
2714-
this.acc = acc;
27152655
}
27162656
}
27172657

@@ -2740,12 +2680,10 @@ void register(ObjectInputValidation obj, int priority)
27402680
prev = cur;
27412681
cur = cur.next;
27422682
}
2743-
@SuppressWarnings("removal")
2744-
AccessControlContext acc = AccessController.getContext();
27452683
if (prev != null) {
2746-
prev.next = new Callback(obj, priority, cur, acc);
2684+
prev.next = new Callback(obj, priority, cur);
27472685
} else {
2748-
list = new Callback(obj, priority, list, acc);
2686+
list = new Callback(obj, priority, list);
27492687
}
27502688
}
27512689

@@ -2756,23 +2694,15 @@ void register(ObjectInputValidation obj, int priority)
27562694
* throws an InvalidObjectException, the callback process is terminated
27572695
* and the exception propagated upwards.
27582696
*/
2759-
@SuppressWarnings("removal")
27602697
void doCallbacks() throws InvalidObjectException {
27612698
try {
27622699
while (list != null) {
2763-
AccessController.doPrivileged(
2764-
new PrivilegedExceptionAction<Void>()
2765-
{
2766-
public Void run() throws InvalidObjectException {
2767-
list.obj.validateObject();
2768-
return null;
2769-
}
2770-
}, list.acc);
2700+
list.obj.validateObject();
27712701
list = list.next;
27722702
}
2773-
} catch (PrivilegedActionException ex) {
2703+
} catch (InvalidObjectException ex) {
27742704
list = null;
2775-
throw (InvalidObjectException) ex.getException();
2705+
throw ex;
27762706
}
27772707
}
27782708

0 commit comments

Comments
 (0)
Please sign in to comment.