src/server.c: a named default-server leaks its name when completed over several lines

Abierto Apto para principiantes
#3,562 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c
Área
backend

Línea de trabajo

El issue identifica _srv_parse_init() en src/server.c como el lugar donde se produce la fuga y reg-tests/server/from_keyword.vtc como ejemplo de valores predeterminados con nombre en varias líneas. Empieza leyendo la búsqueda de nombres existentes y las rutas de ownership en _srv_parse_init(). Confirma que las líneas repetidas default-server name other ya no provocan la fuga reportada y que server s1 ... from srv:other sigue funcionando.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Detailed Description of the Problem

In _srv_parse_init(), a named default-server line does name = strdup(args[2]), then looks the name up in curproxy->defsrv_by_name. When the default-server already exists, name is only stored in a newly allocated server (newsrv->id = name), and the success path frees fqdn but not name. Every line after the first that completes an existing named default-server therefore leaks its name.

The documentation describes this usage ("The same default-server can be defined over multiple lines, with each line completing the previous one"), and reg-tests/server/from_keyword.vtc does it four times. The leak was introduced with named default-servers in 507f790af ("MEDIUM: proxy: implement named default-server"), so it is in 3.5-dev only. It only happens at config load.

Expected Behavior

No leak when a named default-server is completed over several lines.

Steps to Reproduce the Behavior
  1. Build with ARCH_FLAGS="-fsanitize=address".
  2. Run ASAN_OPTIONS=detect_leaks=1 ./haproxy -c -f t3.cfg with the configuration below.
  3. LeakSanitizer reports 18 bytes in 3 objects ("other" plus its terminator, for each of lines 2-4) allocated by strdup in _srv_parse_init. With a single default-server name other line nothing is reported.
Do you have any idea what may have caused this?

The name is duplicated before the lookup, and only the new-server branch takes ownership of it.

Do you have an idea how to solve the issue?

Free the name when the lookup finds an existing default-server (tested: no leak is reported, and server s1 ... from srv:other still works):

diff --git a/src/server.c b/src/server.c
index 10659b8ad..afbe589e0 100644
--- a/src/server.c
+++ b/src/server.c
@@ -3962,6 +3962,9 @@ static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
 			newsrv = cebuis_item_lookup(&curproxy->defsrv_by_name,
 			                            conf.name_node, id, name,
 			                            struct server);
+			/* an existing default-server already owns its name */
+			if (newsrv)
+				ha_free(&name);
 		}
 		else {
 			/* unnamed default-server instance */
What is your configuration?
defaults
  mode http
  timeout client 1s
  timeout server 1s
  timeout connect 1s
backend be
  default-server name other weight 1
  default-server name other weight 2
  default-server name other weight 3
  default-server name other weight 4
  server s1 127.0.0.1:8080 from srv:other
Output of haproxy -vv
HAProxy version 3.5-dev11 2026/11/04 - https://haproxy.org/
Status: development branch - not safe for use in production.
Known bugs: https://github.com/haproxy/haproxy/issues?q=is:issue+is:open
Running on: Linux 7.0.0-38-generic #38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC Mon Sep 14 16:37:11 UTC 2 x86_64
Build options : 
  TARGET  = linux-glibc
  CC      = cc
  CFLAGS  = -O2 -g -O1 -g -fno-omit-frame-pointer -fwrapv -fvect-cost-model=very-cheap
  OPTIONS = USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE2=1
  DEBUG   = 

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

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

Built with multi-threading support (MAX_TGROUPS=32, MAX_THREADS=1024, default=4).
Built with SSL library version : OpenSSL 3.0.13 30 Jan 2024
Running on SSL library version : OpenSSL 3.0.13 30 Jan 2024
SSL library supports TLS extensions : yes
SSL library supports SNI : yes
SSL library FIPS mode : no
SSL library default verify directory : /usr/lib/ssl/certs
SSL library supports : TLSv1.0 TLSv1.1 TLSv1.2 TLSv1.3
OpenSSL providers loaded : default
Built with network namespace support.
Built with zlib version : 1.3
Running on zlib version : 1.3
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.42 2022-12-11
PCRE2 library supports JIT : no (USE_PCRE2_JIT not set)
Encrypted password support via crypt(3): yes
Built with gcc compiler version 13.3.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)
         h2 : mode=HTTP  side=FE|BE  mux=H2    flags=HTX|HOL_RISK|NO_UPG
       fcgi : mode=HTTP  side=BE     mux=FCGI  flags=HTX|HOL_RISK|NO_UPG
  <default> : mode=SPOP  side=BE     mux=SPOP  flags=HOL_RISK|NO_UPG
       spop : mode=SPOP  side=BE     mux=SPOP  flags=HOL_RISK|NO_UPG
  <default> : mode=HTTP  side=FE|BE  mux=H1    flags=HTX
         h1 : mode=HTTP  side=FE|BE  mux=H1    flags=HTX|NO_UPG
  <default> : mode=TCP   side=FE|BE  mux=PASS  flags=
       none : mode=TCP   side=FE|BE  mux=PASS  flags=NO_UPG

Available services : none

Available filter classes :
	trace, cache-store, decomp-req, decomp-res, spoe, http-comp-req, http-comp-res, bwlim-in, bwlim-out, fcgi
Last Outputs and Backtraces
Direct leak of 18 byte(s) in 3 object(s) allocated from:
    #0 in strdup asan_interceptors.cpp:578
    #1 in _srv_parse_init
    #2 in parse_server
    #3 in proxy_parse_server
    #4 in cfg_parse_listen
    #5 in parse_cfg
    #6 in read_cfg
    #7 in main
SUMMARY: AddressSanitizer: 18 byte(s) leaked in 3 allocation(s).
Additional Information

Found with the clang static analyzer (unix.Malloc) while analysing ce79f339c, then reproduced with ASan/LeakSanitizer and the patch tested the same way. The analysis and this report were prepared with AI assistance (Claude), and every step above was run and checked.

Lenguaje dominante
C
Estrellas
6.9k
Forks
976
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de haproxy/haproxy

Todos los issues de haproxy/haproxy

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.