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

[01/13] mesos git commit: Add ambient capability support.

Repository: mesos
Updated Branches:
  refs/heads/master 024336ac0 -> 35dd2b600


Add ambient capability support.

Add support for the ambient capability so that we can make
effective capabilities survive across execve(2).

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


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

Branch: refs/heads/master
Commit: 4ee86647a45d09528124e9ab0fa732758ac4d7ec
Parents: e70ef31
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:59 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 src/linux/capabilities.cpp                     | 114 +++++++++++++++++---
 src/linux/capabilities.hpp                     |  23 +++-
 src/tests/containerizer/capabilities_tests.cpp |  80 ++++++++++++++
 3 files changed, 201 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/4ee86647/src/linux/capabilities.cpp
----------------------------------------------------------------------
diff --git a/src/linux/capabilities.cpp b/src/linux/capabilities.cpp
index 7aa8c35..b362759 100644
--- a/src/linux/capabilities.cpp
+++ b/src/linux/capabilities.cpp
@@ -30,6 +30,24 @@
 
 #include "linux/capabilities.hpp"
 
+// Vendor the prctl constants for ambient capabilities so that we can build
+// on systems with old glibc but still work on modern kernels.
+#if !defined(PR_CAP_AMBIENT)
+#define PR_CAP_AMBIENT 47
+#endif
+
+#if !defined(PR_CAP_AMBIENT_IS_SET)
+#define PR_CAP_AMBIENT_IS_SET 1
+#endif
+
+#if !defined(PR_CAP_AMBIENT_RAISE)
+#define PR_CAP_AMBIENT_RAISE 2
+#endif
+
+#if !defined(PR_CAP_AMBIENT_CLEAR_ALL)
+#define PR_CAP_AMBIENT_CLEAR_ALL 4
+#endif
+
 using std::hex;
 using std::ostream;
 using std::string;
@@ -132,13 +150,14 @@ static std::set<Capability> toCapabilitySet(uint64_t bitset)
 }
 
 
-std::set<Capability> ProcessCapabilities::get(const Type& type) const
+const std::set<Capability>& ProcessCapabilities::get(const Type& type) const
 {
   switch (type) {
     case EFFECTIVE:   return effective;
     case PERMITTED:   return permitted;
     case INHERITABLE: return inheritable;
     case BOUNDING:    return bounding;
+    case AMBIENT:     return ambient;
   }
 
   UNREACHABLE();
@@ -154,6 +173,7 @@ void ProcessCapabilities::set(
     case PERMITTED:   permitted = capabilities;   return;
     case INHERITABLE: inheritable = capabilities; return;
     case BOUNDING:    bounding = capabilities;    return;
+    case AMBIENT:     ambient = capabilities;     return;
   }
 
   UNREACHABLE();
@@ -169,6 +189,7 @@ void ProcessCapabilities::add(
     case PERMITTED:   permitted.insert(capability);   return;
     case INHERITABLE: inheritable.insert(capability); return;
     case BOUNDING:    bounding.insert(capability);    return;
+    case AMBIENT:     ambient.insert(capability);     return;
   }
 
   UNREACHABLE();
@@ -184,12 +205,18 @@ void ProcessCapabilities::drop(
     case PERMITTED:   permitted.erase(capability);   return;
     case INHERITABLE: inheritable.erase(capability); return;
     case BOUNDING:    bounding.erase(capability);    return;
+    case AMBIENT:     ambient.erase(capability);     return;
   }
 
   UNREACHABLE();
 }
 
 
+Capabilities::Capabilities(int _lastCap, bool _ambientSupported)
+  : ambientCapabilitiesSupported(_ambientSupported),
+    lastCap(_lastCap) {}
+
+
 Try<Capabilities> Capabilities::create()
 {
   // Check for compatible linux capability version.
@@ -231,7 +258,13 @@ Try<Capabilities> Capabilities::create()
         stringify(MAX_CAPABILITY) + "'");
   }
 
-  return Capabilities(lastCap.get());
+  // Test whether the kernel supports ambinent capabilities by testing
+  // for the presence arbitrary capability in the ambient set. This can
+  // only fail if the prctl option is not supported.
+  bool ambientSupported =
+    (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CHOWN, 0, 0) != -1);
+
+  return Capabilities(lastCap.get(), ambientSupported);
 }
 
 
@@ -246,36 +279,74 @@ Try<ProcessCapabilities> Capabilities::get() const
     return ErrnoError("Failed to get capabilities");
   }
 
+  auto getBoundingCapabilities = [this]() {
+    std::set<Capability> bounding;
+
+    // TODO(bbannier): Parse bounding set from the `CapBnd` entry in
+    // `/proc/self/status`.
+    for (int i = 0; i <= lastCap; i++) {
+      if (prctl(PR_CAPBSET_READ, i) == 1) {
+        bounding.insert(Capability(i));
+      }
+    }
+
+    return bounding;
+  };
+
+  auto getAmbientCapabilities = [this]() {
+    std::set<Capability> ambient;
+
+    // TODO(jpeach): Parse the ambient set from the `CapAmb` entry in
+    // `/proc/self/status`.
+    for (int i = 0; i <= lastCap; i++) {
+      if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0) == 1) {
+        ambient.insert(Capability(i));
+      }
+    }
+
+    return ambient;
+  };
+
   ProcessCapabilities capabilities;
 
   capabilities.set(EFFECTIVE, toCapabilitySet(payload.effective()));
   capabilities.set(PERMITTED, toCapabilitySet(payload.permitted()));
   capabilities.set(INHERITABLE, toCapabilitySet(payload.inheritable()));
+  capabilities.set(BOUNDING, getBoundingCapabilities());
 
-  std::set<Capability> bounding;
-
-  // TODO(bbannier): Parse bounding set from the `CapBnd` entry in
-  // `/proc/self/status`.
-  for (int i = 0; i <= lastCap; i++) {
-    if (prctl(PR_CAPBSET_READ, i) == 1) {
-      bounding.insert(Capability(i));
-    }
+  if (ambientCapabilitiesSupported) {
+    capabilities.set(AMBIENT, getAmbientCapabilities());
   }
 
-  capabilities.set(BOUNDING, bounding);
-
   return capabilities;
 }
 
 
-// We do two separate operations:
+// We do three separate operations:
 //  1. Set the `bounding` capabilities for the process.
 //  2. Set the `effective`, `permitted` and `inheritable` capabilities.
+//  3. Clear and then set the `ambient` capabilities.
 //
 // TODO(jojy): Is there a way to make this atomic? Ideally, we would
 // like to rollback any changes if any of the operation fails.
 Try<Nothing> Capabilities::set(const ProcessCapabilities& capabilities)
 {
+  // If we are setting ambient capabilities, verify that they are consistent
+  // so we don't fail after we have already changed our capabilities.
+  if (!capabilities.get(AMBIENT).empty()) {
+    const auto& ambient = capabilities.get(AMBIENT);
+    const auto& permitted = capabilities.get(PERMITTED);
+    const auto& inherited = capabilities.get(INHERITABLE);
+
+    if ((ambient & permitted).size() != ambient.size()) {
+      return Error("Ambient capabilities are not in the permitted set");
+    }
+
+    if ((ambient & inherited).size() != ambient.size()) {
+      return Error("Ambient capabilities are not in the inheritable set");
+    }
+  }
+
   // NOTE: We can only drop capabilities in the bounding set.
   for (int i = 0; i <= lastCap; i++) {
     if (capabilities.get(BOUNDING).count(Capability(i)) > 0) {
@@ -304,6 +375,19 @@ Try<Nothing> Capabilities::set(const ProcessCapabilities& capabilities)
     return ErrnoError("Failed to set capabilities");
   }
 
+  if (ambientCapabilitiesSupported) {
+    if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) {
+      return ErrnoError("Failed to clear ambient capabilities");
+    }
+
+    foreach(auto cap, capabilities.get(AMBIENT)) {
+      if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0) < 0) {
+        return ErrnoError("Failed to raise capability " + stringify(cap) +
+                          " to the ambient set");
+      }
+    }
+  }
+
   return Nothing();
 }
 
@@ -421,6 +505,7 @@ ostream& operator<<(ostream& stream, const Type& type)
     case PERMITTED:   return stream << "perm";
     case INHERITABLE: return stream << "inh";
     case BOUNDING:    return stream << "bnd";
+    case AMBIENT:     return stream << "amb";
   }
 
   UNREACHABLE();
@@ -436,7 +521,8 @@ ostream& operator<<(
     << EFFECTIVE   << ": " << stringify(processCapabilities.effective)   << ", "
     << PERMITTED   << ": " << stringify(processCapabilities.permitted)   << ", "
     << INHERITABLE << ": " << stringify(processCapabilities.inheritable) << ", "
-    << BOUNDING    << ": " << stringify(processCapabilities.bounding)
+    << BOUNDING    << ": " << stringify(processCapabilities.bounding)    << ", "
+    << AMBIENT     << ": " << stringify(processCapabilities.ambient)
     << "}";
 }
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/4ee86647/src/linux/capabilities.hpp
----------------------------------------------------------------------
diff --git a/src/linux/capabilities.hpp b/src/linux/capabilities.hpp
index 5fa3799..9c793a6 100644
--- a/src/linux/capabilities.hpp
+++ b/src/linux/capabilities.hpp
@@ -82,6 +82,7 @@ enum Type
   PERMITTED,
   INHERITABLE,
   BOUNDING,
+  AMBIENT,
 };
 
 
@@ -91,11 +92,20 @@ enum Type
 class ProcessCapabilities
 {
 public:
-  std::set<Capability> get(const Type& type) const;
+  const std::set<Capability>& get(const Type& type) const;
   void set(const Type& type, const std::set<Capability>& capabilities);
   void add(const Type& type, const Capability& capability);
   void drop(const Type& type, const Capability& capability);
 
+  bool operator==(const ProcessCapabilities& right) const
+  {
+    return right.effective == effective &&
+      right.permitted == permitted &&
+      right.inheritable == inheritable &&
+      right.bounding == bounding &&
+      right.ambient == ambient;
+  }
+
 private:
   friend std::ostream& operator<<(
       std::ostream& stream,
@@ -105,6 +115,7 @@ private:
   std::set<Capability> permitted;
   std::set<Capability> inheritable;
   std::set<Capability> bounding;
+  std::set<Capability> ambient;
 };
 
 
@@ -165,8 +176,16 @@ public:
    */
   std::set<Capability> getAllSupportedCapabilities();
 
+  /**
+   * Whether ambient capabilities are supported on this host. If
+   * ambient capabilities are supported, the AMBIENT set will be
+   * populated when getting the process capabilities and applied
+   * when setting them. Otherwise the AMBIENT set will be ignored.
+   */
+  const bool ambientCapabilitiesSupported;
+
 private:
-  explicit Capabilities(int _lastCap) : lastCap(_lastCap) {}
+  Capabilities(int _lastCap, bool _ambientSupported);
 
   // Maximum count of capabilities supported by the system.
   const int lastCap;

http://git-wip-us.apache.org/repos/asf/mesos/blob/4ee86647/src/tests/containerizer/capabilities_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/capabilities_tests.cpp b/src/tests/containerizer/capabilities_tests.cpp
index 15a85ca..aae64db 100644
--- a/src/tests/containerizer/capabilities_tests.cpp
+++ b/src/tests/containerizer/capabilities_tests.cpp
@@ -151,6 +151,86 @@ TEST_F(CapabilitiesTest, ROOT_PingWithJustNetRawSysAdminCap)
   AWAIT_EXPECT_WEXITSTATUS_EQ(0, s->status());
 }
 
+
+TEST(AmbientCapabilities, Supported)
+{
+  Try<Capabilities> manager = Capabilities::create();
+  ASSERT_SOME(manager);
+
+  Try<os::UTSInfo> uname = os::uname();
+  ASSERT_SOME(uname);
+
+  // Trim the kernel version to 2 component because Version::parse() can't
+  // deal with kernel versions like "4.10.14-200.fc25.x86_64".
+  vector<string> components = strings::split(uname->release, ".");
+  components.resize(2);
+
+  Try<Version> version = Version::parse(strings::join(".", components));
+  ASSERT_SOME(version) << " parsing kernel version "
+                       << strings::join(".", components);
+
+  // Ambient capabilities were introduced in Linux 4.3, so if the kernel is
+  // later than that we expect them to be supported.
+  if (version.get() < Version(4, 3, 0)) {
+    EXPECT_FALSE(manager->ambientCapabilitiesSupported);
+  } else {
+    EXPECT_TRUE(manager->ambientCapabilitiesSupported);
+  }
+}
+
+
+TEST(AmbientCapabilities, ROOT_SetAmbient)
+{
+  Try<Capabilities> manager = Capabilities::create();
+  ASSERT_SOME(manager);
+
+  ProcessCapabilities wanted;
+
+  Try<ProcessCapabilities> initial = manager->get();
+  ASSERT_SOME(initial);
+
+  // Setting just the ambient set should fail.
+  wanted.set(capabilities::AMBIENT, {capabilities::CHOWN});
+  EXPECT_ERROR(manager->set(wanted));
+
+  // Keep the full bounding and permitted capabilities because we want
+  // to be able to recover privilege later.
+  wanted.set(capabilities::BOUNDING, initial->get(capabilities::BOUNDING));
+  wanted.set(capabilities::PERMITTED, initial->get(capabilities::PERMITTED));
+
+  wanted.set(
+      capabilities::INHERITABLE,
+      {capabilities::SETPCAP, capabilities::CHOWN});
+
+  wanted.set(
+      capabilities::EFFECTIVE,
+      {capabilities::SETPCAP, capabilities::CHOWN});
+
+  if (manager->ambientCapabilitiesSupported) {
+    wanted.set(
+        capabilities::AMBIENT,
+        {capabilities::SETPCAP, capabilities::CHOWN});
+  }
+
+  // Setting ambient in conjuction with permitted and
+  // interitable should succeed.
+  EXPECT_SOME(manager->set(wanted)) << wanted;
+
+  // We should now have the capabilities that we set.
+  Try<ProcessCapabilities> actual = manager->get();
+  EXPECT_SOME_EQ(wanted, actual)
+    << "wanted capabilities: " << wanted << "\n"
+    << "actual capabilities: " << actual.get();
+
+  ASSERT_SOME(manager->set(initial.get())) << initial.get();
+
+  // And check that we did it right.
+  actual = manager->get();
+  ASSERT_SOME_EQ(initial.get(), actual)
+    << "wanted capabilities: " << initial.get() << "\n"
+    << "actual capabilities: " << actual.get();
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {


[04/13] mesos git commit: Add support for explicitly setting bounding capabilities.

Posted by ji...@apache.org.
Add support for explicitly setting bounding capabilities.

The linux/capabilities isolator implements the `--allowed_capabilities`
option by granting all the allowed capabilities. This change explicitly
populates the only the bounding capabilities in the case where
`--bounding_capabilities` has been set but the task itself has not been
granted any effective capabilities. This improves the security of tasks
since it is now possible to configure the bounding set without actually
granting privilege to the task.

Removed 2 capabilities isolator test cases. These test cases depended on
the framework-specified effective capabilities also setting the bounding
set. Now that the operator flag always determines the bounding set,
these test cases are no longer valid.

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


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

Branch: refs/heads/master
Commit: e70ef312cc3149677fde7f7645332316864ad0a7
Parents: a307d20
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:56 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 .../mesos/isolators/linux/capabilities.cpp      | 84 ++++++++++++++------
 src/slave/containerizer/mesos/launch.cpp        | 60 ++++++++++----
 .../linux_capabilities_isolator_tests.cpp       | 11 +--
 3 files changed, 102 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e70ef312/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
index fcc0a75..26676ff 100644
--- a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
+++ b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
@@ -76,45 +76,66 @@ Future<Option<ContainerLaunchInfo>> LinuxCapabilitiesIsolatorProcess::prepare(
     const ContainerID& containerId,
     const ContainerConfig& containerConfig)
 {
-  // Determine the capabilities of the container that we want to
-  // launch. None() here means that we don't want to set capabilities
-  // for the container.
-  Option<CapabilityInfo> capabilities = None();
+  Option<CapabilityInfo> effective = None();
+  Option<CapabilityInfo> bounding = None();
 
+  // If effective capabilities are specified, those become
+  // both the effective and bounding sets, because we guarantee
+  // that the effective set is at least as restrictive as the
+  // bounding set.
   if (containerConfig.has_container_info() &&
       containerConfig.container_info().has_linux_info() &&
       containerConfig.container_info().linux_info().has_capability_info()) {
-    capabilities =
+    effective =
       containerConfig.container_info().linux_info().capability_info();
+
+    // TODO(jpeach): MESOS-7671: Let the framework specify a bounding
+    // capability set too.
+  }
+
+  // If the framework didn't specify, use the operator effective set.
+  if (effective.isNone()) {
+    effective = flags.allowed_capabilities;
   }
 
-  // If both the framework sets the capabilities for the container and
-  // the operator sets the allowed capabilities on the agent, we need
-  // to verify that the request from the framework is allowed.
-  if (capabilities.isSome() && flags.allowed_capabilities.isSome()) {
-    const set<Capability> requested = convert(capabilities.get());
-    const set<Capability> allowed = convert(flags.allowed_capabilities.get());
+  // TODO(jpeach): MESOS-7671: If the framework specified a bounding set,
+  // test it against flags.bounding_capabilities.
+
+  // If the framework didn't specify, use the operator bounding set and fall
+  // back to the effective set if necessary.
+  if (bounding.isNone()) {
+    bounding = flags.bounding_capabilities;
+  }
+
+  if (effective.isSome() && bounding.isNone()) {
+    bounding = effective;
+  }
+
+  // If the operator specified a bounding set, require effective task
+  // capabilities to be within that set.
+  if (effective.isSome()) {
+    CHECK_SOME(bounding);
+
+    const set<Capability> requested = convert(effective.get());
+    const set<Capability> allowed = convert(bounding.get());
 
     if ((requested & allowed).size() != requested.size()) {
       return Failure(
-          "Capabilities requested '" + stringify(requested) + "', "
+          "Requested capabilities '" + stringify(requested) + "', "
           "but only '" + stringify(allowed) + "' are allowed");
     }
   }
 
-  // If the framework does not set the capabilities and the operator
-  // sets the allowed capabilities, use that as the capabilities for
-  // the container.
-  if (capabilities.isNone() && flags.allowed_capabilities.isSome()) {
-    capabilities = flags.allowed_capabilities.get();
-  }
-
-  // If no capabilities need to be set for the container, we do not
-  // need to modify the container launch info.
-  if (capabilities.isNone()) {
+  // If no capabilities need to be set for the container, we do
+  // not need to modify the container launch info.
+  if (effective.isNone() && bounding.isNone()) {
     return None();
   }
 
+  // The bounding set is always present, but the effective set
+  // may be absent.
+  CHECK_SOME(bounding);
+
   ContainerLaunchInfo launchInfo;
 
   if (containerConfig.has_task_info()) {
@@ -137,14 +158,25 @@ Future<Option<ContainerLaunchInfo>> LinuxCapabilitiesIsolatorProcess::prepare(
     // behavior a user would expect from a root user.
     if (containerConfig.has_rootfs()) {
       launchInfo.mutable_command()->add_arguments(
-          "--effective_capabilities=" +
-          stringify(JSON::protobuf(capabilities.get())));
+          "--bounding_capabilities=" +
+          stringify(JSON::protobuf(bounding.get())));
+      if (effective.isSome()) {
+        launchInfo.mutable_command()->add_arguments(
+            "--effective_capabilities=" +
+            stringify(JSON::protobuf(effective.get())));
+      }
     } else {
-      launchInfo.mutable_effective_capabilities()->CopyFrom(capabilities.get());
+      launchInfo.mutable_bounding_capabilities()->CopyFrom(bounding.get());
+      if (effective.isSome()) {
+        launchInfo.mutable_effective_capabilities()->CopyFrom(effective.get());
+      }
     }
   } else {
     // Custom executor or nested container.
-    launchInfo.mutable_effective_capabilities()->CopyFrom(capabilities.get());
+    launchInfo.mutable_bounding_capabilities()->CopyFrom(bounding.get());
+    if (effective.isSome()) {
+      launchInfo.mutable_effective_capabilities()->CopyFrom(effective.get());
+    }
   }
 
   return launchInfo;

http://git-wip-us.apache.org/repos/asf/mesos/blob/e70ef312/src/slave/containerizer/mesos/launch.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/launch.cpp b/src/slave/containerizer/mesos/launch.cpp
index f623ac0..569262a 100644
--- a/src/slave/containerizer/mesos/launch.cpp
+++ b/src/slave/containerizer/mesos/launch.cpp
@@ -451,16 +451,19 @@ int MesosContainerizerLaunch::execute()
 
 #ifdef __linux__
   // Initialize capabilities support if necessary.
-  Try<Capabilities> capabilitiesManager = Error("Not initialized");
+  Option<Capabilities> capabilitiesManager = None();
 
-  if (launchInfo.has_effective_capabilities()) {
-    capabilitiesManager = Capabilities::create();
-    if (capabilitiesManager.isError()) {
+  if (launchInfo.has_effective_capabilities() ||
+      launchInfo.has_bounding_capabilities()) {
+    Try<Capabilities> _capabilitiesManager = Capabilities::create();
+    if (_capabilitiesManager.isError()) {
       cerr << "Failed to initialize capabilities support: "
-           << capabilitiesManager.error() << endl;
+           << _capabilitiesManager.error() << endl;
       exitWithStatus(EXIT_FAILURE);
     }
 
+    capabilitiesManager = _capabilitiesManager.get();
+
     // Prevent clearing of capabilities on `setuid`.
     if (uid.isSome()) {
       Try<Nothing> keepCaps = capabilitiesManager->setKeepCaps();
@@ -472,7 +475,8 @@ int MesosContainerizerLaunch::execute()
     }
   }
 #else
-  if (launchInfo.has_effective_capabilities()) {
+  if (launchInfo.has_effective_capabilities() ||
+      launchInfo.has_bounding_capabilities()) {
     cerr << "Capabilities are not supported on non Linux system" << endl;
     exitWithStatus(EXIT_FAILURE);
   }
@@ -599,7 +603,7 @@ int MesosContainerizerLaunch::execute()
 #endif // __WINDOWS__
 
 #ifdef __linux__
-  if (launchInfo.has_effective_capabilities()) {
+  if (capabilitiesManager.isSome()) {
     Try<ProcessCapabilities> capabilities = capabilitiesManager->get();
     if (capabilities.isError()) {
       cerr << "Failed to get capabilities for the current process: "
@@ -607,9 +611,10 @@ int MesosContainerizerLaunch::execute()
       exitWithStatus(EXIT_FAILURE);
     }
 
-    // After 'setuid', 'effective' set is cleared. Since `SETPCAP` is
-    // required in the `effective` set of a process to change the
-    // bounding set, we need to restore it first.
+    // After 'setuid', the 'effective' set is cleared. Since `SETPCAP`
+    // is required in the `effective` set of a process to change the
+    // bounding set, we need to restore it first so we can make the
+    // final capability changes.
     capabilities->add(capabilities::EFFECTIVE, capabilities::SETPCAP);
 
     Try<Nothing> setPcap = capabilitiesManager->set(capabilities.get());
@@ -619,14 +624,35 @@ int MesosContainerizerLaunch::execute()
       exitWithStatus(EXIT_FAILURE);
     }
 
-    // Set up requested capabilities.
-    set<Capability> target = capabilities::convert(
-        launchInfo.effective_capabilities());
+    // If the task has any effective capabilities, grant them to all
+    // the capability sets.
+    if (launchInfo.has_effective_capabilities()) {
+      set<Capability> target =
+        capabilities::convert(launchInfo.effective_capabilities());
+
+      capabilities->set(capabilities::EFFECTIVE, target);
+      capabilities->set(capabilities::PERMITTED, target);
+      capabilities->set(capabilities::INHERITABLE, target);
+      capabilities->set(capabilities::BOUNDING, target);
+    }
+
+    // If we also have bounding capabilities, apply that in preference to
+    // the effective capabilities.
+    if (launchInfo.has_bounding_capabilities()) {
+      set<Capability> bounding =
+        capabilities::convert(launchInfo.bounding_capabilities());
+
+      capabilities->set(capabilities::BOUNDING, bounding);
+    }
 
-    capabilities->set(capabilities::EFFECTIVE, target);
-    capabilities->set(capabilities::PERMITTED, target);
-    capabilities->set(capabilities::INHERITABLE, target);
-    capabilities->set(capabilities::BOUNDING, target);
+    // Force the inherited set to be the same as the bounding set. If we
+    // are root and capabilities have not been specified, then this is a
+    // no-op. If capabilities have been specified, then we need to clip the
+    // inherited set to prevent file-based capabilities granting privileges
+    // outside the bounding set.
+    capabilities->set(
+        capabilities::INHERITABLE,
+        capabilities->get(capabilities::BOUNDING));
 
     Try<Nothing> set = capabilitiesManager->set(capabilities.get());
     if (set.isError()) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/e70ef312/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
index 1d70f0e..ba510fb 100644
--- a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
+++ b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
@@ -186,6 +186,7 @@ TEST_P(LinuxCapabilitiesIsolatorTest, ROOT_Ping)
   slave::Flags flags = CreateSlaveFlags();
   flags.isolation = "linux/capabilities";
   flags.allowed_capabilities = param.allowed;
+  flags.bounding_capabilities = param.allowed;
 
   if (param.useImage == TestParam::WITH_IMAGE) {
     const string registry = path::join(sandbox.get(), "registry");
@@ -308,16 +309,6 @@ INSTANTIATE_TEST_CASE_P(
             TestParam::FAILURE),
         TestParam(
             set<Capability>({DAC_READ_SEARCH}),
-            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
-            TestParam::WITHOUT_IMAGE,
-            TestParam::FAILURE),
-        TestParam(
-            set<Capability>({DAC_READ_SEARCH}),
-            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
-            TestParam::WITH_IMAGE,
-            TestParam::FAILURE),
-        TestParam(
-            set<Capability>({DAC_READ_SEARCH}),
             set<Capability>({CHOWN, DAC_READ_SEARCH}),
             TestParam::WITHOUT_IMAGE,
             TestParam::FAILURE),


[08/13] mesos git commit: Additional linux/capabilities isolator documentation.

Posted by ji...@apache.org.
Additional linux/capabilities isolator documentation.

Linux capabilities are somewhat involved, so add some additional
exposition of how the linux/capabilites isolator handles them.

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


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

Branch: refs/heads/master
Commit: 519a3f8b7707663e6000f3593b8cae912cdd559c
Parents: 7492bda
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:45:09 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:33 2017 -0700

----------------------------------------------------------------------
 docs/linux_capabilities.md | 57 +++++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/519a3f8b/docs/linux_capabilities.md
----------------------------------------------------------------------
diff --git a/docs/linux_capabilities.md b/docs/linux_capabilities.md
index 75dc0c0..30b9f0c 100644
--- a/docs/linux_capabilities.md
+++ b/docs/linux_capabilities.md
@@ -18,34 +18,57 @@ list of currently exposed capabilities.
 ## Agent setup
 
 The Linux capabilities isolator is loaded when `linux/capabilities` is
-present in the agent's `--isolation` flag.
-Capabilities which should be allowed are passed with the
-`--effective_capabilities` flag. This isolator requires the
-`CAP_SETPCAP` capability so agent processes typically need to be
-started as root. A possible agent startup invocation could be
+present in the agent's `--isolation` flag.  This isolator requires the
+`CAP_SETPCAP` capability so agent processes typically need to be started
+as root.
+
+The `--effective_capabilities` flag specifies a set of capabilities that
+are always granted to tasks. If the running kernel (Linux 4.3 or later)
+supports ambient capabilities, these capabilities will be added to the
+effective capability set of the task when it is launched. Otherwise
+they must be re-acquired by arranging for the task to execute a file
+with the relevant file-based capabilities enabled.
+
+In the absence of capabilities specified by the scheduler, an empty list
+for `--effective_capabilities` signifies that all capabilities will
+be explicitly dropped.  If the `--effective_capabilities` flag is not
+present, the task will be launched with the default capabilities of the
+task user but the ambient capabilities will not be set.
+
+The `--bounding_capabilities` flag specifies an upper bound on the the
+capabilities a task is allowed to acquire or be granted.  Schedulers are
+not allowed to launch tasks with capabilities outside the set specified
+by the `--bounding_capabilities` flag.
+
+An empty list for `--bounding_capabilities` signifies that no capabilities
+are allowed, while an absent `--bounding_capabilities` flag signifies
+that all capabilities are allowed.
+
+A possible agent startup invocation could be
 
 ```{.console}
 sudo mesos-agent --master=<master ip> --ip=<agent ip>
   --work_dir=/var/lib/mesos
   --isolation=linux/capabilities[,other isolation flags]
   --effective_capabilities='{"capabilities":[NET_RAW,MKNOD]}'
+  --bounding_capabilities='{"capabilities":[NET_RAW,MKNOD,SYSLOG]}'
 ```
 
-An empty list for `--effective_capabilities` signifies that no
-capabilities are allowed, while an absent `--effective_capabilities`
-flag signifies that all capabilities are allowed.
-
 
 ## Task setup
 
-In order for a Mesos task to acquire allowed capabilities it needs to
-declare required capabilities in the `LinuxInfo` of its
+In order for a Mesos task to acquire effective capabilities it should
+declare the required capabilities in the `LinuxInfo` element of its
 `ContainerInfo`.
 
-A Mesos task can only request capabilities which are allowed for the
-agent; a task requesting unallowed capabilities will be rejected.
+A Mesos task can only request capabilities which are allowed according
+to the `--bounding_capabilities` flag of the agent; a task requesting
+other capabilities will be rejected. When the `--bounding_capabilities`
+flag is not present, all capability request will be granted.
 
-If an empty list of capabilities is given the Mesos task will drop all
-capabilities; if the optional `capability_info` field is not set the
-container will be able to acquire the capabilities of the Mesos task's
-user.
+If the optional `capability_info` field is not set, the value of the
+`--effective_capabilities` flag will be used to populate the task
+capabilities. If an empty list of capabilities is given, the Mesos task
+will drop all capabilities.  Note that the task will be unable to acquire
+capabilities not specified in the final `capability_info` even if the
+`--bounding_capabilities` flag would otherwise allow them.


[10/13] mesos git commit: Disabled the AmbientCapabilities.Supported test.

Posted by ji...@apache.org.
Disabled the AmbientCapabilities.Supported test.

The test doesn't pass on some Linux distribution like CentOS 7.


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

Branch: refs/heads/master
Commit: 35dd2b600b8af0204d03c4ee5348a1a6672b136c
Parents: 0f6ec98
Author: Jie Yu <yu...@gmail.com>
Authored: Fri Jun 16 21:00:53 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:33 2017 -0700

----------------------------------------------------------------------
 src/tests/containerizer/capabilities_tests.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/35dd2b60/src/tests/containerizer/capabilities_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/capabilities_tests.cpp b/src/tests/containerizer/capabilities_tests.cpp
index aae64db..3070e1d 100644
--- a/src/tests/containerizer/capabilities_tests.cpp
+++ b/src/tests/containerizer/capabilities_tests.cpp
@@ -152,7 +152,7 @@ TEST_F(CapabilitiesTest, ROOT_PingWithJustNetRawSysAdminCap)
 }
 
 
-TEST(AmbientCapabilities, Supported)
+TEST(AmbientCapabilities, DISABLED_Supported)
 {
   Try<Capabilities> manager = Capabilities::create();
   ASSERT_SOME(manager);


[03/13] mesos git commit: Rename ContainerLaunchInfo `capabilities` field.

Posted by ji...@apache.org.
Rename ContainerLaunchInfo `capabilities` field.

Rename the ContainerLaunchInfo `capabilities` field to
`effective_capabilities` since it is intended to be the set of
capabilities we actually make effective in the launched task.

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


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

Branch: refs/heads/master
Commit: ffcfdb77bd1791064a6c97b93faa23c7ff6cf6db
Parents: 024336a
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:44 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 include/mesos/slave/containerizer.proto         |  2 +-
 src/launcher/executor.cpp                       | 25 ++++++++++----------
 src/slave/containerizer/mesos/containerizer.cpp |  6 ++---
 .../mesos/isolators/linux/capabilities.cpp      |  7 +++---
 src/slave/containerizer/mesos/launch.cpp        |  9 +++----
 5 files changed, 26 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ffcfdb77/include/mesos/slave/containerizer.proto
----------------------------------------------------------------------
diff --git a/include/mesos/slave/containerizer.proto b/include/mesos/slave/containerizer.proto
index 03a983e..d36763e 100644
--- a/include/mesos/slave/containerizer.proto
+++ b/include/mesos/slave/containerizer.proto
@@ -212,7 +212,7 @@ message ContainerLaunchInfo {
 
   // (Linux only) The capabilities requested for the container.
   // The capabilities are set while launching the container.
-  optional CapabilityInfo capabilities = 7;
+  optional CapabilityInfo effective_capabilities = 7;
 
   // (POSIX only) The rlimits requested for the container.
   // The rlimits are set while launching the container.

http://git-wip-us.apache.org/repos/asf/mesos/blob/ffcfdb77/src/launcher/executor.cpp
----------------------------------------------------------------------
diff --git a/src/launcher/executor.cpp b/src/launcher/executor.cpp
index a620678..155c5f9 100644
--- a/src/launcher/executor.cpp
+++ b/src/launcher/executor.cpp
@@ -123,7 +123,7 @@ public:
       const Option<string>& _user,
       const Option<string>& _taskCommand,
       const Option<Environment>& _taskEnvironment,
-      const Option<CapabilityInfo>& _capabilities,
+      const Option<CapabilityInfo>& _effectiveCapabilities,
       const FrameworkID& _frameworkId,
       const ExecutorID& _executorId,
       const Duration& _shutdownGracePeriod)
@@ -145,7 +145,7 @@ public:
       user(_user),
       taskCommand(_taskCommand),
       taskEnvironment(_taskEnvironment),
-      capabilities(_capabilities),
+      effectiveCapabilities(_effectiveCapabilities),
       frameworkId(_frameworkId),
       executorId(_executorId),
       lastTaskStatus(None()) {}
@@ -395,7 +395,7 @@ protected:
       const Option<string>& rootfs,
       const Option<string>& sandboxDirectory,
       const Option<string>& workingDirectory,
-      const Option<CapabilityInfo>& capabilities)
+      const Option<CapabilityInfo>& effectiveCapabilities)
   {
     // Prepare the flags to pass to the launch process.
     slave::MesosContainerizerLaunch::Flags launchFlags;
@@ -452,8 +452,9 @@ protected:
       launchInfo.set_user(user.get());
     }
 
-    if (capabilities.isSome()) {
-      launchInfo.mutable_capabilities()->CopyFrom(capabilities.get());
+    if (effectiveCapabilities.isSome()) {
+      launchInfo.mutable_effective_capabilities()->CopyFrom(
+          effectiveCapabilities.get());
     }
 
     launchFlags.launch_info = JSON::protobuf(launchInfo);
@@ -631,7 +632,7 @@ protected:
         rootfs,
         sandboxDirectory,
         workingDirectory,
-        capabilities);
+        effectiveCapabilities);
 
     LOG(INFO) << "Forked command at " << pid;
 
@@ -1115,7 +1116,7 @@ private:
   Option<string> user;
   Option<string> taskCommand;
   Option<Environment> taskEnvironment;
-  Option<CapabilityInfo> capabilities;
+  Option<CapabilityInfo> effectiveCapabilities;
   const FrameworkID frameworkId;
   const ExecutorID executorId;
   Owned<MesosBase> mesos;
@@ -1167,9 +1168,9 @@ public:
         "should be added to the executor's environment before launching\n"
         "the task.");
 
-    add(&Flags::capabilities,
-        "capabilities",
-        "Capabilities the command can use.");
+    add(&Flags::effective_capabilities,
+        "effective_capabilities",
+        "Capabilities granted to the command at launch.");
 
     add(&Flags::launcher_dir,
         "launcher_dir",
@@ -1186,7 +1187,7 @@ public:
   Option<string> user;
   Option<string> task_command;
   Option<Environment> task_environment;
-  Option<mesos::CapabilityInfo> capabilities;
+  Option<mesos::CapabilityInfo> effective_capabilities;
   string launcher_dir;
 };
 
@@ -1261,7 +1262,7 @@ int main(int argc, char** argv)
           flags.user,
           flags.task_command,
           flags.task_environment,
-          flags.capabilities,
+          flags.effective_capabilities,
           frameworkId,
           executorId,
           shutdownGracePeriod));

http://git-wip-us.apache.org/repos/asf/mesos/blob/ffcfdb77/src/slave/containerizer/mesos/containerizer.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/containerizer.cpp b/src/slave/containerizer/mesos/containerizer.cpp
index f3e6210..9376d14 100644
--- a/src/slave/containerizer/mesos/containerizer.cpp
+++ b/src/slave/containerizer/mesos/containerizer.cpp
@@ -1307,9 +1307,9 @@ Future<bool> MesosContainerizerProcess::_launch(
       return Failure("Multiple isolators specify working directory");
     }
 
-    if (isolatorLaunchInfo->has_capabilities() &&
-        launchInfo.has_capabilities()) {
-      return Failure("Multiple isolators specify capabilities");
+    if (isolatorLaunchInfo->has_effective_capabilities() &&
+        launchInfo.has_effective_capabilities()) {
+      return Failure("Multiple isolators specify effective capabilities");
     }
 
     if (isolatorLaunchInfo->has_rlimits() &&

http://git-wip-us.apache.org/repos/asf/mesos/blob/ffcfdb77/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
index 60d22aa..73b9e8d 100644
--- a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
+++ b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
@@ -126,13 +126,14 @@ Future<Option<ContainerLaunchInfo>> LinuxCapabilitiesIsolatorProcess::prepare(
     // behavior a user would expect from a root user.
     if (containerConfig.has_rootfs()) {
       launchInfo.mutable_command()->add_arguments(
-          "--capabilities=" + stringify(JSON::protobuf(capabilities.get())));
+          "--effective_capabilities=" +
+          stringify(JSON::protobuf(capabilities.get())));
     } else {
-      launchInfo.mutable_capabilities()->CopyFrom(capabilities.get());
+      launchInfo.mutable_effective_capabilities()->CopyFrom(capabilities.get());
     }
   } else {
     // Custom executor or nested container.
-    launchInfo.mutable_capabilities()->CopyFrom(capabilities.get());
+    launchInfo.mutable_effective_capabilities()->CopyFrom(capabilities.get());
   }
 
   return launchInfo;

http://git-wip-us.apache.org/repos/asf/mesos/blob/ffcfdb77/src/slave/containerizer/mesos/launch.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/launch.cpp b/src/slave/containerizer/mesos/launch.cpp
index f48d294..09b36dc 100644
--- a/src/slave/containerizer/mesos/launch.cpp
+++ b/src/slave/containerizer/mesos/launch.cpp
@@ -453,7 +453,7 @@ int MesosContainerizerLaunch::execute()
   // Initialize capabilities support if necessary.
   Try<Capabilities> capabilitiesManager = Error("Not initialized");
 
-  if (launchInfo.has_capabilities()) {
+  if (launchInfo.has_effective_capabilities()) {
     capabilitiesManager = Capabilities::create();
     if (capabilitiesManager.isError()) {
       cerr << "Failed to initialize capabilities support: "
@@ -472,7 +472,7 @@ int MesosContainerizerLaunch::execute()
     }
   }
 #else
-  if (launchInfo.has_capabilities()) {
+  if (launchInfo.has_effective_capabilities()) {
     cerr << "Capabilities are not supported on non Linux system" << endl;
     exitWithStatus(EXIT_FAILURE);
   }
@@ -589,7 +589,7 @@ int MesosContainerizerLaunch::execute()
 #endif // __WINDOWS__
 
 #ifdef __linux__
-  if (launchInfo.has_capabilities()) {
+  if (launchInfo.has_effective_capabilities()) {
     Try<ProcessCapabilities> capabilities = capabilitiesManager->get();
     if (capabilities.isError()) {
       cerr << "Failed to get capabilities for the current process: "
@@ -610,7 +610,8 @@ int MesosContainerizerLaunch::execute()
     }
 
     // Set up requested capabilities.
-    set<Capability> target = capabilities::convert(launchInfo.capabilities());
+    set<Capability> target = capabilities::convert(
+        launchInfo.effective_capabilities());
 
     capabilities->set(capabilities::EFFECTIVE, target);
     capabilities->set(capabilities::PERMITTED, target);


[07/13] mesos git commit: Change launcher working directory before dropping privilege.

Posted by ji...@apache.org.
Change launcher working directory before dropping privilege.

The launcher needs to change its working directory before dropping
privilege by switching users and installing capabilities, because
afterwards it might not have access to traverse to the desired
working directory.

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


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

Branch: refs/heads/master
Commit: a307d200c325162223a8007797b968edd8fc5d43
Parents: 4d1edb9
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:54 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 src/slave/containerizer/mesos/launch.cpp | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a307d200/src/slave/containerizer/mesos/launch.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/launch.cpp b/src/slave/containerizer/mesos/launch.cpp
index 09b36dc..f623ac0 100644
--- a/src/slave/containerizer/mesos/launch.cpp
+++ b/src/slave/containerizer/mesos/launch.cpp
@@ -560,6 +560,16 @@ int MesosContainerizerLaunch::execute()
   }
 #endif // __WINDOWS__
 
+  if (launchInfo.has_working_directory()) {
+    Try<Nothing> chdir = os::chdir(launchInfo.working_directory());
+    if (chdir.isError()) {
+      cerr << "Failed to chdir into current working directory "
+           << "'" << launchInfo.working_directory() << "': "
+           << chdir.error() << endl;
+      exitWithStatus(EXIT_FAILURE);
+    }
+  }
+
 #ifndef __WINDOWS__
   // Change user if provided. Note that we do that after executing the
   // preparation commands so that those commands will be run with the
@@ -626,16 +636,6 @@ int MesosContainerizerLaunch::execute()
   }
 #endif // __linux__
 
-  if (launchInfo.has_working_directory()) {
-    Try<Nothing> chdir = os::chdir(launchInfo.working_directory());
-    if (chdir.isError()) {
-      cerr << "Failed to chdir into current working directory "
-           << "'" << launchInfo.working_directory() << "': "
-           << chdir.error() << endl;
-      exitWithStatus(EXIT_FAILURE);
-    }
-  }
-
   // Prepare the executable and the argument list for the child.
   string executable(launchInfo.command().shell()
     ? os::Shell::name


[09/13] mesos git commit: Add ambient capabilities to launched tasks.

Posted by ji...@apache.org.
Add ambient capabilities to launched tasks.

In the absence of ambient capabilities, capabilities in the
effective set do not survive across execve(2). This means
that tasks attempting to make use of the LinuxInfo capability
support also need to ensure that file capabilities are set on
the file that is ultimately executed. Supporting ambient
capabilities allows the effective capabilities to survive
execve(2), so it is now possible to launch a task with limited
privilege elevations.

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


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

Branch: refs/heads/master
Commit: 32e605ed8fb3669edf34caba3cf711d57d3e4f9e
Parents: 4ee8664
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:45:01 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:33 2017 -0700

----------------------------------------------------------------------
 src/slave/containerizer/mesos/launch.cpp | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/32e605ed/src/slave/containerizer/mesos/launch.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/launch.cpp b/src/slave/containerizer/mesos/launch.cpp
index 569262a..162ca1c 100644
--- a/src/slave/containerizer/mesos/launch.cpp
+++ b/src/slave/containerizer/mesos/launch.cpp
@@ -630,6 +630,7 @@ int MesosContainerizerLaunch::execute()
       set<Capability> target =
         capabilities::convert(launchInfo.effective_capabilities());
 
+      capabilities->set(capabilities::AMBIENT, target);
       capabilities->set(capabilities::EFFECTIVE, target);
       capabilities->set(capabilities::PERMITTED, target);
       capabilities->set(capabilities::INHERITABLE, target);


[11/13] mesos git commit: Update Linux capabilities isolator tests.

Posted by ji...@apache.org.
Update Linux capabilities isolator tests.

Update the Linux capabilities tests to separate the operator
bounding and effective sets clearly and add an additional test
case that wasn't already covered. This better demonstrates
how the various capability set configurations interact.

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


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

Branch: refs/heads/master
Commit: 0f6ec987cdd02749d104ab032efcd385fb424095
Parents: 519a3f8
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:45:12 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:33 2017 -0700

----------------------------------------------------------------------
 .../linux_capabilities_isolator_tests.cpp       | 104 +++++++++++++++----
 1 file changed, 81 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/0f6ec987/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
index 31fc54e..5982c33 100644
--- a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
+++ b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
@@ -80,9 +80,14 @@ namespace internal {
 namespace tests {
 
 // Param for the tests:
-//   'requested'  Framework specified capabilities for the container.
-//   'allowed'    Allowed capabilities configured by the operator.
-//   'success'    True if the task should finish normally.
+//   'framework_effective'
+//            Framework specified effective capabilities for the container.
+//   'operator_effective'
+//            Default effective capabilities configured by the operator.
+//   'operator_bounding'
+//            Default bounding capabilities configured by the  operator
+//   'success'
+//            True if the task should finish normally.
 struct TestParam
 {
   enum Result
@@ -98,21 +103,27 @@ struct TestParam
   };
 
   TestParam(
-      const Option<set<Capability>>& _requested,
-      const Option<set<Capability>>& _allowed,
+      const Option<set<Capability>>& _framework_effective,
+      const Option<set<Capability>>& _operator_effective,
+      const Option<set<Capability>>& _operator_bounding,
       UseImage _useImage,
       Result _result)
-    : requested(_requested.isSome()
-        ? convert(_requested.get())
+    : framework_effective(_framework_effective.isSome()
+        ? convert(_framework_effective.get())
         : Option<CapabilityInfo>::none()),
-      allowed(_allowed.isSome()
-        ? convert(_allowed.get())
+      operator_effective(_operator_effective.isSome()
+        ? convert(_operator_effective.get())
+        : Option<CapabilityInfo>::none()),
+      operator_bounding(_operator_bounding.isSome()
+        ? convert(_operator_bounding.get())
         : Option<CapabilityInfo>::none()),
       useImage(_useImage),
       result(_result) {}
 
-  const Option<CapabilityInfo> requested;
-  const Option<CapabilityInfo> allowed;
+  const Option<CapabilityInfo> framework_effective;
+  const Option<CapabilityInfo> operator_effective;
+  const Option<CapabilityInfo> operator_bounding;
+
   const UseImage useImage;
   const Result result;
 };
@@ -120,16 +131,25 @@ struct TestParam
 
 ostream& operator<<(ostream& stream, const TestParam& param)
 {
-  if (param.requested.isSome()) {
-    stream << "requested='" << JSON::protobuf(param.requested.get()) << "', ";
+  if (param.framework_effective.isSome()) {
+    stream << "framework_effective='"
+           << JSON::protobuf(param.framework_effective.get()) << "', ";
+  } else {
+    stream << "framework_effective='none', ";
+  }
+
+  if (param.operator_effective.isSome()) {
+    stream << "operator_effective='"
+           << JSON::protobuf(param.operator_effective.get()) << "', ";
   } else {
-    stream << "requested='none', ";
+    stream << "operator_effective='none', ";
   }
 
-  if (param.allowed.isSome()) {
-    stream << "allowed='" << JSON::protobuf(param.allowed.get()) << "', ";
+  if (param.operator_bounding.isSome()) {
+    stream << "operator_bounding='"
+           << JSON::protobuf(param.operator_bounding.get()) << "', ";
   } else {
-    stream << "allowed='none', ";
+    stream << "operator_bounding='none', ";
   }
 
   switch (param.useImage) {
@@ -185,8 +205,8 @@ TEST_P(LinuxCapabilitiesIsolatorTest, ROOT_Ping)
 
   slave::Flags flags = CreateSlaveFlags();
   flags.isolation = "linux/capabilities";
-  flags.effective_capabilities = param.allowed;
-  flags.bounding_capabilities = param.allowed;
+  flags.effective_capabilities = param.operator_effective;
+  flags.bounding_capabilities = param.operator_bounding;
 
   if (param.useImage == TestParam::WITH_IMAGE) {
     const string registry = path::join(sandbox.get(), "registry");
@@ -239,13 +259,13 @@ TEST_P(LinuxCapabilitiesIsolatorTest, ROOT_Ping)
       offers.get()[0].resources(),
       command);
 
-  if (param.requested.isSome()) {
+  if (param.framework_effective.isSome()) {
     ContainerInfo* container = task.mutable_container();
     container->set_type(ContainerInfo::MESOS);
 
     LinuxInfo* linux = container->mutable_linux_info();
     CapabilityInfo* capabilities = linux->mutable_capability_info();
-    capabilities->CopyFrom(param.requested.get());
+    capabilities->CopyFrom(param.framework_effective.get());
   }
 
   if (param.useImage == TestParam::WITH_IMAGE) {
@@ -300,46 +320,66 @@ INSTANTIATE_TEST_CASE_P(
         TestParam(
             set<Capability>(),
             None(),
+            None(),
             TestParam::WITHOUT_IMAGE,
             TestParam::FAILURE),
         TestParam(
             set<Capability>(),
             None(),
+            None(),
             TestParam::WITH_IMAGE,
             TestParam::FAILURE),
         TestParam(
             set<Capability>({DAC_READ_SEARCH}),
             set<Capability>({CHOWN, DAC_READ_SEARCH}),
+            set<Capability>({CHOWN, DAC_READ_SEARCH}),
             TestParam::WITHOUT_IMAGE,
             TestParam::FAILURE),
         TestParam(
             set<Capability>({DAC_READ_SEARCH}),
             set<Capability>({CHOWN, DAC_READ_SEARCH}),
+            set<Capability>({CHOWN, DAC_READ_SEARCH}),
             TestParam::WITH_IMAGE,
             TestParam::FAILURE),
 
-        // Allowed capabilities do not contain that ping needs, thus
+        // Effective capabilities do not contain that ping needs, thus
         // ping will fail.
         TestParam(
             None(),
             set<Capability>({CHOWN, DAC_READ_SEARCH}),
+            set<Capability>({CHOWN, DAC_READ_SEARCH}),
             TestParam::WITHOUT_IMAGE,
             TestParam::FAILURE),
         TestParam(
             None(),
             set<Capability>({CHOWN, DAC_READ_SEARCH}),
+            set<Capability>({CHOWN, DAC_READ_SEARCH}),
+            TestParam::WITH_IMAGE,
+            TestParam::FAILURE),
+        TestParam(
+            None(),
+            None(),
+            set<Capability>({CHOWN, DAC_READ_SEARCH}),
             TestParam::WITH_IMAGE,
             TestParam::FAILURE),
+        TestParam(
+            None(),
+            None(),
+            set<Capability>({CHOWN, DAC_READ_SEARCH}),
+            TestParam::WITHOUT_IMAGE,
+            TestParam::FAILURE),
 
-        // Requested capabilities are not allowed, task will fail.
+        // Framework effective capabilities are not allowed, task will fail.
         TestParam(
             set<Capability>({NET_RAW, NET_ADMIN}),
             set<Capability>({CHOWN}),
+            set<Capability>({CHOWN}),
             TestParam::WITHOUT_IMAGE,
             TestParam::FAILURE),
         TestParam(
             set<Capability>({NET_RAW, NET_ADMIN}),
             set<Capability>({CHOWN}),
+            set<Capability>({CHOWN}),
             TestParam::WITH_IMAGE,
             TestParam::FAILURE),
 
@@ -348,31 +388,49 @@ INSTANTIATE_TEST_CASE_P(
         TestParam(
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             None(),
+            None(),
             TestParam::WITHOUT_IMAGE,
             TestParam::SUCCESS),
         TestParam(
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             None(),
+            None(),
             TestParam::WITH_IMAGE,
             TestParam::SUCCESS),
         TestParam(
             None(),
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
+            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             TestParam::WITHOUT_IMAGE,
             TestParam::SUCCESS),
         TestParam(
             None(),
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
+            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
+            TestParam::WITH_IMAGE,
+            TestParam::SUCCESS),
+        TestParam(
+            None(),
+            None(),
+            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
+            TestParam::WITHOUT_IMAGE,
+            TestParam::SUCCESS),
+        TestParam(
+            None(),
+            None(),
+            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             TestParam::WITH_IMAGE,
             TestParam::SUCCESS),
         TestParam(
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
+            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             TestParam::WITHOUT_IMAGE,
             TestParam::SUCCESS),
         TestParam(
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
+            set<Capability>({NET_RAW, NET_ADMIN, DAC_READ_SEARCH}),
             TestParam::WITH_IMAGE,
             TestParam::SUCCESS)));
 


[06/13] mesos git commit: Add the agent --bounding_capabilities flag.

Posted by ji...@apache.org.
Add the agent --bounding_capabilities flag.

Add the agent --bounding_capabilities flag to enable the operator to
specify a default bounding capabilities set.

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


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

Branch: refs/heads/master
Commit: 5454574ef5bf2891f581a354b858b4c7e36f525f
Parents: b75aa51
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:50 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 src/slave/flags.cpp | 14 ++++++++++++++
 src/slave/flags.hpp |  1 +
 2 files changed, 15 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5454574e/src/slave/flags.cpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.cpp b/src/slave/flags.cpp
index 93c8ffb..37d2f3f 100644
--- a/src/slave/flags.cpp
+++ b/src/slave/flags.cpp
@@ -614,6 +614,20 @@ mesos::internal::slave::Flags::Flags()
       "       \"SYS_ADMIN\"\n"
       "     ]\n"
       "}");
+
+  add(&Flags::bounding_capabilities,
+      "bounding_capabilities",
+      "JSON representation of the Linux capabilities that the operator\n"
+      "will allow as the maximum level of privilege that a task launched\n"
+      "by the containerizer may acquire (currently only supported by the\n"
+      "Mesos Containerizer).\n"
+      "\n"
+      "This flag is effective iff `linux/capabilities` isolation is enabled.\n"
+      "When `linux/capabilities` isolation is enabled, the absence of this\n"
+      "flag implies that the operator allows ALL capabilities.\n"
+      "\n"
+      "This flag has the same syntax as `--allowed_capabilities`."
+     );
 #endif
 
   add(&Flags::firewall_rules,

http://git-wip-us.apache.org/repos/asf/mesos/blob/5454574e/src/slave/flags.hpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.hpp b/src/slave/flags.hpp
index 2f9d52e..578ea0f 100644
--- a/src/slave/flags.hpp
+++ b/src/slave/flags.hpp
@@ -111,6 +111,7 @@ public:
   bool systemd_enable_support;
   std::string systemd_runtime_directory;
   Option<CapabilityInfo> allowed_capabilities;
+  Option<CapabilityInfo> bounding_capabilities;
 #endif
   Option<Firewall> firewall_rules;
   Option<Path> credential;


[12/13] mesos git commit: Rename the `--allowed_capabilities` flag to `--effective_capabilities`.

Posted by ji...@apache.org.
Rename the `--allowed_capabilities` flag to `--effective_capabilities`.

Since the `--allowed_capabilities` flag was being used to actually
grant capabilities, rename it to `--effective_capabilities` which better
conveys the intention and semantics of this flag.

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


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

Branch: refs/heads/master
Commit: 1496243d46ec47b9885a267f0291cbd11e25bf8c
Parents: 32e605e
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:45:03 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:33 2017 -0700

----------------------------------------------------------------------
 docs/configuration.md                           | 16 ++++-------
 docs/linux_capabilities.md                      | 10 +++----
 .../mesos/isolators/linux/capabilities.cpp      | 10 ++++---
 src/slave/flags.cpp                             | 29 ++++++++------------
 src/slave/flags.hpp                             |  2 +-
 .../linux_capabilities_isolator_tests.cpp       | 17 ++++++------
 6 files changed, 38 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/1496243d/docs/configuration.md
----------------------------------------------------------------------
diff --git a/docs/configuration.md b/docs/configuration.md
index ed510fa..e2a1a2e 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -1019,24 +1019,18 @@ cgroup.
 </tr>
 <tr>
   <td>
-    --allowed_capabilities=VALUE
+    --effective_capabilities=VALUE
   </td>
   <td>
-JSON representation of system capabilities that the operator allows for
-tasks run in containers launched by the containerizer (currently only
+JSON representation of the Linux capabilities the that the agent will grant
+to tasks run in containers launched by the containerizer (currently only
 supported by the Mesos Containerizer). This set overrides the default
 capabilities requested by the framework.
 <p/>
-The net capability for a task running in the container will be:
-<pre><code>((F & A) & U)</code>
-  where F = capabilities requested by the framework.
-        A = allowed capabilities specified by this flag.
-        U = permitted capabilities for the agent process.
-</pre>
 To set capabilities the agent should have the <code>SETPCAP</code> capability.
 <p/>
-This flag is effective iff <code>capabilities</code> isolation is enabled.
-When <code>capabilities</code> isolation is enabled, the absence of this flag
+This flag is effective iff <code>linux/capabilities</code> isolation is enabled.
+When <code>linux/capabilities</code> isolation is enabled, the absence of this flag
 implies that the operator intends to allow ALL capabilities.
 <p/>
 Example:

http://git-wip-us.apache.org/repos/asf/mesos/blob/1496243d/docs/linux_capabilities.md
----------------------------------------------------------------------
diff --git a/docs/linux_capabilities.md b/docs/linux_capabilities.md
index b588aff..75dc0c0 100644
--- a/docs/linux_capabilities.md
+++ b/docs/linux_capabilities.md
@@ -20,7 +20,7 @@ list of currently exposed capabilities.
 The Linux capabilities isolator is loaded when `linux/capabilities` is
 present in the agent's `--isolation` flag.
 Capabilities which should be allowed are passed with the
-`--allowed_capabilities` flag. This isolator requires the
+`--effective_capabilities` flag. This isolator requires the
 `CAP_SETPCAP` capability so agent processes typically need to be
 started as root. A possible agent startup invocation could be
 
@@ -28,12 +28,12 @@ started as root. A possible agent startup invocation could be
 sudo mesos-agent --master=<master ip> --ip=<agent ip>
   --work_dir=/var/lib/mesos
   --isolation=linux/capabilities[,other isolation flags]
-  --allowed_capabilities='{"capabilities":[NET_RAW,MKNOD]}'
+  --effective_capabilities='{"capabilities":[NET_RAW,MKNOD]}'
 ```
 
-An empty list for `--allowed_capabilities` signifies that no
-capabilities are allowed, while an absent `--allowed_capabilities` flag
-signifies that all capabilities are allowed.
+An empty list for `--effective_capabilities` signifies that no
+capabilities are allowed, while an absent `--effective_capabilities`
+flag signifies that all capabilities are allowed.
 
 
 ## Task setup

http://git-wip-us.apache.org/repos/asf/mesos/blob/1496243d/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
index 26676ff..ff7b9f1 100644
--- a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
+++ b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
@@ -56,10 +56,12 @@ Try<Isolator*> LinuxCapabilitiesIsolatorProcess::create(const Flags& flags)
     return Error("Failed to initialize capabilities: " + create.error());
   }
 
-  if (flags.allowed_capabilities.isSome() &&
+  if (flags.effective_capabilities.isSome() &&
       flags.bounding_capabilities.isSome()) {
-    const set<Capability> bounding = convert(flags.bounding_capabilities.get());
-    const set<Capability> effective = convert(flags.allowed_capabilities.get());
+    const set<Capability> bounding = convert(
+        flags.bounding_capabilities.get());
+    const set<Capability> effective = convert(
+        flags.effective_capabilities.get());
 
     if ((effective & bounding).size() != effective.size()) {
       return Error(
@@ -95,7 +97,7 @@ Future<Option<ContainerLaunchInfo>> LinuxCapabilitiesIsolatorProcess::prepare(
 
   // If the framework didn't specify, use the operator effective set.
   if (effective.isNone()) {
-    effective = flags.allowed_capabilities;
+    effective = flags.effective_capabilities;
   }
 
   // TODO(jpeach): MESOS-7671: If the framework specified a bounding set,

http://git-wip-us.apache.org/repos/asf/mesos/blob/1496243d/src/slave/flags.cpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.cpp b/src/slave/flags.cpp
index 37d2f3f..c84aa67 100644
--- a/src/slave/flags.cpp
+++ b/src/slave/flags.cpp
@@ -587,25 +587,20 @@ mesos::internal::slave::Flags::Flags()
       "The path to the systemd system run time directory\n",
       "/run/systemd/system");
 
-  add(&Flags::allowed_capabilities,
-      "allowed_capabilities",
-      "JSON representation of system capabilities that the operator\n"
-      "allows tasks run in containers launched by the containerizer\n"
-      "(currently only supported by the Mesos Containerizer). This set\n"
-      "overrides the default capabilities for the user and the\n"
-      "capabilities requested by the framework.\n"
-      "\n"
-      "The net capability for a task running in the container will be:\n"
-      "   ((F & A) & U)\n"
-      "   where F = capabilities requested by the framework.\n"
-      "         A = allowed capabilities specified by this flag.\n"
-      "         U = permitted capabilities for the agent process.\n"
+  add(&Flags::effective_capabilities,
+      "effective_capabilities",
+      flags::DeprecatedName("allowed_capabilities"),
+      "JSON representation of the Linux capabilities that the agent will\n"
+      "grant to a task that will be run in containers launched by the\n"
+      "containerizer (currently only supported by the Mesos Containerizer).\n"
+      "This set overrides the default capabilities for the user but not\n"
+      "the capabilities requested by the framework.\n"
       "\n"
       "To set capabilities the agent should have the `SETPCAP` capability.\n"
       "\n"
-      "This flag is effective iff `capabilities` isolation is enabled.\n"
-      "When `capabilities` isolation is enabled, the absence of this flag\n"
-      "implies that the operator intends to allow ALL capabilities.\n"
+      "This flag is effective iff `linux/capabilities` isolation is enabled.\n"
+      "When `linux/capabilities` isolation is enabled, the absence of this\n"
+      "flag implies that the operator intends to allow ALL capabilities.\n"
       "\n"
       "Example:\n"
       "{\n"
@@ -626,7 +621,7 @@ mesos::internal::slave::Flags::Flags()
       "When `linux/capabilities` isolation is enabled, the absence of this\n"
       "flag implies that the operator allows ALL capabilities.\n"
       "\n"
-      "This flag has the same syntax as `--allowed_capabilities`."
+      "This flag has the same syntax as `--effective_capabilities`."
      );
 #endif
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/1496243d/src/slave/flags.hpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.hpp b/src/slave/flags.hpp
index 578ea0f..e75c1b4 100644
--- a/src/slave/flags.hpp
+++ b/src/slave/flags.hpp
@@ -110,7 +110,7 @@ public:
   bool revocable_cpu_low_priority;
   bool systemd_enable_support;
   std::string systemd_runtime_directory;
-  Option<CapabilityInfo> allowed_capabilities;
+  Option<CapabilityInfo> effective_capabilities;
   Option<CapabilityInfo> bounding_capabilities;
 #endif
   Option<Firewall> firewall_rules;

http://git-wip-us.apache.org/repos/asf/mesos/blob/1496243d/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
index ba510fb..31fc54e 100644
--- a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
+++ b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
@@ -185,7 +185,7 @@ TEST_P(LinuxCapabilitiesIsolatorTest, ROOT_Ping)
 
   slave::Flags flags = CreateSlaveFlags();
   flags.isolation = "linux/capabilities";
-  flags.allowed_capabilities = param.allowed;
+  flags.effective_capabilities = param.allowed;
   flags.bounding_capabilities = param.allowed;
 
   if (param.useImage == TestParam::WITH_IMAGE) {
@@ -395,38 +395,39 @@ TEST_F(LinuxCapabilitiesIsolatorFlagsTest, ROOT_IsolatorFlags)
 
   Try<Owned<cluster::Slave>> slave = Owned<cluster::Slave>();
 
+
   // Allowed is not a subset of bounding, so this should fail.
-  flags.allowed_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
+  flags.effective_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
   flags.bounding_capabilities = convert(set<Capability>({NET_RAW}));
   slave = StartSlave(&detector, flags);
   ASSERT_ERROR(slave);
 
   // Allowed is the same as bounding, which is OK.
-  flags.allowed_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
+  flags.effective_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
   flags.bounding_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
   slave = StartSlave(&detector, flags);
   ASSERT_SOME(slave);
 
   // Allowed is a subset of bounding, which is OK.
-  flags.allowed_capabilities = convert(set<Capability>({NET_RAW}));
+  flags.effective_capabilities = convert(set<Capability>({NET_RAW}));
   flags.bounding_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
   slave = StartSlave(&detector, flags);
   ASSERT_SOME(slave);
 
   // Both sets are allowed to be missing.
-  flags.allowed_capabilities = None();
+  flags.effective_capabilities = None();
   flags.bounding_capabilities = None();
   slave = StartSlave(&detector, flags);
   ASSERT_SOME(slave);
 
   // Bounding capabilities are allowed to be missing.
-  flags.allowed_capabilities = convert(set<Capability>({NET_RAW}));
+  flags.effective_capabilities = convert(set<Capability>({NET_RAW}));
   flags.bounding_capabilities = None();
   slave = StartSlave(&detector, flags);
   ASSERT_SOME(slave);
 
-  // Allowed capabilities are allowed to be missing.
-  flags.allowed_capabilities = None();
+  // Effective capabilities are allowed to be missing.
+  flags.effective_capabilities = None();
   flags.bounding_capabilities = convert(set<Capability>({NET_RAW}));
   slave = StartSlave(&detector, flags);
   ASSERT_SOME(slave);


[13/13] mesos git commit: Synchronize capabilities flags documentation.

Posted by ji...@apache.org.
Synchronize capabilities flags documentation.

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


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

Branch: refs/heads/master
Commit: 7492bdac326e106a79dabd40c59c5b322fee0e73
Parents: 1496243
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:45:05 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:33 2017 -0700

----------------------------------------------------------------------
 docs/configuration.md | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/7492bdac/docs/configuration.md
----------------------------------------------------------------------
diff --git a/docs/configuration.md b/docs/configuration.md
index e2a1a2e..0eb696a 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -1016,16 +1016,18 @@ Present functionality is intended for resource monitoring and
 no cgroup limits are set, they are inherited from the root mesos
 cgroup.
   </td>
+
 </tr>
 <tr>
   <td>
     --effective_capabilities=VALUE
   </td>
   <td>
-JSON representation of the Linux capabilities the that the agent will grant
-to tasks run in containers launched by the containerizer (currently only
-supported by the Mesos Containerizer). This set overrides the default
-capabilities requested by the framework.
+JSON representation of the Linux capabilities that the agent will
+grant to a task that will be run in containers launched by the
+containerizer (currently only supported by the Mesos Containerizer).
+This set overrides the default capabilities for the user but not
+the capabilities requested by the framework.
 <p/>
 To set capabilities the agent should have the <code>SETPCAP</code> capability.
 <p/>
@@ -1044,6 +1046,26 @@ Example:
 </code></pre>
   </td>
 </tr>
+
+</tr>
+<tr>
+  <td>
+    --bounding_capabilities=VALUE
+  </td>
+  <td>
+JSON representation of the Linux capabilities that the operator
+will allow as the maximum level of privilege that a task launched
+by the containerizer may acquire (currently only supported by the
+Mesos Containerizer).
+<p/>
+This flag is effective iff <code>linux/capabilities</code> isolation is enabled.
+When <code>linux/capabilities</code> isolation is enabled, the absence of this flag
+implies that the operator intends to allow ALL capabilities.
+<p/>
+This flag has the same syntax as <code>--effective_capabilities</code>.
+  </td>
+</tr>
+
 <tr>
   <td>
     --appc_simple_discovery_uri_prefix=VALUE


[02/13] mesos git commit: Add a `bounding_capabilities` field to ContainerLaunchInfo.

Posted by ji...@apache.org.
Add a `bounding_capabilities` field to ContainerLaunchInfo.

Add a `bounding_capabilities` field to ContainerLaunchInfo and propagate
bounding capabilities through the command executor.

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


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

Branch: refs/heads/master
Commit: b75aa51175551552651e577d99ac9ec47ab12de5
Parents: ffcfdb7
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:48 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 include/mesos/slave/containerizer.proto |  5 +++++
 src/launcher/executor.cpp               | 20 ++++++++++++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/b75aa511/include/mesos/slave/containerizer.proto
----------------------------------------------------------------------
diff --git a/include/mesos/slave/containerizer.proto b/include/mesos/slave/containerizer.proto
index d36763e..84f9ca7 100644
--- a/include/mesos/slave/containerizer.proto
+++ b/include/mesos/slave/containerizer.proto
@@ -214,6 +214,11 @@ message ContainerLaunchInfo {
   // The capabilities are set while launching the container.
   optional CapabilityInfo effective_capabilities = 7;
 
+  // (Linux only) The bounding capability set requested for
+  // the container. This limits the file capabilities a task
+  // can acquire without granting them at launch time.
+  optional CapabilityInfo bounding_capabilities = 16;
+
   // (POSIX only) The rlimits requested for the container.
   // The rlimits are set while launching the container.
   optional RLimitInfo rlimits = 8;

http://git-wip-us.apache.org/repos/asf/mesos/blob/b75aa511/src/launcher/executor.cpp
----------------------------------------------------------------------
diff --git a/src/launcher/executor.cpp b/src/launcher/executor.cpp
index 155c5f9..65b9a2c 100644
--- a/src/launcher/executor.cpp
+++ b/src/launcher/executor.cpp
@@ -124,6 +124,7 @@ public:
       const Option<string>& _taskCommand,
       const Option<Environment>& _taskEnvironment,
       const Option<CapabilityInfo>& _effectiveCapabilities,
+      const Option<CapabilityInfo>& _boundingCapabilities,
       const FrameworkID& _frameworkId,
       const ExecutorID& _executorId,
       const Duration& _shutdownGracePeriod)
@@ -146,6 +147,7 @@ public:
       taskCommand(_taskCommand),
       taskEnvironment(_taskEnvironment),
       effectiveCapabilities(_effectiveCapabilities),
+      boundingCapabilities(_boundingCapabilities),
       frameworkId(_frameworkId),
       executorId(_executorId),
       lastTaskStatus(None()) {}
@@ -395,7 +397,8 @@ protected:
       const Option<string>& rootfs,
       const Option<string>& sandboxDirectory,
       const Option<string>& workingDirectory,
-      const Option<CapabilityInfo>& effectiveCapabilities)
+      const Option<CapabilityInfo>& effectiveCapabilities,
+      const Option<CapabilityInfo>& boundingCapabilities)
   {
     // Prepare the flags to pass to the launch process.
     slave::MesosContainerizerLaunch::Flags launchFlags;
@@ -457,6 +460,11 @@ protected:
           effectiveCapabilities.get());
     }
 
+    if (boundingCapabilities.isSome()) {
+      launchInfo.mutable_bounding_capabilities()->CopyFrom(
+          boundingCapabilities.get());
+    }
+
     launchFlags.launch_info = JSON::protobuf(launchInfo);
 
     // TODO(tillt): Consider using a flag allowing / disallowing the
@@ -632,7 +640,8 @@ protected:
         rootfs,
         sandboxDirectory,
         workingDirectory,
-        effectiveCapabilities);
+        effectiveCapabilities,
+        boundingCapabilities);
 
     LOG(INFO) << "Forked command at " << pid;
 
@@ -1117,6 +1126,7 @@ private:
   Option<string> taskCommand;
   Option<Environment> taskEnvironment;
   Option<CapabilityInfo> effectiveCapabilities;
+  Option<CapabilityInfo> boundingCapabilities;
   const FrameworkID frameworkId;
   const ExecutorID executorId;
   Owned<MesosBase> mesos;
@@ -1172,6 +1182,10 @@ public:
         "effective_capabilities",
         "Capabilities granted to the command at launch.");
 
+    add(&Flags::bounding_capabilities,
+        "bounding_capabilities",
+        "The bounding set of capabilities the command can use.");
+
     add(&Flags::launcher_dir,
         "launcher_dir",
         "Directory path of Mesos binaries.",
@@ -1188,6 +1202,7 @@ public:
   Option<string> task_command;
   Option<Environment> task_environment;
   Option<mesos::CapabilityInfo> effective_capabilities;
+  Option<mesos::CapabilityInfo> bounding_capabilities;
   string launcher_dir;
 };
 
@@ -1263,6 +1278,7 @@ int main(int argc, char** argv)
           flags.task_command,
           flags.task_environment,
           flags.effective_capabilities,
+          flags.bounding_capabilities,
           frameworkId,
           executorId,
           shutdownGracePeriod));


[05/13] mesos git commit: Check bounding capabilities at isolator creation time.

Posted by ji...@apache.org.
Check bounding capabilities at isolator creation time.

When we create the `linux/capabilities` isolator, enforce the rule that
the bounding capabilities are a superset of the allowed capabilities
when both are specified.

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


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

Branch: refs/heads/master
Commit: 4d1edb9d98ef63fe1032d42ae0f2f3ba94e6d605
Parents: 5454574
Author: James Peach <jp...@apache.org>
Authored: Fri Jun 16 20:44:53 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 16 21:01:32 2017 -0700

----------------------------------------------------------------------
 .../mesos/isolators/linux/capabilities.cpp      | 11 +++++
 .../linux_capabilities_isolator_tests.cpp       | 52 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/4d1edb9d/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
index 73b9e8d..fcc0a75 100644
--- a/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
+++ b/src/slave/containerizer/mesos/isolators/linux/capabilities.cpp
@@ -56,6 +56,17 @@ Try<Isolator*> LinuxCapabilitiesIsolatorProcess::create(const Flags& flags)
     return Error("Failed to initialize capabilities: " + create.error());
   }
 
+  if (flags.allowed_capabilities.isSome() &&
+      flags.bounding_capabilities.isSome()) {
+    const set<Capability> bounding = convert(flags.bounding_capabilities.get());
+    const set<Capability> effective = convert(flags.allowed_capabilities.get());
+
+    if ((effective & bounding).size() != effective.size()) {
+      return Error(
+          "Allowed capabilities are not a subset of the bounding capabilites");
+    }
+  }
+
   return new MesosIsolator(
       Owned<MesosIsolatorProcess>(new LinuxCapabilitiesIsolatorProcess(flags)));
 }

http://git-wip-us.apache.org/repos/asf/mesos/blob/4d1edb9d/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
index 40376a0..1d70f0e 100644
--- a/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
+++ b/src/tests/containerizer/linux_capabilities_isolator_tests.cpp
@@ -43,6 +43,8 @@
 
 #include "linux/capabilities.hpp"
 
+#include "master/detector/standalone.hpp"
+
 #include "slave/flags.hpp"
 
 #include "slave/containerizer/fetcher.hpp"
@@ -61,6 +63,7 @@ using mesos::internal::capabilities::NET_RAW;
 using mesos::internal::slave::Fetcher;
 
 using mesos::master::detector::MasterDetector;
+using mesos::master::detector::StandaloneMasterDetector;
 
 using process::Future;
 using process::Owned;
@@ -389,6 +392,55 @@ INSTANTIATE_TEST_CASE_P(
 // TODO(bbannier): Reject these tasks that specify capabilities if
 // capabilities isolator is not enabled.
 
+class LinuxCapabilitiesIsolatorFlagsTest : public MesosTest {};
+
+
+TEST_F(LinuxCapabilitiesIsolatorFlagsTest, ROOT_IsolatorFlags)
+{
+  StandaloneMasterDetector detector;
+
+  slave::Flags flags = CreateSlaveFlags();
+  flags.isolation = "linux/capabilities";
+
+  Try<Owned<cluster::Slave>> slave = Owned<cluster::Slave>();
+
+  // Allowed is not a subset of bounding, so this should fail.
+  flags.allowed_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
+  flags.bounding_capabilities = convert(set<Capability>({NET_RAW}));
+  slave = StartSlave(&detector, flags);
+  ASSERT_ERROR(slave);
+
+  // Allowed is the same as bounding, which is OK.
+  flags.allowed_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
+  flags.bounding_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
+  slave = StartSlave(&detector, flags);
+  ASSERT_SOME(slave);
+
+  // Allowed is a subset of bounding, which is OK.
+  flags.allowed_capabilities = convert(set<Capability>({NET_RAW}));
+  flags.bounding_capabilities = convert(set<Capability>({NET_RAW, NET_ADMIN}));
+  slave = StartSlave(&detector, flags);
+  ASSERT_SOME(slave);
+
+  // Both sets are allowed to be missing.
+  flags.allowed_capabilities = None();
+  flags.bounding_capabilities = None();
+  slave = StartSlave(&detector, flags);
+  ASSERT_SOME(slave);
+
+  // Bounding capabilities are allowed to be missing.
+  flags.allowed_capabilities = convert(set<Capability>({NET_RAW}));
+  flags.bounding_capabilities = None();
+  slave = StartSlave(&detector, flags);
+  ASSERT_SOME(slave);
+
+  // Allowed capabilities are allowed to be missing.
+  flags.allowed_capabilities = None();
+  flags.bounding_capabilities = convert(set<Capability>({NET_RAW}));
+  slave = StartSlave(&detector, flags);
+  ASSERT_SOME(slave);
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {