>From 43a450c869f5593b1357c51d384c9ab7cb6d2b75 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sun, 27 Jul 2014 14:44:24 +0200 Subject: [PATCH 3/3] TESTS: Add a test to change user IDs Adds a unit test using the nss_wrapper and uid_wrapper libraries that exercises the ability to become another user. --- Makefile.am | 3 + configure.ac | 2 +- contrib/ci/deps.sh | 2 + contrib/sssd.spec.in | 4 + src/tests/cwrap/Makefile.am | 47 +++++++++++ src/tests/cwrap/cwrap_test_setup.sh | 17 ++++ src/tests/cwrap/group | 1 + src/tests/cwrap/passwd | 1 + src/tests/cwrap/test_become_user.c | 156 ++++++++++++++++++++++++++++++++++++ 9 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/tests/cwrap/Makefile.am create mode 100755 src/tests/cwrap/cwrap_test_setup.sh create mode 100644 src/tests/cwrap/group create mode 100644 src/tests/cwrap/passwd create mode 100644 src/tests/cwrap/test_become_user.c diff --git a/Makefile.am b/Makefile.am index ac3f26cad6e1d0a5ac8979ce13286bc896c62b62..ee6e39ba7f818524884ab9e4efc4e78791af1122 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,6 +19,8 @@ if HAVE_MANPAGES SUBDIRS += src/man endif +SUBDIRS += . src/tests/cwrap + # Some old versions of automake don't define builddir builddir ?= . @@ -2829,6 +2831,7 @@ endif CLEANFILES = *.X */*.X */*/*.X tests: all $(check_PROGRAMS) + (cd src/tests/cwrap && $(MAKE) $(AM_MAKEFLAGS) $@) || exit 1; # RPM-related tasks diff --git a/configure.ac b/configure.ac index 1edf4cb19e390ced2043353341d321425652da71..660ea8d373272917d2d6ceeafdccf5dddbe493bc 100644 --- a/configure.ac +++ b/configure.ac @@ -335,7 +335,7 @@ AC_SUBST([abs_builddir], $abs_build_dir) AC_CONFIG_FILES([Makefile contrib/sssd.spec src/examples/rwtab src/doxy.config src/sysv/sssd src/sysv/gentoo/sssd src/sysv/SUSE/sssd - po/Makefile.in src/man/Makefile + po/Makefile.in src/man/Makefile src/tests/cwrap/Makefile src/providers/ipa/ipa_hbac.pc src/providers/ipa/ipa_hbac.doxy src/lib/idmap/sss_idmap.pc src/lib/idmap/sss_idmap.doxy src/sss_client/sudo/sss_sudo.doxy diff --git a/contrib/ci/deps.sh b/contrib/ci/deps.sh index 998d2858b6aab6032782266c6e679a703ae28ffc..37a8e46c03d269281cbfc9f0b2137c0c25754af7 100644 --- a/contrib/ci/deps.sh +++ b/contrib/ci/deps.sh @@ -35,6 +35,8 @@ if [[ "$DISTRO_BRANCH" == -redhat-* ]]; then libcmocka-devel mock rpm-build + uid_wrapper + nss_wrapper ) _DEPS_LIST_SPEC=` sed -e 's/@PACKAGE_VERSION@/0/g' \ diff --git a/contrib/sssd.spec.in b/contrib/sssd.spec.in index 611730c1928b5bbfaa2aee50967226c5824bbca0..529304205274ad39e4281e3d298222420d5eb439 100644 --- a/contrib/sssd.spec.in +++ b/contrib/sssd.spec.in @@ -114,6 +114,10 @@ BuildRequires: selinux-policy-targeted %if 0%{?fedora} BuildRequires: libcmocka-devel %endif +%if (0%{?fedora} >= 20) +BuildRequires: uid_wrapper +BuildRequires: nss_wrapper +%endif %if (0%{?fedora} || 0%{?rhel} >= 7) BuildRequires: libnl3-devel %else diff --git a/src/tests/cwrap/Makefile.am b/src/tests/cwrap/Makefile.am new file mode 100644 index 0000000000000000000000000000000000000000..34aec92c197a60c47067c72df9c8f3644e1a3c45 --- /dev/null +++ b/src/tests/cwrap/Makefile.am @@ -0,0 +1,47 @@ +AM_CPPFLAGS = \ + -Wall \ + -I$(top_srcdir)/src \ + -I. \ + $(DBUS_CFLAGS) \ + $(NULL) + +TESTS_ENVIRONMENT = \ + CWRAP_TEST_SRCDIR=$(abs_srcdir) \ + . $(srcdir)/cwrap_test_setup.sh; \ + $(AUX_TESTS_ENVIRONMENT) \ + $(NULL) + +dist_noinst_SCRIPTS = \ + cwrap_test_setup.sh \ + $(NULL) + +dist_noinst_DATA = \ + group \ + passwd \ + $(NULL) + +check_PROGRAMS = +if HAVE_CMOCKA +if HAVE_NSS_WRAPPER +if HAVE_UID_WRAPPER +check_PROGRAMS += become_user-tests +endif # HAVE_UID_WRAPPER +endif # HAVE_NSS_WRAPPER +endif # HAVE_CMOCKA + +TESTS = $(check_PROGRAMS) + +become_user_tests_SOURCES = \ + test_become_user.c \ + $(NULL) +become_user_tests_CFLAGS = \ + $(AM_CFLAGS) \ + $(NULL) +become_user_tests_LDADD = \ + $(POPT_LIBS) \ + $(CMOCKA_LIBS) \ + $(abs_top_builddir)/libsss_debug.la \ + $(abs_top_builddir)/libsss_test_common.la \ + $(NULL) + +tests: $(check_PROGRAMS) diff --git a/src/tests/cwrap/cwrap_test_setup.sh b/src/tests/cwrap/cwrap_test_setup.sh new file mode 100755 index 0000000000000000000000000000000000000000..0d35cb7e5f94a4ee035bbc794640f1dd79b17649 --- /dev/null +++ b/src/tests/cwrap/cwrap_test_setup.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +pkg-config --exists nss_wrapper || exit 1 +pkg-config --exists uid_wrapper || exit 1 + +nss_wrapper=$(pkg-config --libs nss_wrapper) +uid_wrapper=$(pkg-config --libs uid_wrapper) +if [ -z $nss_wrapper -o -z $uid_wrapper ]; then + echo "Cannot locate cwrap libraries" + exit 2 +fi + +export LD_PRELOAD="$nss_wrapper $uid_wrapper" +export NSS_WRAPPER_PASSWD=$CWRAP_TEST_SRCDIR/passwd +export NSS_WRAPPER_GROUP=$CWRAP_TEST_SRCDIR/group +export UID_WRAPPER=1 +export UID_WRAPPER_ROOT=1 diff --git a/src/tests/cwrap/group b/src/tests/cwrap/group new file mode 100644 index 0000000000000000000000000000000000000000..61e428c41a3df9d18cd216864d90c7f2603e8820 --- /dev/null +++ b/src/tests/cwrap/group @@ -0,0 +1 @@ +sssd:x:123: diff --git a/src/tests/cwrap/passwd b/src/tests/cwrap/passwd new file mode 100644 index 0000000000000000000000000000000000000000..aa0a97db5259172c0b4ab47c7c2346fa5c2aa88e --- /dev/null +++ b/src/tests/cwrap/passwd @@ -0,0 +1 @@ +sssd:x:123:123:sssd unprivileged user:/:/sbin/nologin diff --git a/src/tests/cwrap/test_become_user.c b/src/tests/cwrap/test_become_user.c new file mode 100644 index 0000000000000000000000000000000000000000..06d3ad425c4928e9e9bff661fbb8f7b4536b8896 --- /dev/null +++ b/src/tests/cwrap/test_become_user.c @@ -0,0 +1,156 @@ +/* + Authors: + Jakub Hrozek + + Copyright (C) 2014 Red Hat + + SSSD tests: User switching + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* Yes, a .c file. We need to call static functions during the test */ +#include "../../../src/util/become_user.c" + +#include +#include "util/util.h" +#include "tests/cmocka/common_mock.h" + +void test_become_user(void **state) +{ + struct passwd *sssd; + errno_t ret; + pid_t pid, wpid; + int status; + + /* Must root as root, real or fake */ + assert_int_equal(geteuid(), 0); + + sssd = getpwnam("sssd"); + assert_non_null(sssd); + + pid = fork(); + if (pid == 0) { + /* Change the UID in a child */ + ret = become_user(sssd->pw_uid, sssd->pw_gid); + assert_int_equal(ret, EOK); + + /* Make sure we have the requested UID and GID now and there + * are no supplementary groups + */ + assert_int_equal(geteuid(), sssd->pw_uid); + assert_int_equal(getegid(), sssd->pw_gid); + assert_int_equal(getuid(), sssd->pw_uid); + assert_int_equal(getgid(), sssd->pw_gid); + + /* Another become_user is a no-op */ + ret = become_user(sssd->pw_uid, sssd->pw_gid); + assert_int_equal(ret, EOK); + + assert_int_equal(getgroups(0, NULL), 0); + exit(0); + } + + assert_int_not_equal(pid, -1); + + wpid = waitpid(pid, &status, 0); + assert_int_equal(wpid, pid); + assert_true(WIFEXITED(status)); + assert_int_equal(WEXITSTATUS(status), 0); +} + +void test_switch_user(void **state) +{ + errno_t ret; + struct passwd *sssd; + TALLOC_CTX *tmp_ctx; + struct sss_creds *saved_creds; + + check_leaks_push(global_talloc_context); + tmp_ctx = talloc_new(global_talloc_context); + assert_non_null(tmp_ctx); + + /* Must root as root, real or fake */ + assert_int_equal(geteuid(), 0); + + sssd = getpwnam("sssd"); + assert_non_null(sssd); + + check_leaks_push(tmp_ctx); + + ret = switch_creds(tmp_ctx, sssd->pw_uid, sssd->pw_gid, + 0, NULL, &saved_creds); + assert_int_equal(ret, EOK); + assert_int_equal(geteuid(), sssd->pw_uid); + assert_int_equal(getegid(), sssd->pw_gid); + /* Only effective UID is changed.. */ + assert_int_equal(getuid(), 0); + assert_int_equal(getgid(), 0); + + assert_non_null(saved_creds); + assert_int_equal(saved_creds->uid, 0); + assert_int_equal(saved_creds->gid, 0); + + /* restore root */ + ret = restore_creds(saved_creds); + assert_int_equal(ret, EOK); + assert_int_equal(geteuid(), 0); + assert_int_equal(getegid(), 0); + assert_int_equal(getuid(), 0); + assert_int_equal(getgid(), 0); + + talloc_free(saved_creds); + check_leaks_pop(tmp_ctx); + talloc_free(tmp_ctx); + check_leaks_pop(global_talloc_context); +} + +int main(int argc, const char *argv[]) +{ + poptContext pc; + int opt; + struct poptOption long_options[] = { + POPT_AUTOHELP + SSSD_DEBUG_OPTS + POPT_TABLEEND + }; + + const UnitTest tests[] = { + unit_test(test_become_user), + unit_test(test_switch_user), + }; + + /* Set debug level to invalid value so we can deside if -d 0 was used. */ + debug_level = SSSDBG_INVALID; + + pc = poptGetContext(argv[0], argc, argv, long_options, 0); + while((opt = poptGetNextOpt(pc)) != -1) { + switch(opt) { + default: + fprintf(stderr, "\nInvalid option %s: %s\n\n", + poptBadOption(pc, 0), poptStrerror(opt)); + poptPrintUsage(pc, stderr, 0); + return 1; + } + } + poptFreeContext(pc); + + DEBUG_CLI_INIT(debug_level); + + /* Even though normally the tests should clean up after themselves + * they might not after a failed run. Remove the old db to be sure */ + tests_set_cwd(); + + return run_tests(tests); +} -- 1.9.3