@@ -309,6 +309,9 @@ static int FwNvMarshalPublic(byte* buf, word32* pos, word32 maxSz,
309309 XMEMCPY (& pub2b .publicArea , pub , sizeof (TPMT_PUBLIC ));
310310 TPM2_Packet_AppendPublic (& pkt , & pub2b );
311311
312+ if (pkt .pos <= 0 || (word32 )pkt .pos > (maxSz - * pos )) {
313+ return TPM_RC_FAILURE ;
314+ }
312315 * pos += pkt .pos ;
313316 return 0 ;
314317}
@@ -325,6 +328,10 @@ static int FwNvUnmarshalPublic(const byte* buf, word32* pos, word32 maxSz,
325328 pkt .size = (int )(maxSz - * pos );
326329
327330 TPM2_Packet_ParsePublic (& pkt , & pub2b );
331+
332+ if (pkt .pos <= 0 || (word32 )pkt .pos > (maxSz - * pos )) {
333+ return TPM_RC_FAILURE ;
334+ }
328335 XMEMCPY (pub , & pub2b .publicArea , sizeof (TPMT_PUBLIC ));
329336
330337 * pos += pkt .pos ;
@@ -509,7 +516,6 @@ static int FwNvUnmarshalObject(const byte* buf, word32* pos, word32 maxSz,
509516 UINT16 privSz ;
510517
511518 XMEMSET (obj , 0 , sizeof (FWTPM_Object ));
512- obj -> used = 1 ;
513519
514520 rc = FwNvUnmarshalU32 (buf , pos , maxSz , & obj -> handle );
515521 if (rc == 0 ) {
@@ -523,8 +529,10 @@ static int FwNvUnmarshalObject(const byte* buf, word32* pos, word32 maxSz,
523529 }
524530 if (rc == 0 ) {
525531 if (privSz > FWTPM_MAX_PRIVKEY_DER ) {
526- return TPM_RC_FAILURE ;
532+ rc = TPM_RC_FAILURE ;
527533 }
534+ }
535+ if (rc == 0 ) {
528536 obj -> privKeySize = (int )privSz ;
529537 if (privSz > 0 ) {
530538 rc = FwNvUnmarshalBytes (buf , pos , maxSz , obj -> privKey , privSz );
@@ -533,6 +541,9 @@ static int FwNvUnmarshalObject(const byte* buf, word32* pos, word32 maxSz,
533541 if (rc == 0 ) {
534542 rc = FwNvUnmarshalName (buf , pos , maxSz , & obj -> name );
535543 }
544+ if (rc == 0 ) {
545+ obj -> used = 1 ;
546+ }
536547 return rc ;
537548}
538549
@@ -571,7 +582,6 @@ static int FwNvUnmarshalPrimaryCache(const byte* buf, word32* pos,
571582 UINT16 privSz ;
572583
573584 XMEMSET (cache , 0 , sizeof (FWTPM_PrimaryCache ));
574- cache -> used = 1 ;
575585
576586 rc = FwNvUnmarshalU32 (buf , pos , maxSz , & cache -> hierarchy );
577587 if (rc == 0 ) {
@@ -586,13 +596,18 @@ static int FwNvUnmarshalPrimaryCache(const byte* buf, word32* pos,
586596 }
587597 if (rc == 0 ) {
588598 if (privSz > FWTPM_MAX_PRIVKEY_DER ) {
589- return TPM_RC_FAILURE ;
599+ rc = TPM_RC_FAILURE ;
590600 }
601+ }
602+ if (rc == 0 ) {
591603 cache -> privKeySize = (int )privSz ;
592604 if (privSz > 0 ) {
593605 rc = FwNvUnmarshalBytes (buf , pos , maxSz , cache -> privKey , privSz );
594606 }
595607 }
608+ if (rc == 0 ) {
609+ cache -> used = 1 ;
610+ }
596611 return rc ;
597612}
598613
@@ -603,15 +618,15 @@ static int FwNvUnmarshalPrimaryCache(const byte* buf, word32* pos,
603618/* Write NV header at offset 0 */
604619static int FwNvWriteHeader (FWTPM_CTX * ctx )
605620{
606- FWTPM_NV_HEADER hdr ;
621+ byte hdr [ sizeof ( FWTPM_NV_HEADER )]; /* 4 x UINT32 = 16 bytes */
607622 FWTPM_NV_HAL * hal = & ctx -> nvHal ;
608623
609- hdr . magic = FWTPM_NV_MAGIC ;
610- hdr . version = FWTPM_NV_VERSION ;
611- hdr . writePos = ctx -> nvWritePos ;
612- hdr . maxSize = hal -> maxSize ;
624+ FwStoreU32LE ( hdr + 0 , FWTPM_NV_MAGIC ) ;
625+ FwStoreU32LE ( hdr + 4 , FWTPM_NV_VERSION ) ;
626+ FwStoreU32LE ( hdr + 8 , ctx -> nvWritePos ) ;
627+ FwStoreU32LE ( hdr + 12 , hal -> maxSize ) ;
613628
614- return hal -> write (hal -> ctx , 0 , ( const byte * ) & hdr , sizeof (hdr ));
629+ return hal -> write (hal -> ctx , 0 , hdr , sizeof (hdr ));
615630}
616631
617632/* Append a single TLV entry to the journal */
@@ -960,6 +975,7 @@ int FWTPM_NV_Init(FWTPM_CTX* ctx)
960975{
961976 int rc ;
962977 FWTPM_NV_HEADER hdr ;
978+ byte hdrBuf [sizeof (FWTPM_NV_HEADER )];
963979 FWTPM_NV_HAL * hal ;
964980 word32 pos ;
965981 byte tlvHdr [TLV_HDR_SIZE ];
@@ -992,8 +1008,14 @@ int FWTPM_NV_Init(FWTPM_CTX* ctx)
9921008 }
9931009
9941010 /* Try to read existing NV header */
995- XMEMSET (& hdr , 0 , sizeof (hdr ));
996- rc = hal -> read (hal -> ctx , 0 , (byte * )& hdr , sizeof (FWTPM_NV_HEADER ));
1011+ XMEMSET (hdrBuf , 0 , sizeof (hdrBuf ));
1012+ rc = hal -> read (hal -> ctx , 0 , hdrBuf , sizeof (hdrBuf ));
1013+ if (rc == TPM_RC_SUCCESS ) {
1014+ hdr .magic = FwLoadU32LE (hdrBuf + 0 );
1015+ hdr .version = FwLoadU32LE (hdrBuf + 4 );
1016+ hdr .writePos = FwLoadU32LE (hdrBuf + 8 );
1017+ hdr .maxSize = FwLoadU32LE (hdrBuf + 12 );
1018+ }
9971019
9981020 if (rc == TPM_RC_SUCCESS &&
9991021 hdr .magic == FWTPM_NV_MAGIC &&
0 commit comments