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-attr.c
Line
Count
Source
1
/*
2
 * Copyright 2022 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
0
#define G_LOG_DOMAIN "FuSecurityAttr"
8
9
#include "config.h"
10
11
#include "fwupd-security-attr-private.h"
12
13
#include "fu-security-attr.h"
14
#include "fu-version-common.h"
15
16
typedef struct {
17
  FuContext *ctx;
18
} FuSecurityAttrPrivate;
19
20
0
G_DEFINE_TYPE_WITH_PRIVATE(FuSecurityAttr, fu_security_attr, FWUPD_TYPE_SECURITY_ATTR)
21
0
22
0
#define GET_PRIVATE(o) (fu_security_attr_get_instance_private(o))
23
24
/**
25
 * fu_security_attr_check_fwupd_version:
26
 * @self: a #FuSecurityAttr
27
 * @fwupd_version: a fwupd version, e.g. `2.0.7`
28
 *
29
 * Checks if this attribute was available in a given fwupd release.
30
 *
31
 * If @fwupd_version is %NULL then expect %TRUE.
32
 *
33
 * Returns: %TRUE if the fwupd release contained this attribute
34
 **/
35
gboolean
36
fu_security_attr_check_fwupd_version(FuSecurityAttr *self, const gchar *fwupd_version)
37
0
{
38
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTR(self), FALSE);
39
0
  if (fwupd_version == NULL)
40
0
    return TRUE;
41
0
  if (fu_security_attr_get_fwupd_version(self) == NULL)
42
0
    return TRUE;
43
0
  return fu_version_compare(fwupd_version,
44
0
          fu_security_attr_get_fwupd_version(self),
45
0
          FWUPD_VERSION_FORMAT_UNKNOWN) >= 0;
46
0
}
47
48
/**
49
 * fu_security_attr_add_bios_target_value:
50
 * @self: a #FuSecurityAttr
51
 * @id: a #FwupdBiosSetting ID or name
52
 * @needle: The substring of a target value
53
 *
54
 * Checks all configured possible values of an enumeration attribute and
55
 * if any match @needle then set as the target value.
56
 **/
57
void
58
fu_security_attr_add_bios_target_value(FuSecurityAttr *self, const gchar *id, const gchar *needle)
59
0
{
60
0
  FuSecurityAttrPrivate *priv = GET_PRIVATE(self);
61
0
  FuBiosSetting *bios_setting;
62
0
  GPtrArray *values;
63
0
  const gchar *current;
64
65
0
  bios_setting = fu_context_get_bios_setting(priv->ctx, id);
66
0
  if (bios_setting == NULL)
67
0
    return;
68
0
  current = fu_bios_setting_get_current_value(bios_setting);
69
0
  fu_security_attr_set_bios_setting_id(self, fu_bios_setting_get_id(bios_setting));
70
0
  fu_security_attr_set_bios_setting_current_value(self, current);
71
0
  if (fu_bios_setting_get_kind(bios_setting) != FWUPD_BIOS_SETTING_KIND_ENUMERATION)
72
0
    return;
73
0
  if (fu_bios_setting_get_read_only(bios_setting))
74
0
    return;
75
0
  values = fu_bios_setting_get_possible_values(bios_setting);
76
0
  for (guint i = 0; i < values->len; i++) {
77
0
    const gchar *possible = g_ptr_array_index(values, i);
78
0
    g_autofree gchar *lower = g_utf8_strdown(possible, -1);
79
0
    if (g_strrstr(lower, needle)) {
80
0
      fu_security_attr_set_bios_setting_target_value(self, possible);
81
      /* this is built-in to the engine */
82
0
      if (g_strcmp0(possible, current) != 0) {
83
0
        fu_security_attr_add_flag(self, FWUPD_SECURITY_ATTR_FLAG_CAN_FIX);
84
0
        fu_security_attr_add_flag(self, FWUPD_SECURITY_ATTR_FLAG_CAN_UNDO);
85
0
      }
86
0
      return;
87
0
    }
88
0
  }
89
0
}
90
91
static FuContext *
92
fu_security_attr_get_context(FuSecurityAttr *self)
93
0
{
94
0
  FuSecurityAttrPrivate *priv = GET_PRIVATE(self);
95
0
  g_return_val_if_fail(FU_IS_SECURITY_ATTR(self), NULL);
96
0
  return priv->ctx;
97
0
}
98
99
static void
100
fu_security_attr_set_context(FuSecurityAttr *self, FuContext *ctx)
101
0
{
102
0
  FuSecurityAttrPrivate *priv = GET_PRIVATE(self);
103
0
  g_return_if_fail(FU_IS_SECURITY_ATTR(self));
104
0
  g_return_if_fail(FU_IS_CONTEXT(ctx));
105
0
  g_set_object(&priv->ctx, ctx);
106
0
}
107
108
/**
109
 * fu_security_attr_copy:
110
 * @donor: a #FuSecurityAttr
111
 *
112
 * Makes a full (deep) copy of a security attribute.
113
 *
114
 * Returns: (transfer full): a #FuSecurityAttr
115
 **/
116
FuSecurityAttr *
117
fu_security_attr_copy(FuSecurityAttr *donor)
118
0
{
119
0
  g_autoptr(FuSecurityAttr) self = g_object_new(FU_TYPE_SECURITY_ATTR, NULL);
120
0
  fu_security_attr_set_context(self, fu_security_attr_get_context(donor));
121
0
  fwupd_security_attr_incorporate(FWUPD_SECURITY_ATTR(self), FWUPD_SECURITY_ATTR(donor));
122
0
  fwupd_security_attr_set_created(
123
0
      FWUPD_SECURITY_ATTR(self),
124
0
      fwupd_security_attr_get_created(FWUPD_SECURITY_ATTR(donor)));
125
0
  return g_steal_pointer(&self);
126
0
}
127
128
static void
129
fu_security_attr_init(FuSecurityAttr *self)
130
0
{
131
0
}
132
133
static void
134
fu_security_attr_dispose(GObject *object)
135
0
{
136
0
  FuSecurityAttr *self = FU_SECURITY_ATTR(object);
137
0
  FuSecurityAttrPrivate *priv = GET_PRIVATE(self);
138
0
  g_clear_object(&priv->ctx);
139
0
  G_OBJECT_CLASS(fu_security_attr_parent_class)->dispose(object);
140
0
}
141
142
static void
143
fu_security_attr_class_init(FuSecurityAttrClass *klass)
144
0
{
145
0
  GObjectClass *object_class = G_OBJECT_CLASS(klass);
146
0
  object_class->dispose = fu_security_attr_dispose;
147
0
}
148
149
/**
150
 * fu_security_attr_new:
151
 * @ctx: a #FuContext
152
 * @appstream_id: (nullable): the AppStream component ID, e.g. `com.intel.BiosGuard`
153
 *
154
 * Creates a new #FuSecurityAttr with context set.
155
 *
156
 * Returns: (transfer full): a #FuSecurityAttr
157
 **/
158
FuSecurityAttr *
159
fu_security_attr_new(FuContext *ctx, const gchar *appstream_id)
160
0
{
161
0
  g_autoptr(FuSecurityAttr) self = g_object_new(FU_TYPE_SECURITY_ATTR, NULL);
162
0
  g_return_val_if_fail(FU_IS_CONTEXT(ctx), NULL);
163
0
  if (appstream_id != NULL)
164
0
    fu_security_attr_set_appstream_id(FWUPD_SECURITY_ATTR(self), appstream_id);
165
0
  fu_security_attr_set_context(self, ctx);
166
0
  return g_steal_pointer(&self);
167
0
}