What is the preferred way in Rust to say "If at all possible, please do not allow this value/object/whatever to be paged out of RAM?"
What is the preferred way in Rust to say "If at all possible, please do not allow this value/object/whatever to be paged out of RAM?"
For instance see Raymond Chen's explanation here
> Is VirtualLock safe to use for cryptographic purposes – that is, to avoid sensitive memory being written to disk?
> ...
> [There does not appear to be any guarantee that the memory won't be written to disk while locked. As you noted, the machine may be hibernated, or it may be running in a VM that gets snapshotted. -Raymond]
VirtualLock and the equivalents only guarantee that the canonical copy of that page stays in RAM, not that other copies won't make their way out to disk.
Edit: add link https://blogs.msdn.microsoft.com/oldnewthing/20140207-00/?p=...
As mlock / VirtualProtect are OS dependent I strongly suspect this is not part of Rust itself. Additionally, I suspect there would be some design problems for example putting this protected thing on the stack (protect the whole stack? Or just parts? Bookkeeping wether a stack lock can be released after a function exits?). How do you move values into / out of such an object?
So I don't think this exists, but I've been pleasantly surprised by Rust before.
My general solution for this case is to either mlockall the whole process if it is small / short-living or to have no swap in the machine, for example long running crypto servers. Who runs servers with swap anyways?
See mlock(2) for details [1].
> Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process can lock and the RLIMIT_MEMLOCK soft resource limit instead defines a limit on how much memory an unprivileged process may lock.
In rust, there's the zeroize crate [1], which uses compiler fences and volitile memory to have a shot at it, but it's still not a sure thing.
That library doesn't help with the paging issue you mention, but what you can do for that is entirely platform dependent (up to the kernel's paging mechanism), and so rust isn't going to make it any easier or harder than just making the libc calls/syscalls yourself.
[0]: http://www.daemonology.net/blog/2014-09-06-zeroing-buffers-i...
[1]:https://github.com/iqlusioninc/crates/tree/zeroize/v0.5.2/ze...
If you write in, say, assembler, you will have precise control in zeroing the buffers and not spilling sensitive register values to the stack, and also zero out XMM registers.
In fact that the very article you linked to outlined a solution in the form of a proposed language extension disagrees with your characterization that this problem cannot be solved in any languages. Clearly, the author thinks that it's still possible to zero buffers correctly in a language, but just that C is not such a language unless it gains his proposed extension.
Not really, because micro-coded CPUs have won and you don't have any control over what they do, and as last year has proven there are ways to exploit execution timing in micro-code.
It has to be solved at all levels; each level gets a chance to sabotage it, and will.
With the precondition you're running on bare metal (no virtualization), I think you'd need a kernel mode driver for that to cover all scenarios, including system hibernation and suspend.
You can reliably zero out the secret data before hibernation or even suspend by implementing power management calls – mlock or VirtualProtect aren't going to protect you from that leak.
But PBT_APMSUSPEND is not necessarily. There's a 2 second timeout before Windows suspends or hibernates regardless. It's possible for the event handling thread not to run until after the timeout has passed, that is, after the system has returned from suspend or hibernate.
That's important considering the adversary might induce this kind of scenario to steal the secret.
Kernel mode driver does not have that limitation, because it can't be pre-empted.
As for where it comes up, it's usually because you're handing ownership of the memory to something outside of Rust's normal memory management. In most cases you'll be using a higher-level API like Box::into_raw(), but if you look at the implementation of that you'll see it uses mem::forget() internally.
For instance if you're running in a VM, all bets are off, beacuse the host can and will page you out guest phys memory.
As you note, you can't just mlock memory handed out by the ordinary allocator, as you never know when it will be free to munlock.