Skip to content

Commit c7a9a01

Browse files
authored
Improve heap2 bounds checking (#224)
* Improve heap bounds checking in pvPortMalloc
1 parent b5020cb commit c7a9a01

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

portable/MemMang/heap_1.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* https://www.FreeRTOS.org
2323
* https://github.com/FreeRTOS
2424
*
25-
* 1 tab == 4 spaces!
2625
*/
2726

2827

@@ -72,13 +71,20 @@ void * pvPortMalloc( size_t xWantedSize )
7271
void * pvReturn = NULL;
7372
static uint8_t * pucAlignedHeap = NULL;
7473

75-
/* Ensure that blocks are always aligned to the required number of bytes. */
74+
/* Ensure that blocks are always aligned. */
7675
#if ( portBYTE_ALIGNMENT != 1 )
7776
{
7877
if( xWantedSize & portBYTE_ALIGNMENT_MASK )
7978
{
80-
/* Byte alignment required. */
81-
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
79+
/* Byte alignment required. Check for overflow. */
80+
if ( (xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) )) > xWantedSize )
81+
{
82+
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
83+
}
84+
else
85+
{
86+
xWantedSize = 0;
87+
}
8288
}
8389
}
8490
#endif
@@ -91,8 +97,9 @@ void * pvPortMalloc( size_t xWantedSize )
9197
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
9298
}
9399

94-
/* Check there is enough room left for the allocation. */
95-
if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
100+
/* Check there is enough room left for the allocation and. */
101+
if( ( xWantedSize > 0 ) && /* valid size */
102+
( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
96103
( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
97104
{
98105
/* Return the next free byte then increment the index past this

portable/MemMang/heap_2.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* https://www.FreeRTOS.org
2323
* https://github.com/FreeRTOS
2424
*
25-
* 1 tab == 4 spaces!
2625
*/
2726

2827
/*
@@ -132,21 +131,32 @@ void * pvPortMalloc( size_t xWantedSize )
132131
xHeapHasBeenInitialised = pdTRUE;
133132
}
134133

135-
/* The wanted size is increased so it can contain a BlockLink_t
134+
/* The wanted size must be increased so it can contain a BlockLink_t
136135
* structure in addition to the requested amount of bytes. */
137-
if( xWantedSize > 0 )
136+
if( ( xWantedSize > 0 ) &&
137+
( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check */
138138
{
139139
xWantedSize += heapSTRUCT_SIZE;
140140

141-
/* Ensure that blocks are always aligned to the required number of bytes. */
142-
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
141+
/* Byte alignment required. Check for overflow. */
142+
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
143+
> xWantedSize )
143144
{
144-
/* Byte alignment required. */
145145
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
146+
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
146147
}
148+
else
149+
{
150+
xWantedSize = 0;
151+
}
152+
}
153+
else
154+
{
155+
xWantedSize = 0;
147156
}
148157

149-
if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
158+
159+
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
150160
{
151161
/* Blocks are stored in byte order - traverse the list from the start
152162
* (smallest) block until one of adequate size is found. */

portable/MemMang/heap_4.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,34 +136,42 @@ void * pvPortMalloc( size_t xWantedSize )
136136
* kernel, so it must be free. */
137137
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
138138
{
139-
/* The wanted size is increased so it can contain a BlockLink_t
139+
/* The wanted size must be increased so it can contain a BlockLink_t
140140
* structure in addition to the requested amount of bytes. */
141-
if( xWantedSize > 0 )
141+
if( ( xWantedSize > 0 ) &&
142+
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
142143
{
143144
xWantedSize += xHeapStructSize;
144145

145-
/* Ensure that blocks are always aligned to the required number
146-
* of bytes. */
146+
/* Ensure that blocks are always aligned. */
147147
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
148148
{
149-
/* Byte alignment required. */
150-
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
151-
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
149+
/* Byte alignment required. Check for overflow. */
150+
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
151+
> xWantedSize )
152+
{
153+
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
154+
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
155+
}
156+
else
157+
{
158+
xWantedSize = 0;
159+
}
152160
}
153161
else
154162
{
155163
mtCOVERAGE_TEST_MARKER();
156164
}
157-
}
158-
else
165+
}
166+
else
159167
{
160-
mtCOVERAGE_TEST_MARKER();
168+
xWantedSize = 0;
161169
}
162170

163171
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
164172
{
165173
/* Traverse the list from the start (lowest address) block until
166-
* one of adequate size is found. */
174+
* one of adequate size is found. */
167175
pxPreviousBlock = &xStart;
168176
pxBlock = xStart.pxNextFreeBlock;
169177

@@ -174,7 +182,7 @@ void * pvPortMalloc( size_t xWantedSize )
174182
}
175183

176184
/* If the end marker was reached then a block of adequate size
177-
* was not found. */
185+
* was not found. */
178186
if( pxBlock != pxEnd )
179187
{
180188
/* Return the memory space pointed to - jumping over the

portable/MemMang/heap_5.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* https://www.FreeRTOS.org
2323
* https://github.com/FreeRTOS
2424
*
25-
* 1 tab == 4 spaces!
2625
*/
2726

2827
/*
@@ -150,16 +149,24 @@ void * pvPortMalloc( size_t xWantedSize )
150149
{
151150
/* The wanted size is increased so it can contain a BlockLink_t
152151
* structure in addition to the requested amount of bytes. */
153-
if( xWantedSize > 0 )
152+
if( ( xWantedSize > 0 ) &&
153+
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
154154
{
155155
xWantedSize += xHeapStructSize;
156156

157-
/* Ensure that blocks are always aligned to the required number
158-
* of bytes. */
157+
/* Ensure that blocks are always aligned */
159158
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
160159
{
161-
/* Byte alignment required. */
162-
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
160+
/* Byte alignment required. Check for overflow */
161+
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) >
162+
xWantedSize )
163+
{
164+
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
165+
}
166+
else
167+
{
168+
xWantedSize = 0;
169+
}
163170
}
164171
else
165172
{
@@ -168,13 +175,13 @@ void * pvPortMalloc( size_t xWantedSize )
168175
}
169176
else
170177
{
171-
mtCOVERAGE_TEST_MARKER();
178+
xWantedSize = 0;
172179
}
173180

174181
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
175182
{
176183
/* Traverse the list from the start (lowest address) block until
177-
* one of adequate size is found. */
184+
* one of adequate size is found. */
178185
pxPreviousBlock = &xStart;
179186
pxBlock = xStart.pxNextFreeBlock;
180187

@@ -185,7 +192,7 @@ void * pvPortMalloc( size_t xWantedSize )
185192
}
186193

187194
/* If the end marker was reached then a block of adequate size
188-
* was not found. */
195+
* was not found. */
189196
if( pxBlock != pxEnd )
190197
{
191198
/* Return the memory space pointed to - jumping over the

0 commit comments

Comments
 (0)