ipfw_log_rtsock() puts the rule comment after the log header in the
AF_IPFWLOG sockaddr_dl, so the sockaddr takes 64 bytes plus the
comment. rtsock_msg_mbuf() copies every sockaddr into a struct
sockaddr_storage on the stack, which holds 128 bytes. A comment of 64
characters or more, and ipfw(8) accepts up to 80, overflowed that
buffer whenever a matching packet was logged while a route socket was
open. INVARIANTS kernels stop at a KASSERT, kernels built with the
stack protector panic when rtsock_msg_mbuf() returns.
The comment was also read with strnlen() and strlcpy() without regard
to the length of the O_NOP instruction holding it. Nothing requires
that comment to be NUL-terminated, so the copy could run on into the
following instructions.
Copy no more than the O_NOP payload and no more than 63 characters,
so that the sockaddr always fits a sockaddr_storage. The limit is
IPFWLOG_RTSOCK_COMMENT_MAX in ip_fw.h, next to the message header,
and ipfw.8 documents it.
"ipfw internal monitor", the only reader of these messages in the
tree, compared its filter with up to 80 characters of the comment, so
a filter naming a longer comment would no longer match. Compare only
as many characters as a message can carry. Also take the length of
the comment from the sockaddr instead of relying on a NUL inside it.
New test:
log:rtsock_comment logs rules with 1, 63, 64 and 79 character
comments to the route socket and checks what
"ipfw internal monitor" receives without a
filter, with the full comment as the filter,
and with a filter that must not matchWithout the fix the test crashes the kernel at 64 characters. With
only the kernel part, the filtered monitor misses the 64 character
comment.
Fixes: 4a77657cbc01 ("ipfw: migrate ipfw to 32-bit size rule numbers")
MFC after: 3 days