|
159 | 159 | #include "wolfssl/internal.h" |
160 | 160 | #endif |
161 | 161 |
|
| 162 | +#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT) |
| 163 | + #include <wolfssl/sniffer.h> |
| 164 | + #include <wolfssl/sniffer_error.h> |
| 165 | + #include <sys/uio.h> |
| 166 | +#endif |
| 167 | + |
162 | 168 | /* include misc.c here regardless of NO_INLINE, because misc.c implementations |
163 | 169 | * have default (hidden) visibility, and in the absence of visibility, it's |
164 | 170 | * benign to mask out the library implementation. |
@@ -33786,6 +33792,46 @@ int test_wc_LmsKey_reload_cache(void) |
33786 | 33792 | return EXPECT_RESULT(); |
33787 | 33793 | } |
33788 | 33794 |
|
| 33795 | +#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT) |
| 33796 | +static int test_sniffer_chain_input_overflow(void) |
| 33797 | +{ |
| 33798 | + EXPECT_DECLS; |
| 33799 | + struct iovec chain[3]; |
| 33800 | + byte* data = NULL; |
| 33801 | + char error[WOLFSSL_MAX_ERROR_SZ]; |
| 33802 | + int ret; |
| 33803 | + byte dummy[1] = {0}; |
| 33804 | + |
| 33805 | + /* Test 1: iov_len values that sum to more than INT_MAX. |
| 33806 | + * Before the fix, these size_t values would be truncated when accumulated |
| 33807 | + * into an int, causing an undersized allocation followed by an oversized |
| 33808 | + * copy (heap buffer overflow). After the fix, the function should detect |
| 33809 | + * the overflow and return an error without allocating or copying. */ |
| 33810 | + chain[0].iov_base = dummy; |
| 33811 | + chain[0].iov_len = (size_t)0x80000000UL; /* 2GB */ |
| 33812 | + chain[1].iov_base = dummy; |
| 33813 | + chain[1].iov_len = (size_t)0x80000000UL; /* 2GB */ |
| 33814 | + chain[2].iov_base = dummy; |
| 33815 | + chain[2].iov_len = (size_t)0x80000000UL; /* 2GB */ |
| 33816 | + |
| 33817 | + XMEMSET(error, 0, sizeof(error)); |
| 33818 | + ret = ssl_DecodePacketWithChain(chain, 3, &data, error); |
| 33819 | + ExpectIntEQ(ret, WOLFSSL_SNIFFER_ERROR); |
| 33820 | + |
| 33821 | + /* Test 2: total exactly at INT_MAX boundary should also be rejected since |
| 33822 | + * it would require a ~2GB allocation that is unreasonable for a packet. */ |
| 33823 | + chain[0].iov_len = (size_t)0x7FFFFFFFUL; /* INT_MAX */ |
| 33824 | + chain[1].iov_len = (size_t)1; |
| 33825 | + chain[2].iov_len = (size_t)0; |
| 33826 | + |
| 33827 | + XMEMSET(error, 0, sizeof(error)); |
| 33828 | + ret = ssl_DecodePacketWithChain(chain, 2, &data, error); |
| 33829 | + ExpectIntEQ(ret, WOLFSSL_SNIFFER_ERROR); |
| 33830 | + |
| 33831 | + return EXPECT_RESULT(); |
| 33832 | +} |
| 33833 | +#endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_CHAIN_INPUT */ |
| 33834 | + |
33789 | 33835 | TEST_CASE testCases[] = { |
33790 | 33836 | TEST_DECL(test_fileAccess), |
33791 | 33837 |
|
@@ -34591,6 +34637,11 @@ TEST_CASE testCases[] = { |
34591 | 34637 | TEST_DECL(test_ocsp_responder), |
34592 | 34638 | TEST_TLS_DECLS, |
34593 | 34639 | TEST_DECL(test_wc_DhSetNamedKey), |
| 34640 | + |
| 34641 | +#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT) |
| 34642 | + TEST_DECL(test_sniffer_chain_input_overflow), |
| 34643 | +#endif |
| 34644 | + |
34594 | 34645 | /* This test needs to stay at the end to clean up any caches allocated. */ |
34595 | 34646 | TEST_DECL(test_wolfSSL_Cleanup) |
34596 | 34647 | }; |
|
0 commit comments