Skip to content

Commit 6765c88

Browse files
bnoordhuissam-github
authored andcommitted
src: make copies of startup environment variables
Mutations of the environment can invalidate pointers to environment variables, so make `secure_getenv()` copy them out instead of returning pointers. PR-URL: nodejs#11051 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sam Roberts <[email protected]>
1 parent c60fd7f commit 6765c88

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

src/node.cc

+27-16
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static node_module* modlist_addon;
165165

166166
#if defined(NODE_HAVE_I18N_SUPPORT)
167167
// Path to ICU data (for i18n / Intl)
168-
static const char* icu_data_dir = nullptr;
168+
static std::string icu_data_dir; // NOLINT(runtime/string)
169169
#endif
170170

171171
// used by C++ modules as well
@@ -943,12 +943,21 @@ Local<Value> UVException(Isolate* isolate,
943943

944944

945945
// Look up environment variable unless running as setuid root.
946-
inline const char* secure_getenv(const char* key) {
946+
inline bool SafeGetenv(const char* key, std::string* text) {
947947
#ifndef _WIN32
948-
if (getuid() != geteuid() || getgid() != getegid())
949-
return nullptr;
948+
// TODO(bnoordhuis) Should perhaps also check whether getauxval(AT_SECURE)
949+
// is non-zero on Linux.
950+
if (getuid() != geteuid() || getgid() != getegid()) {
951+
text->clear();
952+
return false;
953+
}
950954
#endif
951-
return getenv(key);
955+
if (const char* value = getenv(key)) {
956+
*text = value;
957+
return true;
958+
}
959+
text->clear();
960+
return false;
952961
}
953962

954963

@@ -3131,11 +3140,11 @@ void SetupProcessObject(Environment* env,
31313140
"icu",
31323141
OneByteString(env->isolate(), U_ICU_VERSION));
31333142

3134-
if (icu_data_dir != nullptr) {
3143+
if (!icu_data_dir.empty()) {
31353144
// Did the user attempt (via env var or parameter) to set an ICU path?
31363145
READONLY_PROPERTY(process,
31373146
"icu_data_dir",
3138-
OneByteString(env->isolate(), icu_data_dir));
3147+
OneByteString(env->isolate(), icu_data_dir.c_str()));
31393148
}
31403149
#endif
31413150

@@ -3850,7 +3859,7 @@ static void ParseArgs(int* argc,
38503859
#endif /* HAVE_OPENSSL */
38513860
#if defined(NODE_HAVE_I18N_SUPPORT)
38523861
} else if (strncmp(arg, "--icu-data-dir=", 15) == 0) {
3853-
icu_data_dir = arg + 15;
3862+
icu_data_dir.assign(arg + 15);
38543863
#endif
38553864
} else if (strcmp(arg, "--expose-internals") == 0 ||
38563865
strcmp(arg, "--expose_internals") == 0) {
@@ -4351,12 +4360,11 @@ void Init(int* argc,
43514360
#endif
43524361

43534362
#if defined(NODE_HAVE_I18N_SUPPORT)
4354-
if (icu_data_dir == nullptr) {
4355-
// if the parameter isn't given, use the env variable.
4356-
icu_data_dir = secure_getenv("NODE_ICU_DATA");
4357-
}
4363+
// If the parameter isn't given, use the env variable.
4364+
if (icu_data_dir.empty())
4365+
SafeGetenv("NODE_ICU_DATA", &icu_data_dir);
43584366
// Initialize ICU.
4359-
// If icu_data_dir is nullptr here, it will load the 'minimal' data.
4367+
// If icu_data_dir is empty here, it will load the 'minimal' data.
43604368
if (!i18n::InitializeICUDirectory(icu_data_dir)) {
43614369
FatalError(nullptr, "Could not initialize ICU "
43624370
"(check NODE_ICU_DATA or --icu-data-dir parameters)");
@@ -4707,8 +4715,11 @@ int Start(int argc, char** argv) {
47074715
Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
47084716

47094717
#if HAVE_OPENSSL
4710-
if (const char* extra = secure_getenv("NODE_EXTRA_CA_CERTS"))
4711-
crypto::UseExtraCaCerts(extra);
4718+
{
4719+
std::string extra_ca_certs;
4720+
if (SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs))
4721+
crypto::UseExtraCaCerts(extra_ca_certs);
4722+
}
47124723
#ifdef NODE_FIPS_MODE
47134724
// In the case of FIPS builds we should make sure
47144725
// the random source is properly initialized first.
@@ -4717,7 +4728,7 @@ int Start(int argc, char** argv) {
47174728
// V8 on Windows doesn't have a good source of entropy. Seed it from
47184729
// OpenSSL's pool.
47194730
V8::SetEntropySource(crypto::EntropySource);
4720-
#endif
4731+
#endif // HAVE_OPENSSL
47214732

47224733
v8_platform.Initialize(v8_thread_pool_size);
47234734
V8::Initialize();

src/node_i18n.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ bool flag_icu_data_dir = false;
6262

6363
namespace i18n {
6464

65-
bool InitializeICUDirectory(const char* icu_data_path) {
66-
if (icu_data_path != nullptr) {
65+
bool InitializeICUDirectory(const std::string& path) {
66+
if (!path.empty()) {
6767
flag_icu_data_dir = true;
68-
u_setDataDirectory(icu_data_path);
68+
u_setDataDirectory(path.c_str());
6969
return true; // no error
7070
} else {
7171
UErrorCode status = U_ZERO_ERROR;

src/node_i18n.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "node.h"
7+
#include <string>
78

89
#if defined(NODE_HAVE_I18N_SUPPORT)
910

@@ -13,7 +14,7 @@ extern bool flag_icu_data_dir;
1314

1415
namespace i18n {
1516

16-
bool InitializeICUDirectory(const char* icu_data_path);
17+
bool InitializeICUDirectory(const std::string& path);
1718

1819
} // namespace i18n
1920
} // namespace node

src/node_internals.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <stdint.h>
1313
#include <stdlib.h>
1414

15+
#include <string>
16+
1517
struct sockaddr;
1618

1719
// Variation on NODE_DEFINE_CONSTANT that sets a String value.

0 commit comments

Comments
 (0)