CVE-2022-29885 - Don't Open That Port - A Denial Of Service vulnerability on Apache Tomcat Cluster Service Listener
The Apache Tomcat cluster receiver on port 4000 accepts messages without proper bounds checking, allowing an attacker to send a crafted header followed by junk data that causes the internal buffer to expand indefinitely. This can exhaust the Java heap and cause a permanent denial of service until the service is restarted.
While performing the analysis I discovered that this was a part of a research made by 4ra1n, who reported the issue to the Apache Tomcat Security Team on 17 April 2022 and marked as CVE-2022-29885. Nonetheless, I had no luck finding a suitable PoC of the vulnerability.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Apache Tomcat is, as the report puts it, perhaps the best known free and open-source implementation of the Jakarta Servlet, Jakarta EL and WebSocket technologies, mainly used to host Java web applications. The feature under examination is clustering: grouping several Tomcat instances so they work together and distribute load across the group. The report draws the analogy with storage clustering, such as a RAID set of disks providing a redundant, fail-safe architecture, and with the common web deployment where a load balancer forwards incoming requests to a cluster of web servers.

Web Server Clustering example
In Tomcat, clustering is configured through a <Cluster> element. The report breaks the sample configuration from the official Tomcat website into its parts:
- The
<Cluster>element defines a cluster listener class that will be instantiated,org.apache.catalina.ha.tcp.SimpleTcpClusterin this case. - The
<Manager>is responsible for incoming message handling and is handled by theorg.apache.catalina.ha.session.DeltaManagerclass. - The
<Receiver>is the front-man when dealing with incoming objects, handled byorg.apache.catalina.tribes.transport.nio.NioReceiver. This class is the target of the research. - The other elements are settings for replication, deployers and message dispatchers.
The receiver listens on port 4000, as stated in the <Receiver> definition, and accepts incoming messages.
Setup
The report does not describe a particular deployment, account or tooling beyond reading the Tomcat source and running a proof-of-concept script against a Tomcat instance. The one concrete environment detail given is memory: the author allocated 2G only to the Java heap space, to avoid locks on the machine, and notes that the attack heavily depends on the heap space allocated to that particular VM. In a later test the author used 1000 A characters, 5 threads and 2g of memory for the JVM.
A note at the top of the report gives the provenance of the issue: the author discovered during the analysis that this was part of research by 4ra1n, who reported it to the Apache Tomcat Security Team on 17 April 2022 and it was marked as CVE-2022-29885. The author says he had no luck finding a suitable PoC of the vulnerability, and later found the author's website, crediting him for the CVE. A further update mentions an old unsafe deserialisation write-up of unknown authorship that the author could not translate from Chinese.
Finding it
The starting point was reading the Apache Tomcat source code and stumbling on the clustering functionality. Knowing that the NioReceiver accepts incoming messages on port 4000, the author followed the code path from the receiver's start to the point where socket data is handled.
The following code runs when org.apache.catalina.tribes.transport.nio.NioReceiver is started (NioReceiver#446). The listen method is the main loop that listens for incoming data (NioReceiver.java#280), and readDataFromSocket runs a NioReplicationTask object (NioReceiver.java#468). Fast-forwarding to the actual vulnerable code brings the reader to NioTaskReceiver.java#164.
The code flows to a location where the data from the socket is appended via the append() method of an ObjectReader object held in the reader variable. That append() method simply adds the sent data to a buffer and continues.

Analysing the XByteBuffer class, described as nothing more than a serializable POJO, shows what the application expects from the input socket:
public class XByteBuffer implements Serializable {
//...
private static final byte[] START_DATA = {70,76,84,50,48,48,50};
private static final byte[] END_DATA = {84,76,70,50,48,48,51};
The receiver expects a header so it can recognise that this is an XByteBuffer object. The byte sequences above are the decimal representation of the ASCII sequences "FLT2002" for START_DATA and "TLF2003" for END_DATA. That raised the question the rest of the report answers: assuming an attacker never sends an END_DATA sequence, what could go wrong?
Exploitation
Step 1: speak the protocol
The proof of concept forces the application to recognise the object as an XByteBuffer by specifying a START_DATA header, then appends junk data after it. The report presents the PoC as a script and describes it as pretty straightforward; the script itself is not reproduced in the text, only its behaviour.
Step 2: observe the result
Executing the script produces a stack trace in the Tomcat logs.

Stack trace
Memory analysis follows.

Memory analysis
And the free command output.

Free command
Two notes accompany these results. First, the 2G heap limit was chosen to avoid locks on the author's machine, and the attack heavily depends on the heap space allocated to that particular VM. Second, once the attack is made, the heap space is not cleaned up, even after the attack is stopped, which causes a permanent denial of service condition until the next service restart.
Step 3: fewer junk bytes
Sending less junk data does not prevent the outcome; it simply takes more time to fill the heap until it is full. With 1000 A characters, 5 threads and 2g of memory for the JVM, it took around 15 seconds to fill the heap space, with the same results: heap full, cluster functionality unstable, high memory usage.
Root cause
Following the stack trace, the vulnerable method is append() of the XByteBuffer class. When a new buffer is bigger than the previous one, it tries to allocate a new one by calling expand and passing the size of the newly created buffer (XByteBuffer#158):
public void expand(int newcount) {
//don't change the allocation strategy
byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, bufSize); // <-- vulnerable code
buf = newbuf;
}
The result is allocating too much space for a single object. Because the buffer keeps being expanded to hold the appended data, too much space is allocated for a single object and the heap fills. The report does not discuss any authentication or validation on the cluster port; the only protection it mentions is the documentation change described below.
Impact
An attacker who can reach the cluster port can fill the JVM heap by sending data that starts with the START_DATA header and never terminates with END_DATA. The observed effects are a full heap, unstable cluster functionality and high memory usage. Because the heap is not cleaned up after the attack stops, the denial of service is permanent until the service is restarted. The severity depends on the heap size of the target VM: the author notes the attack heavily depends on the allocated heap space, and that sending fewer junk bytes only slows the fill down.
Fix
While the Apache Tomcat Security team is aware of the issue, the report states that their "FIX" is just an update to the documentation advising not to open the cluster port on untrusted networks.


The """FIX"""
The author's conclusion is blunt: don't open that port on untrusted networks.

Variants to test
- Send the
START_DATAheader and a small amount of junk from several concurrent connections. A positive result is heap growth that persists after the connections close, matching the report's observation that the heap is not cleaned up after the attack stops. - Vary the junk size and thread count. A positive result is the same heap exhaustion reached in a different amount of time, as the report saw with 1000
Acharacters, 5 threads and 2g of heap in around 15 seconds. - Test against instances with different heap sizes. A positive result is exhaustion scaled to the configured heap, since the report says the attack depends on the heap allocated to the VM.
- Check whether the cluster port is reachable from untrusted networks at all. A positive result is a reachable receiver on port
4000, the kind of exposure the documentation update warns against.
What happened, in plain words
A security researcher was reading the source code of Apache Tomcat, a very popular piece of software that runs Java web applications, and found a way to make a Tomcat server run out of memory and stop working properly. The trick is to connect to a special network port that Tomcat uses when several Tomcat servers are grouped together, send a short greeting that the server recognises, and then keep sending junk without ever sending the "I'm finished" marker. The server keeps storing the junk in its memory, the memory fills up, and the server becomes unstable. The issue was reported to the Apache Tomcat Security Team on 17 April 2022 and marked as CVE-2022-29885, but the fix was only a documentation change telling people not to expose that port to untrusted networks.
This article walks through the same story for someone who has never done security work: what Tomcat is, what clustering means, how the researcher followed the code, what the proof of concept did, why the memory fills up, and what the official response was.
Warning
While doing the analysis, the researcher discovered that this was part of research done by 4ra1n, who reported the issue to the Apache Tomcat Security Team on 17 April 2022 and it was marked as CVE-2022-29885. The researcher had no luck finding a suitable proof of concept (a working demonstration) of the vulnerability.
A later update says he finally found the author's website and credits him for the CVE. A further update mentions an old write-up about unsafe deserialisation, of unknown authorship, which the researcher could not translate from Chinese.
A CVE is a public catalogue number given to a known security flaw so that everyone can refer to the same issue by the same name. A proof of concept (PoC) is a small, working demonstration that shows a flaw is real, usually the minimum needed to trigger it.
Introduction
As part of his latest research the author was analysing the Apache Tomcat source code and occasionally stumbled upon the clustering functionality. "Source code" is the human-written text that makes up a program; reading it is like reading the recipe rather than tasting the cake, and it lets you see exactly what the program will do in each situation.
Apache Tomcat
For those who lived under a rock for the last 10 years, Apache Tomcat is (maybe) the most known free and open-source implementation of the Jakarta Servlet, Jakarta EL and WebSocket technologies, and it is mainly used to host Java-written web applications.
In everyday terms: a web application is a program that answers requests from browsers. Tomcat is one of the containers that runs such programs on the server side. "Open source" means anyone can download and read its code, which is exactly what made this research possible.
One part that the research focused on, and that caught the author's attention, was the clustering functionality.
Clustering
Clustering is the task of grouping multiple objects in a way that they could work together in order to distribute the load over the instances of the group.
A simple comparison: one shop assistant can serve a few customers, but on a busy day you put several assistants behind several counters and a queue manager sends each customer to a free counter. Clustering is the same idea for servers: several copies of the same application share the work so no single one is overwhelmed.
The concept of clustering is not limited to applications or networks only. In fact it can be applied to the storage world, like, for example, a RAID disk that joins a specific cluster of disks in order to provide a redundant and fail-safe architecture. "Redundant" means there is a spare that can take over; "fail-safe" means the whole thing keeps working even when one part fails.
A simple and most common use of the clustering concepts could be summarised by the following schema, where a Load Balancer forwards the incoming request through a cluster of web servers:

Web Server Clustering example
In the picture, visitors arrive at a single front door (the load balancer) and are spread across several identical web servers behind it. If one server is busy or broken, the others keep serving.
Analysis of the Apache Tomcat Clustering functionality
How to configure clustering on Apache Tomcat? Nothing easier (they say). The report refers to the sample configuration file published on the Apache Tomcat official website and breaks its parts down:
- The
<Cluster>element defines a cluster listener class that will be instantiated.org.apache.catalina.ha.tcp.SimpleTcpClusterin this case. - The
<Manager>is responsible for incoming messages handling and it's handled by theorg.apache.catalina.ha.session.DeltaManagerclass. - The
<Receiver>represents the front-man when dealing with incoming objects. It is currently handled by theorg.apache.catalina.tribes.transport.nio.NioReceiverclass. For the impatient: this class will be our target. - Other elements are just settings for replication, deployers and message dispatchers.
A few words on that list. A class is a named block of code that describes one kind of thing in a program, for example "a receiver". To instantiate a class means to create a working copy of it while the program runs. The <Cluster>, <Manager> and <Receiver> names are configuration tags: short labels in a settings file that tell Tomcat which classes to switch on. The receiver is the part that sits at the network door and takes in whatever arrives; the report calls it the front-man because it is the first thing an incoming message meets. The manager handles those messages once they are inside. Replication here means copying session data between the servers in the cluster so a visitor stays logged in whichever server answers.
A tour on the source code
By now we know that there's a service running on port 4000 (as stated in the <Receiver> definition above), and that it is accepting incoming messages.
A port is a numbered door on a computer. One machine can run many services at once, and each listens on its own number, so the network knows which program a message is meant for. Port 4000 is the door the cluster receiver keeps open.
The following code will be executed when the org.apache.catalina.tribes.transport.nio.NioReceiver class is started (NioReceiver#446). The listen method will be the main loop that listens for incoming data (NioReceiver.java#280). While the readDataFromSocket runs a NioReplicationTask object (NioReceiver.java#468). Fast-forward to the actual vulnerable code brings to NioTaskReceiver.java#164.
A method is a named action inside a class, like a button you can press. A loop is code that repeats over and over; a listening loop is like a receptionist who never leaves the desk, waiting for the next visitor. The numbers after the # are line numbers in the source files, so a reader can jump straight to the exact spot. "Vulnerable" simply means "contains the weakness".
The code flows to a location where the data from the socket is appended via the append() method of an ObjectReader object, the reader variable. The append() method simply adds the sent data to a buffer and continues.
A socket is the connection between two programs over a network, like a phone line that stays open while both sides talk. A buffer is a temporary holding area in memory, like a tray where incoming letters are stacked before someone reads them. A variable is a name a program uses to refer to a value; here reader is the name of the object doing the reading. "Append" means "add to the end".

While analysing the XByteBuffer class, which is nothing more than a serializable POJO, it shows what the application is expecting from the input socket:
public class XByteBuffer implements Serializable {
//...
private static final byte[] START_DATA = {70,76,84,50,48,48,50};
private static final byte[] END_DATA = {84,76,70,50,48,48,51};
Line by line:
public class XByteBuffer implements Serializable {declares a class calledXByteBuffer.publicmeans other code can use it.implements Serializablemeans its contents can be turned into a stream of bytes and back, which is how it can travel over a network.//...is a comment standing for code left out of the excerpt.private static final byte[] START_DATA = {70,76,84,50,48,48,50};declares a fixed sequence of bytes namedSTART_DATA.privatemeans only this class uses it,staticmeans it belongs to the class itself rather than to any one copy,finalmeans it never changes, andbyte[]means "an array of bytes", that is, an ordered list of small numbers.private static final byte[] END_DATA = {84,76,70,50,48,48,51};does the same for a sequence namedEND_DATA.
A POJO (plain old Java object) is a simple data holder with no clever machinery inside. Serializable is the property that lets an object be flattened into bytes for transport. The receiver expects a header to be sent in order to recognise that this is an XByteBuffer object. The above byte sequences are just the decimal representation of the ASCII sequences: "FLT2002" for START_DATA and "TLF2003" for END_DATA.
ASCII is the standard table that maps characters to numbers, so the letter F is 70, L is 76, and so on. A header here is a short fixed marker at the start of a message that tells the receiver what kind of message follows, like the label on a parcel. So the receiver looks for the text FLT2002 at the beginning and TLF2003 at the end.
So, assuming an attacker never sends an END_DATA char sequence, what could go wrong?
That question is the heart of the whole issue. If the receiver only stops collecting when it sees the closing marker, and the marker never comes, the collecting never stops.
PoC || GTFO
The report presents the proof of concept as a script and describes it as pretty straightforward: we force the application to recognise this object as an XByteBuffer by specifying a START_DATA header and we append junk data after. The script itself is not reproduced in the text, only its behaviour.
Why this step: the receiver will only treat the incoming bytes as an XByteBuffer if they begin with the expected marker. Sending FLT2002 first is like showing the right membership card at the door; after that, the receiver accepts the rest of the bytes into the buffer. The junk data is the payload that will fill the memory, and the missing TLF2003 is what stops the receiver from ever closing the parcel.
By executing the above script, the following stack trace shows up on the Tomcat logs:

Stack trace
A stack trace is the list a program prints when something goes wrong, showing the chain of method calls that led to the failure, like the list of hands a parcel passed through before it was dropped. Here it points at the code path the researcher had been following.

Memory analysis

Free command
The free command is a standard Linux command that reports how much memory the machine has free and how much is in use.
Note: the author allocated 2G only to the Java Heap Space in order to avoid locks on his machine. This attack heavily depends on the allocated Heap space for that particular VM.
The Java heap is the pool of memory a Java program is allowed to use for its objects; the JVM (Java Virtual Machine) is the engine that runs Java programs, and it is given a maximum heap size when it starts. Limiting it to 2G kept the test from freezing the researcher's own computer. The point about dependence on heap size matters: a server with a large heap takes longer to fill, and a small one fills quickly.
Note 2: once the attack is made, the heap space isn't cleaned up, even after the attack is stopped. This will cause a permanent Denial Of Service condition until the next service restart.
A denial of service (DoS) means making a service unavailable to its legitimate users, without necessarily stealing anything. "Permanent until restart" is worse than a temporary slowdown: stopping the attack is not enough, someone has to restart the service.
Why this happens?
By following the stack trace, the vulnerable method is the append() of the XByteBuffer class, which, when a new buffer is bigger than the previous, it tries to allocate a new one by calling the expand method and passing the size of the newly created buffer (XByteBuffer#158):
public void expand(int newcount) {
//don't change the allocation strategy
byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, bufSize); // <-- vulnerable code
buf = newbuf;
}
Line by line:
public void expand(int newcount) {declares a method namedexpandthat takes one whole number,newcount, and returns nothing (void). It is called when the buffer needs more room.//don't change the allocation strategyis a comment warning future maintainers not to alter how memory is reserved.byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];creates a new, larger array of bytes.buf.length << 1doubles the current size (shifting left by one bit is the same as multiplying by two),Math.maxpicks the larger of that doubled size and the requestednewcount, andnew byte[...]reserves that much memory.System.arraycopy(buf, 0, newbuf, 0, bufSize); // <-- vulnerable codecopies the existing contents into the new array. The comment marks this as the vulnerable line.buf = newbuf;makes the variablebufpoint at the new, bigger array, and the old one becomes garbage.
With the result of allocating too much space for a single object. Because the buffer keeps being expanded to hold the appended data, too much space is allocated for a single object and the heap fills. The report does not discuss any authentication or validation on the cluster port; the only protection it mentions is the documentation change described below.
Authentication means proving who you are before being allowed in; validation means checking that incoming data is well formed and acceptable. Neither is described here as standing in the way.
What happens if I send fewer junk data?
It simply takes more time to fill the heap until it is full. In the tests, the author tried with 1000 A, 5 threads and 2g of memory for the JVM. It took around 15 seconds to fill up the heap space with the same results: heap full, cluster functionality unstable, high memory usage.
A thread is one line of execution inside a program; several threads can work at the same time, like several cashiers serving in parallel. Sending from 5 threads means five lines of execution pushing junk at the same time. The lesson is that the size of each junk chunk only changes the speed, not the outcome.
Further info & Remediation
While the Apache Tomcat Security team is aware of this issue, their "FIX" is just an update on the documentation that advises not to open the cluster port on untrusted networks.
Remediation means the steps taken to remove or reduce a risk. An untrusted network is any network you do not control, the internet being the obvious example. The practical advice is therefore about exposure: if the cluster port is reachable from outside, anyone can knock on it.


The """FIX"""
The two images show the documentation change that the report calls the fix, with the quotation marks around the word signalling the author's scepticism.
Moral of the story: don't open that door port on untrusted networks.

Glossary
- Apache Tomcat: a free, open-source Java web server and servlet container used to host Java web applications.
- ASCII: the standard table mapping characters to numbers, for example
Fis 70. - Authentication: the process of proving who you are before being allowed access.
- Buffer: a temporary area in memory where incoming data is collected before being processed.
- Byte: a small unit of data, a number from 0 to 255; a list of bytes can represent text or binary data.
- Class: a named block of code describing one kind of thing in a program.
- Cluster / clustering: grouping several servers so they share work and act as one service.
- CVE: a public catalogue number assigned to a known security vulnerability.
- Denial of service (DoS): making a service unavailable to its legitimate users.
- Deserialisation: turning a stream of bytes back into a live object in memory; unsafe deserialisation is a classic source of vulnerabilities.
- Header: a short fixed marker at the start of a message identifying what kind of message it is.
- Heap (Java heap): the pool of memory a Java program uses for its objects, with a maximum size set at start-up.
- Instantiate: to create a working copy of a class while a program runs.
- JVM (Java Virtual Machine): the engine that runs Java programs.
- Load balancer: a front-end component that spreads incoming requests across several servers.
- Loop: code that repeats, such as a listening loop that waits for incoming data.
- Method: a named action inside a class.
- POJO (plain old Java object): a simple data-holding object with no special machinery.
- Port: a numbered door on a computer that a particular service listens on; the cluster receiver uses port
4000. - Proof of concept (PoC): a minimal working demonstration that a vulnerability is real.
- RAID: a way of combining several disks so data is redundant and survives a disk failure.
- Remediation: the steps taken to fix or reduce a security risk.
- Replication: copying data, such as session state, between servers in a cluster.
- Serializable: the property of an object that lets it be turned into bytes and back.
- Socket: an open network connection between two programs.
- Source code: the human-written text that makes up a program.
- Stack trace: the list a program prints showing the chain of method calls that led to an error.
- Thread: one line of execution inside a program; several can run at once.
- Untrusted network: any network you do not control, such as the internet.
- Validation: checking that incoming data is well formed and acceptable.
- Variable: a name a program uses to refer to a value.
- Vulnerable: containing a weakness that can be exploited.
- WebSocket: a technology for two-way, long-lived connections between a browser and a server.