From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Florian Stosse <florian.stosse@harvester.fr>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH libmnl v2 1/2] nlmsg: do not read past nlmsg_len in mnl_nlmsg_fprintf()
Date: Sun, 22 Nov 2026 00:46:20 +0100 [thread overview]
Message-ID: <awItTN9gNPqGsf_4@chamomile> (raw)
In-Reply-To: <20261121143407.19426-2-florian.stosse@harvester.fr>
On Sat, Nov 21, 2026 at 03:34:06PM +0100, Florian Stosse wrote:
> mnl_nlmsg_fprintf_payload() prints the message in 4-byte words while
> i < nlmsg_len. mnl_nlmsg_ok() does not require nlmsg_len to be a multiple
> of 4, so for an unaligned length the last word reads up to 3 bytes past
> the end of the message, and past the buffer when it ends there.
>
> Only print complete words in the loop, then print the 1 to 3 bytes left
> over on a last row labelled "trailing bytes", so that the dump shows
> them:
>
> | 42 42 42 | | trailing bytes |
>
> Assisted-by: LLM
> Signed-off-by: Florian Stosse <florian.stosse@harvester.fr>
> ---
>
> Notes:
> v2: print the 1 to 3 bytes left over on a last row labelled
> "trailing bytes" instead of skipping them, as suggested. Aligned messages
> print exactly as before.
>
> src/nlmsg.c | 16 +++++++++++++++-
> 1 file changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/src/nlmsg.c b/src/nlmsg.c
> index f8eed79..0db0fa7 100644
> --- a/src/nlmsg.c
> +++ b/src/nlmsg.c
> @@ -302,7 +302,7 @@ static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh,
> if (fdnum != -1)
> colorize = isatty(fdnum);
>
> - for (i=sizeof(struct nlmsghdr); i<nlh->nlmsg_len; i+=4) {
> + for (i=sizeof(struct nlmsghdr); i + 4 <= nlh->nlmsg_len; i+=4) {
Probably a good chance to add spaces here too:
+ for (i = sizeof(struct nlmsghdr); i + 4 <= nlh->nlmsg_len; i += 4) {
^ ^ ^
> char *b = (char *) nlh;
> struct nlattr *attr = (struct nlattr *) (b+i);
>
> @@ -346,6 +346,20 @@ static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh,
> isprint(b[i+3]) ? b[i+3] : ' ');
> }
> }
> + /* nlmsg_len is not a multiple of 4: print the bytes left over. */
> + if (i < nlh->nlmsg_len) {
Could you use unlikely here that I introduced in patch:
[PATCH libmnl] include: add unlikely() notation and use it
ie.
+ if (unlikely(i < nlh->nlmsg_len)) {
So there is no need to add a comment and git annotate will help
whoever looks at this line.
Thanks.
> + const char *b = (const char *) nlh;
> + unsigned int j;
> +
> + fprintf(fd, "|");
> + for (j = i; j < i + 4; j++) {
> + if (j < nlh->nlmsg_len)
> + fprintf(fd, " %.2x", 0xff & b[j]);
> + else
> + fprintf(fd, " ");
> + }
> + fprintf(fd, " |\t| trailing bytes |\n");
> + }
> fprintf(fd, "----------------\t------------------\n");
> }
>
> --
> 2.56.0.windows.4
>
next prev parent reply other threads:[~2026-11-21 23:46 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 14:34 [PATCH libmnl v2 0/2] fprintf trailing bytes, errno sign workaround Florian Stosse
2026-11-21 14:34 ` [PATCH libmnl v2 1/2] nlmsg: do not read past nlmsg_len in mnl_nlmsg_fprintf() Florian Stosse
2026-11-21 23:46 ` Pablo Neira Ayuso [this message]
2026-11-21 14:34 ` [PATCH libmnl v2 2/2] callback: remove the errno sign workaround in mnl_cb_error() Florian Stosse
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awItTN9gNPqGsf_4@chamomile \
--to=pablo@netfilter.org \
--cc=florian.stosse@harvester.fr \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.