Page MenuHomeFreeBSD

D61916.1795425412.diff
No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None

D61916.1795425412.diff

diff --git a/sbin/ipfw/ipfw.8 b/sbin/ipfw/ipfw.8
--- a/sbin/ipfw/ipfw.8
+++ b/sbin/ipfw/ipfw.8
@@ -772,6 +772,7 @@
Logs a packet to the
.Xr route 4
socket.
+The message carries at most the first 63 characters of the rule comment.
See the comments of
.Fn ipfw_log_rtsock
in ipfw source code for more
@@ -4411,6 +4412,10 @@
.Ar filter-comment
can be specified to show only those messages, that were logged
by rules with specific rule comment.
+Messages carry at most the first 63 characters of the rule comment,
+so only the first 63 characters of a longer
+.Ar filter-comment
+are compared.
.It Cm talist
List all table lookup algorithms currently available.
.El
diff --git a/sbin/ipfw/ipfw2.c b/sbin/ipfw/ipfw2.c
--- a/sbin/ipfw/ipfw2.c
+++ b/sbin/ipfw/ipfw2.c
@@ -6177,7 +6177,7 @@
struct sockaddr_dl *sdl;
ipfwlog_rtsock_hdr_v2 *loghdr;
ssize_t nread;
- size_t msglen;
+ size_t clen, msglen, off;
int rtsock;
rtsock = socket(PF_ROUTE, SOCK_RAW, AF_IPFWLOG);
@@ -6212,9 +6212,21 @@
msglen -= SA_SIZE(sdl);
loghdr = (ipfwlog_rtsock_hdr_v2 *)sdl->sdl_data;
- /* filter by rule comment. MAX_COMMENT_LEN = 80 */
+ /*
+ * The comment ends with the sockaddr and takes at most
+ * sdl_nlen bytes. The kernel cuts it to
+ * IPFWLOG_RTSOCK_COMMENT_MAX - 1 characters, so a comment
+ * of that length matches any filter that starts with it.
+ */
+ off = offsetof(struct sockaddr_dl, sdl_data) + sizeof(*loghdr);
+ clen = 0;
+ if (sdl->sdl_len > off)
+ clen = strnlen(loghdr->comment,
+ MIN(sdl->sdl_nlen, sdl->sdl_len - off));
if (filter != NULL &&
- strncmp(filter, loghdr->comment, 80) != 0)
+ (strncmp(filter, loghdr->comment, clen) != 0 ||
+ (filter[clen] != '\0' &&
+ clen < IPFWLOG_RTSOCK_COMMENT_MAX - 1)))
continue;
sa = (struct sockaddr *)((char *)sdl + SA_SIZE(sdl));
@@ -6257,8 +6269,8 @@
bprintf(&bp, ", nh ");
bprint_sa(&bp, sa);
}
- if (sdl->sdl_nlen > 0)
- bprintf(&bp, " // %s", loghdr->comment);
+ if (clen > 0)
+ bprintf(&bp, " // %.*s", (int)clen, loghdr->comment);
printf("%s\n", bp.buf);
}
bp_free(&bp);
diff --git a/sys/netinet/ip_fw.h b/sys/netinet/ip_fw.h
--- a/sys/netinet/ip_fw.h
+++ b/sys/netinet/ip_fw.h
@@ -564,6 +564,13 @@
char comment[0];
} ipfwlog_rtsock_hdr_v2;
+/*
+ * The comment follows the header in an AF_IPFWLOG sockaddr_dl, which
+ * must fit a struct sockaddr_storage. This is the most it takes in a
+ * message, terminating NUL included; longer rule comments are cut.
+ */
+#define IPFWLOG_RTSOCK_COMMENT_MAX 64
+
/* Legacy NAT structures, compat only */
#ifndef _KERNEL
/*
diff --git a/sys/netpfil/ipfw/ip_fw_log.c b/sys/netpfil/ipfw/ip_fw_log.c
--- a/sys/netpfil/ipfw/ip_fw_log.c
+++ b/sys/netpfil/ipfw/ip_fw_log.c
@@ -549,20 +549,32 @@
return (NULL);
}
-#define MAX_COMMENT_LEN 80
+/*
+ * The rule comment follows the header in the AF_IPFWLOG sockaddr_dl,
+ * and rtsock_msg_mbuf() handles sockaddrs of up to sizeof(struct
+ * sockaddr_storage) bytes.
+ */
+CTASSERT(offsetof(struct sockaddr_dl, sdl_data) +
+ sizeof(ipfwlog_rtsock_hdr_v2) + IPFWLOG_RTSOCK_COMMENT_MAX <=
+ sizeof(struct sockaddr_storage));
+/*
+ * Copies the comment of rule f into dst, truncated to size - 1
+ * characters, and returns its length including the NUL, or 0 if the
+ * rule has no comment.
+ */
static size_t
-ipfw_copy_rule_comment(struct ip_fw *f, char *dst)
+ipfw_copy_rule_comment(struct ip_fw *f, char *dst, size_t size)
{
ipfw_insn *cmd;
- size_t rcomment_len = 0;
+ size_t len;
int l, cmdlen;
for (l = f->cmd_len, cmd = f->cmd; l > 0; l -= cmdlen, cmd += cmdlen) {
cmdlen = F_LEN(cmd);
if (cmd->opcode != O_NOP) {
continue;
- } else if (cmd->len == 1) {
+ } else if (cmdlen == 1) {
return (0);
}
break;
@@ -570,9 +582,12 @@
if (l <= 0) {
return (0);
}
- rcomment_len = strnlen((char *)(cmd + 1), MAX_COMMENT_LEN - 1) + 1;
- strlcpy(dst, (char *)(cmd + 1), rcomment_len);
- return (rcomment_len);
+ /* Nothing requires the comment to be NUL-terminated. */
+ len = strnlen((char *)(cmd + 1),
+ MIN((cmdlen - 1) * sizeof(uint32_t), size - 1));
+ memcpy(dst, cmd + 1, len);
+ dst[len] = '\0';
+ return (len + 1);
}
/*
@@ -620,7 +635,7 @@
ipfw_insn *cmd;
ipfw_insn_log *l;
char *buf, *orig_buf;
- /* at least 4 x sizeof(struct sockaddr_dl) + rule comment (80) */
+ /* rt_addrinfo, the sockaddr_dl with the comment, and 3 sockaddrs */
size_t buflen = 512;
/* Should we log? O_LOG is the first one */
@@ -655,7 +670,8 @@
sdl_ipfwcmd->sdl_alen = sizeof(*hdr);
hdr = (ipfwlog_rtsock_hdr_v2 *)(sdl_ipfwcmd->sdl_data);
/* fill rule comment in if any */
- sdl_ipfwcmd->sdl_nlen = ipfw_copy_rule_comment(f, hdr->comment);
+ sdl_ipfwcmd->sdl_nlen = ipfw_copy_rule_comment(f, hdr->comment,
+ IPFWLOG_RTSOCK_COMMENT_MAX);
targ_value = &hdr->tablearg;
hdr->rulenum = f->rulenum;
hdr->mark = args->rule.pkt_mark;
diff --git a/tests/sys/netpfil/ipfw/log.sh b/tests/sys/netpfil/ipfw/log.sh
--- a/tests/sys/netpfil/ipfw/log.sh
+++ b/tests/sys/netpfil/ipfw/log.sh
@@ -92,7 +92,74 @@
firewall_cleanup $1
}
+atf_test_case "rtsock_comment" "cleanup"
+rtsock_comment_head()
+{
+ atf_set descr 'Logs rules with long comments to the route socket'
+ atf_set require.user root
+}
+
+rtsock_comment_body()
+{
+ 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
+
+ # The comment travels in a sockaddr limited to 128 bytes, which
+ # leaves room for 63 characters. Longer ones used to overflow
+ # the kernel's sockaddr buffer.
+ for len in 1 63 64 79; do
+ comment=$(jot -s "" -b c ${len})
+ expect=$(jot -s "" -b c $((len > 63 ? 63 : len)))
+ # A filter must not match a different comment, nor a
+ # shorter one, unless it was cut to 63 characters.
+ if [ ${len} -lt 63 ]; then
+ other="${comment}d"
+ else
+ other="d${comment}"
+ fi
+
+ atf_check jexec alcatraz ipfw -q -f flush
+ atf_check jexec alcatraz ipfw -q add 100 count log \
+ logamount 100 logdst rtsock ip from 192.0.2.0 to any in \
+ // ${comment}
+
+ # Messages are only generated while somebody listens.
+ jexec alcatraz stdbuf -o L ipfw internal monitor \
+ > all.out &
+ pids=$!
+ jexec alcatraz stdbuf -o L ipfw internal monitor \
+ "${comment}" > match.out &
+ pids="${pids} $!"
+ jexec alcatraz stdbuf -o L ipfw internal monitor \
+ "${other}" > other.out &
+ pids="${pids} $!"
+ for i in $(jot 10); do
+ ping -c 1 -t 1 192.0.2.1 > /dev/null
+ [ -s all.out ] && [ -s match.out ] && break
+ sleep 0.5
+ done
+ # Give the last monitor time to print what it should not.
+ sleep 0.5
+ kill ${pids}
+ wait ${pids}
+
+ atf_check -o match:" // ${expect}\$" head -n 1 all.out
+ atf_check -o match:" // ${expect}\$" head -n 1 match.out
+ atf_check -o empty cat other.out
+ done
+}
+
+rtsock_comment_cleanup()
+{
+ firewall_cleanup "ipfw"
+}
+
atf_init_test_cases()
{
atf_add_test_case "bpf"
+ atf_add_test_case "rtsock_comment"
}

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 23, 9:16 AM (23 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41924422
Default Alt Text
D61916.1795425412.diff (6 KB)

Event Timeline