Expression DoS Vulnerability Found In Spring - CVE-2023-20861
A user can supply a crafted SpEL expression that uses the repeat operator to allocate a very large string, causing an OutOfMemoryError and denial of service. In older Spring versions, this can lead to a legitimate OutOfMemoryError.
Published on code-intelligence.com
Dae Glendowne2 min read
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
Spring Framework is one of the most widely used frameworks for building web applications in Java. Because so many applications depend on it, a vulnerability in the framework has an amplified impact on everything that relies on the affected version.
The issue described here is a denial-of-service condition reachable through SpEL, the Spring Expression Language. SpEL expressions can be supplied by a user in some application configurations, and evaluating them is normally a bounded operation. The report shows that the repeat operator inside SpEL can be abused to make evaluation fail with an OutOfMemoryError.
The work came out of ongoing efforts to improve the security of open-source software: the team continuously tests open-source projects with its JVM fuzzing engine Jazzer inside Google's OSS-Fuzz. One of those tests produced this finding, tracked as CVE-2023-20861.
Setup
The testing was performed with the JVM fuzzing engine Jazzer, run through the project's OSS-Fuzz integration.
The relevant version ranges are:
| Branch | Affected versions |
|---|---|
| 6.0.x | 6.0.0 to 6.0.6 |
| 5.3.x | 5.3.0 to 5.3.25 |
| 5.2.x | 5.2.0.RELEASE to 5.2.22.RELEASE |
Older, unsupported versions are also affected.
Finding it
Fuzzing with Jazzer surfaced a case where a crafted SpEL expression drives evaluation into an OutOfMemoryError. The trigger is the repeat operator, written as <string> * <integer>, which repeats a string a given number of times. Supplying a very large integer to that operator makes the evaluator attempt to build an enormous string.
The behaviour differs between branches. Spring Framework 6.0.x implements the repeat operator with String#repeat, which was introduced in Java 11. In that case the OutOfMemoryError is somewhat superficial, because no attempt is made to allocate a string of the requested size. In Spring Framework 5.3.x and previous versions, a StringBuilder is used to create the repeated text in a for-loop, which can lead to a legitimate OutOfMemoryError.
Exploitation
Step 1: supply a repeat expression with a large multiplier
The following SpEL expression triggers the issue:
'xyz' * 941859717
The left operand is the string to repeat and the right operand is the repeat count. With a count this large, evaluation of the expression results in an OutOfMemoryError.
Step 2: use the generated string as a gadget
The repeat operator can also be used as a gadget to easily generate large strings inside SpEL expressions. Those strings can then be passed to other SpEL operators, such as the matches operator, or to other components, potentially resulting in undesired effects.
Root cause
The repeat operator places no limit on the effective size of the repeated text. A user-supplied expression can therefore ask for a string far larger than memory allows. On 5.3.x and earlier the StringBuilder loop genuinely attempts to build that text, so the resulting OutOfMemoryError is a real allocation failure. On 6.0.x the use of String#repeat makes the error somewhat superficial, since no attempt is made to allocate a string of the requested size, but evaluation still fails with OutOfMemoryError.
The fix added limit checks for the effective size of repeated text and for the length of a regular expression supplied to the matches operator.
Impact
A user who can supply a SpEL expression can cause a denial-of-service condition through an OutOfMemoryError. Because Spring is so widely used, the report notes that vulnerabilities here have an amplified impact on all applications that rely on the vulnerable version.
The CVSS vector referenced in the report is:
CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L/RL:O
That vector describes a network-reachable, low-complexity attack requiring no privileges and no user interaction, with no confidentiality or integrity impact and a low availability impact. The weakness is classified as CWE-770, allocation of resources without limits or throttling.
Fix
Fixes have already been released. They include limit checks for the effective size of repeated text as well as for the length of a regular expression supplied to the matches operator.
Users of affected versions should upgrade:
- 6.0.x users should upgrade to 6.0.7+.
- 5.3.x users should upgrade to 5.3.26+.
- 5.2.x users should upgrade to 5.2.23.RELEASE+.
Users of older, unsupported versions should upgrade to 6.0.7+ or 5.3.26+. No other steps are necessary.
The authors thank the Spring maintainers for responding quickly to the issue and providing a fix and new releases.
Variants to test
- Other SpEL operators that build or transform strings, such as
matcheswith a very long regular expression. A positive result is evaluation that consumes excessive memory or CPU, or throwsOutOfMemoryError. - Chained expressions that feed the output of the repeat operator into another operator or component. A positive result is an unexpected failure or resource spike further down the evaluation path.
- The same repeat pattern on other expression engines and template languages that expose a repetition or multiplication operator. A positive result is an unbounded allocation driven by a user-supplied count.
- Applications that evaluate user-supplied SpEL at all. A positive result is any path where user input reaches expression evaluation without a size limit on the resulting values.
Update: a second Expression DoS in Spring
Three weeks after the announcement of CVE-2023-20861, Jazzer's OSS-Fuzz integration found a second, more severe Expression DoS vulnerability in Spring, tracked as CVE-2023-20863. The team published a separate blog post with mitigation advice for that issue.
What happened, in plain words
A team that tests open-source software for security problems found a way to make a Java web framework called Spring Framework run out of memory. They did it by feeding it a short piece of text written in a small expression language that Spring understands. The text asks Spring to repeat a word an enormous number of times. Spring evaluates the expression and raises an OutOfMemoryError, and the application can stop working. This kind of attack is called a denial of service: the goal is not to steal data or break in, it is to make the service unavailable to normal users.
The problem was given an official tracking number, CVE-2023-20861. A CVE is a public catalogue entry for a known security flaw, so that everyone can refer to the same issue by the same name. The team that found it was testing with an automated tool called Jazzer, which throws huge amounts of random input at software to see what breaks. Jazzer runs inside Google's OSS-Fuzz, a service that does this kind of automated testing for open-source projects for free.
Spring Framework is a set of ready-made building blocks that Java programmers use to build web applications. Because it is used in a very large number of applications, a flaw in the framework affects every application built on the affected version, not just one website. That is why the report says the impact is amplified.
Background: the pieces involved
Before the details, here are the main pieces, explained from scratch.
- Java: a programming language. Programs written in Java run on a Java runtime, which manages memory for them.
- Spring Framework: a collection of Java code that developers reuse instead of writing everything themselves. Think of it as a box of standard parts for building a web application.
- SpEL (Spring Expression Language): a small language built into Spring for writing short expressions, such as a calculation or a text operation, that Spring evaluates at runtime. An expression is a one-line instruction, a bit like a formula in a spreadsheet cell. Some applications let a user supply such an expression, for example in a configuration field or a template.
- Expression evaluation: the act of reading an expression and computing its result. Normally this is a quick, bounded operation.
- Denial of service (DoS): an attack that makes a service unusable for legitimate users, for example by exhausting its memory or processing power.
- OutOfMemoryError: the error a Java program raises when it cannot get more memory from the runtime. It is not a polite warning; it usually means the program can no longer continue normally.
- Fuzzing: automated testing that feeds a program large amounts of unusual or random input to find inputs that make it crash or misbehave.
- Jazzer: the JVM fuzzing engine used here. A JVM is the Java Virtual Machine, the runtime that executes Java programs, so a JVM fuzzer targets Java and other languages that run on the same runtime.
- OSS-Fuzz: Google's service that runs fuzzers continuously against open-source projects.
The setup
The testing was done with Jazzer, run through the project's OSS-Fuzz integration. In plain terms: the team connected their fuzzing tool to a service that runs such tools automatically, and let it hammer Spring with generated inputs.
Whether a given application allows that depends on how it was built.
The affected version ranges are:
| Branch | Affected versions |
|---|---|
| 6.0.x | 6.0.0 to 6.0.6 |
| 5.3.x | 5.3.0 to 5.3.25 |
| 5.2.x | 5.2.0.RELEASE to 5.2.22.RELEASE |
Older, unsupported versions are also affected. A version number like 6.0.6 identifies one specific release of the framework; the table lists the releases that contain the flaw.
Finding it
Fuzzing with Jazzer turned up an input where a crafted SpEL expression pushes evaluation into an OutOfMemoryError. The trigger is the repeat operator, written as <string> * <integer>. In SpEL, the asterisk means multiplication for numbers, but when the left side is text it means repetition: repeat this text this many times. Supplying a very large integer on the right makes the evaluator try to build an enormous string.
The behaviour differs between branches. Spring Framework 6.0.x implements the repeat operator with String#repeat, a method introduced in Java 11. In that branch the OutOfMemoryError is somewhat superficial, because no attempt is made to allocate a string of the requested size. In Spring Framework 5.3.x and previous versions, a StringBuilder is used to create the repeated text in a for-loop, which can lead to a legitimate OutOfMemoryError. A StringBuilder is a Java object for assembling text piece by piece, and a for-loop is a programming construct that repeats a block of code a set number of times. So on the older branch the program really does try to build the giant text, one copy at a time, which can lead to a legitimate OutOfMemoryError.
Exploitation
Step 1: supply a repeat expression with a large multiplier
The following SpEL expression triggers the issue:
'xyz' * 941859717
Line by line, in plain words:
'xyz'is the text to repeat. The single quotes mark it as a string, meaning text rather than a number or a variable name.*is the repeat operator here, because the left side is text. It means "repeat the left side as many times as the right side says".941859717is the repeat count, a little over nine hundred million.- The whole line is one expression. When Spring evaluates it, it is asked to produce a string made of
xyzrepeated 941859717 times, which is far more text than memory can hold.
With a count this large, evaluation of the expression results in an OutOfMemoryError. The reason this matters is that the expression is short and cheap to send, while the work it asks for is enormous. That asymmetry is what makes it an attack rather than a mistake.
Step 2: use the generated string as a gadget
The repeat operator can also be used as a gadget to easily generate large strings inside SpEL expressions. A gadget, in security language, is a small existing feature that an attacker reuses for a purpose it was not designed for. Those generated strings can then be passed to other SpEL operators, such as the matches operator, or to other components, potentially resulting in undesired effects. The matches operator checks text against a pattern, called a regular expression, and a very large input can make that check expensive too.
Root cause
The repeat operator places no limit on the effective size of the repeated text. A user-supplied expression can therefore ask for a string far larger than memory allows. On 5.3.x and earlier the StringBuilder loop genuinely attempts to build that text, so the resulting OutOfMemoryError is a real allocation failure. On 6.0.x the use of String#repeat makes the error somewhat superficial, since no attempt is made to allocate a string of the requested size, but evaluation still fails with OutOfMemoryError.
The fix added limit checks for the effective size of repeated text and for the length of a regular expression supplied to the matches operator. A limit check is simply a rule that refuses a request that is too big, the way a lift refuses to take more than its stated weight.
Impact
A user who can supply a SpEL expression can cause a denial-of-service condition through an OutOfMemoryError. Because Spring is so widely used, the report notes that vulnerabilities here have an amplified impact on all applications that rely on the vulnerable version.
The CVSS vector referenced in the report is:
CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L/RL:O
CVSS is a standard scoring system for security flaws. The vector is a compact string of codes that describes the characteristics of the issue. Read piece by piece:
AV:Nmeans the attack can be carried out over the network, not only by someone sitting at the machine.AC:Lmeans the attack is of low complexity: no special conditions are needed.PR:Nmeans no privileges are required; the attacker does not need an account with special rights.UI:Nmeans no user interaction is needed; nobody has to click anything for the attack to work.S:Umeans the impact stays within the affected component rather than spreading to other components.C:Nmeans no confidentiality impact: no data is exposed.I:Nmeans no integrity impact: no data is altered.A:Lmeans a low availability impact: the service can be disrupted, but the rating is low.RL:Orefers to the remediation level, here official fix available.
The weakness is classified as CWE-770, allocation of resources without limits or throttling. CWE is a catalogue of weakness types, and this entry means the software hands out a resource, here memory, without any cap on how much can be requested.
Fix
Fixes have already been released. They include limit checks for the effective size of repeated text as well as for the length of a regular expression supplied to the matches operator. In other words, the framework now refuses expressions that would ask for an unreasonable amount of text, and refuses overly long patterns passed to matches.
Users of affected versions should upgrade:
- 6.0.x users should upgrade to 6.0.7+.
- 5.3.x users should upgrade to 5.3.26+.
- 5.2.x users should upgrade to 5.2.23.RELEASE+.
Users of older, unsupported versions should upgrade to 6.0.7+ or 5.3.26+. No other steps are necessary. Upgrading means replacing the version of the framework your application uses with a newer one that contains the fix; the plus sign means that version or any later one.
The authors thank the Spring maintainers for responding quickly to the issue and providing a fix and new releases.
Variants to test
If you want to look for similar problems elsewhere, the report suggests these directions. For each, a positive result is described, so you know what counts as a finding.
- Other SpEL operators that build or transform strings, such as
matcheswith a very long regular expression. A positive result is evaluation that consumes excessive memory or CPU, or throwsOutOfMemoryError. - Chained expressions that feed the output of the repeat operator into another operator or component. A positive result is an unexpected failure or resource spike further down the evaluation path.
- The same repeat pattern on other expression engines and template languages that expose a repetition or multiplication operator. A positive result is an unbounded allocation driven by a user-supplied count.
- Applications that evaluate user-supplied SpEL at all. A positive result is any path where user input reaches expression evaluation without a size limit on the resulting values.
Update: a second Expression DoS in Spring
Three weeks after the announcement of CVE-2023-20861, Jazzer's OSS-Fuzz integration found a second, more severe Expression DoS vulnerability in Spring, tracked as CVE-2023-20863. The team published a separate blog post with mitigation advice for that issue.
Glossary
- API: a defined way for one piece of software to talk to another, like a service counter with fixed rules for what you can ask for.
- CVE: a public catalogue entry that gives a known security flaw a unique identifier.
- CVSS: a standard scoring system that describes the characteristics and severity of a security flaw.
- CWE: a catalogue of weakness types, used to classify what kind of mistake caused a flaw.
- Denial of service (DoS): an attack that makes a service unusable for legitimate users, often by exhausting memory or processing power.
- Expression: a short instruction, such as a calculation or text operation, that software evaluates to produce a result.
- Fuzzing: automated testing that feeds a program large amounts of unusual or random input to find inputs that make it misbehave.
- Gadget: an existing feature reused by an attacker for a purpose it was not designed for.
- Java: a programming language whose programs run on a Java runtime that manages memory for them.
- Jazzer: a fuzzing engine for programs that run on the Java Virtual Machine.
- JVM (Java Virtual Machine): the runtime that executes Java programs.
- Limit check: a rule that refuses a request that is too large.
- Regular expression: a pattern used to describe and match text.
- Repeat operator: in SpEL, the
*operator applied to text, which repeats that text a given number of times. - SpEL (Spring Expression Language): a small expression language built into Spring Framework.
- Spring Framework: a widely used collection of Java building blocks for web applications.
- String: a piece of text in programming.
- StringBuilder: a Java object for assembling text piece by piece.
- for-loop: a programming construct that repeats a block of code a set number of times.
String#repeat: a Java method, introduced in Java 11, that returns text repeated a given number of times.