Skip to content
RCE Write-up Advanced

Potential Remote Code Execution Vulnerability Discovered In HSQLDB

HSQLDB allowed SQL statements to call any static Java method from the class path by default, so an attacker who could supply untrusted input to a statement could execute arbitrary code. The vulnerability was found in the parsing of binary and text format data.

Code Intelligence Follow

Published on code-intelligence.com

Roman Wagner2 min read

Code Intelligence (@CI_Fuzz), on code-intelligence.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

HSQLDB (HyperSQL DataBase) is a SQL relational database system written in Java, maintained by the HSQL Development Group. It is used for development, testing and deployment of database applications, and it is commonly embedded in other software. The finding described here concerns the parsing procedure for binary and text format data in java.sql.Statement and java.sql.PreparedStatement, and specifically the ability of SQL statements to call static methods from Java classes on the class path.

The vulnerability was found while testing open-source projects with Jazzer, the fuzzer from Code Intelligence, in collaboration with OSS-Fuzz. The test run produced a severe finding with a potential remote code execution, tracked as CVE-2022-41853.

Setup

The testing was carried out with Jazzer against HSQLDB as part of the OSS-Fuzz integration. The affected versions are all versions up to and including 2.7.0. The report does not describe any accounts, roles or special configuration needed to reach the vulnerable code path beyond the application using java.sql.Statement or java.sql.PreparedStatement with untrusted input.

Finding it

By default, HSQLDB allows SQL statements to call any static method from any Java class in the class path. The database permitted direct use of methods, for example:

Text
call org.hsqldb.clazz.meth()

This means a SQL statement is not limited to querying and manipulating data: it can name a Java class and a static method, and HSQLDB will invoke it. When the SQL text reaching java.sql.Statement or java.sql.PreparedStatement comes from an untrusted source, that invocation primitive is reachable by an attacker.

Exploitation

Step 1: reach a statement that processes untrusted input

The application must use java.sql.Statement or java.sql.PreparedStatement in HSQLDB with input that an attacker can influence. The parsing procedure for binary and text format data is the code path involved.

Step 2: supply a statement that calls a static Java method

Because static method calls are allowed by default, the attacker can submit a statement of the form call org.hsqldb.clazz.meth(), naming any class on the class path and any of its static methods. HSQLDB resolves the class and invokes the method.

Step 3: obtain code execution

By choosing a suitable class and static method, an attacker may be able to turn the SQL injection point into execution of Java code. The report characterises this as a potential remote code execution; it does not publish a specific gadget class or method chain.

Root cause

HSQLDB's default behaviour permits SQL statements to target any static Java method in any class on the class path, with the single documented exception of methods in java.lang.Math. The hsqldb.method_class_names property governs which classes can be used as routine targets; without it, static methods other than those in java.lang.Math should not be allowed. The report does not go into the internals of the parser beyond naming the parsing procedure for binary and text format data in java.sql.Statement and java.sql.PreparedStatement as the location of the flaw.

Impact

The issue is rated Critical with a CVSS base score of 9.8. Applications using java.sql.Statement or java.sql.PreparedStatement in HSQLDB with untrusted input may be vulnerable to a remote code execution attack.

The reach of the library makes the exposure broad. HSQLDB ranks 139th in the Maven repository and 2nd among embedded SQL databases, and it is used by more than 3,113 Maven packages, including LibreOffice, JBoss, Log4j, Hibernate and Spring-Boot, the last having thousands of usages with the potential of transitive risk, as well as various enterprise software solutions. The library has been downloaded over 100 million times. Consumers that pass untrusted input into statements may be vulnerable.

Fix

If HSQLDB is used to process queries containing user input, the hsqldb.method_class_names property must be defined with a list of class names or wildcards when a static Java method is used as the target of an HSQLDB routine. Without a property definition, the use of Java static methods, except those in java.lang.Math, should not be allowed. Developers can prevent the issue by defining the system property, for example:

Java
System.setProperty("hsqldb.method_class_names", "abc");

or

Java
java -Dhsqldb.method_class_names="abc"

The issue is already fixed upstream and will be available in the next release. From version 2.7.1, the property hsqldb.method_class_names must be defined with a list of class names or wildcards if any Java static method is used as an HSQLDB routine target.

Variants to test

A positive result is a statement that names a class and static method and causes the method to run. 2. Check whether the hsqldb.method_class_names property is set in applications that embed HSQLDB. A positive result is an application that passes user input into statements while the property is undefined, leaving static method calls open. 3. Trace transitive dependencies for HSQLDB versions up to and including 2.7.0. A positive result is a dependency tree that pulls in an affected version behind a component that accepts untrusted SQL. A positive result is any class outside java.lang.Math whose static methods remain callable when the property is not configured.

References

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 8 min read.

What happened, in plain words

HSQLDB (HyperSQL DataBase) is a database program written in Java. A database is software that stores information in tables and answers questions about it, and Java is a programming language in which many server applications are written. HSQLDB is often built into other software rather than run on its own, so a lot of programs carry it inside them without their users ever noticing.

A team at Code Intelligence was testing HSQLDB with a tool called Jazzer. Jazzer is a fuzzer: a program that throws large amounts of automatically generated, often strange input at software to see whether anything breaks. The testing was done together with OSS-Fuzz, a Google-run service that runs fuzzers on open-source projects for free. One of those test runs turned up a serious problem: a potential way in which a crafted database query could make the database run Java code. That kind of flaw is called remote code execution, or RCE, and it was given the identifier CVE-2022-41853. A CVE is a public catalogue number for a known security flaw, so that everyone can refer to the same issue unambiguously.

The rest of this article explains what the flaw is, why it exists, who is affected, and how to protect an application that uses HSQLDB.

Vulnerability Description

The flaw sits in two Java interfaces that programs use to talk to a database: java.sql.Statement and java.sql.PreparedStatement. Think of an interface here as a standard plug shape: any database that speaks Java's standard database language offers these two plugs, and an application sends its SQL through them. SQL is the language used to ask a database questions such as "give me all orders from last week".

The problem is in the part of HSQLDB that parses, that is reads and interprets, data arriving in binary and text format. By default, HSQLDB let a SQL statement call any static method from any Java class that was on the class path. Two terms need unpacking:

  • A class is a blueprint in Java that groups related code and data, for example a class that handles files or one that starts other programs.
  • A static method is a piece of code belonging to a class as a whole rather than to one particular object made from it. You can call it by naming the class and the method, without creating anything first.
  • The class path is the list of places Java looks for classes when the program runs. Everything the application ships with, plus its libraries, is on it.

So HSQLDB allowed direct use of methods, for example:

Text
call org.hsqldb.clazz.meth()

Line by line: call is the SQL keyword that asks the database to run a routine, that is a named piece of code. org.hsqldb.clazz is the name of a Java class, written with dots separating the package and the class, the way a postal address separates country, city and street. meth() is the method being called, with empty brackets because it takes no arguments. In plain words, the statement says: "database, please run this Java method for me".

That is convenient for a developer who wants to reuse Java code from inside SQL. It is dangerous when the SQL text comes from someone else, because the person writing the SQL can name any class on the class path, not just the ones the developer had in mind.

Affected versions: all versions <= 2.7.0.

Impact of CVE-2022-41853

The severity is Critical, with a CVSS Base Score of 9.8. CVSS is a standard scoring system for security flaws; scores run from 0 to 10, and 9.8 is near the top of the scale, meaning the flaw is easy to reach and can cause severe damage.

Applications that are using java.sql.Statement or java.sql.PreparedStatement in HSQLDB with untrusted input may be vulnerable to a remote code execution attack. "Untrusted input" means any text that an outsider can influence: a search box, a login form, a URL parameter, a file upload, a message from another system. If such text ends up in a SQL statement, the outsider can try to smuggle in a call of their own.

The reach of the library makes this broad. HSQLDB ranks 139th in the Maven repository and 2nd in embedded SQL databases. Maven is the main public library catalogue for Java, so a high rank means the library is pulled into a great many projects. It is used by more than 3,113 Maven packages, including LibreOffice, JBoss, Log4j, Hibernate, Spring-Boot (having thousands of usages with the potential of transitive risk), and various enterprise software solutions. "Transitive risk" means the danger travels indirectly: you may never have chosen HSQLDB yourself, but a library you chose may have chosen it for you, the way a guest brings a guest to a party.

Remediation and Mitigation

Remediation means fixing the flaw; mitigation means reducing the risk while you wait for, or instead of, a fix. The fix here is a configuration setting, a named property that the database reads at startup.

If HSQLDB is used to process queries with user input, the hsqldb.method_class_names property must be defined with a list of class names or wildcards in case a static Java method is used as a target of an HSQLDB routine. A wildcard is a pattern that matches several names at once, like * meaning "anything". Without a property definition, the use of Java static methods, except those in java.lang.Math, should not be allowed. java.lang.Math is Java's built-in collection of arithmetic helpers such as square root and rounding; it is harmless, which is why it is the one exception.

Developers can prevent the issue by defining the system property. For example:

Java
System.setProperty("hsqldb.method_class_names", "abc");

Line by line: System is Java's built-in handle on the running program's settings. .setProperty is the instruction that writes one setting. The first string, "hsqldb.method_class_names", is the name of the setting. The second string, "abc", is its value: here a placeholder standing for the list of class names you actually want to allow. Only classes named in that list may then be called from SQL.

or

Java
java -Dhsqldb.method_class_names="abc"

Line by line: java starts a Java program. -D is the command-line flag that sets a system property before the program begins, and the rest is the property name, an equals sign, and the value. This form is useful when you cannot change the code and can only change how the program is launched.

The issue is already fixed upstream and will be available in the next release. "Upstream" means the original authors of the library, as opposed to the teams that merely use it. From version 2.7.1, the property hsqldb.method_class_names must be defined with a list of class names or wild cards if any Java static method is used as an HSQLDB routine target. In other words, from 2.7.1 you must define the property if you want to call any Java static method.

Variants to test

  1. Look for other SQL engines embedded in Java applications that expose a call-style syntax for Java methods. A positive result is a statement that names a class and static method and causes the method to run. The idea is that HSQLDB is unlikely to be the only engine with this convenience feature.
  2. Check whether the hsqldb.method_class_names property is set in applications that embed HSQLDB. A positive result is an application that passes user input into statements while the property is undefined, leaving static method calls open. This is a configuration review rather than an attack: you are looking for the missing lock, not picking it.
  3. Trace transitive dependencies for HSQLDB versions up to and including 2.7.0. A positive result is a dependency tree that pulls in an affected version behind a component that accepts untrusted SQL. A dependency tree is the full list of libraries a program needs, including the ones its libraries need.
  4. Test whether the java.lang.Math exception can be combined with other reachable classes. A positive result is any class outside java.lang.Math whose static methods remain callable when the property is not configured.

References

Glossary

  • API: a defined way for one piece of software to ask another to do something, like a restaurant menu that lists what you may order.
  • Class: a blueprint in Java that groups related code and data.
  • Class path: the list of places Java searches for classes when a program runs.
  • Configuration property: a named setting that changes how a program behaves, usually read at startup.
  • CVE: a public catalogue number for a known security flaw.
  • CVSS: a standard 0 to 10 score for the severity of a security flaw.
  • Database: software that stores information in tables and answers questions about it.
  • Dependency tree: the full list of libraries a program needs, including those its libraries need.
  • Embedded database: a database that runs inside another application rather than as a separate server.
  • Fuzzer: a tool that feeds large amounts of generated input to software to find crashes and flaws.
  • HSQLDB: HyperSQL DataBase, a SQL database written in Java.
  • Input: any data that reaches a program, especially data an outsider can influence.
  • Java: a widely used programming language, common on servers.
  • Jazzer: the fuzzer from Code Intelligence used in this testing.
  • Maven: the main public catalogue and build tool for Java libraries.
  • Method: a named piece of code inside a class.
  • Mitigation: reducing the risk of a flaw without fully fixing it.
  • OSS-Fuzz: a service that runs fuzzers on open-source projects.
  • Parsing: reading and interpreting incoming text or binary data according to a grammar.
  • Remote code execution (RCE): a flaw that lets an outsider make a system run code of their choosing.
  • Remediation: the actual fix for a flaw.
  • Routine: a named piece of code a database can be asked to run.
  • SQL: the language used to ask a database questions and change its data.
  • SQL injection: slipping extra SQL into input so that it is executed as part of a query.
  • Static method: code belonging to a class as a whole, callable by naming the class and method.
  • Transitive risk: danger that reaches you through a library you chose, rather than one you picked yourself.
  • Untrusted input: data from a source you do not control.
  • Upstream: the original authors of a library, as opposed to the teams using it.
  • Wildcard: a pattern that matches several names at once.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.