forked from freebsd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit.c
312 lines (271 loc) · 7.65 KB
/
audit.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
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
/*-
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* Copyright (c) 2014 Matthew Seaman <[email protected]>
* Copyright (c) 2014 Vsevolod Stakhov <[email protected]>
* 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
* in this position and unchanged.
* 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(S) ``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(S) 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/queue.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define _WITH_GETLINE
#include <archive.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sysexits.h>
#include <uthash.h>
#include <pkg.h>
#include "pkgcli.h"
void
usage_audit(void)
{
fprintf(stderr, "Usage: pkg audit [-Fqr] [-f file] <pattern>\n\n");
fprintf(stderr, "For more information see 'pkg help audit'.\n");
}
struct pkg_check_entry {
struct pkg *pkg;
UT_hash_handle hh;
UT_hash_handle hs;
};
static void
add_to_check(struct pkg_check_entry **head, struct pkg *pkg)
{
struct pkg_check_entry *e;
const char *uid;
e = malloc(sizeof(*e));
if (e == NULL) {
warnx("malloc failed for pkg_check_entry");
exit(EXIT_FAILURE);
}
e->pkg = pkg;
pkg_get(pkg, PKG_UNIQUEID, &uid);
HASH_ADD_KEYPTR(hh, *head, uid, strlen(uid), e);
}
static void
print_recursive_rdeps(struct pkg_check_entry *head, struct pkg *p,
struct sbuf *sb, struct pkg_check_entry **seen, bool top)
{
struct pkg_dep *dep = NULL;
static char uidbuf[1024];
struct pkg_check_entry *r;
while(pkg_rdeps(p, &dep) == EPKG_OK) {
const char *name = pkg_dep_get(dep, PKG_DEP_NAME);
HASH_FIND(hs, *seen, name, strlen(name), r);
if (r == NULL) {
snprintf(uidbuf, sizeof(uidbuf), "%s~%s",
name, pkg_dep_get(dep, PKG_DEP_ORIGIN));
HASH_FIND(hh, head, uidbuf, strlen(uidbuf), r);
if (r != NULL) {
HASH_ADD_KEYPTR(hs, *seen, name, strlen(name), r);
if (!top)
sbuf_cat(sb, ", ");
sbuf_cat(sb, name);
print_recursive_rdeps(head, r->pkg, sb, seen, false);
top = false;
}
}
}
}
int
exec_audit(int argc, char **argv)
{
struct pkg_audit *audit;
struct pkgdb *db = NULL;
struct pkgdb_it *it = NULL;
struct pkg *pkg = NULL;
const char *db_dir;
char *name;
char *version;
char audit_file_buf[MAXPATHLEN];
char *audit_file = audit_file_buf;
unsigned int vuln = 0;
bool fetch = false, recursive = false;
int ch, i;
int ret = EX_OK;
const char *portaudit_site = NULL;
struct sbuf *sb;
struct pkg_check_entry *check = NULL, *cur, *tmp;
db_dir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
snprintf(audit_file_buf, sizeof(audit_file_buf), "%s/vuln.xml", db_dir);
struct option longopts[] = {
{ "fetch", no_argument, NULL, 'F' },
{ "file", required_argument, NULL, 'f' },
{ "recursive", no_argument, NULL, 'r' },
{ "quiet", no_argument, NULL, 'q' },
{ NULL, 0, NULL, 0 },
};
while ((ch = getopt_long(argc, argv, "+Ff:qr", longopts, NULL)) != -1) {
switch (ch) {
case 'F':
fetch = true;
break;
case 'f':
audit_file = optarg;
break;
case 'q':
quiet = true;
break;
case 'r':
recursive = true;
break;
default:
usage_audit();
return(EX_USAGE);
}
}
argc -= optind;
argv += optind;
audit = pkg_audit_new();
if (fetch == true) {
portaudit_site = pkg_object_string(pkg_config_get("VULNXML_SITE"));
if (pkg_audit_fetch(portaudit_site, audit_file) != EPKG_OK) {
return (EX_IOERR);
}
}
if (pkg_audit_load(audit, audit_file) != EPKG_OK) {
if (errno == ENOENT)
warnx("vulnxml file %s does not exist. "
"Try running 'pkg audit -F' first",
audit_file);
else
warn("unable to open vulnxml file %s",
audit_file);
return (EX_DATAERR);
}
if (argc >= 1) {
for (i = 0; i < argc; i ++) {
name = argv[i];
version = strrchr(name, '-');
if (version != NULL) {
version[0] = '\0';
version++;
}
if (pkg_new(&pkg, PKG_FILE) != EPKG_OK)
err(EX_OSERR, "malloc");
if (version != NULL)
pkg_set(pkg, PKG_NAME, name, PKG_VERSION, version);
else
pkg_set(pkg, PKG_NAME, name);
/* Fake uniqueid */
pkg_set(pkg, PKG_UNIQUEID, name);
add_to_check(&check, pkg);
pkg = NULL;
}
}
else {
/*
* if the database doesn't exist it just means there are no
* packages to audit.
*/
ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_LOCAL);
if (ret == EPKG_ENODB)
return (EX_OK);
else if (ret == EPKG_ENOACCESS) {
warnx("Insufficient privileges to read the package database");
return (EX_NOPERM);
} else if (ret != EPKG_OK) {
warnx("Error accessing the package database");
return (EX_IOERR);
}
if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK)
return (EX_IOERR);
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
pkgdb_close(db);
warnx("Cannot get a read lock on a database, it is locked by another process");
return (EX_TEMPFAIL);
}
if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) {
warnx("Error accessing the package database");
ret = EX_IOERR;
}
else {
while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_RDEPS))
== EPKG_OK) {
add_to_check(&check, pkg);
pkg = NULL;
}
ret = EX_OK;
}
if (db != NULL) {
if (it != NULL)
pkgdb_it_free(it);
pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
pkgdb_close(db);
}
if (ret != EX_OK)
return (ret);
}
/* Now we have vulnxml loaded and check list formed */
#ifdef HAVE_CAPSICUM
if (cap_enter() < 0 && errno != ENOSYS) {
warn("cap_enter() failed");
return (EPKG_FATAL);
}
#endif
if (pkg_audit_process(audit) == EPKG_OK) {
HASH_ITER(hh, check, cur, tmp) {
if (pkg_audit_is_vulnerable(audit, cur->pkg, quiet, &sb)) {
vuln ++;
printf("%s", sbuf_data(sb));
if (recursive) {
const char *name;
struct pkg_check_entry *seen = NULL, *scur, *stmp;
pkg_get(cur->pkg, PKG_NAME, &name);
sbuf_printf(sb, "Packages that depend on %s: ", name);
print_recursive_rdeps(check, cur->pkg, sb, &seen, true);
sbuf_finish(sb);
printf("%s\n", sbuf_data(sb));
HASH_ITER(hs, seen, scur, stmp) {
HASH_DELETE(hs, seen, scur);
}
}
sbuf_delete(sb);
}
}
if (ret == EPKG_END && vuln == 0)
ret = EX_OK;
if (!quiet)
printf("%u problem(s) in the installed packages found.\n", vuln);
HASH_ITER(hh, check, cur, tmp) {
HASH_DELETE(hh, check, cur);
pkg_free(cur->pkg);
free(cur);
}
}
else {
warnx("cannot process vulnxml");
ret = EX_SOFTWARE;
}
pkg_audit_free(audit);
if (vuln != 0)
ret = EXIT_FAILURE;
return (ret);
}