Home | History | Annotate | Line # | Download | only in vfs
t_link.c revision 1.1
      1  1.1  christos /*	$NetBSD: t_link.c,v 1.1 2022/03/28 20:51:04 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #include <sys/param.h>
     30  1.1  christos #include <sys/stat.h>
     31  1.1  christos #include <sys/time.h>
     32  1.1  christos #include <sys/sysctl.h>
     33  1.1  christos 
     34  1.1  christos #include <atf-c.h>
     35  1.1  christos #include <libgen.h>
     36  1.1  christos #include <limits.h>
     37  1.1  christos #include <unistd.h>
     38  1.1  christos #include <stdbool.h>
     39  1.1  christos 
     40  1.1  christos #include <rump/rump_syscalls.h>
     41  1.1  christos #include <rump/rump.h>
     42  1.1  christos 
     43  1.1  christos #include "../common/h_fsmacros.h"
     44  1.1  christos #include "h_macros.h"
     45  1.1  christos 
     46  1.1  christos #define USES_OWNER							 \
     47  1.1  christos 	if (FSTYPE_MSDOS(tc))						 \
     48  1.1  christos 	    atf_tc_skip("owner not supported by file system")
     49  1.1  christos 
     50  1.1  christos 
     51  1.1  christos static void
     52  1.1  christos hardlink(const atf_tc_t *tc, const char *mp, uid_t u1, uid_t u2,
     53  1.1  christos     bool sysctl, bool allowed)
     54  1.1  christos {
     55  1.1  christos 	const char name[] = "foo";
     56  1.1  christos 	const char link[] = "bar";
     57  1.1  christos 	int one = 1, fd;
     58  1.1  christos 
     59  1.1  christos 	USES_OWNER;
     60  1.1  christos 
     61  1.1  christos 	FSTEST_ENTER();
     62  1.1  christos 
     63  1.1  christos 	if (sysctl) {
     64  1.1  christos 		if (sysctlbyname(
     65  1.1  christos 		    "security.models.extensions.hardlink_check_uid",
     66  1.1  christos 		    NULL, 0, &one, sizeof(one)) == -1)
     67  1.1  christos 			atf_tc_fail_errno("sysctlbyname");
     68  1.1  christos 	}
     69  1.1  christos 
     70  1.1  christos 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     71  1.1  christos 	if (rump_sys_chmod(".", 0777) == -1)
     72  1.1  christos 		atf_tc_fail_errno("chmod");
     73  1.1  christos 	if (rump_sys_setuid(u1) == -1)
     74  1.1  christos 		atf_tc_fail_errno("setuid");
     75  1.1  christos         if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
     76  1.1  christos 		atf_tc_fail_errno("open");
     77  1.1  christos 	if (rump_sys_close(fd) == -1)
     78  1.1  christos 		atf_tc_fail_errno("close");
     79  1.1  christos 	rump_pub_lwproc_releaselwp();
     80  1.1  christos 
     81  1.1  christos 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     82  1.1  christos 	if (rump_sys_setuid(u2) == -1)
     83  1.1  christos 		atf_tc_fail_errno("setuid");
     84  1.1  christos         if (rump_sys_link(name, link) == -1) {
     85  1.1  christos 		if (allowed)
     86  1.1  christos 			atf_tc_fail_errno("link");
     87  1.1  christos 	} else {
     88  1.1  christos 		if (!allowed)
     89  1.1  christos 			atf_tc_fail("failed to disallow hard link");
     90  1.1  christos 	}
     91  1.1  christos 	rump_pub_lwproc_releaselwp();
     92  1.1  christos 
     93  1.1  christos 	FSTEST_EXIT();
     94  1.1  christos }
     95  1.1  christos 
     96  1.1  christos 
     97  1.1  christos static void
     98  1.1  christos hardlink_sameuser(const atf_tc_t *tc, const char *mp)
     99  1.1  christos {
    100  1.1  christos 	hardlink(tc, mp, 1, 1, false, true);
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos static void
    104  1.1  christos hardlink_sameuser_sysctl(const atf_tc_t *tc, const char *mp)
    105  1.1  christos {
    106  1.1  christos 	hardlink(tc, mp, 1, 1, true, true);
    107  1.1  christos }
    108  1.1  christos 
    109  1.1  christos static void
    110  1.1  christos hardlink_otheruser(const atf_tc_t *tc, const char *mp)
    111  1.1  christos {
    112  1.1  christos 	hardlink(tc, mp, 1, 2, false, true);
    113  1.1  christos }
    114  1.1  christos 
    115  1.1  christos static void
    116  1.1  christos hardlink_otheruser_sysctl(const atf_tc_t *tc, const char *mp)
    117  1.1  christos {
    118  1.1  christos 	hardlink(tc, mp, 1, 2, true, false);
    119  1.1  christos }
    120  1.1  christos 
    121  1.1  christos static void
    122  1.1  christos hardlink_rootuser(const atf_tc_t *tc, const char *mp)
    123  1.1  christos {
    124  1.1  christos 	hardlink(tc, mp, 1, 0, false, true);
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos static void
    128  1.1  christos hardlink_rootuser_sysctl(const atf_tc_t *tc, const char *mp)
    129  1.1  christos {
    130  1.1  christos 	hardlink(tc, mp, 1, 0, true, true);
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos ATF_TC_FSAPPLY(hardlink_sameuser, "hardlink same user allowed");
    134  1.1  christos ATF_TC_FSAPPLY(hardlink_sameuser_sysctl, "hardlink same user sysctl allowed");
    135  1.1  christos ATF_TC_FSAPPLY(hardlink_otheruser, "hardlink other user allowed");
    136  1.1  christos ATF_TC_FSAPPLY(hardlink_otheruser_sysctl, "hardlink other user sysctl denied");
    137  1.1  christos ATF_TC_FSAPPLY(hardlink_rootuser, "hardlink root user allowed");
    138  1.1  christos ATF_TC_FSAPPLY(hardlink_rootuser_sysctl, "hardlink root user sysctl allowed");
    139  1.1  christos 
    140  1.1  christos ATF_TP_ADD_TCS(tp)
    141  1.1  christos {
    142  1.1  christos 	ATF_TP_FSAPPLY(hardlink_sameuser);
    143  1.1  christos 	ATF_TP_FSAPPLY(hardlink_sameuser_sysctl);
    144  1.1  christos 	ATF_TP_FSAPPLY(hardlink_otheruser);
    145  1.1  christos 	ATF_TP_FSAPPLY(hardlink_otheruser_sysctl);
    146  1.1  christos 	ATF_TP_FSAPPLY(hardlink_rootuser);
    147  1.1  christos 	ATF_TP_FSAPPLY(hardlink_rootuser_sysctl);
    148  1.1  christos 
    149  1.1  christos 	return atf_no_error();
    150  1.1  christos }
    151