aboutsummaryrefslogtreecommitdiff
path: root/sys/security/mac_ddb/mac_ddb.c
blob: 84a56e1d276a93ffe185ffca26713183f6764957 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2021-2022 Klara Systems
 *
 * This software was developed by Mitchell Horne <mhorne@FreeBSD.org>
 * under sponsorship from Juniper Networks and Klara Systems.
 *
 * 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 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/param.h>
#include <sys/jail.h>
#include <sys/kdb.h>
#include <sys/module.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/rman.h>
#include <sys/sysctl.h>

#include <net/vnet.h>

#include <ddb/ddb.h>
#include <ddb/db_command.h>

#include <security/mac/mac_policy.h>

/*
 * This module provides a limited interface to the ddb(4) kernel debugger. The
 * intent is to allow execution of useful debugging commands while disallowing
 * the execution of commands which may be used to inspect/modify arbitrary
 * system memory.
 *
 * Commands which are deterministic in their output or effect and that have
 * been flagged with DB_CMD_MEMSAFE in their definition will be allowed.
 *
 * Other commands are valid within this context so long as there is some
 * constraint placed on their input arguments. This applies to most 'show'
 * commands which accept an arbitrary address. If the provided address can be
 * validated as a real instance of the object (e.g. the 'show proc' address
 * points to a real struct proc in the process list), then the command may be
 * executed. This module defines several validation functions which are used to
 * conditionally allow or block the execution of some commands. For these
 * commands we define and apply the DB_CMD_VALIDATE flag.
 *
 * Any other commands not flagged with DM_CMD_MEMSAFE or DB_CMD_VALIDATE are
 * considered unsafe for execution.
 */

#define	DB_CMD_VALIDATE		DB_MAC1

typedef int db_validation_fn_t(db_expr_t addr, bool have_addr, db_expr_t count,
    char *modif);

static db_validation_fn_t	db_thread_valid;
static db_validation_fn_t	db_show_ffs_valid;
static db_validation_fn_t	db_show_prison_valid;
static db_validation_fn_t	db_show_proc_valid;
static db_validation_fn_t	db_show_rman_valid;
#ifdef VIMAGE
static db_validation_fn_t	db_show_vnet_valid;
#endif

struct cmd_list_item {
	const char *name;
	db_validation_fn_t *validate_fn;
};

/* List of top-level ddb(4) commands which are allowed by this policy. */
static const struct cmd_list_item command_list[] = {
	{ "thread",	db_thread_valid },
};

/* List of ddb(4) 'show' commands which are allowed by this policy. */
static const struct cmd_list_item show_command_list[] = {
	{ "ffs",	db_show_ffs_valid },
	{ "prison",	db_show_prison_valid },
	{ "proc",	db_show_proc_valid },
	{ "rman",	db_show_rman_valid },
	{ "thread",	db_thread_valid },
#ifdef VIMAGE
	{ "vnet",	db_show_vnet_valid },
#endif
};

static int
db_thread_valid(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
{
	struct thread *thr;
	lwpid_t tid;

	/* Default will show the current proc. */
	if (!have_addr)
		return (0);

	/* Validate the provided addr OR tid against the thread list. */
	tid = db_hex2dec(addr);
	for (thr = kdb_thr_first(); thr != NULL; thr = kdb_thr_next(thr)) {
		if ((void *)thr == (void *)addr || tid == thr->td_tid)
			return (0);
	}

	return (EACCES);
}

static int
db_show_ffs_valid(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
{
	struct mount *mp;

	/* No addr will show all mounts. */
	if (!have_addr)
		return (0);

	TAILQ_FOREACH(mp, &mountlist, mnt_list)
		if ((void *)mp == (void *)addr)
			return (0);

	return (EACCES);
}

static int
db_show_prison_valid(db_expr_t addr, bool have_addr, db_expr_t count,
    char *modif)
{
	struct prison *pr;
	int pr_id;

	if (!have_addr || addr == 0)
		return (0);

	/* prison can match by pointer address or ID. */
	pr_id = (int)addr;
	TAILQ_FOREACH(pr, &allprison, pr_list)
		if (pr->pr_id == pr_id || (void *)pr == (void *)addr)
			return (0);

	return (EACCES);
}

static int
db_show_proc_valid(db_expr_t addr, bool have_addr, db_expr_t count,
    char *modif)
{
	struct proc *p;
	int i;

	/* Default will show the current proc. */
	if (!have_addr)
		return (0);

	for (i = 0; i <= pidhash; i++) {
		LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
			if ((void *)p == (void *)addr)
				return (0);
		}
	}

	return (EACCES);
}

static int
db_show_rman_valid(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
{
	struct rman *rm;

	TAILQ_FOREACH(rm, &rman_head, rm_link) {
		if ((void *)rm == (void *)addr)
			return (0);
	}

	return (EACCES);
}

#ifdef VIMAGE
static int
db_show_vnet_valid(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
{
	VNET_ITERATOR_DECL(vnet);

	if (!have_addr)
		return (0);

	VNET_FOREACH(vnet) {
		if ((void *)vnet == (void *)addr)
			return (0);
	}

	return (EACCES);
}
#endif

static int
command_match(struct db_command *cmd, struct cmd_list_item item)
{
	db_validation_fn_t *vfn;
	int n;

	n = strcmp(cmd->name, item.name);
	if (n != 0)
		return (n);

	/* Got an exact match. Update the command struct */
	vfn = item.validate_fn;
	if (vfn != NULL) {
		cmd->flag |= DB_CMD_VALIDATE;
		cmd->mac_priv = vfn;
	}
	return (0);
}

static void
mac_ddb_init(struct mac_policy_conf *conf)
{
	struct db_command *cmd, *prev;
	int i, n;

	/* The command lists are sorted lexographically, as are our arrays. */

	/* Register basic commands. */
	for (i = 0, cmd = prev = NULL; i < nitems(command_list); i++) {
		LIST_FOREACH_FROM(cmd, &db_cmd_table, next) {
			n = command_match(cmd, command_list[i]);
			if (n == 0) {
				/* Got an exact match. */
				prev = cmd;
				break;
			} else if (n > 0) {
				/* Desired command is not registered. */
				break;
			}
		}

		/* Next search begins at the previous match. */
		cmd = prev;
	}

	/* Register 'show' commands which require validation. */
	for (i = 0, cmd = prev = NULL; i < nitems(show_command_list); i++) {
		LIST_FOREACH_FROM(cmd, &db_show_table, next) {
			n = command_match(cmd, show_command_list[i]);
			if (n == 0) {
				/* Got an exact match. */
				prev = cmd;
				break;
			} else if (n > 0) {
				/* Desired command is not registered. */
				break;
			}
		}

		/* Next search begins at the previous match. */
		cmd = prev;
	}

#ifdef INVARIANTS
	/* Verify the lists are sorted correctly. */
	const char *a, *b;

	for (i = 0; i < nitems(command_list) - 1; i++) {
		a = command_list[i].name;
		b = command_list[i + 1].name;
		if (strcmp(a, b) > 0)
			panic("%s: command_list[] not alphabetical: %s,%s",
			    __func__, a, b);
	}
	for (i = 0; i < nitems(show_command_list) - 1; i++) {
		a = show_command_list[i].name;
		b = show_command_list[i + 1].name;
		if (strcmp(a, b) > 0)
			panic("%s: show_command_list[] not alphabetical: %s,%s",
			    __func__, a, b);
	}
#endif
}

static int
mac_ddb_command_register(struct db_command_table *table,
    struct db_command *cmd)
{
	int i, n;

	if ((cmd->flag & DB_CMD_MEMSAFE) != 0)
		return (0);

	/* For other commands, search the allow-lists. */
	if (table == &db_show_table) {
		for (i = 0; i < nitems(show_command_list); i++) {
			n = command_match(cmd, show_command_list[i]);
			if (n == 0)
				/* Got an exact match. */
				return (0);
			else if (n > 0)
				/* Command is not in the policy list. */
				break;
		}
	} else if (table == &db_cmd_table) {
		for (i = 0; i < nitems(command_list); i++) {
			n = command_match(cmd, command_list[i]);
			if (n == 0)
				/* Got an exact match. */
				return (0);
			else if (n > 0)
				/* Command is not in the policy list. */
				break;
		}
	}

	/* The command will not be registered. */
	return (EACCES);
}

static int
mac_ddb_command_exec(struct db_command *cmd, db_expr_t addr,
    bool have_addr, db_expr_t count, char *modif)
{
	db_validation_fn_t *vfn = cmd->mac_priv;

	/* Validate the command and args based on policy. */
	if ((cmd->flag & DB_CMD_VALIDATE) != 0) {
		MPASS(vfn != NULL);
		if (vfn(addr, have_addr, count, modif) == 0)
			return (0);
	} else if ((cmd->flag & DB_CMD_MEMSAFE) != 0)
		return (0);

	return (EACCES);
}

static int
mac_ddb_check_backend(struct kdb_dbbe *be)
{

	/* Only allow DDB backend to execute. */
	if (strcmp(be->dbbe_name, "ddb") == 0)
		return (0);

	return (EACCES);
}

/*
 * Register functions with MAC Framework policy entry points.
 */
static struct mac_policy_ops mac_ddb_ops =
{
	.mpo_init = mac_ddb_init,

	.mpo_ddb_command_register = mac_ddb_command_register,
	.mpo_ddb_command_exec = mac_ddb_command_exec,

	.mpo_kdb_check_backend = mac_ddb_check_backend,
};
MAC_POLICY_SET(&mac_ddb_ops, mac_ddb, "MAC/DDB", 0, NULL);