The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=8a14fcd23a201944f3eb5086403d10cfe1fc128f
commit 8a14fcd23a201944f3eb5086403d10cfe1fc128f Author: KUROSAWA Takahiro <[email protected]> AuthorDate: 2026-07-07 19:43:32 +0000 Commit: Kristof Provost <[email protected]> CommitDate: 2026-07-08 07:23:17 +0000 pf: fix a crash on sendfile() The network layer must not pass unmapped (M_EXTPG) mbufs to if_output() of network interfaces without IFCAP_MEXTPG. pf should convert these mbufs by mb_unmapped_to_ext() for such interfaces but it didn't. The problem had occurred on sendfile because sendfile system call uses unmapped mbufs for the file data. Reported by: feld Reviewed by: kp, glebius Differential Revision: https://reviews.freebsd.org/D58021 --- sys/netpfil/pf/pf.c | 35 ++++++++ tests/sys/netpfil/pf/Makefile | 3 +- tests/sys/netpfil/pf/unmapped_mbuf.sh | 145 ++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+), 1 deletion(-) diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index c7d5e923d50b..10e73ce1e7b9 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -9980,6 +9980,21 @@ pf_route(struct pf_krule *r, struct ifnet *oifp, pd->act.dnpipe = tmp; } + /* + * If the output interface does not accept unmapped mbufs, convert + * them to mapped mbufs. + */ + if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { + error = mb_unmapped_to_ext(m0, &md); + if (error) + goto done; + /* + * The first mbuf should not be reallocated because it is + * always mapped. + */ + MPASS(m0 == md); + } + /* * If small enough for interface, or the interface will take * care of the fragmentation for us, we can just send directly. @@ -10321,6 +10336,11 @@ pf_route6(struct pf_krule *r, struct ifnet *oifp, } if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) { + if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { + if (mb_unmapped_to_ext(m0, &md) != 0) + goto done; + MPASS(m0 == md); + } md = m0; pf_dummynet_route(pd, s, r, ifp, sintosa(&dst), &md); if (md != NULL) { @@ -11585,6 +11605,7 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0 { struct pfi_kkif *kif; u_short action, reason = 0; + struct mbuf *m; struct m_tag *mtag; struct pf_krule *a = NULL, *r = &V_pf_default_rule; struct pf_kstate *s = NULL; @@ -11620,6 +11641,12 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0 } if (__predict_false(! M_WRITABLE(*m0))) { + /* Need to convert unmapped mbufs before calling m_unshare(). */ + if (mb_unmapped_to_ext(*m0, &m) != 0) { + *m0 = NULL; + return (PF_DROP); + } + MPASS(*m0 == m); *m0 = m_unshare(*m0, M_NOWAIT); if (*m0 == NULL) { return (PF_DROP); @@ -11639,6 +11666,14 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0 *m0 = NULL; return (PF_PASS); } + + /* + * No need to call mb_unmapped_to_ext() here because it had + * already been called in pf_route()/pf_route6() and dummynet + * re-injected this packet. + */ + + M_ASSERTMAPPED(*m0); (ifp->if_output)(ifp, *m0, sintosa(&pd.pf_mtag->dst), NULL); *m0 = NULL; return (PF_PASS); diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile index ded0002768b0..7b2e32c09543 100644 --- a/tests/sys/netpfil/pf/Makefile +++ b/tests/sys/netpfil/pf/Makefile @@ -56,7 +56,8 @@ ATF_TESTS_SH+= altq \ synproxy \ table \ tcp \ - tos + tos \ + unmapped_mbuf ATF_TESTS_PYTEST+= frag4.py ATF_TESTS_PYTEST+= frag6.py diff --git a/tests/sys/netpfil/pf/unmapped_mbuf.sh b/tests/sys/netpfil/pf/unmapped_mbuf.sh new file mode 100644 index 000000000000..d8005d12b44d --- /dev/null +++ b/tests/sys/netpfil/pf/unmapped_mbuf.sh @@ -0,0 +1,145 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2026 KUROSAWA Takahiro <[email protected]> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +. $(atf_get_srcdir)/utils.subr + +HELPER=$(atf_get_srcdir)/../../common/sendfile_helper + +# pf must call mb_unmapped_to_ext() before passing an output mbuf to a network +# interface if it does not accept unmapped mbufs as ip_output() does. These +# tests make sure that unmapped mbufs are correctly converted to mapped ones +# before pf passes mbufs to interfaces. + +atf_test_case "v4" "cleanup" +v4_head() { + atf_set descr 'unmapped mbuf IPv4 test' + atf_set require.user root +} + +v4_body() { + pft_init + + l=$(vnet_mkepair) + vnet_mkjail a ${l}a + jexec a ifconfig ${l}a 192.0.2.1/24 up + vnet_mkjail b ${l}b + jexec b ifconfig ${l}b 192.0.2.2/24 up + + # Sanity check + atf_check -s exit:0 -o ignore jexec a ping -c 1 192.0.2.2 + + jexec a pfctl -e + pft_set_rules a "pass out on ${l}a route-to (${l}a 192.0.2.2) all" + jexec b timeout 30s nc -4 -d -l 2345 > out & + sleep 1 + atf_check -s exit:0 -o ignore \ + jexec a ${HELPER} -c 192.0.2.2 -p 2345 ${HELPER} 0 512 0 + wait + atf_check_equal $(dd bs=512 count=1 if=${HELPER} | sha256 -q) \ + $(sha256 -q out) +} + +v4_cleanup() { + pft_cleanup + rm -f out +} + +atf_test_case "v6" "cleanup" +v6_head() { + atf_set descr 'unmapped mbuf IPv6 test' + atf_set require.user root +} + +v6_body() { + pft_init + + l=$(vnet_mkepair) + vnet_mkjail a ${l}a + jexec a ifconfig ${l}a inet6 2001:db8::1/64 up + vnet_mkjail b ${l}b + jexec b ifconfig ${l}b inet6 2001:db8::2/64 up + + # Sanity check + atf_check -s exit:0 -o ignore jexec a ping -c 1 2001:db8::2 + + jexec a pfctl -e + pft_set_rules a "pass out on ${l}a route-to (${l}a 2001:db8::2) all" + jexec b timeout 30s nc -6 -d -l 2345 > out & + sleep 1 + atf_check -s exit:0 -o ignore \ + jexec a ${HELPER} -c 2001:db8::2 -p 2345 ${HELPER} 0 512 0 + wait + atf_check_equal $(dd bs=512 count=1 if=${HELPER} | sha256 -q) \ + $(sha256 -q out) +} + +v6_cleanup() { + pft_cleanup + rm -f out +} + +atf_test_case "dummynet" "cleanup" +dummynet_head() { + atf_set descr 'unmapped mbuf dummynet test' + atf_set require.user root +} + +dummynet_body() { + pft_init + + l=$(vnet_mkepair) + vnet_mkjail a ${l}a + jexec a ifconfig ${l}a 192.0.2.1/24 up + vnet_mkjail b ${l}b + jexec b ifconfig ${l}b 192.0.2.2/24 up + + # Sanity check + atf_check -s exit:0 -o ignore jexec a ping -c 1 192.0.2.2 + + atf_check -s exit:0 -o ignore \ + jexec a dnctl pipe 1 config delay 100 + jexec a pfctl -e + pft_set_rules a "pass out on ${l}a route-to (${l}a 192.0.2.2) all dnpipe 1" + jexec b timeout 30s nc -4 -d -l 2345 > out & + sleep 1 + atf_check -s exit:0 -o ignore \ + jexec a ${HELPER} -c 192.0.2.2 -p 2345 ${HELPER} 0 512 0 + wait + atf_check_equal $(dd bs=512 count=1 if=${HELPER} | sha256 -q) \ + $(sha256 -q out) +} + +dummynet_cleanup() { + pft_cleanup + rm -f out +} + +atf_init_test_cases() +{ + atf_add_test_case "v4" + atf_add_test_case "v6" + atf_add_test_case "dummynet" +}
