HTTP wrapper leaks dechunk filter when header parsing fails after Transfer-Encoding: chunked
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 78/100
- Tipo de issue
- Bug
- Clareza
- Claramente especificada
- Status de atividade
- Ativa
- Domínio
- backend, networking
Direção de pesquisa
Comece em ext/standard/http_fopen_wrapper.c, na análise dos cabeçalhos da resposta e no caminho de limpeza out:; a issue indica onde o filtro dechunk é criado e onde a limpeza o ignora após um cabeçalho malformado. Reproduza o problema com os comandos server.php e client.php fornecidos e, em seguida, confira o relatório de vazamentos da build de depuração. O trabalho estará concluído quando a requisição com falha não reportar vazamentos e uma resposta chunked bem formada continuar funcionando.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Description
The following code:
server.php (run first, php server.php):
<?php
$srv = stream_socket_server("tcp://127.0.0.1:18231", $errno, $errstr);
$conn = stream_socket_accept($srv);
while (($line = fgets($conn, 8192)) !== false) { if (trim($line) === '') break; }
// Transfer-Encoding: chunked, then a malformed header line
fwrite($conn, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nBrokenHeaderNoColon\r\n\r\n");
fclose($conn);
client.php (debug build, php -d report_memleaks=1 client.php):
<?php
@file_get_contents("http://127.0.0.1:18231/");
Resulted in this output:
ext/standard/filters.c(1885) : Freeing 0x... (16 bytes), script=.../client.php
main/streams/filter.c(267) : Freeing 0x... (80 bytes), script=.../client.php
=== Total 2 memory leaks detected ===
But I expected this output instead:
(no leaks reported)
Root cause: when the response carries Transfer-Encoding: chunked, the wrapper creates a dechunk filter while parsing headers (php_stream_filter_create, ext/standard/http_fopen_wrapper.c:379 in 8.5.12, :366 on master). If a later header line is malformed, header_info.error is set, the caller closes the stream and jumps to out: (master :1052-1058). The out: cleanup only appends the filter to the stream's readfilters when stream != NULL (master :1257-1259), so on the error path the filter is never freed. Per failed request this leaks the php_stream_filter struct (80 bytes) plus the dechunk filter data (16 bytes).
Reproduces on current master (8.7.0-dev) as well.
Suggested patch (verified against master: repro reports 0 leaks after patching; well-formed chunked responses unaffected):
--- a/ext/standard/http_fopen_wrapper.c
+++ b/ext/standard/http_fopen_wrapper.c
@@ -1230,6 +1230,13 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
efree(header_info.location);
}
+ /* On the error path stream is NULL and the filter is never appended
+ * to readfilters below, so it must be freed here. */
+ if (header_info.transfer_encoding != NULL && stream == NULL) {
+ php_stream_filter_free(header_info.transfer_encoding);
+ header_info.transfer_encoding = NULL;
+ }
+
if (resource) {
php_uri_struct_free(resource);
}
PHP Version
PHP 8.5.12 (also reproduces on master, 8.7.0-dev)
Operating System
Linux x86_64
- Linguagem predominante
- C
- Estrelas
- 40.7k
- Forks
- 8.3k
- Merge médio
- 17h 29min
- PRs com merge (30d)
- 10
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de php/php-src
-
Case-insensitive substr_compare, ksort, krsort are still locale-dependentTalvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. AbertaBug Status: Needs Triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 73/100
php/php-src#24552 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Bug Status: Needs Triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 1 dia
-
Bug Status: Needs Triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 71/100
php/php-src#24505 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
-
master: part of the mbstring bailout fix got lost in a mergeTalvez já em andamento @lazerg assumiu há 1 dia. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 1 dia
-
Bug Status: Needs Triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 64/100
Mantenedores costumam responder em até 1 dia
Todas as issues de php/php-src
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 66/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
MerginMaps/geodiff#272 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
open-quantum-safe/liboqs#2644 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
espressif/esp-adf-libs#89 · 1 comentário ·
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 88/100
Mantenedores costumam responder em até 2 dias