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
This commit is contained in:
Gleb Smirnoff 2024-12-03 12:04:27 -08:00
parent 0601c0f989
commit f1c6edba88
6 changed files with 21 additions and 20 deletions

View file

@ -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));
}

View file

@ -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);

View file

@ -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));
}

View file

@ -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);

View file

@ -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);

View file

@ -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);