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