Security vulnerabilities can leave systems exposed to attacks, potentially compromising sensitive data and leading to unexpected behavior. One such vulnerability, CVE-2022-3328, has recently been identified in the widely used snap package manager. In this post, we will examine the race condition within snap-confine's must_mkdir_and_open_with_perms() function, including code snippets, links to original references, and details about the exploit.

Background on snap-confine

Snap-confine is a security feature implemented in the snap package management system. It is responsible for setting up a secure environment to execute snap applications and restricting their access to only the necessary resources. This is achieved through the use of namespaces which create isolated workspaces for snap processes.

The Vulnerability: CVE-2022-3328

CVE-2022-3328 is a race condition vulnerability in the must_mkdir_and_open_with_perms() function within snap-confine. A race condition occurs when multiple threads compete for resources, and the outcome depends on which thread wins. In this case, the issue gives an attacker the ability to exploit file-system related time-of-check, time-of-use (TOCTOU) race conditions.

Code Snippet

The root cause of CVE-2022-3328 is evident in the must_mkdir_and_open_with_perms() function within the snap-confine source code. Here's a simplified version of the affected code:

int must_mkdir_and_open_with_perms(const char *path, mode_t mode) {
    int fd = -1;
    if (mkdir(path, mode) <  && errno != EEXIST) {
        die("cannot create directory %s", path);

    fd = open(path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
    if (fd < ) {
        die("cannot open directory %s", path);

    return fd;

The function attempts to create a directory and then open it. However, between the creation of the directory (using mkdir) and the opening of the directory (using open), there is a brief window of time when a malicious process can manipulate the filesystem and bypass security measures.

Exploit Details

An attacker could exploit the race condition by quickly inserting a symbolic link between the calls to mkdir() and open(). This could redirect the open() call to an unintended location, potentially granting the attacker access to restricted files. Moreover, since snap-confine runs with root privileges, this vulnerability could be used to escalate privileges and compromise the system.

Original References

The vulnerability, CVE-2022-3328, was first reported on the Snapcraft forum. The post provides a detailed analysis of the issue, along with proof-of-concept code demonstrating the exploit:

- Snapcraft Forum: CVE-2022-3328 Vulnerability Report

The official CVE entry for this vulnerability can be found in the

- NIST National Vulnerability Database: CVE-2022-3328


CVE-2022-3328 is a critical race condition vulnerability affecting snap-confine. It highlights the importance of vigilance in software development and the necessity of timely security updates for widely used tools. As with any vulnerability, users and administrators should keep their systems updated and apply patches as soon as they become available to minimize the risk of exploitation.


Published on: 01/08/2024 18:15:45 UTC