You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by jp...@apache.org on 2017/11/15 17:45:29 UTC

[3/3] mesos git commit: Improved the signal safety of `ns::clone`.

Improved the signal safety of `ns::clone`.

`ns::clone` was implicitly using malloc to allocate a stack
after fork but before exec, where we should be avoiding
`malloc`.

Updated the second clone to use `os::signal_safe::clone`. We
still explicitly allocate a stack after the fork, but since
that is allocated with `mmap` is it safe in this particular
case.

Review: https://reviews.apache.org/r/63678/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f1c861de
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f1c861de
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f1c861de

Branch: refs/heads/master
Commit: f1c861de652ad1dccff67e1b0d1ba97d52f6b00e
Parents: 49f7686
Author: James Peach <jp...@apache.org>
Authored: Wed Nov 15 07:59:02 2017 -0800
Committer: James Peach <jp...@apache.org>
Committed: Wed Nov 15 07:59:02 2017 -0800

----------------------------------------------------------------------
 src/linux/ns.cpp | 64 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f1c861de/src/linux/ns.cpp
----------------------------------------------------------------------
diff --git a/src/linux/ns.cpp b/src/linux/ns.cpp
index 33751b5..5e2df1e 100644
--- a/src/linux/ns.cpp
+++ b/src/linux/ns.cpp
@@ -223,6 +223,12 @@ Try<pid_t> clone(
     {CLONE_NEWNS, "mnt"}
   };
 
+  // Since we assume below that the parent can deallocate the stack
+  // after cloning the children, the caller must not pass CLONE_VM.
+  // That would cause the both processes to share their address space
+  // so deallocating the stack in the parent would affect the child.
+  CHECK_EQ(0, flags & CLONE_VM);
+
   // Support for user namespaces in all filesystems is incomplete
   // until version 3.12 (see 'Availability' in man page of
   // 'user_namespaces'), so for now we don't support entering them.
@@ -443,27 +449,31 @@ Try<pid_t> clone(
       //
       // Now clone with the specified flags, close the unused socket,
       // and execute the specified function.
-      pid_t pid = os::clone([=]() {
-        // Now send back the pid and have it be translated appropriately
-        // by the kernel to the enclosing pid namespace.
-        //
-        // NOTE: sending back the pid is best effort because we're going
-        // to exit no matter what.
-        ((struct ucred*) CMSG_DATA(CMSG_FIRSTHDR(&message)))->pid = ::getpid();
-        ((struct ucred*) CMSG_DATA(CMSG_FIRSTHDR(&message)))->uid = ::getuid();
-        ((struct ucred*) CMSG_DATA(CMSG_FIRSTHDR(&message)))->gid = ::getgid();
-
-        if (sendmsg(sockets[1], &message, 0) == -1) {
-          // Failed to send the pid back to the parent!
-          _exit(EXIT_FAILURE);
-        }
-
-        ::close(sockets[1]);
-
-        return f();
-      },
-      flags,
-      stack.get());
+      pid_t pid = os::signal_safe::clone(
+          stack.get(),
+          flags,
+          [=]() {
+            struct ucred* cred = reinterpret_cast<struct ucred*>(
+                CMSG_DATA(CMSG_FIRSTHDR(&message)));
+
+            // Now send back the pid and have it be translated appropriately
+            // by the kernel to the enclosing pid namespace.
+            //
+            // NOTE: sending back the pid is best effort because we're going
+            // to exit no matter what.
+            cred->pid = ::getpid();
+            cred->uid = ::getuid();
+            cred->gid = ::getgid();
+
+            if (sendmsg(sockets[1], &message, 0) == -1) {
+              // Failed to send the pid back to the parent!
+              _exit(EXIT_FAILURE);
+            }
+
+            ::close(sockets[1]);
+
+            return f();
+          });
 
       ::close(sockets[1]);
 
@@ -473,6 +483,13 @@ Try<pid_t> clone(
       UNREACHABLE();
     };
 
+    os::Stack grandchildStack(os::Stack::DEFAULT_SIZE);
+
+    if (!grandchildStack.allocate()) {
+      ::close(sockets[1]);
+      _exit(EXIT_FAILURE);
+    }
+
     // Fork again to make sure we're actually in those namespaces
     // (required for the pid namespace at least).
     //
@@ -489,7 +506,10 @@ Try<pid_t> clone(
     //
     // TODO(benh): Don't do a fork if we're not actually entering the
     // PID namespace since the extra fork is unnecessary.
-    pid_t grandchild = os::clone(grandchildMain, SIGCHLD);
+    pid_t grandchild =
+      os::signal_safe::clone(grandchildStack, SIGCHLD, grandchildMain);
+
+    grandchildStack.deallocate();
 
     if (grandchild < 0) {
       // TODO(benh): Exit with `errno` in order to capture `fork` error?