Page MenuHomeFreeBSD

D62405.1796422862.diff
No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None

D62405.1796422862.diff

diff --git a/sys/netpfil/ipfw/ip_fw2.c b/sys/netpfil/ipfw/ip_fw2.c
--- a/sys/netpfil/ipfw/ip_fw2.c
+++ b/sys/netpfil/ipfw/ip_fw2.c
@@ -2213,6 +2213,9 @@
check_uidgid(insntod(cmd, u32),
args, &ucred_lookup,
&ucred_cache);
+ /* Skip packets without a socket */
+ if (ucred_cache == NULL)
+ break;
if (lookup_type == LOOKUP_UID)
key.u32 = ucred_cache->cr_uid;
else if (lookup_type == LOOKUP_JAIL)
diff --git a/tests/sys/netpfil/ipfw/Makefile b/tests/sys/netpfil/ipfw/Makefile
--- a/tests/sys/netpfil/ipfw/Makefile
+++ b/tests/sys/netpfil/ipfw/Makefile
@@ -11,6 +11,7 @@
skipto \
table \
tcpmss \
+ uid \
unmapped \
zero
@@ -18,7 +19,8 @@
rule_check
${PACKAGE}FILES+= fwd_inetd.conf \
- lookup_inetd.conf
+ lookup_inetd.conf \
+ uid_inetd.conf
# Allow tests to run in parallel in their own jails
TEST_METADATA+= execenv="jail"
diff --git a/tests/sys/netpfil/ipfw/lookup.sh b/tests/sys/netpfil/ipfw/lookup.sh
--- a/tests/sys/netpfil/ipfw/lookup.sh
+++ b/tests/sys/netpfil/ipfw/lookup.sh
@@ -408,6 +408,91 @@
atf_check -s exit:1 ${NC} 192.0.2.1 82
}
+atf_test_case "uid" "cleanup"
+uid_head()
+{
+ atf_set descr 'Lookup uid test'
+ atf_set require.user root
+}
+
+uid_body()
+{
+ firewall_init "ipfw"
+
+ epair=$(vnet_mkepair)
+
+ setup_network_v4 ${epair}
+
+ # The connection belongs to inetd, which runs as root.
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add 0" \
+ "ipfw -q add 100 deny tcp from any to any lookup uid 1 out"
+ atf_check -s ignore -o empty -e ignore ${NC} 192.0.2.1 82
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add 4242" \
+ "ipfw -q add 100 deny tcp from any to any lookup uid 1 out"
+ atf_check -o "inline:GOOD 82\n" ${NC} 192.0.2.1 82
+
+ # An echo request has no socket, so it has no uid either.
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add 0" \
+ "ipfw -q add 100 deny icmp from any to any lookup uid 1 in"
+ atf_check -o ignore ping -c 1 -t 1 192.0.2.1
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add 0" \
+ "ipfw -q add 100 deny icmp from any to any not lookup uid 1 in"
+ atf_check -s exit:2 -o ignore ping -c 1 -t 1 192.0.2.1
+}
+
+atf_test_case "jail" "cleanup"
+jail_head()
+{
+ atf_set descr 'Lookup jail test'
+ atf_set require.user root
+}
+
+jail_body()
+{
+ firewall_init "ipfw"
+
+ epair=$(vnet_mkepair)
+
+ setup_network_v6 ${epair}
+
+ jid=$(jls -j alcatraz jid)
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add ${jid}" \
+ "ipfw -q add 100 deny tcp from any to any lookup jail 1 out"
+ atf_check -s ignore -o empty -e ignore ${NC} 2001:db8:42::2 82
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add 0" \
+ "ipfw -q add 100 deny tcp from any to any lookup jail 1 out"
+ atf_check -o "inline:GOOD 82\n" ${NC} 2001:db8:42::2 82
+
+ # An echo request has no socket, so it has no jail either.
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add ${jid}" \
+ "ipfw -q add 100 deny ip6 from any to any lookup jail 1 in"
+ atf_check -o ignore ping6 -c 1 -t 1 2001:db8:42::2
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q table 1 create type number" \
+ "ipfw -q table 1 add ${jid}" \
+ "ipfw -q add 100 deny ip6 from any to any not lookup jail 1 in"
+ atf_check -s exit:2 -o ignore ping6 -c 1 -t 1 2001:db8:42::2
+}
+
lookup_cleanup()
{
@@ -419,6 +504,7 @@
for test in "ip4" "ip6" \
"rulenum" "port" \
"dscp_v4" "dscp_v6" \
+ "uid" "jail" \
; do
atf_add_test_case "${test}"
alias "${test}_cleanup"="lookup_cleanup"
diff --git a/tests/sys/netpfil/ipfw/uid.sh b/tests/sys/netpfil/ipfw/uid.sh
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/ipfw/uid.sh
@@ -0,0 +1,88 @@
+# Copyright (c) 2026 Maxim Konovalov <maxim@FreeBSD.org>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+
+. $(atf_get_srcdir)/../common/utils.subr
+
+NC="nc -w 1 -dnN"
+
+setup_network()
+{
+ epair=$(vnet_mkepair)
+ vnet_mkjail alcatraz ${epair}b
+ atf_check ifconfig ${epair}a 192.0.2.0/31 up
+ atf_check jexec alcatraz ifconfig ${epair}b 192.0.2.1/31 up
+
+ # The sockets inetd accepts keep its credentials, root:wheel,
+ # although echo itself runs as nobody.
+ atf_check jexec alcatraz inetd -p $(pwd)/inetd.pid \
+ $(atf_get_srcdir)/uid_inetd.conf
+ atf_check -o "inline:GOOD 82\n" ${NC} 192.0.2.1 82
+}
+
+atf_test_case "uid" "cleanup"
+uid_head()
+{
+ atf_set descr 'Match packets by the user of their socket'
+ atf_set require.user root
+}
+
+uid_body()
+{
+ firewall_init "ipfw"
+ setup_network
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any uid 0 out"
+ atf_check -s ignore -o empty -e ignore ${NC} 192.0.2.1 82
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any uid root out"
+ atf_check -s ignore -o empty -e ignore ${NC} 192.0.2.1 82
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any uid 4242"
+ atf_check -o "inline:GOOD 82\n" ${NC} 192.0.2.1 82
+}
+
+uid_cleanup()
+{
+ firewall_cleanup "ipfw"
+}
+
+atf_test_case "gid" "cleanup"
+gid_head()
+{
+ atf_set descr 'Match packets by the group of their socket'
+ atf_set require.user root
+}
+
+gid_body()
+{
+ firewall_init "ipfw"
+ setup_network
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any gid 0 out"
+ atf_check -s ignore -o empty -e ignore ${NC} 192.0.2.1 82
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any gid wheel out"
+ atf_check -s ignore -o empty -e ignore ${NC} 192.0.2.1 82
+
+ firewall_config "alcatraz" ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any gid 4242"
+ atf_check -o "inline:GOOD 82\n" ${NC} 192.0.2.1 82
+}
+
+gid_cleanup()
+{
+ firewall_cleanup "ipfw"
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case "gid"
+ atf_add_test_case "uid"
+}
diff --git a/tests/sys/netpfil/ipfw/uid_inetd.conf b/tests/sys/netpfil/ipfw/uid_inetd.conf
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/ipfw/uid_inetd.conf
@@ -0,0 +1 @@
+xfer stream tcp46 nowait nobody /bin/echo echo GOOD 82

File Metadata

Mime Type
text/plain
Expires
Fri, Dec 4, 10:21 PM (4 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42213392
Default Alt Text
D62405.1796422862.diff (6 KB)

Event Timeline