Page MenuHomeFreeBSD

pf: don't use state keys after pf_state_insert()
ClosedPublic

Authored by kp on Mar 28 2025, 2:47 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Apr 30, 6:31 AM
Unknown Object (File)
Mon, Apr 21, 9:25 PM
Unknown Object (File)
Mon, Apr 21, 1:45 PM
Unknown Object (File)
Thu, Apr 17, 2:47 AM
Unknown Object (File)
Tue, Apr 15, 3:46 AM
Unknown Object (File)
Mon, Apr 14, 12:32 PM
Unknown Object (File)
Sun, Apr 13, 4:08 AM
Unknown Object (File)
Wed, Apr 9, 12:10 PM

Details

Summary

pf_state_insert() may free the state keys, it's not safe to access these
pointers after the call.

Introduce osrc/odst (similar to osport/odport) to store the original source and
destination addresses. This allows us to undo NAT transformations without having
to access the state keys.

MFC after: 3 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

kp requested review of this revision.Mar 28 2025, 2:47 PM

The pf_pdesc is always on the stack, is it?

This revision is now accepted and ready to land.Mar 28 2025, 8:08 PM
markj added inline comments.
sys/netpfil/pf/pf.c
6265

Maybe it'd be nice to deduplicate this with the identical code in pf_return().

9926

This turns into a function call where we switch on af, even though we know its value here. It'd be nicer to have pf_addrcpy() be inlined, at least when the value of af is known at compile-time. Or have pf_addrcpy_inet()/pf_addrcpy_inet6().

The pf_pdesc is always on the stack, is it?

Yes. In pf_test().
There's another instance in pf_icmp_state_lookup(), for the embedded packet in ICMP errors. That's on the stack too.

sys/netpfil/pf/pf.c
6265

I'll propose a separate commit for that.

9926

I'm tempted to move pf_addrcpy() into pfvar.h so we can leave that to the compiler.
We basically always 'PF_ACPY()' to copy addresses, and I'd like to not have to think about why we sometimes do and sometimes don't.

sys/netpfil/pf/pf.c
9926

That makes sense to me. I'd maybe also hardcode the value of af here, i.e., PF_ACPY(..., AF_INET6), just in case.

This revision was automatically updated to reflect the committed changes.