Home | History | Annotate | Line # | Download | only in vfs
t_unpriv.c revision 1.16.12.2
      1  1.16.12.2  perseant /*	$NetBSD: t_unpriv.c,v 1.16.12.2 2025/08/02 05:58:00 perseant 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.9     njoly #include <sys/stat.h>
     30        1.6     njoly #include <sys/time.h>
     31        1.6     njoly 
     32        1.1     njoly #include <atf-c.h>
     33        1.5     njoly #include <libgen.h>
     34       1.11      gson #include <limits.h>
     35        1.1     njoly #include <unistd.h>
     36        1.1     njoly 
     37        1.1     njoly #include <rump/rump_syscalls.h>
     38        1.1     njoly #include <rump/rump.h>
     39        1.1     njoly 
     40        1.1     njoly #include "../common/h_fsmacros.h"
     41       1.13  christos #include "h_macros.h"
     42        1.1     njoly 
     43        1.2     njoly #define USES_OWNER							 \
     44  1.16.12.1  perseant 	if (FSTYPE_MSDOS(tc) || FSTYPE_EXFATFS(tc))			 \
     45        1.1     njoly 	    atf_tc_skip("owner not supported by file system")
     46        1.1     njoly 
     47        1.1     njoly static void
     48        1.1     njoly owner(const atf_tc_t *tc, const char *mp)
     49        1.1     njoly {
     50        1.1     njoly 
     51        1.2     njoly 	USES_OWNER;
     52        1.1     njoly 
     53        1.1     njoly 	FSTEST_ENTER();
     54        1.1     njoly 
     55        1.1     njoly 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     56        1.1     njoly 	if (rump_sys_setuid(1) == -1)
     57        1.1     njoly 		atf_tc_fail_errno("setuid");
     58        1.1     njoly         if (rump_sys_chown(".", 1, -1) != -1 || errno != EPERM)
     59        1.1     njoly 		atf_tc_fail_errno("chown");
     60        1.1     njoly         if (rump_sys_chmod(".", 0000) != -1 || errno != EPERM)
     61        1.1     njoly                 atf_tc_fail_errno("chmod");
     62        1.1     njoly 	rump_pub_lwproc_releaselwp();
     63        1.1     njoly 
     64        1.1     njoly 	if (rump_sys_chown(".", 1, -1) == -1)
     65        1.1     njoly 		atf_tc_fail_errno("chown");
     66        1.1     njoly 
     67        1.1     njoly 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     68        1.1     njoly 	if (rump_sys_setuid(1) == -1)
     69        1.1     njoly 		atf_tc_fail_errno("setuid");
     70        1.1     njoly         if (rump_sys_chown(".", 1, -1) == -1)
     71        1.1     njoly 		atf_tc_fail_errno("chown");
     72        1.1     njoly         if (rump_sys_chmod(".", 0000) == -1)
     73        1.1     njoly                 atf_tc_fail_errno("chmod");
     74        1.1     njoly 	rump_pub_lwproc_releaselwp();
     75        1.1     njoly 
     76        1.1     njoly 	FSTEST_EXIT();
     77        1.1     njoly }
     78        1.1     njoly 
     79        1.5     njoly static void
     80        1.5     njoly dirperms(const atf_tc_t *tc, const char *mp)
     81        1.5     njoly {
     82        1.5     njoly 	char name[] = "dir.test/file.test";
     83        1.5     njoly 	char *dir = dirname(name);
     84        1.5     njoly 	int fd;
     85        1.5     njoly 
     86        1.5     njoly 	if (FSTYPE_SYSVBFS(tc))
     87        1.5     njoly 		atf_tc_skip("directories not supported by file system");
     88        1.5     njoly 
     89        1.5     njoly 	FSTEST_ENTER();
     90        1.5     njoly 
     91        1.5     njoly 	if (rump_sys_mkdir(dir, 0777) == -1)
     92        1.5     njoly 		atf_tc_fail_errno("mkdir");
     93        1.5     njoly 
     94        1.5     njoly 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     95        1.5     njoly 	if (rump_sys_setuid(1) == -1)
     96        1.5     njoly 		atf_tc_fail_errno("setuid");
     97  1.16.12.2  perseant fprintf(stderr, "OPEN AS USER #1\n");
     98        1.5     njoly         if (rump_sys_open(name, O_RDWR|O_CREAT, 0666) != -1 || errno != EACCES)
     99        1.5     njoly 		atf_tc_fail_errno("open");
    100        1.5     njoly 	rump_pub_lwproc_releaselwp();
    101        1.5     njoly 
    102  1.16.12.2  perseant fprintf(stderr, "OPEN AS USER #0\n");
    103        1.5     njoly 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
    104        1.5     njoly 		atf_tc_fail_errno("open");
    105  1.16.12.2  perseant fprintf(stderr, "CLOSE AS USER #0\n");
    106        1.5     njoly 	if (rump_sys_close(fd) == -1)
    107        1.5     njoly 		atf_tc_fail_errno("close");
    108        1.5     njoly 
    109        1.5     njoly 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    110        1.5     njoly 	if (rump_sys_setuid(1) == -1)
    111        1.5     njoly 		atf_tc_fail_errno("setuid");
    112  1.16.12.2  perseant fprintf(stderr, "UNLINK AS USER #1\n");
    113  1.16.12.2  perseant         if (rump_sys_unlink(name) != -1)
    114  1.16.12.2  perseant 		atf_tc_fail_errno("unlink[1]: succeeded");
    115  1.16.12.2  perseant 	if (errno != EACCES)
    116  1.16.12.2  perseant 		atf_tc_fail_errno("unlink[1]: not EACCES");
    117        1.5     njoly 	rump_pub_lwproc_releaselwp();
    118        1.5     njoly 
    119  1.16.12.2  perseant fprintf(stderr, "UNLINK AS USER #0\n");
    120        1.5     njoly         if (rump_sys_unlink(name) == -1)
    121  1.16.12.2  perseant 		atf_tc_fail_errno("unlink[0]");
    122        1.5     njoly 
    123        1.5     njoly 	if (rump_sys_rmdir(dir) == -1)
    124        1.5     njoly 		atf_tc_fail_errno("rmdir");
    125        1.5     njoly 
    126        1.5     njoly 	FSTEST_EXIT();
    127        1.5     njoly }
    128        1.1     njoly 
    129        1.6     njoly static void
    130        1.6     njoly times(const atf_tc_t *tc, const char *mp)
    131        1.6     njoly {
    132        1.6     njoly 	const char *name = "file.test";
    133        1.8     njoly 	int fd;
    134       1.11      gson 	unsigned int i, j;
    135        1.7     njoly 	struct timeval tmv[2];
    136       1.11      gson 	static struct timeval tmvs[] = {
    137       1.11      gson 		{ QUAD_MIN, 0 },
    138       1.11      gson 		{ 0, 0 },
    139       1.11      gson 		{ QUAD_MAX, 999999 }
    140       1.11      gson 	};
    141        1.6     njoly 
    142        1.6     njoly 	FSTEST_ENTER();
    143        1.6     njoly 
    144        1.6     njoly 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
    145        1.6     njoly 		atf_tc_fail_errno("open");
    146        1.6     njoly 	if (rump_sys_close(fd) == -1)
    147        1.6     njoly 		atf_tc_fail_errno("close");
    148        1.6     njoly 
    149        1.6     njoly 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    150        1.6     njoly 	if (rump_sys_setuid(1) == -1)
    151        1.6     njoly 		atf_tc_fail_errno("setuid");
    152  1.16.12.2  perseant 	if (rump_sys_utimes(name, NULL) != -1)
    153  1.16.12.2  perseant 		atf_tc_fail_errno("utimes: succeeded");
    154  1.16.12.2  perseant 	if (errno != EACCES)
    155  1.16.12.2  perseant 		atf_tc_fail_errno("utimes: not EACCES");
    156        1.6     njoly 	rump_pub_lwproc_releaselwp();
    157        1.6     njoly 
    158        1.6     njoly 	if (rump_sys_utimes(name, NULL) == -1)
    159        1.6     njoly 		atf_tc_fail_errno("utimes");
    160        1.6     njoly 
    161       1.11      gson 	for (i = 0; i < sizeof(tmvs) / sizeof(tmvs[0]); i++) {
    162       1.11      gson 		for (j = 0; j < sizeof(tmvs) / sizeof(tmvs[0]); j++) {
    163       1.11      gson 			tmv[0] = tmvs[i];
    164       1.11      gson 			tmv[1] = tmvs[j];
    165       1.11      gson 			rump_pub_lwproc_rfork(RUMP_RFCFDG);
    166       1.11      gson 			if (rump_sys_setuid(1) == -1)
    167       1.11      gson 				atf_tc_fail_errno("setuid");
    168  1.16.12.2  perseant 			if (rump_sys_utimes(name, tmv) != -1)
    169  1.16.12.2  perseant 				atf_tc_fail_errno("utimes[1]: succeeded");
    170  1.16.12.2  perseant 			if (errno != EPERM)
    171  1.16.12.2  perseant 				atf_tc_fail_errno("utimes[1]: not EPERM");
    172       1.11      gson 			rump_pub_lwproc_releaselwp();
    173       1.11      gson 
    174       1.11      gson 			if (rump_sys_utimes(name, tmv) == -1)
    175       1.11      gson 				atf_tc_fail_errno("utimes");
    176       1.11      gson 		}
    177       1.11      gson 	}
    178        1.7     njoly 
    179        1.6     njoly 	if (rump_sys_unlink(name) == -1)
    180        1.6     njoly 		atf_tc_fail_errno("unlink");
    181        1.6     njoly 
    182        1.6     njoly 	FSTEST_EXIT();
    183        1.6     njoly }
    184        1.6     njoly 
    185        1.9     njoly static void
    186        1.9     njoly flags(const atf_tc_t *tc, const char *mp)
    187        1.9     njoly {
    188        1.9     njoly 	const char *name = "file.test";
    189        1.9     njoly 	int fd, fflags;
    190        1.9     njoly 	struct stat st;
    191        1.9     njoly 
    192        1.9     njoly 	FSTEST_ENTER();
    193        1.9     njoly 
    194        1.9     njoly 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
    195        1.9     njoly 		atf_tc_fail_errno("open");
    196        1.9     njoly 	if (rump_sys_close(fd) == -1)
    197        1.9     njoly 		atf_tc_fail_errno("close");
    198        1.9     njoly 
    199        1.9     njoly 	if (rump_sys_stat(name, &st) == -1)
    200        1.9     njoly 		atf_tc_fail_errno("stat");
    201        1.9     njoly 	if (rump_sys_chflags(name, st.st_flags) == -1) {
    202        1.9     njoly 		if (errno == EOPNOTSUPP)
    203        1.9     njoly 			atf_tc_skip("file flags not supported by file system");
    204  1.16.12.2  perseant 		atf_tc_fail_errno("chflags[0]");
    205        1.9     njoly 	}
    206        1.9     njoly 
    207       1.16   hannken 	fflags = st.st_flags | UF_NODUMP;
    208        1.9     njoly 
    209  1.16.12.2  perseant 	if (rump_sys_stat(mp, &st) == -1)
    210  1.16.12.2  perseant 		atf_tc_fail_errno("stat[0]");
    211  1.16.12.2  perseant 	fprintf(stderr, "stat(%s) -> mode=%o\n", mp, st.st_mode);
    212        1.9     njoly 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    213        1.9     njoly 	if (rump_sys_setuid(1) == -1)
    214        1.9     njoly 		atf_tc_fail_errno("setuid");
    215  1.16.12.2  perseant 	if (rump_sys_stat(name, &st) == -1)
    216  1.16.12.2  perseant 		atf_tc_fail_errno("stat[1]");
    217       1.16   hannken 	fflags |= UF_NODUMP;
    218  1.16.12.2  perseant 	if (rump_sys_chflags(name, fflags) != -1)
    219  1.16.12.2  perseant 		atf_tc_fail_errno("chflags[1]: succeeded");
    220  1.16.12.2  perseant 	if (errno != EPERM) {
    221  1.16.12.2  perseant 		fprintf(stderr, "errno = %d\n", errno);
    222  1.16.12.2  perseant 		atf_tc_fail_errno("chflags[1]: not EPERM");
    223  1.16.12.2  perseant 	}
    224        1.9     njoly 	rump_pub_lwproc_releaselwp();
    225        1.9     njoly 
    226        1.9     njoly 	if (rump_sys_chflags(name, fflags) == -1)
    227  1.16.12.2  perseant 		atf_tc_fail_errno("chflags[2]");
    228        1.9     njoly 
    229       1.16   hannken 	fflags &= ~UF_NODUMP;
    230        1.9     njoly 	if (rump_sys_chflags(name, fflags) == -1)
    231  1.16.12.2  perseant 		atf_tc_fail_errno("chflags[3]");
    232        1.9     njoly 
    233        1.9     njoly 	if (rump_sys_unlink(name) == -1)
    234        1.9     njoly 		atf_tc_fail_errno("unlink");
    235        1.9     njoly 
    236        1.9     njoly 	FSTEST_EXIT();
    237        1.9     njoly }
    238        1.9     njoly 
    239        1.1     njoly ATF_TC_FSAPPLY(owner, "owner unprivileged checks");
    240        1.5     njoly ATF_TC_FSAPPLY(dirperms, "directory permission checks");
    241        1.6     njoly ATF_TC_FSAPPLY(times, "time set checks");
    242        1.9     njoly ATF_TC_FSAPPLY(flags, "file flags checks");
    243        1.1     njoly 
    244        1.1     njoly ATF_TP_ADD_TCS(tp)
    245        1.1     njoly {
    246        1.1     njoly 
    247        1.1     njoly 	ATF_TP_FSAPPLY(owner);
    248        1.5     njoly 	ATF_TP_FSAPPLY(dirperms);
    249        1.6     njoly 	ATF_TP_FSAPPLY(times);
    250        1.9     njoly 	ATF_TP_FSAPPLY(flags);
    251        1.1     njoly 
    252        1.1     njoly 	return atf_no_error();
    253        1.1     njoly }
    254