The Java classes used by a program are not necessarily loaded upon program startup. Many Java Virtual Machines (JVMs) load classes only when they need them.
...
This noncompliant code example shows a vulnerability present in several versions of the Tomcat HTTP web server (fixed in version 6.0.20) that allows untrusted web applications to override the default XML parser used by the system to process web.xml
, context.xml
, and tag library descriptor (TLD) files of other web applications deployed on the Tomcat instance. Consequently, untrusted web applications that install a parser could view and/or alter these files under certain circumstances.
...
Code Block | ||
---|---|---|
| ||
// This method exists in the class DigesterFactory and is called by ContextConfig.createWebXmlDigester() // which is in turn called by ContextConfig.createWebDigester() // webDigester finally contains the value of digester defined // in this method public static Digester newDigester(boolean xmlValidation, boolean xmlNamespaceAware, RuleSet rule) { Digester digester = new Digester(); // ... digester.setUseContextClassLoader(true); // ... return digester; } |
...
Code Block | ||
---|---|---|
| ||
public ClassLoader getClassLoader() { // ... if (this.useContextClassLoader) { // Uses the context class loader which was previously set // to the WebappClassLoader ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); } return classloader; } |
...
The underlying problem is that the newInstance()
method is being invoked on behalf of a web application's class loader, the WebappClassLoader
, and it loads classes before Tomcat has loaded all the classes it needs. If a web application has loaded its own javax.xml.parsers.SAXParserFactory
, then when Tomcat tries to access a SAXParserFactory
, it will access accesses the incorrect trojan SaxParserFactory
used installed by the web app application rather than the standard Java SAXParserFactory
that it Tomcat depends on.
Compliant Solution (Tomcat)
In this compliant solution, Tomcat initializes the SAXParserFactory
when it creates the Digester
. This guarantees that the SAXParserFactory
is constructed using the container's class loader rather than the WebappClassLoader
.
The webDigester
is also marked declared final. This prevents any subclasses from assigning a new object reference to webDigester
. (See rule OBJ10-J. Do not use public static nonfinal variables for more information.) It also prevents a race condition where another thread could access webDigester
before it is fully initialized. (See rule OBJ11-J. Be wary of letting constructors throw exceptions for more information.)
Code Block | ||
---|---|---|
| ||
protected static final Digester webDigester = init(); protected Digester init() { Digester digester = createWebDigester(); digester.getParser(); // Does not use the context Classloader at initialization, so safe digester.getParser(); return digester; } |
Even if the Tomcat server continues to use the WebappClassLoader
to create the parser instance when attempting to process the web.xml
and other files, the explicit call to getParser()
in init()
ensures that the default parser has been set during prior initialization and is impossible to replace. Because this is a one-time setting, future attempts to change the parser are futile.
...
Allowing untrusted code to load classes enables untrusted code to replace benign classes with malicious trojan classes.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SEC03-J | high | probable | medium | P12 | L1 |
...
Secure Coding Guidelines for the Java Programming Language, Version 3.0 | Guideline 6-3. Safely invoke standard APIs that bypass |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f797d13f3b96b3e3-74dd8d1f-42854173-8d28b6d9-9435ea6fd04c4f3b069e8fff"><ac:plain-text-body><![CDATA[ | [[CVE 20082011 | AA. Bibliography#CVE 08]] | [CVE-2009-0783 | http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-0783] | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4c987d988ee83161-44944694-491542ad-b7b6bc03-724ec75b1c60bf04c1d68b9e"><ac:plain-text-body><![CDATA[ | [[Gong 2003 | AA. Bibliography#Gong 03]] | Section 4.3.2, Class Loader Delegation Hierarchy | ]]></ac:plain-text-body></ac:structured-macro> | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6a3967cd27de382e-6d4b3dac-4edb467b-9f3fa712-9e5c8d45beab2ca41e1dcb71"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | §4.3.2, " The Class | ]]></ac:plain-text-body></ac:structured-macro> | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7fc9ed762b007d6b-01fbb170-4d904ea0-9f7f9cb7-4cdfc150acc247200493e136"><ac:plain-text-body><![CDATA[ | [[Tomcat 2009 | AA. Bibliography#Tomcat 09]] | [Bug ID 29936 | https://issues.apache.org/bugzilla/show_bug.cgi?id=29936], API Class | http://tomcat.apache.org/security-6.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...