Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.
Sources: National Vulnerability Database, CISA KEV, Exploit-DB
CVE-2024-40958 Overview
CVE-2024-40958 is a use-after-free vulnerability [CWE-416] in the Linux kernel's network namespace (netns) subsystem. The flaw resides in get_net_ns(), which failed to handle a network namespace with a zero reference count. A local attacker manipulating TUN devices across network namespaces can trigger a refcount addition on an already-freed struct net, causing memory corruption and kernel panic. Syzkaller originally surfaced the issue with a refcount_t: addition on 0; use-after-free warning on kernel 6.10.0-rc3. The fix replaces the unconditional get_net() with maybe_get_net(), which refuses to increment a zero refcount.
Critical Impact
Local, low-privileged users can corrupt kernel memory and crash the system through TUN ioctl operations across network namespaces, enabling denial of service and potential privilege escalation.
Affected Products
- Linux kernel versions preceding the stable fix commits
- Linux kernel 6.10 release candidates 6.10-rc1 through 6.10-rc4
- Downstream distributions including Debian LTS and multiple Siemens industrial products (SSA-265688, SSA-355557, SSA-613116)
Discovery Timeline
- 2024-07-12 - CVE-2024-40958 published to NVD
- 2026-10-15 - Last updated in NVD database
Technical Details for CVE-2024-40958
Vulnerability Analysis
The vulnerability is a use-after-free condition in the kernel's network namespace reference counting logic. When a TUN device is created in one network namespace (ns0) and then moved to another namespace (ns1) using ip link set tun0 netns ns1, the original namespace's reference count can drop to zero. If a thread concurrently issues the TUNGETDEVNETNS ioctl on the TUN character device, __tun_chr_ioctl() resolves the device's network namespace through dev_net(tun->dev) and passes it to open_related_ns(), which calls get_net_ns(). The helper unconditionally invokes get_net(), performing a refcount increment on a struct net that has already reached zero and begun teardown. The resulting refcount_t: addition on 0 warning escalates to a kernel panic when panic_on_warn is set.
Root Cause
The root cause is missing refcount validation in get_net_ns(). The function assumed the referenced struct net was still live, but TUN device namespace migration can race with namespace destruction. The upstream fix substitutes maybe_get_net() for get_net(), returning failure when the refcount is zero rather than corrupting it.
Attack Vector
Exploitation requires local access with the ability to create TUN devices and manipulate network namespaces. An attacker scripts the race between TUNGETDEVNETNS ioctl calls and namespace teardown triggered by moving the TUN device to another namespace. Successful races leave the kernel operating on freed struct net memory, enabling denial of service and, depending on heap layout, potential exploitation primitives against kernel memory.
The vulnerability chain, as documented in the upstream commit message, follows this sequence: tun_set_iff() assigns the device to ns0; moving the device to ns1 drops the original namespace refcount to zero; a concurrent TUNGETDEVNETNS ioctl invokes open_related_ns(&net->ns, get_net_ns), which calls get_net() on the already-freed namespace.
Detection Methods for CVE-2024-40958
Indicators of Compromise
- Kernel log entries containing refcount_t: addition on 0; use-after-free originating from refcount_warn_saturate+0xdf/0x1d0
- Call traces showing get_net_ns invoked from open_related_ns and __tun_chr_ioctl paths
- Unexpected kernel panics on hosts that run container runtimes, VPN clients, or orchestrators that heavily manipulate TUN interfaces and network namespaces
Detection Strategies
- Monitor dmesg and /var/log/kern.log for refcount_warn_saturate warnings tied to network namespace functions
- Audit process execution for sequences combining ioctl(TUNSETIFF), ip link set ... netns, and ioctl(TUNGETDEVNETNS) from the same or coordinating processes
- Track unshare, setns, and clone with CLONE_NEWNET activity from unprivileged users as a precondition for exploitation attempts
Monitoring Recommendations
- Centralize kernel logs and alert on refcount_t warnings, especially from get_net_ns, open_related_ns, or __tun_chr_ioctl symbols
- Baseline TUN/TAP device creation volume per host and investigate anomalous spikes from non-service accounts
- Correlate kernel panics with recent container workload changes or user-mode namespace activity to surface exploitation attempts
How to Mitigate CVE-2024-40958
Immediate Actions Required
- Apply the stable kernel update for your distribution that includes the maybe_get_net() fix in get_net_ns()
- Prioritize patching hosts that permit unprivileged user namespaces or run multi-tenant container workloads
- Review Siemens advisories (SSA-265688, SSA-355557, SSA-613116) for industrial product remediation guidance
Patch Information
The fix was committed upstream across multiple stable branches. Reference the upstream commits 1b631bf, 2b82028, 3a6cd32, 3af28df, cb7f811, ef0394ca, and ff960f9d. Debian LTS users should follow the Debian LTS Announcement January 2025.
Workarounds
- Disable unprivileged user namespaces where feasible by setting kernel.unprivileged_userns_clone=0 to limit namespace manipulation to privileged users
- Restrict access to /dev/net/tun through filesystem permissions or device cgroups so that only trusted workloads can issue TUN ioctls
- Apply seccomp or AppArmor/SELinux profiles to container workloads that deny the TUNGETDEVNETNS ioctl path where not required
# Configuration example
# Restrict unprivileged user namespace creation
sysctl -w kernel.unprivileged_userns_clone=0
echo 'kernel.unprivileged_userns_clone=0' >> /etc/sysctl.d/99-harden-netns.conf
# Restrict access to /dev/net/tun to the 'tun' group
chown root:tun /dev/net/tun
chmod 0660 /dev/net/tun
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.
Sources: National Vulnerability Database, CISA KEV, Exploit-DB

