You are on page 1of 1

Memory Management

Memory management represents a vital part of secure application


development. Proper memory management is like good personal hygiene.
We are physically healthier when we practice proper hygiene. Similarly,
applications perform better when memory use is properly allocated.

Buffer Overflow Zeroing Out Memory


A buffer overflow is simply allocating an array Programming near the assembly layer at a low
of memory onto the call stack—the data level like in C or C++ leaves a lot of hygiene duties
structure where methods and functions are in the hands of programmers rather than the
stored—and then overfilling it with more data underlying system. To “zero out” memory is to
than it was supposed to handle. The extra fill in a region of memory with zeros, ones,
bytes written to memory spill over and overwrite or some pattern to erase what was in there
adjacent memory, usually corrupting other before. It should be obvious that if you have
stack-based variables. In severe cases a buffer memory pointing to a password, bank account
overflow will corrupt the call stack, leading to numbers, or some other confidential information,
a massive crash. Even worse, if an attacker has that memory should be cleared out immediately
access to the source code, they could deduce when finished.
a way to corrupt the call stack just enough to
For instance, if your application crashes, an
change the value of a variable that normal code
external application can collect a dump by
could not reach, such as changing the privileges
sweeping up and copying all the system memory.
of a user to that of an admin.
That memory could then be saved to a file, where
an attacker could have ample time to search it for
Setting Pointers to Null sensitive data.
One of the benefits of programming with C and
Thus, when you are done with sensitive memory,
C++ is their use of pointers (with the attendant
take special care to clean up after yourself!
speed boosts they provide). But pointers can also
create security holes if not properly addressed.
Memory Leaks
To null out a pointer is to assign it a value of
A memory leak occurs when a developer fails
zero or some other compiler-directed value.
to free an allocated block of memory when
This disassociates the pointer with the region of
no longer needed. An application littered with
memory it previously pointed to. When you are
memory leaks will eventually request a memory
done with memory and you have zeroed it out,
chunk and fail, because the address space is
assign zero to your pointer. That is the classic

© 2021 The Security Awareness Company - KnowBe4, Inc. All rights reserved.
indication that the pointer is no longer in use. fragmented into tiny pieces.

The security implications of this process are huge. Dynamic Memory


You could have special password memory that
perhaps you didn’t free or fill in with zeros; if you Allocating memory on the stack is easy to
fail to null out a pointer to it, you leave clues as clean up afterwards, since the compiler does it
to where that memory is (or was) located. That for you. As the stack unwinds, the memory is
would be like leaving a sign posted in your yard automatically freed. Memory allocated on the
telling everyone where the extra key to your front heap is different; it is not automatically freed
door is hidden. and you have to do it manually.

Good memory management not only improves the way your application functions, it also
helps reduce security risks and is an imperative part of secure application development.

You might also like