CVE-2021-21261 Overview
CVE-2021-21261 is a sandbox escape vulnerability in Flatpak, a popular system for building, distributing, and running sandboxed desktop applications on Linux. A critical bug was discovered in the flatpak-portal service that allows sandboxed applications to execute arbitrary code on the host system, effectively breaking out of the security sandbox designed to isolate applications.
Critical Impact
Malicious or compromised Flatpak applications can escape the sandbox and execute arbitrary code on the host system with the privileges of the user running Flatpak.
Affected Products
- Flatpak versions from 0.11.4 to before 1.8.5
- Flatpak versions from 1.9.x to before 1.10.0
- Debian Linux 10.0
Discovery Timeline
- January 14, 2021 - CVE-2021-21261 published to NVD
- November 21, 2024 - Last updated in NVD database
Technical Details for CVE-2021-21261
Vulnerability Analysis
This vulnerability is classified as CWE-74 (Improper Neutralization of Special Elements in Output Used by a Downstream Component), commonly known as an injection vulnerability. The flaw exists in the Flatpak portal D-Bus service (flatpak-portal, also known by its D-Bus service name org.freedesktop.portal.Flatpak), which is responsible for allowing sandboxed apps to launch subprocesses in new sandbox instances.
The portal service allows apps to spawn subprocesses with either the same security settings as the caller or with more restrictive settings. This functionality is crucial for applications like Flatpak-packaged web browsers (e.g., Chromium) that need to launch subprocesses to handle untrusted web content with additional restrictions.
Root Cause
The root cause of this vulnerability lies in the improper handling of environment variables. In vulnerable versions, the Flatpak portal service passes caller-specified environment variables directly to non-sandboxed processes on the host system, specifically to the flatpak run command used to launch new sandbox instances. This allows a malicious Flatpak application to set environment variables that are trusted by the flatpak run command, enabling arbitrary code execution outside the sandbox.
Attack Vector
The attack exploits the local attack vector where a malicious or compromised Flatpak application can:
- Craft malicious environment variables that are interpreted by the flatpak run command
- Pass these environment variables through the flatpak-portal D-Bus service
- Have those variables influence the execution of flatpak run on the host system
- Execute arbitrary code outside the sandbox with the user's privileges
The security patches introduce a new --env-fd option to safely pass environment variables through a file descriptor instead of through the command line or inherited environment, preventing the injection attack:
// Security patch - Adding flatpak_bwrap_take_arg function
// Source: https://github.com/flatpak/flatpak/commit/6d1773d2a54dde9b099043f07a2094a4f1c2f486
/*
* flatpak_bwrap_take_arg:
* @arg: (transfer full): Take ownership of this argument
*
* Add @arg to @bwrap's argv, taking ownership of the pointer.
*/
void
flatpak_bwrap_take_arg (FlatpakBwrap *bwrap, char *arg)
{
g_ptr_array_add (bwrap->argv, arg);
}
// Security patch - Adding --env-fd option for secure environment variable handling
// Source: https://github.com/flatpak/flatpak/commit/6e5ae7a109cdfa9735ea7ccbd8cb79f9e8d3ae8b
static gboolean
option_env_fd_cb (const gchar *option_name,
const gchar *value,
gpointer data,
GError **error)
{
FlatpakContext *context = data;
g_autoptr(GBytes) env_block = NULL;
gsize remaining;
const char *p;
guint64 fd;
gchar *endptr;
fd = g_ascii_strtoull (value, &endptr, 10);
if (endptr == NULL || *endptr != '\0' || fd > G_MAXINT)
return glnx_throw (error, "Not a valid file descriptor: %s", value);
env_block = glnx_fd_readall_bytes ((int) fd, NULL, error);
if (env_block == NULL)
return FALSE;
p = g_bytes_get_data (env_block, &remaining);
/* env_block might not be \0-terminated */
while (remaining > 0)
Detection Methods for CVE-2021-21261
Indicators of Compromise
- Unusual D-Bus activity involving the org.freedesktop.portal.Flatpak service
- Unexpected processes spawned outside of Flatpak sandboxes with suspicious environment variables
- Anomalous flatpak run command executions with non-standard environment variable configurations
- Signs of code execution in contexts where Flatpak applications should be restricted
Detection Strategies
- Monitor D-Bus traffic for calls to the flatpak-portal service, especially those passing unusual environment variables
- Implement system call auditing to detect sandbox escape attempts via flatpak run
- Use application whitelisting to detect unexpected processes spawned by Flatpak applications
- Deploy endpoint detection solutions capable of monitoring inter-process communication (IPC) patterns
Monitoring Recommendations
- Enable detailed logging for the flatpak-portal D-Bus service
- Monitor process creation events for child processes spawned by Flatpak sandboxed applications
- Implement alerts for environment variable manipulation patterns associated with known exploitation techniques
- Review Flatpak application permissions and installed packages regularly
How to Mitigate CVE-2021-21261
Immediate Actions Required
- Upgrade Flatpak to version 1.8.5 or 1.10.0 or later immediately
- Review installed Flatpak applications for any suspicious or untrusted packages
- Audit system logs for any signs of attempted exploitation
- Consider temporarily disabling untrusted Flatpak applications until patching is complete
Patch Information
Security patches have been released by the Flatpak project and are available through multiple commits:
- GitHub Commit 6d1773d2 - Converts environment variables into bwrap arguments
- GitHub Commit 6e5ae7a1 - Adds --env-fd option for secure environment handling
- GitHub Commit aeb6a7ab - Portal service conversion of --env to --env-fd
Distribution-specific advisories:
For complete details, see the GitHub Security Advisory GHSA-4ppf-fxf6-vxg2.
Workarounds
- As a temporary mitigation, prevent the flatpak-portal service from starting (note: this will prevent many Flatpak apps from working correctly)
- Restrict usage of Flatpak applications to only trusted, verified packages
- Implement additional application sandboxing using SELinux or AppArmor policies
- Limit user access to systems where Flatpak is installed until patches can be applied
# Temporary workaround: Disable flatpak-portal service
# WARNING: This will break functionality for many Flatpak applications
systemctl --user mask flatpak-portal.service
systemctl --user stop flatpak-portal.service
# Verify Flatpak version after upgrade
flatpak --version
# List installed Flatpak applications for review
flatpak list --app
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

