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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*-
* Copyright (c) 2012-2013 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Pawel Jakub Dawidek under sponsorship from
* the FreeBSD Foundation.
*
* 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 AUTHORS 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 AUTHORS 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>
__FBSDID("$FreeBSD$");
#include <sys/nv.h>
#include <netinet/in.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <libcapsicum.h>
#include <libcapsicum_dns.h>
#include <libcasper.h>
#include <pjdlog.h>
static bool
dns_allowed_type(const nvlist_t *limits, const char *type)
{
const char *name;
bool notypes;
void *cookie;
if (limits == NULL)
return (true);
notypes = true;
cookie = NULL;
while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
if (strncmp(name, "type", sizeof("type") - 1) != 0)
continue;
notypes = false;
if (strcmp(nvlist_get_string(limits, name), type) == 0)
return (true);
}
/* If there are no types at all, allow any type. */
if (notypes)
return (true);
return (false);
}
static bool
dns_allowed_family(const nvlist_t *limits, int family)
{
const char *name;
bool nofamilies;
void *cookie;
if (limits == NULL)
return (true);
nofamilies = true;
cookie = NULL;
while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
if (strncmp(name, "family", sizeof("family") - 1) != 0)
continue;
nofamilies = false;
if (family == AF_UNSPEC)
continue;
if (nvlist_get_number(limits, name) == (uint64_t)family)
return (true);
}
/* If there are no families at all, allow any family. */
if (nofamilies)
return (true);
return (false);
}
static void
hostent_pack(const struct hostent *hp, nvlist_t *nvl)
{
unsigned int ii;
char nvlname[64];
int n;
nvlist_add_string(nvl, "name", hp->h_name);
nvlist_add_number(nvl, "addrtype", (uint64_t)hp->h_addrtype);
nvlist_add_number(nvl, "length", (uint64_t)hp->h_length);
if (hp->h_aliases == NULL) {
nvlist_add_number(nvl, "naliases", 0);
} else {
for (ii = 0; hp->h_aliases[ii] != NULL; ii++) {
n = snprintf(nvlname, sizeof(nvlname), "alias%u", ii);
assert(n > 0 && n < (int)sizeof(nvlname));
nvlist_add_string(nvl, nvlname, hp->h_aliases[ii]);
}
nvlist_add_number(nvl, "naliases", (uint64_t)ii);
}
if (hp->h_addr_list == NULL) {
nvlist_add_number(nvl, "naddrs", 0);
} else {
for (ii = 0; hp->h_addr_list[ii] != NULL; ii++) {
n = snprintf(nvlname, sizeof(nvlname), "addr%u", ii);
assert(n > 0 && n < (int)sizeof(nvlname));
nvlist_add_binary(nvl, nvlname, hp->h_addr_list[ii],
(size_t)hp->h_length);
}
nvlist_add_number(nvl, "naddrs", (uint64_t)ii);
}
}
static int
dns_gethostbyname(const nvlist_t *limits, const nvlist_t *nvlin,
nvlist_t *nvlout)
{
struct hostent *hp;
int family;
if (!dns_allowed_type(limits, "NAME"))
return (NO_RECOVERY);
family = (int)nvlist_get_number(nvlin, "family");
if (!dns_allowed_family(limits, family))
return (NO_RECOVERY);
hp = gethostbyname2(nvlist_get_string(nvlin, "name"), family);
if (hp == NULL)
return (h_errno);
hostent_pack(hp, nvlout);
return (0);
}
static int
dns_gethostbyaddr(const nvlist_t *limits, const nvlist_t *nvlin,
nvlist_t *nvlout)
{
struct hostent *hp;
const void *addr;
size_t addrsize;
int family;
if (!dns_allowed_type(limits, "ADDR"))
return (NO_RECOVERY);
family = (int)nvlist_get_number(nvlin, "family");
if (!dns_allowed_family(limits, family))
return (NO_RECOVERY);
addr = nvlist_get_binary(nvlin, "addr", &addrsize);
hp = gethostbyaddr(addr, (socklen_t)addrsize, family);
if (hp == NULL)
return (h_errno);
hostent_pack(hp, nvlout);
return (0);
}
static int
dns_getnameinfo(const nvlist_t *limits, const nvlist_t *nvlin, nvlist_t *nvlout)
{
struct sockaddr_storage sast;
const void *sabin;
char *host, *serv;
size_t sabinsize, hostlen, servlen;
socklen_t salen;
int error, flags;
if (!dns_allowed_type(limits, "NAME"))
return (NO_RECOVERY);
error = 0;
host = serv = NULL;
memset(&sast, 0, sizeof(sast));
hostlen = (size_t)nvlist_get_number(nvlin, "hostlen");
servlen = (size_t)nvlist_get_number(nvlin, "servlen");
if (hostlen > 0) {
host = calloc(1, hostlen + 1);
if (host == NULL) {
error = EAI_MEMORY;
goto out;
}
}
if (servlen > 0) {
serv = calloc(1, servlen + 1);
if (serv == NULL) {
error = EAI_MEMORY;
goto out;
}
}
sabin = nvlist_get_binary(nvlin, "sa", &sabinsize);
if (sabinsize > sizeof(sast)) {
error = EAI_FAIL;
goto out;
}
memcpy(&sast, sabin, sabinsize);
salen = (socklen_t)sabinsize;
if ((sast.ss_family != AF_INET ||
salen != sizeof(struct sockaddr_in)) &&
(sast.ss_family != AF_INET6 ||
salen != sizeof(struct sockaddr_in6))) {
error = EAI_FAIL;
goto out;
}
if (!dns_allowed_family(limits, (int)sast.ss_family)) {
error = NO_RECOVERY;
goto out;
}
flags = (int)nvlist_get_number(nvlin, "flags");
error = getnameinfo((struct sockaddr *)&sast, salen, host, hostlen,
serv, servlen, flags);
if (error != 0)
goto out;
nvlist_move_string(nvlout, "host", host);
nvlist_move_string(nvlout, "serv", serv);
out:
if (error != 0) {
free(host);
free(serv);
}
return (error);
}
static nvlist_t *
addrinfo_pack(const struct addrinfo *ai)
{
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_number(nvl, "ai_flags", (uint64_t)ai->ai_flags);
nvlist_add_number(nvl, "ai_family", (uint64_t)ai->ai_family);
nvlist_add_number(nvl, "ai_socktype", (uint64_t)ai->ai_socktype);
nvlist_add_number(nvl, "ai_protocol", (uint64_t)ai->ai_protocol);
nvlist_add_binary(nvl, "ai_addr", ai->ai_addr, (size_t)ai->ai_addrlen);
nvlist_add_string(nvl, "ai_canonname", ai->ai_canonname);
return (nvl);
}
static int
dns_getaddrinfo(const nvlist_t *limits, const nvlist_t *nvlin, nvlist_t *nvlout)
{
struct addrinfo hints, *hintsp, *res, *cur;
const char *hostname, *servname;
char nvlname[64];
nvlist_t *elem;
unsigned int ii;
int error, family, n;
if (!dns_allowed_type(limits, "ADDR"))
return (NO_RECOVERY);
hostname = nvlist_get_string(nvlin, "hostname");
servname = nvlist_get_string(nvlin, "servname");
if (nvlist_exists_number(nvlin, "hints.ai_flags")) {
size_t addrlen;
hints.ai_flags = (int)nvlist_get_number(nvlin,
"hints.ai_flags");
hints.ai_family = (int)nvlist_get_number(nvlin,
"hints.ai_family");
hints.ai_socktype = (int)nvlist_get_number(nvlin,
"hints.ai_socktype");
hints.ai_protocol = (int)nvlist_get_number(nvlin,
"hints.ai_protocol");
hints.ai_addrlen = 0;
hints.ai_addr = NULL;
hints.ai_canonname = NULL;
hintsp = &hints;
family = hints.ai_family;
} else {
hintsp = NULL;
family = AF_UNSPEC;
}
if (!dns_allowed_family(limits, family))
return (NO_RECOVERY);
error = getaddrinfo(hostname, servname, hintsp, &res);
if (error != 0)
goto out;
for (cur = res, ii = 0; cur != NULL; cur = cur->ai_next, ii++) {
elem = addrinfo_pack(cur);
n = snprintf(nvlname, sizeof(nvlname), "res%u", ii);
assert(n > 0 && n < (int)sizeof(nvlname));
nvlist_move_nvlist(nvlout, nvlname, elem);
}
freeaddrinfo(res);
error = 0;
out:
return (error);
}
static bool
limit_has_entry(const nvlist_t *limits, const char *prefix)
{
const char *name;
size_t prefixlen;
void *cookie;
if (limits == NULL)
return (false);
prefixlen = strlen(prefix);
cookie = NULL;
while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
if (strncmp(name, prefix, prefixlen) == 0)
return (true);
}
return (false);
}
static int
dns_limit(const nvlist_t *oldlimits, const nvlist_t *newlimits)
{
const char *name;
void *cookie;
int nvtype;
bool hastype, hasfamily;
hastype = false;
hasfamily = false;
cookie = NULL;
while ((name = nvlist_next(newlimits, &nvtype, &cookie)) != NULL) {
if (nvtype == NV_TYPE_STRING) {
const char *type;
if (strncmp(name, "type", sizeof("type") - 1) != 0)
return (EINVAL);
type = nvlist_get_string(newlimits, name);
if (strcmp(type, "ADDR") != 0 &&
strcmp(type, "NAME") != 0) {
return (EINVAL);
}
if (!dns_allowed_type(oldlimits, type))
return (ENOTCAPABLE);
hastype = true;
} else if (nvtype == NV_TYPE_NUMBER) {
int family;
if (strncmp(name, "family", sizeof("family") - 1) != 0)
return (EINVAL);
family = (int)nvlist_get_number(newlimits, name);
if (!dns_allowed_family(oldlimits, family))
return (ENOTCAPABLE);
hasfamily = true;
} else {
return (EINVAL);
}
}
/*
* If the new limit doesn't mention type or family we have to
* check if the current limit does have those. Missing type or
* family in the limit means that all types or families are
* allowed.
*/
if (!hastype) {
if (limit_has_entry(oldlimits, "type"))
return (ENOTCAPABLE);
}
if (!hasfamily) {
if (limit_has_entry(oldlimits, "family"))
return (ENOTCAPABLE);
}
return (0);
}
static int
dns_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
nvlist_t *nvlout)
{
int error;
if (strcmp(cmd, "gethostbyname") == 0)
error = dns_gethostbyname(limits, nvlin, nvlout);
else if (strcmp(cmd, "gethostbyaddr") == 0)
error = dns_gethostbyaddr(limits, nvlin, nvlout);
else if (strcmp(cmd, "getnameinfo") == 0)
error = dns_getnameinfo(limits, nvlin, nvlout);
else if (strcmp(cmd, "getaddrinfo") == 0)
error = dns_getaddrinfo(limits, nvlin, nvlout);
else
error = NO_RECOVERY;
return (error);
}
int
main(int argc, char *argv[])
{
return (service_start("system.dns", PARENT_FILENO, dns_limit,
dns_command, argc, argv));
}
|