summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/r3/linux/tpm-linux.cpp
blob: 4851eabc9d3d5d07f6ce3eb35d650011cc997aa5 (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
/* $Id: tpm-linux.cpp $ */
/** @file
 * IPRT - Trusted Platform Module (TPM) access, Linux variant.
 */

/*
 * Copyright (C) 2021-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_DEFAULT
#include <iprt/tpm.h>

#include <iprt/assertcompile.h>
#include <iprt/asm.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/log.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/linux/sysfs.h>


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/**
 * Internal TPM instance data.
 */
typedef struct RTTPMINT
{
    /** Handle to the /dev/tpmX device. */
    RTFILE                      hTpmDev;
    /** Handle to the sysfs cancel interface. */
    RTFILE                      hTpmCancel;
    /** The deduced TPM version. */
    RTTPMVERSION                enmTpmVers;
    /** Flag whether a request is currently being executed. */
    volatile bool               fReqExec;
} RTTPMINT;
/** Pointer to the internal TPM instance data. */
typedef RTTPMINT *PRTTPMINT;


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/

RTDECL(int) RTTpmOpen(PRTTPM phTpm, uint32_t idTpm)
{
    AssertPtrReturn(phTpm, VERR_INVALID_POINTER);
    if (idTpm == RTTPM_ID_DEFAULT)
        idTpm = 0;

    int rc = VINF_SUCCESS;
    PRTTPMINT pThis = (PRTTPMINT)RTMemAllocZ(sizeof(*pThis));
    if (pThis)
    {
        pThis->hTpmDev    = NIL_RTFILE;
        pThis->hTpmCancel = NIL_RTFILE;
        pThis->enmTpmVers = RTTPMVERSION_UNKNOWN;
        pThis->fReqExec   = false;

        rc = RTFileOpenF(&pThis->hTpmDev, RTFILE_O_OPEN | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE,
                         "/dev/tpm%u", idTpm);
        if (RT_SUCCESS(rc))
        {
            /* Open the sysfs path to cancel a request, either /sys/class/tpm/tpmX/device/cancel or /sys/class/misc/tpmX/device/cancel. */
            rc = RTFileOpenF(&pThis->hTpmCancel, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
                             "/sys/class/tpm/tpm%u/device/cancel", idTpm);
            if (rc == VERR_FILE_NOT_FOUND)
                rc = RTFileOpenF(&pThis->hTpmCancel, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
                                 "/sys/class/misc/tpm%u/device/cancel", idTpm);
            if (   RT_SUCCESS(rc)
                || rc == VERR_FILE_NOT_FOUND)
            {
                /* Try to figure out the TPM version. */
                int64_t iVersion = 0;
                rc = RTLinuxSysFsReadIntFile(10 /*uBase*/, &iVersion, "/sys/class/tpm/tpm%u/tpm_version_major", idTpm);
                if (rc == VERR_FILE_NOT_FOUND)
                    rc = RTLinuxSysFsReadIntFile(10 /*uBase*/, &iVersion, "/sys/class/misc/tpm%u/tpm_version_major", idTpm);
                if (RT_SUCCESS(rc))
                {
                    if (iVersion == 1)
                        pThis->enmTpmVers = RTTPMVERSION_1_2;
                    else if (iVersion == 2)
                        pThis->enmTpmVers = RTTPMVERSION_2_0;
                }

                *phTpm = pThis;
                return VINF_SUCCESS;
            }

            RTFileClose(pThis->hTpmDev);
            pThis->hTpmDev = NIL_RTFILE;
        }

        RTMemFree(pThis);
    }
    else
        rc = VERR_NO_MEMORY;
    return rc;
}


RTDECL(int) RTTpmClose(RTTPM hTpm)
{
    PRTTPMINT pThis = hTpm;

    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);

    RTFileClose(pThis->hTpmDev);
    if (pThis->hTpmCancel != NIL_RTFILE)
        RTFileClose(pThis->hTpmCancel);

    pThis->hTpmDev    = NIL_RTFILE;
    pThis->hTpmCancel = NIL_RTFILE;
    RTMemFree(pThis);
    return VINF_SUCCESS;
}


RTDECL(RTTPMVERSION) RTTpmGetVersion(RTTPM hTpm)
{
    PRTTPMINT pThis = hTpm;

    AssertPtrReturn(pThis, RTTPMVERSION_INVALID);
    return pThis->enmTpmVers;
}


RTDECL(uint32_t) RTTpmGetLocalityMax(RTTPM hTpm)
{
    RT_NOREF(hTpm);
    return 0; /* On Linux only TPM locality 0 is supported. */
}


RTDECL(int) RTTpmReqCancel(RTTPM hTpm)
{
    PRTTPMINT pThis = hTpm;

    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    if (pThis->hTpmCancel == NIL_RTFILE)
        return VERR_NOT_SUPPORTED;

    if (ASMAtomicReadBool(&pThis->fReqExec))
    {
        uint8_t bCancel = '-';
        return RTFileWrite(pThis->hTpmCancel, &bCancel, sizeof(bCancel), NULL /*pcbWritten*/);
    }

    return VINF_SUCCESS;
}


RTDECL(int) RTTpmReqExec(RTTPM hTpm, uint8_t bLoc, const void *pvReq, size_t cbReq,
                         void *pvResp, size_t cbRespMax, size_t *pcbResp)
{
    PRTTPMINT pThis = hTpm;

    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertPtrReturn(pvReq, VERR_INVALID_POINTER);
    AssertPtrReturn(pvResp, VERR_INVALID_POINTER);
    AssertReturn(cbReq && cbRespMax, VERR_INVALID_PARAMETER);
    AssertReturn(bLoc == 0, VERR_NOT_SUPPORTED); /** @todo There doesn't seem to be a way to use a different locality. */

    /* The request has to be supplied by a single blocking write. */
    ASMAtomicXchgBool(&pThis->fReqExec, true);
    int rc = RTFileWrite(pThis->hTpmDev, pvReq, cbReq, NULL /*pcbWritten*/);
    if (RT_SUCCESS(rc))
    {
        size_t cbResp = 0;
        /* The response has to be retrieved in a single read as well. */
        rc = RTFileRead(pThis->hTpmDev, pvResp, cbRespMax, &cbResp);
        ASMAtomicXchgBool(&pThis->fReqExec, false);
        if (RT_SUCCESS(rc))
        {
            /* Check whether the response is complete. */
            if (   cbResp >= sizeof(TPMRESPHDR)
                && RTTpmRespGetSz((PCTPMRESPHDR)pvResp) == cbResp)
            {
                if (pcbResp)
                    *pcbResp = cbResp;
            }
            else
                rc = VERR_BUFFER_OVERFLOW;
        }
    }

    return rc;
}