VYPR
Unrated severityNVD Advisory· Published Nov 19, 2021· Updated Aug 3, 2024

Heap-based Buffer Overflow in vim/vim

CVE-2021-3968

Description

CVE-2021-3968 is a heap-based buffer overflow in vim caused by triggering ModeChanged before VIsual is set, leading to a crash.

AI Insight

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

CVE-2021-3968 is a heap-based buffer overflow in vim caused by triggering ModeChanged before VIsual is set, leading to a crash.

Vulnerability

CVE-2021-3968 is a heap-based buffer overflow vulnerability in vim, specifically in the visual mode handling code. The flaw occurs because the trigger_modechanged() function is called before VIsual is set in n_start_visual_mode(), causing a crash when a ModeChanged autocommand is triggered during visual mode initialization. This bug affects vim versions up to patch 8.2.3610, where the order of operations is corrected by moving the trigger_modechanged() call after VIsual is properly assigned [2].

Exploitation

An attacker can exploit this vulnerability by crafting a file or input that, when opened in vim, triggers the ModeChanged event during visual mode startup. This requires the victim to open the malicious file in vim, and a ModeChanged autocommand must be defined (e.g., au! ModeChanged * norm 0u). The attacker does not need prior authentication or special network access; luring the user into opening the file with a vulnerable vim version is sufficient. The exploit sequence involves the file causing visual mode to start, leading to the premature trigger_modechanged() call before VIsual is set, resulting in a heap overflow [2].

Impact

Successful exploitation leads to a heap-based buffer overflow, which can crash vim, resulting in a denial-of-service condition. As noted in the reference, under normal circumstances this does not cross a security boundary, but if vim is configured to run with elevated privileges (e.g., editing system configuration files), the crash could have broader stability implications [1]. No code execution or data corruption beyond the crash is indicated.

Mitigation

The fix was committed as patch 8.2.3610, which moves the trigger_modechanged() call to after VIsual is set, preventing the crash [2]. Users should update to vim version 8.2.3610 or later. If patching is not immediately possible, users can avoid defining ModeChanged autocommands that interact with visual mode, or refrain from opening untrusted files in vim.

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

37

Patches

0

No patches discovered yet.

Vulnerability mechanics

Root cause

"Calling trigger_modechanged() before VIsual is fully initialized in n_start_visual_mode() causes a heap-based buffer overflow when ModeChanged autocommands execute."

Attack vector

An attacker can craft a Vim script or input that triggers the `ModeChanged` autocommand during visual mode entry. Because `trigger_modechanged()` was called before `VIsual` was fully set, the autocommand could operate on an inconsistent visual state, leading to a heap-based buffer overflow [ref_id=1]. The attack requires the victim to execute the malicious script or open a crafted file in Vim.

Affected code

The bug is in `n_start_visual_mode()` in Vim's source code. The patch moves the `trigger_modechanged()` call from before setting `VIsual_active` and `VIsual_reselect` to after those assignments and after `foldAdjustVisual()` [ref_id=1].

What the fix does

The patch moves the `trigger_modechanged()` call in `n_start_visual_mode()` to after `VIsual_active`, `VIsual_reselect`, and `foldAdjustVisual()` are set [ref_id=1]. This ensures that when the `ModeChanged` autocommand fires, the visual selection state is fully initialized, preventing the crash and heap buffer overflow.

Preconditions

  • inputVictim must open a file or execute a Vim script that defines a ModeChanged autocommand and enters visual mode.
  • configThe ModeChanged autocommand must perform an action (e.g., `norm 0u`) that relies on a fully initialized visual state.

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

References

6

News mentions

0

No linked articles in our index yet.