Module Name:    src
Committed By:   jruoho
Date:           Mon Jul  4 05:10:24 UTC 2011

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/syscall: Makefile
Added Files:
        src/tests/syscall: t_issetugid.c

Log Message:
Basic tests for issetugid(2).


To generate a diff of this commit:
cvs rdiff -u -r1.360 -r1.361 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.37 -r1.38 src/tests/syscall/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/syscall/t_issetugid.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.360 src/distrib/sets/lists/tests/mi:1.361
--- src/distrib/sets/lists/tests/mi:1.360	Sun Jul  3 21:06:56 2011
+++ src/distrib/sets/lists/tests/mi	Mon Jul  4 05:10:23 2011
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.360 2011/07/03 21:06:56 jruoho Exp $
+# $NetBSD: mi,v 1.361 2011/07/04 05:10:23 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -630,6 +630,7 @@
 ./usr/libdata/debug/usr/tests/syscall/t_getrusage.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_getsid.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_gettimeofday.debug		tests-syscall-debug	debug,atf
+./usr/libdata/debug/usr/tests/syscall/t_issetugid.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_itimer.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_kill.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_link.debug			tests-syscall-debug	debug,atf
@@ -2380,6 +2381,7 @@
 ./usr/tests/syscall/t_getrusage			tests-syscall-tests	atf
 ./usr/tests/syscall/t_getsid			tests-syscall-tests	atf
 ./usr/tests/syscall/t_gettimeofday		tests-syscall-tests	atf
+./usr/tests/syscall/t_issetugid			tests-syscall-tests	atf
 ./usr/tests/syscall/t_itimer			tests-syscall-tests	atf
 ./usr/tests/syscall/t_kill			tests-syscall-tests	atf
 ./usr/tests/syscall/t_link			tests-syscall-tests	atf

Index: src/tests/syscall/Makefile
diff -u src/tests/syscall/Makefile:1.37 src/tests/syscall/Makefile:1.38
--- src/tests/syscall/Makefile:1.37	Sun Jul  3 21:06:56 2011
+++ src/tests/syscall/Makefile	Mon Jul  4 05:10:24 2011
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.37 2011/07/03 21:06:56 jruoho Exp $
+# $NetBSD: Makefile,v 1.38 2011/07/04 05:10:24 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -7,7 +7,7 @@
 TESTS_C+=	t_access t_cmsg t_dup t_fsync
 TESTS_C+=	t_getgroups t_getlogin t_getpid
 TESTS_C+=	t_getrusage t_getsid t_gettimeofday
-TESTS_C+=	t_itimer t_kill t_link
+TESTS_C+=	t_issetugid t_itimer t_kill t_link
 TESTS_C+=	t_mincore t_mknod t_mmap t_mprotect t_msync t_nanosleep
 TESTS_C+=	t_poll t_pollts t_pselect t_revoke
 TESTS_C+=	t_setrlimit t_setuid t_stat t_timer t_truncate

Added files:

Index: src/tests/syscall/t_issetugid.c
diff -u /dev/null src/tests/syscall/t_issetugid.c:1.1
--- /dev/null	Mon Jul  4 05:10:24 2011
+++ src/tests/syscall/t_issetugid.c	Mon Jul  4 05:10:24 2011
@@ -0,0 +1,148 @@
+/* $NetBSD: t_issetugid.c,v 1.1 2011/07/04 05:10:24 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_issetugid.c,v 1.1 2011/07/04 05:10:24 jruoho Exp $");
+
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static bool check(int (*fuid)(uid_t), int (*fgid)(gid_t));
+
+static bool
+check(int (*fuid)(uid_t), int (*fgid)(gid_t))
+{
+	struct passwd *pw;
+	pid_t pid;
+	int sta;
+
+	pw = getpwnam("nobody");
+
+	if (pw == NULL)
+		return false;
+
+	pid = fork();
+
+	if (pid < 0)
+		return false;
+
+	if (pid == 0) {
+
+		if (fuid != NULL && (*fuid)(pw->pw_uid) != 0)
+			_exit(EXIT_FAILURE);
+
+		if (fgid != NULL && (*fgid)(pw->pw_gid) != 0)
+			_exit(EXIT_FAILURE);
+
+		if (issetugid() != 1)
+			_exit(EXIT_FAILURE);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
+		return false;
+
+	return true;
+}
+
+ATF_TC(issetugid_egid);
+ATF_TC_HEAD(issetugid_egid, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A test of issetugid(2), eff. GID");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(issetugid_egid, tc)
+{
+
+	if (check(NULL, setegid) != true)
+		atf_tc_fail("issetugid(2) failed with effective GID");
+}
+
+ATF_TC(issetugid_euid);
+ATF_TC_HEAD(issetugid_euid, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A test of issetugid(2), eff. UID");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(issetugid_euid, tc)
+{
+
+	if (check(seteuid, NULL) != true)
+		atf_tc_fail("issetugid(2) failed with effective UID");
+}
+
+ATF_TC(issetugid_rgid);
+ATF_TC_HEAD(issetugid_rgid, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A test of issetugid(2), real GID");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(issetugid_rgid, tc)
+{
+
+	if (check(NULL, setgid) != true)
+		atf_tc_fail("issetugid(2) failed with real GID");
+}
+
+ATF_TC(issetugid_ruid);
+ATF_TC_HEAD(issetugid_ruid, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A test of issetugid(2), real UID");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(issetugid_ruid, tc)
+{
+
+	if (check(setuid, NULL) != true)
+		atf_tc_fail("issetugid(2) failed with real UID");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, issetugid_egid);
+	ATF_TP_ADD_TC(tp, issetugid_euid);
+	ATF_TP_ADD_TC(tp, issetugid_rgid);
+	ATF_TP_ADD_TC(tp, issetugid_ruid);
+
+	return atf_no_error();
+}

Reply via email to