naccyde / rpms / systemd

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