Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Few fundamental checks for chroot(2) and fchroot(2).



details:   https://anonhg.NetBSD.org/src/rev/340b5d8dd158
branches:  trunk
changeset: 766922:340b5d8dd158
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Mon Jul 04 08:09:11 2011 +0000

description:
Few fundamental checks for chroot(2) and fchroot(2).

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    4 +-
 tests/syscall/t_chroot.c    |  313 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 318 insertions(+), 3 deletions(-)

diffs (truncated from 358 to 300 lines):

diff -r e77ae3b18467 -r 340b5d8dd158 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Mon Jul 04 08:07:29 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Mon Jul 04 08:09:11 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.361 2011/07/04 05:10:23 jruoho Exp $
+# $NetBSD: mi,v 1.362 2011/07/04 08:09:11 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -621,6 +621,7 @@
 ./usr/libdata/debug/usr/tests/sbin/resize_ffs                          tests-sbin-tests
 ./usr/libdata/debug/usr/tests/syscall                                  tests-syscall-debug
 ./usr/libdata/debug/usr/tests/syscall/t_access.debug                   tests-syscall-debug     debug,atf
+./usr/libdata/debug/usr/tests/syscall/t_chroot.debug                   tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_cmsg.debug                     tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_dup.debug                      tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_fsync.debug                    tests-syscall-debug     debug,atf
@@ -2372,6 +2373,7 @@
 ./usr/tests/syscall                            tests-syscall-tests
 ./usr/tests/syscall/Atffile                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_access                   tests-syscall-tests     atf
+./usr/tests/syscall/t_chroot                   tests-syscall-tests     atf
 ./usr/tests/syscall/t_cmsg                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_dup                      tests-syscall-tests     atf
 ./usr/tests/syscall/t_fsync                    tests-syscall-tests     atf
diff -r e77ae3b18467 -r 340b5d8dd158 tests/syscall/Makefile
--- a/tests/syscall/Makefile    Mon Jul 04 08:07:29 2011 +0000
+++ b/tests/syscall/Makefile    Mon Jul 04 08:09:11 2011 +0000
@@ -1,10 +1,10 @@
-# $NetBSD: Makefile,v 1.38 2011/07/04 05:10:24 jruoho Exp $
+# $NetBSD: Makefile,v 1.39 2011/07/04 08:09:11 jruoho Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=      ${TESTSBASE}/syscall
 
-TESTS_C+=      t_access t_cmsg t_dup t_fsync
+TESTS_C+=      t_access t_chroot 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_issetugid t_itimer t_kill t_link
diff -r e77ae3b18467 -r 340b5d8dd158 tests/syscall/t_chroot.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_chroot.c  Mon Jul 04 08:09:11 2011 +0000
@@ -0,0 +1,313 @@
+/* $NetBSD: t_chroot.c,v 1.1 2011/07/04 08:09:11 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_chroot.c,v 1.1 2011/07/04 08:09:11 jruoho Exp $");
+
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+ATF_TC(chroot_basic);
+ATF_TC_HEAD(chroot_basic, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "A basic test of chroot(2)");
+       atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(chroot_basic, tc)
+{
+       char buf[PATH_MAX];
+       int fd, sta;
+       pid_t pid;
+
+       (void)memset(buf, '\0', sizeof(buf));
+       (void)getcwd(buf, sizeof(buf));
+       (void)strlcat(buf, "/dir", sizeof(buf));
+
+       ATF_REQUIRE(mkdir(buf, 0500) == 0);
+       ATF_REQUIRE(chdir(buf) == 0);
+
+       pid = fork();
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+
+               if (chroot(buf) != 0)
+                       _exit(EXIT_FAILURE);
+
+               errno = 0;
+
+               if (chroot("/root") != -1)
+                       _exit(EXIT_FAILURE);
+
+               if (errno != ENOENT)
+                       _exit(EXIT_FAILURE);
+
+               fd = open("file", O_RDONLY | O_CREAT, 0600);
+
+               if (fd < 0)
+                       _exit(EXIT_FAILURE);
+
+               if (close(fd) != 0)
+                       _exit(EXIT_FAILURE);
+
+               _exit(EXIT_SUCCESS);
+       }
+
+       (void)wait(&sta);
+
+       if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
+               atf_tc_fail("chroot(2) failed");
+
+       (void)chdir("/");
+       (void)strlcat(buf, "/file", sizeof(buf));
+
+       fd = open(buf, O_RDONLY);
+
+       if (fd < 0)
+               atf_tc_fail("chroot(2) did not change the root directory");
+
+       ATF_REQUIRE(close(fd) == 0);
+       ATF_REQUIRE(unlink(buf) == 0);
+}
+
+ATF_TC(chroot_err);
+ATF_TC_HEAD(chroot_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test error conditions of chroot(2)");
+       atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(chroot_err, tc)
+{
+       char buf[PATH_MAX + 1];
+
+       (void)memset(buf, 'x', sizeof(buf));
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENAMETOOLONG, chroot(buf) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, chroot((void *)-1) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOENT, chroot("/a/b/c/d/e/f/g/h/i/j") == -1);
+}
+
+ATF_TC(chroot_perm);
+ATF_TC_HEAD(chroot_perm, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test permissions with chroot(2)");
+       atf_tc_set_md_var(tc, "require.user", "unprivileged");
+}
+
+ATF_TC_BODY(chroot_perm, tc)
+{
+       static char buf[LINE_MAX];
+       pid_t pid;
+       int sta;
+
+       (void)memset(buf, '\0', sizeof(buf));
+       ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
+
+       pid = fork();
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+
+               errno = 0;
+
+               if (chroot(buf) != -1)
+                       _exit(EXIT_FAILURE);
+
+               if (errno != EPERM)
+                       _exit(EXIT_FAILURE);
+
+               _exit(EXIT_SUCCESS);
+       }
+
+       (void)wait(&sta);
+
+       if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
+               atf_tc_fail("chroot(2) succeeded as unprivileged user");
+}
+
+ATF_TC(fchroot_basic);
+ATF_TC_HEAD(fchroot_basic, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "A basic test of fchroot(2)");
+       atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(fchroot_basic, tc)
+{
+       char buf[PATH_MAX];
+       int fd, sta;
+       pid_t pid;
+
+       (void)memset(buf, '\0', sizeof(buf));
+       (void)getcwd(buf, sizeof(buf));
+       (void)strlcat(buf, "/dir", sizeof(buf));
+
+       ATF_REQUIRE(mkdir(buf, 0500) == 0);
+       ATF_REQUIRE(chdir(buf) == 0);
+
+       fd = open(buf, O_RDONLY);
+       ATF_REQUIRE(fd >= 0);
+
+       pid = fork();
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+
+               if (fchroot(fd) != 0)
+                       _exit(EXIT_FAILURE);
+
+               if (close(fd) != 0)
+                       _exit(EXIT_FAILURE);
+
+               fd = open("file", O_RDONLY | O_CREAT, 0600);
+
+               if (fd < 0)
+                       _exit(EXIT_FAILURE);
+
+               if (close(fd) != 0)
+                       _exit(EXIT_FAILURE);
+
+               _exit(EXIT_SUCCESS);
+       }
+
+       (void)wait(&sta);
+
+       if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
+               atf_tc_fail("fchroot(2) failed");
+
+       (void)chdir("/");
+       (void)strlcat(buf, "/file", sizeof(buf));
+
+       fd = open(buf, O_RDONLY);
+
+       if (fd < 0)
+               atf_tc_fail("fchroot(2) did not change the root directory");
+
+       ATF_REQUIRE(close(fd) == 0);
+       ATF_REQUIRE(unlink(buf) == 0);
+}
+
+ATF_TC(fchroot_err);
+ATF_TC_HEAD(fchroot_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test error conditions of fchroot(2)");
+       atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(fchroot_err, tc)
+{
+       int fd;
+
+       fd = open("/etc/passwd", O_RDONLY);
+       ATF_REQUIRE(fd >= 0);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EBADF, fchroot(-1) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOTDIR, fchroot(fd) == -1);
+
+       ATF_REQUIRE(close(fd) == 0);
+}
+
+ATF_TC(fchroot_perm);



Home | Main Index | Thread Index | Old Index