Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * @file rpmverifypackage_probe.c
3 * @brief rpmverifypackage 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 "probe-chroot.h"
47
48 /* Individual RPM headers */
49 #include <rpm/rpmfi.h>
50 #include <rpm/rpmcli.h>
51 #include <popt.h>
52
53 /* SEAP */
54 #include <probe-api.h>
55 #include "debug_priv.h"
56 #include "probe/entcmp.h"
57
58 #include <probe/probe.h>
59 #include <probe/option.h>
60
61 #include "rpmverifypackage_probe.h"
62
63 typedef struct {
64 const char *a_name;
65 uint64_t a_flag;
66 const char *a_option;
67 } rpmverifypackage_bhmap_t;
68
69 const rpmverifypackage_bhmap_t rpmverifypackage_bhmap[] = {
70 { "nodeps", (uint64_t)VERIFY_DEPS , "--nodeps"},
71 { "noscripts", (uint64_t)VERIFY_SCRIPT , "--noscript"},
72 };
73
74 struct rpmverify_res {
75 char *name; /**< package name */
76 char *epoch;
77 char *version;
78 char *release;
79 char *arch;
80 char extended_name[1024];
81 uint64_t vflags; /**< rpm verify flags */
82 uint64_t vresults;
83 };
84
85 #define RPMVERIFY_SKIP_CONFIG 0x1000000000000000
86 #define RPMVERIFY_SKIP_GHOST 0x2000000000000000
87 #define RPMVERIFY_RPMATTRMASK 0x00000000ffffffff
88
89 struct verifypackage_global {
90 struct rpm_probe_global rpm;
91 struct probe_chroot chr;
92 };
93
94 static struct poptOption optionsTable[] = {
95 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
96 "Common options for all rpm modes and executables:", NULL },
97 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmVerifyPoptTable, 0,
98 "Query options (with -q or --query):", NULL },
99 POPT_AUTOALIAS
100 POPT_AUTOHELP
101 POPT_TABLEEND
102 };
103
104 #define RPMVERIFY_LOCK RPM_MUTEX_LOCK(&g_rpm->rpm.mutex)
105
106 #define RPMVERIFY_UNLOCK RPM_MUTEX_UNLOCK(&g_rpm->rpm.mutex)
107
108 #define CHROOT_ENTER() probe_chroot_enter(&g_rpm->chr)
109
110 #define CHROOT_LEAVE() probe_chroot_leave(&g_rpm->chr)
111
112 #define CHROOT_IS_SET() probe_chroot_is_set(&g_rpm->chr)
113
114 #define CHROOT_PATH() probe_chroot_get_path(&g_rpm->chr)
115
116 /* modify passed-in iterator to test also given entity */
117 static int adjust_filter(rpmdbMatchIterator iterator, SEXP_t *ent, rpmTag rpm_tag) {
118 oval_operation_t ent_op;
119 char ent_str[1024] = "";
120 int ret = 0;
121
122 if (ent) {
123 ent_op = probe_ent_getoperation(ent, OVAL_OPERATION_EQUALS);
124 PROBE_ENT_STRVAL(ent, ent_str, sizeof ent_str, /* void */, strcpy(ent_str, ""););
125 if (rpm_tag == RPMTAG_EPOCH && strcmp(ent_str, "(none)") == 0) {
126 return ret;
127 }
128
129 switch (ent_op) {
130 case OVAL_OPERATION_EQUALS:
131 if (rpmdbSetIteratorRE (iterator, rpm_tag, RPMMIRE_STRCMP, ent_str) != 0)
132 ret = -1;
133
134 break;
135 case OVAL_OPERATION_PATTERN_MATCH:
136 if (rpmdbSetIteratorRE (iterator, rpm_tag, RPMMIRE_REGEX,
137 (const char *)ent_str) != 0)
138 ret = -1;
139
140 break;
141 default:
142 ret = 0;
143 }
144 }
145 return ret;
146 }
147
148 static int rpmverify_collect(probe_ctx *ctx,
149 SEXP_t *name_ent, SEXP_t *epoch_ent, SEXP_t *version_ent, SEXP_t *release_ent, SEXP_t *arch_ent,
150 uint64_t flags,
151 int (*callback)(probe_ctx *, struct rpmverify_res *),
152 struct verifypackage_global *g_rpm)
153 {
154 rpmdbMatchIterator match;
155 Header pkgh;
156 int ret = -1;
157 unsigned int i, j, rpmcli_argc = 0;
158 const char * rpmcli_argv[10];
159 poptContext rpmcli_context;
160 QVA_t qva;
161
162 RPMVERIFY_LOCK;
163
164 match = rpmtsInitIterator (g_rpm->rpm.rpmts, RPMDBI_PACKAGES, NULL, 0);
165 if (match == NULL) {
166 ret = 0;
167 goto ret;
168 }
169
170 if ((ret = adjust_filter(match, name_ent, RPMTAG_NAME)) == -1) {
171 dE("can't adjust filter with name");
172 goto ret;
173 }
174 if ((ret = adjust_filter(match, epoch_ent, RPMTAG_EPOCH)) == -1) {
175 dE("can't adjust filter with epoch");
176 goto ret;
177 }
178 if ((ret = adjust_filter(match, version_ent, RPMTAG_VERSION)) == -1) {
179 dE("can't adjust filter with version");
180 goto ret;
181 }
182 if ((ret = adjust_filter(match, release_ent, RPMTAG_RELEASE)) == -1) {
183 dE("can't adjust filter with version");
184 goto ret;
185 }
186 if ((ret = adjust_filter(match, arch_ent, RPMTAG_ARCH)) == -1) {
187 dE("can't adjust filter with version");
188 goto ret;
189 }
190
191 if (RPMTAG_BASENAMES == 0 || RPMTAG_DIRNAMES == 0) {
192 return -1;
193 }
194
195 rpmcli_argv[0] = "probe_rpmverifypackage";
196 rpmcli_argv[1] = "--quiet";
197 rpmcli_argv[2] = "--nofiles";
198
199 while ((pkgh = rpmdbNextIterator (match)) != NULL) {
200 SEXP_t *ent;
201 struct rpmverify_res res;
202 errmsg_t rpmerr;
203
204 #define COMPARE_ENT(XXX) \
205 if (XXX ## _ent != NULL) { \
206 ent = probe_entval_from_cstr( \
207 probe_ent_getdatatype(XXX ## _ent), res.XXX, strlen(res.XXX) \
208 ); \
209 if (ent != NULL && probe_entobj_cmp(XXX ## _ent, ent) != OVAL_RESULT_TRUE) { \
210 SEXP_free(ent); \
211 continue; \
212 } \
213 SEXP_free(ent); \
214 }
215
216 res.name = headerFormat(pkgh, "%{NAME}", &rpmerr);
217 COMPARE_ENT(name);
218
219 res.epoch = headerFormat(pkgh, "%{EPOCH}", &rpmerr);
220 COMPARE_ENT(epoch);
221
222 res.version = headerFormat(pkgh, "%{VERSION}", &rpmerr);
223 COMPARE_ENT(version);
224 res.release = headerFormat(pkgh, "%{RELEASE}", &rpmerr);
225 COMPARE_ENT(release);
226 res.arch = headerFormat(pkgh, "%{ARCH}", &rpmerr);
227 COMPARE_ENT(arch);
228 snprintf(res.extended_name, 1024, "%s-%s:%s-%s.%s", res.name,
229 oscap_streq(res.epoch, "(none)") ? "0" : res.epoch,
230 res.version, res.release, res.arch);
231
232 /*
233 * Verify package
234 */
235 res.vflags = res.vresults = 0;
236
237 /* if a --no<flag> is set then we don't run rpmVerify else
238 * we need to run rpm -V with all other --no<flags>
239 */
240 for (i = 0; i < sizeof rpmverifypackage_bhmap/sizeof(rpmverifypackage_bhmap_t); ++i) {
241 rpmcli_argc = 3;
242 if (flags & rpmverifypackage_bhmap[i].a_flag)
243 continue;
244
245 for (j = 0; j < sizeof rpmverifypackage_bhmap/sizeof(rpmverifypackage_bhmap_t); ++j) {
246 if (j == i)
247 continue;
248
249 rpmcli_argv[rpmcli_argc++] = rpmverifypackage_bhmap[j].a_option;
250 }
251 rpmcli_argv[rpmcli_argc++] = res.name;
252 rpmcli_argv[rpmcli_argc] = NULL;
253
254 if (CHROOT_IS_SET())
255 {
256 rpmLibsPreload();
257 if (CHROOT_ENTER() < 0) {
258 ret = 1;
259 goto ret;
260 }
261 }
262
263 rpmcli_context = rpmcliInit(rpmcli_argc, (char * const*)rpmcli_argv, optionsTable);
264 qva = &rpmQVKArgs;
265 rpmVerifyFlags verifyFlags = VERIFY_ALL;
266 verifyFlags &= ~qva->qva_flags;
267 qva->qva_flags = (rpmQueryFlags) verifyFlags;
268
269 // rpmcliFini() causes free of rpmrc, macros, ...
270 // so we have to reload everything again
271 rpmReadConfigFiles ((const char *)NULL, (const char *)NULL);
272
273 /*
274 * Fedora >=36 changed the default dbpath in librpm from /var/lib/rpm to /usr/lib/sysimage/rpm
275 * See: https://fedoraproject.org/wiki/Changes/RelocateRPMToUsr
276 * Therefore, when running openscap on a Fedora >=36 system scanning another systems (such as RHEL, SLES, Fedora<36)
277 * openscap's librpm will try to read the rpm db from /usr/lib/sysimage/rpm which doesn't exist and therefore won't work.
278 * In implementing this change, /var/lib/rpm is still a symlink to /usr/lib/sysimage/rpm
279 * so /var/lib/rpm still works. So /var/lib/rpm is a dbpath that will work on all systems.
280 * Therefore, set the dbpath to be /var/lib/rpm, allow openscap running on any system to scan any system.
281 */
282 rpmPushMacro(NULL, "_dbpath", NULL, "/var/lib/rpm", RMIL_CMDLINE);
283
284 rpmts ts = rpmtsCreate();
285 char* const * args = (char* const *)poptGetArgs(rpmcli_context);
286
287 if (CHROOT_IS_SET()){
288
289 // plugins for offline mode can cause, that .so from
290 // container are loaded - we don't want it
291 DISABLE_PLUGINS(ts);
292 CHROOT_LEAVE();
293 } else {
294 ret = rpmcliVerify(ts, qva, args);
295 }
296
297 ts = rpmtsFree(ts);
298 rpmcli_context = rpmcliFini(rpmcli_context);
299
300 res.vflags |= rpmverifypackage_bhmap[i].a_flag;
301 if (ret == 0)
302 res.vresults |= rpmverifypackage_bhmap[i].a_flag;
303
304 }
305 if (callback(ctx, &res)) {
306 ret = 1;
307 goto ret;
308 }
309 free(res.name);
310 free(res.epoch);
311 free(res.version);
312 free(res.release);
313 free(res.arch);
314 }
315
316 match = rpmdbFreeIterator (match);
317 ret = 0;
318 ret:
319 RPMVERIFY_UNLOCK;
320 return (ret);
321 }
322
323 int rpmverifypackage_probe_offline_mode_supported()
324 {
325 return PROBE_OFFLINE_OWN;
326 }
327
328 void *rpmverifypackage_probe_init(void)
329 {
330 const char* root = getenv("OSCAP_PROBE_ROOT");
331 if ((root!= NULL) && (strlen(root) == 0)) {
332 root = NULL;
333 }
334
335 struct verifypackage_global *g_rpm = malloc(sizeof(struct verifypackage_global));
336 probe_chroot_init(&g_rpm->chr, root);
337
338 #ifdef RPM46_FOUND
339 rpmlogSetCallback(rpmErrorCb, NULL);
340 #endif
341
342 if (CHROOT_IS_SET()) {
343 rpmLibsPreload();
344 if (CHROOT_ENTER() < 0) {
345 probe_chroot_free(&g_rpm->chr);
346 free(g_rpm);
347 return (NULL);
348 }
349 }
350
351 if (rpmReadConfigFiles (NULL, (const char *)NULL) != 0) {
352 dD("rpmReadConfigFiles failed: %u, %s.", errno, strerror (errno));
353 g_rpm->rpm.rpmts = NULL;
354 return ((void *)g_rpm);
355 }
356
357
358 g_rpm->rpm.rpmts = rpmtsCreate();
359
360 if (CHROOT_IS_SET()) {
361 CHROOT_LEAVE();
362
363 // plugins for offline mode can cause, that .so from
364 // container are loaded - we don't want it
365 DISABLE_PLUGINS(g_rpm->rpm.rpmts);
366
367 rpmtsSetRootDir(g_rpm->rpm.rpmts, CHROOT_PATH());
368 }
369
370 set_rpm_db_path();
371 pthread_mutex_init(&(g_rpm->rpm.mutex), NULL);
372 return ((void *)g_rpm);
373 }
374
375 void rpmverifypackage_probe_fini(void *ptr)
376 {
377 struct verifypackage_global *r = (struct verifypackage_global *)ptr;
378
379 rpmFreeCrypto();
380 rpmFreeRpmrc();
381 rpmFreeMacros(NULL);
382 rpmlogClose();
383
384 // If r is null, probe_init() failed during chroot
385 if (r == NULL)
386 return;
387
388 // This will be always set by probe_init(), lets free it
389 probe_chroot_free(&r->chr);
390
391 // If r->rpm.rpmts was not initialized the mutex was not as well
392 if (r->rpm.rpmts == NULL)
393 return;
394
395 rpmtsFree(r->rpm.rpmts);
396 pthread_mutex_destroy (&(r->rpm.mutex));
397
398 free(r);
399 return;
400 }
401
402 static int rpmverifypackage_additem(probe_ctx *ctx, struct rpmverify_res *res)
403 {
404 SEXP_t *item, *value;
405
406 item = probe_item_create(OVAL_LINUX_RPMVERIFYPACKAGE, NULL,
407 "name", OVAL_DATATYPE_STRING, res->name,
408 "epoch", OVAL_DATATYPE_STRING, res->epoch,
409 "version", OVAL_DATATYPE_STRING, res->version,
410 "release", OVAL_DATATYPE_STRING, res->release,
411 "arch", OVAL_DATATYPE_STRING, res->arch,
412 "extended_name", OVAL_DATATYPE_STRING, res->extended_name,
413 NULL);
414
415 if (res->vflags & VERIFY_DEPS) {
416 dD("VERIFY_DEPS %lu", res->vresults & VERIFY_DEPS);
417 value = probe_entval_from_cstr(OVAL_DATATYPE_BOOLEAN, (res->vresults & VERIFY_DEPS ? "1" : "0"), 1);
418 probe_item_ent_add(item, "dependency_check_passed", NULL, value);
419 SEXP_free(value);
420 }
421 if (res->vflags & VERIFY_SCRIPT) {
422 dD("VERIFY_SCRIPT %lu", res->vresults & VERIFY_SCRIPT);
423 value = probe_entval_from_cstr(OVAL_DATATYPE_BOOLEAN, (res->vresults & VERIFY_SCRIPT ? "1" : "0"), 1);
424 probe_item_ent_add(item, "verification_script_successful", NULL, value);
425 SEXP_free(value);
426 }
427
428 return probe_item_collect(ctx, item) == 2 ? 1 : 0;
429 }
430 int rpmverifypackage_probe_main(probe_ctx *ctx, void *arg)
431 {
432 SEXP_t *probe_in, *bh_ent;
433 SEXP_t *name_ent, *epoch_ent, *version_ent, *release_ent, *arch_ent;
434 uint64_t collect_flags = 0;
435 unsigned int i;
436
437 // arg is NULL if we were not able to chroot during probe_init()
438 if (arg == NULL) {
439 return PROBE_EINIT;
440 }
441 struct verifypackage_global *g_rpm = (struct verifypackage_global *)arg;
442
443 // There was no rpm config files
444 if (g_rpm->rpm.rpmts == NULL) {
445 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_NOT_APPLICABLE);
446 return 0;
447 }
448
449 /*
450 * Get refs to object entities
451 */
452 probe_in = probe_ctx_getobject(ctx);
453
454 name_ent = probe_obj_getent(probe_in, "name", 1);
455 epoch_ent = probe_obj_getent(probe_in, "epoch", 1);
456 version_ent = probe_obj_getent(probe_in, "version", 1);
457 release_ent = probe_obj_getent(probe_in, "release", 1);
458 arch_ent = probe_obj_getent(probe_in, "arch", 1);
459
460 /*
461 * Parse behaviors
462 */
463 bh_ent = probe_obj_getent(probe_in, "behaviors", 1);
464
465 if (bh_ent != NULL) {
466 SEXP_t *aval;
467
468 for (i = 0; i < sizeof rpmverifypackage_bhmap/sizeof(rpmverifypackage_bhmap_t); ++i) {
469 aval = probe_ent_getattrval(bh_ent, rpmverifypackage_bhmap[i].a_name);
470
471 if (aval != NULL) {
472 if (SEXP_strcmp(aval, "true") == 0) {
473 dD("omit verify attr: %s", rpmverifypackage_bhmap[i].a_name);
474 collect_flags |= rpmverifypackage_bhmap[i].a_flag;
475 }
476
477 SEXP_free(aval);
478 }
479 }
480
481 SEXP_free(bh_ent);
482 }
483
484 if (rpmverify_collect(ctx, name_ent, epoch_ent, version_ent, release_ent,
485 arch_ent, collect_flags, rpmverifypackage_additem, g_rpm) != 0)
486 {
487 dE("An error ocured while collecting rpmverifypackage data");
488 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
489 }
490
491 SEXP_free(name_ent);
492 SEXP_free(epoch_ent);
493 SEXP_free(version_ent);
494 SEXP_free(release_ent);
495 SEXP_free(arch_ent);
496
497 return 0;
498 }