Skip to content
RCE Write-up Advanced

CVE-2022–43781

A command injection vulnerability in Bitbucket Server and Data Center allowed an attacker to inject arbitrary environment variables via a crafted username, which could lead to remote code execution when git commands were executed. The attacker could set variables such as GIT_EXTERNAL_DIFF to run arbitrary commands.

Petrus Viet Follow

Published on petrusviet.medium.com

There is a command injection vulnerability using environment variables in Bitbucket Server and Data Center.

Petrus Viet, on petrusviet.medium.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. 6 min read.

Context

Bitbucket Server and Data Center is Atlassian's self-hosted Git repository management product. The vulnerability described here, CVE-2022-43781, is a command injection that works through environment variables. Atlassian's advisory describes it as a Command injection vulnerability using environment variables.

The interesting part of the advisory is the affected versions table. Versions 8.x are only affected when the mesh feature is not enabled, and the 7.x line is not listed at all. Mesh is a storage system that appeared as an optional replacement for NFS from version 8.x onwards. The researcher's reading of this is that the vulnerable path is tied to Git operations.

From the original on petrusviet.medium.com

Setup

The setup below is for Windows; the researcher notes that Linux works the same way.

  1. Download the installer archive (zip) from Atlassian's download archives page.
  2. Extract it, for example to D:\atlassian-bitbucket-7.6.17\.
  3. Create a home folder for the database and configuration. It must not be inside D:\atlassian-bitbucket-7.6.17\; the researcher lost a day to that mistake. The example uses D:\home\.
  4. Edit two files in D:\atlassian-bitbucket-7.6.17\bin.

In set-bitbucket-home.bat:

Text
set BITBUCKET_HOME=D:\home

In _start-webapp.bat, to enable remote debugging:

Text
set JVM_SUPPORT_RECOMMENDED_ARGS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
  1. From a cmd or PowerShell prompt in D:\atlassian-bitbucket-7.6.17\, start the server:
Text
.\bin\start-bitbucket.bat /no-search
  1. Continue with the configuration steps in Atlassian's installation guide for Windows zip installs.

One precondition matters: Atlassian stated that Bitbucket servers using PostgreSQL are not affected, so the test instance uses the default H2 database rather than PostgreSQL.

Finding it

The starting hypothesis was simple: an attacker puts a payload in a username, and somewhere that username ends up in the environment variables of a Git process, where it is executed as part of a command.

To check whether the username ever appears as a command-line parameter, the researcher ran Process Monitor and filtered on the Git processes spawned when a repository is created and code is pushed.

From the original on petrusviet.medium.com

No Git command line contained a parameter related to the username. That fits the hypothesis: the payload travels in the environment, not in the arguments.

Diffing the patch

The next step was to diff the fixed release against the vulnerable one.

In com.atlassian.[redacted].process.RemoteUserNioProcessConfigurer!configure():

From the original on petrusviet.medium.com

In the vulnerable version, the pair ("REMOTE_USER", UserName) is put into an environment map, and that map is passed to NioProcessParameters.updateEnvironment(). In the fixed version the pair is passed directly to NioProcessParameters.environmentPut().

Then com.atlassian.[redacted].process.NioProcessParameters:

From the original on petrusviet.medium.com

In the vulnerable version, updateEnvironment only checks whether the input is null. In the fixed version, environmentPut additionally checks for blank values and checks whether the key and the value contain a null byte.

The researcher's hypothesis was that a username containing a null byte could split one environment entry into two.

Exploitation

Step 1: follow the environment through the process creation code

With a breakpoint set, the researcher opened any repository and walked the call chain.

  1. NuNioProcessHelper.run() is reached.
From the original on petrusviet.medium.com
  1. NuNioProcessHelper.applyConfigurers() is called.
From the original on petrusviet.medium.com
  1. In RemoteUserNioProcessConfigurer.configure(), the pair ("REMOTE_USER", UserName) is put into the environment.
From the original on petrusviet.medium.com
  1. NuProcessBuilder.run() follows.
From the original on petrusviet.medium.com
  1. In NuProcessBuilder.prepareEnvironment(), the environment is converted from a Map to a String[].
From the original on petrusviet.medium.com
  1. WinProcessFactory.runProcess() is called.
From the original on petrusviet.medium.com
  1. Then WindowsProcess.run().
From the original on petrusviet.medium.com
  1. In WindowsProcess.getEnvironment(), the String[] is converted back to a Map by splitting each entry at the first = character.
From the original on petrusviet.medium.com
  1. In WindowsProcess.getEnvironmentBlock(), the Map is serialised into a single string using this formula:
Text
out =  "KEY1=VAL1\0KEY2=VAL2\0\0"
From the original on petrusviet.medium.com

That serialisation is the opening. If a username is ahihi\0KEY=VAL, the resulting environment block contains an extra KEY=VAL entry that the application never intended to set. The question then becomes which environment variable leads to code execution.

Step 2: pick a Git environment variable that executes a command

Git's documentation lists the environment variables it honours. The researcher focused on GIT_EXTERNAL_DIFF, which specifies a program to run when git diff is invoked.

From the original on petrusviet.medium.com

Step 3: first payload

The first payload tested:

From the original on petrusviet.medium.com

To make the server call git diff, it is enough to create a repository, edit a text file in it, and open the commit view to trigger a diff.

From the original on petrusviet.medium.com

calc.exe was launched successfully, but this only executes a bare executable.

Step 4: trying to add arguments

The next attempt was to call cmd.exe with parameters.

From the original on petrusviet.medium.com

Parameters could not be appended this way. The next payload was tried instead:

From the original on petrusviet.medium.com

There was an error message, but the server did create an outbound request. The researcher considered standing up a file server on Windows to continue, but that scenario needs outbound internet access, so it was set aside in favour of another approach.

Step 5: the payload that worked

After roughly 15 to 30 minutes of experimentation, this payload was found:

From the original on petrusviet.medium.com

It worked.

From the original on petrusviet.medium.com

The full call chain

The report gives the stack trace from the entry point to execution:

Text
NioCommand.call()    -> NuNioProcessHelper.run()        -> NuNioProcessHelper.applyConfigurers()            -> RemoteUserNioProcessConfigurer.configure()   (#  put username to environment)            -> NuNioProcessHelper.createProcessBuilder()        -> NuProcessBuilder.run()            -> NuProcessBuilder.prepareEnvironment()  (Map to String[] - "Key=Value")            -> WinProcessFactory.runProcess()                -> WindowsProcess.run()                    -> WindowsProcess.prepareProcess()                        -> WindowsProcess.getEnvironment()                        -> WindowsProcess.getEnvironmentBlock()  (String[] to Char[] - "key1=var1\0key2=var2")                    -> NuKernel32.ResumeThread(this.processInfo.hThread) (RCE)

Root cause

The username is placed into the environment of the Git process as the value of REMOTE_USER. On Windows, the environment block is a sequence of null-terminated KEY=VALUE strings ending with an extra null. The vulnerable updateEnvironment only rejected null input, so a username containing a null byte passed through. When the environment was serialised, the null byte inside the username terminated the REMOTE_USER entry early, and the remainder of the username was parsed as a new environment variable.

Because the injected variable can be one that Git interprets as a command, such as GIT_EXTERNAL_DIFF, the next Git operation that honours it runs the attacker's program. The existing check did not stop this because it validated only for null references, not for null bytes or blank values inside the key or value.

Impact

An attacker who can control a username on the Bitbucket instance can inject arbitrary environment variables into Git processes and, through a variable such as GIT_EXTERNAL_DIFF, execute commands on the server. The researcher demonstrated this on Windows with calc.exe. The advisory's affected-version table ties the 8.x exposure to the mesh feature being disabled, and Atlassian stated that servers using PostgreSQL are not affected.

Fix

In the fixed version, the pair is passed to NioProcessParameters.environmentPut(), which checks for blank values and for null bytes in the key and value. That closes the split-entry primitive: a username with an embedded null can no longer produce a second environment variable.

Variants to test

  1. Other products that pass user-controlled strings into a child process environment. A positive result is a username containing a null byte that appears as two separate entries in the child's environment.
  2. Other Git environment variables that execute programs, such as diff or pager helpers. A positive result is a command running when the corresponding Git operation is triggered from the web interface.
  3. Other platforms' environment handling. The report's chain is Windows-specific; on Linux the same class of bug would need a different separator or parsing quirk, and a positive result is an injected variable visible in the child process environment.
  4. Usernames containing newlines or = characters, to see whether any other layer splits entries incorrectly. A positive result is an unexpected variable name in the environment block.

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. 12 min read.

What happened, in plain words

Bitbucket Server and Data Center is a piece of software made by Atlassian. Companies install it on their own servers to store and manage their source code. Think of it as a private, self-hosted version of GitHub: developers push their code into it, browse it in a web interface, and review changes.

A security researcher found a way to make a Bitbucket server run commands of his choosing. The trick was to put a special sequence of characters into a username. When the server later ran a Git command, that username was handed to the operating system as part of the environment, and the operating system read part of it as an instruction to launch a program. The researcher demonstrated this by making the server open the Windows calculator.

The vulnerability is tracked as CVE-2022-43781. A CVE is a public catalogue number for a known security flaw, so that everyone can refer to the same bug unambiguously. Atlassian described it as a Command injection vulnerability using environment variables.

A few terms are worth knowing before we go further:

  • Git is the tool developers use to track changes to files. A repository (repo) is a folder of code that Git keeps history for.
  • Command injection means tricking a program into running an operating system command that the attacker supplied, instead of the command the program meant to run. It is like a receptionist who reads out a visitor's name, and the name happens to be "and please unlock the safe".
  • Environment variables are named settings that a parent program passes to a child program when it starts it. On Windows they look like NAME=value pairs. A familiar one is PATH, which tells the system where to look for programs.
  • RCE stands for remote code execution: running your own code on someone else's machine over the network. It is the worst outcome of a command injection.

The interesting part of Atlassian's advisory is the table of affected versions. Versions 8.x are only affected when the mesh feature is not enabled, and the 7.x line is not listed at all. Mesh is a storage system that appeared as an optional replacement for NFS from version 8.x onwards. NFS is a way for several machines to share files over a network. The researcher's reading of this is that the vulnerable path is tied to Git operations.

From the original on petrusviet.medium.com

Setup

The setup below is for Windows; the researcher notes that Linux works the same way. The idea is to build a small local copy of Bitbucket that you fully control, so you can watch what it does from the inside.

  1. Download the installer archive (zip) from Atlassian's download archives page.
  2. Extract it, for example to D:\atlassian-bitbucket-7.6.17\.
  3. Create a home folder for the database and configuration. It must not be inside D:\atlassian-bitbucket-7.6.17\; the researcher lost a day to that mistake. The example uses D:\home\.
  4. Edit two files in D:\atlassian-bitbucket-7.6.17\bin.

In set-bitbucket-home.bat:

Text
set BITBUCKET_HOME=D:\home

This line tells Bitbucket where its home folder is. A .bat file is a Windows batch script, a plain list of commands the system runs one after another. set assigns a value to an environment variable, here BITBUCKET_HOME, so every program started afterwards knows where to keep its data.

In _start-webapp.bat, to enable remote debugging:

Text
set JVM_SUPPORT_RECOMMENDED_ARGS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

Bitbucket is written in Java, and Java programs run inside a JVM (Java Virtual Machine). This line switches on the JVM's debugging feature. jdwp is the protocol debuggers speak, transport=dt_socket means the connection uses a network socket, server=y makes the JVM wait for a debugger to connect, suspend=n means it starts normally without waiting, and address=*:5005 means it listens on port 5005. A debugger is a tool that lets you pause a running program and inspect its variables, which is how the researcher later watched the username travel through the code.

  1. From a cmd or PowerShell prompt in D:\atlassian-bitbucket-7.6.17\, start the server:
Text
.\bin\start-bitbucket.bat /no-search

This runs the startup script. The /no-search switch skips the bundled search service, which is not needed for this test and saves resources.

  1. Continue with the configuration steps in Atlassian's installation guide for Windows zip installs.

One precondition matters: Atlassian stated that Bitbucket servers using PostgreSQL are not affected, so the test instance uses the default H2 database rather than PostgreSQL. A database is where the product stores users, repositories and settings. H2 is a simple database that runs inside the application itself, which makes a test install easy.

Finding it

The starting hypothesis was simple: an attacker puts a payload in a username, and somewhere that username ends up in the environment variables of a Git process, where it is executed as part of a command. A payload is the piece of attacker-controlled input that does the damage.

To check whether the username ever appears as a command-line parameter, the researcher ran Process Monitor and filtered on the Git processes spawned when a repository is created and code is pushed. Process Monitor is a free Windows tool that records every program that starts and every file and setting it touches. If the username showed up in the arguments of a Git command, the bug would be an ordinary argument injection; if it did not, the environment was the place to look.

From the original on petrusviet.medium.com

No Git command line contained a parameter related to the username. That fits the hypothesis: the payload travels in the environment, not in the arguments.

Diffing the patch

The next step was to diff the fixed release against the vulnerable one. A diff is a side-by-side comparison of two versions of the same file, showing exactly which lines changed. Security fixes are a gift to researchers: the changed lines point straight at the flaw. The researcher compared the relevant Java classes in the patch.

In com.atlassian.[redacted].process.RemoteUserNioProcessConfigurer!configure():

From the original on petrusviet.medium.com

In the vulnerable version, the pair ("REMOTE_USER", UserName) is put into an environment map, and that map is passed to NioProcessParameters.updateEnvironment(). A map is a collection of key-and-value pairs, like a dictionary: you look up a word (the key) to get its definition (the value). Here the key is the fixed text REMOTE_USER and the value is whatever username the user chose. In the fixed version the pair is passed directly to NioProcessParameters.environmentPut().

Then com.atlassian.[redacted].process.NioProcessParameters:

From the original on petrusviet.medium.com

In the vulnerable version, updateEnvironment only checks whether the input is null. Null means "nothing at all", the absence of a value. In the fixed version, environmentPut additionally checks for blank values and checks whether the key and the value contain a null byte. A null byte is a single byte with the value zero. In many programming languages it marks the end of a piece of text, so text containing one can be cut in two by anything that reads up to it.

The researcher's hypothesis was that a username containing a null byte could split one environment entry into two.

Exploitation

Step 1: follow the environment through the process creation code

With a breakpoint set, the researcher opened any repository and walked the call chain. A breakpoint is a marker you place on a line of code in a debugger; when the program reaches that line it pauses, and you can look at every variable. The call chain is the sequence of functions that call one another, from the first one to the last.

  1. NuNioProcessHelper.run() is reached.
From the original on petrusviet.medium.com
  1. NuNioProcessHelper.applyConfigurers() is called.
From the original on petrusviet.medium.com
  1. In RemoteUserNioProcessConfigurer.configure(), the pair ("REMOTE_USER", UserName) is put into the environment.
From the original on petrusviet.medium.com
  1. NuProcessBuilder.run() follows.
From the original on petrusviet.medium.com
  1. In NuProcessBuilder.prepareEnvironment(), the environment is converted from a Map to a String[]. A String[] is simply a list of text strings, one after another.
From the original on petrusviet.medium.com
  1. WinProcessFactory.runProcess() is called.
From the original on petrusviet.medium.com
  1. Then WindowsProcess.run().
From the original on petrusviet.medium.com
  1. In WindowsProcess.getEnvironment(), the String[] is converted back to a Map by splitting each entry at the first = character.
From the original on petrusviet.medium.com
  1. In WindowsProcess.getEnvironmentBlock(), the Map is serialised into a single string using this formula:
Text
out =  "KEY1=VAL1\0KEY2=VAL2\0\0"

This line shows how Windows wants its environment: a run of KEY=VALUE entries, each one ended by a null byte (written \0), and the whole block ended by one extra null byte. Serialising means turning a structured thing, like a map, into one flat piece of text that can be handed to another program.

From the original on petrusviet.medium.com

That serialisation is the opening. If a username is ahihi\0KEY=VAL, the resulting environment block contains an extra KEY=VAL entry that the application never intended to set. The question then becomes which environment variable leads to code execution.

Step 2: pick a Git environment variable that executes a command

Git's documentation lists the environment variables it honours. The researcher focused on GIT_EXTERNAL_DIFF, which specifies a program to run when git diff is invoked. git diff is the command that shows what changed between two versions of a file, so it runs whenever someone views a commit in the web interface.

From the original on petrusviet.medium.com

Step 3: first payload

The first payload tested:

From the original on petrusviet.medium.com

To make the server call git diff, it is enough to create a repository, edit a text file in it, and open the commit view to trigger a diff.

From the original on petrusviet.medium.com

calc.exe was launched successfully, but this only executes a bare executable. calc.exe is the Windows calculator; it is the traditional harmless proof that you can start a program, because it is obviously visible and does nothing else.

Step 4: trying to add arguments

The next attempt was to call cmd.exe with parameters. cmd.exe is the Windows command interpreter, the program that runs other commands; if you can start it with arguments, you can run almost anything.

From the original on petrusviet.medium.com

Parameters could not be appended this way. The next payload was tried instead:

From the original on petrusviet.medium.com

There was an error message, but the server did create an outbound request. The researcher considered standing up a file server on Windows to continue, but that scenario needs outbound internet access, so it was set aside in favour of another approach.

Step 5: the payload that worked

After roughly 15 to 30 minutes of experimentation, this payload was found:

From the original on petrusviet.medium.com

It worked.

From the original on petrusviet.medium.com

The full call chain

The report gives the stack trace from the entry point to execution. A stack trace is the list of functions a program was inside at a given moment, from the outermost to the innermost, like a list of nested phone calls.

Text
NioCommand.call()    -> NuNioProcessHelper.run()        -> NuNioProcessHelper.applyConfigurers()            -> RemoteUserNioProcessConfigurer.configure()   (#  put username to environment)            -> NuNioProcessHelper.createProcessBuilder()        -> NuProcessBuilder.run()            -> NuProcessBuilder.prepareEnvironment()  (Map to String[] - "Key=Value")            -> WinProcessFactory.runProcess()                -> WindowsProcess.run()                    -> WindowsProcess.prepareProcess()                        -> WindowsProcess.getEnvironment()                        -> WindowsProcess.getEnvironmentBlock()  (String[] to Char[] - "key1=var1\0key2=var2")                    -> NuKernel32.ResumeThread(this.processInfo.hThread) (RCE)

Read it left to right and top to bottom: each arrow means "which then calls". The comments in brackets mark the two important conversions, the map becoming a list of strings and the list becoming the null-separated block, and the final call, NuKernel32.ResumeThread, which is where the new process actually starts running. That last step is the RCE.

Root cause

The username is placed into the environment of the Git process as the value of REMOTE_USER. On Windows, the environment block is a sequence of null-terminated KEY=VALUE strings ending with an extra null. The vulnerable updateEnvironment only rejected null input, so a username containing a null byte passed through. When the environment was serialised, the null byte inside the username terminated the REMOTE_USER entry early, and the remainder of the username was parsed as a new environment variable.

Because the injected variable can be one that Git interprets as a command, such as GIT_EXTERNAL_DIFF, the next Git operation that honours it runs the attacker's program. The existing check did not stop this because it validated only for null references, not for null bytes or blank values inside the key or value.

Impact

An attacker who can control a username on the Bitbucket instance can inject arbitrary environment variables into Git processes and, through a variable such as GIT_EXTERNAL_DIFF, execute commands on the server. The researcher demonstrated this on Windows with calc.exe. The advisory's affected-version table ties the 8.x exposure to the mesh feature being disabled, and Atlassian stated that servers using PostgreSQL are not affected.

Fix

In the fixed version, the pair is passed to NioProcessParameters.environmentPut(), which checks for blank values and for null bytes in the key and value. That closes the split-entry primitive: a username with an embedded null can no longer produce a second environment variable.

Variants to test

  1. Other products that pass user-controlled strings into a child process environment. A positive result is a username containing a null byte that appears as two separate entries in the child's environment.
  2. Other Git environment variables that execute programs, such as diff or pager helpers. A positive result is a command running when the corresponding Git operation is triggered from the web interface.
  3. Other platforms' environment handling. The report's chain is Windows-specific; on Linux the same class of bug would need a different separator or parsing quirk, and a positive result is an injected variable visible in the child process environment.
  4. Usernames containing newlines or = characters, to see whether any other layer splits entries incorrectly. A positive result is an unexpected variable name in the environment block.

Glossary

  • Bitbucket Server and Data Center: Atlassian's self-hosted product for storing and managing Git repositories on your own servers.
  • Git: the version control tool developers use to track changes to files over time.
  • Repository (repo): a folder of code whose history Git keeps.
  • CVE: a public catalogue number for a known security vulnerability.
  • Command injection: tricking a program into running an operating system command supplied by an attacker.
  • Environment variables: named settings a parent program passes to a child program when it starts it, in NAME=value form.
  • RCE (remote code execution): running your own code on someone else's machine over a network.
  • Mesh: an optional storage system in Bitbucket 8.x that can replace NFS.
  • NFS: a way for several machines to share files over a network.
  • JVM (Java Virtual Machine): the runtime that executes Java programs.
  • JDWP: the protocol Java debuggers use to talk to a running JVM.
  • Debugger: a tool that pauses a running program so you can inspect its variables and step through its code.
  • Breakpoint: a marker that makes a debugger pause the program at a chosen line.
  • Call chain / stack trace: the list of functions a program is inside at a given moment, from outermost to innermost.
  • Payload: the attacker-controlled input that triggers the flaw.
  • Process Monitor: a Windows tool that records every program that starts and the files and settings it touches.
  • Diff: a side-by-side comparison of two versions of a file showing which lines changed.
  • Map: a collection of key-and-value pairs, like a dictionary.
  • Null: the absence of a value.
  • Null byte: a byte with the value zero, often used to mark the end of text.
  • String[]: a list of text strings.
  • Serialise: turn a structured value, such as a map, into one flat piece of text.
  • GIT_EXTERNAL_DIFF: a Git environment variable naming a program to run when git diff is invoked.
  • git diff: the Git command that shows what changed between two versions of a file.
  • calc.exe: the Windows calculator, used as a harmless proof that a program can be started.
  • cmd.exe: the Windows command interpreter, which runs other commands.
  • REMOTE_USER: the environment variable into which Bitbucket writes the username for the Git process.
  • PostgreSQL / H2: database systems; PostgreSQL is a separate server, H2 runs inside the application.
  • Batch script (.bat): a plain list of Windows commands run one after another.

More by Petrus Viet

View all

3 more disclosed reports, best first.

More from Atlassian

View all

The newest disclosed reports for this program.

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.