CVE-2021-45707: Nix Out-of-bounds Write Vulnerability

MODERATE
Advisory Identifier: GHSA-76w9-p8mg-j927
CVSS 6.7
CWE-787
Published
Advisory Summary

CVE-2021-45707 is a MODERATE (CVSS 6.7) out-of-bounds write vulnerability (CWE-787: Out-of-bounds Write) affecting Nix libraries in the crates.io ecosystem. The issue involves out-of-bounds write in nix. Remediation is available by upgrading affected packages to version 0.20.2, 0.21.2, 0.22.2.

CVE-2021-45707 at a Glance

CVE Identifier CVE-2021-45707
GHSA Identifier GHSA-76w9-p8mg-j927
Severity MODERATE
CVSS v3.1 Base Score 6.7
CWE Classification CWE-787: Out-of-bounds Write
Affected Product Nix
Package Ecosystem crates.io
Published Date
Last Updated

Security Impact

CVSS: 6.7

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:L/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H).

Attack Vector Local (AV:L)
Attack Complexity Low (AC:L)
Privileges Required High (PR:H)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity High (I:H)
Availability High (A:H)

How to Fix CVE-2021-45707

To remediate this vulnerability, upgrade all affected Nix dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

nix fixed: 0.20.2
Upgrade nix to 0.20.2
nix fixed: 0.21.2
Upgrade nix to 0.21.2
nix fixed: 0.22.2
Upgrade nix to 0.22.2
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for nix

How to Check if You Are Affected

To determine whether your application is affected by CVE-2021-45707, inspect your project dependencies for Nix:

inspect dependencies for nix

Analyze the terminal output to locate any version of Nix in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.20.2), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-787: Out-of-bounds Write, where the product writes data past the end, or before the beginning, of the intended buffer. In Nix, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

On certain platforms, if a user has more than 16 groups, the nix::unistd::getgrouplist function will call the libc getgrouplist function with a length parameter greater than the size of the buffer it provides, resulting in an out-of-bounds write and memory corruption. The libc getgrouplist function takes an in/out parameter ngroups specifying the size of the group buffer. When the buffer is too small to hold all of the requested user's group memberships, some libc implementations, including glibc and Solaris libc, will modify ngroups to indicate the actual number of groups for the user, in addition to returning an error. The version of nix::unistd::getgrouplist in nix 0.16.0 and up will resize the buffer to twice its size, but will not read or modify the ngroups variable. Thus, if the user has more than twice as many groups as the initial buffer size of 8, the next call to getgrouplist will then write past the end of the buffer. The issue would require editing /etc/groups to exploit, which is usually only editable by the root user.

CVE-2021-45707 Affected Nix Versions

3 component(s)
Fixed Versions:
0.20.20.21.20.22.2
Product / Package Ecosystem Affected Versions Fixed Version
nix
crates.io
≥ 0.16.0
< 0.20.2
0.20.2
nix
crates.io
≥ 0.21.0
< 0.21.2
0.21.2
nix
crates.io
≥ 0.22.0
< 0.22.2
0.22.2

References

Frequently Asked Questions

What is CVE-2021-45707?
CVE-2021-45707 is a security vulnerability in Nix involving Out-of-bounds Write in nix under CWE-787: Out-of-bounds Write.
Which Nix versions are affected?
Affected versions include releases prior to 0.20.2, 0.21.2, 0.22.2 across affected packages: nix.
What version fixes CVE-2021-45707?
Upgrading to 0.20.2, 0.21.2, 0.22.2 (or the latest patched release for your respective branch) resolves the vulnerability.
Is nix affected?
Yes, nix is affected in releases prior to 0.20.2, 0.21.2, 0.22.2. Upgrade your dependency to 0.20.2, 0.21.2, 0.22.2 or later.
What is the CVSS score of CVE-2021-45707?
CVE-2021-45707 has a CVSS v3.1 base score of 6.7 (MODERATE) with vector string CVSS:3.1/AV:L/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H.
What CWE is associated with CVE-2021-45707?
CVE-2021-45707 is classified under CWE-787: Out-of-bounds Write (The product writes data past the end, or before the beginning, of the intended buffer.).
How can I check crates.io dependencies for CVE-2021-45707?
Run the dependency tree command: 'inspect dependencies for nix'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-76w9-p8mg-j927",
  "modified" : "2024-02-21T19:15:12Z",
  "published" : "2022-01-06T22:07:14Z",
  "aliases" : [ "CVE-2021-45707" ],
  "summary" : "Out-of-bounds Write in nix",
  "details" : "On certain platforms, if a user has more than 16 groups, the nix::unistd::getgrouplist function will call the libc getgrouplist function with a length parameter greater than the size of the buffer it provides, resulting in an out-of-bounds write and memory corruption.\n\nThe libc getgrouplist function takes an in/out parameter ngroups specifying the size of the group buffer. When the buffer is too small to hold all of the requested user's group memberships, some libc implementations, including glibc and Solaris libc, will modify ngroups to indicate the actual number of groups for the user, in addition to returning an error. The version of nix::unistd::getgrouplist in nix 0.16.0 and up will resize the buffer to twice its size, but will not read or modify the ngroups variable. Thus, if the user has more than twice as many groups as the initial buffer size of 8, the next call to getgrouplist will then write past the end of the buffer.\n\nThe issue would require editing /etc/groups to exploit, which is usually only editable by the root user.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:L/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "crates.io",
      "name" : "nix"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0.16.0"
      }, {
        "fixed" : "0.20.2"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "crates.io",
      "name" : "nix"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0.21.0"
      }, {
        "fixed" : "0.21.2"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "crates.io",
      "name" : "nix"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0.22.0"
      }, {
        "fixed" : "0.22.2"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2021-45707"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/nix-rust/nix/issues/1541"
  }, {
    "type" : "ADVISORY",
    "url" : "https://github.com/advisories/GHSA-wgrg-5h56-jg27"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/nix-rust/nix"
  }, {
    "type" : "WEB",
    "url" : "https://raw.githubusercontent.com/rustsec/advisory-db/main/crates/nix/RUSTSEC-2021-0119.md"
  }, {
    "type" : "WEB",
    "url" : "https://rustsec.org/advisories/RUSTSEC-2021-0119.html"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-787" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2022-01-05T23:13:23Z",
    "nvd_published_at" : "2021-12-27T00:15:00Z"
  }
}