Home | History | Annotate | Line # | Download | only in kernel
      1 /*	$NetBSD: t_proccwd.c,v 1.2 2019/06/01 22:18:23 kamil Exp $	*/
      2 /*-
      3  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __COPYRIGHT("@(#) Copyright (c) 2019\
     30  The NetBSD Foundation, inc. All rights reserved.");
     31 __RCSID("$NetBSD: t_proccwd.c,v 1.2 2019/06/01 22:18:23 kamil Exp $");
     32 
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 #include <sys/sysctl.h>
     36 #include <sys/wait.h>
     37 
     38 #include <errno.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <err.h>
     43 
     44 #include <atf-c.h>
     45 
     46 static int
     47 getproccwd(char *path, size_t *len, pid_t pid)
     48 {
     49 	const int name[] = {
     50 	    CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_CWD,
     51 	};
     52 
     53      	return sysctl(name, __arraycount(name), path, len, NULL, 0);
     54 }
     55 
     56 ATF_TC(prompt_pid);
     57 ATF_TC_HEAD(prompt_pid, tc)
     58 {
     59 
     60         atf_tc_set_md_var(tc, "descr",
     61             "Prompt length of the current dir and assert that it is sane");
     62 }
     63 
     64 ATF_TC_BODY(prompt_pid, tc)
     65 {
     66 	char buf[MAXPATHLEN];
     67 	char cwdbuf[MAXPATHLEN];
     68 	size_t len, prompted_len;
     69 	size_t i;
     70 
     71 	pid_t t[] = {
     72 		-1,
     73 		getpid(),
     74 		1 /* /sbin/init */
     75 	};
     76 
     77 	for (i = 0; i < __arraycount(t); i++) {
     78 		len = 0;
     79 		ATF_REQUIRE_EQ(getproccwd(NULL, &len, t[i]), 0);
     80 
     81 		prompted_len = len;
     82 		ATF_REQUIRE_EQ(getproccwd(buf, &len, t[i]), 0);
     83 
     84 		ATF_REQUIRE_EQ(strlen(buf) + 1, prompted_len);
     85 		ATF_REQUIRE(strlen(buf) > 0);
     86 
     87 		if (t[i] == -1 || t[i] == getpid()) {
     88 			getcwd(cwdbuf, MAXPATHLEN);
     89 			ATF_REQUIRE_EQ(strcmp(buf, cwdbuf), 0);
     90 			ATF_REQUIRE(strlen(buf) > strlen("/"));
     91 		} else if (t[i] == 1) {
     92 			ATF_REQUIRE_EQ(strcmp(buf, "/"), 0);
     93 		}
     94 	}
     95 }
     96 
     97 ATF_TC(chroot);
     98 ATF_TC_HEAD(chroot, tc)
     99 {
    100 
    101         atf_tc_set_md_var(tc, "descr",
    102             "prompt length of the current dir and assert that it is right");
    103 
    104 	atf_tc_set_md_var(tc, "require.user", "root");
    105 }
    106 
    107 #define ASSERT(x) do { if (!(x)) _exit(EXIT_FAILURE); } while (0)
    108 
    109 ATF_TC_BODY(chroot, tc)
    110 {
    111 	char buf[MAXPATHLEN];
    112 	struct stat root_dir;
    113 	struct stat cur_dir;
    114 	size_t len;
    115 	pid_t child, wpid;
    116 	int status;
    117 	int rv;
    118 
    119 	const pid_t pid_one = 1; /* /sbin/init */
    120 
    121 	ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
    122 
    123 	ATF_REQUIRE_EQ(stat(buf, &cur_dir), 0);
    124 	ATF_REQUIRE_EQ(stat("/", &root_dir), 0);
    125 
    126 	if (cur_dir.st_ino == root_dir.st_ino)
    127 		atf_tc_skip("This test does not work in /");
    128 
    129 	child = atf_utils_fork();
    130 	if (child == 0) {
    131 		len = 0;
    132 
    133 		ASSERT(chroot(buf) == 0);
    134 
    135 		errno = 0;
    136 		rv = getproccwd(buf, &len, pid_one);
    137 		ASSERT(rv == -1);
    138 		ASSERT(errno == ENOENT);
    139 
    140 		_exit(EXIT_SUCCESS);
    141 	}
    142 	wpid = waitpid(child, &status, WEXITED);
    143 	ATF_REQUIRE_EQ(wpid, child);
    144 	ATF_REQUIRE_EQ(WEXITSTATUS(status), EXIT_SUCCESS);
    145 }
    146 
    147 ATF_TP_ADD_TCS(tp)
    148 {
    149 
    150 	ATF_TP_ADD_TC(tp, prompt_pid);
    151 	ATF_TP_ADD_TC(tp, chroot);
    152 
    153 	return atf_no_error();
    154 }
    155