Welcome! Log In Create A New Profile

Advanced

[nginx] Core: fixed environment variables on exit.

Sergey Kandaurov
July 19, 2023 04:08AM
details: https://hg.nginx.org/nginx/rev/c209dc4eed17
branches:
changeset: 9134:c209dc4eed17
user: Maxim Dounin <mdounin@mdounin.ru>
date: Wed Jul 19 05:09:23 2023 +0300
description:
Core: fixed environment variables on exit.

Similarly to 6822:c045b4926b2c, environment variables introduced with
the "env" directive (and "NGINX_BPF_MAPS" added by QUIC) are now allocated
via ngx_alloc(), and explicitly freed by a cleanup handler if no longer used.

In collaboration with Sergey Kandaurov.

diffstat:

src/core/nginx.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 51 insertions(+), 2 deletions(-)

diffs (84 lines):

diff -r f91dc350be9f -r c209dc4eed17 src/core/nginx.c
--- a/src/core/nginx.c Wed Jul 12 15:27:35 2023 +0400
+++ b/src/core/nginx.c Wed Jul 19 05:09:23 2023 +0300
@@ -13,6 +13,7 @@
static void ngx_show_version_info(void);
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
static void ngx_cleanup_environment(void *data);
+static void ngx_cleanup_environment_variable(void *data);
static ngx_int_t ngx_get_options(int argc, char *const *argv);
static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);
static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
@@ -518,7 +519,8 @@ ngx_add_inherited_sockets(ngx_cycle_t *c
char **
ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
{
- char **p, **env;
+ char **p, **env, *str;
+ size_t len;
ngx_str_t *var;
ngx_uint_t i, n;
ngx_core_conf_t *ccf;
@@ -600,7 +602,31 @@ tz_found:
for (i = 0; i < ccf->env.nelts; i++) {

if (var[i].data[var[i].len] == '=') {
- env[n++] = (char *) var[i].data;
+
+ if (last) {
+ env[n++] = (char *) var[i].data;
+ continue;
+ }
+
+ cln = ngx_pool_cleanup_add(cycle->pool, 0);
+ if (cln == NULL) {
+ return NULL;
+ }
+
+ len = ngx_strlen(var[i].data) + 1;
+
+ str = ngx_alloc(len, cycle->log);
+ if (str == NULL) {
+ return NULL;
+ }
+
+ ngx_memcpy(str, var[i].data, len);
+
+ cln->handler = ngx_cleanup_environment_variable;
+ cln->data = str;
+
+ env[n++] = str;
+
continue;
}

@@ -645,6 +671,29 @@ ngx_cleanup_environment(void *data)
}


+static void
+ngx_cleanup_environment_variable(void *data)
+{
+ char *var = data;
+
+ char **p;
+
+ for (p = environ; *p; p++) {
+
+ /*
+ * if an environment variable is still used, as it happens on exit,
+ * the only option is to leak it
+ */
+
+ if (*p == var) {
+ return;
+ }
+ }
+
+ ngx_free(var);
+}
+
+
ngx_pid_t
ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
{
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[nginx] Core: fixed environment variables on exit.

Sergey Kandaurov 342 July 19, 2023 04:08AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 276
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready