Skip to content

Conversation

nmav
Copy link

@nmav nmav commented Jul 30, 2024

This includes the C interfaces that exist to overwrite sensitive data from memory such as explicit_bzero. As this is a common mistake not to overwrite the data, I think listing these interfaces has value even if a complete solution that includes security from the memory swap is harder.

#### Minimizing the Time Keys/Decrypted Data Exists

Remember that per least privilege, we want to minimize the time a privilege is active. In cryptography, you often want to minimize the time a private key or password is available, or at least minimize the time that the decrypted data is available. This can be harder that you might think. At the operating system level you can probably lock it into memory with **mlock()** or **VirtualLock()**; this will at least prevent the data from being copied into storage. Ideally, you would erase it from memory after use, though that is often surprisingly difficult. Compilers may turn overwrite code into a no-op, because they detect that nothing reads the overwritten values. Languages with built-in garbage collection often quietly make extra copies and/or do not provide a mechanism for erasure. That said, some languages or infrastructure do make this easy. For example, those using the .NET framework (e.g., C#) can use SecureString.
Remember that per least privilege, we want to minimize the time a privilege is active. In cryptography, you often want to minimize the time a private key or password is available, or at least minimize the time that the decrypted data is available. This can be harder that you might think. At the operating system level you can probably lock it into memory with **mlock()** or **VirtualLock()**; this will at least prevent the data from being copied into storage. Ideally, you would erase it from memory after use using interfaces that are safe for that purpose. The reason is that compilers since they detect that nothing reads the overwritten values optimize the code out as no-op. The following interfaces in C and C++ are safe for overwriting sensitive data in memory.
Copy link
Contributor

Choose a reason for hiding this comment

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

The reason is that compilers since they detect that nothing reads the overwritten values optimize the code out as no-op.

This sentence doesn't make sense. Did you mean:

Compiler optimizers may quietly eliminate the code to overwrite data, because they detect that nothing else in the program reads the overwritten values. In addition, languages with built-in garbage collection often quietly make extra copies and/or do not provide a mechanism for erasure.

Also, change:

safe for overwriting

to:

safer for overwriting

as there are still ways to misuse them, esp. if data regions are declared as volatile. See:

It doesn't mention memset_explicit, but that's new so I think that's okay.

Copy link
Author

@nmav nmav Aug 26, 2024

Choose a reason for hiding this comment

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

I couldn't have expressed it better. Thanks, I've updated the text.

I checked GNU libc manual and there is only explicit_bzero. memset_explicit seems to be available only in gnulib (which despite the name is not the same as the gnu libc).

Copy link
Author

@nmav nmav Aug 26, 2024

Choose a reason for hiding this comment

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

The text In addition, languages with built-in garbage collection often quietly make extra copies and/or do not provide a mechanism for erasure. was listed (in the first version I submitted) at the end, after the C/C++ interfaces. I merged the texts, and kept For example, those using the .NET framework (e.g., C#) can use SecureString..

As a result the C/C++ interfaces are now listed at the end of the paragraph.

@nmav nmav force-pushed the tmp-memcmp branch 2 times, most recently from 1eadd2c to 0f23031 Compare August 26, 2024 14:29
@nmav nmav changed the title Minimizing the time: included existing C interfaces Minimizing the time: include existing C interfaces Sep 19, 2024
This includes the C interfaces that exist to overwrite sensitive data from
memory such as explicit_bzero. As this is a common mistake not to overwrite
the data, I think listing these interfaces has value even if a complete solution
that includes security from the memory swap is harder.

Signed-off-by: Nikos Mavrogiannopoulos <[email protected]>
@david-a-wheeler
Copy link
Contributor

Thanks so much! Merged. Again, sorry for the delay.

@david-a-wheeler david-a-wheeler merged commit de4e51f into ossf:main Dec 10, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants