Blame SOURCES/0468-test-capability-rebase-to-upstream-version.patch

17b0f1
From 900251c41dab192ff863024e07864c09462e86d2 Mon Sep 17 00:00:00 2001
17b0f1
From: Lukas Nykryn <lnykryn@redhat.com>
17b0f1
Date: Mon, 20 Mar 2017 12:24:09 +0100
17b0f1
Subject: [PATCH] test-capability: rebase to upstream version
17b0f1
17b0f1
Related: #1387398
17b0f1
---
17b0f1
 src/test/test-capability.c                    | 80 +++++++++++++++++--
17b0f1
 src/test/test-execute.c                       | 43 ++++++++++
17b0f1
 ...pabilityambientset-merge-nfsnobody.service |  9 +++
17b0f1
 test/exec-capabilityambientset-merge.service  |  9 +++
17b0f1
 ...xec-capabilityambientset-nfsnobody.service |  8 ++
17b0f1
 test/exec-capabilityambientset.service        |  8 ++
17b0f1
 .../exec-capabilityboundingset-invert.service |  7 ++
17b0f1
 test/exec-capabilityboundingset-merge.service |  8 ++
17b0f1
 test/exec-capabilityboundingset-reset.service |  8 ++
17b0f1
 .../exec-capabilityboundingset-simple.service |  7 ++
17b0f1
 10 files changed, 179 insertions(+), 8 deletions(-)
17b0f1
 create mode 100644 test/exec-capabilityambientset-merge-nfsnobody.service
17b0f1
 create mode 100644 test/exec-capabilityambientset-merge.service
17b0f1
 create mode 100644 test/exec-capabilityambientset-nfsnobody.service
17b0f1
 create mode 100644 test/exec-capabilityambientset.service
17b0f1
 create mode 100644 test/exec-capabilityboundingset-invert.service
17b0f1
 create mode 100644 test/exec-capabilityboundingset-merge.service
17b0f1
 create mode 100644 test/exec-capabilityboundingset-reset.service
17b0f1
 create mode 100644 test/exec-capabilityboundingset-simple.service
17b0f1
17b0f1
diff --git a/src/test/test-capability.c b/src/test/test-capability.c
17b0f1
index 43769923b0..67a9ec2d14 100644
17b0f1
--- a/src/test/test-capability.c
17b0f1
+++ b/src/test/test-capability.c
17b0f1
@@ -17,21 +17,22 @@
17b0f1
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
17b0f1
 ***/
17b0f1
 
17b0f1
-#include <sys/types.h>
17b0f1
-#include <sys/wait.h>
17b0f1
-#include <sys/capability.h>
17b0f1
-#include <sys/socket.h>
17b0f1
 #include <netinet/in.h>
17b0f1
 #include <pwd.h>
17b0f1
+#include <sys/capability.h>
17b0f1
+#include <sys/prctl.h>
17b0f1
+#include <sys/socket.h>
17b0f1
+#include <sys/wait.h>
17b0f1
 #include <unistd.h>
17b0f1
 
17b0f1
 #include "capability.h"
17b0f1
-#include "util.h"
17b0f1
 #include "macro.h"
17b0f1
+#include "util.h"
17b0f1
 
17b0f1
 static uid_t test_uid = -1;
17b0f1
 static gid_t test_gid = -1;
17b0f1
-// We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage
17b0f1
+
17b0f1
+/* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */
17b0f1
 static uint64_t test_flags = 1ULL << CAP_DAC_OVERRIDE;
17b0f1
 
17b0f1
 static void fork_test(void (*test_func)(void)) {
17b0f1
@@ -65,8 +66,9 @@ static void show_capabilities(void) {
17b0f1
         cap_free(text);
17b0f1
 }
17b0f1
 
17b0f1
-static int setup_tests(void) {
17b0f1
+static int setup_tests(bool *run_ambient) {
17b0f1
         struct passwd *nobody;
17b0f1
+        int r;
17b0f1
 
17b0f1
         nobody = getpwnam("nobody");
17b0f1
         if (!nobody) {
17b0f1
@@ -76,6 +78,18 @@ static int setup_tests(void) {
17b0f1
         test_uid = nobody->pw_uid;
17b0f1
         test_gid = nobody->pw_gid;
17b0f1
 
17b0f1
+        *run_ambient = false;
17b0f1
+
17b0f1
+        r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
17b0f1
+
17b0f1
+        /* There's support for PR_CAP_AMBIENT if the prctl() call
17b0f1
+         * succeeded or error code was something else than EINVAL. The
17b0f1
+         * EINVAL check should be good enough to rule out false
17b0f1
+         * positives. */
17b0f1
+
17b0f1
+        if (r >= 0 || errno != EINVAL)
17b0f1
+                *run_ambient = true;
17b0f1
+
17b0f1
         return 0;
17b0f1
 }
17b0f1
 
17b0f1
@@ -139,8 +153,53 @@ static void test_have_effective_cap(void) {
17b0f1
         assert_se(!have_effective_cap(CAP_CHOWN));
17b0f1
 }
17b0f1
 
17b0f1
+static void test_update_inherited_set(void) {
17b0f1
+        cap_t caps;
17b0f1
+        uint64_t set = 0;
17b0f1
+        cap_flag_value_t fv;
17b0f1
+
17b0f1
+        caps = cap_get_proc();
17b0f1
+        assert_se(caps);
17b0f1
+        assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv));
17b0f1
+        assert(fv == CAP_CLEAR);
17b0f1
+
17b0f1
+        set = (UINT64_C(1) << CAP_CHOWN);
17b0f1
+
17b0f1
+        assert_se(!capability_update_inherited_set(caps, set));
17b0f1
+        assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv));
17b0f1
+        assert(fv == CAP_SET);
17b0f1
+
17b0f1
+        cap_free(caps);
17b0f1
+}
17b0f1
+
17b0f1
+static void test_set_ambient_caps(void) {
17b0f1
+        cap_t caps;
17b0f1
+        uint64_t set = 0;
17b0f1
+        cap_flag_value_t fv;
17b0f1
+
17b0f1
+        caps = cap_get_proc();
17b0f1
+        assert_se(caps);
17b0f1
+        assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv));
17b0f1
+        assert(fv == CAP_CLEAR);
17b0f1
+        cap_free(caps);
17b0f1
+
17b0f1
+        assert_se(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) == 0);
17b0f1
+
17b0f1
+        set = (UINT64_C(1) << CAP_CHOWN);
17b0f1
+
17b0f1
+        assert_se(!capability_ambient_set_apply(set, true));
17b0f1
+
17b0f1
+        caps = cap_get_proc();
17b0f1
+        assert_se(!cap_get_flag(caps, CAP_CHOWN, CAP_INHERITABLE, &fv));
17b0f1
+        assert(fv == CAP_SET);
17b0f1
+        cap_free(caps);
17b0f1
+
17b0f1
+        assert_se(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) == 1);
17b0f1
+}
17b0f1
+
17b0f1
 int main(int argc, char *argv[]) {
17b0f1
         int r;
17b0f1
+        bool run_ambient;
17b0f1
 
17b0f1
         log_parse_environment();
17b0f1
         log_open();
17b0f1
@@ -148,14 +207,19 @@ int main(int argc, char *argv[]) {
17b0f1
         if (getuid() != 0)
17b0f1
                 return EXIT_TEST_SKIP;
17b0f1
 
17b0f1
-        r = setup_tests();
17b0f1
+        r = setup_tests(&run_ambient);
17b0f1
         if (r < 0)
17b0f1
                 return -r;
17b0f1
 
17b0f1
         show_capabilities();
17b0f1
 
17b0f1
         test_drop_privileges();
17b0f1
+        test_update_inherited_set();
17b0f1
+
17b0f1
         fork_test(test_have_effective_cap);
17b0f1
 
17b0f1
+        if (run_ambient)
17b0f1
+                fork_test(test_set_ambient_caps);
17b0f1
+
17b0f1
         return 0;
17b0f1
 }
17b0f1
diff --git a/src/test/test-execute.c b/src/test/test-execute.c
17b0f1
index 6e5567c3ed..8e70702cbb 100644
17b0f1
--- a/src/test/test-execute.c
17b0f1
+++ b/src/test/test-execute.c
17b0f1
@@ -17,7 +17,11 @@
17b0f1
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
17b0f1
 ***/
17b0f1
 
17b0f1
+#include <grp.h>
17b0f1
+#include <pwd.h>
17b0f1
 #include <stdio.h>
17b0f1
+#include <sys/prctl.h>
17b0f1
+#include <sys/types.h>
17b0f1
 
17b0f1
 #include "unit.h"
17b0f1
 #include "manager.h"
17b0f1
@@ -25,6 +29,7 @@
17b0f1
 #include "macro.h"
17b0f1
 #include "strv.h"
17b0f1
 #include "mkdir.h"
17b0f1
+#include "path-util.h"
17b0f1
 
17b0f1
 typedef void (*test_function_t)(Manager *m);
17b0f1
 
17b0f1
@@ -177,6 +182,42 @@ static void test_exec_runtimedirectory(Manager *m) {
17b0f1
         test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
17b0f1
 }
17b0f1
 
17b0f1
+static void test_exec_capabilityboundingset(Manager *m) {
17b0f1
+        int r;
17b0f1
+
17b0f1
+        r = find_binary("capsh", true, NULL);
17b0f1
+        if (r < 0) {
17b0f1
+                log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
17b0f1
+                return;
17b0f1
+        }
17b0f1
+
17b0f1
+        test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
17b0f1
+        test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
17b0f1
+        test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
17b0f1
+        test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
17b0f1
+}
17b0f1
+
17b0f1
+static void test_exec_capabilityambientset(Manager *m) {
17b0f1
+        int r;
17b0f1
+
17b0f1
+        /* Check if the kernel has support for ambient capabilities. Run
17b0f1
+         * the tests only if that's the case. Clearing all ambient
17b0f1
+         * capabilities is fine, since we are expecting them to be unset
17b0f1
+         * in the first place for the tests. */
17b0f1
+        r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
17b0f1
+        if (r >= 0 || errno != EINVAL) {
17b0f1
+                if (getpwnam("nobody")) {
17b0f1
+                        test(m, "exec-capabilityambientset.service", 0, CLD_EXITED);
17b0f1
+                        test(m, "exec-capabilityambientset-merge.service", 0, CLD_EXITED);
17b0f1
+                } else if (getpwnam("nfsnobody")) {
17b0f1
+                        test(m, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED);
17b0f1
+                        test(m, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED);
17b0f1
+                } else
17b0f1
+                        log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
17b0f1
+        } else
17b0f1
+                log_error_errno(errno, "Skipping %s, the kernel does not support ambient capabilities: %m", __func__);
17b0f1
+}
17b0f1
+
17b0f1
 int main(int argc, char *argv[]) {
17b0f1
         test_function_t tests[] = {
17b0f1
                 test_exec_workingdirectory,
17b0f1
@@ -192,6 +233,8 @@ int main(int argc, char *argv[]) {
17b0f1
                 test_exec_passenvironment,
17b0f1
                 test_exec_umask,
17b0f1
                 test_exec_runtimedirectory,
17b0f1
+                test_exec_capabilityboundingset,
17b0f1
+                test_exec_capabilityambientset,
17b0f1
                 NULL,
17b0f1
         };
17b0f1
         test_function_t *test = NULL;
17b0f1
diff --git a/test/exec-capabilityambientset-merge-nfsnobody.service b/test/exec-capabilityambientset-merge-nfsnobody.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..00bec581b5
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityambientset-merge-nfsnobody.service
17b0f1
@@ -0,0 +1,9 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for AmbientCapabilities
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(grep "CapAmb:" /proc/self/status); test "$$c" = "CapAmb:	0000000000003000"'
17b0f1
+Type=oneshot
17b0f1
+User=nfsnobody
17b0f1
+AmbientCapabilities=CAP_NET_ADMIN
17b0f1
+AmbientCapabilities=CAP_NET_RAW
17b0f1
diff --git a/test/exec-capabilityambientset-merge.service b/test/exec-capabilityambientset-merge.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..64964380e2
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityambientset-merge.service
17b0f1
@@ -0,0 +1,9 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for AmbientCapabilities
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(grep "CapAmb:" /proc/self/status); test "$$c" = "CapAmb:	0000000000003000"'
17b0f1
+Type=oneshot
17b0f1
+User=nobody
17b0f1
+AmbientCapabilities=CAP_NET_ADMIN
17b0f1
+AmbientCapabilities=CAP_NET_RAW
17b0f1
diff --git a/test/exec-capabilityambientset-nfsnobody.service b/test/exec-capabilityambientset-nfsnobody.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..614cfdd584
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityambientset-nfsnobody.service
17b0f1
@@ -0,0 +1,8 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for AmbientCapabilities
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(grep "CapAmb:" /proc/self/status); test "$$c" = "CapAmb:	0000000000003000"'
17b0f1
+Type=oneshot
17b0f1
+User=nfsnobody
17b0f1
+AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW
17b0f1
diff --git a/test/exec-capabilityambientset.service b/test/exec-capabilityambientset.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..d63f884ef8
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityambientset.service
17b0f1
@@ -0,0 +1,8 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for AmbientCapabilities
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(grep "CapAmb:" /proc/self/status); test "$$c" = "CapAmb:	0000000000003000"'
17b0f1
+Type=oneshot
17b0f1
+User=nobody
17b0f1
+AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW
17b0f1
diff --git a/test/exec-capabilityboundingset-invert.service b/test/exec-capabilityboundingset-invert.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..fd5d248702
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityboundingset-invert.service
17b0f1
@@ -0,0 +1,7 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for CapabilityBoundingSet
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(capsh --print | grep "^Bounding set .*cap_chown"); test -z "$$c"'
17b0f1
+Type=oneshot
17b0f1
+CapabilityBoundingSet=~CAP_CHOWN
17b0f1
diff --git a/test/exec-capabilityboundingset-merge.service b/test/exec-capabilityboundingset-merge.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..5c7fcaf437
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityboundingset-merge.service
17b0f1
@@ -0,0 +1,8 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for CapabilityBoundingSet
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(capsh --print | grep "Bounding set "); test "$$c" = "Bounding set =cap_chown,cap_fowner,cap_kill"'
17b0f1
+Type=oneshot
17b0f1
+CapabilityBoundingSet=CAP_FOWNER
17b0f1
+CapabilityBoundingSet=CAP_KILL CAP_CHOWN
17b0f1
diff --git a/test/exec-capabilityboundingset-reset.service b/test/exec-capabilityboundingset-reset.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..d7d3320204
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityboundingset-reset.service
17b0f1
@@ -0,0 +1,8 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for CapabilityBoundingSet
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(capsh --print | grep "Bounding set "); test "$$c" = "Bounding set ="'
17b0f1
+Type=oneshot
17b0f1
+CapabilityBoundingSet=CAP_FOWNER CAP_KILL
17b0f1
+CapabilityBoundingSet=
17b0f1
diff --git a/test/exec-capabilityboundingset-simple.service b/test/exec-capabilityboundingset-simple.service
17b0f1
new file mode 100644
17b0f1
index 0000000000..bf1a7f575a
17b0f1
--- /dev/null
17b0f1
+++ b/test/exec-capabilityboundingset-simple.service
17b0f1
@@ -0,0 +1,7 @@
17b0f1
+[Unit]
17b0f1
+Description=Test for CapabilityBoundingSet
17b0f1
+
17b0f1
+[Service]
17b0f1
+ExecStart=/bin/sh -x -c 'c=$$(capsh --print | grep "Bounding set "); test "$$c" = "Bounding set =cap_fowner,cap_kill"'
17b0f1
+Type=oneshot
17b0f1
+CapabilityBoundingSet=CAP_FOWNER CAP_KILL