t_link.c revision 1.4 1 1.4 christos /* $NetBSD: t_link.c,v 1.4 2022/03/30 14:24:50 christos 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.3 christos if (FSTYPE_MSDOS(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.3 christos #define USES_OWNCHECK \
57 1.3 christos if (FSTYPE_ZFS(tc)) \
58 1.3 christos atf_tc_skip("zfs not supported since it has its " \
59 1.3 christos "own rules for hardlinks")
60 1.1 christos
61 1.1 christos
62 1.1 christos static void
63 1.1 christos hardlink(const atf_tc_t *tc, const char *mp, uid_t u1, uid_t u2,
64 1.1 christos bool sysctl, bool allowed)
65 1.1 christos {
66 1.1 christos const char name[] = "foo";
67 1.1 christos const char link[] = "bar";
68 1.1 christos int one = 1, fd;
69 1.1 christos
70 1.1 christos USES_OWNER;
71 1.3 christos USES_USERLEVEL;
72 1.3 christos USES_OWNCHECK;
73 1.1 christos
74 1.1 christos FSTEST_ENTER();
75 1.1 christos
76 1.1 christos if (sysctl) {
77 1.1 christos if (sysctlbyname(
78 1.1 christos "security.models.extensions.hardlink_check_uid",
79 1.1 christos NULL, 0, &one, sizeof(one)) == -1)
80 1.1 christos atf_tc_fail_errno("sysctlbyname");
81 1.1 christos }
82 1.1 christos
83 1.1 christos rump_pub_lwproc_rfork(RUMP_RFCFDG);
84 1.1 christos if (rump_sys_chmod(".", 0777) == -1)
85 1.1 christos atf_tc_fail_errno("chmod");
86 1.1 christos if (rump_sys_setuid(u1) == -1)
87 1.1 christos atf_tc_fail_errno("setuid");
88 1.1 christos if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
89 1.1 christos atf_tc_fail_errno("open");
90 1.1 christos if (rump_sys_close(fd) == -1)
91 1.1 christos atf_tc_fail_errno("close");
92 1.1 christos rump_pub_lwproc_releaselwp();
93 1.1 christos
94 1.1 christos rump_pub_lwproc_rfork(RUMP_RFCFDG);
95 1.1 christos if (rump_sys_setuid(u2) == -1)
96 1.1 christos atf_tc_fail_errno("setuid");
97 1.1 christos if (rump_sys_link(name, link) == -1) {
98 1.2 christos if (errno != EOPNOTSUPP && allowed)
99 1.1 christos atf_tc_fail_errno("link");
100 1.1 christos } else {
101 1.1 christos if (!allowed)
102 1.1 christos atf_tc_fail("failed to disallow hard link");
103 1.1 christos }
104 1.1 christos rump_pub_lwproc_releaselwp();
105 1.1 christos
106 1.1 christos FSTEST_EXIT();
107 1.1 christos }
108 1.1 christos
109 1.1 christos
110 1.1 christos static void
111 1.1 christos hardlink_sameuser(const atf_tc_t *tc, const char *mp)
112 1.1 christos {
113 1.1 christos hardlink(tc, mp, 1, 1, false, true);
114 1.1 christos }
115 1.1 christos
116 1.1 christos static void
117 1.1 christos hardlink_sameuser_sysctl(const atf_tc_t *tc, const char *mp)
118 1.1 christos {
119 1.1 christos hardlink(tc, mp, 1, 1, true, true);
120 1.1 christos }
121 1.1 christos
122 1.1 christos static void
123 1.1 christos hardlink_otheruser(const atf_tc_t *tc, const char *mp)
124 1.1 christos {
125 1.1 christos hardlink(tc, mp, 1, 2, false, true);
126 1.1 christos }
127 1.1 christos
128 1.1 christos static void
129 1.1 christos hardlink_otheruser_sysctl(const atf_tc_t *tc, const char *mp)
130 1.1 christos {
131 1.1 christos hardlink(tc, mp, 1, 2, true, false);
132 1.1 christos }
133 1.1 christos
134 1.1 christos static void
135 1.1 christos hardlink_rootuser(const atf_tc_t *tc, const char *mp)
136 1.1 christos {
137 1.1 christos hardlink(tc, mp, 1, 0, false, true);
138 1.1 christos }
139 1.1 christos
140 1.1 christos static void
141 1.1 christos hardlink_rootuser_sysctl(const atf_tc_t *tc, const char *mp)
142 1.1 christos {
143 1.1 christos hardlink(tc, mp, 1, 0, true, true);
144 1.1 christos }
145 1.1 christos
146 1.1 christos ATF_TC_FSAPPLY(hardlink_sameuser, "hardlink same user allowed");
147 1.1 christos ATF_TC_FSAPPLY(hardlink_sameuser_sysctl, "hardlink same user sysctl allowed");
148 1.1 christos ATF_TC_FSAPPLY(hardlink_otheruser, "hardlink other user allowed");
149 1.1 christos ATF_TC_FSAPPLY(hardlink_otheruser_sysctl, "hardlink other user sysctl denied");
150 1.1 christos ATF_TC_FSAPPLY(hardlink_rootuser, "hardlink root user allowed");
151 1.1 christos ATF_TC_FSAPPLY(hardlink_rootuser_sysctl, "hardlink root user sysctl allowed");
152 1.1 christos
153 1.1 christos ATF_TP_ADD_TCS(tp)
154 1.1 christos {
155 1.1 christos ATF_TP_FSAPPLY(hardlink_sameuser);
156 1.1 christos ATF_TP_FSAPPLY(hardlink_sameuser_sysctl);
157 1.1 christos ATF_TP_FSAPPLY(hardlink_otheruser);
158 1.1 christos ATF_TP_FSAPPLY(hardlink_otheruser_sysctl);
159 1.1 christos ATF_TP_FSAPPLY(hardlink_rootuser);
160 1.1 christos ATF_TP_FSAPPLY(hardlink_rootuser_sysctl);
161 1.1 christos
162 1.1 christos return atf_no_error();
163 1.1 christos }
164