| 1 |
// Buffer safety test for resolve_host |
| 2 |
// Tests that we don't overflow the buffer even with many results |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#ifndef _WIN32 |
| 9 |
#include <unistd.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <netdb.h> |
| 12 |
#include <arpa/inet.h> |
| 13 |
#else |
| 14 |
#include <winsock2.h> |
| 15 |
#include <ws2tcpip.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#define MAX_IP_RESULTS 10 |
| 19 |
#define CANARY_PATTERN 0xDEADBEEF |
| 20 |
|
| 21 |
static int |
| 22 |
resolve_host(const char *hostname, char *ipaddr_list, int max_results, |
| 23 |
int req_ai_family) |
| 24 |
{ |
| 25 |
struct addrinfo hints; |
| 26 |
struct addrinfo *result; |
| 27 |
char ipaddr[INET6_ADDRSTRLEN]; |
| 28 |
int error; |
| 29 |
|
| 30 |
// Truncate ipaddr_list to an empty string. |
| 31 |
ipaddr_list[0] = '\0'; |
| 32 |
|
| 33 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 34 |
|
| 35 |
hints.ai_family = req_ai_family; |
| 36 |
hints.ai_socktype = SOCK_STREAM; |
| 37 |
|
| 38 |
error = getaddrinfo(hostname, NULL, &hints, &result); |
| 39 |
if (error) return error; |
| 40 |
int i = 0; |
| 41 |
size_t offset = 0; |
| 42 |
for(struct addrinfo *ai = result; ai != NULL && i < max_results; ai = ai->ai_next, i++) { |
| 43 |
getnameinfo(ai->ai_addr, ai->ai_addrlen, ipaddr, sizeof(ipaddr), NULL, 0, |
| 44 |
NI_NUMERICHOST); |
| 45 |
if (offset > 0) { |
| 46 |
ipaddr_list[offset++] = ';'; |
| 47 |
} |
| 48 |
size_t len = strlen(ipaddr); |
| 49 |
strcpy(ipaddr_list + offset, ipaddr); |
| 50 |
offset += len; |
| 51 |
} |
| 52 |
freeaddrinfo(result); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
int main() { |
| 57 |
printf("=== Buffer Safety Test ===\n\n"); |
| 58 |
|
| 59 |
// Create a buffer with canary values before and after |
| 60 |
size_t buffer_size = INET6_ADDRSTRLEN * MAX_IP_RESULTS + MAX_IP_RESULTS; |
| 61 |
unsigned int *canary_before = malloc(sizeof(unsigned int)); |
| 62 |
char *ipaddr_list = malloc(buffer_size); |
| 63 |
unsigned int *canary_after = malloc(sizeof(unsigned int)); |
| 64 |
|
| 65 |
*canary_before = CANARY_PATTERN; |
| 66 |
*canary_after = CANARY_PATTERN; |
| 67 |
|
| 68 |
printf("Buffer size: %zu bytes\n", buffer_size); |
| 69 |
printf("Canary before buffer: 0x%X at %p\n", *canary_before, (void*)canary_before); |
| 70 |
printf("Buffer address: %p\n", (void*)ipaddr_list); |
| 71 |
printf("Canary after buffer: 0x%X at %p\n\n", *canary_after, (void*)canary_after); |
| 72 |
|
| 73 |
// Test 1: Normal hostname |
| 74 |
printf("Test 1: Normal hostname (google.com)\n"); |
| 75 |
int result = resolve_host("google.com", ipaddr_list, MAX_IP_RESULTS, AF_UNSPEC); |
| 76 |
if (result == 0) { |
| 77 |
printf("Result: %s\n", ipaddr_list); |
| 78 |
printf("Length: %zu bytes\n", strlen(ipaddr_list)); |
| 79 |
} |
| 80 |
|
| 81 |
if (*canary_before != CANARY_PATTERN) { |
| 82 |
printf("✗ CANARY BEFORE CORRUPTED: 0x%X\n", *canary_before); |
| 83 |
} else { |
| 84 |
printf("✓ Canary before intact\n"); |
| 85 |
} |
| 86 |
|
| 87 |
if (*canary_after != CANARY_PATTERN) { |
| 88 |
printf("✗ CANARY AFTER CORRUPTED: 0x%X\n", *canary_after); |
| 89 |
} else { |
| 90 |
printf("✓ Canary after intact\n"); |
| 91 |
} |
| 92 |
|
| 93 |
// Test 2: Fill buffer to near-capacity |
| 94 |
printf("\n\nTest 2: Large max_results (stress test)\n"); |
| 95 |
result = resolve_host("www.google.com", ipaddr_list, MAX_IP_RESULTS, AF_UNSPEC); |
| 96 |
if (result == 0) { |
| 97 |
size_t len = strlen(ipaddr_list); |
| 98 |
printf("Result length: %zu bytes (%.1f%% of buffer)\n", |
| 99 |
len, (len * 100.0) / buffer_size); |
| 100 |
printf("Result: %s\n", ipaddr_list); |
| 101 |
|
| 102 |
// Count IPs |
| 103 |
int count = 1; |
| 104 |
for (char *p = ipaddr_list; *p; p++) { |
| 105 |
if (*p == ';') count++; |
| 106 |
} |
| 107 |
printf("Number of IPs: %d\n", count); |
| 108 |
} |
| 109 |
|
| 110 |
if (*canary_before != CANARY_PATTERN) { |
| 111 |
printf("✗ CANARY BEFORE CORRUPTED: 0x%X\n", *canary_before); |
| 112 |
} else { |
| 113 |
printf("✓ Canary before intact\n"); |
| 114 |
} |
| 115 |
|
| 116 |
if (*canary_after != CANARY_PATTERN) { |
| 117 |
printf("✗ CANARY AFTER CORRUPTED: 0x%X\n", *canary_after); |
| 118 |
} else { |
| 119 |
printf("✓ Canary after intact\n"); |
| 120 |
} |
| 121 |
|
| 122 |
// Test 3: Edge case - max_results = 0 |
| 123 |
printf("\n\nTest 3: Edge case - max_results = 0\n"); |
| 124 |
result = resolve_host("localhost", ipaddr_list, 0, AF_INET); |
| 125 |
printf("Result: '%s'\n", ipaddr_list); |
| 126 |
printf("Length: %zu bytes (should be 0)\n", strlen(ipaddr_list)); |
| 127 |
|
| 128 |
if (*canary_before != CANARY_PATTERN) { |
| 129 |
printf("✗ CANARY BEFORE CORRUPTED: 0x%X\n", *canary_before); |
| 130 |
} else { |
| 131 |
printf("✓ Canary before intact\n"); |
| 132 |
} |
| 133 |
|
| 134 |
if (*canary_after != CANARY_PATTERN) { |
| 135 |
printf("✗ CANARY AFTER CORRUPTED: 0x%X\n", *canary_after); |
| 136 |
} else { |
| 137 |
printf("✓ Canary after intact\n"); |
| 138 |
} |
| 139 |
|
| 140 |
// Test 4: Very long IPv6 addresses |
| 141 |
printf("\n\nTest 4: IPv6 addresses (longest format)\n"); |
| 142 |
result = resolve_host("ipv6.google.com", ipaddr_list, MAX_IP_RESULTS, AF_INET6); |
| 143 |
if (result == 0) { |
| 144 |
printf("Result: %s\n", ipaddr_list); |
| 145 |
printf("Length: %zu bytes\n", strlen(ipaddr_list)); |
| 146 |
} else { |
| 147 |
printf("Could not resolve ipv6.google.com (error: %s)\n", gai_strerror(result)); |
| 148 |
} |
| 149 |
|
| 150 |
if (*canary_before != CANARY_PATTERN) { |
| 151 |
printf("✗ CANARY BEFORE CORRUPTED: 0x%X\n", *canary_before); |
| 152 |
} else { |
| 153 |
printf("✓ Canary before intact\n"); |
| 154 |
} |
| 155 |
|
| 156 |
if (*canary_after != CANARY_PATTERN) { |
| 157 |
printf("✗ CANARY AFTER CORRUPTED: 0x%X\n", *canary_after); |
| 158 |
} else { |
| 159 |
printf("✓ Canary after intact\n"); |
| 160 |
} |
| 161 |
|
| 162 |
free(canary_before); |
| 163 |
free(ipaddr_list); |
| 164 |
free(canary_after); |
| 165 |
|
| 166 |
printf("\n=== Buffer Safety Test Complete ===\n"); |
| 167 |
printf("If all canaries are intact, the function is memory-safe.\n"); |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |