From nobody Wed Sep 04 20:29:39 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WzYxv4XMHz5VKSr; Wed, 04 Sep 2024 20:29:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WzYxv3LWZz4JGp; Wed, 4 Sep 2024 20:29:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725481779; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UJg7eGWoGwWveLOvNzBsqCPgNhC6e6w3ebCoibCfo8g=; b=YPypkKGj8bDNL/7C+a+1AHlqZlePBnCsXUIlay2z75apt6epdRn9BR28nJjlHRtXQCq3/p L9QBcdN0EFFiuXn9G1XAew0x34znbrpXK0zeiQB4mBE9Du7MhaHvPj74tLQBXgBThaoK4y 0IzZx7+KXoHUEHvAnq3nswmDUBcjja4j8g+35k9k1mLiWn5yCCidD4y6dJR3p9TO1bhFmq Atk6pRBtrEjY6XswzpPjrec7SeOtLRfnWPT1kgb0osA7nCJAqGpULuwLW3lcDviXvNacDu YnjhX+eQihzh8LEF8lTqhNviAQp6uIw62q4RS+bV5bA0HQgOuB5NygyBsKQz5g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725481779; a=rsa-sha256; cv=none; b=smgL+P3yLi9Romovxc0cEVFG+FJpyxJbQ6/tK6WdiKp2lJxJ02zoM54x95J3xjO5uxEbNU asctWR5y+EZSc6FjbS2I7mTGcTqH7D9ApkyVbycbYmWjtJbKCdOmHsrmnC9KFzOgsF+n08 KQRFQMXX5E+5J1zwXPjruqRg+zdIaXxSX1XUAc5J+OCVq2rHjekqTAjdIe+ckpmjhy8Jw7 FSNRbamuGBB2tIhpdiS1nzFTZ9L6B98FWrI5zeUQ2l416MWweSixJSpgPgvCqZwH0slBKT u1XVO4vgoNpCSL8AUsa2UUrNeZeiT2PpG4KtZrmuZLVIw+9QB1WbC8P0n2eWcg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725481779; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UJg7eGWoGwWveLOvNzBsqCPgNhC6e6w3ebCoibCfo8g=; b=dN6kdu2W3p9xxATnXvnMG3z7st6QVn5xh61g+0cWLx/L+kkFjFeHfgJu+qArcOHczPNGCK 09RnatljY/MuxtYRr/2FKdNDxXZcxnpkHYAEEhDheCrvwr1bEGRi14Kso7OAKP5qKa+ypH PaAMXo87NCODnE1OdD+dZhBdQ3svJLh2Pp0mKe4xGfqwrZQGjIrOt73rrLcpAjmLFPPRTu Qmr00VRnivWs1iTA800PvRzRKOVVptdWvpqU0mcwd9trlsqIQLj1egEyVla0aPdLPq/RZi Etc47M/JkApRptmg8Xh2h/AJyaVzYuP00O/zzfuEm/Mbc2nF70GiyBpM9PIkIQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WzYxv2ygkzf6Y; Wed, 4 Sep 2024 20:29:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 484KTdf9082448; Wed, 4 Sep 2024 20:29:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 484KTdps082445; Wed, 4 Sep 2024 20:29:39 GMT (envelope-from git) Date: Wed, 4 Sep 2024 20:29:39 GMT Message-Id: <202409042029.484KTdps082445@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: fd4ee5b9eabf - releng/13.3 - libnv: allocate buffer in a safe way List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/releng/13.3 X-Git-Reftype: branch X-Git-Commit: fd4ee5b9eabf1843a0b67dca583696b91bd7fb13 Auto-Submitted: auto-generated The branch releng/13.3 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=fd4ee5b9eabf1843a0b67dca583696b91bd7fb13 commit fd4ee5b9eabf1843a0b67dca583696b91bd7fb13 Author: Mariusz Zaborski AuthorDate: 2024-08-26 18:10:25 +0000 Commit: Ed Maste CommitDate: 2024-09-04 20:28:50 +0000 libnv: allocate buffer in a safe way Ensure that the calculation of size of array doesn't overflow. Security: FreeBSD-24:09.libnv Security: CVE-2024-45287 Security: CAP-02 Reported by: Synacktiv Reported by: Taylor R Campbell (NetBSD) Sponsored by: The Alpha-Omega Project Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D46131 (cherry picked from commit 36fa90dbde0060aacb5677d0b113ee168e839071) (cherry picked from commit 2e7f9244ebefe019ef016a3a5b47c4562850d1c2) Approved by: so --- sys/contrib/libnv/bsd_nvpair.c | 18 +++++++++--------- sys/contrib/libnv/nvlist.c | 8 ++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/sys/contrib/libnv/bsd_nvpair.c b/sys/contrib/libnv/bsd_nvpair.c index 9fd233e1f1d1..41d262a18c9b 100644 --- a/sys/contrib/libnv/bsd_nvpair.c +++ b/sys/contrib/libnv/bsd_nvpair.c @@ -997,7 +997,7 @@ nvpair_unpack_string_array(bool isbe __unused, nvpair_t *nvp, return (NULL); } - value = nv_malloc(sizeof(*value) * nvp->nvp_nitems); + value = nv_calloc(nvp->nvp_nitems, sizeof(*value)); if (value == NULL) return (NULL); @@ -1090,7 +1090,7 @@ nvpair_unpack_nvlist_array(bool isbe __unused, nvpair_t *nvp, return (NULL); } - value = nv_malloc(nvp->nvp_nitems * sizeof(*value)); + value = nv_calloc(nvp->nvp_nitems, sizeof(*value)); if (value == NULL) return (NULL); @@ -1328,10 +1328,10 @@ nvpair_create_bool_array(const char *name, const bool *value, size_t nitems) return (NULL); } - size = sizeof(value[0]) * nitems; - data = nv_malloc(size); + data = nv_calloc(nitems, sizeof(value[0])); if (data == NULL) return (NULL); + size = sizeof(value[0]) * nitems; memcpy(data, value, size); nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY, (uint64_t)(uintptr_t)data, @@ -1358,10 +1358,10 @@ nvpair_create_number_array(const char *name, const uint64_t *value, return (NULL); } - size = sizeof(value[0]) * nitems; - data = nv_malloc(size); + data = nv_calloc(nitems, sizeof(value[0])); if (data == NULL) return (NULL); + size = sizeof(value[0]) * nitems; memcpy(data, value, size); nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY, @@ -1391,7 +1391,7 @@ nvpair_create_string_array(const char *name, const char * const *value, nvp = NULL; datasize = 0; - data = nv_malloc(sizeof(value[0]) * nitems); + data = nv_calloc(nitems, sizeof(value[0])); if (data == NULL) return (NULL); @@ -1438,7 +1438,7 @@ nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, return (NULL); } - nvls = nv_malloc(sizeof(value[0]) * nitems); + nvls = nv_calloc(nitems, sizeof(value[0])); if (nvls == NULL) return (NULL); @@ -1505,7 +1505,7 @@ nvpair_create_descriptor_array(const char *name, const int *value, nvp = NULL; - fds = nv_malloc(sizeof(value[0]) * nitems); + fds = nv_calloc(nitems, sizeof(value[0])); if (fds == NULL) return (NULL); for (ii = 0; ii < nitems; ii++) { diff --git a/sys/contrib/libnv/nvlist.c b/sys/contrib/libnv/nvlist.c index 56b818691660..e399d610a7ce 100644 --- a/sys/contrib/libnv/nvlist.c +++ b/sys/contrib/libnv/nvlist.c @@ -758,7 +758,7 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) int *fds; nitems = nvlist_ndescriptors(nvl); - fds = nv_malloc(sizeof(fds[0]) * (nitems + 1)); + fds = nv_calloc(nitems + 1, sizeof(fds[0])); if (fds == NULL) return (NULL); if (nitems > 0) @@ -1029,6 +1029,10 @@ static bool nvlist_check_header(struct nvlist_header *nvlhdrp) { + if (nvlhdrp->nvlh_size > SIZE_MAX - sizeof(nvlhdrp)) { + ERRNO_SET(EINVAL); + return (false); + } if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) { ERRNO_SET(EINVAL); return (false); @@ -1302,7 +1306,7 @@ nvlist_recv(int sock, int flags) goto out; if (nfds > 0) { - fds = nv_malloc(nfds * sizeof(fds[0])); + fds = nv_calloc(nfds, sizeof(fds[0])); if (fds == NULL) goto out; if (fd_recv(sock, fds, nfds) == -1)