aboutsummaryrefslogtreecommitdiff
path: root/hw/uefi/var-service-policy.c
blob: 3b1155fe4ea1439bcc23bbc211e6290fb09f20ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * uefi vars device - VarCheckPolicyLibMmiHandler implementation
 *
 * variable policy specs:
 * https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Library/VariablePolicyLib/ReadMe.md
 */
#include "qemu/osdep.h"
#include "system/dma.h"
#include "migration/vmstate.h"

#include "hw/uefi/var-service.h"
#include "hw/uefi/var-service-api.h"
#include "hw/uefi/var-service-edk2.h"

#include "trace/trace-hw_uefi.h"

static void calc_policy(uefi_var_policy *pol);

static int uefi_var_policy_post_load(void *opaque, int version_id)
{
    uefi_var_policy *pol = opaque;

    calc_policy(pol);
    return 0;
}

const VMStateDescription vmstate_uefi_var_policy = {
    .name = "uefi-var-policy",
    .post_load = uefi_var_policy_post_load,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(entry_size, uefi_var_policy),
        VMSTATE_VBUFFER_ALLOC_UINT32(entry, uefi_var_policy,
                                     0, NULL, entry_size),
        VMSTATE_END_OF_LIST()
    },
};

static void print_policy_entry(variable_policy_entry *pe)
{
    uint16_t *name = (void *)pe + pe->offset_to_name;

    fprintf(stderr, "%s:\n", __func__);

    fprintf(stderr, "    name ยด");
    while (*name) {
        fprintf(stderr, "%c", *name);
        name++;
    }
    fprintf(stderr, "', version=%d.%d, size=%d\n",
            pe->version >> 16, pe->version & 0xffff, pe->size);

    if (pe->min_size) {
        fprintf(stderr, "    size min=%d\n", pe->min_size);
    }
    if (pe->max_size != UINT32_MAX) {
        fprintf(stderr, "    size max=%u\n", pe->max_size);
    }
    if (pe->attributes_must_have) {
        fprintf(stderr, "    attr must=0x%x\n", pe->attributes_must_have);
    }
    if (pe->attributes_cant_have) {
        fprintf(stderr, "    attr cant=0x%x\n", pe->attributes_cant_have);
    }
    if (pe->lock_policy_type) {
        fprintf(stderr, "    lock policy type %d\n", pe->lock_policy_type);
    }
}

static gboolean wildcard_str_equal(uefi_var_policy *pol,
                                   uefi_variable *var)
{
    return uefi_str_equal_ex(pol->name, pol->name_size,
                             var->name, var->name_size,
                             true);
}

static uefi_var_policy *find_policy(uefi_vars_state *uv, QemuUUID guid,
                                    uint16_t *name, uint64_t name_size)
{
    uefi_var_policy *pol;

    QTAILQ_FOREACH(pol, &uv->var_policies, next) {
        if (!qemu_uuid_is_equal(&pol->entry->namespace, &guid)) {
            continue;
        }
        if (!uefi_str_equal(pol->name, pol->name_size,
                            name, name_size)) {
            continue;
        }
        return pol;
    }
    return NULL;
}

static uefi_var_policy *wildcard_find_policy(uefi_vars_state *uv,
                                             uefi_variable *var)
{
    uefi_var_policy *pol;

    QTAILQ_FOREACH(pol, &uv->var_policies, next) {
        if (!qemu_uuid_is_equal(&pol->entry->namespace, &var->guid)) {
            continue;
        }
        if (!wildcard_str_equal(pol, var)) {
            continue;
        }
        return pol;
    }
    return NULL;
}

static void calc_policy(uefi_var_policy *pol)
{
    variable_policy_entry *pe = pol->entry;
    unsigned int i;

    pol->name = (void *)pol->entry + pe->offset_to_name;
    pol->name_size = pe->size - pe->offset_to_name;

    for (i = 0; i < pol->name_size / 2; i++) {
        if (pol->name[i] == '#') {
            pol->hashmarks++;
        }
    }
}

uefi_var_policy *uefi_vars_add_policy(uefi_vars_state *uv,
                                      variable_policy_entry *pe)
{
    uefi_var_policy *pol, *p;

    pol = g_new0(uefi_var_policy, 1);
    pol->entry = g_malloc(pe->size);
    memcpy(pol->entry, pe, pe->size);
    pol->entry_size = pe->size;

    calc_policy(pol);

    /* keep list sorted by priority, add to tail of priority group */
    QTAILQ_FOREACH(p, &uv->var_policies, next) {
        if ((p->hashmarks > pol->hashmarks) ||
            (!p->name_size && pol->name_size)) {
            QTAILQ_INSERT_BEFORE(p, pol, next);
            return pol;
        }
    }

    QTAILQ_INSERT_TAIL(&uv->var_policies, pol, next);
    return pol;
}

efi_status uefi_vars_policy_check(uefi_vars_state *uv,
                                  uefi_variable *var,
                                  gboolean is_newvar)
{
    uefi_var_policy *pol;
    variable_policy_entry *pe;
    variable_lock_on_var_state *lvarstate;
    uint16_t *lvarname;
    size_t lvarnamesize;
    uefi_variable *lvar;

    if (!uv->end_of_dxe) {
        return EFI_SUCCESS;
    }

    pol = wildcard_find_policy(uv, var);
    if (!pol) {
        return EFI_SUCCESS;
    }
    pe = pol->entry;

    uefi_trace_variable(__func__, var->guid, var->name, var->name_size);
    print_policy_entry(pe);

    if ((var->attributes & pe->attributes_must_have) != pe->attributes_must_have) {
        trace_uefi_vars_policy_deny("must-have-attr");
        return EFI_INVALID_PARAMETER;
    }
    if ((var->attributes & pe->attributes_cant_have) != 0) {
        trace_uefi_vars_policy_deny("cant-have-attr");
        return EFI_INVALID_PARAMETER;
    }

    if (var->data_size < pe->min_size) {
        trace_uefi_vars_policy_deny("min-size");
        return EFI_INVALID_PARAMETER;
    }
    if (var->data_size > pe->max_size) {
        trace_uefi_vars_policy_deny("max-size");
        return EFI_INVALID_PARAMETER;
    }

    switch (pe->lock_policy_type) {
    case VARIABLE_POLICY_TYPE_NO_LOCK:
        break;

    case VARIABLE_POLICY_TYPE_LOCK_NOW:
        trace_uefi_vars_policy_deny("lock-now");
        return EFI_WRITE_PROTECTED;

    case VARIABLE_POLICY_TYPE_LOCK_ON_CREATE:
        if (!is_newvar) {
            trace_uefi_vars_policy_deny("lock-on-create");
            return EFI_WRITE_PROTECTED;
        }
        break;

    case VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE:
        lvarstate    = (void *)pol->entry + sizeof(*pe);
        lvarname     = (void *)pol->entry + sizeof(*pe) + sizeof(*lvarstate);
        lvarnamesize = pe->offset_to_name - sizeof(*pe) - sizeof(*lvarstate);

        uefi_trace_variable(__func__, lvarstate->namespace,
                            lvarname, lvarnamesize);
        lvar = uefi_vars_find_variable(uv, lvarstate->namespace,
                                          lvarname, lvarnamesize);
        if (lvar && lvar->data_size == 1) {
            uint8_t *value = lvar->data;
            if (lvarstate->value == *value) {
                return EFI_WRITE_PROTECTED;
            }
        }
        break;
    }

    return EFI_SUCCESS;
}

void uefi_vars_policies_clear(uefi_vars_state *uv)
{
    uefi_var_policy *pol;

    while (!QTAILQ_EMPTY(&uv->var_policies)) {
        pol = QTAILQ_FIRST(&uv->var_policies);
        QTAILQ_REMOVE(&uv->var_policies, pol, next);
        g_free(pol->entry);
        g_free(pol);
    }
}

static size_t uefi_vars_mm_policy_error(mm_header *mhdr,
                                        mm_check_policy *mchk,
                                        uint64_t status)
{
    mchk->result = status;
    return sizeof(*mchk);
}

static uint32_t uefi_vars_mm_check_policy_is_enabled(uefi_vars_state *uv,
                                                     mm_header       *mhdr,
                                                     mm_check_policy *mchk,
                                                     void            *func)
{
    mm_check_policy_is_enabled *mpar = func;
    size_t length;

    length = sizeof(*mchk) + sizeof(*mpar);
    if (mhdr->length < length) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }

    mpar->state  = TRUE;
    mchk->result = EFI_SUCCESS;
    return sizeof(*mchk);
}

static uint32_t uefi_vars_mm_check_policy_register(uefi_vars_state *uv,
                                                   mm_header       *mhdr,
                                                   mm_check_policy *mchk,
                                                   void            *func)
{
    variable_policy_entry *pe = func;
    uefi_var_policy *pol;
    uint64_t length;

    if (uadd64_overflow(sizeof(*mchk), pe->size, &length)) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }
    if (mhdr->length < length) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }
    if (pe->size < sizeof(*pe)) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }
    if (pe->offset_to_name < sizeof(*pe)) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }

    if (pe->lock_policy_type == VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE &&
        pe->offset_to_name < sizeof(*pe) + sizeof(variable_lock_on_var_state)) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }

    /* check space for minimum string length */
    if (pe->size < (size_t)pe->offset_to_name) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
    }

    if (!uefi_str_is_valid((void *)pe + pe->offset_to_name,
                           pe->size - pe->offset_to_name,
                           false)) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_INVALID_PARAMETER);
    }

    pol = find_policy(uv, pe->namespace,
                      (void *)pe + pe->offset_to_name,
                      pe->size - pe->offset_to_name);
    if (pol) {
        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_ALREADY_STARTED);
    }

    uefi_vars_add_policy(uv, pe);

    mchk->result = EFI_SUCCESS;
    return sizeof(*mchk);
}

uint32_t uefi_vars_mm_check_policy_proto(uefi_vars_state *uv)
{
    static const char *fnames[] = {
        "zero",
        "disable",
        "is-enabled",
        "register",
        "dump",
        "lock",
    };
    const char      *fname;
    mm_header       *mhdr = (mm_header *) uv->buffer;
    mm_check_policy *mchk = (mm_check_policy *) (uv->buffer + sizeof(*mhdr));
    void            *func = (uv->buffer + sizeof(*mhdr) + sizeof(*mchk));

    if (mhdr->length < sizeof(*mchk)) {
        return UEFI_VARS_STS_ERR_BAD_BUFFER_SIZE;
    }

    fname = mchk->command < ARRAY_SIZE(fnames)
        ? fnames[mchk->command]
        : "unknown";
    trace_uefi_vars_policy_cmd(fname);

    switch (mchk->command) {
    case VAR_CHECK_POLICY_COMMAND_DISABLE:
        mchk->result = EFI_UNSUPPORTED;
        break;
    case VAR_CHECK_POLICY_COMMAND_IS_ENABLED:
        uefi_vars_mm_check_policy_is_enabled(uv, mhdr, mchk, func);
        break;
    case VAR_CHECK_POLICY_COMMAND_REGISTER:
        if (uv->policy_locked) {
            mchk->result = EFI_WRITE_PROTECTED;
        } else {
            uefi_vars_mm_check_policy_register(uv, mhdr, mchk, func);
        }
        break;
    case VAR_CHECK_POLICY_COMMAND_LOCK:
        uv->policy_locked = true;
        mchk->result = EFI_SUCCESS;
        break;
    default:
        mchk->result = EFI_UNSUPPORTED;
        break;
    }

    uefi_trace_status(__func__, mchk->result);
    return UEFI_VARS_STS_SUCCESS;
}