Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Jan 2024 04:52:12 GMT
From:      Gleb Smirnoff <glebius@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: e6f4c3146065 - main - netlink: improve edge case when reading out truncated last nlmsg in nb
Message-ID:  <202401110452.40B4qCTq041569@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=e6f4c31460658697827aed7f29ec6e960d6f0a87

commit e6f4c31460658697827aed7f29ec6e960d6f0a87
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-01-11 04:51:53 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-01-11 04:51:53 +0000

    netlink: improve edge case when reading out truncated last nlmsg in nb
    
    When there is not enough space for one full message we return it truncated.
    This enters special block of code that previously may leave empty buffer
    with offset == datalen in the queue.  Avoid that, as dealing later with
    empty buffers causes more pain than just avoiding them.  While here add
    missing msgrcv increment.
---
 sys/netlink/netlink_domain.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/sys/netlink/netlink_domain.c b/sys/netlink/netlink_domain.c
index 4790845d1d31..94989af73dfe 100644
--- a/sys/netlink/netlink_domain.c
+++ b/sys/netlink/netlink_domain.c
@@ -762,11 +762,23 @@ nl_soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
 				} else if (len == 0 && uio->uio_resid > 0) {
 					flags |= MSG_TRUNC;
 					partlen = uio->uio_resid;
-					if (!peek) {
-						/* XXX: may leave empty nb */
+					if (peek)
+						goto nospace;
+					datalen += hdr->nlmsg_len;
+					if (nb->offset + hdr->nlmsg_len ==
+					    nb->datalen) {
+						/*
+						 * Avoid leaving empty nb.
+						 * Process last nb normally.
+						 * Trust uiomove() to care
+						 * about negative uio_resid.
+						 */
+						nb = TAILQ_NEXT(nb, tailq);
+						overflow = 0;
+						partlen = 0;
+					} else
 						nb->offset += hdr->nlmsg_len;
-						datalen += hdr->nlmsg_len;
-					}
+					msgrcv++;
 				} else
 					partlen = 0;
 				goto nospace;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202401110452.40B4qCTq041569>