-
Notifications
You must be signed in to change notification settings - Fork 0
/
devctl_jail.c
230 lines (202 loc) · 6.15 KB
/
devctl_jail.c
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
/*-
* SPDX-License-Identifier: BSD-3-Clause-FreeBSD
*
* BSD 3-Clause License
*
* Copyright (c) 2019, Fabian Freyer
* 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. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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/cdefs.h>
#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h> /* uprintf */
#include <sys/errno.h>
#include <sys/param.h> /* defines used in kernel.h */
#include <sys/kernel.h> /* types used in module initialization */
#include <sys/jail.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/sx.h>
#include <sys/malloc.h>
#include <sys/sbuf.h>
#include <sys/mount.h> /* vfs_getopt */
#include <sys/queue.h>
#include "devctl.h"
#define DEVCTL_JAIL_LOGSIZE 128
static unsigned devctl_jail_osd_jail_slot;
static MALLOC_DEFINE(M_DEVCTL_JAIL, "devctl_jail", "Devctl jail notifications");
/*
* Jail OSD methods
*/
static int
devctl_jail_prison_create(void *obj, void *data __unused)
{
struct prison* pr = obj;
struct sbuf sb;
char *buf;
buf = malloc(DEVCTL_JAIL_LOGSIZE, M_DEVCTL_JAIL, M_NOWAIT);
if (buf == NULL) {
printf("devctl_jail_prison_create: out of memory\n");
return (0);
}
sbuf_new(&sb, buf, DEVCTL_JAIL_LOGSIZE, SBUF_FIXEDLEN);
sbuf_printf(&sb, "jid=%d", pr->pr_id);
sbuf_finish(&sb);
devctl_notify_f("kernel", "jail", "created", sbuf_data(&sb), M_NOWAIT);
sbuf_delete(&sb);
free(buf, M_DEVCTL_JAIL);
return (0);
}
static int
devctl_jail_prison_check(void *obj __unused, void *data __unused)
{
return (0);
}
static int
devctl_jail_prison_set(void *obj, void *data)
{
struct prison* pr = obj;
struct vfsoptlist *opts = data;
struct vfsopt *opt;
int firstopt = 0;
struct sbuf sb;
char *buf;
buf = malloc(DEVCTL_JAIL_LOGSIZE, M_DEVCTL_JAIL, M_NOWAIT);
if (buf == NULL) {
printf("devctl_jail_prison_set: out of memory\n");
return (0);
}
sbuf_new(&sb, buf, DEVCTL_JAIL_LOGSIZE, SBUF_FIXEDLEN);
sbuf_printf(&sb, "jid=%d", pr->pr_id);
TAILQ_FOREACH(opt, opts, link) {
/* errmsg isn't a jail parameter, so we exclude that. */
if (strcmp(opt->name, "errmsg") == 0)
continue;
if (!firstopt) {
sbuf_printf(&sb, " params=%s", opt->name);
firstopt = 1;
}
sbuf_printf(&sb, ",%s", opt->name);
}
sbuf_finish(&sb);
devctl_notify_f("kernel", "jail", "updated", sbuf_data(&sb), M_NOWAIT);
sbuf_delete(&sb);
free(buf, M_DEVCTL_JAIL);
return (0);
}
static int
devctl_jail_prison_get(void *obj, void *data __unused)
{
return (0);
}
static int
devctl_jail_prison_remove(void *obj, void *data __unused)
{
struct prison* pr = obj;
struct sbuf sb;
char *buf;
buf = malloc(DEVCTL_JAIL_LOGSIZE, M_DEVCTL_JAIL, M_NOWAIT);
if (buf == NULL) {
return (0);
}
sbuf_new(&sb, buf, DEVCTL_JAIL_LOGSIZE, SBUF_FIXEDLEN);
sbuf_printf(&sb, "jid=%d", pr->pr_id);
sbuf_finish(&sb);
devctl_notify_f("kernel", "jail", "removed", sbuf_data(&sb), M_NOWAIT);
sbuf_delete(&sb);
free(buf, M_DEVCTL_JAIL);
return (0);
}
static int
devctl_jail_prison_attach(void *obj, void *data __unused)
{
struct prison* pr = obj;
struct thread* td = data;
struct sbuf sb;
char *buf;
buf = malloc(DEVCTL_JAIL_LOGSIZE, M_DEVCTL_JAIL, M_NOWAIT);
if (buf == NULL) {
printf("devctl_jail_prison_attach: out of memory\n");
return (0);
}
sbuf_new(&sb, buf, DEVCTL_JAIL_LOGSIZE, SBUF_FIXEDLEN);
sbuf_printf(&sb, "jid=%d pid=%d", pr->pr_id, td->td_proc->p_pid);
sbuf_finish(&sb);
devctl_notify_f("kernel", "jail", "attached", sbuf_data(&sb), M_NOWAIT);
sbuf_delete(&sb);
free(buf, M_DEVCTL_JAIL);
return (0);
}
static void
devctl_jail_prison_destructor(void *data)
{
}
static void
devctl_jail_osd_jail_register(void)
{
osd_method_t methods[PR_MAXMETHOD] = {
[PR_METHOD_CREATE] = devctl_jail_prison_create,
[PR_METHOD_GET] = devctl_jail_prison_get,
[PR_METHOD_SET] = devctl_jail_prison_set,
[PR_METHOD_REMOVE] = devctl_jail_prison_remove,
[PR_METHOD_ATTACH] = devctl_jail_prison_attach,
[PR_METHOD_CHECK] = devctl_jail_prison_check
};
devctl_jail_osd_jail_slot =
osd_jail_register(devctl_jail_prison_destructor, methods);
}
static void
devctl_jail_osd_jail_deregister(void)
{
osd_jail_deregister(devctl_jail_osd_jail_slot);
}
static int
devctl_jail_loader(struct module *m, int what, void *arg)
{
int err = 0;
switch (what) {
case MOD_LOAD: /* kldload */
devctl_jail_osd_jail_register();
uprintf("devctl_jail KLD loaded.\n");
break;
case MOD_UNLOAD:
devctl_jail_osd_jail_deregister();
uprintf("devctl_jail KLD unloaded.\n");
break;
default:
err = EOPNOTSUPP;
break;
}
return(err);
}
static moduledata_t devctl_jail_mod = {
"devctl_jail",
devctl_jail_loader,
NULL
};
DECLARE_MODULE(devctl_jail, devctl_jail_mod, SI_SUB_KLD, SI_ORDER_ANY);