• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / swtpm / #2915

04 Apr 2026 04:50AM UTC coverage: 72.916% (-0.4%) from 73.286%
#2915

push

travis-ci

web-flow
Merge 60feae31e into e1e01d1f2

7689 of 10545 relevant lines covered (72.92%)

6467.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/src/swtpm/mainloop.c
1
/*
2
 * mainloop.c -- The TPM Emulator's main processing loop
3
 *
4
 * (c) Copyright IBM Corporation 2014, 2015, 2016
5
 *
6
 * Author: Stefan Berger <stefanb@us.ibm.com>
7
 *
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are
12
 * met:
13
 *
14
 * Redistributions of source code must retain the above copyright notice,
15
 * this list of conditions and the following disclaimer.
16
 *
17
 * Redistributions in binary form must reproduce the above copyright
18
 * notice, this list of conditions and the following disclaimer in the
19
 * documentation and/or other materials provided with the distribution.
20
 *
21
 * Neither the names of the IBM Corporation nor the names of its
22
 * contributors may be used to endorse or promote products derived from
23
 * this software without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 */
37

38
/* mainLoop() is the main server loop.
39

40
   It reads a TPM request, processes the ordinal, and writes the response
41
*/
42

43
#include <config.h>
44

45
#include <errno.h>
46
#include <stdio.h>
47
#include <stdint.h>
48
#include <stdbool.h>
49
#include <stdlib.h>
50
#include <string.h>
51
#include <poll.h>
52
#include <sys/stat.h>
53
#include <fcntl.h>
54
#include <sys/socket.h>
55

56
#include <libtpms/tpm_error.h>
57
#include <libtpms/tpm_library.h>
58
#include <libtpms/tpm_memory.h>
59

60
#include "swtpm_debug.h"
61
#include "swtpm_io.h"
62
#include "tpmlib.h"
63
#include "locality.h"
64
#include "logging.h"
65
#include "ctrlchannel.h"
66
#include "mainloop.h"
67
#include "utils.h"
68
#include "sys_dependencies.h"
69
#include "compiler_dependencies.h"
70
#include "swtpm_utils.h"
71
#include "swtpm_nvstore.h"
72

73
/* local variables */
74
static TPM_MODIFIER_INDICATOR g_locality;
75

76
bool g_mainloop_terminate;
77

78
TPM_RESULT
79
mainloop_cb_get_locality(TPM_MODIFIER_INDICATOR *loc,
1,955✔
80
                         uint32_t tpmnum SWTPM_ATTR_UNUSED)
81
{
82
    *loc = g_locality;
1,955✔
83

84
    return TPM_SUCCESS;
1,955✔
85
}
86

87
/* ensure that the storage is locked; returns false in case of failure */
88
bool mainloop_ensure_locked_storage(struct mainLoopParams *mlp)
1,993✔
89
{
90
    TPM_RESULT res;
1,993✔
91

92
    if (mlp->storage_locked)
1,993✔
93
        return true;
94

95
    /* if NVRAM hasn't been initialized yet locking may need to be retried */
96
    res = SWTPM_NVRAM_Lock_Storage(mlp->locking_retries);
152✔
97
    if (res == TPM_RETRY)
152✔
98
        return true;
99
    if (res != TPM_SUCCESS)
25✔
100
        return false;
101

102
    mlp->locking_retries = 0;
25✔
103
    mlp->storage_locked = true;
25✔
104
    mlp->incoming_migration = false;
25✔
105

106
    return true;
25✔
107
}
108

109
void mainloop_unlock_nvram(struct mainLoopParams *mlp,
7✔
110
                           unsigned int locking_retries)
111
{
112
    SWTPM_NVRAM_Unlock();
7✔
113

114
    mlp->storage_locked = false;
7✔
115
    mlp->locking_retries = locking_retries;
7✔
116
}
7✔
117

118
int mainLoop(struct mainLoopParams *mlp, int notify_fd, bool tpm_running)
395✔
119
{
120
    TPM_RESULT          rc = 0;
395✔
121
    TPM_CONNECTION_FD   connection_fd;             /* file descriptor for read/write */
395✔
122
    unsigned char       *command = NULL;           /* command buffer */
395✔
123
    uint32_t            command_length;            /* actual length of command bytes */
395✔
124
    uint32_t            max_command_length;        /* command buffer size */
395✔
125
    off_t               cmd_offset;
395✔
126
    /* The response buffer is reused for each command. Thus it can grow but never shrink */
127
    unsigned char       *rbuffer = NULL;           /* actual response bytes */
395✔
128
    uint32_t            rlength = 0;               /* bytes in response buffer */
395✔
129
    uint32_t            rTotal = 0;                /* total allocated bytes */
395✔
130
    int                 ctrlfd;
395✔
131
    int                 ctrlclntfd;
395✔
132
    int                 sockfd;
395✔
133
    int                 ready;
395✔
134
    struct iovec        iov[3];
395✔
135
    uint32_t            ack = htobe32(0);
395✔
136
    struct tpm2_resp_prefix respprefix;
395✔
137
    uint32_t            lastCommand;
395✔
138

139
    /* poolfd[] indexes */
140
    enum {
395✔
141
        DATA_CLIENT_FD = 0,
142
        NOTIFY_FD,
143
        CTRL_SERVER_FD,
144
        CTRL_CLIENT_FD,
145
        DATA_SERVER_FD
146
    };
147

148
    TPM_DEBUG("mainLoop:\n");
395✔
149

150
    max_command_length = tpmlib_get_tpm_property(TPMPROP_TPM_BUFFER_MAX) +
395✔
151
                         sizeof(struct tpm2_send_command_prefix);
152

153
    command = malloc(max_command_length);
395✔
154
    if (!command) {
395✔
155
        logprintf(STDERR_FILENO, "Could not allocate %u bytes for buffer.\n",
×
156
                  max_command_length);
157
        return TPM_FAIL;
×
158
    }
159

160
    /* header and trailer that we may send by setting iov_len */
161
    iov[0].iov_base = &respprefix;
395✔
162
    iov[0].iov_len = 0;
395✔
163
    iov[2].iov_base = &ack;
395✔
164
    iov[2].iov_len = 0;
395✔
165

166
    connection_fd.fd = -1;
395✔
167
    ctrlfd = ctrlchannel_get_fd(mlp->cc);
395✔
168
    ctrlclntfd = ctrlchannel_get_client_fd(mlp->cc);
395✔
169

170
    sockfd = SWTPM_IO_GetSocketFD();
395✔
171

172
    if (mlp->startupType != _TPM_ST_NONE) {
395✔
173
        command_length = tpmlib_create_startup_cmd(
206✔
174
                                  mlp->startupType,
175
                                  mlp->tpmversion,
176
                                  command, max_command_length);
177
        if (command_length > 0) {
206✔
178
            pcap_packet_record_write(&mlp->ps, command, command_length, true);
206✔
179

180
            mlp->lastCommand = tpmlib_get_cmd_ordinal(command, command_length);
206✔
181
            rc = TPMLIB_Process(&rbuffer, &rlength, &rTotal,
206✔
182
                                command, command_length);
183

184
            pcap_packet_record_write(&mlp->ps, rbuffer, rlength, false);
206✔
185
        }
186

187
        if (rc || command_length == 0) {
206✔
188
            g_mainloop_terminate = true;
×
189
            if (rc)
×
190
                logprintf(STDERR_FILENO, "Could not send Startup: 0x%x\n", rc);
×
191
        }
192
    }
193

194
    while (!g_mainloop_terminate) {
1,525✔
195

196
        while (rc == 0) {
9,509✔
197
            if (mlp->flags & MAIN_LOOP_FLAG_USE_FD) {
9,319✔
198
                if (connection_fd.fd != mlp->fd) {
1,353✔
199
                    SWTPM_IO_Disconnect(&connection_fd);
143✔
200
                    connection_fd.fd = mlp->fd;
143✔
201
                }
202
            }
203

204
            struct pollfd pollfds[] = {
9,319✔
205
                [DATA_CLIENT_FD] = {
206
                    .fd = connection_fd.fd,
9,319✔
207
                    .events = POLLIN | POLLHUP,
208
                    .revents = 0,
209
                },
210
                [NOTIFY_FD] = {
211
                    .fd = notify_fd,
212
                    .events = POLLIN,
213
                    .revents = 0,
214
                },
215
                [CTRL_SERVER_FD] = {
216
                    .fd = -1,
217
                    .events = POLLIN,
218
                    .revents = 0,
219
                },
220
                [CTRL_CLIENT_FD] = {
221
                    .fd = ctrlclntfd,
222
                    .events = POLLIN | POLLHUP,
223
                    .revents = 0,
224
                },
225
                [DATA_SERVER_FD] = {
226
                    /* listen socket for accepting clients */
227
                    .fd = -1,
228
                    .events = POLLIN,
229
                    .revents = 0,
230
                }
231
            };
232

233
            /* only listen for clients if we don't have one */
234
            if (connection_fd.fd < 0)
9,319✔
235
                pollfds[DATA_SERVER_FD].fd = sockfd;
7,039✔
236
            if (ctrlclntfd < 0)
9,319✔
237
                pollfds[CTRL_SERVER_FD].fd = ctrlfd;
2,848✔
238

239
            ready = poll(pollfds, 5, -1);
9,319✔
240
            if (ready < 0 && errno == EINTR)
9,319✔
241
                continue;
7,023✔
242

243
            if (ready < 0 ||
9,316✔
244
                (pollfds[NOTIFY_FD].revents & POLLIN) != 0) {
9,316✔
245
                SWTPM_IO_Disconnect(&connection_fd);
3✔
246
                break;
947✔
247
            }
248

249
            if (pollfds[DATA_CLIENT_FD].revents & (POLLHUP | POLLERR)) {
9,313✔
250
                logprintf(STDERR_FILENO, "Data client disconnected\n");
65✔
251
                mlp->fd = -1;
65✔
252
                /* chardev and unixio get this signal, not tcp */
253
                if (mlp->flags & MAIN_LOOP_FLAG_END_ON_HUP) {
65✔
254
                    /* only the chardev terminates here */
255
                    g_mainloop_terminate = true;
×
256
                    break;
×
257
                }
258
            }
259

260
            if (pollfds[DATA_SERVER_FD].revents & POLLIN)
9,313✔
261
                connection_fd.fd = accept(pollfds[DATA_SERVER_FD].fd, NULL, 0);
740✔
262

263
            if (pollfds[CTRL_SERVER_FD].revents & POLLIN)
9,313✔
264
                ctrlclntfd = accept(ctrlfd, NULL, 0);
1,182✔
265

266
            if (pollfds[CTRL_CLIENT_FD].revents & POLLIN) {
9,313✔
267
                ctrlclntfd = ctrlchannel_process_fd(ctrlclntfd,
5,491✔
268
                                                    &g_mainloop_terminate,
269
                                                    &g_locality, &tpm_running,
270
                                                    mlp);
271
                if (ctrlclntfd < 0 &&
5,491✔
272
                    mlp->flags & MAIN_LOOP_FLAG_CTRL_END_ON_HUP)
934✔
273
                    g_mainloop_terminate = true;
4✔
274

275
                if (g_mainloop_terminate)
5,491✔
276
                    break;
277
            }
278

279
            if (pollfds[CTRL_CLIENT_FD].revents & POLLHUP) {
8,924✔
280
                if (ctrlclntfd >= 0)
484✔
281
                    close(ctrlclntfd);
×
282
                ctrlclntfd = -1;
484✔
283
                /* unixio gets this signal, not tcp */
284
                if (mlp->flags & MAIN_LOOP_FLAG_CTRL_END_ON_HUP) {
484✔
285
                    g_mainloop_terminate = true;
×
286
                    break;
×
287
                }
288
            }
289

290
            if (!(pollfds[DATA_CLIENT_FD].revents & POLLIN))
8,924✔
291
                continue;
7,020✔
292

293
            /* before processing a command ensure that the storage is locked */
294
            if ((g_mainloop_terminate = !mainloop_ensure_locked_storage(mlp)))
1,904✔
295
                break;
296

297
            /* Read the command.  The number of bytes is determined by 'paramSize' in the stream */
298
            if (rc == 0) {
1,904✔
299
                rc = SWTPM_IO_Read(&connection_fd, command, &command_length,
1,904✔
300
                                   max_command_length, &mlp->ps);
301
                if (rc != 0) {
1,904✔
302
                    /* connection broke */
303
                    SWTPM_IO_Disconnect(&connection_fd);
369✔
304
                }
305
            }
306

307
            cmd_offset = 0;
1,904✔
308
            /* Handle optional TCG Header in front of TPM 2 Command */
309
            if (rc == 0 && mlp->tpmversion == TPMLIB_TPM_VERSION_2) {
1,904✔
310
                cmd_offset = tpmlib_handle_tcg_tpm2_cmd_header(command,
1,092✔
311
                                                               command_length,
312
                                                               &g_locality);
313
                if (cmd_offset > 0) {
1,092✔
314
                    /* send header and trailer */
315
                    iov[0].iov_len = sizeof(respprefix);
×
316
                    iov[2].iov_len = sizeof(ack);
×
317
                } else {
318
                    iov[0].iov_len = 0;
1,092✔
319
                    iov[2].iov_len = 0;
1,092✔
320
                }
321
            }
322

323
            if (rc == 0) {
1,904✔
324
                if (!tpm_running) {
1,535✔
325
                    tpmlib_write_fatal_error_response(&rbuffer, &rlength,
14✔
326
                                                      &rTotal,
327
                                                      mlp->tpmversion);
328
                    goto skip_process;
14✔
329
                }
330
            }
331

332
            if (rc == 0) {
1,890✔
333
                lastCommand =
1,521✔
334
                    tpmlib_get_cmd_ordinal(&command[cmd_offset],
3,042✔
335
                                           command_length - cmd_offset);
1,521✔
336
                if (lastCommand != TPM_ORDINAL_NONE)
1,521✔
337
                    mlp->lastCommand = lastCommand;
1,521✔
338
            }
339

340
            if (rc == 0) {
1,890✔
341
                rlength = 0;                                /* clear the response buffer */
1,521✔
342
                rc = tpmlib_process(&rbuffer,
1,521✔
343
                                    &rlength,
344
                                    &rTotal,
345
                                    &command[cmd_offset],
346
                                    command_length - cmd_offset,
347
                                    mlp->locality_flags,
348
                                    &g_locality,
349
                                    mlp->tpmversion);
350
                if (rlength)
1,521✔
351
                    goto skip_process;
×
352
            }
353

354
            if (rc == 0) {
1,890✔
355
                rlength = 0;                                /* clear the response buffer */
1,521✔
356
                rc = TPMLIB_Process(&rbuffer,
1,521✔
357
                                    &rlength,
358
                                    &rTotal,
359
                                    &command[cmd_offset],
360
                                    command_length - cmd_offset);
361
            }
362

363
skip_process:
369✔
364
            /* write the results */
365
            if (rc == 0) {
1,904✔
366
                respprefix.size = htobe32(rlength);
1,535✔
367
                iov[1].iov_base = rbuffer;
1,535✔
368
                iov[1].iov_len  = rlength;
1,535✔
369

370
                SWTPM_IO_Write(&connection_fd, iov, ARRAY_LEN(iov),
1,535✔
371
                               &mlp->ps);
372
            }
373

374
            if (!(mlp->flags & MAIN_LOOP_FLAG_KEEP_CONNECTION)) {
1,904✔
375
                SWTPM_IO_Disconnect(&connection_fd);
552✔
376
                break;
552✔
377
            }
378
        }
379

380
        rc = 0; /* A fatal TPM_Process() error should cause the TPM to enter shutdown.  IO errors
1,134✔
381
                   are outside the TPM, so the TPM does not shut down.  The main loop should
382
                   continue to function.*/
383
        if (connection_fd.fd < 0 && mlp->flags & MAIN_LOOP_FLAG_TERMINATE)
1,134✔
384
            break;
385
    }
386

387
    if (tpm_running && !mlp->disable_auto_shutdown)
395✔
388
        tpmlib_maybe_send_tpm2_shutdown(mlp->tpmversion, &mlp->lastCommand,
5✔
389
                                        &mlp->ps);
390

391
    free(rbuffer);
395✔
392
    free(command);
395✔
393

394
    if (ctrlclntfd >= 0)
395✔
395
        close(ctrlclntfd);
386✔
396
    ctrlchannel_set_client_fd(mlp->cc, -1);
395✔
397

398
    if (connection_fd.fd >= 0 && !(mlp->flags & MAIN_LOOP_FLAG_USE_FD))
395✔
399
        close(connection_fd.fd);
×
400

401
    if (mlp->fd >= 0) {
395✔
402
        close(mlp->fd);
377✔
403
        mlp->fd = -1;
377✔
404
    }
405

406
    return rc;
395✔
407
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc