From nobody Wed Sep 4 12:24:55 2024 X-Original-To: dev-commits-src-branches@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 4WzMBc2ZqBz5TnV4; Wed, 04 Sep 2024 12:24:56 +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 4WzMBb5gS4z56Yh; Wed, 4 Sep 2024 12:24:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725452695; 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=ZFJa2JVg0FOBF39ZJ4v/mSG4GxQfTJDEkYneTePtjqg=; b=T42DeGxg/Snwoo7SJBHHb9byDTlDCRTxy2nz/NKmcx0m9WaZu85U90nPcOVKDIpjKrh5RK FfzfCSTAi2HJ8ohpkPd38XQlnG4DgelhSjlwMrofJksDuusvKDJf1s9Q9xty+dIxYTAyOv pO/a6G4OW/0lZuQq8E02z+gAzDrgtuH12KEaji6sIjIkbqH3SRMvJR7T2wU6jOa6XGfv8F omfL2jsUTEEc2gDX1N5iFv+EECsUygBHQoG9i2IzXix8QjOTLPHLzH2XsUlNXTQx7GEKeL 4F9jT7pNxL9M/YLcyZqqYy4ixD9zwlqWo8xBWB6kafObEfZQ+bmhkiAZekoGBQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725452695; a=rsa-sha256; cv=none; b=CA6sgmZK6LFF5Sg6A4Vq6A6tUgzQDGmnD4jO3ec7AZe5H75tT5NdqSD04oD2kULl6P3S/n XIRs5aUYSSZhIG82EImcUbt75t0SGGF6RAI6u73t+TfGViekCY9xxFaucz0UrIpT7QGOz4 DmRcn22Tg8FTo4K7qxVTWhFMvsfuFA6ZckL5OkY48Ew0KmtzqHjUgOrH3FXdxF0nU8XbUF +RryT+hC1/qs1H7WcEFwYgbNJ01LWxl7SC4PUyjE1zaqj1HcyYHTBha/BtAdOBzX/SzN4R xqB/TE4rYhXtgN6e5QCO7yVVWanh1QtR0S8319ILtDCWWZyqS928vF+PIfom7w== 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=1725452695; 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=ZFJa2JVg0FOBF39ZJ4v/mSG4GxQfTJDEkYneTePtjqg=; b=uM5hke8m9qIlNNUPHabRVHycYhkb9XIihlZnTM7XVjylUwpGR4zkyQNSeUx5sWtOS3+n6q PUDiZ55YISEjI157gZmPQT8sH6ZDd1pR1s9FKe8wELFV0OU8z6xBDYuC1/A0ZAGp56Ckwu ETnlWjFiwdL2jZ0KlBswVhkVSyVVpa6tauwBFTWzic/GEuI8fEUx561iC2GqFMMfqQpJ0o tvzXLd/m7YTGjJJK5p3lttOSERfY6u8CqkOa+1nUam9NzNb0LU4x8sbDvlqMEC+aiuBIY2 +2oA24Ff5Tr0aju4pnOiVnIk/8i27CugpL+h8JsEqVWRGwMzvGbuR2IDGEW8lA== 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 4WzMBb52YkzPY4; Wed, 4 Sep 2024 12:24:55 +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 484COtRT062512; Wed, 4 Sep 2024 12:24:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 484COtwj062509; Wed, 4 Sep 2024 12:24:55 GMT (envelope-from git) Date: Wed, 4 Sep 2024 12:24:55 GMT Message-Id: <202409041224.484COtwj062509@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mariusz Zaborski Subject: git: 371af89975e3 - stable/14 - libnv: allocate buffer in a safe way List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: oshogbo X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 371af89975e3edd1e9f57aa5efba2598b63c0d2d Auto-Submitted: auto-generated The branch stable/14 has been updated by oshogbo: URL: https://cgit.FreeBSD.org/src/commit/?id=371af89975e3edd1e9f57aa5efba2598b63c0d2d commit 371af89975e3edd1e9f57aa5efba2598b63c0d2d Author: Mariusz Zaborski AuthorDate: 2024-08-26 18:10:25 +0000 Commit: Mariusz Zaborski CommitDate: 2024-09-04 12:26:05 +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) --- 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 0c76fefeebb6..9560ebc74f83 100644 --- a/sys/contrib/libnv/bsd_nvpair.c +++ b/sys/contrib/libnv/bsd_nvpair.c @@ -999,7 +999,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); @@ -1092,7 +1092,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); @@ -1330,10 +1330,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, @@ -1360,10 +1360,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, @@ -1393,7 +1393,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); @@ -1440,7 +1440,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); @@ -1507,7 +1507,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 57343f953e94..64078b10973e 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); @@ -1313,7 +1317,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)