Home | History | Annotate | Line # | Download | only in vfs
t_link.c revision 1.3
      1 /*	$NetBSD: t_link.c,v 1.3 2022/03/30 13:43:42 christos 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/param.h>
     30 #include <sys/stat.h>
     31 #include <sys/time.h>
     32 #include <sys/sysctl.h>
     33 
     34 #include <atf-c.h>
     35 #include <libgen.h>
     36 #include <limits.h>
     37 #include <unistd.h>
     38 #include <stdbool.h>
     39 
     40 #include <rump/rump_syscalls.h>
     41 #include <rump/rump.h>
     42 
     43 #include "../common/h_fsmacros.h"
     44 #include "h_macros.h"
     45 
     46 #define USES_OWNER							\
     47 	if (FSTYPE_MSDOS(tc))						\
     48 	    atf_tc_skip("owner not supported by file system")
     49 #define USES_USERLEVEL							\
     50 	if (FSTYPE_PUFFS(tc) || FSTYPE_P2K_FFS(tc))			\
     51 	    atf_tc_skip("userlevel pass not supported, "		\
     52 		"since sysctl might not be set in underlying system")
     53 #define USES_OWNCHECK							\
     54 	if (FSTYPE_ZFS(tc))						\
     55 	    atf_tc_skip("zfs not supported since it has its "		\
     56 		"own rules for hardlinks")
     57 
     58 
     59 static void
     60 hardlink(const atf_tc_t *tc, const char *mp, uid_t u1, uid_t u2,
     61     bool sysctl, bool allowed)
     62 {
     63 	const char name[] = "foo";
     64 	const char link[] = "bar";
     65 	int one = 1, fd;
     66 
     67 	USES_OWNER;
     68 	USES_USERLEVEL;
     69 	USES_OWNCHECK;
     70 
     71 	FSTEST_ENTER();
     72 
     73 	if (sysctl) {
     74 		if (sysctlbyname(
     75 		    "security.models.extensions.hardlink_check_uid",
     76 		    NULL, 0, &one, sizeof(one)) == -1)
     77 			atf_tc_fail_errno("sysctlbyname");
     78 	}
     79 
     80 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     81 	if (rump_sys_chmod(".", 0777) == -1)
     82 		atf_tc_fail_errno("chmod");
     83 	if (rump_sys_setuid(u1) == -1)
     84 		atf_tc_fail_errno("setuid");
     85         if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
     86 		atf_tc_fail_errno("open");
     87 	if (rump_sys_close(fd) == -1)
     88 		atf_tc_fail_errno("close");
     89 	rump_pub_lwproc_releaselwp();
     90 
     91 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
     92 	if (rump_sys_setuid(u2) == -1)
     93 		atf_tc_fail_errno("setuid");
     94         if (rump_sys_link(name, link) == -1) {
     95 		if (errno != EOPNOTSUPP && allowed)
     96 			atf_tc_fail_errno("link");
     97 	} else {
     98 		if (!allowed)
     99 			atf_tc_fail("failed to disallow hard link");
    100 	}
    101 	rump_pub_lwproc_releaselwp();
    102 
    103 	FSTEST_EXIT();
    104 }
    105 
    106 
    107 static void
    108 hardlink_sameuser(const atf_tc_t *tc, const char *mp)
    109 {
    110 	hardlink(tc, mp, 1, 1, false, true);
    111 }
    112 
    113 static void
    114 hardlink_sameuser_sysctl(const atf_tc_t *tc, const char *mp)
    115 {
    116 	hardlink(tc, mp, 1, 1, true, true);
    117 }
    118 
    119 static void
    120 hardlink_otheruser(const atf_tc_t *tc, const char *mp)
    121 {
    122 	hardlink(tc, mp, 1, 2, false, true);
    123 }
    124 
    125 static void
    126 hardlink_otheruser_sysctl(const atf_tc_t *tc, const char *mp)
    127 {
    128 	hardlink(tc, mp, 1, 2, true, false);
    129 }
    130 
    131 static void
    132 hardlink_rootuser(const atf_tc_t *tc, const char *mp)
    133 {
    134 	hardlink(tc, mp, 1, 0, false, true);
    135 }
    136 
    137 static void
    138 hardlink_rootuser_sysctl(const atf_tc_t *tc, const char *mp)
    139 {
    140 	hardlink(tc, mp, 1, 0, true, true);
    141 }
    142 
    143 ATF_TC_FSAPPLY(hardlink_sameuser, "hardlink same user allowed");
    144 ATF_TC_FSAPPLY(hardlink_sameuser_sysctl, "hardlink same user sysctl allowed");
    145 ATF_TC_FSAPPLY(hardlink_otheruser, "hardlink other user allowed");
    146 ATF_TC_FSAPPLY(hardlink_otheruser_sysctl, "hardlink other user sysctl denied");
    147 ATF_TC_FSAPPLY(hardlink_rootuser, "hardlink root user allowed");
    148 ATF_TC_FSAPPLY(hardlink_rootuser_sysctl, "hardlink root user sysctl allowed");
    149 
    150 ATF_TP_ADD_TCS(tp)
    151 {
    152 	ATF_TP_FSAPPLY(hardlink_sameuser);
    153 	ATF_TP_FSAPPLY(hardlink_sameuser_sysctl);
    154 	ATF_TP_FSAPPLY(hardlink_otheruser);
    155 	ATF_TP_FSAPPLY(hardlink_otheruser_sysctl);
    156 	ATF_TP_FSAPPLY(hardlink_rootuser);
    157 	ATF_TP_FSAPPLY(hardlink_rootuser_sysctl);
    158 
    159 	return atf_no_error();
    160 }
    161