From 22e6d5212b5c90b460bfbf33594652c9849454c7 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sat, 17 Oct 2020 12:20:16 +0100 Subject: [PATCH] spawn: in discrete env setting case do it readonly OSX changed to blow a segfault on write to .rodata, exposing that we're dropping a NUL in what can be .rodata to set the environment manually. We don't do this on Linux typically because we take the code path where execvpe() is available to do the env for us. Adapt the code to treat it as const, and underscore it by changing its type to be const char ** in the info struct. --- include/libwebsockets/lws-misc.h | 2 +- lib/plat/unix/unix-spawn.c | 11 +++++++---- lib/roles/cgi/cgi-server.c | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/include/libwebsockets/lws-misc.h b/include/libwebsockets/lws-misc.h index 073108ad4..d8519d297 100644 --- a/include/libwebsockets/lws-misc.h +++ b/include/libwebsockets/lws-misc.h @@ -895,7 +895,7 @@ struct lws_spawn_piped_info { struct lws *opt_parent; const char * const *exec_array; - char **env_array; + const char **env_array; const char *protocol_name; const char *chroot_path; const char *wd; diff --git a/lib/plat/unix/unix-spawn.c b/lib/plat/unix/unix-spawn.c index 1e3a01e7d..bfa0fc9e3 100644 --- a/lib/plat/unix/unix-spawn.c +++ b/lib/plat/unix/unix-spawn.c @@ -528,16 +528,19 @@ lws_spawn_piped(const struct lws_spawn_piped_info *i) #if defined(__linux__) || defined(__APPLE__) m = 0; while (i->env_array[m]){ - char *p = strchr(i->env_array[m], '='); - *p++ = '\0'; - setenv(i->env_array[m], p, 1); + const char *p = strchr(i->env_array[m], '='); + int naml = lws_ptr_diff(p, i->env_array[m]); + char enam[32]; + + lws_strnncpy(enam, i->env_array[m], naml, sizeof(enam)); + setenv(enam, p, 1); m++; } #endif execvp(i->exec_array[0], (char * const *)&i->exec_array[0]); #else execvpe(i->exec_array[0], (char * const *)&i->exec_array[0], - &i->env_array[0]); + (char **)&i->env_array[0]); #endif lwsl_err("%s: child exec of %s failed %d\n", __func__, i->exec_array[0], diff --git a/lib/roles/cgi/cgi-server.c b/lib/roles/cgi/cgi-server.c index 5a39116d0..1aa2f9cff 100644 --- a/lib/roles/cgi/cgi-server.c +++ b/lib/roles/cgi/cgi-server.c @@ -385,7 +385,7 @@ lws_cgi(struct lws *wsi, const char * const *exec_array, #endif memset(&info, 0, sizeof(info)); - info.env_array = env_array; + info.env_array = (const char **)env_array; info.exec_array = exec_array; info.max_log_lines = 20000; info.opt_parent = wsi;