* [PATCH libmnl v2 0/2] fprintf trailing bytes, errno sign workaround @ 2026-11-21 14:34 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 14:34 ` [PATCH libmnl v2 2/2] callback: remove the errno sign workaround in mnl_cb_error() Florian Stosse 0 siblings, 2 replies; 4+ messages in thread From: Florian Stosse @ 2026-11-21 14:34 UTC (permalink / raw) To: netfilter-devel; +Cc: pablo, Florian Stosse Hi Pablo, Thanks for applying patches 1-3. This v2 reworks the other two patches of the series: https://lore.kernel.org/netfilter-devel/20261106191841.1645-1-florian.stosse@harvester.fr/ Patch 1 makes mnl_nlmsg_fprintf() print the 1 to 3 bytes left at the end of an unaligned message on a last row labelled "trailing bytes", instead of reading past the message. Aligned messages print exactly as before. Patch 2 removes the errno sign workaround in mnl_cb_error(), as you suggested. It removes the branch for positive values and keeps the negation, because the kernel sends a negative errno. So -INT_MIN can still be computed for a hand-built NLMSG_ERROR, although the kernel never sends that value. The note in the patch has the details and a branch-free alternative, in case you want that case gone too. Florian Stosse (2): nlmsg: do not read past nlmsg_len in mnl_nlmsg_fprintf() callback: remove the errno sign workaround in mnl_cb_error() src/callback.c | 6 +----- src/nlmsg.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) -- 2.56.0.windows.4 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH libmnl v2 1/2] nlmsg: do not read past nlmsg_len in mnl_nlmsg_fprintf() 2026-11-21 14:34 [PATCH libmnl v2 0/2] fprintf trailing bytes, errno sign workaround Florian Stosse @ 2026-11-21 14:34 ` Florian Stosse 2026-11-21 23:46 ` Pablo Neira Ayuso 2026-11-21 14:34 ` [PATCH libmnl v2 2/2] callback: remove the errno sign workaround in mnl_cb_error() Florian Stosse 1 sibling, 1 reply; 4+ messages in thread From: Florian Stosse @ 2026-11-21 14:34 UTC (permalink / raw) To: netfilter-devel; +Cc: pablo, Florian Stosse 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) { 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) { + 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 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH libmnl v2 1/2] nlmsg: do not read past nlmsg_len in mnl_nlmsg_fprintf() 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 0 siblings, 0 replies; 4+ messages in thread From: Pablo Neira Ayuso @ 2026-11-21 23:46 UTC (permalink / raw) To: Florian Stosse; +Cc: netfilter-devel 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 > ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH libmnl v2 2/2] callback: remove the errno sign workaround in mnl_cb_error() 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 14:34 ` Florian Stosse 1 sibling, 0 replies; 4+ messages in thread From: Florian Stosse @ 2026-11-21 14:34 UTC (permalink / raw) To: netfilter-devel; +Cc: pablo, Florian Stosse The kernel reports the error of an NLMSG_ERROR message as a negative errno, or 0 for an acknowledgment. mnl_cb_error() also accepts a positive value, a workaround for very old kernels. Remove it and always negate the error. An NLMSG_ERROR with a positive error now sets a negative errno. Assisted-by: LLM Signed-off-by: Florian Stosse <florian.stosse@harvester.fr> --- Notes: v2: drop the INT_MIN check and remove the workaround instead, as suggested. The branch that goes is the one for positive values. The kernel sends a negative errno (netlink(7): "Negative errno or 0 for acknowledgements"), so the negation stays. Two consequences: - -err->error still overflows for an NLMSG_ERROR whose error is INT_MIN. The kernel never sends that value, so only a hand-built message gets there. UBSan reports: "negation of -2147483648 cannot be represented in type 'int'" - A positive error now gives a negative errno. If you prefer no undefined behaviour at all, errno = -(unsigned int)err->error avoids it without a special case. I left it out of the patch. src/callback.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/callback.c b/src/callback.c index 703ae80..6bd8259 100644 --- a/src/callback.c +++ b/src/callback.c @@ -24,11 +24,7 @@ static int mnl_cb_error(const struct nlmsghdr *nlh, void *data) errno = EBADMSG; return MNL_CB_ERROR; } - /* Netlink subsystems returns the errno value with different signess */ - if (err->error < 0) - errno = -err->error; - else - errno = err->error; + errno = -err->error; return err->error == 0 ? MNL_CB_STOP : MNL_CB_ERROR; } -- 2.56.0.windows.4 ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-21 23:46 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 2026-11-21 14:34 ` [PATCH libmnl v2 2/2] callback: remove the errno sign workaround in mnl_cb_error() Florian Stosse
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.