When developing embedded systems with MCUs at their core, a stack buffer overflow occurs when a software program writes data to the memory address range of the program call stack outside of a preset data structure (usually a fixed-length buffer). This will almost certainly corrupt nearby data or even change the return function. If this is intentional, it is known as stack smashing. One way to protect against stack buffer overflows is to use a stack canary, so named because it is similar to using canaries in coal mines to detect poison gas. Stack protection is now supported in all recent versions of leading development tools, represented by IAR Embedded Workbench.
Stack protection has become a must-have feature in the latest embedded development tools, but to implement stack protection in industry benchmark tools such as IAR Embedded Workbench for Arm, a heuristic algorithm is used to determine whether a function needs stack protection. If any local variable defined in a function is an array type or a structure type containing an array type member, then the function needs stack protection. In addition, if the address of any local variable is propagated outside the function, then the function also needs stack protection.
If a function needs stack protection, then the local variables of the function are arranged in order, placing the variables of array type at the highest possible address in the function stack. After these variables, a canary element is placed. At the function entry, the canary is initialized. The initialization value is taken from the global variable __stack_chk_guard. At function exit, the code verifies whether the canary element still contains the initialization value. If the value is changed, the function __stack_chk_fail is called.
Taking the widely used IAR Embedded Workbench for Arm embedded development tool as an example, use the Project>Options>C/C++ Compiler>Code>Stack protection option to enable stack protection for functions identified as requiring protection.
Alternatively, you can use the Project>Options>C/C++ Compiler>Extra Options page to enable stack protection by specifying the --stack_protection command line option.
Implementing stack protection in practical applications
To use stack protection, developers must define the following objects in their application:
• extern uint32_t __stack_chk_guard
The global variable __stack_chk_guard must be initialized before the first use. If the initialization value is random, it will be safer.
• __interwork __nounwind __noreturn void __stack_chk_fail(void)
The purpose of the __stack_chk_fail function is to notify that an error has occurred and then terminate the application. Please note that the return address of this function will point to the failure function.
The file stack_protection.c in the armsrclibruntime directory provides reference templates for the __stack_chk_guard and __stack_chk_fail functions.
Summarize
As the tight global semiconductor supply chain has not been alleviated, many embedded applications such as MCUs need to use development tools to maintain flexibility in the supply of core technologies and devices and maximize the reuse of completed software on different hardware platforms. In this case, both MCU chip developers and embedded system engineers need to use the most widely used development tools in the industry, such as IAR Embedded Workbench for Arm. Since these tools are also the result of years of cooperation between their developers and industry-leading MCU suppliers, they can provide corresponding assistance for different hardware resource systems and application environments, such as the stack protection function in IAR Embedded Workbench, so that embedded developers can achieve their R&D goals with a shorter R&D cycle.
Previous article:Customer experience will determine success in 2022, and low-code is the quick way to win
Next article:A real-world example of using edge AI for wildfire warnings
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Semantic Segmentation for Autonomous Driving: Model Evaluation, Dataset Generation, Viewpoint Comparison, and Real-time Performance
- Machine Learning and Embedded Computing in Advanced Driver Assistance Systems (ADAS)
- Foundations of ARM64 Linux Debugging, Disassembling, and Reversing Analyze Code, Understand Stack Me
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University----[High Precision Laboratory] Interface: RS-485
- Newbie, please help me with CPLD frequency multiplication
- [The third stop of Shijian’s ADI journey] Learn about cutting-edge IoT devices and solutions, and win Kindle and other gifts
- Are there any netizens working in the field of motors?
- Evaluation report summary: Mir MYS-8MMX
- 2020 Share the beauty of Qingdao in my eyes
- Zigbee Technology Exchange
- Allwinner V853+XR829 Tina wireless network wifimanger2.0 user guide
- Confusion about TFT LCD interface
- What is the function of TI DSP GEL file?