Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * @file fwupdsecattr_probe.c
3 * @brief firmware security attribute probe
4 * @author "Kate Hsuan" <[email protected]>
5 *
6 * 2022/11/28 [email protected]
7 * This probe can fetch firmware information from fwupd for testing and
8 * it is defined in 5.11.3.
9 *
10 */
11
12 /*
13 * Copyright 2022 Red Hat Inc., Durham, North Carolina.
14 * All Rights Reserved.
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 *
30 * Authors:
31 * "Kate Hsuan" <[email protected]>
32 */
33
34 /*
35 * fwupdsecattr probe:
36 *
37 * fwupdsecattr_object(string stream-id)
38 *
39 * fwupdsecattr_state(string security-attr)
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <assert.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <fcntl.h>
53 #include <regex.h>
54 #include <dbus/dbus.h>
55
56 /* SEAP */
57 #include "_seap.h"
58 #include <probe-api.h>
59 #include <probe/probe.h>
60 #include <probe/option.h>
61 #include "probe/entcmp.h"
62 #include "common/debug_priv.h"
63
64 #include "oval_dbus.h"
65 #include "fwupdsecattr_probe.h"
66
67
68 static struct cachehed hsi_result_cache;
69
70 static void hsicache_callback(char *name, const uint32_t value)
71 {
72 struct secattr_cache *entry;
73
74 if (name == NULL)
75 return;
76
77 entry = (struct secattr_cache *)malloc(sizeof(struct secattr_cache));
78 if (entry == NULL) {
79 dE("Out of memory when creating fwupd cache.");
80 return;
81 }
82 entry->name = oscap_strdup(name);
83 entry->hsi_result = value;
84 dD("HSI cache add name: %s value: %d\n", entry->name, entry->hsi_result);
85 LIST_INSERT_HEAD(&hsi_result_cache, entry, entries);
86 }
87
88 static uint32_t hsicache_get(const char *key)
89 {
90 struct secattr_cache *next;
91
92 LIST_FOREACH(next, &hsi_result_cache, entries) {
93 dD("HSI search key: %s (name: %s value: %d)\n", key, next->name, next->hsi_result);
94 if (!strncmp(next->name, key, strlen(next->name))) {
95 return next->hsi_result;
96 }
97 }
98
99 dW("HSI key not found: %s\n", key);
100 return UINT32_MAX;
101 }
102
103 static int get_all_security_attributes(DBusConnection *conn, void(*callback)(char *name, const uint32_t value), void *cbarg)
104 {
105 int ret = 1;
106 DBusMessage *msg = NULL;
107 DBusPendingCall *pending = NULL;
108 char *property_name = NULL;
109 char *appstream_name = NULL;
110 uint32_t hsi_flags = 0;
111
112 msg = dbus_message_new_method_call(
113 "org.freedesktop.fwupd",
114 "/",
115 "org.freedesktop.fwupd",
116 "GetHostSecurityAttrs"
117 );
118 if (msg == NULL) {
119 dD("Failed to create dbus_message via dbus_message_new_method_call!");
120 goto cleanup;
121 }
122
123 DBusMessageIter args, property_iter;
124 _DBusBasicValue value;
125
126 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
127 dD("Failed to send message via D-Bus!");
128 goto cleanup;
129 }
130 if (pending == NULL) {
131 dD("Invalid D-Bus pending call!");
132 goto cleanup;
133 }
134
135 dbus_connection_flush(conn);
136 dbus_message_unref(msg); msg = NULL;
137
138 dbus_pending_call_block(pending);
139 msg = dbus_pending_call_steal_reply(pending);
140 if (msg == NULL) {
141 dD("Failed to steal D-Bus pending call reply.");
142 goto cleanup;
143 }
144 dbus_pending_call_unref(pending); pending = NULL;
145
146 if (!dbus_message_iter_init(msg, &args)) {
147 dD("Failed to initialize iterator over received D-Bus message.");
148 goto cleanup;
149 }
150
151 if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_ERROR) {
152 dbus_message_iter_get_basic(&args, &value);
153 dW("Received an error from D-Bus (%s): %s", dbus_message_get_error_name(msg), value.str);
154 goto cleanup;
155 }
156
157 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY && dbus_message_iter_get_element_type(&args) != DBUS_TYPE_DICT_ENTRY) {
158 dD("Expected array of dict_entry argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
159 goto cleanup;
160 }
161
162 // The variant format is a{sv}
163 dbus_message_iter_recurse(&args, &property_iter);
164 do {
165 DBusMessageIter array_entry, dict_entry, value_variant;
166
167 /* Process array */
168 if (dbus_message_iter_get_arg_type(&property_iter) != DBUS_TYPE_ARRAY) {
169 dD("Expected property_iter is an array but receiving a %s", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&property_iter)));
170 goto cleanup;
171 }
172 dbus_message_iter_recurse(&property_iter, &array_entry);
173
174 if (dbus_message_iter_get_arg_type(&array_entry) != DBUS_TYPE_DICT_ENTRY) {
175 dD("Expected array_entry is a dict but receiving a %s", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&array_entry)));
176 goto cleanup;
177 }
178
179 do {
180 dbus_message_iter_recurse(&array_entry, &dict_entry);
181
182 if (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_STRING) {
183 dD("Expected dict_entry is a string but receiving a %s", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&dict_entry)));
184 goto cleanup;
185 }
186
187 dbus_message_iter_get_basic(&dict_entry, &value);
188 property_name = oscap_strdup(value.str);
189 dD("Element key: %s", property_name);
190
191 if (dbus_message_iter_next(&dict_entry) == false) {
192 dW("Expected another field in dict_entry.");
193 free(property_name);
194 goto cleanup;
195 }
196
197 if (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_VARIANT) {
198 dW("Expected variant as value in dict_entry. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&dict_entry)));
199 free(property_name);
200 goto cleanup;
201 }
202
203 dbus_message_iter_recurse(&dict_entry, &value_variant);
204
205 const int arg_type = dbus_message_iter_get_arg_type(&value_variant);
206
207 switch (arg_type) {
208 case DBUS_TYPE_UINT32:
209 if(!strncmp(property_name, "HsiResult", strlen("HsiResult"))) {
210 _DBusBasicValue hsiresult_value;
211 dbus_message_iter_get_basic(&value_variant, &hsiresult_value);
212 hsi_flags = hsiresult_value.u32;
213 }
214 break;
215 case DBUS_TYPE_STRING:
216 if(!strncmp(property_name, "AppstreamId", strlen("AppstreamId"))) {
217 free(appstream_name);
218 appstream_name = oval_dbus_value_to_string(&value_variant);
219 dD("Element string: %s", appstream_name);
220 }
221 break;
222 }
223 free(property_name);
224 } while (dbus_message_iter_next(&array_entry));
225 callback(appstream_name, hsi_flags);
226 free(appstream_name);
227 appstream_name = NULL;
228 } while (dbus_message_iter_next(&property_iter));
229
230 dbus_message_unref(msg); msg = NULL;
231 ret = 0;
232
233 cleanup:
234 if (pending != NULL)
235 dbus_pending_call_unref(pending);
236
237 if (msg != NULL)
238 dbus_message_unref(msg);
239
240 free(appstream_name);
241
242 return ret;
243 }
244
245 /**
246 * fwupd_security_attr_result_to_string:
247 * @result: security attribute result, e.g. %FWUPD_SECURITY_ATTR_RESULT_ENABLED
248 *
249 * Returns the printable string for the result enum.
250 *
251 * Returns: string, or %NULL
252 **/
253 static const char *
254 fwupd_security_attr_result_to_string(FwupdSecurityAttrResult result)
255 {
256 if (result == FWUPD_SECURITY_ATTR_RESULT_VALID)
257 return "valid";
258 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_VALID)
259 return "not-valid";
260 if (result == FWUPD_SECURITY_ATTR_RESULT_ENABLED)
261 return "enabled";
262 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED)
263 return "not-enabled";
264 if (result == FWUPD_SECURITY_ATTR_RESULT_LOCKED)
265 return "locked";
266 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_LOCKED)
267 return "not-locked";
268 if (result == FWUPD_SECURITY_ATTR_RESULT_ENCRYPTED)
269 return "encrypted";
270 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_ENCRYPTED)
271 return "not-encrypted";
272 if (result == FWUPD_SECURITY_ATTR_RESULT_TAINTED)
273 return "tainted";
274 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_TAINTED)
275 return "not-tainted";
276 if (result == FWUPD_SECURITY_ATTR_RESULT_FOUND)
277 return "found";
278 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_FOUND)
279 return "not-found";
280 if (result == FWUPD_SECURITY_ATTR_RESULT_SUPPORTED)
281 return "supported";
282 if (result == FWUPD_SECURITY_ATTR_RESULT_NOT_SUPPORTED)
283 return "not-supported";
284 if (result == FWUPD_SECURITY_ATTR_RESULT_UNKNOWN) {
285 dD("Got FWUPD_SECURITY_ATTR_RESULT_UNKNOWN\n");
286 return "unknown";
287 }
288 dW("Unknown/invalid FwupdSecurityAttrResult value: %d\n", result);
289 return "invalid-hsi-result";
290 }
291
292 int fwupdsecattr_probe_main(probe_ctx *ctx, void *arg)
293 {
294 SEXP_t *val, *item, *ent, *probe_in;
295 char *stream_id = NULL;
296 const char *hsi_result_str;
297 uint64_t hsi_result = UINT64_MAX;
298
299 probe_in = probe_ctx_getobject(ctx);
300 if (probe_in == NULL)
301 return PROBE_ENOOBJ;
302
303 ent = probe_obj_getent(probe_in, "stream_id", 1);
304 if (ent == NULL)
305 return PROBE_ENOENT;
306
307 val = probe_ent_getval(ent);
308 if (val == NULL) {
309 SEXP_free(ent);
310 return PROBE_ENOVAL;
311 }
312
313 stream_id = SEXP_string_cstr(val);
314 SEXP_free(val);
315 SEXP_free(ent);
316
317 if (LIST_EMPTY(&hsi_result_cache)) {
318 DBusError dbus_error;
319 DBusConnection *dbus_conn;
320
321 dbus_error_init(&dbus_error);
322 dbus_conn = oval_connect_dbus();
323
324 if (dbus_conn == NULL) {
325 dbus_error_free(&dbus_error);
326 SEXP_t *msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_INFO, "D-Bus connection failed, could not identify fwupd.");
327 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
328 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
329 SEXP_free(msg);
330 goto exit;
331 }
332
333 int res = get_all_security_attributes(dbus_conn, hsicache_callback, NULL);
334 oval_disconnect_dbus(dbus_conn);
335
336 if (res) {
337 dbus_error_free(&dbus_error);
338 SEXP_t *msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_INFO, "The fwupd service is not properly installed or configured.");
339 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
340 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
341 SEXP_free(msg);
342 goto exit;
343 }
344 }
345
346 hsi_result = hsicache_get(stream_id);
347
348 if (hsi_result == UINT32_MAX) {
349 item = probe_item_create(OVAL_LINUX_FWUPDSECATTR, NULL,
350 "security_attr", OVAL_DATATYPE_STRING, "not-found",
351 NULL);
352 probe_item_setstatus(item, SYSCHAR_STATUS_NOT_COLLECTED);
353 probe_item_collect(ctx, item);
354 goto exit;
355 }
356
357 hsi_result_str = fwupd_security_attr_result_to_string(hsi_result);
358 item = probe_item_create(OVAL_LINUX_FWUPDSECATTR, NULL,
359 "security_attr", OVAL_DATATYPE_STRING, hsi_result_str,
360 NULL);
361 probe_item_collect(ctx, item);
362
363 exit:
364 free(stream_id);
365 return 0;
366 }