On December 9, 2021, a remote code execution (RCE) flaw in the Java logging library Log4j was disclosed. The vulnerability, tracked as CVE-2021-44228, affects Log4j version 2 from 2.0-beta-9 through 2.14.1 and is fixed in 2.16.0. Given how widely Java and Log4j are deployed, it is likely one of the most serious vulnerabilities seen on the Internet since Heartbleed and ShellShock.

The flaw allows an attacker to run code on a remote server. Organizations running Java-based software that uses Log4j should apply the mitigations below before anything else.

Mitigation

  • Users on Java 8 or later should upgrade to release 2.16.0.
  • Users on Java 7 should upgrade to release 2.12.2.
  • On any release other than 2.16.0, you can remove the JndiLookup class from the classpath: zip -q -d log4j-core-*.jar org/apache/logging/log4j/core/lookup/JndiLookup.class

For reference, Apache has updated its official guidance over time and the steps above follow its current recommendations.

How the flaw was introduced

Log4j 2.0-beta9, released in 2013, added a “JNDILookup plugin” under issue LOG4J2-313. Understanding why that change created a vulnerability requires some background on JNDI, the Java Naming and Directory Interface.

JNDI has existed in Java since the late 1990s. It is a directory service that lets a Java program locate data — in the form of a Java object — through a directory, and it exposes a set of service provider interfaces (SPIs) for different directory services. SPIs exist for the CORBA COS (Common Object Service), the Java RMI (Remote Method Interface) Registry and LDAP. LDAP, the Lightweight Directory Access Protocol, is the primary focus of CVE-2021-44228, though other SPIs could potentially be used as well.

JNDI and LDAP can be combined so that a Java program fetches an object holding data it needs. The standard Java documentation includes an example that talks to an LDAP server to read attributes from an object; it uses the URL ldap://localhost:389/o=JNDITutorial to find the JNDITutorial object on an LDAP server running on the same machine at port 389. The tutorial notes that “If your LDAP server is located on another machine or is using another port, then you need to edit the LDAP URL”. Because the LDAP server can run on another machine — potentially anywhere on the Internet — an attacker who controls the LDAP URL could make a Java program load an object from a server of their choosing.

The Lookup mechanism and the exploit

Log4j supports special syntax of the form ${prefix:name}, where prefix is one of several Lookups and name is the value to evaluate — for example, ${java:version} resolves to the running Java version. LOG4J2-313 added a jndi Lookup described as follows: “The JndiLookup allows variables to be retrieved via JNDI. By default the key will be prefixed with java:comp/env/, however if the key contains a ":" no prefix will be added.”

When the key contains a :, as in ${jndi:ldap://example.com/a}, no prefix is added and the LDAP server is queried for the object. Lookups work both in Log4j's configuration and when lines are logged.

The attack therefore only requires finding an input that gets logged and placing a string such as ${jndi:ldap://example.com/a} in it. A common HTTP header like User-Agent is one candidate, since it is frequently logged; a form parameter like username may be another. In that case Log4j connects to the LDAP server at example.com and retrieves the object.

Exposure beyond the internet-facing tier

This pattern is likely very common in Java-based, internet-facing software that uses Log4j. Less obviously, non-internet-facing Java software can be exploitable too, because data moves between systems. A User-Agent string carrying the exploit could reach a backend Java system that does indexing or data science work, and be logged there. That is why every Java application using Log4j version 2 needs patching or mitigation immediately even if its internet-facing software is not written in Java: strings can pass to other systems that are.

Even if the Internet-facing software is not written in Java it is possible that strings get passed to other systems that are in Java allowing the exploit to happen.

A Java-based billing system, for instance, might log when a customer's first name is not found. A malicious user could place an order with a first name containing the exploit, which may take multiple hops — and considerable time — from the web server through a customer database to the billing system, where it finally executes. Nor is Java limited to web services: a package handling system scanning QR codes on boxes, or a contactless door key, could be vulnerable if written in Java with Log4j. A crafted QR code might carry a postal address containing the exploit string, and a tampered door key could be logged by a system tracking entries and exits.

Periodic workloads can also pick up the exploit and log it later, so it may lie dormant until an indexing, roll-up or archive process written in Java inadvertently logs the bad string — hours or even days afterward.

Cloudflare firewall protection

Cloudflare uses some Java-based software, and its teams confirmed that its systems were either not vulnerable or had the vulnerability mitigated. In parallel, the company rolled out Firewall rules for customers that block the jndi Lookup in common locations of an HTTP request, as detailed separately. Those rules continue to be refined as attackers modify their exploits. Details of actual attempted exploitation blocked by the firewall service are covered in a separate blog post.