Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/libfwupdplugin/fu-security-attrs.c
Line
Count
Source
1
/*
2
 * Copyright 2020 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
0
#define G_LOG_DOMAIN "FuSecurityAttrs"
8
9
#include "config.h"
10
11
#include <fwupd.h>
12
#include <glib/gi18n.h>
13
14
#include "fwupd-security-attr-private.h"
15
16
#include "fu-context-private.h"
17
#include "fu-security-attr.h"
18
#include "fu-security-attrs-private.h"
19
#include "fu-security-attrs.h"
20
21
/**
22
 * FuSecurityAttrs:
23
 *
24
 * A set of Host Security ID attributes that represents the system state.
25
 */
26
27
struct _FuSecurityAttrs {
28
  GObject parent_instance;
29
  GPtrArray *attrs;
30
};
31
32
/* probably sane to *not* make this part of the ABI */
33
0
#define FWUPD_SECURITY_ATTR_ID_DOC_URL "https://fwupd.github.io/libfwupdplugin/hsi.html"
34
35
static void
36
fu_security_attrs_codec_iface_init(FwupdCodecInterface *iface);
37
38
0
G_DEFINE_TYPE_WITH_CODE(FuSecurityAttrs,
39
0
      fu_security_attrs,
40
0
      G_TYPE_OBJECT,
41
0
      G_IMPLEMENT_INTERFACE(FWUPD_TYPE_CODEC, fu_security_attrs_codec_iface_init))
42
0
43
0
static void
44
0
fu_security_attrs_finalize(GObject *obj)
45
0
{
46
0
  FuSecurityAttrs *self = FU_SECURITY_ATTRS(obj);
47
0
  g_ptr_array_unref(self->attrs);
48
0
  G_OBJECT_CLASS(fu_security_attrs_parent_class)->finalize(obj);
49
0
}
50
51
static void
52
fu_security_attrs_class_init(FuSecurityAttrsClass *klass)
53
0
{
54
0
  GObjectClass *object_class = G_OBJECT_CLASS(klass);
55
0
  object_class->finalize = fu_security_attrs_finalize;
56
0
}
57
58
static void
59
fu_security_attrs_init(FuSecurityAttrs *self)
60
0
{
61
0
  self->attrs = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
62
0
}
63
64
/**
65
 * fu_security_attrs_append_internal:
66
 * @self: a #FuSecurityAttrs
67
 * @attr: a #FuSecurityAttr
68
 *
69
 * Adds a #FuSecurityAttr to the array with no sanity checks.
70
 **/
71
void
72
fu_security_attrs_append_internal(FuSecurityAttrs *self, FuSecurityAttr *attr)
73
0
{
74
0
  g_return_if_fail(FU_IS_SECURITY_ATTRS(self));
75
0
  g_return_if_fail(FU_IS_SECURITY_ATTR(attr));
76
0
  g_ptr_array_add(self->attrs, g_object_ref(attr));
77
0
}
78
79
/**
80
 * fu_security_attrs_is_valid:
81
 * @self: a #FuSecurityAttrs
82
 *
83
 * Adds a #FuSecurityAttr to the array with no sanity checks.
84
 *
85
 * Returns: %TRUE if the collection is valid
86
 **/
87
gboolean
88
fu_security_attrs_is_valid(FuSecurityAttrs *self)
89
0
{
90
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(self), FALSE);
91
0
  return self->attrs->len > 0;
92
0
}
93
94
/**
95
 * fu_security_attrs_append:
96
 * @self: a #FuSecurityAttrs
97
 * @attr: a #FuSecurityAttr
98
 *
99
 * Adds a #FuSecurityAttr to the array.
100
 **/
101
void
102
fu_security_attrs_append(FuSecurityAttrs *self, FuSecurityAttr *attr)
103
0
{
104
0
  g_return_if_fail(FU_IS_SECURITY_ATTRS(self));
105
0
  g_return_if_fail(FU_IS_SECURITY_ATTR(attr));
106
107
  /* sanity check */
108
0
  if (fu_security_attr_get_plugin(attr) == NULL) {
109
0
    g_warning("%s has no plugin set", fu_security_attr_get_appstream_id(attr));
110
0
  }
111
112
  /* sanity check, and correctly prefix the URLs with the current mirror */
113
0
  if (fu_security_attr_get_url(attr) == NULL) {
114
0
    g_autofree gchar *url = NULL;
115
0
    url = g_strdup_printf("%s#%s",
116
0
              FWUPD_SECURITY_ATTR_ID_DOC_URL,
117
0
              fu_security_attr_get_appstream_id(attr));
118
0
    fu_security_attr_set_url(attr, url);
119
0
  } else if (g_str_has_prefix(fu_security_attr_get_url(attr), "#")) {
120
0
    g_autofree gchar *url = NULL;
121
0
    url = g_strdup_printf("%s%s",
122
0
              FWUPD_SECURITY_ATTR_ID_DOC_URL,
123
0
              fu_security_attr_get_url(attr));
124
0
    fu_security_attr_set_url(attr, url);
125
0
  }
126
0
  fu_security_attrs_append_internal(self, attr);
127
0
}
128
129
/**
130
 * fu_security_attrs_get_by_appstream_id:
131
 * @self: a #FuSecurityAttrs
132
 * @appstream_id: an ID, e.g. %FWUPD_SECURITY_ATTR_ID_ENCRYPTED_RAM
133
 * @error: (nullable): optional return location for an error
134
 *
135
 * Gets a specific #FuSecurityAttr from the array.
136
 *
137
 * Returns: (transfer full): a #FuSecurityAttr or %NULL
138
 **/
139
FuSecurityAttr *
140
fu_security_attrs_get_by_appstream_id(FuSecurityAttrs *self,
141
              const gchar *appstream_id,
142
              GError **error)
143
0
{
144
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(self), NULL);
145
0
  g_return_val_if_fail(appstream_id != NULL, NULL);
146
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
147
0
  if (self->attrs->len == 0) {
148
0
    g_set_error_literal(error,
149
0
            FWUPD_ERROR,
150
0
            FWUPD_ERROR_NOT_FOUND,
151
0
            "no attributes are loaded");
152
0
    return NULL;
153
0
  }
154
0
  for (guint i = 0; i < self->attrs->len; i++) {
155
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
156
0
    if (g_strcmp0(fu_security_attr_get_appstream_id(attr), appstream_id) == 0)
157
0
      return g_object_ref(attr);
158
0
  }
159
0
  g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no attr with ID %s", appstream_id);
160
0
  return NULL;
161
0
}
162
163
/**
164
 * fu_security_attrs_to_variant:
165
 * @self: a #FuSecurityAttrs
166
 *
167
 * Serializes the #FuSecurityAttr objects.
168
 *
169
 * Returns: a #GVariant or %NULL
170
 **/
171
GVariant *
172
fu_security_attrs_to_variant(FuSecurityAttrs *self)
173
0
{
174
0
  GVariantBuilder builder;
175
176
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(self), NULL);
177
178
0
  g_variant_builder_init(&builder, G_VARIANT_TYPE("aa{sv}"));
179
0
  for (guint i = 0; i < self->attrs->len; i++) {
180
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
181
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_OBSOLETED))
182
0
      continue;
183
0
    g_variant_builder_add_value(
184
0
        &builder,
185
0
        fwupd_codec_to_variant(FWUPD_CODEC(attr), FWUPD_CODEC_FLAG_NONE));
186
0
  }
187
0
  return g_variant_new("(aa{sv})", &builder);
188
0
}
189
190
/**
191
 * fu_security_attrs_get_all:
192
 * @self: a #FuSecurityAttrs
193
 * @fwupd_version: (nullable): fwupd version string, e.g. `2.0.7`
194
 *
195
 * Gets all the non-obsoleted attributes in the object.
196
 *
197
 * Returns: (transfer container) (element-type FuSecurityAttr): attributes
198
 **/
199
GPtrArray *
200
fu_security_attrs_get_all(FuSecurityAttrs *self, const gchar *fwupd_version)
201
0
{
202
0
  g_autoptr(GPtrArray) all = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
203
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(self), NULL);
204
0
  for (guint i = 0; i < self->attrs->len; i++) {
205
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
206
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_OBSOLETED))
207
0
      continue;
208
0
    if (!fu_security_attr_check_fwupd_version(attr, fwupd_version))
209
0
      continue;
210
0
    g_ptr_array_add(all, g_object_ref(attr));
211
0
  }
212
0
  return g_steal_pointer(&all);
213
0
}
214
215
/**
216
 * fu_security_attrs_get_all_mutable: (skip):
217
 **/
218
GPtrArray *
219
fu_security_attrs_get_all_mutable(FuSecurityAttrs *self)
220
0
{
221
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(self), NULL);
222
0
  return g_ptr_array_ref(self->attrs);
223
0
}
224
225
/**
226
 * fu_security_attrs_remove_all:
227
 * @self: a #FuSecurityAttrs
228
 *
229
 * Removes all the attributes in the object.
230
 **/
231
void
232
fu_security_attrs_remove_all(FuSecurityAttrs *self)
233
0
{
234
0
  g_return_if_fail(FU_IS_SECURITY_ATTRS(self));
235
0
  return g_ptr_array_set_size(self->attrs, 0);
236
0
}
237
238
/**
239
 * fu_security_attrs_calculate_hsi:
240
 * @self: a #FuSecurityAttrs
241
 * @fwupd_version: fwupd version, e.g. `2.0.7`
242
 * @flags: HSI attribute flags
243
 *
244
 * Calculates the HSI string from the appended attributes.
245
 *
246
 * Returns: (transfer full): a string or %NULL
247
 **/
248
gchar *
249
fu_security_attrs_calculate_hsi(FuSecurityAttrs *self,
250
        const gchar *fwupd_version,
251
        FuSecurityAttrsFlags flags)
252
0
{
253
0
  guint hsi_number = 0;
254
0
  FwupdSecurityAttrFlags attr_flags = FWUPD_SECURITY_ATTR_FLAG_NONE;
255
0
  g_autoptr(GString) str = g_string_new("HSI:");
256
0
  const FwupdSecurityAttrFlags hpi_suffixes[] = {
257
0
      FWUPD_SECURITY_ATTR_FLAG_RUNTIME_ISSUE,
258
0
      FWUPD_SECURITY_ATTR_FLAG_NONE,
259
0
  };
260
261
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(self), NULL);
262
263
  /* find the highest HSI number where there are no failures and at least
264
   * one success */
265
0
  for (guint j = 1; j <= FWUPD_SECURITY_ATTR_LEVEL_LAST; j++) {
266
0
    gboolean success_cnt = 0;
267
0
    gboolean failure_cnt = 0;
268
0
    for (guint i = 0; i < self->attrs->len; i++) {
269
0
      FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
270
0
      if (fu_security_attr_get_level(attr) != j)
271
0
        continue;
272
0
      if (!fu_security_attr_check_fwupd_version(attr, fwupd_version))
273
0
        continue;
274
0
      if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS))
275
0
        success_cnt++;
276
0
      else if (!fu_security_attr_has_flag(attr,
277
0
                  FWUPD_SECURITY_ATTR_FLAG_OBSOLETED))
278
0
        failure_cnt++;
279
0
    }
280
281
    /* abort */
282
0
    if (failure_cnt > 0)
283
0
      break;
284
285
    /* we matched at least one thing on this level */
286
0
    if (success_cnt > 0)
287
0
      hsi_number = j;
288
0
  }
289
290
  /* get a logical OR of the runtime flags */
291
0
  for (guint i = 0; i < self->attrs->len; i++) {
292
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
293
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_OBSOLETED))
294
0
      continue;
295
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_RUNTIME_ISSUE) &&
296
0
        fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS))
297
0
      continue;
298
0
    if (!fu_security_attr_check_fwupd_version(attr, fwupd_version))
299
0
      continue;
300
301
0
    attr_flags |= fu_security_attr_get_flags(attr);
302
0
  }
303
304
0
  g_string_append_printf(str, "%u", hsi_number);
305
0
  if (attr_flags & FWUPD_SECURITY_ATTR_FLAG_RUNTIME_ISSUE) {
306
0
    for (guint j = 0; hpi_suffixes[j] != FWUPD_SECURITY_ATTR_FLAG_NONE; j++) {
307
0
      if (attr_flags & hpi_suffixes[j])
308
0
        g_string_append(
309
0
            str,
310
0
            fwupd_security_attr_flag_to_suffix(hpi_suffixes[j]));
311
0
    }
312
0
  }
313
314
0
  if (flags & FU_SECURITY_ATTRS_FLAG_ADD_VERSION) {
315
0
    g_string_append_printf(str, " (v%d.%d)", FWUPD_MAJOR_VERSION, FWUPD_MINOR_VERSION);
316
0
  }
317
318
0
  return g_string_free_and_steal(g_steal_pointer(&str));
319
0
}
320
321
static gchar *
322
fu_security_attrs_get_sort_key(FuSecurityAttr *attr)
323
0
{
324
0
  GString *str = g_string_new(NULL);
325
326
  /* level */
327
0
  g_string_append_printf(str, "%u", fu_security_attr_get_level(attr));
328
329
  /* success -> fail -> obsoletes */
330
0
  if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS)) {
331
0
    g_string_append(str, "0");
332
0
  } else if (!fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS) &&
333
0
       !fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_OBSOLETED)) {
334
0
    g_string_append(str, "1");
335
0
  } else {
336
0
    g_string_append(str, "9");
337
0
  }
338
339
  /* prefer name, but fallback to appstream-id for tests */
340
0
  if (fu_security_attr_get_name(attr) != NULL) {
341
0
    g_string_append(str, fu_security_attr_get_name(attr));
342
0
  } else {
343
0
    g_string_append(str, fu_security_attr_get_appstream_id(attr));
344
0
  }
345
0
  return g_string_free_and_steal(str);
346
0
}
347
348
static gint
349
fu_security_attrs_sort_cb(gconstpointer item1, gconstpointer item2)
350
0
{
351
0
  FuSecurityAttr *attr1 = *((FuSecurityAttr **)item1);
352
0
  FuSecurityAttr *attr2 = *((FuSecurityAttr **)item2);
353
0
  g_autofree gchar *sort1 = fu_security_attrs_get_sort_key(attr1);
354
0
  g_autofree gchar *sort2 = fu_security_attrs_get_sort_key(attr2);
355
0
  return g_strcmp0(sort1, sort2);
356
0
}
357
358
static const struct {
359
  const gchar *appstream_id;
360
  FwupdSecurityAttrLevel level;
361
} appstream_id_level_map[] = {
362
    {FWUPD_SECURITY_ATTR_ID_AMD_ROLLBACK_PROTECTION, FWUPD_SECURITY_ATTR_LEVEL_SYSTEM_PROTECTION},
363
    {FWUPD_SECURITY_ATTR_ID_AMD_SPI_REPLAY_PROTECTION, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
364
    {FWUPD_SECURITY_ATTR_ID_AMD_SPI_WRITE_PROTECTION, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
365
    {FWUPD_SECURITY_ATTR_ID_AMD_PLATFORM_SECURE_BOOT, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
366
    {FWUPD_SECURITY_ATTR_ID_COREBOOT_VBOOT, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
367
    {FWUPD_SECURITY_ATTR_ID_ENCRYPTED_RAM, FWUPD_SECURITY_ATTR_LEVEL_SYSTEM_PROTECTION},
368
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_ACM, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
369
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_ENABLED, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
370
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_OTP, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
371
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_POLICY, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
372
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_VERIFIED, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
373
    {FWUPD_SECURITY_ATTR_ID_CET_ACTIVE, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
374
    {FWUPD_SECURITY_ATTR_ID_CET_ENABLED, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
375
    {FWUPD_SECURITY_ATTR_ID_INTEL_GDS, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
376
    {FWUPD_SECURITY_ATTR_ID_SMAP, FWUPD_SECURITY_ATTR_LEVEL_SYSTEM_PROTECTION},
377
    {FWUPD_SECURITY_ATTR_ID_IOMMU, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
378
    {FWUPD_SECURITY_ATTR_ID_MEI_MANUFACTURING_MODE, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
379
    {FWUPD_SECURITY_ATTR_ID_MEI_OVERRIDE_STRAP, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
380
    {FWUPD_SECURITY_ATTR_ID_MEI_KEY_MANIFEST, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
381
    {FWUPD_SECURITY_ATTR_ID_MEI_VERSION, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
382
    {FWUPD_SECURITY_ATTR_ID_PLATFORM_DEBUG_ENABLED, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
383
    {FWUPD_SECURITY_ATTR_ID_PLATFORM_DEBUG_LOCKED, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
384
    {FWUPD_SECURITY_ATTR_ID_PLATFORM_FUSED, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
385
    {FWUPD_SECURITY_ATTR_ID_PREBOOT_DMA_PROTECTION, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
386
    {FWUPD_SECURITY_ATTR_ID_SPI_BIOSWE, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
387
    {FWUPD_SECURITY_ATTR_ID_SPI_BLE, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
388
    {FWUPD_SECURITY_ATTR_ID_SPI_DESCRIPTOR, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
389
    {FWUPD_SECURITY_ATTR_ID_MTD_LOCKED, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
390
    {FWUPD_SECURITY_ATTR_ID_SPI_SMM_BWP, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
391
    {FWUPD_SECURITY_ATTR_ID_SUPPORTED_CPU, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
392
    {FWUPD_SECURITY_ATTR_ID_SUSPEND_TO_IDLE, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
393
    {FWUPD_SECURITY_ATTR_ID_SUSPEND_TO_RAM, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
394
    {FWUPD_SECURITY_ATTR_ID_HW_DISK_ENCRYPTION, FWUPD_SECURITY_ATTR_LEVEL_SYSTEM_PROTECTION},
395
    {FWUPD_SECURITY_ATTR_ID_TPM_EMPTY_PCR, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
396
    {FWUPD_SECURITY_ATTR_ID_TPM_RECONSTRUCTION_PCR0, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
397
    {FWUPD_SECURITY_ATTR_ID_TPM_VERSION_20, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
398
    {FWUPD_SECURITY_ATTR_ID_UEFI_PK, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
399
    {FWUPD_SECURITY_ATTR_ID_UEFI_SECUREBOOT, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
400
    {FWUPD_SECURITY_ATTR_ID_UEFI_BOOTSERVICE_VARS, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
401
    {FWUPD_SECURITY_ATTR_ID_BIOS_ROLLBACK_PROTECTION, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
402
    {FWUPD_SECURITY_ATTR_ID_BIOS_CAPSULE_UPDATES, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
403
    {FWUPD_SECURITY_ATTR_ID_AMD_SMM_LOCKED, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
404
    {FWUPD_SECURITY_ATTR_ID_UEFI_MEMORY_PROTECTION, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
405
    {FWUPD_SECURITY_ATTR_ID_UEFI_NX_COMPAT, FWUPD_SECURITY_ATTR_LEVEL_THEORETICAL},
406
    {FWUPD_SECURITY_ATTR_ID_UEFI_DB_MS_UEFI, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
407
    {FWUPD_SECURITY_ATTR_ID_UEFI_DB_PRODUCTION, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
408
    {FWUPD_SECURITY_ATTR_ID_HP_SURESTART, FWUPD_SECURITY_ATTR_LEVEL_IMPORTANT},
409
    {FWUPD_SECURITY_ATTR_ID_AMD_ENTRY_SIGN, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL},
410
};
411
412
static void
413
fu_security_attrs_ensure_level(FuSecurityAttr *attr)
414
0
{
415
0
  const gchar *appstream_id = fu_security_attr_get_appstream_id(attr);
416
417
  /* already set */
418
0
  if (fu_security_attr_get_level(attr) != FWUPD_SECURITY_ATTR_LEVEL_NONE)
419
0
    return;
420
421
  /* not required */
422
0
  if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_RUNTIME_ISSUE))
423
0
    return;
424
425
  /* map ID to level in one place */
426
0
  for (guint i = 0; i < G_N_ELEMENTS(appstream_id_level_map); i++) {
427
0
    if (g_strcmp0(appstream_id, appstream_id_level_map[i].appstream_id) == 0) {
428
0
      fu_security_attr_set_level(attr, appstream_id_level_map[i].level);
429
0
      return;
430
0
    }
431
0
  }
432
433
  /* somebody forgot to add to the level map... */
434
0
  g_warning("cannot map %s to a HSI level, assuming critical", appstream_id);
435
0
  fu_security_attr_set_level(attr, FWUPD_SECURITY_ATTR_LEVEL_CRITICAL);
436
0
}
437
438
static const struct {
439
  const gchar *appstream_id;
440
  const gchar *fwupd_version;
441
} appstream_id_version_map[] = {
442
    {FWUPD_SECURITY_ATTR_ID_AMD_ROLLBACK_PROTECTION, "1.8.0"},
443
    {FWUPD_SECURITY_ATTR_ID_AMD_SMM_LOCKED, "2.0.2"},
444
    {FWUPD_SECURITY_ATTR_ID_AMD_SPI_REPLAY_PROTECTION, "1.8.0"},
445
    {FWUPD_SECURITY_ATTR_ID_AMD_SPI_WRITE_PROTECTION, "1.8.0"},
446
    {FWUPD_SECURITY_ATTR_ID_BIOS_CAPSULE_UPDATES, "1.9.6"},
447
    {FWUPD_SECURITY_ATTR_ID_BIOS_ROLLBACK_PROTECTION, "1.8.8"},
448
    {FWUPD_SECURITY_ATTR_ID_AMD_ENTRY_SIGN, "2.1.2"},
449
    {FWUPD_SECURITY_ATTR_ID_AMD_PLATFORM_SECURE_BOOT, "2.1.1"},
450
    {FWUPD_SECURITY_ATTR_ID_CET_ACTIVE, "2.0.0"},
451
    {FWUPD_SECURITY_ATTR_ID_CET_ENABLED, "2.0.0"},
452
    {FWUPD_SECURITY_ATTR_ID_COREBOOT_VBOOT, "2.1.6"},
453
    {FWUPD_SECURITY_ATTR_ID_ENCRYPTED_RAM, "1.5.0"},
454
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_ACM, "1.5.0"},
455
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_ENABLED, "1.5.0"},
456
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_OTP, "1.5.0"},
457
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_POLICY, "1.5.0"},
458
    {FWUPD_SECURITY_ATTR_ID_INTEL_BOOTGUARD_VERIFIED, "1.5.0"},
459
    {FWUPD_SECURITY_ATTR_ID_INTEL_GDS, "1.9.4"},
460
    {FWUPD_SECURITY_ATTR_ID_IOMMU, "1.5.0"},
461
    {FWUPD_SECURITY_ATTR_ID_KERNEL_LOCKDOWN, "1.5.0"},
462
    {FWUPD_SECURITY_ATTR_ID_KERNEL_TAINTED, "1.5.0"},
463
    {FWUPD_SECURITY_ATTR_ID_MEI_KEY_MANIFEST, "1.8.7"},
464
    {FWUPD_SECURITY_ATTR_ID_MEI_MANUFACTURING_MODE, "1.5.0"},
465
    {FWUPD_SECURITY_ATTR_ID_MEI_OVERRIDE_STRAP, "1.5.0"},
466
    {FWUPD_SECURITY_ATTR_ID_MEI_VERSION, "1.5.0"},
467
    {FWUPD_SECURITY_ATTR_ID_PLATFORM_DEBUG_ENABLED, "1.5.0"},
468
    {FWUPD_SECURITY_ATTR_ID_PLATFORM_DEBUG_LOCKED, "1.8.0"},
469
    {FWUPD_SECURITY_ATTR_ID_PLATFORM_FUSED, "1.8.0"},
470
    {FWUPD_SECURITY_ATTR_ID_PREBOOT_DMA_PROTECTION, "1.8.0"},
471
    {FWUPD_SECURITY_ATTR_ID_SMAP, "2.0.0"},
472
    {FWUPD_SECURITY_ATTR_ID_SPI_BIOSWE, "1.5.0"},
473
    {FWUPD_SECURITY_ATTR_ID_SPI_BLE, "1.5.0"},
474
    {FWUPD_SECURITY_ATTR_ID_SPI_DESCRIPTOR, "1.6.0"},
475
    {FWUPD_SECURITY_ATTR_ID_MTD_LOCKED, "2.1.7"},
476
    {FWUPD_SECURITY_ATTR_ID_SPI_SMM_BWP, "1.5.0"},
477
    {FWUPD_SECURITY_ATTR_ID_SUPPORTED_CPU, "1.8.0"},
478
    {FWUPD_SECURITY_ATTR_ID_SUSPEND_TO_IDLE, "1.5.0"},
479
    {FWUPD_SECURITY_ATTR_ID_SUSPEND_TO_RAM, "1.5.0"},
480
    {FWUPD_SECURITY_ATTR_ID_HW_DISK_ENCRYPTION, "2.1.7"},
481
    {FWUPD_SECURITY_ATTR_ID_TPM_EMPTY_PCR, "1.7.2"},
482
    {FWUPD_SECURITY_ATTR_ID_TPM_RECONSTRUCTION_PCR0, "1.5.0"},
483
    {FWUPD_SECURITY_ATTR_ID_TPM_VERSION_20, "1.5.0"},
484
    {FWUPD_SECURITY_ATTR_ID_UEFI_BOOTSERVICE_VARS, "1.9.3"},
485
    {FWUPD_SECURITY_ATTR_ID_UEFI_DB_MS_UEFI, "2.0.8"},
486
    {FWUPD_SECURITY_ATTR_ID_UEFI_DB_PRODUCTION, "2.1.8"},
487
    {FWUPD_SECURITY_ATTR_ID_UEFI_MEMORY_PROTECTION, "2.0.7"},
488
    {FWUPD_SECURITY_ATTR_ID_UEFI_PK, "1.5.0"},
489
    {FWUPD_SECURITY_ATTR_ID_UEFI_SECUREBOOT, "1.5.0"},
490
    {FWUPD_SECURITY_ATTR_ID_HP_SURESTART, "2.1.1"},
491
    {FWUPD_SECURITY_ATTR_ID_UEFI_NX_COMPAT, "2.1.6"},
492
};
493
494
static void
495
fu_security_attrs_ensure_fwupd_version(FuSecurityAttr *attr)
496
0
{
497
0
  const gchar *appstream_id = fu_security_attr_get_appstream_id(attr);
498
499
  /* already set */
500
0
  if (fu_security_attr_get_fwupd_version(attr) != NULL)
501
0
    return;
502
503
  /* not required */
504
0
  if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_RUNTIME_ISSUE))
505
0
    return;
506
507
  /* map ID to fwupd version in one place */
508
0
  for (guint i = 0; i < G_N_ELEMENTS(appstream_id_version_map); i++) {
509
0
    if (g_strcmp0(appstream_id, appstream_id_version_map[i].appstream_id) == 0) {
510
0
      fu_security_attr_set_fwupd_version(
511
0
          attr,
512
0
          appstream_id_version_map[i].fwupd_version);
513
0
      return;
514
0
    }
515
0
  }
516
517
  /* somebody forgot to add to the level map... */
518
0
  g_warning("cannot map %s to a fwupd version", appstream_id);
519
0
}
520
521
static gboolean
522
fu_security_attrs_has_success(FuSecurityAttrs *self, const gchar *appstream_id)
523
0
{
524
0
  for (guint i = 0; i < self->attrs->len; i++) {
525
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
526
0
    const gchar *attr_id = fu_security_attr_get_appstream_id(attr);
527
528
0
    if (g_strcmp0(attr_id, appstream_id) != 0)
529
0
      continue;
530
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS))
531
0
      return TRUE;
532
0
  }
533
534
0
  return FALSE;
535
0
}
536
537
static void
538
fu_security_attrs_add_suspend_to_ram_obsoletes(FuSecurityAttrs *self)
539
0
{
540
0
  if (!fu_security_attrs_has_success(self, FWUPD_SECURITY_ATTR_ID_ENCRYPTED_RAM))
541
0
    return;
542
543
0
  for (guint i = 0; i < self->attrs->len; i++) {
544
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
545
0
    const gchar *attr_id = fu_security_attr_get_appstream_id(attr);
546
547
0
    if (g_strcmp0(attr_id, FWUPD_SECURITY_ATTR_ID_SUSPEND_TO_RAM) != 0)
548
0
      continue;
549
0
    if (fu_security_attr_get_result(attr) != FWUPD_SECURITY_ATTR_RESULT_ENABLED)
550
0
      continue;
551
0
    fu_security_attr_remove_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONFIG_FW);
552
0
    fu_security_attr_remove_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONFIG_OS);
553
0
    fu_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
554
0
    fu_security_attr_add_obsolete(attr, FWUPD_SECURITY_ATTR_ID_SUSPEND_TO_IDLE);
555
0
  }
556
0
}
557
558
/**
559
 * fu_security_attrs_depsolve:
560
 * @self: a #FuSecurityAttrs
561
 *
562
 * Marks any attributes with %FWUPD_SECURITY_ATTR_FLAG_OBSOLETED that have been
563
 * defined as obsoleted by other attributes.
564
 *
565
 * It is only required to call this function once, and should be done when all
566
 * attributes have been added. This will also sort the attrs.
567
 **/
568
void
569
fu_security_attrs_depsolve(FuSecurityAttrs *self)
570
0
{
571
0
  g_return_if_fail(FU_IS_SECURITY_ATTRS(self));
572
573
  /* assign HSI levels if not already done */
574
0
  for (guint i = 0; i < self->attrs->len; i++) {
575
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
576
0
    fu_security_attrs_ensure_level(attr);
577
0
    fu_security_attrs_ensure_fwupd_version(attr);
578
0
  }
579
580
0
  fu_security_attrs_add_suspend_to_ram_obsoletes(self);
581
582
  /* set flat where required */
583
0
  for (guint i = 0; i < self->attrs->len; i++) {
584
0
    FuSecurityAttr *attr = g_ptr_array_index(self->attrs, i);
585
0
    const gchar *attr_id = fu_security_attr_get_appstream_id(attr);
586
0
    const gchar *attr_plugin = fu_security_attr_get_plugin(attr);
587
0
    GPtrArray *obsoletes = fu_security_attr_get_obsoletes(attr);
588
589
0
    for (guint j = 0; j < self->attrs->len; j++) {
590
0
      FuSecurityAttr *attr_tmp = g_ptr_array_index(self->attrs, j);
591
0
      const gchar *attr_tmp_id = fu_security_attr_get_appstream_id(attr_tmp);
592
0
      const gchar *attr_tmp_plugin = fu_security_attr_get_plugin(attr_tmp);
593
594
      /* skip self */
595
0
      if (g_strcmp0(attr_plugin, attr_tmp_plugin) == 0 &&
596
0
          g_strcmp0(attr_id, attr_tmp_id) == 0)
597
0
        continue;
598
599
      /* add duplicate (negative) attributes when obsolete not explicitly set
600
       */
601
0
      if (obsoletes->len == 0) {
602
0
        if (g_strcmp0(attr_id, attr_tmp_id) != 0)
603
0
          continue;
604
0
        if (fu_security_attr_has_flag(attr,
605
0
                    FWUPD_SECURITY_ATTR_FLAG_SUCCESS))
606
0
          continue;
607
0
        if (fu_security_attr_has_flag(attr_tmp,
608
0
                    FWUPD_SECURITY_ATTR_FLAG_SUCCESS))
609
0
          continue;
610
611
0
        if (fu_security_attr_has_obsolete(attr, attr_id))
612
0
          continue;
613
0
        if (fu_security_attr_has_obsolete(attr_tmp, attr_id))
614
0
          continue;
615
0
        g_debug("duplicate security attr %s from plugin %s implicitly "
616
0
          "obsoleted by plugin %s",
617
0
          attr_id,
618
0
          attr_plugin,
619
0
          attr_tmp_plugin);
620
0
        fu_security_attr_add_obsolete(attr, attr_id);
621
0
      }
622
623
      /* walk all the obsoletes for matches appstream ID or plugin */
624
0
      for (guint k = 0; k < obsoletes->len; k++) {
625
0
        const gchar *obsolete = g_ptr_array_index(obsoletes, k);
626
627
0
        if (g_strcmp0(attr_tmp_id, obsolete) == 0 ||
628
0
            g_strcmp0(attr_tmp_plugin, obsolete) == 0) {
629
0
          g_debug("security attr %s:%s obsoleted by %s:%s",
630
0
            attr_tmp_id,
631
0
            attr_tmp_plugin,
632
0
            attr_id,
633
0
            attr_plugin);
634
0
          fu_security_attr_add_flag(
635
0
              attr_tmp,
636
0
              FWUPD_SECURITY_ATTR_FLAG_OBSOLETED);
637
0
        }
638
0
      }
639
0
    }
640
0
  }
641
642
  /* sort */
643
0
  g_ptr_array_sort(self->attrs, fu_security_attrs_sort_cb);
644
0
}
645
646
static void
647
fu_security_attrs_add_json(FwupdCodec *codec, FwupdJsonObject *json_obj, FwupdCodecFlags flags)
648
0
{
649
0
  FuSecurityAttrs *self = FU_SECURITY_ATTRS(codec);
650
0
  g_autoptr(GPtrArray) items = fu_security_attrs_get_all(self, NULL);
651
0
  g_autoptr(FwupdJsonArray) json_arr = fwupd_json_array_new();
652
653
0
  for (guint i = 0; i < items->len; i++) {
654
0
    FuSecurityAttr *attr = g_ptr_array_index(items, i);
655
0
    guint64 created = fu_security_attr_get_created(attr);
656
0
    g_autoptr(FwupdJsonObject) json_obj_tmp = fwupd_json_object_new();
657
658
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_OBSOLETED))
659
0
      continue;
660
0
    fu_security_attr_set_created(attr, 0);
661
0
    fwupd_codec_to_json(FWUPD_CODEC(attr), json_obj_tmp, FWUPD_CODEC_FLAG_NONE);
662
0
    fu_security_attr_set_created(attr, created);
663
0
    fwupd_json_array_add_object(json_arr, json_obj_tmp);
664
0
  }
665
0
  fwupd_json_object_add_array(json_obj, "SecurityAttributes", json_arr);
666
0
}
667
668
static gboolean
669
fu_security_attrs_from_json(FwupdCodec *codec, FwupdJsonObject *json_obj, GError **error)
670
0
{
671
0
  FuSecurityAttrs *self = FU_SECURITY_ATTRS(codec);
672
0
  g_autoptr(FuContext) ctx = fu_context_new();
673
0
  g_autoptr(FwupdJsonArray) json_arr = NULL;
674
675
  /* this has to exist */
676
0
  json_arr = fwupd_json_object_get_array(json_obj, "SecurityAttributes", error);
677
0
  if (json_arr == NULL)
678
0
    return FALSE;
679
0
  for (guint i = 0; i < fwupd_json_array_get_size(json_arr); i++) {
680
0
    g_autoptr(FwupdJsonObject) json_obj_tmp = NULL;
681
0
    g_autoptr(FuSecurityAttr) attr = fu_security_attr_new(ctx, NULL);
682
683
0
    json_obj_tmp = fwupd_json_array_get_object(json_arr, i, error);
684
0
    if (json_obj_tmp == NULL)
685
0
      return FALSE;
686
0
    if (!fwupd_codec_from_json(FWUPD_CODEC(attr), json_obj_tmp, error))
687
0
      return FALSE;
688
0
    if (fu_security_attr_has_flag(attr, FWUPD_SECURITY_ATTR_FLAG_OBSOLETED))
689
0
      continue;
690
0
    fu_security_attrs_append(self, attr);
691
0
  }
692
693
  /* success */
694
0
  return TRUE;
695
0
}
696
697
static void
698
fu_security_attrs_codec_iface_init(FwupdCodecInterface *iface)
699
0
{
700
0
  iface->add_json = fu_security_attrs_add_json;
701
0
  iface->from_json = fu_security_attrs_from_json;
702
0
}
703
704
/**
705
 * fu_security_attrs_compare:
706
 * @attrs1: a #FuSecurityAttrs
707
 * @attrs2: another #FuSecurityAttrs, perhaps newer in some way
708
 *
709
 * Compares the two objects, returning the differences.
710
 *
711
 * If the two sets of attrs are considered the same then an empty array is returned.
712
 * Only the AppStream ID results are compared, extra metadata is ignored.
713
 *
714
 * Returns: (element-type FuSecurityAttr) (transfer container): differences
715
 */
716
GPtrArray *
717
fu_security_attrs_compare(FuSecurityAttrs *attrs1, FuSecurityAttrs *attrs2)
718
0
{
719
0
  g_autoptr(GHashTable) hash1 = g_hash_table_new(g_str_hash, g_str_equal);
720
0
  g_autoptr(GHashTable) hash2 = g_hash_table_new(g_str_hash, g_str_equal);
721
0
  g_autoptr(GPtrArray) array1 = fu_security_attrs_get_all(attrs1, NULL);
722
0
  g_autoptr(GPtrArray) array2 = fu_security_attrs_get_all(attrs2, NULL);
723
0
  g_autoptr(GPtrArray) results =
724
0
      g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
725
726
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(attrs1), NULL);
727
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTRS(attrs2), NULL);
728
729
  /* create hash tables of appstream-id -> FuSecurityAttr */
730
0
  for (guint i = 0; i < array1->len; i++) {
731
0
    FuSecurityAttr *attr1 = g_ptr_array_index(array1, i);
732
0
    g_hash_table_insert(hash1,
733
0
            (gpointer)fu_security_attr_get_appstream_id(attr1),
734
0
            (gpointer)attr1);
735
0
  }
736
0
  for (guint i = 0; i < array2->len; i++) {
737
0
    FuSecurityAttr *attr2 = g_ptr_array_index(array2, i);
738
0
    g_hash_table_insert(hash2,
739
0
            (gpointer)fu_security_attr_get_appstream_id(attr2),
740
0
            (gpointer)attr2);
741
0
  }
742
743
  /* present in attrs2, not present in attrs1 */
744
0
  for (guint i = 0; i < array2->len; i++) {
745
0
    FuSecurityAttr *attr1;
746
0
    FuSecurityAttr *attr2 = g_ptr_array_index(array2, i);
747
0
    attr1 = g_hash_table_lookup(hash1, fu_security_attr_get_appstream_id(attr2));
748
0
    if (attr1 == NULL) {
749
0
      g_autoptr(FuSecurityAttr) attr = fu_security_attr_copy(attr2);
750
0
      g_ptr_array_add(results, g_steal_pointer(&attr));
751
0
      continue;
752
0
    }
753
0
  }
754
755
  /* present in attrs1, not present in attrs2 */
756
0
  for (guint i = 0; i < array1->len; i++) {
757
0
    FuSecurityAttr *attr1 = g_ptr_array_index(array1, i);
758
0
    FuSecurityAttr *attr2;
759
0
    attr2 = g_hash_table_lookup(hash2, fu_security_attr_get_appstream_id(attr1));
760
0
    if (attr2 == NULL) {
761
0
      g_autoptr(FuSecurityAttr) attr = fu_security_attr_copy(attr1);
762
0
      fu_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_UNKNOWN);
763
0
      fu_security_attr_set_result_fallback(attr, /* flip these around */
764
0
                   fu_security_attr_get_result(attr1));
765
0
      g_ptr_array_add(results, g_steal_pointer(&attr));
766
0
      continue;
767
0
    }
768
0
  }
769
770
  /* find any attributes that differ */
771
0
  for (guint i = 0; i < array2->len; i++) {
772
0
    FuSecurityAttr *attr1;
773
0
    FuSecurityAttr *attr2 = g_ptr_array_index(array2, i);
774
0
    attr1 = g_hash_table_lookup(hash1, fu_security_attr_get_appstream_id(attr2));
775
0
    if (attr1 == NULL)
776
0
      continue;
777
778
    /* result of specific attr differed */
779
0
    if (fu_security_attr_get_result(attr1) != fu_security_attr_get_result(attr2)) {
780
0
      g_autoptr(FuSecurityAttr) attr = fu_security_attr_copy(attr1);
781
0
      fu_security_attr_set_result(attr, fu_security_attr_get_result(attr2));
782
0
      fu_security_attr_set_result_fallback(attr,
783
0
                   fu_security_attr_get_result(attr1));
784
0
      fu_security_attr_set_flags(attr, fu_security_attr_get_flags(attr2));
785
0
      g_ptr_array_add(results, g_steal_pointer(&attr));
786
0
    }
787
0
  }
788
789
  /* success */
790
0
  return g_steal_pointer(&results);
791
0
}
792
793
/**
794
 * fu_security_attrs_equal:
795
 * @attrs1: a #FuSecurityAttrs
796
 * @attrs2: another #FuSecurityAttrs
797
 *
798
 * Tests the objects for equality. Only the AppStream ID results are compared, extra metadata
799
 * is ignored.
800
 *
801
 * Returns: %TRUE if the set of attrs can be considered equal
802
 */
803
gboolean
804
fu_security_attrs_equal(FuSecurityAttrs *attrs1, FuSecurityAttrs *attrs2)
805
0
{
806
0
  g_autoptr(GPtrArray) compare = fu_security_attrs_compare(attrs1, attrs2);
807
0
  return compare->len == 0;
808
0
}
809
810
/**
811
 * fu_security_attrs_new:
812
 *
813
 * Returns: a security attribute
814
 **/
815
FuSecurityAttrs *
816
fu_security_attrs_new(void)
817
0
{
818
0
  return g_object_new(FU_TYPE_SECURITY_ATTRS, NULL);
819
0
}