tests: Add setresgid unprivileged user test
authorAndreas Schneider <asn@samba.org>
Tue, 27 Oct 2015 09:42:35 +0000 (10:42 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 27 Oct 2015 13:56:11 +0000 (14:56 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
tests/CMakeLists.txt
tests/test_setresgid_unprivileged.c [new file with mode: 0644]

index cfb45ec4e6439a32160beed059482bce0d216e35..50a505d4d9ae7ea549b1442b4e574312506694ae 100644 (file)
@@ -47,6 +47,10 @@ endif (HAVE_SETREGID)
 
 if (HAVE_SETRESGID)
     list(APPEND UWRAP_GID_TESTS test_setresgid)
+
+    if (HAVE_GETRESGID)
+        list(APPEND UWRAP_GID_TESTS test_setresgid_unprivileged)
+    endif (HAVE_GETRESGID)
 endif (HAVE_SETRESGID)
 
 set(UWRAP_TESTS
diff --git a/tests/test_setresgid_unprivileged.c b/tests/test_setresgid_unprivileged.c
new file mode 100644 (file)
index 0000000..121c2cc
--- /dev/null
@@ -0,0 +1,147 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <pwd.h>
+
+static void test_uwrap_setresgid_unprivileged_uid(void **state)
+{
+       int rc;
+       gid_t cp_rgid, cp_egid, cp_sgid;
+       gid_t cp_ruid, cp_euid, cp_suid;
+
+       (void) state; /* unused */
+
+       rc = setresuid(0x0, 0x9999, 0x9999);
+       assert_return_code(rc, errno);
+
+       cp_ruid = cp_euid = cp_suid = -1;
+       rc = getresuid(&cp_ruid, &cp_euid, &cp_suid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_ruid, 0x0);
+       assert_int_equal(cp_euid, 0x9999);
+       assert_int_equal(cp_suid, 0x9999);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x0);
+       assert_int_equal(cp_egid, 0x0);
+       assert_int_equal(cp_sgid, 0x0);
+
+
+       rc = setresgid(-1, -1, -1);
+       assert_return_code(rc, errno);
+
+       rc = setresgid(0x4444, -1, -1);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x0);
+       assert_int_equal(cp_egid, 0x0);
+       assert_int_equal(cp_sgid, 0x0);
+
+       rc = setresgid(-1, 0x5555, -1);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x0);
+       assert_int_equal(cp_egid, 0x0);
+       assert_int_equal(cp_sgid, 0x0);
+
+       rc = setresgid(-1, -1, 0x6666);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x0);
+       assert_int_equal(cp_egid, 0x0);
+       assert_int_equal(cp_sgid, 0x0);
+
+       setresuid(0x0, 0x0, 0x0);
+}
+
+static void test_uwrap_setresgid_unprivileged_uid_and_gid(void **state)
+{
+       int rc;
+       gid_t cp_rgid, cp_egid, cp_sgid;
+       gid_t cp_ruid, cp_euid, cp_suid;
+
+       (void) state; /* unused */
+
+       rc = setresgid(0x4444, 0x5555, 0x6666);
+       assert_return_code(rc, errno);
+
+       rc = setresuid(0x0, 0x9999, 0x9999);
+       assert_return_code(rc, errno);
+
+       cp_ruid = cp_euid = cp_suid = -1;
+       rc = getresuid(&cp_ruid, &cp_euid, &cp_suid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_ruid, 0x0);
+       assert_int_equal(cp_euid, 0x9999);
+       assert_int_equal(cp_suid, 0x9999);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x4444);
+       assert_int_equal(cp_egid, 0x5555);
+       assert_int_equal(cp_sgid, 0x6666);
+
+       rc = setresgid(0x5555, 0x6666, 0x4444);
+       assert_return_code(rc, errno);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x5555);
+       assert_int_equal(cp_egid, 0x6666);
+       assert_int_equal(cp_sgid, 0x4444);
+
+       rc = setresgid(0x5555, 0x4444, -1);
+       assert_return_code(rc, errno);
+
+       cp_rgid = cp_egid = cp_sgid = -1;
+       rc = getresgid(&cp_rgid, &cp_egid, &cp_sgid);
+       assert_return_code(rc, errno);
+       assert_int_equal(cp_rgid, 0x5555);
+       assert_int_equal(cp_egid, 0x4444);
+       assert_int_equal(cp_sgid, 0x4444);
+
+       rc = setresgid(0x1111, 0x2222, 0x3333);
+       assert_int_equal(rc, -1);
+       assert_int_equal(errno, EPERM);
+
+       setresuid(0x0, 0x0, 0x0);
+       setresgid(0x0, 0x0, 0x0);
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest uwrap_tests[] = {
+               cmocka_unit_test(test_uwrap_setresgid_unprivileged_uid),
+               cmocka_unit_test(test_uwrap_setresgid_unprivileged_uid_and_gid),
+       };
+
+       rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);
+
+       return rc;
+}