Fix segfault in builtin fetchurl

The stack allocated for the builder was way too small (32 KB). This is
sufficient for normal derivations, because they just do some setup and
then exec() the actual builder. But for the fetchurl builtin
derivation it's not enough. Also, allocating the stack on the caller's
stack was fishy business.
This commit is contained in:
Eelco Dolstra 2015-10-21 14:45:56 +02:00
parent 133a421bb4
commit 357d31b339

View file

@ -74,6 +74,7 @@
#if __linux__ #if __linux__
#include <sys/personality.h> #include <sys/personality.h>
#include <sys/mman.h>
#endif #endif
#if HAVE_STATVFS #if HAVE_STATVFS
@ -2128,14 +2129,17 @@ void DerivationGoal::startBuilder()
ProcessOptions options; ProcessOptions options;
options.allowVfork = false; options.allowVfork = false;
Pid helper = startProcess([&]() { Pid helper = startProcess([&]() {
char stack[32 * 1024]; size_t stackSize = 1 * 1024 * 1024;
char * stack = (char *) mmap(0, stackSize,
PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (!stack) throw SysError("allocating stack");
int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | CLONE_NEWUTS | CLONE_PARENT | SIGCHLD; int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | CLONE_NEWUTS | CLONE_PARENT | SIGCHLD;
if (!fixedOutput) flags |= CLONE_NEWNET; if (!fixedOutput) flags |= CLONE_NEWNET;
pid_t child = clone(childEntry, stack + sizeof(stack) - 8, flags, this); pid_t child = clone(childEntry, stack + stackSize, flags, this);
if (child == -1 && errno == EINVAL) if (child == -1 && errno == EINVAL)
/* Fallback for Linux < 2.13 where CLONE_NEWPID and /* Fallback for Linux < 2.13 where CLONE_NEWPID and
CLONE_PARENT are not allowed together. */ CLONE_PARENT are not allowed together. */
child = clone(childEntry, stack + sizeof(stack) - 8, flags & ~CLONE_NEWPID, this); child = clone(childEntry, stack + stackSize, flags & ~CLONE_NEWPID, this);
if (child == -1) throw SysError("cloning builder process"); if (child == -1) throw SysError("cloning builder process");
writeFull(builderOut.writeSide, int2String(child) + "\n"); writeFull(builderOut.writeSide, int2String(child) + "\n");
_exit(0); _exit(0);