Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/crypto/secret_keyring.c
Line
Count
Source
1
/*
2
 * QEMU crypto secret support
3
 *
4
 * Copyright 2020 Yandex N.V.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 *
19
 */
20
21
#include "qemu/osdep.h"
22
#include <asm/unistd.h>
23
#include <linux/keyctl.h>
24
#include "qapi/error.h"
25
#include "qom/object_interfaces.h"
26
#include "trace.h"
27
#include "crypto/secret_keyring.h"
28
29
30
static inline
31
long keyctl_read(int32_t key, uint8_t *buffer, size_t buflen)
32
0
{
33
0
    return syscall(__NR_keyctl, KEYCTL_READ, key, buffer, buflen, 0);
34
0
}
35
36
37
static void
38
qcrypto_secret_keyring_load_data(QCryptoSecretCommon *sec_common,
39
                                 uint8_t **output,
40
                                 size_t *outputlen,
41
                                 Error **errp)
42
0
{
43
0
    QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(sec_common);
44
0
    g_autofree uint8_t *buffer = NULL;
45
0
    long retcode;
46
47
0
    *output = NULL;
48
0
    *outputlen = 0;
49
50
0
    if (!secret->serial) {
51
0
        error_setg(errp, "'serial' parameter must be provided");
52
0
        return;
53
0
    }
54
55
0
    retcode = keyctl_read(secret->serial, NULL, 0);
56
0
    if (retcode <= 0) {
57
0
        goto keyctl_error;
58
0
    }
59
60
0
    buffer = g_new0(uint8_t, retcode);
61
62
0
    retcode = keyctl_read(secret->serial, buffer, retcode);
63
0
    if (retcode < 0) {
64
0
        goto keyctl_error;
65
0
    }
66
67
0
    *outputlen = retcode;
68
0
    *output = g_steal_pointer(&buffer);
69
0
    return;
70
71
0
keyctl_error:
72
0
    error_setg_errno(errp, errno,
73
0
                     "Unable to read serial key %08x",
74
0
                     secret->serial);
75
0
}
76
77
78
static void
79
qcrypto_secret_prop_set_key(Object *obj, Visitor *v,
80
                            const char *name, void *opaque,
81
                            Error **errp)
82
0
{
83
0
    QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(obj);
84
0
    int32_t value;
85
0
    visit_type_int32(v, name, &value, errp);
86
0
    if (!value) {
87
0
        error_setg(errp, "'serial' should not be equal to 0");
88
0
    }
89
0
    secret->serial = value;
90
0
}
91
92
93
static void
94
qcrypto_secret_prop_get_key(Object *obj, Visitor *v,
95
                            const char *name, void *opaque,
96
                            Error **errp)
97
0
{
98
0
    QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(obj);
99
0
    int32_t value = secret->serial;
100
0
    visit_type_int32(v, name, &value, errp);
101
0
}
102
103
104
static void
105
qcrypto_secret_keyring_class_init(ObjectClass *oc, const void *data)
106
61
{
107
61
    QCryptoSecretCommonClass *sic = QCRYPTO_SECRET_COMMON_CLASS(oc);
108
61
    sic->load_data = qcrypto_secret_keyring_load_data;
109
110
61
    object_class_property_add(oc, "serial", "int32_t",
111
61
                                  qcrypto_secret_prop_get_key,
112
61
                                  qcrypto_secret_prop_set_key,
113
61
                                  NULL, NULL);
114
61
}
115
116
117
static const TypeInfo qcrypto_secret_info = {
118
    .parent = TYPE_QCRYPTO_SECRET_COMMON,
119
    .name = TYPE_QCRYPTO_SECRET_KEYRING,
120
    .instance_size = sizeof(QCryptoSecretKeyring),
121
    .class_init = qcrypto_secret_keyring_class_init,
122
};
123
124
125
static void
126
qcrypto_secret_register_types(void)
127
61
{
128
61
    type_register_static(&qcrypto_secret_info);
129
61
}
130
131
132
type_init(qcrypto_secret_register_types);