Page MenuHomeFreeBSD

D62033.1795669423.diff
No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None

D62033.1795669423.diff

diff --git a/sbin/ipfw/ipfw.8 b/sbin/ipfw/ipfw.8
--- a/sbin/ipfw/ipfw.8
+++ b/sbin/ipfw/ipfw.8
@@ -2577,6 +2577,11 @@
.Cm skipto
action, the user should be aware that the code will walk the ruleset
up to a rule equal to, or past, the given number.
+With the
+.Cm skipto
+and
+.Cm call
+actions, a table value above 65535 is treated as 65535.
.Pp
See the
.Sx EXAMPLES
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
@@ -1213,7 +1213,7 @@
jump_lookup_pos(struct ip_fw_chain *chain, struct ip_fw *f, uint32_t num,
int tablearg, bool jump_backwards)
{
- int f_pos, i;
+ uint32_t f_pos, i;
/*
* Make sure we do not jump backward.
@@ -1222,16 +1222,18 @@
if (!jump_backwards && i <= f->rulenum)
i = f->rulenum + 1;
+ /*
+ * Table values are 32-bit and not range checked, so treat any
+ * target past the last rule number as the last rule number.
+ * This also keeps the skipto cache lookup in bounds.
+ */
+ if (i > IPFW_DEFAULT_RULE)
+ i = IPFW_DEFAULT_RULE;
+
if (V_skipto_cache == 0)
f_pos = ipfw_find_rule(chain, i, 0);
- else {
- /*
- * Make sure we do not do out of bounds access.
- */
- if (i >= IPFW_DEFAULT_RULE)
- i = IPFW_DEFAULT_RULE - 1;
+ else
f_pos = chain->idxmap[i];
- }
return (f_pos);
}
@@ -1240,7 +1242,7 @@
jump(struct ip_fw_chain *chain, struct ip_fw *f, uint32_t num,
int tablearg, bool jump_backwards)
{
- int f_pos;
+ uint32_t f_pos;
/* Can't use cache with IP_FW_TARG */
if (num == IP_FW_TARG)
@@ -3164,9 +3166,11 @@
* Skip disabled rules, and re-enter
* the inner loop with the correct
* f_pos, f, l and cmd.
- * Also clear cmdlen and skip_or
+ * Also clear cmdlen and skip_or.
+ * Like skipto, the jump may land on the
+ * default rule.
*/
- MPASS(f_pos < chain->n_rules - 1);
+ MPASS(f_pos < chain->n_rules);
for (; f_pos < chain->n_rules - 1 &&
(V_set_disable &
(1 << chain->map[f_pos]->set)); f_pos++)
diff --git a/sys/netpfil/ipfw/ip_fw_sockopt.c b/sys/netpfil/ipfw/ip_fw_sockopt.c
--- a/sys/netpfil/ipfw/ip_fw_sockopt.c
+++ b/sys/netpfil/ipfw/ip_fw_sockopt.c
@@ -1561,6 +1561,14 @@
if (CHECK_TARG(insntoc(cmd, u32)->d[0], ci))
goto bad_targ;
}
+ /* skipto and call take a rule number, return does not. */
+ if (cmd->opcode != O_SETMARK &&
+ (cmd->opcode != O_CALLRETURN || (cmd->len & F_NOT) == 0) &&
+ insntoc(cmd, u32)->d[0] > IPFW_DEFAULT_RULE) {
+ printf("ipfw: opcode %d: invalid rule number %u\n",
+ cmd->opcode, insntoc(cmd, u32)->d[0]);
+ return (FAILED);
+ }
return (CHECK_ACTION);
case O_CHECK_STATE:
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
@@ -8,10 +8,12 @@
log \
lookup \
nat \
+ skipto \
table \
unmapped
-ATF_TESTS_C+= nat_getlog
+ATF_TESTS_C+= nat_getlog \
+ rule_check
${PACKAGE}FILES+= fwd_inetd.conf \
lookup_inetd.conf
diff --git a/tests/sys/netpfil/ipfw/rule_check.c b/tests/sys/netpfil/ipfw/rule_check.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/ipfw/rule_check.c
@@ -0,0 +1,200 @@
+/*
+ * Copyright (c) 2026 Maxim Konovalov <maxim@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * Tests for the checks IP_FW_XADD applies to new rules. The rules are
+ * built from raw instructions, since ipfw(8) never generates most of
+ * them.
+ */
+
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/ip_fw.h>
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#define RULENUM 100
+
+/* Returns an instruction header as a 32-bit command word. */
+static uint32_t
+insn(int opcode, int len, int arg1)
+{
+ ipfw_insn cmd;
+ uint32_t w;
+
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.opcode = opcode;
+ cmd.len = len;
+ cmd.arg1 = arg1;
+ memcpy(&w, &cmd, sizeof(w));
+ return (w);
+}
+
+static void
+del_rule(int s)
+{
+ ipfw_range_header rh;
+
+ memset(&rh, 0, sizeof(rh));
+ rh.opheader.opcode = IP_FW_XDEL;
+ rh.opheader.version = IP_FW3_OPVER;
+ rh.range.head.type = IPFW_TLV_RANGE;
+ rh.range.head.length = sizeof(rh.range);
+ rh.range.flags = IPFW_RCFLAG_RANGE;
+ rh.range.start_rule = RULENUM;
+ rh.range.end_rule = RULENUM;
+ ATF_CHECK_MSG(setsockopt(s, IPPROTO_IP, IP_FW3, &rh, sizeof(rh)) == 0,
+ "IP_FW_XDEL: %s", strerror(errno));
+}
+
+/*
+ * Adds rule RULENUM made of the ncmd command words in cmd, with the
+ * action at act_ofs. An accepted rule is deleted again. Returns 0 or
+ * the errno value from IP_FW_XADD.
+ */
+static int
+add_rule(uint16_t act_ofs, const uint32_t *cmd, size_t ncmd)
+{
+ ip_fw3_opheader *op3;
+ ipfw_obj_ctlv *ctlv;
+ struct ip_fw_rule *r;
+ size_t len, rlen;
+ char *buf;
+ int error, s;
+
+ rlen = roundup2(sizeof(*r) + (ncmd - 1) * sizeof(uint32_t),
+ sizeof(uint64_t));
+ len = sizeof(*op3) + sizeof(*ctlv) + rlen;
+ buf = calloc(1, len);
+ ATF_REQUIRE(buf != NULL);
+
+ op3 = (ip_fw3_opheader *)(void *)buf;
+ op3->opcode = IP_FW_XADD;
+ op3->version = IP_FW3_OPVER;
+
+ ctlv = (ipfw_obj_ctlv *)(void *)(op3 + 1);
+ ctlv->head.type = IPFW_TLV_RULE_LIST;
+ ctlv->head.length = sizeof(*ctlv) + rlen;
+ ctlv->count = 1;
+ r = (struct ip_fw_rule *)(void *)(ctlv + 1);
+ r->rulenum = RULENUM;
+ r->act_ofs = act_ofs;
+ r->cmd_len = ncmd;
+ memcpy((char *)r + offsetof(struct ip_fw_rule, cmd), cmd,
+ ncmd * sizeof(uint32_t));
+
+ s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+ ATF_REQUIRE_MSG(s >= 0, "socket: %s", strerror(errno));
+ error = 0;
+ if (setsockopt(s, IPPROTO_IP, IP_FW3, buf, len) != 0)
+ error = errno;
+ else
+ del_rule(s);
+ close(s);
+ free(buf);
+ return (error);
+}
+
+#define CHECK_ADD(expect, act_ofs, cmd) do { \
+ int _error = add_rule((act_ofs), (cmd), nitems(cmd)); \
+ ATF_CHECK_EQ_MSG((expect), _error, "expected %d, got %d", \
+ (expect), _error); \
+} while (0)
+
+ATF_TC(skipto_range);
+ATF_TC_HEAD(skipto_range, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "skipto targets above the default rule number are rejected");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "require.kmods", "ipfw");
+}
+ATF_TC_BODY(skipto_range, tc)
+{
+ uint32_t cmd[2];
+
+ cmd[0] = insn(O_SKIPTO, F_INSN_SIZE(ipfw_insn_u32), 0);
+ cmd[1] = IPFW_DEFAULT_RULE;
+ CHECK_ADD(0, 0, cmd);
+ cmd[1] = IPFW_DEFAULT_RULE + 1;
+ CHECK_ADD(EINVAL, 0, cmd);
+ cmd[1] = INT32_MAX + 1U;
+ CHECK_ADD(EINVAL, 0, cmd);
+ cmd[1] = UINT32_MAX;
+ CHECK_ADD(EINVAL, 0, cmd);
+
+ /* F_NOT does not exempt skipto, only "return". */
+ cmd[0] = insn(O_SKIPTO, F_INSN_SIZE(ipfw_insn_u32) | F_NOT, 0);
+ cmd[1] = UINT32_MAX;
+ CHECK_ADD(EINVAL, 0, cmd);
+}
+
+ATF_TC(call_range);
+ATF_TC_HEAD(call_range, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "call targets above the default rule number are rejected");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "require.kmods", "ipfw");
+}
+ATF_TC_BODY(call_range, tc)
+{
+ uint32_t cmd[2];
+
+ cmd[0] = insn(O_CALLRETURN, F_INSN_SIZE(ipfw_insn_u32), 0);
+ cmd[1] = IPFW_DEFAULT_RULE;
+ CHECK_ADD(0, 0, cmd);
+ cmd[1] = IPFW_DEFAULT_RULE + 1;
+ CHECK_ADD(EINVAL, 0, cmd);
+ cmd[1] = INT32_MAX + 1U;
+ CHECK_ADD(EINVAL, 0, cmd);
+ cmd[1] = UINT32_MAX;
+ CHECK_ADD(EINVAL, 0, cmd);
+
+ /* "return" carries no rule number, so its value is not checked. */
+ cmd[0] = insn(O_CALLRETURN, F_INSN_SIZE(ipfw_insn_u32) | F_NOT,
+ RETURN_NEXT_RULENUM);
+ cmd[1] = UINT32_MAX;
+ CHECK_ADD(0, 0, cmd);
+}
+
+ATF_TC(setmark_value);
+ATF_TC_HEAD(setmark_value, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "setmark takes any 32-bit value, not a rule number");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "require.kmods", "ipfw");
+}
+ATF_TC_BODY(setmark_value, tc)
+{
+ uint32_t cmd[2];
+
+ cmd[0] = insn(O_SETMARK, F_INSN_SIZE(ipfw_insn_u32), 0);
+ cmd[1] = IPFW_DEFAULT_RULE + 1;
+ CHECK_ADD(0, 0, cmd);
+ cmd[1] = UINT32_MAX;
+ CHECK_ADD(0, 0, cmd);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, skipto_range);
+ ATF_TP_ADD_TC(tp, call_range);
+ ATF_TP_ADD_TC(tp, setmark_value);
+
+ return (atf_no_error());
+}
diff --git a/tests/sys/netpfil/ipfw/skipto.sh b/tests/sys/netpfil/ipfw/skipto.sh
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/ipfw/skipto.sh
@@ -0,0 +1,92 @@
+# Copyright (c) 2026 Maxim Konovalov <maxim@FreeBSD.org>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+
+. $(atf_get_srcdir)/../common/utils.subr
+
+# Table values are 32-bit, so a "skipto tablearg" or "call tablearg"
+# target can be far past the last rule number. Such a jump must behave
+# like a jump to the end of the ruleset, with and without the skipto
+# cache. Each entry is a target and the number of packets rule 200
+# must count: 150 is in range and lands on rule 200, the others skip it.
+TARGETS="150:1 65535:0 70000:0 2147483648:0 4294967295:0"
+
+jump_setup()
+{
+ firewall_init "ipfw"
+
+ epair=$(vnet_mkepair)
+ vnet_mkjail alcatraz ${epair}b
+ ifconfig ${epair}a 192.0.2.0/31 up
+ jexec alcatraz ifconfig ${epair}b 192.0.2.1/31 up
+
+ jexec alcatraz ipfw -q table 1 create type addr valtype skipto
+ jexec alcatraz ipfw -q add 100 $1 tablearg ip from 'table(1)' to any in
+ jexec alcatraz ipfw -q add 200 count ip from 192.0.2.0 to any in
+}
+
+# For every target and skipto cache state, one echo request must hit
+# rule 100, reach rule 200 only if the target is in range, and get
+# through on the default rule.
+jump_check()
+{
+ for entry in ${TARGETS}; do
+ target=${entry%:*}
+ hits=${entry#*:}
+ atf_check jexec alcatraz ipfw -q table 1 flush
+ atf_check -o ignore \
+ jexec alcatraz ipfw table 1 add 192.0.2.0/32 ${target}
+ for cache in disable enable; do
+ atf_check jexec alcatraz ipfw -q ${cache} skipto_cache
+ atf_check jexec alcatraz ipfw -q zero
+ atf_check -o ignore ping -c 1 -t 2 192.0.2.1
+ atf_check -o match:"^00100 +1 " \
+ jexec alcatraz ipfw show 100
+ atf_check -o match:"^00200 +${hits} " \
+ jexec alcatraz ipfw show 200
+ done
+ done
+}
+
+atf_test_case "skipto_tablearg_range" "cleanup"
+skipto_tablearg_range_head()
+{
+ atf_set descr 'skipto tablearg to a rule and past the last rule'
+ atf_set require.user root
+}
+
+skipto_tablearg_range_body()
+{
+ jump_setup skipto
+ jump_check
+}
+
+skipto_tablearg_range_cleanup()
+{
+ firewall_cleanup "ipfw"
+}
+
+atf_test_case "call_tablearg_range" "cleanup"
+call_tablearg_range_head()
+{
+ atf_set descr 'call tablearg to a rule and past the last rule'
+ atf_set require.user root
+}
+
+call_tablearg_range_body()
+{
+ jump_setup call
+ jump_check
+}
+
+call_tablearg_range_cleanup()
+{
+ firewall_cleanup "ipfw"
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case "skipto_tablearg_range"
+ atf_add_test_case "call_tablearg_range"
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 26, 5:03 AM (22 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42006486
Default Alt Text
D62033.1795669423.diff (10 KB)

Event Timeline