Stack Protection Example
This application demonstrates various stack overflow and underflow protection methods for embedded systems using the NBRTOS real-time operating system.
Overview
The Stack Protection Example is designed to showcase how stack protection mechanisms work by intentionally triggering stack overflow conditions in a controlled manner. This educational tool helps developers understand stack protection features and their importance in embedded system development.
Features
- Buffer Overflow Detection: Demonstrates overflow caused by writing past buffer boundaries
- Call Stack Overflow Detection: Shows overflow through infinite recursion
- Cross-Platform Support: Works on both ARM Cortex-M7 and Coldfire processors
- Real-time Stack Monitoring: Provides live stack usage information
- Interactive Menu System: User-selectable overflow scenarios
Architecture Support
ARM Cortex-M7
- Supports monitoring of both stack overflow AND underflow simultaneously
- Can monitor 2 memory ranges concurrently
- Full stack protection capabilities enabled
Coldfire
- Limited to monitoring a single memory range
- Must choose between overflow OR underflow protection (not both)
- Typically configured for overflow protection
Prerequisites
Required Configuration
To enable full functionality, the following preprocessor definitions must be set:
#define NBRTOS_STACKCHECK (1)
#define NBRTOS_STACKOVERFLOW (1)
#define NBRTOS_STACKUNDERFLOW (1)
Note**: For Coldfire processors, only enable either NBRTOS_STACKOVERFLOW or NBRTOS_STACKUNDERFLOW, not both.
Compiler Settings
The application requires the -fstack-check compiler flag to be enabled:
- NBEclipse IDE: Right-click project > Properties > C/C++ Build > Settings > GNU C++ Compiler > Debugging > Enable "Enable stack checking (-fstack-check)"
- Command Line: The flag is automatically included in the provided makefile
File Configuration
The configuration is handled through the predef-overload.h file, which automatically sets appropriate definitions based on the target processor:
- Coldfire: Enables only stack overflow protection
- Cortex-M7: Enables both overflow and underflow protection
Application Structure
Main Components
- bufferOverflow(): Creates a buffer overflow by writing beyond array boundaries
- callStackOverflow(): Triggers overflow through recursive function calls with multiple parameters
- UserMain(): Main application loop with interactive menu system
Menu Options
The application presents an interactive menu with the following options:
- Buffer Underflow - Demonstrates buffer boundary violations
- Call Stack Overflow - Shows recursive call stack exhaustion
Usage
- Build and Deploy: Compile the application with the provided makefile or IDE project
- Run Application: Execute on target hardware
- Monitor Output: Observe stack range information displayed at startup
- Select Test: Choose overflow scenario from the interactive menu
- Trigger Event: Press any key when prompted to initiate the overflow condition
- Observe Results: Watch as the stack protection mechanisms detect and respond to violations
Expected Output
Upon startup, the application displays:
- Current stack space range (top and bottom addresses)
- Available protection features status
- Interactive menu for selecting overflow scenarios
- Real-time feedback during overflow events
Safety Features
- Stack Range Monitoring: Displays current stack boundaries
- Controlled Overflow: Intentional violations for educational purposes
- System Diagnostics: Built-in diagnostic capabilities
- Protection Mechanisms: Hardware-assisted stack monitoring
Development Notes
Stack Monitoring
The application uses OSDumpTCBStacks() to display current stack information and EnableOSStackProtector() to activate protection mechanisms.
Memory Layout
Stack boundaries are defined by:
OSTCBStkTop: Top of stack address
OSTCBStkBot: Bottom of stack address
Time Delays
Strategic delays (OSTimeDly(1)) are inserted to allow observation of the overflow progression.
Important Warnings
This application is designed for educational and testing purposes only. It intentionally creates stack overflow conditions that would be catastrophic in production code. Do not use these techniques in production systems.
Build Instructions
Command Line
IDE
Import the project into NBEclipse and build using the standard build process to rebuild libraries.
Troubleshooting
If stack protection features don't work as expected:
- Verify preprocessor definitions are correctly set
- Ensure
-fstack-check compiler flag is enabled
- Confirm target processor type matches configuration
- Check that NBRTOS stack protection is supported on your platform