Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent RAM from paging to swap area (mlock)

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.

like image 662
ArekBulski Avatar asked Apr 08 '15 19:04

ArekBulski


1 Answers

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 memoryviews (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.

like image 72
ShadowRanger Avatar answered Sep 28 '22 08:09

ShadowRanger