Basics of Design • CC Blog • Research & Design Hub

From Buffer Overflows to Control Flow Attacks

Written by Colin O'Flynn

This article extends the January 2026 article by showing a buffer overflow attack that changes a return value. I also discuss the use of the GCC stack protection feature, and demonstrate that it’s not always a bulletproof protection method, even if it is better than doing nothing.


  • What is a buffer overflow attack in embedded systems?
  • How does GCC stack protection work?
  • Why can stack canaries fail to stop certain attacks?
  • How can a buffer overflow overwrite a return address?
  • What lessons do embedded developers learn from real attack demonstrations?
  • STM32F3 Nucleo
  • Raspberry Pi Pico
  • GCC
  • Segger Ozone
  • J-Link OB Firmware
  • Python
  • UART Serial Communication
  • ARM Cortex-M MCU
  • Stack Protector
  • Buffer Overflow Exploitation
  • Segger | segger.com
  • STMicroelectronics | st.com
  • NewAE Technology | newae.com

In my previous article, I demonstrated how a simple buffer overflow attack could be used to bypass a password check. The attack in my Circuit Cellar #426 article worked because you were able to overwrite a “secret” password, meaning you had control over what the password comparison checked against.

But when discussing buffer overflows, the more common attack would just be to overwrite the stack itself. In fact, most compilers even have an option to enable a “stack protector” specifically to prevent this type of attack.

The previous article introduced the setup for a buffer overflow, and in this article I’m going to extend the previous work to show you how that stack protector works. This is a feature you can turn on for your own systems, but I wanted to specifically show you why they aren’t a perfect shield that will detect all buffer overflows. In fact it won’t even detect the buffer overflow from my previous article!

I specifically wanted to introduce this attack to demonstrate that embedded systems often have more attack vectors to consider than “normal” software. Compiling code for a Linux or Windows host can be fairly consistent, but embedded systems have more variability.

In this article I’ll also show you how a buffer overflow can overwrite the stack, and how you can test this yourself using low-cost hardware. This specific example is something I challenged my graduate student Brian Peters to experiment with—in fact he made an even more complex attack, which I’ll discuss at the end of this article, but it will show you what is possible beyond what you might think of as a normal buffer overflow attack.

Hardware Setup

In the previous article I used a Raspberry Pi Pico. You can use the same device again, but I’m going to be switching platforms to a STMicroelectronics STM32F3 Nucleo board. The reason being that this board has a built-in debugger, which you can use to inspect the memory and stack. When it comes time to experimenting with the target, I find it much easier to use a graphical interface to see the exact changes that happen to your memory.

From the stock STM32F3 Nucleo board, I flash it with the Segger J-Link On-Board (OB) firmware (see resources). This step lets me use Segger’s lightweight “Ozone” debugger tool. I greatly prefer this tool; it’s especially useful for a teaching environment as it makes it very easy to see memory and registers. It even works without the source code for your firmware, which makes it very helpful when reverse engineering targets.

For this column I’ll be using it in that mode, the reason being it will look closer to what an attacker would actually see (since they won’t have your source code).

GCC Stack Protector

To keep this article (mostly) self-contained, I’m including a short snippet of the serial code from my previous article. This is shown in Listing 1. Compared to my previous article’s Listing 1, this is much shorter as there is no password comparison logic.

#include <stdio.h>#include “string.h”void receive_password(void){    //Local variables    char c;    char * buf;    char command_buffer[64];    //Command buffer pointer - set first character to something    //defined so loop enters below    buf = command_buffer;    c = 0;    //Receive until user hits enter (newline or CR)    while ((c != ‘\n’) && (c != ‘\r’)){        c = getchar();        *buf++ = c;    }    //Comparison code removed from here}

Listing 1
A very simple buffer handling routine with an obvious buffer overflow.

In the previous article we overwrote a variable on the stack, in this article we are targeting the return address itself. This also lends itself to show you how the first protection might work, the GCC stack protector.

If you haven’t used this before, it’s as easy as passing the option “-fstack-protector” to GCC. You can choose to enable it on “most” functions with -fstack-protector-strong, and force it onto every function with -fstack-protector-all.

I’ll again suggest you try this on the Compiler Explorer (godbolt.org). Putting a simple function (like the one from Listing 1) should make it more obvious what the effect is. I’ve copied two examples of the output into Figure 1, which shows (on the left) the unprotected assembly output, and on the right the protected assembly output.

Figure 1
Using the compiler explorer (Godbolt.org) we can compare what happens when we take unprotected code on the left and enable the stack protector to give us the code on the right.
Figure 1
Using the compiler explorer (Godbolt.org) we can compare what happens when we take unprotected code on the left and enable the stack protector to give us the code on the right.

You’ll notice the protected version is longer—the downside of the stack protector is that each function call is a little slower. If this was something called frequently in a loop it could be a bigger issue, but for something like my serial receive function it’s negligible overhead. A few extra instruction when we are dealing with a serial receive is well worth it.

Going back to Figure 1, you can see that at the start of the function on the right (the protected code), line 3 loads into register r3 the __stack_chk_guard constant. This then gets stored to the stack before the return address. The idea of this is that nothing in the function should modify that value, so if we see the value changed, something bad happened.

The second part of the protection is at the end of the function to do the check. The stack check value gets reloaded into a local register at line 16, and then compared to the value on the stack at line 19. If this check fails there is a conditional jump at line 21 that will cause the program to fail.

The default compiler option of -fstack-protector only enables this logic on functions with larger buffers. If you change the buffer from the 64-byte length I have in Listing 1 to a 4-byte buffer the protection will be dropped.

The idea of this is to avoid adding the overhead to all possible functions, but just ones that might have vulnerable buffers. In my example the vulnerability exists because of the I/O functionality, and in many embedded systems it is not the size of the buffers that matter but the existence of some user controllable code.For this reason you may want to enable this on all functions, and selectively disable it on ones you know have no possible vulnerabilities or are called frequently (where the overhead of the stack protector negatively impacts your code).

The other problem with using the protector is it may give you a false sense of security. My attack from the previous article will still succeed, because it never actually gets far enough to impact the stack check value. I overwrite a local stack variable, but the stack protector is only protecting the return address from corruption.

A Concrete Attack

Now I’m going to return to the more complete example from my previous article – this code is longer than that in Listing 1, the difference is it also had a local variable storing the known good password. If you imagine Listing 1 with some additional buffers, you’d then believe that we could have a stack that looks like Figure 2.

Figure 2
An example of the stack of our implementation, which has a buffer index stored on the stack itself.
Figure 2
An example of the stack of our implementation, which has a buffer index stored on the stack itself.

This looks a tiny bit different from the stack setup I had in my previous article. This reflects what we found was actually being implemented, and there is one subtle difference that has major implications for the attack. The difference is that when we compiled this code we had disabled optimizations, which caused the buffer index itself to get stored on the stack.

This buffer index caused us grief when we were first trying to implement an attack. As I overflowed the stack what I expected to happen is the device would crash—this is the normal sign that you’ve caused a buffer overflow, because you are overflowing either the stack check or the return value. Either of those represents a critical error and the device should halt.

Instead, we were seeing the device continued on as normal! Using the debugger showed however memory in other locations was getting overwritten or corrupted, which you can see in Figure 3.

Figure 3
Using Ozone memory watch (or any debugger) helps to detect these types of problems, since I could see both the memory overflow, and in this case also see that data was being corrupted even outside of my expected overflow locations.
Figure 3
Using Ozone memory watch (or any debugger) helps to detect these types of problems, since I could see both the memory overflow, and in this case also see that data was being corrupted even outside of my expected overflow locations.

Here I’ve sent 100 “x” characters (hex 0x78). In the memory watch from Ozone the changed memory bytes in red show a bunch of 64 “x” characters in a block (shown in the cyan rectangle I added).

But there are a couple of “random” other characters of the same value (shown in purple boxes I added). While I expected to see 100 “x”s, instead the addresses causes it to point elsewhere in memory and only write the 64 in the expected location.

Ultimately, due to the addressing of the array, it provides an ability to write to unintended locations. This happens because as we overflowed the stack, we were overwriting the buffer index.

I was initially just sending null characters (0x00) which means my buffer index was always getting reset back to 0—at this point the overflow “stopped”, since my buffer (which I was trying to overflow) was written to the start of the buffer. The index would increment again & the entire thing would repeat.

Figure 4 shows a simplified version of the memory setup that is causing this entire problem. As we approach the buffer index (the purple square in Figure 4) we then need to send the new index itself. Whatever index is set will then be incremented by the code for each new received byte.

Figure 4
A simple example of how the buffer index points to the buffer, but if the index increments to point at itself we suddenly have an ability to point elsewhere in memory.
Figure 4
A simple example of how the buffer index points to the buffer, but if the index increments to point at itself we suddenly have an ability to point elsewhere in memory.

This means if we wanted to overwrite the return value we need to set up a pointer to the return value first. A successful attack on this platform is given in Listing 2. Again this attack was demonstrated by my graduate student Brian Peters—in a “Goodwill Hunting” moment when I had initially left this problem for Brian to solve I didn’t realize the index problem made this a much more interesting (difficult) attack. But it shows how embedded systems leave a lot of complexity that we have to carefully manage, even in cases (like this one) where I set out to demonstrate one vulnerability, but ended up demonstrating an entirely separate vulnerability.

import serialser = serial.Serial(“com4”, timeout=2)# Fillerser.write(48*b’x’)# DEAD BEEF (memory check)ser.write(b’\xde’)ser.write(b’\xad’)ser.write(b’\xbe’)ser.write(b’\xef’)# Fillerser.write(8*b’x’)# Point to the return address (adjust the index)ser.write(b’\xd4’)# RETURN ADDRESS -set to “Access Granted” functionser.write(b’\x71’)ser.write(b’\x04’)ser.write(b’\x00’)ser.write(b’\x08’)# Terminate the bufferser.write(b’\r\n’)

Listing 2
An example of an attack on my code, thus time jumping to the “Access Granted” function directly instead of bypassing the password check logic.

In fact the vulnerability allowed Brian to rewrite sections of the memory, and not just change the return value. This included changing strings and other parts of the program!

The final point I’ll leave you with is to consider whether the GCC stack protection feature would have even prevented this attack. In this example it wouldn’t—I added a second memory check feature that Brian bypassed (the fixed 0xDEADBEEF constant), but because we are able to index into a specific memory location we can even skip the GCC stack protection check!

If you’d like to experiment with this yourself (or perhaps use this example for your own classes), I’ve uploaded the source code and a few example attacks to my companion repository, link available on the Circuit Cellar Article Materials webpage. As I mentioned, you can also try this on a Raspberry Pi Pico; you don’t have to run this on the STM32F3 Nucleo.

Even if you don’t run the attack, hopefully this gave you some ideas about how to use the GCC stack protection feature, and where you might want to add additional protection beyond the default compiler protection. 

RESOURCES
Segger | https://www.segger.com
STMicroelectronics | https://www.st.com

SOURCES
Segger J-Link OB: https://www.segger.com/products/debug-probes/j-link/models/j-link-ob/
Segger Ozone: https://www.segger.com/products/development-tools/ozone-j-link-debugger/
STM32 Nucleo Boards: https://www.st.com/en/evaluation-tools/stm32-nucleo-boards.html

Source Code: https://github.com/colinoflynn/circuitcellar-EmbeddedSystemEssentials

PUBLISHED IN CIRCUIT CELLAR MAGAZINE • MARCH 2026 #428 – Get a PDF of the issue

Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.


Note: We’ve made the Dec 2022 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Sponsor this Article
Website | + posts

Colin O’Flynn has been building and breaking electronic devices for many years. He is an assistant professor at Dalhousie University, and also CTO of NewAE Technology both based in Halifax, NS, Canada. Some of his work is posted on his website (see link above).

Supporting Companies

Upcoming Events


Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2026 KCK Media Corp.

From Buffer Overflows to Control Flow Attacks

by Colin O'Flynn time to read: 9 min