summaryrefslogtreecommitdiff
path: root/sys/secmodel/extensions/secmodel_extensions_vfs.c
blob: db08bd786ad0795001069523431c2edfb7edda86 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*	$NetBSD: secmodel_extensions_vfs.c,v 1.1 2023/04/22 13:54:19 riastradh Exp $	*/

/*-
 * Copyright (c) 2011 Elad Efrat <elad@NetBSD.org>
 * All rights reserved.
 *
 * 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.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: secmodel_extensions_vfs.c,v 1.1 2023/04/22 13:54:19 riastradh Exp $");

#include <sys/types.h>
#include <sys/param.h>

#include <sys/kauth.h>
#include <sys/vnode.h>

#include <secmodel/secmodel.h>
#include <secmodel/extensions/extensions.h>
#include <secmodel/extensions/extensions_impl.h>

static int dovfsusermount;
static int hardlink_check_uid;
static int hardlink_check_gid;

static kauth_listener_t l_system, l_vnode;

static int secmodel_extensions_system_cb(kauth_cred_t, kauth_action_t,
    void *, void *, void *, void *, void *);
static int secmodel_extensions_vnode_cb(kauth_cred_t, kauth_action_t,
    void *, void *, void *, void *, void *);

void
secmodel_extensions_vfs_start(void)
{

	l_system = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
	    secmodel_extensions_system_cb, NULL);
	l_vnode = kauth_listen_scope(KAUTH_SCOPE_VNODE,
	    secmodel_extensions_vnode_cb, NULL);
}

void
secmodel_extensions_vfs_stop(void)
{

	kauth_unlisten_scope(l_system);
	kauth_unlisten_scope(l_vnode);
}

void
secmodel_extensions_vfs_sysctl(struct sysctllog **clog,
    const struct sysctlnode *rnode)
{

	sysctl_createv(clog, 0, &rnode, NULL,
		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
		       CTLTYPE_INT, "usermount",
		       SYSCTL_DESCR("Whether unprivileged users may mount "
				    "filesystems"),
		       sysctl_extensions_user_handler, 0, &dovfsusermount, 0,
		       CTL_CREATE, CTL_EOL);

	sysctl_createv(clog, 0, &rnode, NULL,
		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
		       CTLTYPE_INT, "hardlink_check_uid",
		       SYSCTL_DESCR("Whether unprivileged users can hardlink "\
			    "to files they don't own"),
		       sysctl_extensions_user_handler, 0,
		       &hardlink_check_uid, 0,
		       CTL_CREATE, CTL_EOL);

	sysctl_createv(clog, 0, &rnode, NULL,
		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
		       CTLTYPE_INT, "hardlink_check_gid",
		       SYSCTL_DESCR("Whether unprivileged users can hardlink "\
			    "to files that are not in their " \
			    "group membership"),
		       sysctl_extensions_user_handler, 0,
		       &hardlink_check_gid, 0,
		       CTL_CREATE, CTL_EOL);

	/* Compatibility: vfs.generic.usermount */
	sysctl_createv(clog, 0, NULL, NULL,
		       CTLFLAG_PERMANENT,
		       CTLTYPE_NODE, "generic",
		       SYSCTL_DESCR("Non-specific vfs related information"),
		       NULL, 0, NULL, 0,
		       CTL_VFS, VFS_GENERIC, CTL_EOL);

	sysctl_createv(clog, 0, NULL, NULL,
		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
		       CTLTYPE_INT, "usermount",
		       SYSCTL_DESCR("Whether unprivileged users may mount "
				    "filesystems"),
		       sysctl_extensions_user_handler, 0, &dovfsusermount, 0,
		       CTL_VFS, VFS_GENERIC, VFS_USERMOUNT, CTL_EOL);
}

static int
secmodel_extensions_system_cb(kauth_cred_t cred, kauth_action_t action,
    void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
{
	vnode_t *vp;
	struct vattr va;
	struct mount *mp;
	u_long flags;
	int result;
	enum kauth_system_req req;
	int error;

	req = (enum kauth_system_req)(uintptr_t)arg0;
	result = KAUTH_RESULT_DEFER;

	switch (action) {
	case KAUTH_SYSTEM_MOUNT:
		if (dovfsusermount == 0)
			break;
		switch (req) {
		case KAUTH_REQ_SYSTEM_MOUNT_NEW:
			vp = (vnode_t *)arg1;
			mp = vp->v_mount;
			flags = (u_long)arg2;

			/*
			 * Ensure that the user owns the directory onto which
			 * the mount is attempted.
			 */
			vn_lock(vp, LK_SHARED | LK_RETRY);
			error = VOP_GETATTR(vp, &va, cred);
			VOP_UNLOCK(vp);
			if (error)
				break;

			if (va.va_uid != kauth_cred_geteuid(cred))
				break;

			error = usermount_common_policy(mp, flags);
			if (error)
				break;

			result = KAUTH_RESULT_ALLOW;

			break;

		case KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT:
			mp = arg1;

			/* Must own the mount. */
			if (mp->mnt_stat.f_owner == kauth_cred_geteuid(cred))
				result = KAUTH_RESULT_ALLOW;

			break;

		case KAUTH_REQ_SYSTEM_MOUNT_UPDATE:
			mp = arg1;
			flags = (u_long)arg2;

			/* Must own the mount. */
			if (mp->mnt_stat.f_owner == kauth_cred_geteuid(cred) &&
				usermount_common_policy(mp, flags) == 0)
				result = KAUTH_RESULT_ALLOW;

			break;

		default:
			break;
		}
		break;

	default:
		break;
	}

	return (result);
}

static int
secmodel_extensions_vnode_cb(kauth_cred_t cred, kauth_action_t action,
    void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
{
	int error;
	bool isroot;
	struct vattr va;

	if ((action & KAUTH_VNODE_ADD_LINK) == 0)
		return KAUTH_RESULT_DEFER;

	error = VOP_GETATTR((vnode_t *)arg0, &va, cred);
	if (error)
		goto checkroot;

	if (hardlink_check_uid && kauth_cred_geteuid(cred) != va.va_uid)
		goto checkroot;

	if (hardlink_check_gid && kauth_cred_groupmember(cred, va.va_gid) != 0)
		goto checkroot;

	return KAUTH_RESULT_DEFER;
checkroot:
	error = secmodel_eval("org.netbsd.secmodel.suser", "is-root",
	    cred, &isroot);
	if (error || !isroot)
		return KAUTH_RESULT_DENY;

	return KAUTH_RESULT_DEFER;
}