Commit 474b8ea
committed
fix(ssl): eliminate coredump under HTTPS + multi-thread, harden server
Repros (kMultiThread + InitSSL + concurrent ab):
- HttpServer::ssl_sockets_map_ was an unordered_map mutated concurrently
by Accept/Read/Leave workers, causing rehash-time UAF on the bucket
array. macOS libmalloc trapped (SIGTRAP) inside the freelist; lldb's
unordered_map pretty-printer reliably segfaulted while inspecting the
corrupted map, confirming the data structure itself was torn.
Root causes addressed:
- ssl_sockets_map_ has no synchronization. Add a std::mutex; critical
sections only touch the map (shared_ptr is copied out and used outside
the lock so SSL_read/write never run under the map lock).
- SSLSocket ctor checked the member mode_ before assigning it, so
pmutex_ was never created and the kSafely lock was silently disabled
even when explicitly requested. Move the assignment before the check.
- HandleAccept now passes SSLSocket::Mode::kSafely so the per-SSL* mutex
actually serializes future concurrent SSL_read/write on the same
session, in addition to the map lock.
Additional robustness (orthogonal but surfaced by the same stress test):
- SIGPIPE killed the entire server when a peer hung up mid-write. Ignore
it once in TcpServer::Init via std::call_once, but only if the user
has not installed their own handler (probe with sigaction + SIG_DFL),
so the library does not stomp user signal policy.
- SSL_accept blocked workers (and the single event loop in
kIOMultiplexing mode) indefinitely on silent/stalled clients. Apply a
bounded read timeout (default 10s, configurable via
SetSSLHandshakeTimeout) before SSL_accept and restore the user's
read_timeout_ for the actual request afterwards.
Verified on macOS arm64:
- Before: ab -n 2000 -c 100 -> SIGTRAP at ~15 requests.
- After: 3000 concurrent curls -> 2996/2999 200 OK, server alive.
10 stalled TCP-only connections no longer starve workers;
a legitimate curl is served within ~1.5s (handshake timeout
reclaims a worker), instead of hanging forever.1 parent 6e0efc3 commit 474b8ea
4 files changed
Lines changed: 96 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
259 | 259 | | |
260 | 260 | | |
261 | 261 | | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | 262 | | |
266 | 263 | | |
267 | 264 | | |
268 | 265 | | |
269 | 266 | | |
270 | 267 | | |
271 | | - | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
272 | 277 | | |
273 | 278 | | |
274 | 279 | | |
275 | 280 | | |
276 | 281 | | |
277 | 282 | | |
278 | 283 | | |
279 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
280 | 293 | | |
281 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
282 | 305 | | |
283 | 306 | | |
284 | 307 | | |
| |||
287 | 310 | | |
288 | 311 | | |
289 | 312 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
294 | 326 | | |
295 | 327 | | |
296 | 328 | | |
| |||
302 | 334 | | |
303 | 335 | | |
304 | 336 | | |
305 | | - | |
306 | | - | |
307 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
308 | 340 | | |
309 | 341 | | |
310 | 342 | | |
311 | 343 | | |
312 | 344 | | |
| 345 | + | |
313 | 346 | | |
314 | 347 | | |
315 | 348 | | |
| |||
463 | 496 | | |
464 | 497 | | |
465 | 498 | | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
466 | 503 | | |
467 | 504 | | |
468 | 505 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
231 | 240 | | |
232 | 241 | | |
233 | 242 | | |
| |||
242 | 251 | | |
243 | 252 | | |
244 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
245 | 259 | | |
246 | 260 | | |
247 | 261 | | |
| |||
259 | 273 | | |
260 | 274 | | |
261 | 275 | | |
| 276 | + | |
| 277 | + | |
262 | 278 | | |
263 | 279 | | |
264 | 280 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
8 | 13 | | |
9 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
10 | 36 | | |
11 | 37 | | |
12 | 38 | | |
| |||
258 | 284 | | |
259 | 285 | | |
260 | 286 | | |
| 287 | + | |
261 | 288 | | |
262 | 289 | | |
263 | 290 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
29 | | - | |
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
| |||
0 commit comments