Deterministic naming of subclasses of `java/lang/reflect/Proxy`
Aman Sharma
amansha at kth.se
Wed May 15 23:59:53 UTC 2024
Hi,
My name is Aman and I am a PhD student at KTH Royal Institute of Technology, Stockholm, Sweden. I research as part of CHAINS<https://chains.proj.kth.se/> project to strengthen the software supply chain of multiple ecosystem. I particularly focus on runtime integrity in Java. In this email, I want to write about an issue I have discovered with dynamic generation of `java.lang.reflect.Proxy`classes. I will propose a solution and would love to hear the feedback from the community. Let me know if this is the correct mailing-list for such discussions. It seemed the most relevant from this list<https://mail.openjdk.org/mailman/listinfo>.
My research
Java has features to load class on the fly - it can either download or generate a class at runtime. These features are useful for inner workings of JDK. For example, implementing annotations, reflective access, etc. However, these features have also contributed to critical vulnerabilities in the past - CVE-2021-44228 (log4shell), CVE-2022-33980, CVE-2022-42392. All of these vulnerabilities have one thing in common - a class that was not known during build time was downloaded/generated at runtime and loaded into JVM.
To defend against such vulnerabilities, we propose a solution to allowlist classes for runtime. This allowlist will contain an exhaustive list of classes that can be loaded by the JVM and it will be enforced at runtime. We build this allowlist from three sources:
1. All classes of all modules provided by the Java Standard Library. We use ClassGraph<https://github.com/classgraph/classgraph> to scan the JDK.
2. We can take the source code and all dependencies of an application. We use a software bill of materials to get all the data.
3. Finally, we use run the test suite to include any runtime downloaded/generated classes.
Such a list is able to prevent the above 3 CVEs because it does not let the "unknown" bytecode to be loaded.
Problem with generating such an allowlist
The first two parts of the allowlist are easy to get. The problem is with the third step where we want to allowlist all the classes that could be downloaded or generated. Upon running the test suite and hooking to the classes it loads, we observer that the list consists of classes that are called "com/sun/proxy/$Proxy2", "jdk/internal/reflect/GeneratedConstructorAccessor3" among many more. The purpose of these classes can be identifed. The proxy class is created for to implement an annotation. The accessor gives access to constructor of a class to the JVM.
When enforcing this allowlist at runtime, we see that the bytecode content for "com/sun/proxy/$Proxy2" differs in the allowlist and at runtime. In our case, we we are experimenting with pdfbox<https://github.com/apache/pdfbox> so we created the allowlist using its test suite. Then we enforced this allowlist while running some of its subcommands. However, there was some other proxy class say "com/sun/proxy/$Proxy5" at runtime that implemented the same interfaces and had the same methods as "com/sun/proxy/$Proxy2" in the allowlist. They only differed in the name of the class, order of fields, and types for fields references. This could happen because the order of the loading of class is workload dependent, but it causes problem to generate such an allowlist.
Solution
We propose that naming of subclasses of "java/lang/reflect/Proxy" should not be dependent upon the order of loading. In order to do so, two issues can be fixed:
1. The naming of the class should not be based on AtomicLong<https://github.com/openjdk/jdk/blob/b687aa550837830b38f0f0faa69c353b1e85219c/src/java.base/share/classes/java/lang/reflect/Proxy.java#L531><https://github.com/openjdk/jdk/blob/4083255440cfbf39b9683ea88a433d71ec6111e7/src/java.base/share/classes/java/lang/reflect/Proxy.java#L531>. Rather it could be named based on the interfaces it implements. I also wonder why AtomicLong is chosen in the first place.
2. Methods of the interfaces must be in a particular order. Right now, they are not sorted in any particular order<https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Class.java#L2178>.
These fixes will make proxy class generation deterministic with respect to order of loading and won't be flagged at runtime since the test suite would already detect them.
I would love to hear from the community about these ideas. If in agreement, I would be happy to produce a patch. I have discovered this issue with subclasses of GeneratedConstructorAccessor<https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/reflect/ConstructorAccessor.java> as well and I imagine it will also apply to some other runtime generated classes. If you disagree, please let me know also. It helps with my research.
I also have PoCs for the above CVEs<https://github.com/chains-project/exploits-for-sbom.exe> and a proof concept tool is being developed under the name sbom.exe<https://github.com/chains-project/sbom.exe> in case any one wonders about the implementation. I would also be happy to explain more.
Regards,
Aman Sharma
PhD Student
KTH Royal Institute of Technology
School of Electrical Engineering and Computer Science (EECS)
Department of Theoretical Computer Science (TCS)
<http://www.kth.se><https://www.kth.se/profile/amansha><https://www.kth.se/profile/amansha>
<https://www.kth.se/profile/amansha>https://algomaster99.github.io/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20240515/a3b144e0/attachment-0001.htm>
More information about the jdk-dev
mailing list