Debian Codegraph @

src/OVAL/probes/unix/linux/rpmverifyfile_probe.c

raw symbols SCIP
1 /**
2 * @file rpmverifyfile_probe.c
3 * @brief rpmverifyfile probe
4 * @author "Daniel Kopecek" <[email protected]>
5 * @author "Petr Lautrbach" <[email protected]>
6 *
7 */
8
9 /*
10 * Copyright 2012 Red Hat Inc., Durham, North Carolina.
11 * All Rights Reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * Authors:
28 * "Daniel Kopecek" <[email protected]>
29 * "Petr Lautrbach" <[email protected]>
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <assert.h>
40 #include <limits.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44
45 #include "rpm-helper.h"
46 #include "oscap_helpers.h"
47
48 /* Individual RPM headers */
49 #include <rpm/rpmfi.h>
50 #include <rpm/rpmcli.h>
51
52 /* SEAP */
53 #include <probe-api.h>
54 #include "debug_priv.h"
55 #include "probe/entcmp.h"
56
57 #include <probe/probe.h>
58 #include <probe/option.h>
59 #include "common/oscap_pcre.h"
60
61 #include "rpmverifyfile_probe.h"
62
63 struct rpmverify_res {
64 char *name; /**< package name */
65 char *epoch;
66 char *version;
67 char *release;
68 char *arch;
69 char *file; /**< filepath */
70 char extended_name[1024];
71 rpmVerifyAttrs vflags; /**< rpm verify flags */
72 rpmVerifyAttrs oflags; /**< rpm verify omit flags */
73 rpmfileAttrs fflags; /**< rpm file flags */
74 };
75
76 #define RPMVERIFY_SKIP_CONFIG 0x1000000000000000
77 #define RPMVERIFY_SKIP_GHOST 0x2000000000000000
78 #define RPMVERIFY_RPMATTRMASK 0x00000000ffffffff
79
80 /* In rmplib older than 4.7 some of the enum values aren't defined.
81 * We need to provide fallback definitions.
82 */
83 #ifndef RPM47_FOUND
84 /* *VERIFY_FILEDIGEST were introduced as aliases to *VERIFY_MD5
85 * They all have the same value (1) - see 'rpm/rpmvf.h'.
86 */
87 #define RPMVERIFY_FILEDIGEST RPMVERIFY_MD5
88 /* RPMVERIFY_CAPS is not supported in older rpmlib.
89 * We can set it to 0 because 0 is neutral to bit OR operation.
90 */
91 #define RPMVERIFY_CAPS 0
92 #endif
93
94 #define RPMVERIFY_LOCK RPM_MUTEX_LOCK(&g_rpm->mutex)
95
96 #define RPMVERIFY_UNLOCK RPM_MUTEX_UNLOCK(&g_rpm->mutex)
97
98 static int rpmverify_additem(probe_ctx *ctx, struct rpmverify_res *res);
99
100 /* modify passed-in iterator to test also given entity */
101 static int adjust_filter(rpmdbMatchIterator iterator, SEXP_t *ent, rpmTag rpm_tag) {
102 oval_operation_t ent_op;
103 char ent_str[1024];
104 int ret = 0;
105
106 if (ent) {
107 ent_op = probe_ent_getoperation(ent, OVAL_OPERATION_EQUALS);
108 PROBE_ENT_STRVAL(ent, ent_str, sizeof ent_str, /* void */, strcpy(ent_str, ""););
109
110 switch (ent_op) {
111 case OVAL_OPERATION_EQUALS:
112 if (rpmdbSetIteratorRE (iterator, rpm_tag, RPMMIRE_STRCMP, ent_str) != 0)
113 ret = -1;
114
115 break;
116 case OVAL_OPERATION_PATTERN_MATCH:
117 if (rpmdbSetIteratorRE (iterator, rpm_tag, RPMMIRE_REGEX,
118 (const char *)ent_str) != 0)
119 ret = -1;
120
121 break;
122 default:
123 ret = 0;
124 }
125 }
126 return ret;
127 }
128
129 /*
130 * Compare file with item iterated over.
131 * Returns 0 when they match, 1 when don't match, -1 on error.
132 * Returns the matched file path in result_file.
133 */
134 static int _compare_file_with_current_file(oval_operation_t file_op, const char *file, const char *current_file, char **result_file)
135 {
136 int ret = 0;
137
138 char *file_realpath = oscap_realpath(file, NULL);
139 char *current_file_realpath = oscap_realpath(current_file, NULL);
140
141 if (file_op == OVAL_OPERATION_EQUALS) {
142 if (strcmp(current_file, file) != 0 &&
143 current_file_realpath && file_realpath &&
144 strcmp(current_file_realpath, file_realpath) != 0) {
145 ret = 1;
146 goto cleanup;
147 }
148 *result_file = oscap_strdup(file);
149 } else if (file_op == OVAL_OPERATION_NOT_EQUAL) {
150 if (strcmp(current_file, file) == 0 ||
151 (current_file_realpath && file_realpath &&
152 strcmp(current_file_realpath, file_realpath) == 0)) {
153 ret = 1;
154 goto cleanup;
155 }
156 *result_file = current_file_realpath ? oscap_strdup(current_file_realpath) : oscap_strdup(current_file);
157 } else if (file_op == OVAL_OPERATION_PATTERN_MATCH) {
158 char *errmsg;
159 int erroff;
160 oscap_pcre_t *re = oscap_pcre_compile(file, OSCAP_PCRE_OPTS_UTF8, &errmsg, &erroff);
161 if (re == NULL) {
162 dE("oscap_pcre_compile pattern='%s': %s", file, errmsg);
163 ret = -1;
164 oscap_pcre_err_free(errmsg);
165 goto cleanup;
166 }
167 int pcre_ret = oscap_pcre_exec(re, current_file, strlen(current_file), 0, 0, NULL, 0);
168 oscap_pcre_free(re);
169 if (pcre_ret > OSCAP_PCRE_ERR_NOMATCH) {
170 /* match */
171 *result_file = oscap_strdup(current_file);
172 } else if (pcre_ret == OSCAP_PCRE_ERR_NOMATCH) {
173 /* no match */
174 ret = 1;
175 goto cleanup;
176 } else {
177 dE("oscap_pcre_exec() failed!");
178 ret = -1;
179 goto cleanup;
180 }
181 } else {
182 /* unsupported operation */
183 dE("Operation \"%d\" on `filepath' not supported", file_op);
184 ret = -1;
185 goto cleanup;
186 }
187
188 cleanup:
189 free(file_realpath);
190 free(current_file_realpath);
191
192 return ret;
193 }
194
195 static int rpmverify_collect_package_files_or_directories(
196 struct rpm_probe_global *g_rpm, probe_ctx *ctx, Header pkgh,
197 const char *file, oval_operation_t file_op, rpmTag tag,
198 struct rpmverify_res *res, uint64_t flags)
199 {
200 int ret = 0;
201 rpmVerifyAttrs omit = (rpmVerifyAttrs)(flags & RPMVERIFY_RPMATTRMASK);
202 rpmfi fi = rpmfiNew(g_rpm->rpmts, pkgh, tag, 1);
203
204 while (rpmfiNext(fi) != -1) {
205 const char *current_file = rpmfiFN(fi);
206 res->fflags = rpmfiFFlags(fi);
207 res->oflags = omit;
208
209 if (((res->fflags & RPMFILE_CONFIG) && (flags & RPMVERIFY_SKIP_CONFIG)) ||
210 ((res->fflags & RPMFILE_GHOST) && (flags & RPMVERIFY_SKIP_GHOST))) {
211 continue;
212 }
213 int cmp_res = _compare_file_with_current_file(file_op, file, current_file, &res->file);
214 if (cmp_res == 1) {
215 /* no match */
216 continue;
217 }
218 if (cmp_res == -1) {
219 ret = -1;
220 goto cleanup;
221 }
222
223 if (rpmVerifyFile(g_rpm->rpmts, fi, &res->vflags, omit) != 0) {
224 res->vflags = RPMVERIFY_FAILURES;
225 }
226
227 if (rpmverify_additem(ctx, res) != 0) {
228 ret = -1;
229 free(res->file);
230 goto cleanup;
231 }
232 free(res->file);
233 }
234
235 cleanup:
236 rpmfiFree(fi);
237 return ret;
238 }
239
240 static int rpmverify_collect(probe_ctx *ctx,
241 const char *file, oval_operation_t file_op,
242 SEXP_t *name_ent, SEXP_t *epoch_ent, SEXP_t *version_ent, SEXP_t *release_ent, SEXP_t *arch_ent,
243 uint64_t flags,
244 struct rpm_probe_global *g_rpm)
245 {
246 rpmdbMatchIterator match;
247 Header pkgh;
248 int ret = -1;
249
250 RPMVERIFY_LOCK;
251
252 if (file != NULL && file_op == OVAL_OPERATION_EQUALS) {
253 /*
254 * When we know the exact file path we look for, we don't need to
255 * filter all RPM packages, but we can ask the rpmdb directly for
256 * the package which provides this file, similar to `rpm -q -f`.
257 */
258 match = rpmtsInitIterator(g_rpm->rpmts, RPMDBI_INSTFILENAMES, file, 0);
259 } else {
260 match = rpmtsInitIterator(g_rpm->rpmts, RPMDBI_PACKAGES, NULL, 0);
261 }
262 if (match == NULL) {
263 ret = 0;
264 goto ret;
265 }
266
267 if ((ret = adjust_filter(match, name_ent, RPMTAG_NAME)) == -1) {
268 dE("can't adjust filter with name");
269 goto ret;
270 }
271 if ((ret = adjust_filter(match, epoch_ent, RPMTAG_EPOCH)) == -1) {
272 dE("can't adjust filter with epoch");
273 goto ret;
274 }
275 if ((ret = adjust_filter(match, version_ent, RPMTAG_VERSION)) == -1) {
276 dE("can't adjust filter with version");
277 goto ret;
278 }
279 if ((ret = adjust_filter(match, release_ent, RPMTAG_RELEASE)) == -1) {
280 dE("can't adjust filter with version");
281 goto ret;
282 }
283 if ((ret = adjust_filter(match, arch_ent, RPMTAG_ARCH)) == -1) {
284 dE("can't adjust filter with version");
285 goto ret;
286 }
287
288 while ((pkgh = rpmdbNextIterator (match)) != NULL) {
289 SEXP_t *ent;
290 struct rpmverify_res res;
291 errmsg_t rpmerr;
292
293 #define COMPARE_ENT(XXX) \
294 if (XXX ## _ent != NULL) { \
295 ent = probe_entval_from_cstr( \
296 probe_ent_getdatatype(XXX ## _ent), res.XXX, strlen(res.XXX) \
297 ); \
298 if (ent != NULL && probe_entobj_cmp(XXX ## _ent, ent) != OVAL_RESULT_TRUE) { \
299 SEXP_free(ent); \
300 continue; \
301 } \
302 SEXP_free(ent); \
303 }
304
305 res.name = headerFormat(pkgh, "%{NAME}", &rpmerr);
306 COMPARE_ENT(name);
307
308 res.epoch = headerFormat(pkgh, "%{EPOCH}", &rpmerr);
309 COMPARE_ENT(epoch);
310
311 res.version = headerFormat(pkgh, "%{VERSION}", &rpmerr);
312 COMPARE_ENT(version);
313 res.release = headerFormat(pkgh, "%{RELEASE}", &rpmerr);
314 COMPARE_ENT(release);
315 res.arch = headerFormat(pkgh, "%{ARCH}", &rpmerr);
316 COMPARE_ENT(arch);
317 snprintf(res.extended_name, 1024, "%s-%s:%s-%s.%s", res.name,
318 oscap_streq(res.epoch, "(none)") ? "0" : res.epoch,
319 res.version, res.release, res.arch);
320
321 if (rpmverify_collect_package_files_or_directories(g_rpm, ctx, pkgh, file, file_op, RPMTAG_BASENAMES, &res, flags) != 0 ||
322 rpmverify_collect_package_files_or_directories(g_rpm, ctx, pkgh, file, file_op, RPMTAG_DIRNAMES, &res, flags) != 0) {
323 ret = -1;
324 }
325
326 free(res.name);
327 free(res.epoch);
328 free(res.version);
329 free(res.release);
330 free(res.arch);
331 if (ret == -1) {
332 goto ret;
333 }
334 }
335
336 ret = 0;
337 ret:
338 match = rpmdbFreeIterator(match);
339 RPMVERIFY_UNLOCK;
340 return (ret);
341 }
342
343 int rpmverifyfile_probe_offline_mode_supported()
344 {
345 // TODO: Switch this to OFFLINE_MODE_OWN once rpmtsSetRootDir is fully supported by librpm
346 return PROBE_OFFLINE_CHROOT;
347 }
348
349 void *rpmverifyfile_probe_init(void)
350 {
351 #ifdef RPM46_FOUND
352 rpmlogSetCallback(rpmErrorCb, NULL);
353 #endif
354 if (rpmReadConfigFiles ((const char *)NULL, (const char *)NULL) != 0) {
355 dD("rpmReadConfigFiles failed: %u, %s.", errno, strerror (errno));
356 return (NULL);
357 }
358
359 struct rpm_probe_global *g_rpm = malloc(sizeof(struct rpm_probe_global));
360
361 set_rpm_db_path();
362
363 g_rpm->rpmts = rpmtsCreate();
364
365 pthread_mutex_init(&(g_rpm->mutex), NULL);
366
367 return ((void *)g_rpm);
368 }
369
370 void rpmverifyfile_probe_fini(void *ptr)
371 {
372 struct rpm_probe_global *r = (struct rpm_probe_global *)ptr;
373
374 rpmFreeCrypto();
375 rpmFreeRpmrc();
376 rpmFreeMacros(NULL);
377 rpmlogClose();
378
379 // If probe_init() failed r->rpmts and r->mutex were not initialized
380 if (r == NULL)
381 return;
382
383 rpmtsFree(r->rpmts);
384 pthread_mutex_destroy (&(r->mutex));
385 free(r);
386
387 return;
388 }
389
390 static void _add_ent_from_cstr(SEXP_t *item, const char *name, const char *value)
391 {
392 SEXP_t *sexp_str = SEXP_string_new(value, strlen(value));
393 probe_item_ent_add(item, name, NULL, sexp_str);
394 SEXP_free(sexp_str);
395 }
396
397 static void _add_ent_from_flag(SEXP_t *item, const char *name, struct rpmverify_res *res, int flag)
398 {
399 const char *result;
400 if (res->oflags & flag || res->vflags & RPMVERIFY_FAILURES) {
401 result = "not performed";
402 } else if (res->vflags & flag) {
403 result = "fail";
404 } else {
405 result = "pass";
406 }
407 _add_ent_from_cstr(item, name, result);
408 }
409
410 static void _add_ent_bool_from_flag(SEXP_t *item, const char *name, struct rpmverify_res *res, int flag)
411 {
412 SEXP_t *sexp_str;
413 sexp_str = SEXP_number_newb(res->fflags & flag);
414 probe_item_ent_add(item, name, NULL, sexp_str);
415 SEXP_free(sexp_str);
416 }
417
418 static int rpmverify_additem(probe_ctx *ctx, struct rpmverify_res *res)
419 {
420 SEXP_t *item;
421 oval_schema_version_t oval_version;
422
423 item = probe_item_create(OVAL_LINUX_RPMVERIFYFILE, NULL,NULL);
424 _add_ent_from_cstr(item, "name", res->name);
425 _add_ent_from_cstr(item, "epoch", res->epoch);
426 _add_ent_from_cstr(item, "version", res->version);
427 _add_ent_from_cstr(item, "release", res->release);
428 _add_ent_from_cstr(item, "arch", res->arch);
429 _add_ent_from_cstr(item, "filepath", res->file);
430 _add_ent_from_cstr(item, "extended_name", res->extended_name);
431 _add_ent_from_flag(item, "size_differs", res, RPMVERIFY_FILESIZE);
432 _add_ent_from_flag(item, "mode_differs", res, RPMVERIFY_MODE);
433 _add_ent_from_flag(item, "md5_differs", res, RPMVERIFY_MD5); // deprecated since OVAL 5.11.1
434 oval_version = probe_obj_get_platform_schema_version(probe_ctx_getobject(ctx));
435 if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.11.1)) >= 0) {
436 _add_ent_from_flag(item, "filedigest_differs", res, RPMVERIFY_FILEDIGEST);
437 }
438 _add_ent_from_flag(item, "device_differs", res, RPMVERIFY_RDEV);
439 _add_ent_from_flag(item, "link_mismatch", res, RPMVERIFY_LINKTO);
440 _add_ent_from_flag(item, "ownership_differs", res, RPMVERIFY_USER);
441 _add_ent_from_flag(item, "group_differs", res, RPMVERIFY_GROUP);
442 _add_ent_from_flag(item, "mtime_differs", res, RPMVERIFY_MTIME);
443 #ifndef HAVE_LIBRPM44
444 _add_ent_from_flag(item, "capabilities_differ", res, RPMVERIFY_CAPS);
445 #endif
446 _add_ent_bool_from_flag(item, "configuration_file", res, RPMFILE_CONFIG);
447 _add_ent_bool_from_flag(item, "documentation_file", res, RPMFILE_DOC);
448 _add_ent_bool_from_flag(item, "ghost_file", res, RPMFILE_GHOST);
449 _add_ent_bool_from_flag(item, "license_file", res, RPMFILE_LICENSE);
450 _add_ent_bool_from_flag(item, "readme_file", res, RPMFILE_README);
451
452 return probe_item_collect(ctx, item) == 2 ? 1 : 0;
453 }
454
455 typedef struct {
456 const char *a_name;
457 uint64_t a_flag;
458 } rpmverifyfile_bhmap_t;
459
460 const rpmverifyfile_bhmap_t rpmverifyfile_bhmap[] = {
461 { "nolinkto", (uint64_t)RPMVERIFY_LINKTO },
462 { "nomd5", (uint64_t)RPMVERIFY_MD5 }, // deprecated since OVAL 5.11.1
463 { "nosize", (uint64_t)RPMVERIFY_FILESIZE },
464 { "nouser", (uint64_t)RPMVERIFY_USER },
465 { "nogroup", (uint64_t)RPMVERIFY_GROUP },
466 { "nomtime", (uint64_t)RPMVERIFY_MTIME },
467 { "nomode", (uint64_t)RPMVERIFY_MODE },
468 { "nordev", (uint64_t)RPMVERIFY_RDEV },
469 { "noconfigfiles", RPMVERIFY_SKIP_CONFIG },
470 { "noghostfiles", RPMVERIFY_SKIP_GHOST },
471 { "nofiledigest", (uint64_t)RPMVERIFY_FILEDIGEST },
472 { "nocaps", (uint64_t)RPMVERIFY_CAPS }
473 };
474
475 int rpmverifyfile_probe_main(probe_ctx *ctx, void *arg)
476 {
477 SEXP_t *probe_in, *file_ent, *bh_ent;
478 SEXP_t *name_ent, *epoch_ent, *version_ent, *release_ent, *arch_ent;
479 char file[PATH_MAX];
480 size_t file_len = sizeof file;
481 oval_operation_t file_op;
482 uint64_t collect_flags = 0;
483 unsigned int i;
484
485 // If probe_init() failed it's because there was no rpm config files
486 if (arg == NULL) {
487 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_NOT_APPLICABLE);
488 return 0;
489 }
490 struct rpm_probe_global *g_rpm = (struct rpm_probe_global *)arg;
491
492 if (ctx->offline_mode & PROBE_OFFLINE_OWN) {
493 const char* root = getenv("OSCAP_PROBE_ROOT");
494 rpmtsSetRootDir(g_rpm->rpmts, root);
495 }
496
497 /*
498 * Get refs to object entities
499 */
500 probe_in = probe_ctx_getobject(ctx);
501 file_ent = probe_obj_getent(probe_in, "filepath", 1);
502
503 if (file_ent == NULL) {
504 dE("Missing \"filepath\" (%p) entity", file_ent);
505
506 SEXP_free(file_ent);
507
508 return (PROBE_ENOENT);
509 }
510
511
512 /*
513 * Extract the requested operation for each entity
514 */
515 file_op = probe_ent_getoperation(file_ent, OVAL_OPERATION_EQUALS);
516
517 if (file_op == OVAL_OPERATION_UNKNOWN)
518 {
519 SEXP_free(file_ent);
520
521 return (PROBE_EINVAL);
522 }
523
524 PROBE_ENT_STRVAL(file_ent, file, file_len, /* void */, strcpy(file, ""););
525 SEXP_free(file_ent);
526
527 name_ent = probe_obj_getent(probe_in, "name", 1);
528 epoch_ent = probe_obj_getent(probe_in, "epoch", 1);
529 version_ent = probe_obj_getent(probe_in, "version", 1);
530 release_ent = probe_obj_getent(probe_in, "release", 1);
531 arch_ent = probe_obj_getent(probe_in, "arch", 1);
532
533 /*
534 * Parse behaviors
535 */
536 bh_ent = probe_obj_getent(probe_in, "behaviors", 1);
537
538 if (bh_ent != NULL) {
539 SEXP_t *aval;
540
541 for (i = 0; i < sizeof rpmverifyfile_bhmap/sizeof(rpmverifyfile_bhmap_t); ++i) {
542 aval = probe_ent_getattrval(bh_ent, rpmverifyfile_bhmap[i].a_name);
543
544 if (aval != NULL) {
545 if (SEXP_strcmp(aval, "true") == 0) {
546 dD("omit verify attr: %s", rpmverifyfile_bhmap[i].a_name);
547 collect_flags |= rpmverifyfile_bhmap[i].a_flag;
548 }
549
550 SEXP_free(aval);
551 }
552 }
553
554 SEXP_free(bh_ent);
555 }
556
557 dD("Collecting rpmverifyfile data, query: f=\"%s\" (%d)",
558 file, file_op);
559
560 if (rpmverify_collect(ctx,
561 file, file_op,
562 name_ent, epoch_ent, version_ent, release_ent, arch_ent,
563 collect_flags,
564 g_rpm) != 0)
565 {
566 dE("An error ocured while collecting rpmverifyfile data");
567 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
568 }
569
570 SEXP_free(name_ent);
571 SEXP_free(epoch_ent);
572 SEXP_free(version_ent);
573 SEXP_free(release_ent);
574 SEXP_free(arch_ent);
575
576 return 0;
577 }