Re: [PATCH] audit: fix incomplete NETFILTER_PKT records and return void

From: Paul Moore

Date: Mon Nov 30 2026 - 20:09:20 EST


On Tue, Nov 17, 2026 at 8:35 AM jiangjiehua (A) <jiangjiehua1@xxxxxxxxxx> wrote:
>
> Thanks for the review.
>
> On 11/10/2026 9:10 PM, Pablo Neira Ayuso wrote:
> > Hi,
> >
> > On Tue, Nov 10, 2026 at 08:29:39PM +0800, JiangJieHua wrote:
> >> Commit f19590b07cb6 ("audit: add audit_log_nf_skb helper function") and
> >> commit 15b0c43aa621 ("audit: include source and destination ports to
> >> NETFILTER_PKT") introduced audit_log_nf_skb() and its transport header
> >> parsing.
> >
> > I guess conntrack is not enough in your scenario. How does your
> > ruleset look like?
> >
>
> The main scenario is conntrack not being enabled. The ruleset is:
>
> Bridge table: nat
> Bridge chain: PREROUTING, entries: 1, policy: ACCEPT
> -j AUDIT
>
> table inet filter {
> chain output {
> type filter hook output priority filter; policy accept;
> log level audit
> }
> }
>
> Even with conntrack enabled, two cases are still affected:
> - The bridge nat PREROUTING hook runs at priority -300, before conntrack
> at -200.AUDIT sees the packet before conntrack has validated,
> trimmed, or defragged it.
> - Normal IPv6 packets with extension headers. ip6_rcv_core() sets
> transport_header to the first extension header, and conntrack does not
> update it. This makes skb_transport_offset() incorrect, so we read ports
> from the wrong location.
>
> > More comments below.
> >
> >> The helper returned early with -ENOMEM when skb_header_pointer() failed for
> >> the network header, and the later port parsing did the same for the
> >> transport header. Such an early return skips the remaining fields, so the
> >> NETFILTER_PKT record is emitted with only "mark=" and without the address
> >> and protocol fields.
> >>
> >> Log the address and protocol once the network header is available, and
> >> route the error paths through placeholder logging: a missing network header
> >> logs "saddr=? daddr=? proto=-1", as the pre-refactoring callers did, and a
> >> transport header that cannot be read where one is expected logs
> >> "sport=? dport=?". The same "proto=-1" placeholder is used for an
> >> unknown bridge ethertype and an unknown nfproto, which previously printed
> >> "proto=?".
> >>
> >> Validate the IP version and the IPv4 header length, since bridge packets
> >> can reach the audit hook without the receive path validating them. Locate
> >> the transport header from the network header instead of relying on
> >> skb_transport_offset(), which is not guaranteed to point at the transport
> >> header. For IPv4, a non-first fragment or an unreadable transport header
> >> logs unknown ports; protocols without ports are unaffected. For IPv6, a
> >> non-first fragment leaves proto at IPPROTO_FRAGMENT (44) and a negative
> >> offset from ipv6_skip_exthdr() means the extension header chain cannot be
> >> parsed; in both cases the transport header is not available, so keep the
> >> addresses and protocol and do not log ports.
> >>
> >> Both callers ignore the return value (net/netfilter/nft_log.c and
> >> net/netfilter/xt_AUDIT.c), so make the helper return void instead of
> >> inventing error codes for a value nobody consumes.
> >>
> >> A packet whose network header cannot be read now logs:
> >>
> >> before: mark=0x0
> >> after: mark=0x0 saddr=? daddr=? proto=-1
> > [...]
> >> diff --git a/kernel/audit.c b/kernel/audit.c
> >> index 631d6d4a23cf..c5997077a46b 100644
> >> --- a/kernel/audit.c
> >> +++ b/kernel/audit.c
> >> @@ -2511,8 +2511,8 @@ void audit_log_path_denied(int type, const char *operation)
> >> audit_log_end(ab);
> >> }
> >>
> >> -int audit_log_nf_skb(struct audit_buffer *ab,
> >> - const struct sk_buff *skb, u8 nfproto)
> >> +void audit_log_nf_skb(struct audit_buffer *ab,
> >> + const struct sk_buff *skb, u8 nfproto)
> >> {
> >> /* find the IP protocol in the case of NFPROTO_BRIDGE */
> >> if (nfproto == NFPROTO_BRIDGE) {
> >> @@ -2532,24 +2532,32 @@ int audit_log_nf_skb(struct audit_buffer *ab,
> >> case NFPROTO_IPV4: {
> >> struct iphdr iph;
> >> const struct iphdr *ih;
> >> + int thoff;
> >> + bool frag;
> >>
> >> ih = skb_header_pointer(skb, skb_network_offset(skb),
> >> sizeof(iph), &iph);
> >> if (!ih)
> >> - return -ENOMEM;
> >> + goto unknown_proto;
> >> + if (ih->version != 4 || ih->ihl < 5)
> >> + goto unknown_proto;
> >> +
> >> + thoff = skb_network_offset(skb) + ih->ihl * 4;
> >> + frag = ntohs(ih->frag_off) & IP_OFFSET;
> >
> > Are we going to add all these validations everywhere in the code?
> >
> > So we will have to validate that the IPv4 header is well-formed over
> > and over again?
>
> The version/ihl checks were added to handle malformed headers seen by
> bridge hooks. Should we drop these checks, or move the validation into a
> common helper as Florian suggested?

>From an audit perspective it would be nice if we could simply call a
network helper that would let us know if the packet is properly formed
or garbage. I would think the netdev folks would prefer that as well,
but one never knows. You haven't gotten any definitive feedback on
this, so you might consider putting together a small RFC patch with
some suitable helpers and sending it to netdev/netfilter-devel (please
CC me too) to see if there is any interest.

> For normal packets, I think we should keep the fixes for non-first
> fragments and transport offset calculation, since those are unrelated to
> malformed headers.

I imagine that you could make a helper to report if the skb is
fragmented. It could return a simple boolean flag and I think it
should only be a single line ... ?

--
paul-moore.com