Search code examples
securitydynamic-memory-allocation

Data security during dynamic memory allocation


Several minutes ago, I and my friends solved some algorithmic problems on the leetcode.com and share our solutions. We used high level languages and when new memory allocated by Array.new(128) in Ruby or int[] map = new int[128]; in Java it already filled by zero-like values nil or 0 respectively.

So it's guarantied that high level program have cleared place.

And here I have a question: In C or Assembler program could it happens that new chunk of memory stores data from other process unchanged?

And thus one process get data of another process. And even may be data from another user that worked in system some time ago. Could it be a way information leaked?

Do OS clear a memory before sharing it among processes? and If so is it very expensive to run so many iterations?

Thank you.

UPD: http://www.cplusplus.com/articles/ETqpX9L8/ looks like it need to clear valuable data in "lower-level" languages manually to prevent data leaks to other processes.


Solution

  • Yes, in lower-level languages where memory is not initialized, it could contain valuable stuff from other processes. There have been encryption key leakage attacks done this way by continually allocating memory and scanning it for what looks like useful information.

    Security sensitive programs that store passwords or crypto keys, etc should always clear the memory ASAP after use. It's not only to prevent leaks through re-allocated memory, but there are also other attack vectors like RAM dumps that could be used to extract secrets. Always zero or randomize your memory when you are done with it.