diff options
Diffstat (limited to 'lib/libnv/tests/nvlist_send_recv_test.c')
| -rw-r--r-- | lib/libnv/tests/nvlist_send_recv_test.c | 115 |
1 files changed, 113 insertions, 2 deletions
diff --git a/lib/libnv/tests/nvlist_send_recv_test.c b/lib/libnv/tests/nvlist_send_recv_test.c index 79297dfe2043..306c91560400 100644 --- a/lib/libnv/tests/nvlist_send_recv_test.c +++ b/lib/libnv/tests/nvlist_send_recv_test.c @@ -1,5 +1,8 @@ /*- + * SPDX-License-Identifier: BSD-2-Clause + * * Copyright (c) 2013 The FreeBSD Foundation + * Copyright (c) 2024-2026 Mariusz Zaborski <oshogbo@FreeBSD.org> * * This software was developed by Pawel Jakub Dawidek under sponsorship from * the FreeBSD Foundation. @@ -27,6 +30,8 @@ */ #include <sys/param.h> +#include <sys/resource.h> +#include <sys/select.h> #include <sys/socket.h> #include <sys/sysctl.h> #include <sys/wait.h> @@ -482,8 +487,6 @@ ATF_TC_BODY(nvlist_send_recv__send_many_fds__dgram, tc) size_t len; int error; - atf_tc_skip("https://bugs.freebsd.org/260891"); - /* size of the largest datagram to send */ temp_maxdgram = 16772; len = sizeof(maxdgram); @@ -533,6 +536,59 @@ ATF_TC_BODY(nvlist_send_recv__send_nvlist__stream, tc) nvlist_send_recv__send_nvlist(SOCK_STREAM); } +/* + * Regression test for fd_wait(): the previous select(2)-based implementation + * called FD_SET() unconditionally, which is an out-of-bounds stack write when + * the socket fd is >= FD_SETSIZE. Force the socketpair fds above FD_SETSIZE + * and verify a full nvlist round-trip still works. + */ +ATF_TC_WITHOUT_HEAD(nvlist_send_recv__highfd); +ATF_TC_BODY(nvlist_send_recv__highfd, tc) +{ + struct rlimit rl; + nvlist_t *nvl; + int socks[2], hi_send, hi_recv, status; + pid_t pid; + + hi_send = FD_SETSIZE + 5; + hi_recv = FD_SETSIZE + 6; + + rl.rlim_cur = rl.rlim_max = hi_recv + 1; + if (setrlimit(RLIMIT_NOFILE, &rl) != 0) + atf_tc_skip("cannot raise RLIMIT_NOFILE: %s", strerror(errno)); + + ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, socks) == 0); + ATF_REQUIRE(dup2(socks[0], hi_recv) == hi_recv); + ATF_REQUIRE(dup2(socks[1], hi_send) == hi_send); + (void)close(socks[0]); + (void)close(socks[1]); + + pid = fork(); + ATF_REQUIRE(pid >= 0); + if (pid == 0) { + /* Child: send. */ + (void)close(hi_recv); + nvl = nvlist_create(0); + nvlist_add_string(nvl, "key", "value"); + if (nvlist_send(hi_send, nvl) != 0) + err(EXIT_FAILURE, "nvlist_send"); + nvlist_destroy(nvl); + _exit(0); + } + + (void)close(hi_send); + nvl = nvlist_recv(hi_recv, 0); + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(nvlist_error(nvl) == 0); + ATF_REQUIRE(nvlist_exists_string(nvl, "key")); + ATF_REQUIRE(strcmp(nvlist_get_string(nvl, "key"), "value") == 0); + nvlist_destroy(nvl); + + ATF_REQUIRE(waitpid(pid, &status, 0) == pid); + ATF_REQUIRE(status == 0); + (void)close(hi_recv); +} + ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_closed_fd__dgram); ATF_TC_BODY(nvlist_send_recv__send_closed_fd__dgram, tc) { @@ -608,6 +664,58 @@ ATF_TC_BODY(nvlist_send_recv__overflow_header_size, tc) } } +ATF_TC_WITHOUT_HEAD(nvlist_send_recv__overflow_big_endian_size); +ATF_TC_BODY(nvlist_send_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_TC_WITHOUT_HEAD(nvlist_send_recv__overflow_little_endian_size); +ATF_TC_BODY(nvlist_send_recv__overflow_little_endian_size, tc) +{ + static const unsigned char payload[] = { + 0x6c, /* magic */ + 0x00, /* version */ + 0x00, /* flags */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xf5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + }; + 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_TC_WITHOUT_HEAD(nvlist_send_recv__invalid_fd_size); ATF_TC_BODY(nvlist_send_recv__invalid_fd_size, tc) { @@ -736,12 +844,15 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, nvlist_send_recv__send_nvlist__dgram); ATF_TP_ADD_TC(tp, nvlist_send_recv__send_nvlist__stream); + ATF_TP_ADD_TC(tp, nvlist_send_recv__highfd); ATF_TP_ADD_TC(tp, nvlist_send_recv__send_closed_fd__dgram); ATF_TP_ADD_TC(tp, nvlist_send_recv__send_closed_fd__stream); ATF_TP_ADD_TC(tp, nvlist_send_recv__send_many_fds__dgram); ATF_TP_ADD_TC(tp, nvlist_send_recv__send_many_fds__stream); ATF_TP_ADD_TC(tp, nvlist_send_recv__overflow_header_size); + ATF_TP_ADD_TC(tp, nvlist_send_recv__overflow_big_endian_size); + ATF_TP_ADD_TC(tp, nvlist_send_recv__overflow_little_endian_size); ATF_TP_ADD_TC(tp, nvlist_send_recv__invalid_fd_size); ATF_TP_ADD_TC(tp, nvlist_send_recv__overflow_fd_size); |
