netfilter: logging cleanups
Subsystems (2)
Florian Westphal
maintainercover netfilter: logging cleanups
1) Remove dead ARPHRD_SIT special-casing from MAC/HW header logging in nf_log_syslog and nfnetlink_log. The branch became unreachable once SIT tunnel hard_header_len was zeroed.
2) Fix MAC header dump in nf_log_syslog to use skb_mac_header_len() clamped to dev->hard_header_len instead of unconditionally logging hard_header_len bytes, preventing reads into the IP/IPv6 header.
3) Refactor hw address extraction into a shared helper for nfnetlink_log and nfnetlink_queue. Clamp dev_parse_header() result to 8 bytes and validate skb preconditions in the common helper. Add BUILD_BUG_ON assertions for identical container struct layouts.
4) Use skb_mac_header_len() from indev instead of hard_header_len from skb->dev for NFULA_HWHEADER length. Omit the NFULA_HWHEADER attribute when the computed length is zero.
5) Skip hwheader logging on output hooks where the MAC header is stale. Gate emission with a family-and-hook aware nfulnl_pre_egress() check.
6) Emit the nfnetlink_log timestamp only on the input path, since output-path skb->tstamp may carry an SO_TXTIME delivery deadline rather than an observation time. Replace the broken raw hooknum comparison with nfulnl_pre_egress() so ARP and NETDEV families no longer emit timestamps at their output hooks.
Florian Westphal (6):
netfilter: nf_log: remove dead ARPHRD_SIT handling from MAC/HW header logging
netfilter: nf_log_syslog: fix MAC header dump to use real header length
netfilter: nfnetlink_log: fix up dev_parse_header usage
netfilter: nfnetlink_log: use real mac header length for hwheader
netfilter: nfnetlink_log: don't log hwheader on output hooks
netfilter: nfnetlink_log: only add timestamp on the input path
net/netfilter/nf_log_syslog.c | 28 ++--- net/netfilter/nfnetlink_log.c | 151 ++++++++++++++++++++------- net/netfilter/nfnetlink_log_common.h | 106 +++++++++++++++++++ net/netfilter/nfnetlink_queue.c | 18 +--- 4 files changed, 232 insertions(+), 71 deletions(-) create mode 100644 net/netfilter/nfnetlink_log_common.h
Signature · 1 line
2.56.1
Florian Westphal
maintainerPATCH 1/6 netfilter: nf_log: remove dead ARPHRD_SIT handling from MAC/HW header logging
dump_mac_header() in nf_log_syslog.c and the HWHEADER dump in nfnetlink_log.c special-case ARPHRD_SIT devices by backing the header pointer up by ETH_HLEN before dumping dev->hard_header_len bytes, on the assumption that a SIT tunnel's "hard header" covers the underlying device's real Ethernet header plus the tunnel's IPv4 header.
That assumption stopped holding after commit 8ef9ba4d6666 ("IPv6: Set SIT tunnel hard_header_len to zero"), which zeroed dev->hard_header_len for SIT tunnels.
The syslog call site was gated on dev->hard_header_len being nonzero, so the ARPHRD_SIT branch there has been unreachable dead code for every SIT device ever since.
For nfnetlink_log the branch was still reachable, but the adjusted pointer is never read: nla_put() for the NFULA_HWHEADER attribute uses dev->hard_header_len as the length, which is 0 for SIT tunnels.
Remove the ARPHRD_SIT special-handling; it's been 6 years and no one reported this ever since.
--- net/netfilter/nf_log_syslog.c | 17 +---------------- net/netfilter/nfnetlink_log.c | 6 +----- 2 files changed, 2 insertions(+), 21 deletions(-)
net/netfilter/nf_log_syslog.c +1 -16
@@ -820,26 +820,11 @@ static void dump_mac_header(struct nf_log_buf *m,const unsigned char *p =skb_mac_header (skb);unsigned int i; -if (dev->type ==ARPHRD_SIT ) { - p -=ETH_HLEN ; - -if (p < skb->head) - p =NULL ; - } - -if (p) { +if (1 ) {nf_log_buf_add (m,"%02x" , *p++);for (i =1 ; i < dev->hard_header_len; i++)nf_log_buf_add (m,":%02x" , *p++); } - -if (dev->type ==ARPHRD_SIT ) { -const struct iphdr *iph = - (struct iphdr *)skb_mac_header (skb); - -nf_log_buf_add (m," TUNNEL=%pI4->%pI4" , &iph->saddr, - &iph->daddr); - } }nf_log_buf_add (m," " ); }
net/netfilter/nfnetlink_log.c +1 -5
@@ -610,11 +610,7 @@ __build_packet_message(struct nfnl_log_net *log, hwhdrp =skb_mac_header (skb); -if (skb->dev->type ==ARPHRD_SIT ) - hwhdrp -=ETH_HLEN ; - -if (hwhdrp >= skb->head && -nla_put (inst->skb,NFULA_HWHEADER , +if (nla_put (inst->skb,NFULA_HWHEADER , skb->dev->hard_header_len, hwhdrp)) goto nla_put_failure; }
Signature · 1 line
2.56.1
Florian Westphal
maintainerPATCH 2/6 netfilter: nf_log_syslog: fix MAC header dump to use real header length
In reply to Florian Westphalold code unconditionally iterated dev->hard_header_len bytes, assuming the SKB's MAC header is always exactly that long.
If the actual mac header in the packet is smaller, the loop reads past the MAC and logs ip/ipv6 header as if it was part of the address.
Use skb_mac_header_len() as the base length, then clamp the result to dev->hard_header_len so we never read beyond what the device actually uses for its hard header, even if mac header was mangled.
LLM finding, no bug was ever reported about this.
--- net/netfilter/nf_log_syslog.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
net/netfilter/nf_log_syslog.c +9 -6
@@ -815,14 +815,17 @@ static void dump_mac_header(struct nf_log_buf *m, fallback:nf_log_buf_add (m,"MAC=" ); -if (dev->hard_header_len &&skb_mac_header_was_set (skb) && -skb_mac_header_len (skb) !=0 ) { -const unsigned char *p =skb_mac_header (skb); -unsigned int i; +if (dev->hard_header_len &&skb_mac_header_was_set (skb)) { +unsigned int mac_len; + + mac_len = min_t(unsigned int, skb_mac_header_len(skb), + dev->hard_header_len); +if (mac_len) { +const unsigned char *p =skb_mac_header (skb); +unsigned int i; -if (1 ) {nf_log_buf_add (m,"%02x" , *p++); -for (i =1 ; i < dev->hard_header_len; i++) +for (i =1 ; i < mac_len; i++)nf_log_buf_add (m,":%02x" , *p++); } }
Signature · 1 line
2.56.1
Florian Westphal
maintainerPATCH 3/6 netfilter: nfnetlink_log: fix up dev_parse_header usage
In reply to Florian Westphaldev_parse_header() grabs the source hw address from the skb and stores it in the given buffer.
e.g.: eth_header_parse() is a simple:
int eth_header_parse(const struct sk_buff *skb,
const struct net_device *dev,
unsigned char *haddr) {
memcpy(haddr, eth_hdr(skb)->h_source, ETH_ALEN);
return ETH_ALEN;
}Problem is that the compound structure used by the UABI in nfnetlink_log and nfnetlink_queue has a fixed 8-byte size.
Enough for ETH_ALEN, but not necessarily enough for other address types. Move this to a helper and allow sharing it between the copypasted users.
The struct layout of the two container structures have same layout:
add BUILD_BUG_ON checks to assert this.
Due to the nature of the dev_parse_header() helpers:
1. skb MUST have a valid mac header, else we read garbage
2. users MUST NOT use this when ingress device and skb->dev have diverged (we can't know we would decode something meaningful anymore).
3. Because container structure only has space for 8 octets, we have to clamp the result.
Do all of this in the common helper.
This is an LLM finding, no bug was ever reported about this.
--- net/netfilter/nfnetlink_log.c | 18 +---- net/netfilter/nfnetlink_log_common.h | 106 +++++++++++++++++++++++++++ net/netfilter/nfnetlink_queue.c | 18 +---- 3 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 net/netfilter/nfnetlink_log_common.h
net/netfilter/nfnetlink_log.c +4 -14
@@ -47,6 +47,8 @@#include <net/netfilter/nf_conntrack.h> #endif +#include "nfnetlink_log_common.h" +#define NFULNL_COPY_DISABLED 0xff#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE#define NFULNL_TIMEOUT_DEFAULT 100/* every second */
@@ -587,20 +589,8 @@ __build_packet_message(struct nfnl_log_net *log, nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark))) goto nla_put_failure; -if (indev && skb->dev && -skb_mac_header_was_set (skb) && -skb_mac_header_len (skb) !=0 ) { -struct nfulnl_msg_packet_hw phw; -int len; - -memset (&phw,0 ,sizeof (phw)); - len =dev_parse_header (skb, phw.hw_addr); -if (len >0 ) { - phw.hw_addrlen =htons (len); -if (nla_put (inst->skb,NFULA_HWADDR ,sizeof (phw), &phw)) - goto nla_put_failure; - } - } +if (nfnetlink_put_hwaddr (inst->skb, skb, indev,NFULA_HWADDR )) + goto nla_put_failure;if (indev &&skb_mac_header_was_set (skb)) { if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
net/netfilter/nfnetlink_log_common.h new file +106 -0
106 lines not on this page · raw message
net/netfilter/nfnetlink_queue.c +4 -14
@@ -56,6 +56,8 @@#include <net/netfilter/nf_conntrack.h> #endif +#include "nfnetlink_log_common.h" +#define NFQNL_QMAX_DEFAULT 1024#define NFQNL_HASH_MIN 8#define NFQNL_HASH_MAX 32768
@@ -906,20 +908,8 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, nla_put_be32(skb, NFQA_PRIORITY, htonl(entskb->priority))) goto nla_put_failure; -if (indev && entskb->dev && -skb_mac_header_was_set (entskb) && -skb_mac_header_len (entskb) !=0 ) { -struct nfqnl_msg_packet_hw phw; -int len; - -memset (&phw,0 ,sizeof (phw)); - len =dev_parse_header (entskb, phw.hw_addr); -if (len) { - phw.hw_addrlen =htons (len); -if (nla_put (skb,NFQA_HWADDR ,sizeof (phw), &phw)) - goto nla_put_failure; - } - } +if (nfnetlink_put_hwaddr (skb, entskb, indev,NFQA_HWADDR )) + goto nla_put_failure;if (nfqnl_put_bridge (entry, skb) <0 ) goto nla_put_failure;
Signature · 1 line
2.56.1
Florian Westphal
maintainerPATCH 4/6 netfilter: nfnetlink_log: use real mac header length for hwheader
In reply to Florian Westphalnfnetlink_log uses skb->dev->hard_header_len for the length of the NFULA_HWHEADER attribute.
hard_header_len says how long a link layer header can get, not how many bytes this packet holds. Use skb_mac_header_len() instead and cap at hard_header_len, as before, if needed.
Also take hwtype and hard_header_len from indev instead of skb->dev:skb->dev may have been changed since the packet was received, indev is the device that wrote the mac header.
If the resulting length is 0, omit NFULA_HWHEADER instead of sending an empty attribute. NFULA_HWLEN is still sent, userspace relies on it.
I suspect that this is almost exclusively used with plain ethernet, where hard_header_len and skb_mac_header_len() are both 14 bytes, which would explain the absence of bug reports.
--- net/netfilter/nfnetlink_log.c | 75 +++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 13 deletions(-)
net/netfilter/nfnetlink_log.c +62 -13
@@ -458,6 +458,65 @@ static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buffreturn -1 ; } +/** +* nfulnl_put_hwheader - Append link-layer header info to the netlink skb. +* @nlskb: Target netlink message to append attributes to. +* @skb: Original packet skb whose link-layer header is described. +* @indev: The input device that received @skb. +* +* Emits the NFULA_HWTYPE and NFULA_HWLEN netlink attributes into @nlskb +* based on the link-layer header of @skb. +* +* @indev, not @skb->dev, is the source of the device's type and +* hard_header_len: on bridging and br_netfilter paths skb->dev is +* repointed to the logical or egress device while @indev keeps +* referring to the device that actually wrote @skb's mac header. +* +* The device's hard_header_len describes the maximum size a link-layer +* header can grow to, not the actual size of this packet's header. +* The actual length is taken from skb_mac_header_len() and capped at +* hard_header_len if needed. +* +* The NFULA_HWLEN attribute is retained for backward compatibility. +* Userspace should derive the length from the NFULA_HWHEADER attribute +* size instead. +* +* Caller must ensure @indev is not NULL and check +* skb_mac_header_was_set(skb) before using this function. +* +* Return: -EMSGSIZE if the tailroom of the skb is insufficient to store +* the attribute header and payload. +*/ +static int nfulnl_put_hwheader (struct sk_buff *nlskb,const struct sk_buff *skb, +const struct net_device *indev) +{ +unsigned int hwlen =READ_ONCE (indev->hard_header_len); +const unsigned char *hwhdrp =skb_mac_header (skb); +int len =skb_mac_header_len (skb); +u16 hwtype = indev->type; + +if (nla_put_be16 (nlskb,NFULA_HWTYPE ,htons (hwtype))) +return -EMSGSIZE ; + +if (len <0 ) + len =0 ; +else if ((unsigned int )len > hwlen) + len = hwlen; + +/* This attribute should not exist, userspace should +* use NFULA_HWHEADER attribute size. Removing +* this breaks existing users which unfortunately +* rely on NFULA_HWLEN presence. +*/ +if (nla_put_be16 (nlskb,NFULA_HWLEN ,htons (len))) +return -EMSGSIZE ; + +if (len ==0 ) +return 0 ; + +return nla_put (nlskb,NFULA_HWHEADER , len, hwhdrp); +} +#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )static int nflog_put_master_ifindex (struct sk_buff *nlskb,int attr,const struct net_device *dev)
@@ -494,7 +553,6 @@ __build_packet_message(struct nfnl_log_net *log,struct nlmsghdr *nlh;sk_buff_data_t old_tail = inst->skb->tail;struct sock *sk; -const unsigned char *hwhdrp; nlh = nfnl_msg_put(inst->skb, 0, 0, nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
@@ -592,18 +650,9 @@ __build_packet_message(struct nfnl_log_net *log,if (nfnetlink_put_hwaddr (inst->skb, skb, indev,NFULA_HWADDR )) goto nla_put_failure; -if (indev &&skb_mac_header_was_set (skb)) { -if (nla_put_be16 (inst->skb,NFULA_HWTYPE ,htons (skb->dev->type)) || -nla_put_be16 (inst->skb,NFULA_HWLEN , -htons (skb->dev->hard_header_len))) - goto nla_put_failure; - - hwhdrp =skb_mac_header (skb); - -if (nla_put (inst->skb,NFULA_HWHEADER , - skb->dev->hard_header_len, hwhdrp)) - goto nla_put_failure; - } +if (indev &&skb_mac_header_was_set (skb) && +nfulnl_put_hwheader (inst->skb, skb, indev)) + goto nla_put_failure;if (hooknum <=NF_INET_FORWARD ) {struct timespec64 kts =ktime_to_timespec64 (skb_tstamp_cond (skb, true));
Signature · 1 line
2.56.1
Florian Westphal
maintainerPATCH 5/6 netfilter: nfnetlink_log: don't log hwheader on output hooks
In reply to Florian WestphalOn output hooks (LOCAL_OUT, POST_ROUTING and their per-family equivalents) the mac header is either unset or still belongs to the ingress device, not the egress one, so we would log stale data. Only emit the hardware header attributes while the packet is still on the input path.
The check has to use both family and hook value, hook numbers are family specific, e.g. NF_BR_BROUTING has the same value as NF_INET_INGRESS.
nfulnl_pre_egress(pf, hooknum) answers the question the check cares
about: has the packet left indev's input path yet, i.e. does skb's mac header still belong to indev.
This keeps hwheader logging working for the nftables inet ingress hook (nft_do_chain_inet_ingress() sets pf to NFPROTO_IPV4/IPV6 with hook = NF_INET_INGRESS) and for the ebtables broute table (NF_BR_BROUTING, indev and mac header are those of the received frame). For NFPROTO_ARP and NFPROTO_NETDEV only NF_ARP_IN and NF_NETDEV_INGRESS are accepted.
--- net/netfilter/nfnetlink_log.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
net/netfilter/nfnetlink_log.c +33 -1
@@ -21,6 +21,7 @@#include <linux/netdevice.h> #include <linux/netfilter.h> #include <linux/netfilter_bridge.h> +#include <linux/netfilter_arp.h> #include <net/netlink.h> #include <linux/netfilter/nfnetlink.h> #include <linux/netfilter/nfnetlink_log.h>
@@ -458,6 +459,36 @@ static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buffreturn -1 ; } +/** +* nfulnl_pre_egress - Can @skb's mac header still be attributed to @indev? +* @pf: protocol family of the hook this packet is being logged from. +* @hooknum: hook number within @pf's family. +* +* True only for hook points where the packet has not yet left @indev's +* input path, i.e. where @skb's mac header still reflects what @indev +* actually received. Past LOCAL_OUT/POST_ROUTING (and their per-family +* equivalents) the packet may have been re-routed, re-encapsulated, or +* is headed to a different device than it arrived on. +* +* Return: true if mac header can be attributed to indev +*/ +static bool nfulnl_pre_egress (u8 pf,unsigned int hooknum) +{ +switch (pf) { +case NFPROTO_IPV4 : +case NFPROTO_IPV6 : +return hooknum <=NF_INET_FORWARD || hooknum ==NF_INET_INGRESS ; +case NFPROTO_BRIDGE : +return hooknum <=NF_BR_FORWARD || hooknum ==NF_BR_BROUTING ; +case NFPROTO_ARP : +return hooknum ==NF_ARP_IN ; +case NFPROTO_NETDEV : +return hooknum ==NF_NETDEV_INGRESS ; +default : +return false; + } +} +/** * nfulnl_put_hwheader - Append link-layer header info to the netlink skb. * @nlskb: Target netlink message to append attributes to.
@@ -650,7 +681,8 @@ __build_packet_message(struct nfnl_log_net *log,if (nfnetlink_put_hwaddr (inst->skb, skb, indev,NFULA_HWADDR )) goto nla_put_failure; -if (indev &&skb_mac_header_was_set (skb) && +if (nfulnl_pre_egress (pf, hooknum) && + indev &&skb_mac_header_was_set (skb) &&nfulnl_put_hwheader (inst->skb, skb, indev)) goto nla_put_failure;
Signature · 1 line
2.56.1
Florian Westphal
maintainerPATCH 6/6 netfilter: nfnetlink_log: only add timestamp on the input path
In reply to Florian WestphalThe timestamp attribute should only be emitted while the packet is on the input path, like the hardware header attributes. On output hooks skb->tstamp can hold the SO_TXTIME transmit time, a delivery deadline rather than the time the packet was observed.
The old check compared the raw hooknum against NF_INET_FORWARD, which is meaningless for families with their own hook numbering: it was too liberal for NFPROTO_ARP and NFPROTO_NETDEV. Use nfulnl_pre_egress() instead, so those no longer emit a timestamp at their output hooks.
--- net/netfilter/nfnetlink_log.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)
net/netfilter/nfnetlink_log.c +21 -11
@@ -548,6 +548,22 @@ static int nfulnl_put_hwheader(struct sk_buff *nlskb, const struct sk_buff *skb,return nla_put (nlskb,NFULA_HWHEADER , len, hwhdrp); } +/* The timestamp is the time the packet was observed at the hook. +* Only called while the packet is on the input path: on output +* hooks skb->tstamp can hold the SO_TXTIME transmit time, which +* is a delivery deadline, not an observation time. +*/ +static int nfulnl_put_timestamp (struct sk_buff *nlskb,const struct sk_buff *skb) +{ +struct timespec64 kts =ktime_to_timespec64 (skb_tstamp_cond (skb, true)); +struct nfulnl_msg_packet_timestamp ts = { + .sec =cpu_to_be64 (kts.tv_sec), + .usec =cpu_to_be64 (kts.tv_nsec /NSEC_PER_USEC ), + }; + +return nla_put (nlskb,NFULA_TIMESTAMP ,sizeof (ts), &ts); +} +#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )static int nflog_put_master_ifindex (struct sk_buff *nlskb,int attr,const struct net_device *dev)
@@ -681,18 +697,12 @@ __build_packet_message(struct nfnl_log_net *log,if (nfnetlink_put_hwaddr (inst->skb, skb, indev,NFULA_HWADDR )) goto nla_put_failure; -if (nfulnl_pre_egress (pf, hooknum) && - indev &&skb_mac_header_was_set (skb) && -nfulnl_put_hwheader (inst->skb, skb, indev)) - goto nla_put_failure; - -if (hooknum <=NF_INET_FORWARD ) { -struct timespec64 kts =ktime_to_timespec64 (skb_tstamp_cond (skb, true)); -struct nfulnl_msg_packet_timestamp ts; - ts.sec =cpu_to_be64 (kts.tv_sec); - ts.usec =cpu_to_be64 (kts.tv_nsec /NSEC_PER_USEC ); +if (nfulnl_pre_egress (pf, hooknum)) { +if (indev &&skb_mac_header_was_set (skb) && +nfulnl_put_hwheader (inst->skb, skb, indev)) + goto nla_put_failure; -if (nla_put (inst->skb,NFULA_TIMESTAMP ,sizeof (ts), &ts)) +if (nfulnl_put_timestamp (inst->skb, skb)) goto nla_put_failure; }
Signature · 1 line
2.56.1