Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of user defined malloc, realloc and free functions #11

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

MiSimon
Copy link

@MiSimon MiSimon commented Mar 14, 2024

This PR adds the following functions:

  • TSS_Free, wrapper for free or similar function
  • TSS_SetMemoryFunctions, allow the usage of platform specific malloc, realloc and free functions. Must be called before any other TSS function. If not called malloc, realloc and free will be used.

Replaced all calls to free with calls to TSS_Free.

MiSimon added 2 commits March 8, 2024 08:58
- TSS_Free, wrapper for free or similar function
- TSS_SetMemoryFunctions, allow the usage of platform specific malloc, realloc and free functions. Must be called before any other TSS function

Replaced all calls to free with calls to TSS_free
@kgoldman
Copy link
Owner

kgoldman commented Mar 29, 2024

Small comment: It would be nice to explain 'why' this is needed in the pull request. I can add that to the documentation.

Question 1: ifdef for Windows and Posix were added to the three calls. Why is this needed, since the code is the same in both cases?

Question 2: I suspect that calling TSS_SetMemoryFunctions() after a malloc and before a free would be unpredictable. Is there a use case for setting it more than once?

If not, perhaps the function should check for 'already set' and return an error.

If there is a use case for setting it more than once, then perhaps 'all three parameters NULL' should be legal, a way to set back to the default.

Another comment: There's no test case. I'd rather not upstream untested code.

@kgoldman kgoldman self-assigned this Mar 29, 2024
@kgoldman kgoldman added the enhancement New feature or request label Mar 29, 2024
@MiSimon
Copy link
Author

MiSimon commented Apr 2, 2024

I used the library in UEFI applications and there is no standard library (no malloc, realloc, free,...), but there are direct replacements for most of the standard functions (strlen, strcpy, memcpy,...). I solved this by using defines for most of the standard functions and provided function pointers for malloc, realloc and free.

I created this PR because others may also use the library in environments where the standard library is not available.

Answer 1: You are right that does not make sense, I am working on another PR and this was part of it.

Answer 2: You are correct if you change TSS_SetMemoryFunctions calls to free with pointers that were allocated using the previous malloc are undefined behaviour. I don't think that there is a use case for setting it more then once. I will add a check to ensure it can only be set once.

Answer test case: I used the test scripts but they are not ideal for this kind of PR. Is there already a test collection for such "internal" changes?

@MiSimon MiSimon marked this pull request as draft July 22, 2024 06:20
@kgoldman
Copy link
Owner

kgoldman commented Aug 7, 2024

This patch did not apply either to the current head or on top of your previous patch. I think you have to 'rebase' to head, but I'm not a git expert.

I though I suggested this once before: E.g., (applies to all three) If tssMalloc is statically initialized to malloc, then the if statement within TSS_Malloc is unnecessary. I think the tssAllowMemoryCustomize flag is also unnecessary - if tssMalloc != malloc, it's been set.

It also needs a regression test. I wonder if using the openssl OPENSSL_malloc() function would work.

@MiSimon
Copy link
Author

MiSimon commented Aug 22, 2024

It seems i missed your suggestion with initializing the functions to their stdlib counterpart.

Using OPENSSL_malloc, OPENSSL_realloc and OPENSSL_free should work, maybe this would be a much simpler solution:

  • Use malloc, realloc and free if TPM_TSS_NOCRYPTO is defined.
  • Use OPENSSL_malloc, OPENSSL_realloc and OPENSSL_free if TPM_TSS_NOCRYPTO is not defined.

I don't know if any special regression tests would be required if this solution is used. The existing tests should still work and if the user calls CRYPTO_set_mem_functions it is his responsibility to do this before calling any other crypto function (including ibmtss).

The only disadvantage is that there is no way to change the memory functions if ibmtss is dynamically linked. If this should be possible TSS_SetMemoryFunctions (as wrapper for CRYPTO_set_mem_functions) would still be necessary and with it some new regression tests.

@kgoldman
Copy link
Owner

I think you misread my suggestion. I was suggesting the openssl commands for the regression test, not for the implementation.

Openssl cannot be used for the implementation because the TSS can use other crypto libraries. I have an implementation using mbedtls.

@MiSimon
Copy link
Author

MiSimon commented Oct 11, 2024

I misread that indeed. I did not found a test for CRYPTO_set_mem_functions in the openssl repository.

I suggest to create a new "util" (maybe "testapp", because "writeapp" is also no wrapper for a command) that bundles all internal tests that have no effect on the TPM. This util is then called from a new test script.

Do you think that simple tests (malloc + free, realloc + free and malloc + realloc + free) with and without a prior call to TSS_SetMemoryFunctions would be enough?

@kgoldman
Copy link
Owner

kgoldman commented Dec 6, 2024

FYI: The openssl alloc functions are supported: https://docs.openssl.org/master/man3/OPENSSL_malloc/#synopsis. That's potentially a good design pattern for your implementation.

I don't think a test that a test that just mallocs and frees is sufficient. I think the memory should be used, and that realloc works.

Where openssl is useful is that you can set the memory functions to use openssl. E.g., set the malloc function to OPENSSL_malloc. That's easier than implementing some custom malloc function just for regression testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants