1 | /* $Id: tpm-linux.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Trusted Platform Module (TPM) access, Linux variant.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_DEFAULT
|
---|
32 | #include <iprt/tpm.h>
|
---|
33 |
|
---|
34 | #include <iprt/assertcompile.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/file.h>
|
---|
38 | #include <iprt/log.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/linux/sysfs.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Defined Constants And Macros *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Internal TPM instance data.
|
---|
55 | */
|
---|
56 | typedef struct RTTPMINT
|
---|
57 | {
|
---|
58 | /** Handle to the /dev/tpmX device. */
|
---|
59 | RTFILE hTpmDev;
|
---|
60 | /** Handle to the sysfs cancel interface. */
|
---|
61 | RTFILE hTpmCancel;
|
---|
62 | /** The deduced TPM version. */
|
---|
63 | RTTPMVERSION enmTpmVers;
|
---|
64 | /** Flag whether a request is currently being executed. */
|
---|
65 | volatile bool fReqExec;
|
---|
66 | } RTTPMINT;
|
---|
67 | /** Pointer to the internal TPM instance data. */
|
---|
68 | typedef RTTPMINT *PRTTPMINT;
|
---|
69 |
|
---|
70 |
|
---|
71 | /*********************************************************************************************************************************
|
---|
72 | * Internal Functions *
|
---|
73 | *********************************************************************************************************************************/
|
---|
74 |
|
---|
75 | RTDECL(int) RTTpmOpen(PRTTPM phTpm, uint32_t idTpm)
|
---|
76 | {
|
---|
77 | AssertPtrReturn(phTpm, VERR_INVALID_POINTER);
|
---|
78 | if (idTpm == RTTPM_ID_DEFAULT)
|
---|
79 | idTpm = 0;
|
---|
80 |
|
---|
81 | int rc = VINF_SUCCESS;
|
---|
82 | PRTTPMINT pThis = (PRTTPMINT)RTMemAllocZ(sizeof(*pThis));
|
---|
83 | if (pThis)
|
---|
84 | {
|
---|
85 | pThis->hTpmDev = NIL_RTFILE;
|
---|
86 | pThis->hTpmCancel = NIL_RTFILE;
|
---|
87 | pThis->enmTpmVers = RTTPMVERSION_UNKNOWN;
|
---|
88 | pThis->fReqExec = false;
|
---|
89 |
|
---|
90 | rc = RTFileOpenF(&pThis->hTpmDev, RTFILE_O_OPEN | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE,
|
---|
91 | "/dev/tpm%u", idTpm);
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | {
|
---|
94 | /* Open the sysfs path to cancel a request, either /sys/class/tpm/tpmX/device/cancel or /sys/class/misc/tpmX/device/cancel. */
|
---|
95 | rc = RTFileOpenF(&pThis->hTpmCancel, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
|
---|
96 | "/sys/class/tpm/tpm%u/device/cancel", idTpm);
|
---|
97 | if (rc == VERR_FILE_NOT_FOUND)
|
---|
98 | rc = RTFileOpenF(&pThis->hTpmCancel, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE,
|
---|
99 | "/sys/class/misc/tpm%u/device/cancel", idTpm);
|
---|
100 | if ( RT_SUCCESS(rc)
|
---|
101 | || rc == VERR_FILE_NOT_FOUND)
|
---|
102 | {
|
---|
103 | /* Try to figure out the TPM version. */
|
---|
104 | int64_t iVersion = 0;
|
---|
105 | rc = RTLinuxSysFsReadIntFile(10 /*uBase*/, &iVersion, "/sys/class/tpm/tpm%u/tpm_version_major", idTpm);
|
---|
106 | if (rc == VERR_FILE_NOT_FOUND)
|
---|
107 | rc = RTLinuxSysFsReadIntFile(10 /*uBase*/, &iVersion, "/sys/class/misc/tpm%u/tpm_version_major", idTpm);
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | if (iVersion == 1)
|
---|
111 | pThis->enmTpmVers = RTTPMVERSION_1_2;
|
---|
112 | else if (iVersion == 2)
|
---|
113 | pThis->enmTpmVers = RTTPMVERSION_2_0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | *phTpm = pThis;
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 | RTFileClose(pThis->hTpmDev);
|
---|
121 | pThis->hTpmDev = NIL_RTFILE;
|
---|
122 | }
|
---|
123 |
|
---|
124 | RTMemFree(pThis);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | rc = VERR_NO_MEMORY;
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(int) RTTpmClose(RTTPM hTpm)
|
---|
133 | {
|
---|
134 | PRTTPMINT pThis = hTpm;
|
---|
135 |
|
---|
136 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
137 |
|
---|
138 | RTFileClose(pThis->hTpmDev);
|
---|
139 | if (pThis->hTpmCancel != NIL_RTFILE)
|
---|
140 | RTFileClose(pThis->hTpmCancel);
|
---|
141 |
|
---|
142 | pThis->hTpmDev = NIL_RTFILE;
|
---|
143 | pThis->hTpmCancel = NIL_RTFILE;
|
---|
144 | RTMemFree(pThis);
|
---|
145 | return VINF_SUCCESS;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | RTDECL(RTTPMVERSION) RTTpmGetVersion(RTTPM hTpm)
|
---|
150 | {
|
---|
151 | PRTTPMINT pThis = hTpm;
|
---|
152 |
|
---|
153 | AssertPtrReturn(pThis, RTTPMVERSION_INVALID);
|
---|
154 | return pThis->enmTpmVers;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | RTDECL(uint32_t) RTTpmGetLocalityMax(RTTPM hTpm)
|
---|
159 | {
|
---|
160 | RT_NOREF(hTpm);
|
---|
161 | return 0; /* On Linux only TPM locality 0 is supported. */
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | RTDECL(int) RTTpmReqCancel(RTTPM hTpm)
|
---|
166 | {
|
---|
167 | PRTTPMINT pThis = hTpm;
|
---|
168 |
|
---|
169 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
170 | if (pThis->hTpmCancel == NIL_RTFILE)
|
---|
171 | return VERR_NOT_SUPPORTED;
|
---|
172 |
|
---|
173 | if (ASMAtomicReadBool(&pThis->fReqExec))
|
---|
174 | {
|
---|
175 | uint8_t bCancel = '-';
|
---|
176 | return RTFileWrite(pThis->hTpmCancel, &bCancel, sizeof(bCancel), NULL /*pcbWritten*/);
|
---|
177 | }
|
---|
178 |
|
---|
179 | return VINF_SUCCESS;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | RTDECL(int) RTTpmReqExec(RTTPM hTpm, uint8_t bLoc, const void *pvReq, size_t cbReq,
|
---|
184 | void *pvResp, size_t cbRespMax, size_t *pcbResp)
|
---|
185 | {
|
---|
186 | PRTTPMINT pThis = hTpm;
|
---|
187 |
|
---|
188 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
189 | AssertPtrReturn(pvReq, VERR_INVALID_POINTER);
|
---|
190 | AssertPtrReturn(pvResp, VERR_INVALID_POINTER);
|
---|
191 | AssertReturn(cbReq && cbRespMax, VERR_INVALID_PARAMETER);
|
---|
192 | AssertReturn(bLoc == 0, VERR_NOT_SUPPORTED); /** @todo There doesn't seem to be a way to use a different locality. */
|
---|
193 |
|
---|
194 | /* The request has to be supplied by a single blocking write. */
|
---|
195 | ASMAtomicXchgBool(&pThis->fReqExec, true);
|
---|
196 | int rc = RTFileWrite(pThis->hTpmDev, pvReq, cbReq, NULL /*pcbWritten*/);
|
---|
197 | if (RT_SUCCESS(rc))
|
---|
198 | {
|
---|
199 | size_t cbResp = 0;
|
---|
200 | /* The response has to be retrieved in a single read as well. */
|
---|
201 | rc = RTFileRead(pThis->hTpmDev, pvResp, cbRespMax, &cbResp);
|
---|
202 | ASMAtomicXchgBool(&pThis->fReqExec, false);
|
---|
203 | if (RT_SUCCESS(rc))
|
---|
204 | {
|
---|
205 | /* Check whether the response is complete. */
|
---|
206 | if ( cbResp >= sizeof(TPMRESPHDR)
|
---|
207 | && RTTpmRespGetSz((PCTPMRESPHDR)pvResp) == cbResp)
|
---|
208 | {
|
---|
209 | if (pcbResp)
|
---|
210 | *pcbResp = cbResp;
|
---|
211 | }
|
---|
212 | else
|
---|
213 | rc = VERR_BUFFER_OVERFLOW;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|