From 71c63723c89778d8c4886a246c158678d2e427c8 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 18 Feb 2023 20:41:26 +0100 Subject: [PATCH] Fix GH-10611: fpm_env_init_main leaks environ Closes GH-10618. --- NEWS | 3 +++ sapi/fpm/fpm/fpm_env.c | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/NEWS b/NEWS index 1856ad53e31..b684773f976 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,9 @@ PHP NEWS . Fixed bug GH-10801 (Named arguments in CTE functions cause a segfault). (nielsdos) +- FPM: + . Fixed bug GH-10611 (fpm_env_init_main leaks environ). (nielsdos) + - FTP: . Propagate success status of ftp_close(). (nielsdos) diff --git a/sapi/fpm/fpm/fpm_env.c b/sapi/fpm/fpm/fpm_env.c index 72ed1aa94d3..9771a574739 100644 --- a/sapi/fpm/fpm/fpm_env.c +++ b/sapi/fpm/fpm/fpm_env.c @@ -11,6 +11,7 @@ #include "fpm_env.h" #include "fpm.h" +#include "fpm_cleanup.h" #ifndef HAVE_SETPROCTITLE #if defined(__linux__) || defined(__APPLE__) @@ -194,6 +195,26 @@ static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */ } /* }}} */ + +#ifndef HAVE_SETPROCTITLE +#if defined(__linux__) || defined(__APPLE__) +/* Frees our copied environment variables. */ +static void fpm_env_cleanup(int which, void *arg) /* {{{ */ +{ + char** allocated_environ = environ; + if (allocated_environ) { + environ = NULL; + unsigned int i = 0; + while (allocated_environ[i]) { + free(allocated_environ[i]); + i++; + } + free(allocated_environ); + } +} +#endif +#endif + int fpm_env_init_main(void) { struct fpm_worker_pool_s *wp; @@ -254,6 +275,10 @@ int fpm_env_init_main(void) env_nb++; } + if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT_EXIT_MAIN, fpm_env_cleanup, 0)) { + return -1; + } + if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) { return -1; }