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