BUG/MINOR: sample: be2dec/le2dec heap overflow when the separator argument is longer than the trash buffer

未關閉 適合新手
#3,604 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
62/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
c
領域
backend, security

研究方向

溢位位於 src/sample.c 中的 sample_conv_2dec()。其中 max_size 是 int 型別,當分隔符長度超過 trash->size - 2 時會變成負數。請與同一檔案中的 sample_conv_be2hex() 比較,後者已經有大小上限檢查。使用 ASan 建置,並執行 issue 中的重現設定。完成的標準是:不再出現 heap-buffer-overflow 報告,且過大的分隔符在解析階段就被拒絕。

由索引模型根據 Issue 內容生成。

描述

status: needs-triage type: bug
Detailed Description of the Problem

On any build, be2dec/le2dec (sample_conv_2dec(), src/sample.c @ 711a049f6) overflow the trash buffer when the separator argument is longer than trash->size - 2.
The loop bound max_size is an int that underflows at the first iteration, and the loop condition trash->data <= max_size then compares size_t against a negative int, which disables every remaining bounds check.
The separator is then memcpy'd at a walking offset once per input group, so the overflow grows linearly with the client input, and ulltoa()'s underflowed room argument writes client-derived digits past the end as well.
With a separator longer than bufsize - 2 (config-controlled), any request reaching the converter overflows the trash chunk, and the overflow grows with the input length. Under AddressSanitizer a single request triggers it; on plain builds the corruption of the adjacent per-thread trash chunk is silent.

Expected Behavior

A converter argument must never cause an out-of-bounds write, and a client request must not corrupt the heap.

Steps to Reproduce the Behavior
  1. Build with ASan:

    make -j$(nproc) TARGET=linux-glibc USE_OPENSSL=1 \
         CFLAGS="-O1 -g -fsanitize=address" LDFLAGS="-fsanitize=address"
    
  2. Generate the config (separator = 4400 bytes, tune.bufsize 4096 so the separator exceeds trash->size - 2; arguments are parsed into the pre-parse trash sized with the default bufsize, so the long argument is accepted):

    python3 -c "
    SEP = 'S' * 4400
    open('/tmp/repro.cfg','w').write(f'''global
        nbthread 1
        tune.bufsize 4096
    defaults
        mode http
        timeout client 5s
        timeout server 5s
    frontend fe
        bind 127.0.0.1:18099
        http-request return status 200 content-type text/plain lf-string \"OUT[%[req.hdr(X),be2dec({SEP},8,1)]]\"
    ''')"
    
  3. Validate and run in foreground:

    ./haproxy -c -f /tmp/repro.cfg
    ./haproxy -d -f /tmp/repro.cfg -db
    
  4. In another terminal, send one request:

    printf 'GET / HTTP/1.1\r\nX: ABCDEFGHIJKLMNOP\r\nHost: a\r\n\r\n' | nc -q1 127.0.0.1 18099
    
  5. Terminal 1 shows the AddressSanitizer report below and the worker dies.
    Repeatable 100% of the time.
    With a default tune.bufsize, a 16383-byte separator triggers the same overflow.

Do you have any idea what may have caused this?
	int max_size;
	...
	max_size = trash->size - 2;                        /* 16382 @ default bufsize */
	while (ptr < last && trash->data <= max_size) {    /* size_t <= int */
		if (ptr) {
			memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data);
			trash->data += args[0].data.str.data;      /* no room re-check */
		}
		else
			max_size -= args[0].data.str.data;         /* int -= size_t -> underflow */
		...
		pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data);
	}

With a separator longer than trash->size - 2, max_size -= separator_len makes max_size negative.
The size_t <= int comparison then converts it to a huge value, the loop never terminates on the bound, and both memcpy and ulltoa walk past the buffer, once per chunk_size group of client input.

The sibling converter sample_conv_be2hex() carries exactly the missing cap (if (2 * (size_t)chunk_size + args[0].data.str.data > trash->size) return 0;), which is why it is not affected.

Do you have an idea how to solve the issue?

Apply the same cap in sample_conv_2dec():

	if ((size_t)args[0].data.str.data > trash->size - 2)
		return 0;

ps. this is ai suggestion

What is your configuration?
global
    nbthread 1
    tune.bufsize 4096
defaults
    mode http
    timeout client 5s
    timeout server 5s
frontend fe
    bind 127.0.0.1:18099
    http-request return status 200 content-type text/plain lf-string "OUT[%[req.hdr(X),be2dec(SSSS...(4400 bytes)...SSSS,8,1)]]"



$ ./haproxy -vv
HAProxy version 3.5-dev13-711a04-73 2026/11/21 - https://haproxy.org/
...
  TARGET  = linux-glibc
...
Built with gcc compiler version 15.2.0
Output of haproxy -vv
$ haproxy -vv
HAProxy version 3.2.25-0ubuntu0.26.04.2 2026/10/27 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2030.
Known bugs: http://www.haproxy.org/bugs/bugs-3.2.25.html
Running on: Linux 7.0.0-44-generic #44-Ubuntu SMP PREEMPT_DYNAMIC Fri Nov  6 02:38:04 UTC 2026 x86_64
Build options :
  TARGET  = linux-glibc
  CC      = x86_64-linux-gnu-gcc
  CFLAGS  = -O2 -g -fwrapv -fvect-cost-model=very-cheap -g -O2 -Werror=implicit-function-declaration -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/build/haproxy-6GEqA2/haproxy-3.2.25=/usr/src/haproxy-3.2.25-0ubuntu0.26.04.2 -Wdate-time -D_FORTIFY_SOURCE=3
  OPTIONS = USE_OPENSSL=1 USE_LUA=1 USE_SLZ=1 USE_QUIC=1 USE_PROMEX=1 USE_PCRE2=1 USE_PCRE2_JIT=1
  DEBUG   =

Feature list : -51DEGREES +ACCEPT4 +BACKTRACE -CLOSEFROM +CPU_AFFINITY +CRYPT_H -DEVICEATLAS +DL -ENGINE +EPOLL -EVPORTS +GETADDRINFO -KQUEUE -LIBATOMIC +LIBCRYPT +LINUX_CAP +LINUX_SPLICE +LINUX_TPROXY +LUA +MATH -MEMORY_PROFILING +NETFILTER +NS -OBSOLETE_LINKER +OPENSSL -OPENSSL_AWSLC -OPENSSL_WOLFSSL -OT -PCRE +PCRE2 +PCRE2_JIT -PCRE_JIT +POLL +PRCTL -PROCCTL -PROMEX -PTHREAD_EMULATION +QUIC -QUIC_OPENSSL_COMPAT +RT +SLZ +SSL -STATIC_PCRE -STATIC_PCRE2 +TFO +THREAD +THREAD_DUMP +TPROXY -WURFL -ZLIB +ACME

Default settings :
  bufsize = 16384, maxrewrite = 1024, maxpollevents = 200

Built with multi-threading support (MAX_TGROUPS=32, MAX_THREADS=1024, default=22).
Built with SSL library version : OpenSSL 3.5.5 27 Jan 2026
Running on SSL library version : OpenSSL 3.5.5 27 Jan 2026
SSL library supports TLS extensions : yes
SSL library supports SNI : yes
SSL library supports : TLSv1.0 TLSv1.1 TLSv1.2 TLSv1.3
OpenSSL providers loaded : default
QUIC: connection socket-owner mode support : yes
QUIC: GSO emission support : yes
Built with Lua version : Lua 5.4.8
Built with the Prometheus exporter as a service
Built with network namespace support.
Built with libslz for stateless compression.
Compression algorithms supported : identity("identity"), deflate("deflate"), raw-deflate("deflate"), gzip("gzip")
Built with transparent proxy support using: IP_TRANSPARENT IPV6_TRANSPARENT IP_FREEBIND
Built with PCRE2 version : 10.46 2025-08-27
PCRE2 library supports JIT : yes
Encrypted password support via crypt(3): yes
Built with gcc compiler version 15.2.0

Available polling systems :
      epoll : pref=300,  test result OK
       poll : pref=200,  test result OK
      select : pref=150,  test result OK
Total: 3 (3 usable), will use epoll.

Available multiplexer protocols :
(protocols marked as <default> cannot be specified using 'proto' keyword)
       quic : mode=HTTP  side=FE     mux=QUIC  flags=HTX|NO_UPG|FRAMED
         h2 : mode=HTTP  side=FE|BE  mux=H2    flags=HTX|HOL_RISK|NO_UPG
         h1 : mode=HTTP  side=FE|BE  mux=H1    flags=HTX|NO_UPG
  <default> : mode=HTTP  side=FE|BE  mux=H1    flags=HTX
       fcgi : mode=HTTP  side=BE     mux=FCGI  flags=HTX|HOL_RISK|NO_UPG
       spop : mode=SPOP  side=BE     mux=SPOP  flags=HOL_RISK|NO_UPG
  <default> : mode=SPOP  side=BE     mux=SPOP  flags=HOL_RISK|NO_UPG
       none : mode=TCP   side=FE|BE  mux=PASS  flags=NO_UPG
  <default> : mode=TCP   side=FE|BE  mux=PASS  flags=

Available services : prometheus-exporter
Available filters :
	[BWLIM] bwlim-in
	[BWLIM] bwlim-out
	[CACHE] cache
	[COMP] compression
	[FCGI] fcgi-app
	[SPOE] spoe
	[TRACE] trace

$ uname -a
Linux zuebyir-ThinkPad-PF5DX58M 7.0.0-44-generic #44-Ubuntu SMP PREEMPT_DYNAMIC Fri Nov  6 02:38:04 UTC 2026 x86_64 GNU/Linux
Last Outputs and Backtraces
==412726==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7d06babe5500
==412726==WRITE of size 4400 at 0x7d06babe5500 thread T0
    #0 memcpy
    #1 sample_conv_2dec.isra.0 src/sample.c
    #2 sample_process_cnv src/sample.c
    #3 sample_process src/sample.c
    #4 sess_build_logline_orig src/log.c
    #5 http_reply_to_htx src/http_ana.c
    #6 http_action_return src/http_act.c
    #7 http_req_get_intercept_rule src/http_ana.c
    #8 http_process_req_common src/http_ana.c
    #9 process_stream src/stream.c
Additional Information
  • Both be2dec and le2dec are affected (shared sample_conv_2dec()); be2hex/le2hex are not.
  • No information leak: the converter overwrites the whole region its oversized sample later claims, and the output can never fit the response buffer.
  • Disclosure: initially identified with the help of an AI code-audit agent, then manually verified end-to-end by me on 711a049f6.
主要語言
C
星號
6.9k
分支
976
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

haproxy/haproxy 的其他 Issue

查看 haproxy/haproxy 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。