Is there a way to call the POSIX mlock
function from Python? The effect of mlock
is to disable swapping out certain objects.
I know there are still other issues when it comes to securing cryptographic keys, I just want to know how to contain them in RAM.
For CPython, there is no good answer for this that doesn't involve writing a Python C extension, since mlock
works on pages, not objects. Even if you used ctypes
to retrieve the necessary addresses and mlock
-ed them all through ctypes
mlock
calls, you'll have a hell of a time determining when to mlock
and when to munlock
. You'd need to know the memory address and sizes of all protected data types; since mlock
works on pages, you'd have to carefully track how many objects are currently in any given page (because if you just mlock
and munlock
blindly, and there are more than one things to lock in a page, the first munlock
would unlock all of them; mlock/munlock
is a boolean flag, it doesn't count the number of locks and unlocks).
Even if you manage that, you still would have a race between data acquisition and mlock
during which the data could be written to swap.
You could partially avoid these problems through careful use of the mmap
module and memoryview
s (mmap
gives you pages of memory, memoryview
references said memory without copying it, so ctypes
could be used to mlock
the page), but you'd have to build it all from scratch.
In short, Python doesn't care about swapping or memory protection in the way you want; it trusts the swap file to be configured to your desired security (e.g. disabled or encrypted), neither providing additional protection nor providing the information you'd need to add it in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With