Page MenuHomeFreeBSD

D56342.id175905.diff
No OneTemporary

D56342.id175905.diff

diff --git a/lib/libnv/tests/Makefile b/lib/libnv/tests/Makefile
--- a/lib/libnv/tests/Makefile
+++ b/lib/libnv/tests/Makefile
@@ -3,6 +3,11 @@
ATF_TESTS_C= \
nvlist_send_recv_test
+.if ${MK_ASAN} != "no"
+ATF_TESTS_C+= \
+ nvlist_header_overflow_test
+.endif
+
.PATH: ${SRCTOP}/lib/libnv
SRCS.nvlist_send_recv_test= msgio.c nvlist_send_recv_test.c
CFLAGS.nvlist_send_recv_test+=-I${SRCTOP}/sys/contrib/libnv
diff --git a/lib/libnv/tests/nvlist_header_overflow_test.c b/lib/libnv/tests/nvlist_header_overflow_test.c
new file mode 100644
--- /dev/null
+++ b/lib/libnv/tests/nvlist_header_overflow_test.c
@@ -0,0 +1,66 @@
+/*-
+ * Copyright (c) 2026 Mariusz Zaborski <oshogbo@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/nv.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <unistd.h>
+
+ATF_TC_WITHOUT_HEAD(nvlist_recv__overflow_big_endian_size);
+ATF_TC_BODY(nvlist_recv__overflow_big_endian_size, tc)
+{
+ static const unsigned char payload[] = {
+ 0x6c, /* magic */
+ 0x00, /* version */
+ 0x80, /* flags: NV_FLAG_BIG_ENDIAN */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf5,
+ };
+ nvlist_t *nvl;
+ int sv[2];
+
+ ATF_REQUIRE_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
+ ATF_REQUIRE_EQ(write(sv[1], payload, sizeof(payload)),
+ (ssize_t)sizeof(payload));
+ ATF_REQUIRE_EQ(close(sv[1]), 0);
+
+ errno = 0;
+ nvl = nvlist_recv(sv[0], 0);
+ ATF_REQUIRE(nvl == NULL);
+ ATF_REQUIRE_EQ(errno, EINVAL);
+
+ ATF_REQUIRE_EQ(close(sv[0]), 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, nvlist_recv__overflow_big_endian_size);
+
+ return (atf_no_error());
+}
diff --git a/sys/contrib/libnv/nvlist.c b/sys/contrib/libnv/nvlist.c
--- a/sys/contrib/libnv/nvlist.c
+++ b/sys/contrib/libnv/nvlist.c
@@ -1027,10 +1027,6 @@
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);
@@ -1050,6 +1046,11 @@
nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
}
#endif
+ if (nvlhdrp->nvlh_size > SIZE_MAX - sizeof(*nvlhdrp)) {
+ ERRNO_SET(EINVAL);
+ return (false);
+ }
+
return (true);
}

File Metadata

Mime Type
text/plain
Expires
Wed, Aug 19, 8:05 PM (57 m, 8 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
36954578
Default Alt Text
D56342.id175905.diff (3 KB)

Event Timeline