mod_http2 中 assign_headers() 的堆损坏| CVE | CVE-2026-119618 |
| 组件 | Apache HTTP Server,模块 mod_http2(独立版 mod_h2) |
| 受影响版本 | httpd 2.4.55 – 2.4.70 · mod_h2 2.0.8 – 2.0.42 |
| 修复版本 | httpd 2.4.71 · mod_h2 2.0.43 |
| CWE | CWE-362 — 使用共享资源的并发执行且同步不当 |
| 影响 | httpd 子进程中的堆损坏;可能导致远程代码执行 |
| 攻击向量 | 网络、无需认证、HTTP/2(明文 h2c 或 TLS h2) |
| 触发条件 | 一个无请求体的 HTTP/2 请求,携带 Content-Type 且无 Content-Length,同时请求头表已填满至容量上限 |
| 修复提交 | 824beab9b9424d6cfb29595248fdbf6c3dddeab1(cherry-pick 自 trunk 5ed39287660daacd1233cb9bbc984b25fa9a4fe3) |
| 上游 PR | apache/httpd#975 → apache/httpd#977,于 2026-11-09 合并 |
| 致谢 | Anthropic 的 OSS Scanner(ANT-2026-MAPKT6MZ);Google Cloud 漏洞研究团队的 Anthony Weems 和 Simon Scannell |
| 安全公告 | https://httpd.apache.org/security/vulnerabilities_24.html |
| 临时缓解措施 | 从 Protocols 指令中移除 h2 和 h2c |
| 代码仓库 | https://github.com/googlesky/CVE-2026-119618 |
mod_http2 为每个 HTTP/2 流分配独立的内存池,而该内存池继承拥有该会话的连接的分配器。该分配器没有互斥锁:apr_allocator_create() 不会创建互斥锁,因此 allocator_lock() 对整个连接而言是空操作。请求处理被刻意转移到工作线程,并使用私有分配器(c2_transit_create()),正是为了让工作线程永远不会触碰连接的分配器。assign_headers() 破坏了这一不变量:对于携带 Content-Type 且无 Content-Length 的无请求体请求,工作线程将合成的 Content-Length: 0 写入共享的流表,而非请求私有的 r->headers_in。当该表已满时,此写入会从连接的无锁分配器中分配一个新的 8 KB 节点,而此时连接线程正在从同一空闲链表分配或释放。两个线程可能弹出同一个节点,由此产生的双重所有权会损坏 httpd 子进程的堆。
本仓库包含一个容器化的差分实验环境,可在 httpd 2.4.70 上复现该损坏,用 ThreadSanitizer 证明竞态,展示 httpd 2.4.71 在相同流量下表现正常,测试所有同类请求形态,验证已发布的临时缓解措施,并记录在修复版本线上的 0-day 扫描。
modules/http2/h2_request.c,Apache httpd 2.4.70,assign_headers()。
行号来自 2.4.70 发布版 tarball。
320: static void assign_headers(request_rec *r, const h2_request *req,
321: int no_body, int is_connect)
322: {
323: const char *cl;
324:
325: r->headers_in = apr_table_clone(r->pool, req->headers);
...
341: /* Unless we open a byte stream via CONNECT, apply content-length guards. */
342: if (!is_connect) {
343: cl = apr_table_get(req->headers, "Content-Length");
344: if (no_body) {
345: if (!cl && apr_table_get(req->headers, "Content-Type")) {
346: /* If we have a content-type, but already seen eos, no more
347: * data will come. Signal a zero content length explicitly.
348: */
349: apr_table_setn(req->headers, "Content-Length", "0");
350: }
351: }
2.4.71 中的修复改动了一行:
- apr_table_setn(req->headers, "Content-Length", "0");
+ apr_table_setn(r->headers_in, "Content-Length", "0");
r->headers_in 是在 r->pool 第 325 行创建的克隆,即工作线程拥有的请求池。req->headers 是流的表,从流池中分配,连接线程可以访问它。
h2_session_open_stream()(h2_session.c:117)将会话池的子池创建为每个流的池,并为其打上标签:
apr_pool_create(&stream_pool, session->pool);
apr_pool_tag(stream_pool, "h2_stream");
stream = h2_stream_create(stream_id, stream_pool, session, ...);
会话池是 MPM 在 child_main() 中创建的连接池的后代(server/mpm/prefork/prefork.c:423)。该池的分配器来自 apr_allocator_create(),并且从未在其上设置任何互斥锁:
/* memory/unix/apr_pools.c:164 */
static APR_INLINE void allocator_lock(apr_allocator_t *allocator)
{
#if APR_HAS_THREADS
if (allocator->mutex) /* NULL for the MPM allocator */
apr_thread_mutex_lock(allocator->mutex);
#endif
}
另一方面,请求处理被明确设计为避免使用
该分配器。c2_transit_create()(h2_mplx.c:210)为每个 worker
提供一个私有分配器:
/* We create a pool with its own allocator to be used for
* processing a request. This is the only way to have the processing
* independent of its parent pool in the sense that it can work in
* another thread.
*/
rv = apr_allocator_create(&allocator);
worker 的 request_rec 创建于 c2->pool 之上,而后者是 transit pool 的后代,因此 worker 通常进行的每次分配都是私有的。
存在漏洞的写入是例外:它从 stream pool 分配,而该 pool 的分配器属于连接。
对不存在的键调用 apr_table_setn() 会落入 apr_table_add(),
后者调用 table_push() → apr_array_push_noclear()
(tables/apr_tables.c:126):
if (arr->nelts == arr->nalloc) {
int new_size = arr->nalloc <= 0 ? 1 : 2 * arr->nalloc;
apr_table_entry_t *new_elts = apr_palloc(arr->pool, elt_size * new_size);
...
}
该表由 h2_request_create() 创建为
apr_table_make(pool, 10),因此 nalloc 从 10 开始并翻倍:10、20、
40、80、160。伪头部不会存储在表中——它们会变成
req->method、req->scheme、req->authority 和 req->path。
h2_request_end_headers()(h2_request.c:212)恰好添加一个 Host
条目。
因此,一个带有 79 个常规头部 的请求会持有 79 个条目,Host
使其变为 80,而当 assign_headers() 运行时则为 nelts == nalloc == 80。存在漏洞的
apr_table_setn() 会将数组增长到 160 个条目:
160 * sizeof(apr_table_entry_t) = 160 * 24 = 3840 bytes
allocator_align()(apr_pools.c:268)将其向上取整为 MIN_ALLOC
(8192),因此只要流池的活动节点剩余不足 3840 字节,写操作就会向流池的
分配器请求一个新的 8 KB 节点。
这就是发生竞态的分配。
allocator_alloc()(apr_pools.c:289)遍历空闲节点链表
(allocator->free[index]、allocator->max_index)并弹出一个节点;
allocator_free()(apr_pools.c:426)将一个节点压回并重写
max_index。由于没有互斥锁,这些操作是对同一个 apr_allocator_t 的
未同步读-改-写序列——该对象是一个 200 字节的堆对象,
由 apr_allocator_create() 在 child_main() 中分配。两个线程可能
弹出同一个节点,随后两个不同的池就会分发重叠的
内存。
ThreadSanitizer 在 2.4.70 上恰好捕获到了这一点
(evidence/tsan-vulnerable.txt):
WARNING: ThreadSanitizer: data race
Read of size 8 at 0x7b34000001a0 by thread T1:
#0 allocator_alloc memory/unix/apr_pools.c:315
#1 apr_palloc memory/unix/apr_pools.c:843
#2 apr_array_push_noclear tables/apr_tables.c:132
#3 apr_table_setn tables/apr_tables.c:638
#4 assign_headers /build/httpd-2.4.70/modules/http2/h2_request.c:353
#5 h2_create_request_rec /build/httpd-2.4.70/modules/http2/h2_request.c:387
#6 c2_process /build/httpd-2.4.70/modules/http2/h2_c2.c:748
...
Previous write of size 8 at 0x7b34000001a0 by main thread:
#0 allocator_free memory/unix/apr_pools.c:480
#1 apr_allocator_free memory/unix/apr_pools.c:506
#2 apr_bucket_free buckets/apr_buckets_alloc.c:241
#3 socket_bucket_read buckets/apr_buckets_socket.c:43
...
Location is heap block of size 200 at 0x7b34000001a0 allocated by main thread:
#1 apr_allocator_create memory/unix/apr_pools.c:187
#2 child_main /build/httpd-2.4.70/server/mpm/prefork/prefork.c:423
工作线程(线程 T1,c2_process → assign_headers)读取空闲链表,而连接线程(主线程,h2_session_process → h2_c1_read)写入该链表。发生竞争的对象正是分配器本身。
这种损坏表现为分配器内部以及连接线程上的流代码中发生进程崩溃——allocator_lock、allocator_free、apr_palloc、run_cleanups、h2_stream_send_frame、output_data_buffered(evidence/asan.*)。
h2_c2.c:748 传入 no_body 标志:
r = h2_create_request_rec(req, c, conn_ctx->beam_in == NULL);
conn_ctx->beam_in 由 stream->input 设置,而后者仅在流仍期望一个 body 时才存在。对于在 HEADERS 帧上结束的请求,close_input() 已执行,且 beam_in 为 NULL,因此 no_body == 1。
因此,该分支需要同时满足以下四个条件:
Content-Type 头,Content-Length 头,:authority 合成的 Host 条目。CONNECT 请求会短路该分支(if (!is_connect))。
修复后的构建执行相同的 3840 字节分配,但分配来自 r->headers_in,它位于 r->pool 中。r->pool 是 conn_ctx->req_pool,由 c2->pool(h2_conn_ctx.c:100)创建,后者是 transit pool 的后代——因此由 worker 的私有分配器支撑。分配模式不变;只有分配器改变。这正是使差分干净的原因。
一切都在专用网络(cve119618lab)上的 Podman 容器中运行。主机仅作为暂存区。
| 组件 | 详情 |
|---|---|
| 基础镜像 | docker.io/library/debian:bookworm |
| APR | 1.7.4,从上游 tarball 构建 |
| APR-util | 1.6.3,从上游 tarball 构建 |
| httpd | 2.4.70(受影响)和 2.4.71(已修复),从上游 tarball 构建 |
| mod_http2 | --enable-http2=shared,系统 nghttp2 |
| 插桩 | :vuln 中的 AddressSanitizer(-fsanitize=address),:tsan 中的 ThreadSanitizer(-fsanitize=thread) |
| MPM | prefork,Protocols h2c http/1.1(明文 HTTP/2,无 TLS 材料) |
| 限制 | 每个容器都以 --cpus 2 --memory 4g 运行 |
两个 httpd 版本位于同一镜像中;HTTPD_VERSION 选择其中一个:
HTTPD_VERSION | 服务器根目录 | 角色 |
|---|---|---|
vuln | /opt/httpd-2.4.70 | 受影响构建 |
fixed | /opt/httpd-2.4.71 | 已修复构建 |