4 ms·
it's unsafe because it can lead to leakage of confidential information, and has many times in the past, but that's not the most common problem the most common
by kragen 4y ago
it's unsafe because it can lead to leakage of confidential information, and has many times in the past, but that's not the most common problem
the most common problem is that you write some code that dynamically allocates memory, you test it, it works fine, and then you write more code that depends on it, and later on (weeks, months, years) you run into some hard to reproduce bug
after a weekend of debugging it turns out your code only worked fine when you tested it because malloc was allocating freshly mapped pages the kernel had zeroed for you. so you didn't notice that you left some fields uninitialized in your records. ooops. you didn't need that weekend with your kids anyway
for this reason many debug allocators initialize your malloced memory as a form of offensive programming, and valgrind reports it as a bug if your program's behavior depends on uninitialized memory
- swinglock 4y agoThe other unsafe practice was thinking "works fine" testing is sufficient. Instead the tests should always have been executed with Valgrind or similar before delivery, instead of manually on a weekend.
- kragen 4y agotesting is never sufficient
- _0ffh 4y agoArguably, a leakage of information already happens when you deallocate memory without zeroing it first.
- saagarjha 4y agoMake sure your compiler is actually doing that zeroing, as memory that is destined for deallocation does not have many requirements imposed on it.
- pornel 4y agoIn what security model? If you assume there's an adversary that can read all your memory, then you're pretty screwed no matter what you do. In C's and Rust's model deallocated memory is assumed to be completely inaccessible (and Rust does its best to ensure it is inaccessible), so in a valid (non-UB) program there is nothing that could leak its secrets.
- _0ffh 4y agoI thought of a scenario like, your process deallocates a page of memory, so it gets marked as unused by the OS, then some other application requests memory and gets handed your old page, with all the data you left in there. Would that not be leaking information?
- kragen 4y agoon linux the os zeroes the page to prevent this
- butlerm 4y agoOperating systems with support for memory protection and virtual memory generally don't behave that way for security reasons. Everything that is allocated to a process will be zeroed explicitly or on first reference. There are older operating systems that don't do that though. On the Amiga you have to explicitly ask the OS for memory to be cleared, and if you don't you may get uncleared memory left over from some other process.