Home | History | Annotate | Line # | Download | only in vfs
      1 /*	$NetBSD: t_unpriv.c,v 1.16 2018/11/28 10:01:28 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/stat.h>
     30 #include <sys/time.h>
     31 
     32 #include <atf-c.h>
     33 #include <libgen.h>
     34 #include <limits.h>
     35 #include <unistd.h>
     36 
     37 #include <rump/rump_syscalls.h>
     38 #include <rump/rump.h>
     39 
     40 #include "../common/h_fsmacros.h"
     41 #include "h_macros.h"
     42 
     43 #define USES_OWNER							 \
     44 	if (FSTYPE_MSDOS(tc))						 \
     45 	    atf_tc_skip("owner not supported by file system")
     46 
     47 static void
     48 owner(const atf_tc_t *tc, const char *mp)
     49 {
     50 
     51 	USES_OWNER;
     52 
     53 	FSTEST_ENTER();
     54 
     55 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     56 	if (rump_sys_setuid(1) == -1)
     57 		atf_tc_fail_errno("setuid");
     58         if (rump_sys_chown(".", 1, -1) != -1 || errno != EPERM)
     59 		atf_tc_fail_errno("chown");
     60         if (rump_sys_chmod(".", 0000) != -1 || errno != EPERM)
     61                 atf_tc_fail_errno("chmod");
     62 	rump_pub_lwproc_releaselwp();
     63 
     64 	if (rump_sys_chown(".", 1, -1) == -1)
     65 		atf_tc_fail_errno("chown");
     66 
     67 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     68 	if (rump_sys_setuid(1) == -1)
     69 		atf_tc_fail_errno("setuid");
     70         if (rump_sys_chown(".", 1, -1) == -1)
     71 		atf_tc_fail_errno("chown");
     72         if (rump_sys_chmod(".", 0000) == -1)
     73                 atf_tc_fail_errno("chmod");
     74 	rump_pub_lwproc_releaselwp();
     75 
     76 	FSTEST_EXIT();
     77 }
     78 
     79 static void
     80 dirperms(const atf_tc_t *tc, const char *mp)
     81 {
     82 	char name[] = "dir.test/file.test";
     83 	char *dir = dirname(name);
     84 	int fd;
     85 
     86 	if (FSTYPE_SYSVBFS(tc))
     87 		atf_tc_skip("directories not supported by file system");
     88 
     89 	FSTEST_ENTER();
     90 
     91 	if (rump_sys_mkdir(dir, 0777) == -1)
     92 		atf_tc_fail_errno("mkdir");
     93 
     94 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     95 	if (rump_sys_setuid(1) == -1)
     96 		atf_tc_fail_errno("setuid");
     97         if (rump_sys_open(name, O_RDWR|O_CREAT, 0666) != -1 || errno != EACCES)
     98 		atf_tc_fail_errno("open");
     99 	rump_pub_lwproc_releaselwp();
    100 
    101 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
    102 		atf_tc_fail_errno("open");
    103 	if (rump_sys_close(fd) == -1)
    104 		atf_tc_fail_errno("close");
    105 
    106 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    107 	if (rump_sys_setuid(1) == -1)
    108 		atf_tc_fail_errno("setuid");
    109         if (rump_sys_unlink(name) != -1 || errno != EACCES)
    110 		atf_tc_fail_errno("unlink");
    111 	rump_pub_lwproc_releaselwp();
    112 
    113         if (rump_sys_unlink(name) == -1)
    114 		atf_tc_fail_errno("unlink");
    115 
    116 	if (rump_sys_rmdir(dir) == -1)
    117 		atf_tc_fail_errno("rmdir");
    118 
    119 	FSTEST_EXIT();
    120 }
    121 
    122 static void
    123 times(const atf_tc_t *tc, const char *mp)
    124 {
    125 	const char *name = "file.test";
    126 	int fd;
    127 	unsigned int i, j;
    128 	struct timeval tmv[2];
    129 	static struct timeval tmvs[] = {
    130 		{ QUAD_MIN, 0 },
    131 		{ 0, 0 },
    132 		{ QUAD_MAX, 999999 }
    133 	};
    134 
    135 	FSTEST_ENTER();
    136 
    137 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
    138 		atf_tc_fail_errno("open");
    139 	if (rump_sys_close(fd) == -1)
    140 		atf_tc_fail_errno("close");
    141 
    142 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    143 	if (rump_sys_setuid(1) == -1)
    144 		atf_tc_fail_errno("setuid");
    145 	if (rump_sys_utimes(name, NULL) != -1 || errno != EACCES)
    146 		atf_tc_fail_errno("utimes");
    147 	rump_pub_lwproc_releaselwp();
    148 
    149 	if (rump_sys_utimes(name, NULL) == -1)
    150 		atf_tc_fail_errno("utimes");
    151 
    152 	for (i = 0; i < sizeof(tmvs) / sizeof(tmvs[0]); i++) {
    153 		for (j = 0; j < sizeof(tmvs) / sizeof(tmvs[0]); j++) {
    154 			tmv[0] = tmvs[i];
    155 			tmv[1] = tmvs[j];
    156 			rump_pub_lwproc_rfork(RUMP_RFCFDG);
    157 			if (rump_sys_setuid(1) == -1)
    158 				atf_tc_fail_errno("setuid");
    159 			if (rump_sys_utimes(name, tmv) != -1 || errno != EPERM)
    160 				atf_tc_fail_errno("utimes");
    161 			rump_pub_lwproc_releaselwp();
    162 
    163 			if (rump_sys_utimes(name, tmv) == -1)
    164 				atf_tc_fail_errno("utimes");
    165 		}
    166 	}
    167 
    168 	if (rump_sys_unlink(name) == -1)
    169 		atf_tc_fail_errno("unlink");
    170 
    171 	FSTEST_EXIT();
    172 }
    173 
    174 static void
    175 flags(const atf_tc_t *tc, const char *mp)
    176 {
    177 	const char *name = "file.test";
    178 	int fd, fflags;
    179 	struct stat st;
    180 
    181 	FSTEST_ENTER();
    182 
    183 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
    184 		atf_tc_fail_errno("open");
    185 	if (rump_sys_close(fd) == -1)
    186 		atf_tc_fail_errno("close");
    187 
    188 	if (rump_sys_stat(name, &st) == -1)
    189 		atf_tc_fail_errno("stat");
    190 	if (rump_sys_chflags(name, st.st_flags) == -1) {
    191 		if (errno == EOPNOTSUPP)
    192 			atf_tc_skip("file flags not supported by file system");
    193 		atf_tc_fail_errno("chflags");
    194 	}
    195 
    196 	fflags = st.st_flags | UF_NODUMP;
    197 
    198 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    199 	if (rump_sys_setuid(1) == -1)
    200 		atf_tc_fail_errno("setuid");
    201 	fflags |= UF_NODUMP;
    202 	if (rump_sys_chflags(name, fflags) != -1 || errno != EPERM)
    203 		atf_tc_fail_errno("chflags");
    204 	rump_pub_lwproc_releaselwp();
    205 
    206 	if (rump_sys_chflags(name, fflags) == -1)
    207 		atf_tc_fail_errno("chflags");
    208 
    209 	fflags &= ~UF_NODUMP;
    210 	if (rump_sys_chflags(name, fflags) == -1)
    211 		atf_tc_fail_errno("chflags");
    212 
    213 	if (rump_sys_unlink(name) == -1)
    214 		atf_tc_fail_errno("unlink");
    215 
    216 	FSTEST_EXIT();
    217 }
    218 
    219 ATF_TC_FSAPPLY(owner, "owner unprivileged checks");
    220 ATF_TC_FSAPPLY(dirperms, "directory permission checks");
    221 ATF_TC_FSAPPLY(times, "time set checks");
    222 ATF_TC_FSAPPLY(flags, "file flags checks");
    223 
    224 ATF_TP_ADD_TCS(tp)
    225 {
    226 
    227 	ATF_TP_FSAPPLY(owner);
    228 	ATF_TP_FSAPPLY(dirperms);
    229 	ATF_TP_FSAPPLY(times);
    230 	ATF_TP_FSAPPLY(flags);
    231 
    232 	return atf_no_error();
    233 }
    234