1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 18:31:49 +02:00

Merge remote-tracking branch 'origin/backport-8483-to-2.13-maintenance' into 2.13-maintenance

This commit is contained in:
Eelco Dolstra 2023-08-07 16:43:57 +02:00
commit 01401ffffd

View file

@ -1736,6 +1736,7 @@ void setStackSize(size_t stackSize)
#if __linux__ #if __linux__
static AutoCloseFD fdSavedMountNamespace; static AutoCloseFD fdSavedMountNamespace;
static AutoCloseFD fdSavedRoot;
#endif #endif
void saveMountNamespace() void saveMountNamespace()
@ -1743,10 +1744,11 @@ void saveMountNamespace()
#if __linux__ #if __linux__
static std::once_flag done; static std::once_flag done;
std::call_once(done, []() { std::call_once(done, []() {
AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY); fdSavedMountNamespace = open("/proc/self/ns/mnt", O_RDONLY);
if (!fd) if (!fdSavedMountNamespace)
throw SysError("saving parent mount namespace"); throw SysError("saving parent mount namespace");
fdSavedMountNamespace = std::move(fd);
fdSavedRoot = open("/proc/self/root", O_RDONLY);
}); });
#endif #endif
} }
@ -1759,9 +1761,16 @@ void restoreMountNamespace()
if (fdSavedMountNamespace && setns(fdSavedMountNamespace.get(), CLONE_NEWNS) == -1) if (fdSavedMountNamespace && setns(fdSavedMountNamespace.get(), CLONE_NEWNS) == -1)
throw SysError("restoring parent mount namespace"); throw SysError("restoring parent mount namespace");
if (chdir(savedCwd.c_str()) == -1) {
throw SysError("restoring cwd"); if (fdSavedRoot) {
if (fchdir(fdSavedRoot.get()))
throw SysError("chdir into saved root");
if (chroot("."))
throw SysError("chroot into saved root");
} }
if (chdir(savedCwd.c_str()) == -1)
throw SysError("restoring cwd");
} catch (Error & e) { } catch (Error & e) {
debug(e.msg()); debug(e.msg());
} }