Repository navigation
fix: crash when the environment is written while another thread reads it - #53491
Conversation
Before glibc 2.41, adding an environment variable reallocates the environ array and frees the old one, so a getenv() on another thread can read freed memory. Chromium's thread pool and system libraries call getenv() at any time (the Linux ASan shards have been crashing in fontconfig's getenv() at startup), and apps add variables through process.env whenever they like. Define setenv(), unsetenv(), putenv() and clearenv() in the executable so they replace glibc's for the whole process. They follow the scheme glibc 2.41 adopted: a published environ array is never freed, and "NAME=value" strings are never freed, as in every glibc version. Replaced arrays are kept on a list so leak checkers do not report them.
gin_helper::CallbackHolderBase is freed either by a V8 weak callback or when gin reports that its isolate is being disposed. A Node.js worker's isolate has no gin::PerIsolateData, so gin never reports it, and V8 does not run weak callbacks when it disposes an isolate. Every native function a worker set up (Electron's bindings are installed in each worker) therefore leaked when the worker exited. Track the holders of isolates without gin::PerIsolateData and free them from a Node.js environment cleanup hook. The hook runs on the worker's thread after its JavaScript has ended and before the isolate is disposed. The environ race fixture now waits for its worker to exit, so LeakSanitizer checks the worker's teardown.
deepak1556
left a comment
There was a problem hiding this comment.
-
I guess we can't make a change where we disallow mutating the native environment after process start and use a snapshotted env backend for JS mutations similar to Node.js https://github.com/nodejs/node/blob/f86053855de1e28e00b725acc7a34293e3b0e911/src/node_env_var.cc#L41-L70 ?
-
Can we add a runtime check that this write shim is a noop for glibc >= 2.41, IIUC we don't need this workaround in 2.41 which has its own atomic read/write operations and since we aren't updating the shared counter https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/getenv.c;h=4634387deb80259f6caf29266bf7b64131d8a840;hb=HEAD#l29 and it could pose a different issue.
-
As a followup, we can drop the per isolate data dependency similar to https://chromium-review.googlesource.com/c/chromium/src/+/8279909 and that should avoid the need for a separate tracking of worker thread callbacks.
|
glibc 2.41 made getenv() safe against concurrent writers itself. Its unsetenv() and clearenv() also bump a private counter that getenv() checks to retry a lookup that raced with a removal. The replacements cannot bump that counter, so on those versions a getenv() could miss a variable that is still set. Check gnu_get_libc_version() on the first call and forward all four functions to glibc's own when it is 2.41 or later.
|
|
Release Notes Persisted
|
|
I was unable to backport this PR to "44-x-y" cleanly; |
|
I was unable to backport this PR to "43-x-y" cleanly; |
|
I was unable to backport this PR to "42-x-y" cleanly; |
|
I have automatically backported this PR to "45-x-y", please check out #53508 |
|
@MarshallOfSound has manually backported this PR to "44-x-y", please check out #53509 |
|
@MarshallOfSound has manually backported this PR to "43-x-y", please check out #53510 |
|
@MarshallOfSound has manually backported this PR to "42-x-y", please check out #53511 |
… it (#53511) fix: crash when the environment is written while another thread reads it (#53491) * fix: crash when the environment is written while another thread reads it Before glibc 2.41, adding an environment variable reallocates the environ array and frees the old one, so a getenv() on another thread can read freed memory. Chromium's thread pool and system libraries call getenv() at any time (the Linux ASan shards have been crashing in fontconfig's getenv() at startup), and apps add variables through process.env whenever they like. Define setenv(), unsetenv(), putenv() and clearenv() in the executable so they replace glibc's for the whole process. They follow the scheme glibc 2.41 adopted: a published environ array is never freed, and "NAME=value" strings are never freed, as in every glibc version. Replaced arrays are kept on a list so leak checkers do not report them. * fix: leak of native function callbacks when a worker thread exits gin_helper::CallbackHolderBase is freed either by a V8 weak callback or when gin reports that its isolate is being disposed. A Node.js worker's isolate has no gin::PerIsolateData, so gin never reports it, and V8 does not run weak callbacks when it disposes an isolate. Every native function a worker set up (Electron's bindings are installed in each worker) therefore leaked when the worker exited. Track the holders of isolates without gin::PerIsolateData and free them from a Node.js environment cleanup hook. The hook runs on the worker's thread after its JavaScript has ended and before the isolate is disposed. The environ race fixture now waits for its worker to exit, so LeakSanitizer checks the worker's teardown. * fix: use glibc's environment writers on glibc 2.41 and later glibc 2.41 made getenv() safe against concurrent writers itself. Its unsetenv() and clearenv() also bump a private counter that getenv() checks to retry a lookup that raced with a removal. The replacements cannot bump that counter, so on those versions a getenv() could miss a variable that is still set. Check gnu_get_libc_version() on the first call and forward all four functions to glibc's own when it is 2.41 or later. (cherry picked from commit fee963b)
… it (#53510) fix: crash when the environment is written while another thread reads it (#53491) * fix: crash when the environment is written while another thread reads it Before glibc 2.41, adding an environment variable reallocates the environ array and frees the old one, so a getenv() on another thread can read freed memory. Chromium's thread pool and system libraries call getenv() at any time (the Linux ASan shards have been crashing in fontconfig's getenv() at startup), and apps add variables through process.env whenever they like. Define setenv(), unsetenv(), putenv() and clearenv() in the executable so they replace glibc's for the whole process. They follow the scheme glibc 2.41 adopted: a published environ array is never freed, and "NAME=value" strings are never freed, as in every glibc version. Replaced arrays are kept on a list so leak checkers do not report them. * fix: leak of native function callbacks when a worker thread exits gin_helper::CallbackHolderBase is freed either by a V8 weak callback or when gin reports that its isolate is being disposed. A Node.js worker's isolate has no gin::PerIsolateData, so gin never reports it, and V8 does not run weak callbacks when it disposes an isolate. Every native function a worker set up (Electron's bindings are installed in each worker) therefore leaked when the worker exited. Track the holders of isolates without gin::PerIsolateData and free them from a Node.js environment cleanup hook. The hook runs on the worker's thread after its JavaScript has ended and before the isolate is disposed. The environ race fixture now waits for its worker to exit, so LeakSanitizer checks the worker's teardown. * fix: use glibc's environment writers on glibc 2.41 and later glibc 2.41 made getenv() safe against concurrent writers itself. Its unsetenv() and clearenv() also bump a private counter that getenv() checks to retry a lookup that raced with a removal. The replacements cannot bump that counter, so on those versions a getenv() could miss a variable that is still set. Check gnu_get_libc_version() on the first call and forward all four functions to glibc's own when it is 2.41 or later. (cherry picked from commit fee963b)
… it (#53509) fix: crash when the environment is written while another thread reads it (#53491) * fix: crash when the environment is written while another thread reads it Before glibc 2.41, adding an environment variable reallocates the environ array and frees the old one, so a getenv() on another thread can read freed memory. Chromium's thread pool and system libraries call getenv() at any time (the Linux ASan shards have been crashing in fontconfig's getenv() at startup), and apps add variables through process.env whenever they like. Define setenv(), unsetenv(), putenv() and clearenv() in the executable so they replace glibc's for the whole process. They follow the scheme glibc 2.41 adopted: a published environ array is never freed, and "NAME=value" strings are never freed, as in every glibc version. Replaced arrays are kept on a list so leak checkers do not report them. * fix: leak of native function callbacks when a worker thread exits gin_helper::CallbackHolderBase is freed either by a V8 weak callback or when gin reports that its isolate is being disposed. A Node.js worker's isolate has no gin::PerIsolateData, so gin never reports it, and V8 does not run weak callbacks when it disposes an isolate. Every native function a worker set up (Electron's bindings are installed in each worker) therefore leaked when the worker exited. Track the holders of isolates without gin::PerIsolateData and free them from a Node.js environment cleanup hook. The hook runs on the worker's thread after its JavaScript has ended and before the isolate is disposed. The environ race fixture now waits for its worker to exit, so LeakSanitizer checks the worker's teardown. * fix: use glibc's environment writers on glibc 2.41 and later glibc 2.41 made getenv() safe against concurrent writers itself. Its unsetenv() and clearenv() also bump a private counter that getenv() checks to retry a lookup that raced with a removal. The replacements cannot bump that counter, so on those versions a getenv() could miss a variable that is still set. Check gnu_get_libc_version() on the first call and forward all four functions to glibc's own when it is 2.41 or later. (cherry picked from commit fee963b)
Description of Change
Before glibc 2.41, adding an environment variable reallocates
environand frees the old array. Agetenv()on another thread can then read freed memory. This is the startup crash the Linux ASan shards keep hitting in fontconfig'sgetenv(): the fontconfig worker scans fonts while the main script adds variables.shell/app/environ_linux.ccdefinessetenv,unsetenv,putenvandclearenvin the executable. Linux executables link with-rdynamic, so shared libraries (GTK, native addons) bind to these too.environarray is never freed. Growing it doubles the capacity, copies the entries and publishes the new array. This is the scheme glibc 2.41 adopted upstream.NAME=valuestrings are never freed and identical ones are reused, as in every glibc version.unsetenvandclearenvbump a private counter thatgetenvuses to retry, which the replacements can't do.os.homedir()(libuv callsgetenv()with no lock) while the main script adds 2000 variables.Results on glibc 2.35, the version CI uses:
getenv()getenv()The extra memory is bounded by the peak variable count, not by the number of writes. Live heap, starting from 40 variables:
macOS's libc has the same class of bug and is not covered here.
The new spec also exposed a leak, fixed in the second commit:
gin::PerIsolateData, so thegin_helper::CallbackHolderBaseobjects created for Electron's bindings in every worker were never freed. V8 does not run weak callbacks when it disposes an isolate.Checklist
npm testpassesRelease Notes
Notes: Fixed a crash on Linux when
process.envwas written while another thread was reading the environment, and a memory leak when a worker thread exits.