git: f1c6edba885f - main - netlink: use size_t through the allocation KPI

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 03 Dec 2024 20:04:59 UTC
The branch main has been updated by glebius:

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

commit f1c6edba885fa53cc87f7252ab09d36ec1984697
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-12-03 20:04:27 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-12-03 20:04:27 +0000

    netlink: use size_t through the allocation KPI
    
    This fixes some signedness bugs and potential underflows.  The length of
    nl_buf is still limited by UINT_MAX and this is asserted now.
    
    Reviewed by:            melifaro
    Differential Revision:  https://reviews.freebsd.org/D47551
---
 sys/netlink/ktest_netlink_message_writer.h |  4 ++--
 sys/netlink/netlink_domain.c               |  2 +-
 sys/netlink/netlink_glue.c                 |  5 +++--
 sys/netlink/netlink_message_writer.c       | 14 +++++++-------
 sys/netlink/netlink_message_writer.h       | 14 +++++++-------
 sys/netlink/netlink_var.h                  |  2 +-
 6 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/sys/netlink/ktest_netlink_message_writer.h b/sys/netlink/ktest_netlink_message_writer.h
index 39d2c5e597d6..447593e0e700 100644
--- a/sys/netlink/ktest_netlink_message_writer.h
+++ b/sys/netlink/ktest_netlink_message_writer.h
@@ -30,12 +30,12 @@
 
 #if defined(_KERNEL) && defined(INVARIANTS)
 
-bool nlmsg_get_buf_wrapper(struct nl_writer *nw, u_int size, bool waitok);
+bool nlmsg_get_buf_wrapper(struct nl_writer *nw, size_t size, bool waitok);
 
 #ifndef KTEST_CALLER
 
 bool
-nlmsg_get_buf_wrapper(struct nl_writer *nw, u_int size, bool waitok)
+nlmsg_get_buf_wrapper(struct nl_writer *nw, size_t size, bool waitok)
 {
 	return (nlmsg_get_buf(nw, size, waitok));
 }
diff --git a/sys/netlink/netlink_domain.c b/sys/netlink/netlink_domain.c
index 67ca16d59009..922da32bfb6d 100644
--- a/sys/netlink/netlink_domain.c
+++ b/sys/netlink/netlink_domain.c
@@ -566,7 +566,7 @@ nl_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
 	struct nlpcb *nlp = sotonlpcb(so);
 	struct sockbuf *sb = &so->so_snd;
 	struct nl_buf *nb;
-	u_int len;
+	size_t len;
 	int error;
 
 	MPASS(m == NULL && uio != NULL);
diff --git a/sys/netlink/netlink_glue.c b/sys/netlink/netlink_glue.c
index efb99a717b89..e550a6013654 100644
--- a/sys/netlink/netlink_glue.c
+++ b/sys/netlink/netlink_glue.c
@@ -135,7 +135,8 @@ nlmsg_ignore_limit_stub(struct nl_writer *nw __unused)
 }
 
 static bool
-nlmsg_refill_buffer_stub(struct nl_writer *nw __unused, int required_len __unused)
+nlmsg_refill_buffer_stub(struct nl_writer *nw __unused,
+    size_t required_len __unused)
 {
 	return (false);
 }
@@ -237,7 +238,7 @@ void nlmsg_ignore_limit(struct nl_writer *nw)
 }
 
 bool
-nlmsg_refill_buffer(struct nl_writer *nw, int required_len)
+nlmsg_refill_buffer(struct nl_writer *nw, size_t required_len)
 {
 	return (_nl->nlmsg_refill_buffer(nw, required_len));
 }
diff --git a/sys/netlink/netlink_message_writer.c b/sys/netlink/netlink_message_writer.c
index ee1dab42da74..092e3798f8e7 100644
--- a/sys/netlink/netlink_message_writer.c
+++ b/sys/netlink/netlink_message_writer.c
@@ -45,13 +45,13 @@
 _DECLARE_DEBUG(LOG_INFO);
 
 static bool
-nlmsg_get_buf(struct nl_writer *nw, u_int len, bool waitok)
+nlmsg_get_buf(struct nl_writer *nw, size_t len, bool waitok)
 {
 	const int mflag = waitok ? M_WAITOK : M_NOWAIT;
 
 	MPASS(nw->buf == NULL);
 
-	NL_LOG(LOG_DEBUG3, "Setting up nw %p len %u %s", nw, len,
+	NL_LOG(LOG_DEBUG3, "Setting up nw %p len %zu %s", nw, len,
 	    waitok ? "wait" : "nowait");
 
 	nw->buf = nl_buf_alloc(len, mflag);
@@ -139,17 +139,17 @@ _nlmsg_flush(struct nl_writer *nw)
  * Return true on success.
  */
 bool
-_nlmsg_refill_buffer(struct nl_writer *nw, u_int required_len)
+_nlmsg_refill_buffer(struct nl_writer *nw, size_t required_len)
 {
 	struct nl_buf *new;
-	u_int completed_len, new_len, last_len;
+	size_t completed_len, new_len, last_len;
 
 	MPASS(nw->buf != NULL);
 
 	if (nw->enomem)
 		return (false);
 
-	NL_LOG(LOG_DEBUG3, "no space at offset %u/%u (want %u), trying to "
+	NL_LOG(LOG_DEBUG3, "no space at offset %u/%u (want %zu), trying to "
 	    "reclaim", nw->buf->datalen, nw->buf->buflen, required_len);
 
 	/* Calculate new buffer size and allocate it. */
@@ -182,7 +182,7 @@ _nlmsg_refill_buffer(struct nl_writer *nw, u_int required_len)
 		new->datalen = last_len;
 	}
 
-	NL_LOG(LOG_DEBUG2, "completed: %u bytes, copied: %u bytes",
+	NL_LOG(LOG_DEBUG2, "completed: %zu bytes, copied: %zu bytes",
 	    completed_len, last_len);
 
 	if (completed_len > 0) {
@@ -204,7 +204,7 @@ _nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
 {
 	struct nl_buf *nb = nw->buf;
 	struct nlmsghdr *hdr;
-	u_int required_len;
+	size_t required_len;
 
 	MPASS(nw->hdr == NULL);
 
diff --git a/sys/netlink/netlink_message_writer.h b/sys/netlink/netlink_message_writer.h
index 8cf6951df40c..9469883feaa7 100644
--- a/sys/netlink/netlink_message_writer.h
+++ b/sys/netlink/netlink_message_writer.h
@@ -71,9 +71,9 @@ bool _nl_writer_group(struct nl_writer *, size_t, uint16_t, uint16_t, bool);
 bool _nlmsg_flush(struct nl_writer *nw);
 void _nlmsg_ignore_limit(struct nl_writer *nw);
 
-bool _nlmsg_refill_buffer(struct nl_writer *nw, u_int required_len);
-bool _nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
-    uint16_t flags, uint32_t len);
+bool _nlmsg_refill_buffer(struct nl_writer *nw, size_t required_len);
+bool _nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq,
+    uint16_t type, uint16_t flags, uint32_t len);
 bool _nlmsg_end(struct nl_writer *nw);
 void _nlmsg_abort(struct nl_writer *nw);
 
@@ -107,7 +107,7 @@ nlmsg_ignore_limit(struct nl_writer *nw)
 }
 
 static inline bool
-nlmsg_refill_buffer(struct nl_writer *nw, int required_size)
+nlmsg_refill_buffer(struct nl_writer *nw, size_t required_size)
 {
 	return (_nlmsg_refill_buffer(nw, required_size));
 }
@@ -146,9 +146,9 @@ bool nl_writer_group(struct nl_writer *, size_t, uint16_t, uint16_t,
 bool nlmsg_flush(struct nl_writer *nw);
 void nlmsg_ignore_limit(struct nl_writer *nw);
 
-bool nlmsg_refill_buffer(struct nl_writer *nw, int required_size);
-bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
-    uint16_t flags, uint32_t len);
+bool nlmsg_refill_buffer(struct nl_writer *nw, size_t required_size);
+bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq,
+    uint16_t type, uint16_t flags, uint32_t len);
 bool nlmsg_end(struct nl_writer *nw);
 void nlmsg_abort(struct nl_writer *nw);
 
diff --git a/sys/netlink/netlink_var.h b/sys/netlink/netlink_var.h
index c09e279205f0..8efe84e935c3 100644
--- a/sys/netlink/netlink_var.h
+++ b/sys/netlink/netlink_var.h
@@ -179,7 +179,7 @@ struct nl_pstate;
 struct nl_function_wrapper {
 	bool (*nlmsg_add)(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
 	    uint16_t flags, uint32_t len);
-	bool (*nlmsg_refill_buffer)(struct nl_writer *nw, int required_len);
+	bool (*nlmsg_refill_buffer)(struct nl_writer *nw, size_t required_len);
 	bool (*nlmsg_flush)(struct nl_writer *nw);
 	bool (*nlmsg_end)(struct nl_writer *nw);
 	void (*nlmsg_abort)(struct nl_writer *nw);