Skip to content

Commit 19f0755

Browse files
committed
8365203: defineClass with direct buffer can cause use-after-free
Reviewed-by: jpai
1 parent 0ca38bd commit 19f0755

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2019, Azul Systems, Inc. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -52,6 +52,7 @@
5252
import java.util.stream.Stream;
5353
import java.util.stream.StreamSupport;
5454

55+
import jdk.internal.access.SharedSecrets;
5556
import jdk.internal.loader.BootLoader;
5657
import jdk.internal.loader.BuiltinClassLoader;
5758
import jdk.internal.loader.ClassLoaders;
@@ -1049,14 +1050,22 @@ protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
10491050

10501051
protectionDomain = preDefineClass(name, protectionDomain);
10511052
String source = defineClassSourceLocation(protectionDomain);
1052-
Class<?> c = defineClass2(this, name, b, b.position(), len, protectionDomain, source);
1053-
postDefineClass(c, protectionDomain);
1054-
return c;
1053+
1054+
SharedSecrets.getJavaNioAccess().acquireSession(b);
1055+
try {
1056+
Class<?> c = defineClass2(this, name, b, b.position(), len, protectionDomain, source);
1057+
postDefineClass(c, protectionDomain);
1058+
return c;
1059+
} finally {
1060+
SharedSecrets.getJavaNioAccess().releaseSession(b);
1061+
}
10551062
}
10561063

10571064
static native Class<?> defineClass1(ClassLoader loader, String name, byte[] b, int off, int len,
10581065
ProtectionDomain pd, String source);
10591066

1067+
// Warning: Before calling this method, the provided ByteBuffer must be guarded
1068+
// via JavaNioAccess::(acquire|release)Session
10601069
static native Class<?> defineClass2(ClassLoader loader, String name, java.nio.ByteBuffer b,
10611070
int off, int len, ProtectionDomain pd,
10621071
String source);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2025, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8365203
27+
* @summary Tests guarding of ByteBuffers in ClassLoader::defineClass
28+
* @run junit TestGuardByteBuffer
29+
*/
30+
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.lang.foreign.Arena;
34+
import java.util.HexFormat;
35+
36+
import static org.junit.jupiter.api.Assertions.*;
37+
38+
final class TestGuardByteBuffer {
39+
40+
@Test
41+
void guardCrash() {
42+
final byte[] classBytes = getClassBytes(); // get bytes of a valid class
43+
final var cl = new ClassLoader() {
44+
void tryCrash() {
45+
var arena = Arena.ofConfined();
46+
var byteBuffer = arena.allocate(classBytes.length).asByteBuffer();
47+
// Close the arena underneath
48+
arena.close();
49+
// expected to always fail because the arena
50+
// from which the ByteBuffer was constructed
51+
// has been closed
52+
assertThrows(IllegalStateException.class,
53+
() -> defineClass(null, byteBuffer, null));
54+
}
55+
};
56+
for (int i = 0; i < 10_000; i++) {
57+
cl.tryCrash();
58+
}
59+
}
60+
61+
private static byte[] getClassBytes() {
62+
// unused. this is here just for reference
63+
final String source = """
64+
public class NoOp {}
65+
""";
66+
// (externally) compiled content of the above "source", represented as hex
67+
final String classBytesHex = """
68+
cafebabe00000044000d0a000200030700040c000500060100106a6176612f
69+
6c616e672f4f626a6563740100063c696e69743e0100032829560700080100
70+
044e6f4f70010004436f646501000f4c696e654e756d6265725461626c6501
71+
000a536f7572636546696c650100094e6f4f702e6a61766100210007000200
72+
0000000001000100050006000100090000001d00010001000000052ab70001
73+
b100000001000a000000060001000000010001000b00000002000c
74+
""";
75+
76+
return HexFormat.of().parseHex(classBytesHex.replaceAll("\n", ""));
77+
}
78+
79+
}

0 commit comments

Comments
 (0)