xref: /netbsd-src/tests/fs/vfs/t_link.c (revision 42c92eb5a71192d91876da96b4ea3b60cbfeda2c)
1*42c92eb5Schristos /*	$NetBSD: t_link.c,v 1.5 2022/03/30 16:35:28 christos Exp $	*/
23c5909fbSchristos 
33c5909fbSchristos /*-
442f8118fSchristos  * Copyright (c) 2022 The NetBSD Foundation, Inc.
53c5909fbSchristos  * All rights reserved.
63c5909fbSchristos  *
742f8118fSchristos  * This code is derived from software contributed to The NetBSD Foundation
842f8118fSchristos  * by Christos Zoulas.
942f8118fSchristos  *
103c5909fbSchristos  * Redistribution and use in source and binary forms, with or without
113c5909fbSchristos  * modification, are permitted provided that the following conditions
123c5909fbSchristos  * are met:
133c5909fbSchristos  * 1. Redistributions of source code must retain the above copyright
143c5909fbSchristos  *    notice, this list of conditions and the following disclaimer.
153c5909fbSchristos  * 2. Redistributions in binary form must reproduce the above copyright
163c5909fbSchristos  *    notice, this list of conditions and the following disclaimer in the
173c5909fbSchristos  *    documentation and/or other materials provided with the distribution.
183c5909fbSchristos  *
193c5909fbSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203c5909fbSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213c5909fbSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223c5909fbSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233c5909fbSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243c5909fbSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253c5909fbSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263c5909fbSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273c5909fbSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283c5909fbSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293c5909fbSchristos  * POSSIBILITY OF SUCH DAMAGE.
303c5909fbSchristos  */
313c5909fbSchristos 
323c5909fbSchristos #include <sys/param.h>
333c5909fbSchristos #include <sys/stat.h>
343c5909fbSchristos #include <sys/time.h>
353c5909fbSchristos #include <sys/sysctl.h>
363c5909fbSchristos 
373c5909fbSchristos #include <atf-c.h>
383c5909fbSchristos #include <libgen.h>
393c5909fbSchristos #include <limits.h>
403c5909fbSchristos #include <unistd.h>
413c5909fbSchristos #include <stdbool.h>
423c5909fbSchristos 
433c5909fbSchristos #include <rump/rump_syscalls.h>
443c5909fbSchristos #include <rump/rump.h>
453c5909fbSchristos 
463c5909fbSchristos #include "../common/h_fsmacros.h"
473c5909fbSchristos #include "h_macros.h"
483c5909fbSchristos 
493c5909fbSchristos #define USES_OWNER							\
503c5909fbSchristos 	if (FSTYPE_MSDOS(tc))						\
513c5909fbSchristos 	    atf_tc_skip("owner not supported by file system")
529d538b39Schristos #define USES_USERLEVEL							\
539d538b39Schristos 	if (FSTYPE_PUFFS(tc) || FSTYPE_P2K_FFS(tc))			\
549d538b39Schristos 	    atf_tc_skip("userlevel pass not supported, "		\
559d538b39Schristos 		"since sysctl might not be set in underlying system")
563c5909fbSchristos 
573c5909fbSchristos 
583c5909fbSchristos static void
hardlink(const atf_tc_t * tc,const char * mp,uid_t u1,uid_t u2,bool sysctl,bool allowed)593c5909fbSchristos hardlink(const atf_tc_t *tc, const char *mp, uid_t u1, uid_t u2,
603c5909fbSchristos     bool sysctl, bool allowed)
613c5909fbSchristos {
623c5909fbSchristos 	const char name[] = "foo";
633c5909fbSchristos 	const char link[] = "bar";
643c5909fbSchristos 	int one = 1, fd;
653c5909fbSchristos 
663c5909fbSchristos 	USES_OWNER;
679d538b39Schristos 	USES_USERLEVEL;
683c5909fbSchristos 
693c5909fbSchristos 	FSTEST_ENTER();
703c5909fbSchristos 
713c5909fbSchristos 	if (sysctl) {
723c5909fbSchristos 		if (sysctlbyname(
733c5909fbSchristos 		    "security.models.extensions.hardlink_check_uid",
743c5909fbSchristos 		    NULL, 0, &one, sizeof(one)) == -1)
753c5909fbSchristos 			atf_tc_fail_errno("sysctlbyname");
763c5909fbSchristos 	}
773c5909fbSchristos 
783c5909fbSchristos 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
793c5909fbSchristos 	if (rump_sys_chmod(".", 0777) == -1)
803c5909fbSchristos 		atf_tc_fail_errno("chmod");
813c5909fbSchristos 	if (rump_sys_setuid(u1) == -1)
823c5909fbSchristos 		atf_tc_fail_errno("setuid");
833c5909fbSchristos         if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
843c5909fbSchristos 		atf_tc_fail_errno("open");
853c5909fbSchristos 	if (rump_sys_close(fd) == -1)
863c5909fbSchristos 		atf_tc_fail_errno("close");
873c5909fbSchristos 	rump_pub_lwproc_releaselwp();
883c5909fbSchristos 
893c5909fbSchristos 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
903c5909fbSchristos 	if (rump_sys_setuid(u2) == -1)
913c5909fbSchristos 		atf_tc_fail_errno("setuid");
923c5909fbSchristos         if (rump_sys_link(name, link) == -1) {
93f1d8378eSchristos 		if (errno != EOPNOTSUPP && allowed)
943c5909fbSchristos 			atf_tc_fail_errno("link");
953c5909fbSchristos 	} else {
963c5909fbSchristos 		if (!allowed)
973c5909fbSchristos 			atf_tc_fail("failed to disallow hard link");
983c5909fbSchristos 	}
993c5909fbSchristos 	rump_pub_lwproc_releaselwp();
1003c5909fbSchristos 
1013c5909fbSchristos 	FSTEST_EXIT();
1023c5909fbSchristos }
1033c5909fbSchristos 
1043c5909fbSchristos 
1053c5909fbSchristos static void
hardlink_sameuser(const atf_tc_t * tc,const char * mp)1063c5909fbSchristos hardlink_sameuser(const atf_tc_t *tc, const char *mp)
1073c5909fbSchristos {
1083c5909fbSchristos 	hardlink(tc, mp, 1, 1, false, true);
1093c5909fbSchristos }
1103c5909fbSchristos 
1113c5909fbSchristos static void
hardlink_sameuser_sysctl(const atf_tc_t * tc,const char * mp)1123c5909fbSchristos hardlink_sameuser_sysctl(const atf_tc_t *tc, const char *mp)
1133c5909fbSchristos {
1143c5909fbSchristos 	hardlink(tc, mp, 1, 1, true, true);
1153c5909fbSchristos }
1163c5909fbSchristos 
1173c5909fbSchristos static void
hardlink_otheruser(const atf_tc_t * tc,const char * mp)1183c5909fbSchristos hardlink_otheruser(const atf_tc_t *tc, const char *mp)
1193c5909fbSchristos {
1203c5909fbSchristos 	hardlink(tc, mp, 1, 2, false, true);
1213c5909fbSchristos }
1223c5909fbSchristos 
1233c5909fbSchristos static void
hardlink_otheruser_sysctl(const atf_tc_t * tc,const char * mp)1243c5909fbSchristos hardlink_otheruser_sysctl(const atf_tc_t *tc, const char *mp)
1253c5909fbSchristos {
1263c5909fbSchristos 	hardlink(tc, mp, 1, 2, true, false);
1273c5909fbSchristos }
1283c5909fbSchristos 
1293c5909fbSchristos static void
hardlink_rootuser(const atf_tc_t * tc,const char * mp)1303c5909fbSchristos hardlink_rootuser(const atf_tc_t *tc, const char *mp)
1313c5909fbSchristos {
1323c5909fbSchristos 	hardlink(tc, mp, 1, 0, false, true);
1333c5909fbSchristos }
1343c5909fbSchristos 
1353c5909fbSchristos static void
hardlink_rootuser_sysctl(const atf_tc_t * tc,const char * mp)1363c5909fbSchristos hardlink_rootuser_sysctl(const atf_tc_t *tc, const char *mp)
1373c5909fbSchristos {
1383c5909fbSchristos 	hardlink(tc, mp, 1, 0, true, true);
1393c5909fbSchristos }
1403c5909fbSchristos 
1413c5909fbSchristos ATF_TC_FSAPPLY(hardlink_sameuser, "hardlink same user allowed");
1423c5909fbSchristos ATF_TC_FSAPPLY(hardlink_sameuser_sysctl, "hardlink same user sysctl allowed");
1433c5909fbSchristos ATF_TC_FSAPPLY(hardlink_otheruser, "hardlink other user allowed");
1443c5909fbSchristos ATF_TC_FSAPPLY(hardlink_otheruser_sysctl, "hardlink other user sysctl denied");
1453c5909fbSchristos ATF_TC_FSAPPLY(hardlink_rootuser, "hardlink root user allowed");
1463c5909fbSchristos ATF_TC_FSAPPLY(hardlink_rootuser_sysctl, "hardlink root user sysctl allowed");
1473c5909fbSchristos 
ATF_TP_ADD_TCS(tp)1483c5909fbSchristos ATF_TP_ADD_TCS(tp)
1493c5909fbSchristos {
1503c5909fbSchristos 	ATF_TP_FSAPPLY(hardlink_sameuser);
1513c5909fbSchristos 	ATF_TP_FSAPPLY(hardlink_sameuser_sysctl);
1523c5909fbSchristos 	ATF_TP_FSAPPLY(hardlink_otheruser);
1533c5909fbSchristos 	ATF_TP_FSAPPLY(hardlink_otheruser_sysctl);
1543c5909fbSchristos 	ATF_TP_FSAPPLY(hardlink_rootuser);
1553c5909fbSchristos 	ATF_TP_FSAPPLY(hardlink_rootuser_sysctl);
1563c5909fbSchristos 
1573c5909fbSchristos 	return atf_no_error();
1583c5909fbSchristos }
159