[systemd-commits] 4 commits - Makefile.am src/test

Tom Gundersen tomegun at kemper.freedesktop.org
Sat Jun 21 15:40:45 PDT 2014


 Makefile.am                |   20 +++++
 src/test/test-async.c      |   50 +++++++++++++
 src/test/test-capability.c |  161 +++++++++++++++++++++++++++++++++++++++++++++
 src/test/test-fdset.c      |   21 +++++
 4 files changed, 250 insertions(+), 2 deletions(-)

New commits:
commit 2de61bbebfe6a1a97709b3277b150cacc30a79cd
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date:   Sat Jun 21 22:07:12 2014 +0200

    tests: add test_fdset_remove

diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c
index 7f52748..3b77415 100644
--- a/src/test/test-fdset.c
+++ b/src/test/test-fdset.c
@@ -106,11 +106,32 @@ static void test_fdset_close_others(void) {
         unlink(name);
 }
 
+static void test_fdset_remove(void) {
+        _cleanup_close_ int fd = -1;
+        FDSet *fdset = NULL;
+        char name[] = "/tmp/test-fdset_remove.XXXXXX";
+
+        fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+        assert_se(fd >= 0);
+
+        fdset = fdset_new();
+        assert_se(fdset);
+        assert_se(fdset_put(fdset, fd) >= 0);
+        assert_se(fdset_remove(fdset, fd) >= 0);
+        assert_se(!fdset_contains(fdset, fd));
+        fdset_free(fdset);
+
+        assert_se(fcntl(fd, F_GETFD) >= 0);
+
+        unlink(name);
+}
+
 int main(int argc, char *argv[]) {
         test_fdset_new_fill();
         test_fdset_put_dup();
         test_fdset_cloexec();
         test_fdset_close_others();
+        test_fdset_remove();
 
         return 0;
 }

commit e6b5c5d03cb28d2149dc1c124c2a315911b91f4f
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date:   Sat Jun 21 22:07:11 2014 +0200

    tests: add test-async

diff --git a/Makefile.am b/Makefile.am
index 2bd66d5..8fe7cbe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1246,7 +1246,8 @@ tests += \
 	test-socket-util \
 	test-fdset \
 	test-conf-files \
-	test-capability
+	test-capability \
+	test-async
 
 EXTRA_DIST += \
 	test/sched_idle_bad.service \
@@ -1357,6 +1358,12 @@ test_capability_LDADD = \
 	libsystemd-shared.la \
 	libsystemd-capability.la
 
+test_async_SOURCES = \
+	src/test/test-async.c
+
+test_async_LDADD = \
+	libsystemd-shared.la
+
 test_fdset_SOURCES = \
 	src/test/test-fdset.c
 
diff --git a/src/test/test-async.c b/src/test/test-async.c
new file mode 100644
index 0000000..c1c535b
--- /dev/null
+++ b/src/test/test-async.c
@@ -0,0 +1,50 @@
+/***
+  This file is part of systemd
+
+  Copyright 2014 Ronny Chevalier
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <unistd.h>
+
+#include "async.h"
+#include "util.h"
+#include "macro.h"
+
+static bool test_async = false;
+
+static void *async_func(void *arg) {
+        test_async = true;
+
+        return NULL;
+}
+
+int main(int argc, char *argv[]) {
+        int fd;
+        char name[] = "/tmp/test-asynchronous_close.XXXXXX";
+
+        fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+        assert_se(fd >= 0);
+        asynchronous_close(fd);
+        assert_se(asynchronous_job(async_func, NULL) >= 0);
+        assert_se(asynchronous_sync() >= 0);
+
+        sleep(1);
+
+        assert_se(fcntl(fd, F_GETFD) == -1);
+        assert_se(test_async);
+
+        return 0;
+}

commit 6160e473fc2c52ab7c06f1d884a8901d2a5b6b73
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date:   Sat Jun 21 22:07:10 2014 +0200

    tests: add test-capability

diff --git a/Makefile.am b/Makefile.am
index 00db82d..2bd66d5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1245,7 +1245,8 @@ tests += \
 	test-architecture \
 	test-socket-util \
 	test-fdset \
-	test-conf-files
+	test-conf-files \
+	test-capability
 
 EXTRA_DIST += \
 	test/sched_idle_bad.service \
@@ -1349,6 +1350,13 @@ test_utf8_SOURCES = \
 test_utf8_LDADD = \
 	libsystemd-shared.la
 
+test_capability_SOURCES = \
+	src/test/test-capability.c
+
+test_capability_LDADD = \
+	libsystemd-shared.la \
+	libsystemd-capability.la
+
 test_fdset_SOURCES = \
 	src/test/test-fdset.c
 
diff --git a/src/test/test-capability.c b/src/test/test-capability.c
new file mode 100644
index 0000000..a362fc6
--- /dev/null
+++ b/src/test/test-capability.c
@@ -0,0 +1,161 @@
+/***
+  This file is part of systemd
+
+  Copyright 2014 Ronny Chevalier
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/capability.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <pwd.h>
+#include <unistd.h>
+
+#include "capability.h"
+#include "util.h"
+#include "macro.h"
+
+static uid_t test_uid = -1;
+static gid_t test_gid = -1;
+// We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage
+static uint64_t test_flags = 1ULL << CAP_DAC_OVERRIDE;
+
+static void fork_test(void (*test_func)(void)) {
+        pid_t pid = 0;
+
+        pid = fork();
+        assert_se(pid >= 0);
+        if (pid == 0) {
+                test_func();
+                exit(0);
+        } else if (pid > 0) {
+                int status;
+
+                assert_se(waitpid(pid, &status, 0) > 0);
+                assert_se(WIFEXITED(status) && WEXITSTATUS(status) == 0);
+        }
+}
+
+static void show_capabilities(void) {
+        cap_t caps;
+        char *text;
+
+        caps = cap_get_proc();
+        assert_se(caps);
+
+        text = cap_to_text(caps, NULL);
+        assert_se(text);
+
+        log_info("Capabilities:%s", text);
+        cap_free(caps);
+        cap_free(text);
+}
+
+static int setup_tests(void) {
+        struct passwd *nobody;
+
+        nobody = getpwnam("nobody");
+        if (!nobody) {
+                log_error("Could not find nobody user: %m");
+                return -EXIT_TEST_SKIP;
+        }
+        test_uid = nobody->pw_uid;
+        test_gid = nobody->pw_gid;
+
+        return 0;
+}
+
+static void test_drop_privileges_keep_net_raw(void) {
+        int sock;
+
+        sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
+        assert_se(sock >= 0);
+        safe_close(sock);
+
+        assert_se(drop_privileges(test_uid, test_gid, test_flags | (1ULL << CAP_NET_RAW)) >= 0);
+        assert_se(getuid() == test_uid);
+        assert_se(getgid() == test_gid);
+        show_capabilities();
+
+        sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
+        assert_se(sock >= 0);
+        safe_close(sock);
+}
+
+static void test_drop_privileges_dontkeep_net_raw(void) {
+        int sock;
+
+        sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
+        assert_se(sock >= 0);
+        safe_close(sock);
+
+        assert_se(drop_privileges(test_uid, test_gid, test_flags) >= 0);
+        assert_se(getuid() == test_uid);
+        assert_se(getgid() == test_gid);
+        show_capabilities();
+
+        sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
+        assert_se(sock < 0);
+}
+
+static void test_drop_privileges_fail(void) {
+        assert_se(drop_privileges(test_uid, test_gid, test_flags) >= 0);
+        assert_se(getuid() == test_uid);
+        assert_se(getgid() == test_gid);
+
+        assert_se(drop_privileges(test_uid, test_gid, test_flags) < 0);
+        assert_se(drop_privileges(0, 0, test_flags) < 0);
+}
+
+static void test_drop_privileges(void) {
+        fork_test(test_drop_privileges_keep_net_raw);
+        fork_test(test_drop_privileges_dontkeep_net_raw);
+        fork_test(test_drop_privileges_fail);
+}
+
+static void test_have_effective_cap(void) {
+        assert_se(have_effective_cap(CAP_KILL));
+        assert_se(have_effective_cap(CAP_CHOWN));
+
+        assert_se(drop_privileges(test_uid, test_gid, test_flags | (1ULL << CAP_KILL)) >= 0);
+        assert_se(getuid() == test_uid);
+        assert_se(getgid() == test_gid);
+
+        assert_se(have_effective_cap(CAP_KILL));
+        assert_se(!have_effective_cap(CAP_CHOWN));
+}
+
+int main(int argc, char *argv[]) {
+        int r;
+
+        log_parse_environment();
+        log_open();
+
+        if (getuid() != 0)
+                return EXIT_TEST_SKIP;
+
+        r = setup_tests();
+        if (r < 0)
+                return -r;
+
+        show_capabilities();
+
+        test_drop_privileges();
+        fork_test(test_have_effective_cap);
+
+        return 0;
+}

commit 8e75477abdd838d3beddc5fd1c6a7707b22748b6
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date:   Sat Jun 21 22:07:09 2014 +0200

    build-sys: add -pthread flag for libsystemd-shared
    
    src/shared/async.c uses pthread so it will fail at link time if we link
    only to libsystemd-shared and use async

diff --git a/Makefile.am b/Makefile.am
index c7653ea..00db82d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -837,7 +837,8 @@ nodist_libsystemd_shared_la_SOURCES = \
 
 libsystemd_shared_la_CFLAGS = \
 	$(AM_CFLAGS) \
-	$(SECCOMP_CFLAGS)
+	$(SECCOMP_CFLAGS) \
+	-pthread
 
 # ------------------------------------------------------------------------------
 noinst_LTLIBRARIES += \



More information about the systemd-commits mailing list