Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Hygiene/use mbedtls error strings in logs #1875

Merged
merged 31 commits into from
Apr 9, 2020

Conversation

aggarw13
Copy link
Contributor

@aggarw13 aggarw13 commented Apr 3, 2020

Stringify mbed TLS error codes in logs (to improve debugging experience)

Description

  • Create two utilities, mbedtls_strerror_highlevel and mbedtls_strerror_lowlevel, to support stringification of mbed TLS (high-level and low-level) codes with constant strings. (Existing mbedtls_sterror involves population of buffer string at runtime which is memory intensive)
  • Switch all existing log sites of mbed TLS error code to use the new utilities

Checklist:

  • I have tested my changes. No regression in existing tests.
  • My code is Linted.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@aggarw13 aggarw13 requested review from lundinc2 and gkwicker April 3, 2020 23:43
@@ -0,0 +1,58 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also does this custom file belong in the third party folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will move it to the 3rdparty/mbedtls_utlils folder once #1881 is merged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have relocated files to mbedtls_utils

@aggarw13 aggarw13 requested a review from lundinc2 April 7, 2020 23:58
const char * mbedtls_strerror_highlevel( int errnum )
{
const char * rc = NULL;
int use_ret;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should initial this to 0. Notably the Espressif project will consider this as an error. (For the make project at least)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed offline that this will follow the mbedtls coding style with the goal of eventually pushing it to mbedtls repository

const char * mbedtls_strerror_lowlevel( int errnum )
{
const char * rc = NULL;
int use_ret;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about initialization

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as this reply


if( use_ret == 0 )
{
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single point of return as per the coding standard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as this reply

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this will be contributed to a third-party dependency so needs to be in that style.

@@ -3472,7 +3476,9 @@ CK_DECLARE_FUNCTION( CK_RV, C_Sign )( CK_SESSION_HANDLE xSession,

if( lMbedTLSResult != CKR_OK )
{
PKCS11_PRINT( ( "mbedTLS sign failed with error %d \r\n", lMbedTLSResult ) );
PKCS11_PRINT( ( "mbedTLS sign failed with error %s : %s \r\n",
mbedtls_strerror_highlevel( lMbedTLSResult ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It the function returns NULL this is technically UB, but most implementations will just print (NULL).

I did quick test on the Xtensa compiler and this hardfaults if NULL is returned.

Copy link
Contributor Author

@aggarw13 aggarw13 Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. To address this concern, we could return a default value like "<low-level-code-not-included>"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of adding file-specific constants and macros to log level-codes only if non-null.

static const char * pNoHighLevelMbedTlsCodeStr = "<No-High-Level-Code>";
static const char * pNoLowLevelMbedTlsCodeStr = "<No-Low-Level-Code>";

#define mbedTlsHighLevelCodeOrDefault( errnum )                             \
    {                                                                      \
        ( mbedtls_strerror_highlevel( errnum ) != NULL ) ?                  \
        mbedtls_strerror_highlevel( errnum ) : pNoHighLevelMbedTlsCodeStr ); \
    }

#define mbedTlsLowLevelCodeOrDefault( errnum )                             \
    {                                                                      \
        ( mbedtls_strerror_lowlevel( errnum ) != NULL ) ?                  \
        mbedtls_strerror_lowlevel( errnum ) : pNoLowLevelMbedTlsCodeStr ); \
    }

@aggarw13 aggarw13 requested a review from lundinc2 April 8, 2020 23:52
@@ -65,6 +65,35 @@
#include <stdio.h>
#include <string.h>

/**
* @brief Represents string to be loggedwhen mbed TLS returned error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: logged when typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see it fixed. Also, I see the same at multiple places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please end comment with period.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I forgot to push the change. I will make the change in a follow-up PR.

lundinc2
lundinc2 previously approved these changes Apr 8, 2020
Copy link
Contributor

@gkwicker gkwicker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

110% awesome


if( use_ret == 0 )
{
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this will be contributed to a third-party dependency so needs to be in that style.

@@ -65,6 +65,35 @@
#include <stdio.h>
#include <string.h>

/**
* @brief Represents string to be loggedwhen mbed TLS returned error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see it fixed. Also, I see the same at multiple places.

@@ -65,6 +65,35 @@
#include <stdio.h>
#include <string.h>

/**
* @brief Represents string to be loggedwhen mbed TLS returned error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please end comment with period.

/* High level error codes */
/* */
/* BEGIN generated code */
#if defined( MBEDTLS_CIPHER_C )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of switch would result in a faster code, right? (The same was suggested by ARM here: Mbed-TLS/mbedtls#3176).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look into tackling this in a separate PR to avoid scope-creep.

@aggarw13 aggarw13 merged commit 9db26b8 into master Apr 9, 2020
@aggarw13 aggarw13 deleted the hygiene/use-mbedtls-error-strings-in-logs branch April 9, 2020 19:27
@aggarw13 aggarw13 mentioned this pull request Apr 9, 2020
2 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants