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