GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — vfs: do not reuse the path of a loaded memfd image by pipobscure · Pull Request #67068 · nodejs/node Conversation
|
|
Review requested:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@ ## main #67068 +/- ## ========================================== - Coverage 92.62% 90.72% -1.90% ========================================== Files 430 798 +368 Lines 196333 280089 +83756 Branches 29897 53881 +23984 ========================================== + Hits 181849 254118 +72269 - Misses 14159 16261 +2102 - Partials 325 9710 +9385
🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Do we have to leak here? Can we just close after uv_dlclose()?
There is an expectation that Node.js instances clean up file descriptors opened by them unless explicitly told otherwise.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
You might have a point. I'm reworking it with a slightly better idea that makes sure we don't leak. uv_dlclode() doesn't solve the problem, since that would just lead to reused Hold for changes comint |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
You're right, we didn't have to leak, and I've pushed a fix. The memfd now closes right after dlopen(), so no fd outlives a load, whether it's ffi.close(), worker exit or a failed load. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a good permanent solution tbh, but I don't think it hurts either right now
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| static Mutex mutex; | ||
| static int next_number = 0; | ||
| Mutex::ScopedLock lock(mutex); | ||
| const int moved = fcntl(fd, F_DUPFD_CLOEXEC, next_number); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This will reliably start to break once the fd number reaches the EMFILE limit (ulimit -n) – it doesn't matter how many file descriptors are actually open, the resource limit limits the maximum fd value.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
True. Do you have any idea how to solve that though? I'd actually like to solve this in a solid permanent way, so if you have any ideas I'd very much appreciate it!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yeah no, if something had come to mind, I'd have mentioned it. The real bug here is the dynamic loader's behavior, imo, but I also realize it's obviously a wide-impact and slow-moving change if one were to tackle it there.
I've done a bit of testing though, and it seems there's a ridiculous solution that might just work – while glibc dlopen caches by both path and inode (and at least the latter seems to be spec-mandated), you can get it to work for distinct files by using denormalized path representations – i.e. if the file has been replaced in between, dlopen("/foo"), dlopen("//foo"), dlopen("/./foo"), dlopen("//./foo") etc. will actually give you different objects.
So, and I know this sounds messed up at first, but we could probably just keep a counter, and when loading a temp object, use its binary translation to translate 1s to / and (non-leading) 0s to /. and then go off and load /proc/self/fd/42, //proc/self/fd/42, //./proc/self/fd/42, ///proc/self/fd/42, //././proc/self/fd/42, etc. With the standard Linux path length restriction of 4096, that should give us at least 2^2039 combinations.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
🤣 The funny thing is that's pretty much what I had tried first and discarded as way too hacky, they'll never go for that.😂
What do you think of what I tried now, to just check on load. It seems a good compromise to me, but then 🤷
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's way too hacky to be considered good solution, but I don't think we'd need to worry about glibc's behavior changing too quickly.
But yes, the current approach probably works a little better 🙂
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Given the valid criticisms that @addaleax raised, and the fact that she has more than a point I'd like to solve this in a cleaner way. If anyone has any ideas I'd appreciate hearing them.
Thoughts very much appreciated! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
You were right about the counter: it used up a number on every load, even for libraries that had been closed, so a loop would reliably reach ulimit -n. I've pushed a replacement in 21cb9f5. A number only has to differ from those of objects that are still loaded, and the loader can tell us which those are. So before each load we call dlopen(path, RTLD_NOLOAD) on /proc/self/fd/N, and if the path is taken we move the fd up with F_DUPFD_CLOEXEC and check again. There's no counter or lock, because holding the fd from the check through to the load stops anything else taking that path in between. The fd still closes right after dlopen(), so none outlives a load. A library that has really unloaded gives its number back, so the limit now depends on how many images are loaded at once, not how many have ever been loaded. With ulimit -n 128 and no usable temp dir (so memfd only), 512 FFI open and close cycles pass. In a small C test at a limit of 64, 100,000 load and unload cycles never went above fd 3. It can still run out, but only with roughly ulimit -n images loaded at the same time, including libraries dlclose() can't unload and main-thread addons. At that point it falls back to a temp file, which won't work if temp is noexec or read-only. Each load also checks every taken number on its way up, so it gets slower as more images are loaded: about 0.7 ms with 500 and 10 ms with 2,000. I looked for a way to avoid the path match altogether, but glibc has no dlopen flag for it and no fdlopen. dlmopen namespaces gave out after 13 in my test and each loads its own libc. Temp files everywhere aren't an option either, since Docker's tmpfs mounts are noexec by default and --read-only leaves /tmp unwritable. On musl, which doesn't match by path, the same logic ran correctly in a C test (5,000 cycles with no check hits), but I haven't run a musl build of Node. Your approval was for the counter version, so please take another look at this one, particularly if you know of a case where RTLD_NOLOAD wouldn't report a loaded object. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <[email protected]>
21cb9f5 to
5fea2c6
Compare
|
I fixed the racy tests (determination of deleted tmp files) and rebased on main. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Landed in 985ec87 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <[email protected]> PR-URL: #67068 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <[email protected]> PR-URL: #67068 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <[email protected]> PR-URL: #67068 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <[email protected]> PR-URL: #67068 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
On Linux an addon or FFI library inside a VFS is loaded from an anonymous memfd through /proc/self/fd/N, and the memfd was closed right after a successful load. The next image then got the same fd number, hence the same path, and the dynamic loader, which knows a loaded object by the path it was opened with, returned the object already loaded under that path instead of loading the new file. So the second of any two addons or libraries loaded from a VFS resolved to the first: an addon after an FFI library failed with "Module did not self-register", an FFI library after an addon had none of its own symbols, and two addons silently became the same one. The fd is only needed until dlopen() returns, as the load's mapping keeps the memory alive from then on; what must not be shared is the path. Before the load, ask the loader with RTLD_NOLOAD whether the path is already taken, and if so move the fd to a higher number with F_DUPFD_CLOEXEC until it is free. Holding the fd from the check through the load keeps anyone else from taking the path in between, so this needs neither a counter nor a lock, and a library that has really been unloaded gives its number back. The fd is closed right after the load, whether or not it worked. Should no free number be left, the image goes to a temp file. The leak checks of test-vfs-addon and test-ffi-vfs compared the shared temporary directory before and after the load, so off Linux any other test loading from bytes at the same time could pass for a leak. Point TMPDIR at a directory each test owns instead. Signed-off-by: Philipp Dunkel <[email protected]> PR-URL: nodejs#67068 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.