0% found this document useful (0 votes)
18 views9 pages

Dynamic Memory Management in C/C++

Uploaded by

prasheelkarkera
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Dynamic Memory Management in C/C++

Uploaded by

prasheelkarkera
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Secure Coding

Dr Adarsh Rag S
Department of CSE
Contact: 8951172344

Secure Coding Dr Adarsh Rag S 1


Dynamic Memory Management
▪ Dynamic memory is essential for C and C++ programs that
handle variable-sized data, especially in non-safety-critical
applications where dynamic storage allocation is widely
used.

▪ However, managing memory is prone to programming


defects, security flaws, and vulnerabilities.

▪ Issues like double freeing of memory can lead to exploitable


vulnerabilities.

▪ Additionally, buffer overflows are dangerous not only when


they affect the stack but also when they occur in the heap.

▪ This module explores dynamic memory management on


Linux and Windows platforms, examines common errors,
Secureand assesses their associated security risks. Dr Adarsh Rag S
Coding 2
Dynamic Memory Management

▪ Heap memory is managed by dynamic memory allocators,


such as Doug Lea’s malloc and Microsoft’s RtlHeap2, both of
which are widely adopted.

▪ When used incorrectly, these memory managers can


become vulnerable to attacks.

▪ While these two examples are highlighted due to their


prevalence, other memory managers can also be exploited
through similar heap-based attacks.

▪ The root cause of these vulnerabilities typically stems from a


small set of undefined behaviors introduced by coding
errors, regardless of the specific memory manager in use.

Secure Coding Dr Adarsh Rag S 3


C Standard Memory Management Functions

▪ The C Standard specifies four memory allocation functions:

▪ malloc(size_t size): Allocates size bytes of memory and


returns a pointer to the allocated space. The memory is not
initialized.

▪ aligned_alloc(size_t alignment, size_t size): Allocates size


bytes aligned to alignment. The behavior is undefined if the
size is not a multiple of the alignment.

▪ realloc(void *p, size_t size): Changes the size of the memory


block pointed to by p. If p is NULL, it behaves like malloc(). If
size is zero, it frees the memory.

Secure Coding Dr Adarsh Rag S 4


C Standard Memory Management Functions

▪ calloc(size_t nmemb, size_t size): Allocates memory for an


array of nmemb elements, each of size bytes, and initializes
the memory to zero.

▪ The free(void *p) function deallocates memory that was


allocated by one of these functions.

▪ Using free() incorrectly, or calling it twice, results in


undefined behavior.

▪ Allocated memory remains valid beyond the function in


which it was created, until explicitly deallocated.

Secure Coding Dr Adarsh Rag S 5


C Standard Memory Management Functions

▪ Alignment Requirements:

▪ Complete object types have specific alignment requirements,


dictating the memory addresses at which they can be
allocated.

▪ The alignment is an implementation-defined integer that


indicates the byte boundaries for each object type (e.g.,
32-bit types must align on 4-byte boundaries).

▪ Types such as char, signed char, and unsigned char have


the weakest alignment, while structs, arrays, and unions
align according to their most restrictive member.

Secure Coding Dr Adarsh Rag S 6


C Standard Memory Management Functions

▪ Complete Objects:A complete object is one that is not a


subobject of another object and can contain subobjects (e.g.,
members or array elements).

▪ Alignment Hierarchy:Alignments range from weaker to


stricter, with stricter alignments having larger [Link]
alignment values are nonnegative integral powers of 2,
including both fundamental alignments and additional
implementation-defined values.

Secure Coding Dr Adarsh Rag S 7


C Standard Memory Management Functions

▪ Dynamic Memory Allocation:The aligned_alloc() function


allows for allocating memory with specific alignment
requirements. If an alignment request exceeds
alignof(max_align_t), it may not be portable.

▪ The introduction of the _Alignas keyword and aligned_alloc()


is aimed at supporting SIMD (Single Instruction, Multiple
Data) computing, which requires strict alignment (e.g., SSE
instructions necessitate 16-byte alignment).

▪ Reallocation Considerations:When using realloc() on a


pointer obtained from aligned_alloc(), the C Standard does
not guarantee that the strict alignment will be maintained.
Therefore, checking the alignment of memory before calling
realloc() is recommended to avoid issues.
Secure Coding Dr Adarsh Rag S 8
Secure Coding Dr Adarsh Rag S 9

You might also like