CVE-2021-29657 Overview
CVE-2021-29657 is a use-after-free vulnerability in the Linux kernel's KVM (Kernel-based Virtual Machine) subsystem, specifically affecting the nested SVM (Secure Virtual Machine) implementation for AMD processors. The vulnerability exists in arch/x86/kvm/svm/nested.c in Linux kernel versions before 5.11.12 and allows an AMD KVM guest to bypass access control on host OS MSRs (Model-Specific Registers) when nested guests are present. This flaw stems from a Time-of-Check Time-of-Use (TOCTOU) race condition caused by a VMCB12 double fetch in the nested_svm_vmrun function.
Critical Impact
A malicious nested guest VM can exploit this race condition to gain unauthorized access to host system MSRs, potentially leading to full host compromise, information disclosure, or denial of service in virtualized environments running AMD KVM with nested virtualization enabled.
Affected Products
- Linux Kernel versions prior to 5.11.12
- Systems running AMD KVM with nested virtualization enabled
- Virtualization platforms utilizing vulnerable kernel versions
Discovery Timeline
- 2021-07-22 - CVE-2021-29657 published to NVD
- 2024-11-21 - Last updated in NVD database
Technical Details for CVE-2021-29657
Vulnerability Analysis
This vulnerability represents a classic TOCTOU race condition in the nested virtualization code path. When the Linux kernel handles nested virtual machine execution on AMD processors using SVM, it must process the Virtual Machine Control Block (VMCB) structure that controls guest VM behavior. The nested_svm_vmrun function performs a double fetch of the VMCB12 structure (the L1 guest's VMCB for the L2 nested guest), creating a window where the memory contents can be modified between the validation check and the actual use.
The use-after-free condition occurs when the attacker manipulates the VMCB12 structure during this race window. By precisely timing modifications to the guest-controlled VMCB memory, an attacker can cause the kernel to use stale or malicious values that were validated in a previous fetch but have since been altered. This allows bypassing access control mechanisms that protect host MSRs from guest access.
MSRs are privileged CPU registers that control low-level processor behavior including power management, performance monitoring, and security features. Unauthorized access to these registers from a guest VM represents a significant security boundary violation in virtualized environments.
Root Cause
The root cause is a TOCTOU race condition in arch/x86/kvm/svm/nested.c where the nested_svm_vmrun function fetches the VMCB12 structure from guest memory multiple times without proper synchronization. The first fetch validates the structure, while subsequent fetches use the data, allowing the guest to modify the structure between validation and use.
Attack Vector
The attack requires local access to a nested guest VM on an affected system. An attacker operating within an L2 nested guest can exploit the race condition by:
- Triggering the nested_svm_vmrun code path through nested virtualization operations
- Precisely timing modifications to the VMCB12 structure in shared memory
- Racing against the kernel's double fetch to substitute malicious values after validation
- Gaining unauthorized access to host MSRs that should be protected from guest access
This is a local attack vector requiring no user interaction, but exploitation complexity is high due to the precise timing requirements inherent in race condition exploitation.
The vulnerability manifests in the nested_svm_vmrun function where VMCB12 data is fetched multiple times from guest-accessible memory. The fix involves caching the VMCB12 data after the initial fetch to prevent the guest from modifying it between validation and use. Technical details are available in the Linux Kernel Commit Details and the Chromium Project Zero Issue.
Detection Methods for CVE-2021-29657
Indicators of Compromise
- Unusual activity or crashes related to KVM nested virtualization operations
- Unexpected MSR access attempts logged by the hypervisor
- Anomalous behavior from nested guest VMs attempting repeated VMRUN operations
- Kernel oops or panics in the svm/nested.c code path
Detection Strategies
- Monitor for kernel log entries indicating issues in the nested SVM code path
- Implement runtime integrity checking for KVM module behavior
- Deploy kernel-level intrusion detection to identify exploitation attempts targeting race conditions
- Use audit logging to track nested virtualization operations
Monitoring Recommendations
- Enable verbose KVM logging for nested virtualization events
- Monitor system stability for signs of race condition exploitation attempts
- Track kernel version deployment across virtualized infrastructure
- Implement alerting for anomalous nested VM behavior patterns
How to Mitigate CVE-2021-29657
Immediate Actions Required
- Upgrade Linux kernel to version 5.11.12 or later immediately on all affected systems
- If patching is not immediately possible, consider disabling nested virtualization on AMD KVM hosts
- Review virtualized infrastructure for systems running vulnerable kernel versions
- Assess exposure of systems allowing untrusted nested guest VMs
Patch Information
The vulnerability was fixed in Linux kernel version 5.11.12 through commit a58d9166a756a0f4a6618e4f593232593d6df134. The patch addresses the TOCTOU race condition by ensuring the VMCB12 structure is fetched only once and cached for subsequent operations, eliminating the double fetch vulnerability.
Detailed patch information is available in the Linux Kernel ChangeLog and the Linux Kernel Commit Details.
Workarounds
- Disable nested virtualization on AMD KVM hosts if not required for operations
- Restrict access to create nested VMs to trusted users only
- Implement network segmentation to limit exposure of vulnerable hypervisors
- Consider migrating critical workloads to patched hosts before upgrading remaining infrastructure
# Disable nested virtualization for AMD KVM
echo 0 > /sys/module/kvm_amd/parameters/nested
# Or set in module configuration
echo "options kvm_amd nested=0" > /etc/modprobe.d/kvm-amd.conf
# Verify nested virtualization status
cat /sys/module/kvm_amd/parameters/nested
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

