git: 41ce9c8b8891 - main - netlink: restore original buffer if nlmsgs_to_linux() fails

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 27 Feb 2024 20:47:52 UTC
The branch main has been updated by glebius:

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

commit 41ce9c8b8891eee82b2824c733f103e6671b5731
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-02-27 20:45:54 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-02-27 20:45:54 +0000

    netlink: restore original buffer if nlmsgs_to_linux() fails
    
    Caller is responsible to free it or reuse.
    
    Fixes:  17083b94a91563aba15ba03d1c74796a35bb1c26
---
 sys/compat/linux/linux_netlink.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sys/compat/linux/linux_netlink.c b/sys/compat/linux/linux_netlink.c
index af172fb27ba7..d2afec24fe71 100644
--- a/sys/compat/linux/linux_netlink.c
+++ b/sys/compat/linux/linux_netlink.c
@@ -548,7 +548,7 @@ static bool
 nlmsgs_to_linux(struct nl_writer *nw, struct nlpcb *nlp)
 {
 	struct nl_buf *nb, *orig;
-	u_int offset, msglen, orig_messages __diagused;
+	u_int offset, msglen, orig_messages;
 
 	RT_LOG(LOG_DEBUG3, "%p: in %u bytes %u messages", __func__,
 	    nw->buf->datalen, nw->num_messages);
@@ -558,9 +558,7 @@ nlmsgs_to_linux(struct nl_writer *nw, struct nlpcb *nlp)
 	if (__predict_false(nb == NULL))
 		return (false);
 	nw->buf = nb;
-#ifdef INVARIANTS
 	orig_messages = nw->num_messages;
-#endif
 	nw->num_messages = 0;
 
 	/* Assume correct headers. Buffer IS mutable */
@@ -574,6 +572,8 @@ nlmsgs_to_linux(struct nl_writer *nw, struct nlpcb *nlp)
 			RT_LOG(LOG_DEBUG, "failed to process msg type %d",
 			    hdr->nlmsg_type);
 			nl_buf_free(nb);
+			nw->buf = orig;
+			nw->num_messages = orig_messages;
 			return (false);
 		}
 	}