aboutsummaryrefslogtreecommitdiff
path: root/libstb/tss/tpmLogMgr.C
blob: 3596684a0aaa82a1168a1de437fd42d64c2045fb (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/trusted/tpmLogMgr.C $                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2016                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file TpmLogMgr.C
 *
 * @brief TPM Event log manager
 */

/////////////////////////////////////////////////////////////////
// NOTE: This file is exportable as TSS-Lite for skiboot/PHYP  //
/////////////////////////////////////////////////////////////////

// ----------------------------------------------
// Includes
// ----------------------------------------------
#include <string.h>
#include "tpmLogMgr.H"
#ifdef __HOSTBOOT_MODULE
#include <sys/mm.h>
#include <util/align.H>
#include <secureboot/trustedboot_reasoncodes.H>
#else
#include "trustedboot_reasoncodes.H"
#endif
#include "trustedbootUtils.H"
#include "trustedboot.H"

#ifdef __cplusplus
namespace TRUSTEDBOOT
{
#endif

    uint32_t TCG_EfiSpecIdEventStruct_size(TCG_EfiSpecIdEventStruct* val)
    {
        return (sizeof(TCG_EfiSpecIdEventStruct) + val->vendorInfoSize);
    }

#ifdef __HOSTBOOT_MODULE
    errlHndl_t TpmLogMgr_initialize(TpmLogMgr* val)
    {
        errlHndl_t err = TB_SUCCESS;
        const char vendorInfo[] = "IBM";
        const char eventSignature[] = "Spec ID Event03";
        TCG_EfiSpecIdEventStruct* eventData = NULL;

        TCG_PCR_EVENT eventLogEntry;

        TRACUCOMP( g_trac_trustedboot, ">>initialize()");

        if (NULL == val)
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM LOG INIT FAIL");

                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_INIT_FAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_INITIALIZE
                 * @userdata1      0
                 * @userdata2      0
                 * @devdesc        TPM log buffer init failure.
                 * @custdesc       TPM log buffer init failure.
                 */
                err = tpmCreateErrorLog( MOD_TPMLOGMGR_INITIALIZE,
                                         RC_TPMLOGMGR_INIT_FAIL, 0, 0);

        }
        else
        {

            memset(val, 0, sizeof(TpmLogMgr));
            val->logMaxSize = TPMLOG_BUFFER_SIZE;

            mutex_init( &val->logMutex );
            mutex_lock( &val->logMutex );

            // Assign our new event pointer to the start
            val->newEventPtr = val->eventLog;
            memset(val->eventLog, 0, TPMLOG_BUFFER_SIZE);

            eventData = (TCG_EfiSpecIdEventStruct*) eventLogEntry.event;

            // Add the header event log
            // Values here come from the PC ClientSpecificPlatformProfile spec
            eventLogEntry.eventType = EV_NO_ACTION;
            eventLogEntry.pcrIndex = 0;
            eventLogEntry.eventSize = sizeof(TCG_EfiSpecIdEventStruct) +
                sizeof(vendorInfo);

            memcpy(eventData->signature, eventSignature,
                   sizeof(eventSignature));
            eventData->platformClass = htole32(TPM_PLATFORM_SERVER);
            eventData->specVersionMinor = TPM_SPEC_MINOR;
            eventData->specVersionMajor = TPM_SPEC_MAJOR;
            eventData->specErrata = TPM_SPEC_ERRATA;
            eventData->uintnSize = 1;
            eventData->numberOfAlgorithms = htole32(HASH_COUNT);
            eventData->digestSizes[0].algorithmId = htole16(TPM_ALG_SHA256);
            eventData->digestSizes[0].digestSize = htole16(TPM_ALG_SHA256_SIZE);
            eventData->digestSizes[1].algorithmId = htole16(TPM_ALG_SHA1);
            eventData->digestSizes[1].digestSize = htole16(TPM_ALG_SHA1_SIZE);
            eventData->vendorInfoSize = sizeof(vendorInfo);
            memcpy(eventData->vendorInfo, vendorInfo, sizeof(vendorInfo));
            val->newEventPtr = TCG_PCR_EVENT_logMarshal(&eventLogEntry,
                                                        val->newEventPtr);

            // Done, move our pointers
            val->logSize += TCG_PCR_EVENT_marshalSize(&eventLogEntry);

            mutex_unlock( &val->logMutex );

            // Debug display of raw data
            TRACUBIN(g_trac_trustedboot, "tpmInitialize: Header Entry",
                     val->eventLog, val->logSize);

            TRACUCOMP( g_trac_trustedboot,
                       "<<initialize() LS:%d - %s",
                       val->logSize,
                       ((TB_SUCCESS == err) ? "No Error" : "With Error") );
        }
        return err;
    }
#endif

    errlHndl_t TpmLogMgr_initializeUsingExistingLog(TpmLogMgr* val,
                                                    uint8_t* eventLogPtr,
                                                    uint32_t eventLogSize)
    {
        errlHndl_t err = TB_SUCCESS;
        TRACUCOMP( g_trac_trustedboot,
                   ">>initializeUsingExistingLog()");

        do
        {

            mutex_init( &val->logMutex );
            mutex_lock( &val->logMutex );

            val->logMaxSize = eventLogSize;
            val->eventLogInMem = eventLogPtr;

            // Ok, walk the log to figure out how big this is
            val->logSize = TpmLogMgr_calcLogSize(val);

            if (0 == val->logSize)
            {
                TRACFCOMP( g_trac_trustedboot,
                       "TPM LOG INIT WALK FAIL");
                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_LOGWALKFAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_INITIALIZEEXISTLOG
                 * @userdata1      0
                 * @userdata2      0
                 * @devdesc        TPM log header entry is missing.
                 * @custdesc       TPM log invalid format
                 */
                err = tpmCreateErrorLog(MOD_TPMLOGMGR_INITIALIZEEXISTLOG,
                                        RC_TPMLOGMGR_LOGWALKFAIL,
                                        0,
                                        0);
                break;
            }
            // We are good, let's move the newEventLogPtr
            val->newEventPtr = val->eventLogInMem + val->logSize;

        }
        while(0);

        if (TB_SUCCESS != err)
        {
            val->eventLogInMem = NULL;
            val->newEventPtr = NULL;
            val->logMaxSize = 0;
            val->logSize = 0;
        }

        mutex_unlock( &val->logMutex );

        return err;
    }

    errlHndl_t TpmLogMgr_addEvent(TpmLogMgr* val,
                                  TCG_PCR_EVENT2* logEvent)
    {
        errlHndl_t err = TB_SUCCESS;
        size_t newLogSize = TCG_PCR_EVENT2_marshalSize(logEvent);

        TRACUCOMP( g_trac_trustedboot,
                   ">>tpmAddEvent() PCR:%d Type:%d Size:%d",
                   logEvent->pcrIndex, logEvent->eventType, (int)newLogSize);

        mutex_lock( &val->logMutex );

        do
        {

            // Need to ensure we have room for the new event
            // We have to leave room for the log full event as well
            if (NULL == val->newEventPtr ||
                val->logSize + newLogSize > val->logMaxSize)
            {
                TRACFCOMP( g_trac_trustedboot,
                           "TPM LOG ADD FAIL PNULL(%d) LS(%d) New LS(%d)"
                           " Max LS(%d)",
                           (NULL == val->newEventPtr ? 0 : 1),
                           (int)val->logSize, (int)newLogSize,
                           (int)val->logMaxSize);

                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_ADDEVENT_FAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_ADDEVENT
                 * @userdata1[0:31]  Max log size
                 * @userdata1[32:63] Log buffer NULL
                 * @userdata2[0:31]  Current Log Size
                 * @userdata2[32:63] New entry size
                 * @devdesc        TPM log buffer add failure.
                 * @custdesc       TPM log overflow
                 */
                err = tpmCreateErrorLog( MOD_TPMLOGMGR_ADDEVENT,
                                         RC_TPMLOGMGR_ADDEVENT_FAIL,
                                         (uint64_t)val->logMaxSize << 32 |
                                         (NULL == val->newEventPtr ? 0 : 1),
                                         (uint64_t)val->logSize << 32 |
                                         newLogSize);

                break;
            }

            val->newEventPtr = TCG_PCR_EVENT2_logMarshal(logEvent,
                                                         val->newEventPtr);

            if (NULL == val->newEventPtr)
            {
                TRACFCOMP( g_trac_trustedboot,
                           "TPM LOG MARSHAL Fail");

                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_ADDEVENTMARSH_FAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_ADDEVENT
                 * @userdata1      0
                 * @userdata2      0
                 * @devdesc        log buffer marshal failure.
                 * @custdesc       TPM log operation failure
                 */
                err = tpmCreateErrorLog( MOD_TPMLOGMGR_ADDEVENT,
                                         RC_TPMLOGMGR_ADDEVENTMARSH_FAIL,
                                         0,
                                         0);
                break;
            }

            val->logSize += newLogSize;


        } while ( 0 );

        TRACUCOMP( g_trac_trustedboot,
                   "<<tpmAddEvent() LS:%d - %s",
                   (int)val->logSize,
                   ((TB_SUCCESS == err) ? "No Error" : "With Error") );

        mutex_unlock( &val->logMutex );
        return err;
    }

    uint32_t TpmLogMgr_getLogSize(TpmLogMgr* val)
    {
        return val->logSize;
    }

#ifdef __HOSTBOOT_MODULE
    void TpmLogMgr_dumpLog(TpmLogMgr* val)
    {

        // Debug display of raw data
        TRACUCOMP(g_trac_trustedboot, "tpmDumpLog Size : %d",
                  (int)val->logSize);

        // Debug display of raw data
        if (NULL == val->eventLogInMem)
        {
            TRACUBIN(g_trac_trustedboot, "tpmDumpLog",
                     val->eventLog, val->logSize);
        }
        else
        {
            TRACUBIN(g_trac_trustedboot, "tpmDumpLog From Memory",
                     val->eventLogInMem, val->logSize);
        }
    }
#endif

    uint32_t TpmLogMgr_calcLogSize(TpmLogMgr* val)
    {
        uint32_t logSize = 0;
        TCG_PCR_EVENT event;
        TCG_PCR_EVENT2 event2;
        bool errorFound = false;
        const uint8_t* prevLogHandle = NULL;
        const uint8_t* nextLogHandle = NULL;

        TRACUCOMP( g_trac_trustedboot, ">>calcLogSize");

        // Start walking events
        prevLogHandle = TpmLogMgr_getLogStartPtr(val);
        do
        {

            // First need to deal with the header entry
            nextLogHandle = TCG_PCR_EVENT_logUnmarshal(&event,
                                                       prevLogHandle,
                                                       sizeof(TCG_PCR_EVENT),
                                                       &errorFound);

            if (NULL == nextLogHandle || errorFound ||
                EV_NO_ACTION != event.eventType ||
                0 == event.eventSize)
            {
                TRACFCOMP( g_trac_trustedboot, "Header Marshal Failure");
                prevLogHandle = NULL;
                break;
            }

            if (( nextLogHandle - TpmLogMgr_getLogStartPtr(val)) >
                val->logMaxSize)
            {
                TRACFCOMP( g_trac_trustedboot, "calcLogSize overflow");
                prevLogHandle = NULL;
                break;
            }
            prevLogHandle = nextLogHandle;

            // Now iterate through all the other events
            while (NULL != prevLogHandle)
            {
                nextLogHandle = TCG_PCR_EVENT2_logUnmarshal(
                                               &event2,
                                               prevLogHandle,
                                               sizeof(TCG_PCR_EVENT2),
                                               &errorFound);
                if (NULL == nextLogHandle || errorFound)
                {
                    // Failed parsing so we must have hit the end of log
                    break;
                }
                if (( nextLogHandle - TpmLogMgr_getLogStartPtr(val)) >
                    val->logMaxSize)
                {
                    TRACFCOMP( g_trac_trustedboot, "calcLogSize overflow");
                    prevLogHandle = NULL;
                    break;
                }
                prevLogHandle = nextLogHandle;
            }
        }
        while (0);

        if (NULL == prevLogHandle)
        {
            logSize = 0;
        }
        else
        {
            logSize = (prevLogHandle - TpmLogMgr_getLogStartPtr(val));
        }
        TRACUCOMP( g_trac_trustedboot, "<<calcLogSize : %d", logSize);

        return logSize;
    }

    const uint8_t* TpmLogMgr_getFirstEvent(TpmLogMgr* val)
    {
        TCG_PCR_EVENT event;
        bool err = false;
        const uint8_t* result = NULL;

        // Header event in the log is always first, we skip over that
        const uint8_t* firstEvent = TpmLogMgr_getLogStartPtr(val);
        memset(&event, 0, sizeof(TCG_PCR_EVENT));

        firstEvent = TCG_PCR_EVENT_logUnmarshal(&event, firstEvent,
                                                sizeof(TCG_PCR_EVENT),
                                                &err);
        if (NULL != firstEvent && !err &&
            firstEvent < val->newEventPtr)
        {
            result = firstEvent;
        }

        return result;
    }

    const uint8_t* TpmLogMgr_getNextEvent(TpmLogMgr* val,
                                          const uint8_t* i_handle,
                                          TCG_PCR_EVENT2* i_eventLog,
                                          bool* o_err)
    {
        const uint8_t* l_resultPtr = NULL;
        if (NULL == i_handle)
        {
            *o_err = true;
        }
        else
        {
            memset(i_eventLog, 0, sizeof(TCG_PCR_EVENT2));
            TRACUCOMP( g_trac_trustedboot, "TPM getNextEvent 0x%p", i_handle);
            l_resultPtr = TCG_PCR_EVENT2_logUnmarshal(i_eventLog, i_handle,
                                                      sizeof(TCG_PCR_EVENT2),
                                                      o_err);
            if (NULL == l_resultPtr)
            {
                // An error was detected, ensure o_err is set
                *o_err = true;
            }
            else if (l_resultPtr >= val->newEventPtr)
            {
                l_resultPtr = NULL;
            }
        }

        return l_resultPtr;
    }

    TCG_PCR_EVENT2 TpmLogMgr_genLogEventPcrExtend(TPM_Pcr i_pcr,
                                                  TPM_Alg_Id i_algId_1,
                                                  const uint8_t* i_digest_1,
                                                  size_t i_digestSize_1,
                                                  TPM_Alg_Id i_algId_2,
                                                  const uint8_t* i_digest_2,
                                                  size_t i_digestSize_2,
						  uint32_t i_logType,
                                                  const char* i_logMsg)
    {
        TCG_PCR_EVENT2 eventLog;
        size_t fullDigestSize_1 = 0;
        size_t fullDigestSize_2 = 0;

        fullDigestSize_1 = getDigestSize(i_algId_1);
        if (NULL != i_digest_2)
        {
            fullDigestSize_2 = getDigestSize(i_algId_2);
        }

        memset(&eventLog, 0, sizeof(eventLog));
        eventLog.pcrIndex = i_pcr;
        eventLog.eventType = i_logType;

        // Update digest information
        eventLog.digests.count = 1;
        eventLog.digests.digests[0].algorithmId = i_algId_1;
        memcpy(&(eventLog.digests.digests[0].digest),
               i_digest_1,
               (i_digestSize_1 < fullDigestSize_1 ?
                i_digestSize_1 : fullDigestSize_1));

        if (NULL != i_digest_2)
        {
            eventLog.digests.count = 2;
            eventLog.digests.digests[1].algorithmId = i_algId_2;
            memcpy(&(eventLog.digests.digests[1].digest),
                   i_digest_2,
                   (i_digestSize_2 < fullDigestSize_2 ?
                    i_digestSize_2 : fullDigestSize_2));
        }
        // Event field data
        eventLog.event.eventSize = strlen(i_logMsg);
        memset(eventLog.event.event, 0, sizeof(eventLog.event.event));
        memcpy(eventLog.event.event, i_logMsg,
               (strlen(i_logMsg) > MAX_TPM_LOG_MSG ?
                MAX_TPM_LOG_MSG - 1 // Leave room for NULL termination
                : strlen(i_logMsg)) );

        return eventLog;
    }


    uint8_t* TpmLogMgr_getLogStartPtr(TpmLogMgr* val)
    {
#ifdef __HOSTBOOT_MODULE
        return (val->eventLogInMem == NULL ?
           reinterpret_cast<uint8_t*>(&(val->eventLog)) : val->eventLogInMem);
#else
        return val->eventLogInMem;
#endif
    }

#ifdef __HOSTBOOT_MODULE
    errlHndl_t TpmLogMgr_getDevtreeInfo(TpmLogMgr* val,
                                        uint64_t & io_logAddr,
                                        size_t & o_allocationSize,
                                        uint64_t & o_xscomAddr,
                                        uint32_t & o_i2cMasterOffset)
    {
        errlHndl_t err = NULL;

        mutex_lock( &val->logMutex );

        assert(io_logAddr != 0, "Invalid starting log address");
        assert(val->eventLogInMem == NULL,
               "getDevtreeInfo can only be called once");

        io_logAddr -= ALIGN_PAGE(TPMLOG_DEVTREE_SIZE);
        // Align to 64KB for Opal
        io_logAddr = ALIGN_DOWN_X(io_logAddr,64*KILOBYTE);

        val->inMemlogBaseAddr = io_logAddr;
        o_allocationSize = TPMLOG_DEVTREE_SIZE;
        o_xscomAddr = val->devtreeXscomAddr;
        o_i2cMasterOffset = val->devtreeI2cMasterOffset;

        // Copy image.
        val->eventLogInMem = (uint8_t*)(mm_block_map(
                                 (void*)(io_logAddr),
                                 ALIGN_PAGE(TPMLOG_DEVTREE_SIZE)));
        // Copy log into new location
        memset(val->eventLogInMem, 0, TPMLOG_DEVTREE_SIZE);
        memcpy(val->eventLogInMem, val->eventLog, val->logSize);
        val->newEventPtr = val->eventLogInMem + val->logSize;

        mutex_unlock( &val->logMutex );

        TRACUCOMP( g_trac_trustedboot,
                   "<<getDevtreeInfo() Addr:%lX - %s",
                   io_logAddr,
                   ((TB_SUCCESS == err) ? "No Error" : "With Error") );
        return err;
    }


    void TpmLogMgr_setTpmDevtreeInfo(TpmLogMgr* val,
                                     uint64_t i_xscomAddr,
                                     uint32_t i_i2cMasterOffset)
    {
        val->devtreeXscomAddr = i_xscomAddr;
        val->devtreeI2cMasterOffset = i_i2cMasterOffset;
    }

#endif

#ifdef __cplusplus
} // end TRUSTEDBOOT
#endif