/src/crow/tests/fuzz/request_fuzzer.cpp
Line | Count | Source |
1 | | #include <atomic> |
2 | | #include <cstdint> |
3 | | #include <fuzzer/FuzzedDataProvider.h> |
4 | | #include <thread> |
5 | | |
6 | | #include <sys/socket.h> |
7 | | #include <sys/time.h> |
8 | | #include <unistd.h> |
9 | | |
10 | | #include "crow.h" |
11 | | |
12 | | // Fuzz harness that drives crow's full server path (http_server.h, http_connection.h, |
13 | | // socket_adaptors.h) with fuzzed requests sent over a real loopback socket. |
14 | | // |
15 | | // Harness-defect history this version fixes (the previous revision produced an |
16 | | // all-zero coverage report in production and was a memory hog): |
17 | | // - Fixed port 18080 collided with other fuzz targets running in parallel on the same |
18 | | // host; when the bind failed the server never came up and every iteration silently |
19 | | // exercised nothing. The port is now derived from the process id, and the harness |
20 | | // waits (bounded) until the server accepts before sending. |
21 | | // - .multithreaded() started one Asio worker thread per hardware core (22+ threads on |
22 | | // typical fuzzing hosts) under sanitizers: large per-thread stacks and RSS. The |
23 | | // server now runs single-threaded: bounded threads, bounded memory. |
24 | | // - Request size is capped per iteration so allocations driven by fuzzed lengths stay |
25 | | // bounded; send() passes MSG_NOSIGNAL and the socket is shut down cleanly so a |
26 | | // closed peer cannot turn SIGPIPE into a false crash. |
27 | | |
28 | | namespace |
29 | | { |
30 | | std::atomic<int> g_port{-1}; |
31 | | } // namespace |
32 | | |
33 | | /** |
34 | | * To be run in a separate thread, |
35 | | * |
36 | | * Starts up the web-server, configures a dummy route, and serves incoming requests |
37 | | */ |
38 | | static void start_web_server() |
39 | 0 | { |
40 | 0 | crow::SimpleApp app{}; |
41 | |
|
42 | 0 | CROW_ROUTE(app, "/test/<string>/<int>") |
43 | 0 | ([](const crow::request& req, std::string a, int b) |
44 | 0 | { |
45 | 0 | std::string resp{}; |
46 | 0 | for (const auto& param : req.get_body_params().keys()) |
47 | 0 | { |
48 | 0 | resp += param; |
49 | 0 | } |
50 | 0 | return resp; |
51 | 0 | }); |
52 | |
|
53 | 0 | crow::logger::setLogLevel(crow::LogLevel::CRITICAL); |
54 | | // Memory discipline: bounded worker pool regardless of hardware concurrency |
55 | | // (crow clamps this to a minimum of 2 threads; the previous harness started |
56 | | // one thread per core - 22+ on typical fuzzing hosts - under sanitizers). |
57 | 0 | app.concurrency(2); |
58 | | // Port uniqueness: parallel fuzzing of multiple targets on one host used to |
59 | | // collide on the fixed port 18080 and the server then never came up. |
60 | 0 | const int port = 20000 + (static_cast<int>(::getpid()) % 20000); |
61 | 0 | g_port = port; |
62 | 0 | app.bindaddr("127.0.0.1") |
63 | 0 | .port(static_cast<unsigned short>(port)) |
64 | 0 | .run(); |
65 | 0 | } |
66 | | |
67 | | /** |
68 | | * Called once at fuzzer start-up, initializes the web-server |
69 | | * @return True, |
70 | | */ |
71 | | static bool initialize_web_server() |
72 | 0 | { |
73 | 0 | static std::thread ws_th{start_web_server}; |
74 | | // The server intentionally lives for the whole process lifetime; detach so that |
75 | | // process exit does not run ~thread() on a still-joinable std::thread, which |
76 | | // calls std::terminate -> SIGABRT. The previous harness (upstream revision) |
77 | | // aborted on every run at exit, which is why production recorded a crash per |
78 | | // run and the target contributed zero coverage. |
79 | 0 | ws_th.detach(); |
80 | 0 | return true; |
81 | 0 | } |
82 | | |
83 | | static int send_request_to_web_server(FuzzedDataProvider& fdp) |
84 | 0 | { |
85 | | // Connection: close keeps server-side connection state bounded (no keep-alive |
86 | | // accumulation); the response is drained (bounded) so the server's close is |
87 | | // observed before the next iteration opens a new connection. Without this |
88 | | // backpressure a ~20k iterations/s fire-and-forget loop exhausts the process |
89 | | // file descriptors and the sanitizer runtime aborts on its own pipe() |
90 | | // allocation - a harness defect, not a target bug. |
91 | 0 | std::string http_msg = "GET / HTTP/1.1\r\nConnection: close\r\n"; |
92 | 0 | http_msg += fdp.ConsumeRemainingBytesAsString(); |
93 | 0 | if (http_msg.size() > 4096) |
94 | 0 | http_msg.resize(4096); |
95 | | |
96 | | // Bounded wait (<= ~2s total) until the server is accepting connections, then |
97 | | // exactly one synchronous request/response per iteration. |
98 | 0 | for (int attempt = 0; attempt < 100; ++attempt) |
99 | 0 | { |
100 | 0 | const int port = g_port.load(); |
101 | 0 | if (port > 0) |
102 | 0 | { |
103 | 0 | int sock = socket(AF_INET, SOCK_STREAM, 0); |
104 | 0 | if (sock >= 0) |
105 | 0 | { |
106 | | // Never block for long on recv: bounded server response wait. |
107 | 0 | timeval tv{}; |
108 | 0 | tv.tv_sec = 0; |
109 | 0 | tv.tv_usec = 100 * 1000; |
110 | 0 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
111 | |
|
112 | 0 | sockaddr_in ws_addr{}; |
113 | 0 | ws_addr.sin_family = AF_INET; |
114 | 0 | ws_addr.sin_port = htons(static_cast<unsigned short>(port)); |
115 | 0 | ws_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
116 | |
|
117 | 0 | if (connect(sock, (struct sockaddr*)&ws_addr, sizeof(ws_addr)) == 0) |
118 | 0 | { |
119 | 0 | send(sock, http_msg.data(), http_msg.length(), MSG_NOSIGNAL); |
120 | | // Bounded drain of the response (max 64 KiB, max 128 reads). |
121 | 0 | char buf[4096]; |
122 | 0 | std::size_t drained = 0; |
123 | 0 | for (int reads = 0; reads < 128 && drained < 65536; ++reads) |
124 | 0 | { |
125 | 0 | const ssize_t n = recv(sock, buf, sizeof(buf), 0); |
126 | 0 | if (n <= 0) |
127 | 0 | break; |
128 | 0 | drained += static_cast<std::size_t>(n); |
129 | 0 | } |
130 | 0 | (void)drained; |
131 | 0 | shutdown(sock, SHUT_WR); |
132 | 0 | close(sock); |
133 | 0 | return 0; |
134 | 0 | } |
135 | 0 | close(sock); |
136 | 0 | } |
137 | 0 | } |
138 | 0 | usleep(20 * 1000); |
139 | 0 | } |
140 | 0 | return -1; |
141 | 0 | } |
142 | | |
143 | | extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, const std::size_t size) |
144 | 0 | { |
145 | 0 | static bool initialized = initialize_web_server(); |
146 | 0 | (void)initialized; |
147 | 0 | FuzzedDataProvider fdp{data, size}; |
148 | |
|
149 | 0 | send_request_to_web_server(fdp); |
150 | 0 | return 0; |
151 | 0 | } |
152 | | |