|
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. |
@@ -33813,6 +33819,46 @@ int test_wc_LmsKey_reload_cache(void) |
33813 | 33819 | return EXPECT_RESULT(); |
33814 | 33820 | } |
33815 | 33821 |
|
| 33822 | +#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT) |
| 33823 | +static int test_sniffer_chain_input_overflow(void) |
| 33824 | +{ |
| 33825 | + EXPECT_DECLS; |
| 33826 | + struct iovec chain[3]; |
| 33827 | + byte* data = NULL; |
| 33828 | + char error[WOLFSSL_MAX_ERROR_SZ]; |
| 33829 | + int ret; |
| 33830 | + byte dummy[1] = {0}; |
| 33831 | + |
| 33832 | + /* Test 1: iov_len values that sum to more than INT_MAX. |
| 33833 | + * Before the fix, these size_t values would be truncated when accumulated |
| 33834 | + * into an int, causing an undersized allocation followed by an oversized |
| 33835 | + * copy (heap buffer overflow). After the fix, the function should detect |
| 33836 | + * the overflow and return an error without allocating or copying. */ |
| 33837 | + chain[0].iov_base = dummy; |
| 33838 | + chain[0].iov_len = (size_t)0x80000000UL; /* 2GB */ |
| 33839 | + chain[1].iov_base = dummy; |
| 33840 | + chain[1].iov_len = (size_t)0x80000000UL; /* 2GB */ |
| 33841 | + chain[2].iov_base = dummy; |
| 33842 | + chain[2].iov_len = (size_t)0x80000000UL; /* 2GB */ |
| 33843 | + |
| 33844 | + XMEMSET(error, 0, sizeof(error)); |
| 33845 | + ret = ssl_DecodePacketWithChain(chain, 3, &data, error); |
| 33846 | + ExpectIntEQ(ret, WOLFSSL_SNIFFER_ERROR); |
| 33847 | + |
| 33848 | + /* Test 2: total exactly at INT_MAX boundary should also be rejected since |
| 33849 | + * it would require a ~2GB allocation that is unreasonable for a packet. */ |
| 33850 | + chain[0].iov_len = (size_t)0x7FFFFFFFUL; /* INT_MAX */ |
| 33851 | + chain[1].iov_len = (size_t)1; |
| 33852 | + chain[2].iov_len = (size_t)0; |
| 33853 | + |
| 33854 | + XMEMSET(error, 0, sizeof(error)); |
| 33855 | + ret = ssl_DecodePacketWithChain(chain, 2, &data, error); |
| 33856 | + ExpectIntEQ(ret, WOLFSSL_SNIFFER_ERROR); |
| 33857 | + |
| 33858 | + return EXPECT_RESULT(); |
| 33859 | +} |
| 33860 | +#endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_CHAIN_INPUT */ |
| 33861 | + |
33816 | 33862 | TEST_CASE testCases[] = { |
33817 | 33863 | TEST_DECL(test_fileAccess), |
33818 | 33864 |
|
@@ -34619,6 +34665,11 @@ TEST_CASE testCases[] = { |
34619 | 34665 | TEST_DECL(test_ocsp_responder), |
34620 | 34666 | TEST_TLS_DECLS, |
34621 | 34667 | TEST_DECL(test_wc_DhSetNamedKey), |
| 34668 | + |
| 34669 | +#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT) |
| 34670 | + TEST_DECL(test_sniffer_chain_input_overflow), |
| 34671 | +#endif |
| 34672 | + |
34622 | 34673 | /* This test needs to stay at the end to clean up any caches allocated. */ |
34623 | 34674 | TEST_DECL(test_wolfSSL_Cleanup) |
34624 | 34675 | }; |
|
0 commit comments