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