pf: handle divert packets

In a divert setup pf_test_state() may return PF_PASS, but not set the state
pointer. We didn't handle that, and as a result crashed immediately afterwards
trying to dereference that NULL state pointer.

Add a test case to provoke the problem.

PR:		260867
MFC after:	2 weeks
Submitted by:	Phil Budne <phil.budne@gmail.com>
Sponsored by:	Rubicon Communications, LLC ("Netgate")
This commit is contained in:
Kristof Provost
2025-11-15 14:44:54 +01:00
parent 787d09753f
commit 66f2f1c832
2 changed files with 55 additions and 8 deletions
+12 -8
View File
@@ -11075,10 +11075,12 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0
break;
action = pf_test_state(&s, &pd, &reason);
if (action == PF_PASS || action == PF_AFRT) {
if (V_pfsync_update_state_ptr != NULL)
V_pfsync_update_state_ptr(s);
r = s->rule;
a = s->anchor;
if (s != NULL) {
if (V_pfsync_update_state_ptr != NULL)
V_pfsync_update_state_ptr(s);
r = s->rule;
a = s->anchor;
}
} else if (s == NULL) {
/* Validate remote SYN|ACK, re-create original SYN if
* valid. */
@@ -11127,10 +11129,12 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0
default:
action = pf_test_state(&s, &pd, &reason);
if (action == PF_PASS || action == PF_AFRT) {
if (V_pfsync_update_state_ptr != NULL)
V_pfsync_update_state_ptr(s);
r = s->rule;
a = s->anchor;
if (s != NULL) {
if (V_pfsync_update_state_ptr != NULL)
V_pfsync_update_state_ptr(s);
r = s->rule;
a = s->anchor;
}
} else if (s == NULL) {
action = pf_test_rule(&r, &s,
&pd, &a, &ruleset, &reason, inp, &match_rules);
+43
View File
@@ -372,6 +372,47 @@ in_dn_in_div_in_out_div_out_dn_out_cleanup()
pft_cleanup
}
atf_test_case "pr260867" "cleanup"
pr260867_head()
{
atf_set descr 'Test for the loop reported in PR260867'
atf_set require.user root
}
pr260867_body()
{
pft_init
divert_init
epair=$(vnet_mkepair)
ifconfig ${epair}a 192.0.2.1/24 up
vnet_mkjail alcatraz ${epair}b
jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
# Sanity check
atf_check -s exit:0 -o ignore ping -c3 192.0.2.2
jexec alcatraz /usr/sbin/inetd -p ${PWD}/inetd-echo.pid $(atf_get_srcdir)/echo_inetd.conf
jexec alcatraz $(atf_get_srcdir)/../common/divapp 1001 divert-back &
jexec alcatraz pfctl -e
pft_set_rules alcatraz \
"pass in on ${epair}b proto tcp from any to port 7 divert-to 0.0.0.0 port 1001"
reply=$(echo "foo" | nc -N 192.0.2.2 7)
if ["${reply}" != "foo" ];
then
atf_fail "Did not receive echo reply"
fi
}
pr260867_cleanup()
{
pft_cleanup
}
atf_init_test_cases()
{
atf_add_test_case "in_div"
@@ -383,4 +424,6 @@ atf_init_test_cases()
atf_add_test_case "in_div_in_fwd_out_div_out"
atf_add_test_case "in_dn_in_div_in_out_div_out_dn_out"
atf_add_test_case "pr260867"
}