Security weaknessCWE Top 25 · #14
CWE-121 Stack-based buffer overflow
A program writes past a buffer on its call stack, causing crashes or handing control to an attacker.
- Senior security engineers
- 540+ projects delivered
- Reply within one business day
Quick answer
What is CWE-121?
Stack-based Buffer Overflow: The product writes past the end of a buffer that was allocated on the stack.
Business impact
What CWE-121 could cost your business
MITRE documents what this weakness can do. Here is what that means for a website, store or app.
Confidentiality
Private data exposed
Customer records, credentials, orders or internal files can be read by people who should never see them.
- Modify Memory
- Execute Unauthorized Code or Commands
- Bypass Protection Mechanism
Integrity
Data, pages or code changed
Prices, orders, content or the code your site runs can be altered without your approval.
- Modify Memory
- Execute Unauthorized Code or Commands
- Bypass Protection Mechanism
Availability
Downtime and lost sales
Crashes, slowdowns or exhausted resources can take your site, store or app offline.
- Modify Memory
- DoS: Crash, Exit, or Restart
- DoS: Resource Consumption (CPU)
Access Control
Accounts and admin areas exposed
Logins and permission checks can be bypassed, putting admin and customer accounts at risk.
- Modify Memory
- Execute Unauthorized Code or Commands
- Bypass Protection Mechanism
In plain English
What Stack-based buffer overflow means for your business
- 540+projects delivered
- 9+years in business
- 23+certified experts
A senior engineer replies within one business day.
The stack is the area of memory a program uses to keep track of the function it is running: its local variables and where to return when it finishes. A stack-based buffer overflow happens when code writes more data into a local buffer than it can hold. The excess overwrites other values on the stack. Because the return location is stored close by, this type of overflow has long been tied to attackers redirecting a program.
This weakness lives in C and C++ code. Modern compilers and operating systems add protections that make it harder to exploit, but they do not remove the bug, and older or embedded software often lacks them. If you run a website or store, your exposure is through the native programs in your stack and on your network. If you ship a native app or a device, the responsibility for finding and fixing it is yours.
Where it shows up
- Network-facing services written in C or C++, such as older web servers, proxies and mail servers, that parse headers into fixed-size local buffers.
- Embedded and IoT firmware in routers, cameras, printers and payment hardware, where stack protections are often missing and updates are infrequent.
- Native mobile app code and game engines that copy strings or file data into local arrays without checking length.
- C or C++ extensions and file parsing libraries used by PHP, Python or Node.js for images, archives, fonts and documents.
Free security check
Is CWE-121 hiding in your site?
A senior engineer checks your site, store or app for this weakness and the rest of the CWE Top 25, then sends a prioritized fix plan within one business day. No obligation.
Official definition
How MITRE defines CWE-121
Stack-based Buffer Overflow
The product writes past the end of a buffer that was allocated on the stack.
Also known as: Stack Overflow, Stack Buffer Overflow
Fix it
How to fix Stack-based buffer overflow
Practical steps in priority order. Share them with your developer or agency.
Bounds-check every local buffer
Before writing to a stack buffer, confirm the data length is within its size. Validate lengths that come from files, network messages or user input, and reject anything larger than the destination can hold.
Stop using dangerous functions
Remove gets, strcpy, strcat, sprintf and unbounded scanf formats. Use fgets, snprintf and other length-limited functions, or C++ strings and containers that manage their own size. Enforce this with compiler warnings and lint rules.
Enable stack protection at build
Compile with stack canaries using options such as -fstack-protector-strong or /GS, plus fortified source checks. A canary is a guard value that detects overwrites and stops the program before a corrupted return is used.
Keep OS exploit protections on
Build position-independent executables and keep address space layout randomization and non-executable stack memory enabled. These do not fix the overflow, but they make it far harder to turn into code execution.
Patch, fuzz and replace
Apply vendor updates to server software and firmware promptly, and replace devices that are no longer supported. For your own native code, run fuzzing with AddressSanitizer and consider moving parsers to a memory-safe language.
Signs you may be affected
- Devices or services on your network run firmware or software the vendor has stopped updating.
- A native program crashes with a stack smashing or stack corruption message on long input.
- Native code declares fixed-size local arrays and fills them with unbounded copy or read functions.
Questions to ask your developer
- Are stack protection and other compiler hardening flags enabled in our builds?
- Which devices on our network run firmware that is no longer updated?
- Do we fuzz test the code that parses files and network input?
Prevention
What MITRE recommends, phase by phase
Every mitigation from the CWE entry, in the order a project runs: plan, build, test, operate.
- 01
Architecture and Design
- Use an abstraction library to abstract away risky APIs. Not a complete solution.
- 02
Implementation
- Implement and perform bounds checking on input.
- Do not use dangerous functions such as gets. Use safer, equivalent functions which check for boundary errors.
- 03
Build and Compilation
- Use automatic buffer overflow detection mechanisms that are offered by certain compilers or compiler extensions. Examples include: the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY_SOURCE GCC flag, StackGuard, and ProPolice, which provide various mechanisms including canary-based detection and range/index checking. D3-SFCV (Stack Frame Canary Validation) from D3FEND.
- Run or compile the software using features or extensions that randomly arrange the positions of a program’s executable and libraries in memory. Because this makes the addresses unpredictable, it can prevent an attacker from reliably jumping to exploitable code. Examples include Address Space Layout Randomization (ASLR) [REF-58] [REF-60] and Position-Independent Executables.
Detection and evidence
How CWE-121 is found, and where it has hit
The testing methods MITRE recommends, and real vulnerabilities this weakness has caused.
How it is detected
- FuzzingFuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs – either randomly or algorithmically – and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption.
- Automated Static AnalysisAutomated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then.
- Automated Dynamic AnalysisUse tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++ [REF-1518].
Real vulnerabilities it caused
- CVE-2021-35395Stack-based buffer overflows in SFK for wifi chipset used for IoT/embedded devices, as exploited in the wild per CISA KEV.
How we fix it
From finding to fixed: how Codefreex closes CWE-121
Scan and confirm
We test your site, store or app for this weakness and the rest of the CWE Top 25 with automated scanning and manual review, and confirm what is actually exploitable.
Fix at the source
We change the code, configuration or dependency that causes it, not just the symptom, and plan the changes to avoid downtime.
Verify the fix
We retest every finding, check that nothing else broke and hand you a short report you can share with your team, client or auditor.
Keep it fixed
Updates, monitoring and maintenance so the weakness does not return with the next plugin, theme, app or code change.
Website SecurityPenetration TestingMalware Removal & Hack RepairWordPress Maintenance
Related weaknesses
Weaknesses related to CWE-121
FAQ
CWE-121: FAQs
Short answers for owners and developers. Not sure whether your site is affected? Ask us to check.
What is the difference between a stack and a heap overflow?
Both write past the end of a buffer. The difference is where the buffer lives. A stack overflow affects memory used for a function’s local variables and return information. A heap overflow affects memory the program requested dynamically for longer-lived data. They are detected and mitigated with different techniques, but both are prevented by checking sizes before writing.
How severe is a stack-based buffer overflow?
Severity is high. At minimum the program crashes, which causes downtime. In more serious cases an attacker can change what the program runs next and execute their own code, which can defeat other security controls on the system. MITRE rates the likelihood of exploitation as high. Modern protections reduce the risk but do not remove it.
How do I protect my business from this weakness?
If you run websites, keep server software, libraries and network devices updated, and remove anything unsupported. If you build native apps or devices, check bounds on every buffer, avoid dangerous functions, enable compiler and operating system protections, and fuzz test parsers. Where practical, write new components in a memory-safe language instead of C or C++.
What is CWE-121?
CWE-121, “Stack-based Buffer Overflow”, is a software weakness catalogued by MITRE in the Common Weakness Enumeration. The product writes past the end of a buffer that was allocated on the stack.
How serious is CWE-121?
MITRE rates the likelihood of exploit as High. It can affect availability, integrity, confidentiality, access control, with impacts such as modify memory, dos: crash, exit, or restart, dos: resource consumption (cpu). It ranks number 14 on the 2025 CWE Top 25 Most Dangerous Software Weaknesses. The real risk depends on where the weakness sits and what data or functions it exposes.
How do you prevent CWE-121?
Use automatic buffer overflow detection mechanisms that are offered by certain compilers or compiler extensions. Examples include: the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY_SOURCE GCC flag, StackGuard, and ProPolice, which provide various. Use an abstraction library to abstract away risky APIs. Not a complete solution.
Can Codefreex check my site for CWE-121?
Yes. A senior engineer reviews your site, store or app for CWE-121 and the rest of the CWE Top 25, confirms what is exploitable and sends a prioritized fix plan within one business day. Start with the free security check form on this page.
Free security check
Find out if CWE-121 affects your site
Send your URL. A senior engineer checks for this weakness and the rest of the CWE Top 25, and replies within one business day with what to fix first.
- A senior strategist reviews your request, not a sales bot
- Clear scope, timeline and estimate within one business day
- Free 30-minute discovery call, no obligation
- NDA available on request
- Emailhello@codefreex.com
- CallUSA +1 (209) 375-3475 · India +91 790 3375 530
- Prefer to talk?Book a 30-minute call
Weakness data from the Common Weakness Enumeration (CWE®) version 5.0, Copyright © 2006–2026, The MITRE Corporation. CWE is free to use by any organization or individual for any research, development, and/or commercial purposes under the CWE Terms of Use. CWE is a registered trademark of The MITRE Corporation. The business impact, risk profile and plain-English guidance on this page are written by Codefreex and are general information, not a security assessment of your system.