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