SSD Advisory – Roundcube MarkAsJunk RCE
The markasjunk plugin in Roundcube fails to sanitise the user's identity email address when constructing a shell command, allowing an attacker to inject arbitrary commands. An authenticated user can change their email identity and then mark an email as junk to execute code on the server.
Published on ssd-disclosure.com
A vulnerability in Roundcube’s markasjunk plugin allows attackers that send a specially crafted identity email address to cause the plugin to execute arbitrary code.
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
Roundcube is a webmail client, and the markasjunk plugin ships as part of its core codebase: there is no separate installation step, and the only requirement is that the Roundcube administrator configures it. The plugin lets a user mark a message as junk from the web interface, and depending on how it is configured it can hand that message to an external learning tool so the spam filter improves over time.
The vulnerability covered here is a command injection in the plugin's cmd_learn driver. When the plugin is configured in a particular way, a specially crafted identity email address causes the plugin to execute arbitrary code. The affected versions are Roundcube 1.6.1 and prior with the markasjunk plugin enabled. The issue was found by Selim Enes Karaduman, an independent security researcher working with SSD Secure Disclosure, and published on 6 June 2023.
Setup
Two configuration choices in plugins/markasjunk/config.inc.php are required for the vulnerable path to be reachable.
- The learning driver must be
cmd_learn:
$config['markasjunk_learning_driver'] = "cmd_learn";
- The spam command must be set and must contain the
%iplaceholder:
$config['markasjunk_spam_cmd'] = "salearn %i";
On the attacker side the requirements are trivial: any Roundcube user who can change their own email identity and can mark an email as junk. Both actions are available from the normal web interface, the identity under settings, identities.
Finding it
The researcher reviewed the cmd_learn driver and located the command injection in _do_salearn() in plugins/markasjun/drivers/cmd_learn.php. That function runs whenever an email is moved to a junk folder from the web application interface.
The suspicious part is how the %i placeholder is substituted. When $config['markasjunk_spam_cmd'] contains %i, line 59 is executed:
$command = str_replace('%i', $identity['email'], $command);
The value $identity['email'] is the identity email address of the user, which the user can change through settings, identities in the web panel. It is inserted into the command string without any escaping. The resulting command is then executed on line 102:
$output = shell_exec($tmp_command);
Because the email address flows unescaped into a string passed to shell_exec(), any shell metacharacters the user can place in the identity address become part of the command. The report attributes this to a lack of filtering on the %i value.
Exploitation
Step 1: Change the identity email address
Starting from a normal identity such as [redacted], the user changes the identity email to a value that closes out of the intended argument and appends a shell command:
admin&touch${IFS}test.txt&@roundcube.com
The & characters separate shell commands, and ${IFS} supplies the space that would otherwise be needed between touch and the filename, so the whole string stays a single token as far as the email field is concerned.
Step 2: Mark a message as junk
With the identity changed, the user marks an email as junk through the web interface. This triggers _do_salearn(), which substitutes the identity email into the configured command. The line the driver calls when processing the email is:
salearn admin&touch${IFS}test.txt&@roundcube.com
Step 3: Observe the result
The report states that this creates test.txt, confirming arbitrary command execution.
Root cause
The %i placeholder is replaced with the raw identity email address and the result is passed to shell_exec(). No escaping is applied to the substituted value, so shell metacharacters in the email address are interpreted by the shell rather than treated as data.
The report notes that other formats such as %s and %f are filtered with escapeshellarg(). The researcher's view is that %i was likely forgotten because nobody expected an email address to be usable for command injection. The vulnerable line is plugins/markasjun/drivers/cmd_learn.php:59.
Impact
Any Roundcube user who can change their identity email and mark a message as junk can trigger the command injection when the plugin is configured as described. The attack requires the markasjunk plugin to be enabled and configured with the cmd_learn learning driver and a markasjunk_spam_cmd that contains %i. Under those conditions the report describes the issue as an RCE reachable by any user of the instance.
Fix
The suggested fix is to escape the identity email before it is substituted into the command, matching the treatment already given to the other placeholders. The patch changes line 59 as follows:
- $command = str_replace('%i', $identity['email'], $command);
+ $command = str_replace('%i', escapeshellarg($identity['email']), $command);
Variants to test
- Look for other placeholders in command-building code that are substituted without
escapeshellarg()or an equivalent, especially ones filled from user-controlled profile fields such as display names, signatures or identity addresses. A positive result is a value containing&,;,|or${IFS}that produces an observable side effect when the command runs. - Check other Roundcube plugins and drivers that call
shell_exec()or similar functions with configuration templates. A positive result is a configuration option whose placeholder is replaced by user data and reaches the shell unescaped. - Test whether the identity email field itself validates shell metacharacters on save. A positive result is an address containing
&or${IFS}being accepted and stored unchanged. - Review any
%i-style placeholder handling for consistency with%sand%f. A positive result is one placeholder escaping its value while another does not.
What happened, in plain words
Roundcube is a webmail application: a program that runs on a web server and lets people read and send email through their browser, a bit like a self-hosted version of Gmail. It ships with a feature called the markasjunk plugin. A plugin is an optional add-on that gives a program extra abilities, and this one adds a button that lets a user mark a message as junk (spam).
When a user marks a message as junk, the plugin can pass that message to an external program, a spam-learning tool, so the spam filter gets better over time. To do that, the plugin builds a text command and asks the operating system to run it. The bug in this report is that one piece of that command, the user's own email address, is inserted without any cleaning. A user can therefore put special characters into their email address that the operating system's command interpreter (the shell) treats as instructions rather than as plain text. The result is that the user can make the server run commands of their choosing. This class of bug is called command injection, and when the injected commands let an attacker run their own code it is called remote code execution, or RCE.
The affected versions are Roundcube 1.6.1 and prior with the markasjunk plugin enabled. The issue was found by Selim Enes Karaduman, an independent security researcher working with SSD Secure Disclosure, and published on 6 June 2023.
Context
Roundcube's markasjunk plugin comes as part of Roundcube's core codebase. There is no separate installation step: the only requirement is that the Roundcube administrator configures it. Configuration here means editing a settings file that tells the plugin how to behave.
The plugin lets a user mark a message as junk from the web interface. Depending on how it is configured, it can hand that message to an external learning tool so the spam filter improves over time. The vulnerability covered here is a command injection in the plugin's cmd_learn driver. A driver is the small piece of code that knows how to talk to one particular external tool; cmd_learn is the driver that runs a command-line program.
When the plugin is configured in a particular way, a specially crafted identity email address causes the plugin to execute arbitrary code. An identity in Roundcube is a sender profile attached to your account: the name and email address that appear on mail you send. A user can normally edit their own identities, which is what makes this attack available to ordinary users rather than only administrators.
Setup
Two configuration choices in plugins/markasjunk/config.inc.php are required for the vulnerable path to be reachable. That file is the plugin's settings file; each line below assigns a value to a named setting.
- The learning driver must be
cmd_learn:
$config['markasjunk_learning_driver'] = "cmd_learn";
This line tells the plugin which driver to use when it learns about spam. $config is the settings array, markasjunk_learning_driver is the setting name, and "cmd_learn" is the chosen value. Choosing cmd_learn means the plugin will run an external command-line program instead of, say, talking to a spam service over the network.
- The spam command must be set and must contain the
%iplaceholder:
$config['markasjunk_spam_cmd'] = "salearn %i";
This line defines the command template the plugin will run. salearn is the name of the external learning program, and %i is a placeholder: a marker that the plugin later replaces with a real value. In this case the placeholder stands for the user's identity email address. The whole point of a placeholder is that the program text stays fixed while the value changes per user, and that substitution step is where the bug lives.
On the attacker side the requirements are trivial: any Roundcube user who can change their own email identity and can mark an email as junk. Both actions are available from the normal web interface, the identity under settings, identities.
Finding it
The researcher reviewed the cmd_learn driver and located the command injection in _do_salearn() in plugins/markasjun/drivers/cmd_learn.php. That function runs whenever an email is moved to a junk folder from the web application interface. Reading source code like this, looking for places where user input reaches a dangerous operation, is called security code review.
The suspicious part is how the %i placeholder is substituted. When $config['markasjunk_spam_cmd'] contains %i, line 59 is executed:
$command = str_replace('%i', $identity['email'], $command);
Line by line: str_replace is a function that searches a piece of text for one string and swaps in another. Here it searches $command (the command template) for the literal text %i and replaces every occurrence with $identity['email'], the identity email address of the user. The result is stored back into $command. The important detail is that the email address is dropped in exactly as typed, with no cleaning or quoting. The value $identity['email'] is something the user can change through settings, identities in the web panel, so a user controls part of a command that the server will run.
The resulting command is then executed on line 102:
$output = shell_exec($tmp_command);
shell_exec is a function that hands a string to the operating system's shell and runs it as a command, returning whatever the command prints. $tmp_command is the command string after the substitution above. Because the email address flows unescaped into a string passed to shell_exec(), any shell metacharacters the user can place in the identity address become part of the command. Metacharacters are characters with special meaning to the shell, such as &, ; and |, which separate or chain commands. The report attributes this to a lack of filtering on the %i value.
Exploitation
Step 1: Change the identity email address
Starting from a normal identity such as [redacted], the user changes the identity email to a value that closes out of the intended argument and appends a shell command:
admin&touch${IFS}test.txt&@roundcube.com
Breaking this down: admin is ordinary text. The first & tells the shell that the command before it has ended and another command follows. touch is a standard command that creates an empty file. ${IFS} is a shell variable that holds the characters the shell uses to separate words, normally a space; writing it this way supplies the space that would otherwise be needed between touch and the filename, so the whole string stays a single token as far as the email field is concerned. test.txt is the file to create. The second & ends that command, and @roundcube.com is leftover text that keeps the value looking like an email address. ${IFS} supplies the space that would otherwise be needed between touch and the filename, so the whole string stays a single token as far as the email field is concerned.
Step 2: Mark a message as junk
With the identity changed, the user marks an email as junk through the web interface. This triggers _do_salearn(), which substitutes the identity email into the configured command. The line the driver calls when processing the email is:
salearn admin&touch${IFS}test.txt&@roundcube.com
This is the command template salearn %i after %i has been replaced by the crafted address. The middle one is the attacker's command.
Step 3: Observe the result
The report states that this creates test.txt, confirming arbitrary command execution. Seeing the file appear is the proof: the attacker did not ask the plugin to create a file, they asked it to learn about spam, and yet a file of their choosing now exists on the server.
Root cause
The %i placeholder is replaced with the raw identity email address and the result is passed to shell_exec(). No escaping is applied to the substituted value, so shell metacharacters in the email address are interpreted by the shell rather than treated as data. Escaping means marking a value so that the shell treats every character in it as plain text, never as an instruction.
The report notes that other formats such as %s and %f are filtered with escapeshellarg(). That function wraps a value in quotes and neutralises special characters so it can safely be used as a single shell argument. The researcher's view is that %i was likely forgotten because nobody expected an email address to be usable for command injection. The vulnerable line is plugins/markasjun/drivers/cmd_learn.php:59.
Impact
Any Roundcube user who can change their identity email and mark a message as junk can trigger the command injection when the plugin is configured as described. Running in that context means the commands run with the same permissions as the webmail software itself, so whatever files and services that process can reach, the attacker can reach too. The attack requires the markasjunk plugin to be enabled and configured with the cmd_learn learning driver and a markasjunk_spam_cmd that contains %i. Under those conditions the report describes the issue as an RCE reachable by any user of the instance.
Fix
The suggested fix is to escape the identity email before it is substituted into the command, matching the treatment already given to the other placeholders. The patch changes line 59 as follows:
- $command = str_replace('%i', $identity['email'], $command);
+ $command = str_replace('%i', escapeshellarg($identity['email']), $command);
The line starting with - is the old code and the line starting with + is the new code. The only change is that $identity['email'] is now wrapped in escapeshellarg() before being substituted. With that in place, the crafted address arrives at the shell as one harmless quoted string instead of being split into separate commands.
Variants to test
- Look for other placeholders in command-building code that are substituted without
escapeshellarg()or an equivalent, especially ones filled from user-controlled profile fields such as display names, signatures or identity addresses. A positive result is a value containing&,;,|or${IFS}that produces an observable side effect when the command runs. - Check other Roundcube plugins and drivers that call
shell_exec()or similar functions with configuration templates. A positive result is a configuration option whose placeholder is replaced by user data and reaches the shell unescaped. - Test whether the identity email field itself validates shell metacharacters on save. A positive result is an address containing
&or${IFS}being accepted and stored unchanged. - Review any
%i-style placeholder handling for consistency with%sand%f. A positive result is one placeholder escaping its value while another does not.
Glossary
- Roundcube: a webmail application that runs on a web server and is used through a browser.
- Plugin: an optional add-on that gives a program extra features.
- markasjunk plugin: a Roundcube add-on that lets a user mark a message as junk and optionally pass it to a spam-learning tool.
- Spam filter: software that decides whether a message is unwanted junk mail.
- Identity: a sender profile in a mail account, holding the name and email address shown on outgoing mail.
- Driver: the part of a plugin that knows how to talk to one particular external tool.
- RCE (remote code execution): a bug that lets an attacker run their own code on a remote server.
- Shell: the program that reads text commands and runs them on the operating system.
- Metacharacters: characters such as
&,;and|that the shell treats as separators or instructions rather than plain text. - Placeholder: a marker such as
%iin a template that is later replaced with a real value. str_replace: a function that swaps one piece of text for another inside a string.shell_exec: a function that hands a string to the shell and runs it as a command.touch: a standard command that creates an empty file.${IFS}: a shell variable holding the word-separator characters, normally a space, used here to supply a space without typing one.- Escaping: marking a value so the shell treats every character in it as plain text.
escapeshellarg(): a function that quotes and neutralises a value so it can safely be used as a single shell argument.- Security code review: reading source code to find places where user input reaches a dangerous operation.
- Authenticated user: someone logged in to the application with a valid account.