buglwIP - A Lightweight TCP/IP stack - Bugs: bug #64299, Wrongly identified mbedTLS memory...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #64299: Wrongly identified mbedTLS memory configuration

Submitter:  Hamza Hajeir <hamzahajeir>
Submitted:  Mon 12 Jun 2023 08:14:59 PM UTC
   
 
Category:  apps Severity:  3 - Normal
Item Group:  Change Request Status:  Duplicate
Privacy:  Public Assigned to:  goldsimon
Open/Closed:  Closed Planned Release:  None
lwIP version:  2.1.2

Wed 11 Oct 2023 07:10:01 PM UTC, comment #3: 

Oh, ok, I'll close this as duplicate then.

--> see patch #10368

Simon Goldschmidt <goldsimon>
Group administrator
Tue 10 Oct 2023 09:58:12 PM UTC, comment #2: 

I'm aware of that patch and have submitted one over it. That patch missed the STD- macro being defined.

It's the patch 0010 within these ones:
https://savannah.nongnu.org/patch/?10368

For a shorter description, mbedtls provides 2 options to define an overriding calloc/free: Platform standard (AABB_STD_XX), and User (AABB_XX_MACRO).

  • AABB: MBEDTLS_PLATFORM
  • XX: CALLOC/FREE


Currently ALTCP_TLS_MBEDTLS checks against the user's one only. The patch adds the standard's macro to the check.

Hamza Hajeir <hamzahajeir>
Tue 10 Oct 2023 06:51:09 PM UTC, comment #1: 

Could you please test 2.2.0 or git master if this is already fixed? The master branch has had a fix from 2021 that changed the #if code you're suggesting to change, I think this is a duplicate.

Simon Goldschmidt <goldsimon>
Group administrator
Mon 12 Jun 2023 08:14:59 PM UTC, original submission:  

Hi there

Thanks for the great and solid library!

I've had an issue [1] that forced me to debug deeply last couple of days.

I'm using ALTCP_TLS_MBEDTLS on ESP32, have failed to get a pcb out of calling altcp_tls_new(), that results in mbedtls fails to allocate memory even if very large memory was available, printing to the log:


> IDF/components/mbedtls/mbedtls/library/ssl_tls.c:3857: alloc(16717 bytes) failed
> mbedtls_ssl_setup failed



It starts by the user call [altcp_tls_create_config_server], which creates tls config first, which in its turn initializes memory by [altcp_mbedtls_mem_init], which overrides mbedtls calloc/free [2].

In which its implementation [tls_malloc] compares against LWIP defined macro (MEM_SIZE), which is defaulted to 1600.

Yes, correct that the overriding is protected by the preprocessor (ALTCP_MBEDTLS_PLATFORM_ALLOC) but it's wrongly defined regarding mbedtls configuration.

MbedTLS has three states of memory configuration:

- normal `calloc()`/`free()`, which can be easily overriden when `MBEDTLS_PLATFORM_MEMORY` is defined.

- Platform standard calloc/free, which is the platform's standard calls (MBEDTLS_PLATFORM_STD_{CALLOC/FREE}), (in ESP32 are mapped to its own `heap_caps_calloc()`/`heap_caps_free()` (header [3]) (source [4]))

- Platform-defined macros `MBEDTLS_PLATFORM_{CALLOC,FREE}_MACRO`.

Wherein LwIP relies in defining ALTCP_MBEDTLS_PLATFORM_ALLOC by Only the non-standard platform macros, while it 'should' also consider the standard definition macros (MBEDTLS_PLATFORM_STD_CALLOC).

So what's proposed is to add a check to the preprocessor at (altcp_tls/altcp_tls_mbedtls_mem.c) to become:


#if defined(MBEDTLS_PLATFORM_MEMORY) && \
     !defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
    !defined(MBEDTLS_PLATFORM_STD_CALLOC)


Keep in mind that mbedTLS checks for misconfiguration (missing the correlated free/calloc or mixing std-defined with platform-defined) [5]:


[1] https://github.com/espressif/esp-idf/issues/11647
[2] https://github.com/lwip-tcpip/lwip/blob/e29870c15e8bf28eac9c811dd236c474f3f2008f/src/apps/altcp_tls/altcp_tls_mbedtls_mem.c#LL167C3-L167C35
[3] https://github.com/espressif/esp-idf/blob/master/components/mbedtls/port/include/mbedtls/esp_config.h#L125-L127
[4] https://github.com/espressif/esp-idf/blob/master/components/mbedtls/port/esp_mem.c#L14-L40
[5] https://github.com/espressif/mbedtls/blob/15b55d406db3918bac88aaf5ef2c6e036d1e0f0e/include/mbedtls/check_config.h#L470-L496

Hamza Hajeir <hamzahajeir>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by goldsimon (Posted a comment)
  • -email is unavailable- added by hamzahajeir (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-10-11 goldsimon StatusNeed Info Duplicate
        Open/ClosedOpen Closed
    2023-10-10 goldsimon StatusNone Need Info
        Assigned toNone goldsimon

    Back to the top

    Powered by Savane 3.13-bb6a.
    Corresponding source code