VYPR
Unrated severityNVD Advisory· Published Sep 15, 2021· Updated Aug 3, 2024

Use After Free in vim/vim

CVE-2021-3796

Description

A use-after-free vulnerability in Vim's nv_replace() function can be triggered via crafted editor operations, potentially leading to memory corruption or crash.

AI Insight

LLM-synthesized narrative grounded in this CVE's description and references.

A use-after-free vulnerability in Vim's nv_replace() function can be triggered via crafted editor operations, potentially leading to memory corruption or crash.

Vulnerability

A use-after-free vulnerability exists in Vim versions prior to patch 8.2.3428. The bug resides in the nv_replace() function in src/normal.c [2]. Under certain conditions, the line pointer ptr obtained via ml_get_buf() is used after the line may have been freed or reallocated by u_save(), showmatch(), or ins_copychar() [2]. The commit that fixes this issue [2] reorders the code to fetch the pointer again after these operations, preventing use of freed memory.

Exploitation

An attacker could exploit this vulnerability by crafting a specially designed text file or editor command sequence that triggers the vulnerable code path. Specifically, the issue occurs when the replace command ("R" or "r") is used in combination with Ctrl-E or Ctrl-Y to copy characters from adjacent lines [2]. Local access to Vim and the ability to open a crafted file or execute a sequence of commands is required. The attacker does not need elevated privileges beyond normal Vim usage.

Impact

Successful exploitation results in a use-after-free condition, potentially leading to memory corruption, a crash, or possibly arbitrary code execution in the context of the Vim process. The impact is limited by the need for user interaction and the complexity of the race condition. The advisory from Openwall notes that the security boundary is not crossed in normal Vim usage but could be a concern if Vim runs with raised privileges [1].

Mitigation

The vulnerability is fixed in Vim patch 8.2.3428 [2]. Users should update to Vim version 8.2.3428 or later. Workarounds include avoiding the use of the replace command with Ctrl-E/Ctrl-Y on untrusted files. Fedora package announcements may have been issued [3][4], but their content is not accessible in the available references.

AI Insight generated on May 27, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.

Affected products

43

Patches

0

No patches discovered yet.

Vulnerability mechanics

Root cause

"Use-after-free in nv_replace() where a line pointer obtained before ins_copychar() is used after ins_copychar() may have released the line."

Attack vector

An attacker can trigger a use-after-free by crafting a file that, when opened and edited with specific replace-mode operations (e.g., `r` followed by `Ctrl_E` or `Ctrl_Y`), causes Vim to use a freed line pointer. The freed memory occurs because `ins_copychar()` can release the line, but the stale pointer from a prior `ml_get_buf()` call is still used to write into the line. [ref_id=1]

Affected code

The vulnerability is in the `nv_replace()` function in Vim's source code. The patch moves the `ml_get_buf()` call to after `ins_copychar()` in the `Ctrl_E`/`Ctrl_Y` branch, and adds a missing `ml_get_buf()` call in the else branch, ensuring the line pointer is re-fetched after operations that may release the line.

What the fix does

The patch reorders the `ml_get_buf()` call in the `Ctrl_E`/`Ctrl_Y` branch to occur after `ins_copychar()`, and adds a new `ml_get_buf()` call in the else branch. This ensures the line pointer is always re-fetched after any operation that might release the line, preventing use of freed memory. The commit message explicitly states the bug: 'using freed memory when replacing'. [ref_id=1]

Preconditions

  • inputThe attacker must supply a crafted file that, when opened in Vim, triggers the replace-mode operations (r followed by Ctrl_E or Ctrl_Y) on a line where ins_copychar() causes the line to be released.
  • inputThe user must execute the specific replace-mode keystrokes (e.g., r followed by Ctrl_E) on the crafted file.

Generated on May 29, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.

References

9

News mentions

0

No linked articles in our index yet.