<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ilya Markevich</title>
    <description>The latest articles on Forem by Ilya Markevich (@ilyamarkevich).</description>
    <link>https://forem.com/ilyamarkevich</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F238217%2F9dfd2571-e2ea-4dc1-8fd6-458e5ab07355.jpeg</url>
      <title>Forem: Ilya Markevich</title>
      <link>https://forem.com/ilyamarkevich</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ilyamarkevich"/>
    <language>en</language>
    <item>
      <title>Security fundamentals: buffer overflow</title>
      <dc:creator>Ilya Markevich</dc:creator>
      <pubDate>Mon, 01 Apr 2024 22:04:38 +0000</pubDate>
      <link>https://forem.com/ilyamarkevich/security-fundamentals-buffer-overflow-1ei4</link>
      <guid>https://forem.com/ilyamarkevich/security-fundamentals-buffer-overflow-1ei4</guid>
      <description>&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Preface&lt;/li&gt;
&lt;li&gt;
Basic concepts

&lt;ul&gt;
&lt;li&gt;RAM and registers&lt;/li&gt;
&lt;li&gt;Stack&lt;/li&gt;
&lt;li&gt;Program decompilation&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Buffer overflow&lt;/li&gt;
&lt;li&gt;Buffer overflow sample&lt;/li&gt;
&lt;li&gt;
Protection against buffer overflow

&lt;ul&gt;
&lt;li&gt;Safe code&lt;/li&gt;
&lt;li&gt;High-level languages&lt;/li&gt;
&lt;li&gt;Stack canaries&lt;/li&gt;
&lt;li&gt;Non-executable memory&lt;/li&gt;
&lt;li&gt;Address space layout randomization (ASLR)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Preface &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://owasp.org/www-community/vulnerabilities/Buffer_Overflow"&gt;Buffer overflow attack&lt;/a&gt; is one of the oldest and well documented security vulnerabilities. Despite that, it still happens quite often: not only in old legacy code but also in &lt;a href="https://www.bleepingcomputer.com/news/security/millions-of-exim-mail-servers-exposed-to-zero-day-rce-attacks/"&gt;relatively new products&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Studying the attack was interesting - while I was exploring how buffer overflow works, I had to dive into some core concepts of how computer works (which I also will try to explain in the article). &lt;/p&gt;

&lt;p&gt;I hope after reading the article, you will have basic understanding about buffer overflow: how it works, why it's dangerous and what are the defense mechanisms.&lt;/p&gt;

&lt;p&gt;P.S. For code samples, I will be using C together with gcc compiler and &lt;a href="https://ghidra-sre.org"&gt;Ghidra&lt;/a&gt; for programs decompilation. I'm also on Linux 6.2.0-36-generic with Intel Core i7-7700HQ x64. Keep in mind if you have different processor or operating system, the compilation/decompilation result might be different. And, if you want to experiment, I would recommend using VM not to kill your system accidentally.&lt;/p&gt;

&lt;p&gt;Ok, let's start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic concepts &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Before studying the attack, let's go through some basic concepts that are essential for its understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  RAM and registers &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To understand how buffer overflow attack can be done, we need a basic understanding of how a program is represented in RAM and how the CPU executes program instructions.&lt;/p&gt;

&lt;p&gt;To put it simply, RAM is just a set of bytes, where each byte can be referenced by address. The address is represented with &lt;a href="https://en.wikipedia.org/wiki/Hexadecimal"&gt;hexadecimal&lt;/a&gt; numbers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hv15fbhomtb8eoualh8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hv15fbhomtb8eoualh8.png" alt="Memory structure" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Registers are quickly accessible memory located near CPU which stores important information for program execution. CPU can access the memory much faster than RAM. We will explore some registers later in the article.&lt;/p&gt;

&lt;p&gt;The CPU sequentially reads and executes program instructions from RAM. Registers are used to quickly access some data - for example, address of the next instruction to execute (RIP register on x64 processors).&lt;/p&gt;

&lt;p&gt;Each process in operating system has associated memory split on 4 segments:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2x176bf46j66d4orov1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2x176bf46j66d4orov1.png" alt="Process memory" width="800" height="737"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code segment - the segment stores executable code. Instruction pointer register (&lt;code&gt;RIP&lt;/code&gt;) pointing to program instruction from the segment.&lt;/li&gt;
&lt;li&gt;Globals/static variables segment - the segment stores globally declared and static variables to reuse them between all parts of the program by referencing single address in memory.&lt;/li&gt;
&lt;li&gt;Heap segment - the segment is used for dynamically allocated memory during program execution (for example, calling &lt;code&gt;malloc&lt;/code&gt; function in C). The segment is controlled by programmer, so it's programmers responsibility to cleanup memory when it's not needed (calling &lt;code&gt;free&lt;/code&gt; function in C). The segment grows up, meaning new objects will be allocated in higher addresses.&lt;/li&gt;
&lt;li&gt;Stack segment - the segment is used to store the programs stack. It's controlled by compiler, programmer doesn't have to cleanup the memory on function exit. The segment grows down, meaning the new stack frames will be allocated in lower addresses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stack is the most important part in understanding how buffer overflow attack can be done, so worth discussing it further.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Stack_(abstract_data_type)"&gt;Stack&lt;/a&gt; is an abstract data type with the main property that the last data placed on the stack will be the first removed. This property is usually implemented with &lt;code&gt;PUSH&lt;/code&gt; (adding data to the stack) and &lt;code&gt;POP&lt;/code&gt; (removing data from the stack) commands.&lt;/p&gt;

&lt;p&gt;How is it used in the context of a program? Each function call creates a new stack (called a stack frame), which stores local variables, parameters passed to the function (for x86 processors only; for x64, parameters are passed using registers), and the return address - the address of the next instruction after the function finishes. All stack frames are placed on top of each other.&lt;/p&gt;

&lt;p&gt;Two dedicated registers are used by the CPU to work with the stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RSP&lt;/code&gt; register contains the address of the top of the stack. This address is also known as the stack pointer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RBP&lt;/code&gt; register contains some fixed address inside the active stack frame. This address is also known as the base pointer and is used for referencing local variables and function parameters inside the stack frame.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both registers are used in x64 processors; for x86, there will be &lt;code&gt;ESP&lt;/code&gt; / &lt;code&gt;EBP&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqos4lywgpiqip3fn1j2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqos4lywgpiqip3fn1j2.png" alt="Stack" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Program decompilation &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let's decompile the simple C program to showcase how the core concepts described above work together. To do that, I will be using &lt;a href="https://ghidra-sre.org/"&gt;Ghidra&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here's the program code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// hello.c&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;doubleNum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doubleNum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I compiled the program with &lt;code&gt;gcc hello.c -fno-stack-protector -o hello&lt;/code&gt; (more about the &lt;code&gt;-fno-stack-protector&lt;/code&gt; flag later) and loaded the compiled program into Ghidra.&lt;/p&gt;

&lt;p&gt;Let's set a breakpoint before entering the &lt;code&gt;doubleNum&lt;/code&gt; function and see what memory/stack/registers look like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiwafrwdo9lne6tz0dww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiwafrwdo9lne6tz0dww.png" alt="Entering first function call" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 3 windows shown in Ghidra (I put some tags for easier referencing):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Window 1 shows the compiled listing of the program.&lt;/li&gt;
&lt;li&gt;Window 2 shows how the program instructions are represented in memory.&lt;/li&gt;
&lt;li&gt;Window 3 shows state of the registers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The program is paused on &lt;code&gt;1a&lt;/code&gt; (&lt;code&gt;2d&lt;/code&gt; in the compiled window), which is the call of the &lt;code&gt;doubleNum&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;From the picture we can see interesting details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RSP&lt;/code&gt; register contains the address of the top of the stack (stack pointer). The address is &lt;code&gt;7fffffffdc80&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RBP&lt;/code&gt; register contains the address of the base pointer, representing a fixed address in the current stack frame. The address is &lt;code&gt;7fffffffdc90&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Comparing &lt;code&gt;RSP&lt;/code&gt; and &lt;code&gt;RBP&lt;/code&gt; values, we can see that the stack grows towards lower addresses (the address of the top of the stack &lt;code&gt;7fffffffdc80&lt;/code&gt; is lower than the base pointer address &lt;code&gt;7fffffffdc90&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RIP&lt;/code&gt; register contains the address of the next instruction to execute (&lt;code&gt;5555555551a5&lt;/code&gt;). This matches the address of instuction we are paused on (&lt;code&gt;2d&lt;/code&gt; from the screenshot).&lt;/li&gt;
&lt;li&gt;Note the address differences for instructions and the stack - stack addresses start from &lt;code&gt;7ffff&lt;/code&gt; while instruction from &lt;code&gt;55555&lt;/code&gt;. That's because of the process memory organization described previously. &lt;/li&gt;
&lt;li&gt;At the start of the &lt;code&gt;main&lt;/code&gt; function, 16 bytes are allocated for the active stack frame (&lt;code&gt;2a&lt;/code&gt; instruction). Then, the local variable &lt;code&gt;num&lt;/code&gt; is written at the address &lt;code&gt;RBP - 8&lt;/code&gt; (&lt;code&gt;7fffffffdc88&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;From the instruction &lt;code&gt;2c&lt;/code&gt; we can see that the parameter for &lt;code&gt;doubleNum&lt;/code&gt; function is passed through the register &lt;code&gt;EDI&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's the stack frame for the &lt;code&gt;main&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftc1pb7lccwqq2kbat7h1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftc1pb7lccwqq2kbat7h1.png" alt="Decompilation (step 1)" width="800" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's jump into the &lt;code&gt;doubleNum&lt;/code&gt; function call. Here's the state of the program after entering the function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6h1vai03ijnagr81b8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6h1vai03ijnagr81b8t.png" alt="Inside of doubleNum function" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that &lt;code&gt;RIP&lt;/code&gt; value has changed to the address of &lt;code&gt;2a&lt;/code&gt; instruction. However, what's more important, the &lt;code&gt;RSP&lt;/code&gt; value has also changed. It happened because &lt;code&gt;CALL&lt;/code&gt; instruction pushes the return address to the stack (the address where the program should jump to when &lt;code&gt;doubleNum&lt;/code&gt; function finishes).&lt;/p&gt;

&lt;p&gt;Here's the stack after entering &lt;code&gt;doubleNum&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbqkd09quw9nk8y8gd0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbqkd09quw9nk8y8gd0h.png" alt="Decompilation (step 2)" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's put a breakpoint before calling &lt;code&gt;multiply&lt;/code&gt; function and check the program state:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs56e0bwxnuxbrhfu56w0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs56e0bwxnuxbrhfu56w0.png" alt="Decompilion (step 3)" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instructions &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are called &lt;a href="https://en.wikipedia.org/wiki/Function_prologue_and_epilogue#Prologue"&gt;function prologue&lt;/a&gt;. The instructions prepare new stack frame, so let's break them down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PUSH RBP&lt;/code&gt; - push previous base pointer address to the stack. It's used later to retrieve previous function base pointer when active function finishes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MOV RBP,RSP&lt;/code&gt; - set base pointer for new stack frame.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SUB RSP,0x18&lt;/code&gt; - allocate 24 bytes in new stack frame.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instructions &lt;code&gt;d&lt;/code&gt; and &lt;code&gt;e&lt;/code&gt; put local variables to stack - instruction &lt;code&gt;d&lt;/code&gt; puts function argument, &lt;code&gt;e&lt;/code&gt; puts local variable &lt;code&gt;base&lt;/code&gt;. Then instructions &lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; write &lt;code&gt;multiply&lt;/code&gt; function parameters to &lt;code&gt;ESI&lt;/code&gt; and &lt;code&gt;EDI&lt;/code&gt; registers.&lt;/p&gt;

&lt;p&gt;Here's how the stack will look like after the execution of all the instructions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftk1u1z818bc4c33eeder.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftk1u1z818bc4c33eeder.png" alt="Decompilation (step 4)" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's put a breakpoint in &lt;code&gt;multiply&lt;/code&gt; function. I put it at the end because it has similar instructions as &lt;code&gt;doubleNum&lt;/code&gt;. Here's the program state:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcr44soiw48fgcncdlckp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcr44soiw48fgcncdlckp.png" alt="Decompilation (step 5)" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that the stack pointer shifted again. Now, the stack contains old &lt;code&gt;RBP&lt;/code&gt; value for &lt;code&gt;doubleNum&lt;/code&gt; function. Note there's no &lt;code&gt;SUB RSP, N&lt;/code&gt; instruction, most probably because of the compiler optimizations - no need to allocate new space in the stack if there are no calls to other functions. &lt;br&gt;
Also, the result of the multiplication is set to &lt;code&gt;EAX&lt;/code&gt; register, meaning return value is passed through the registers.&lt;/p&gt;

&lt;p&gt;Here's the stack after execution of all the instructions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbcneesaqnzc21cwemai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbcneesaqnzc21cwemai.png" alt="Decompilation (step 4)" width="800" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last 2 instructions of the function, &lt;code&gt;POP RBP&lt;/code&gt; and &lt;code&gt;RET&lt;/code&gt;, are called &lt;a href="https://en.wikipedia.org/wiki/Function_prologue_and_epilogue#Epilogue"&gt;function epilogue&lt;/a&gt;. Let's break them down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;POP RBP&lt;/code&gt; - takes the value from top of the stack and put it to &lt;code&gt;RBP&lt;/code&gt; register. In our example, top of the stack is the address of the base pointer for &lt;code&gt;doubleNum&lt;/code&gt; function (&lt;code&gt;7fffffffdc70&lt;/code&gt;). Stack pointer is moved to &lt;code&gt;7fffffffdc50&lt;/code&gt; (as &lt;code&gt;POP&lt;/code&gt; removes value from the stack, the stack pointer is shifted as well).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RET&lt;/code&gt; - takes the address from the top of the stack and put it to &lt;code&gt;RIP&lt;/code&gt; register, meaning the program jumps to the address. Top of the stack will be return address of &lt;code&gt;multiply&lt;/code&gt; function which is instruction in &lt;code&gt;doubleNum&lt;/code&gt; right after calling &lt;code&gt;multiply&lt;/code&gt;. Stack pointer is moved to &lt;code&gt;7fffffffdc58&lt;/code&gt; (&lt;code&gt;RET&lt;/code&gt; under the hood executes &lt;code&gt;POP RIP&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how the stack will look like after the instructions execution:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftk1u1z818bc4c33eeder.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftk1u1z818bc4c33eeder.png" alt="Decompilation (step 5)" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, epilogue for &lt;code&gt;doubleNum&lt;/code&gt; function will be executed which will remove &lt;code&gt;doubleNum&lt;/code&gt; stack frame. And then epilogue for main function will remove last stack frame and exit the program.&lt;/p&gt;

&lt;p&gt;I hope now you have basic understanding how program is organized in memory and how instructions are executed. With the knowledge we can dive deeper into buffer overflow attack.&lt;/p&gt;
&lt;h2&gt;
  
  
  Buffer overflow &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine there is code in a C program that either delcares fixed length buffer on the stack (e.g. &lt;code&gt;char buffer[10];&lt;/code&gt;) or on the heap (&lt;code&gt;char* buffer = malloc(10)&lt;/code&gt;). Buffer overflow happens if attacker is able to put data into the buffer that exceedes the length of the buffer. Depending on where the buffer is placed, the attack can target either stack or heap.&lt;/p&gt;

&lt;p&gt;If buffer overflow happens on the heap, it can override other data located near the buffer or even other processes data.&lt;/p&gt;

&lt;p&gt;If overflow happens on the stack, there are multiple attack options: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the simplest will be overflowing the buffer with a random value. This will most likely crash the program because function return address will be overwritten with memory address which contains invalid instructions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mi7796jegppqvg888sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mi7796jegppqvg888sv.png" alt="Buffer overflow - program crash" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;overwrite local variable(s) (or a function pointer) to change the program's behaviour.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa18j8gq18l1zbhzowr5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa18j8gq18l1zbhzowr5v.png" alt="Buffer overflow - variable override" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;overwrite the return address of the function with address containing specific instructions. The simplest attack implementation will be changing the return address to the overflowed buffer address and placing malicious code in the buffer itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqhu2exxxvd2dksaq90cr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqhu2exxxvd2dksaq90cr.png" alt="Buffer overflow - executing malicious code" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's look at buffer overflow in details with code sample.&lt;/p&gt;
&lt;h2&gt;
  
  
  Buffer overflow sample &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To explore the attack, I will use the code sample written in C:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printGreetings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;nameInput&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

 &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nameInput&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;askQuestion&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"How are you?&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// input&lt;/span&gt;

 &lt;span class="n"&gt;printGreetings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="n"&gt;askQuestion&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
 &lt;span class="n"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I compiled it with &lt;code&gt;gcc -fno-stack-protector hello.c -o hello&lt;/code&gt; and disabled ASLR protection (more about the protection later) with &lt;code&gt;echo 0 &amp;gt; /proc/sys/kernel/randomize_va_space&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The program is simple: it takes a name, prints greetings, prints a question and exits. For simplicity, I will set the name directly in the program.&lt;/p&gt;

&lt;p&gt;Let's start with setting the name to "Ilya". Here's the program output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Ilya
How are you?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, seems like program works. Let's try to hack it with buffer overflow!&lt;/p&gt;

&lt;p&gt;As you might already noticed, the potential issue is in the line &lt;code&gt;strcpy(name, nameInput);&lt;/code&gt;. Since &lt;code&gt;strcpy&lt;/code&gt; doesn't check the bounds of the &lt;code&gt;name&lt;/code&gt; buffer, we can pass &lt;code&gt;nameInput&lt;/code&gt; of any size and it will overflow buffer &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The simplest way to execute a buffer overflow attack would be to pass a randomly long name, for example &lt;code&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/code&gt;. After trying the value, I get&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, aaaaaaaaaaaaaaaaaaaaaaaaaaa
Segmentation fault
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly the case of buffer overflow with randomly overwritten return address:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mi7796jegppqvg888sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mi7796jegppqvg888sv.png" alt="Buffer overflow - program crash" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The program crashes after execution of &lt;code&gt;printGreetings&lt;/code&gt; function because it tries to jump to memory address with invalid instructions.&lt;/p&gt;

&lt;p&gt;Ok, this was simple. Let's try something more interesting.&lt;br&gt;
How about changing programs flow? We can override return address and execute real instructions instead of jumping to some random addresses.&lt;/p&gt;

&lt;p&gt;Changing return address is not easy, and todays processors and operating systems have many defense mechanisms in place to make guessing of return address and execution of malicious instructions difficult. Howerver, it's still possible to &lt;a href="https://en.wikipedia.org/wiki/Return-oriented_programming"&gt;overcome the defenses&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For simplicity, I will find the needed address in Ghidra. Let's decompile the program and check what's the state of memory before calling &lt;code&gt;strcpy(name, nameInput)&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8g5z6msui04zpbzxgb02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8g5z6msui04zpbzxgb02.png" alt="Memory state before strcpy" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The stack frame has standard structure. From lower to higher memory addresses: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;local variables: &lt;code&gt;name[10]&lt;/code&gt;, 10 bytes.&lt;/li&gt;
&lt;li&gt;base pointer: 8 bytes, but used 6.&lt;/li&gt;
&lt;li&gt;return address: 8 bytes, but used 6.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, note that base pointer and return address values has written from the least significant bit to the most one because of &lt;a href="https://en.wikipedia.org/wiki/Endianness"&gt;little-endian notation&lt;/a&gt; (for example, base pointer value &lt;code&gt;7fffffffdc90&lt;/code&gt; written in memory like &lt;code&gt;90dcffffff7f&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;What we want is to override return address of the function using buffer &lt;code&gt;name[10]&lt;/code&gt;. That means we have to construct input that will override all the bytes starting from &lt;code&gt;name[10]&lt;/code&gt; through base pointer and till return address where return address should be real address we want to jump to.&lt;/p&gt;

&lt;p&gt;For our exploit, let's jump to &lt;code&gt;finish&lt;/code&gt; function, so we are going to skip &lt;code&gt;askQuestion&lt;/code&gt; call. Finding the &lt;code&gt;finish&lt;/code&gt; call instruction address using Ghidra:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxsiuoa9z8mox8hchelgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxsiuoa9z8mox8hchelgy.png" alt="Finding target return address" width="800" height="684"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The address is &lt;code&gt;55555555526b&lt;/code&gt;. Now, we have all the information to construct the malicious input: 10 random bytes to fill name[10] + 8 random bytes to override base pointer + 6 bytes of return address written in little-endian notation. Here's how result input will look like: &lt;code&gt;111111111122222222\x6b\x52\x55\x55\x55\x55&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's try the input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, 111111111122222222kRUUUU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works! By exploiting the buffer overflow, we were able to change the program flow (skip &lt;code&gt;askQuestion&lt;/code&gt; function call) and exit without any errors.&lt;/p&gt;

&lt;p&gt;That was very simplified version of buffer overflow, but I hope now you can see how dangerous the attack can be - attacker can potentially execute any code, for example execute commands under root/admin user.&lt;/p&gt;

&lt;p&gt;Let's explore what defense mechanisms are used to prevent the attack.&lt;/p&gt;

&lt;h2&gt;
  
  
  Protection against buffer overflow &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Safe code &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;There are functions in C that don't apply bounds checking on buffers, so it's possible to execute buffer overflow attacks targeting them (&lt;code&gt;strcat&lt;/code&gt;, &lt;code&gt;strcpy&lt;/code&gt;, &lt;code&gt;gets&lt;/code&gt; etc.). It's recommended to use safer equivalents of the functions (&lt;code&gt;strncat&lt;/code&gt;, &lt;code&gt;strncpy&lt;/code&gt;, &lt;code&gt;fgets&lt;/code&gt; etc.). Luckily, today's C compilers show warnings if programmer tries to use the unsafe functions. These warnings shouldn't be ignored and should be fixed as soon as possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm36ss333tkx3oom033r8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm36ss333tkx3oom033r8.png" alt="Compile warning on using dangerous function" width="800" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although there are static code analyzers that can indicate potentially insecure code, unfortunately it's not possible to completely eliminate buffer overflow attacks with this defense. There is legacy code and libraries that programmers can't control, as well as runtime code that is impossible to analyze.&lt;/p&gt;

&lt;h3&gt;
  
  
  High-level languages &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;As you could see previously, buffer overflow attacks mostly target C/C++ code because of the low-level API for memory management.&lt;br&gt;
The usage of high-level languages such as Java/C# can prevent such attack. However, a lot of software (inlcuding operating systems and web servers) is written in C/C++ for performance and other reasons.&lt;/p&gt;
&lt;h3&gt;
  
  
  Stack canaries &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Stack canaries is a technique used by compiler to prevent buffer overflow attacks. The idea is simple: the compiler places a random value (canary) on the stack before each return address. After a function is executed, the compiler checks if the canary value has changed before jumping to the specified return address. If it has changed (likely due to a buffer overflow attack), the program will be interrupted without jumping to the specified return address.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1rsrpeoz4ymcczb6leq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1rsrpeoz4ymcczb6leq7.png" alt="Stack canary" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If I compile the buffer overflow example without the &lt;code&gt;-fno-stack-protector&lt;/code&gt; parameter, the compiler detects the buffer overflow and aborts the program&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, 000000000011111111kRUUUU
*** stack smashing detected ***: terminated
Aborted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The technique is simple yet effective, making the buffer overflow attack difficult to execute. However, it's not a silver bullet. The canary value can be brute-forced if it remains unchanged upon program restart. Although this sounds like a rare case, it still occurs in reality. For example, a process created with &lt;code&gt;fork&lt;/code&gt; call (Linux) inherits the canary value from parent process. This is the case for some web server implementations: when a process crashes, the main web server process spawns a child process with the same canary value. An attacker can attempt to brute-force the canary value byte by byte and use server crashes as an indicator that the canary value is incorrect.&lt;/p&gt;

&lt;p&gt;The stack canary is enabled by default in gcc compilers today.&lt;/p&gt;

&lt;h3&gt;
  
  
  Non-executable memory &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Basic buffer overflow attack includes overwriting function return address to the overflowed buffer memory address with malicious instructions. Non-executable memory prevents execution of the instructions by marking the stack memory segment as non-executable.&lt;/p&gt;

&lt;p&gt;While this defense prevents classical buffer overflow attacks, it can still be exploited. Sometimes, executable memory is required to generate code on the fly, for example with &lt;a href="https://en.wikipedia.org/wiki/Just-in-time_compilation"&gt;JIT&lt;/a&gt; compilation. The exploit can potentially be injected during this phase. Another example is the &lt;a href="https://en.wikipedia.org/wiki/Return-oriented_programming"&gt;ROP&lt;/a&gt; attack which uses already existing instructions outside of the stack segment.&lt;/p&gt;

&lt;p&gt;Non-executable memory is enabled by default in gcc compiler today. You can disable it with &lt;code&gt;-z execstack&lt;/code&gt; parameter during compilation (do it only for study purposes).&lt;/p&gt;

&lt;h3&gt;
  
  
  Address space layout randomization (ASLR) &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To exploit buffer overflow, the attacker needs to know the exact address of instructions to jump to. ASLR prevents this by randomizing the stack/heap/code addresses, so the address will be different on every process restart.&lt;/p&gt;

&lt;p&gt;ASLR is enabled by default on all modern operating systems (Windows/Linux/Mac). On Linux, the setting can be found in &lt;code&gt;/proc/sys/kernel/randomize_va_space&lt;/code&gt; (it has a default value 2, meaning enabled). With this defense in place, the buffer overflow sample I used will fail with a &lt;code&gt;segmentation fault&lt;/code&gt; because the return address hardcoded in the program will no longer be valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I tried to show why buffer overflow is still dangerous, the problems it can create and the defense mechanisms that operating systems and compilers use to prevent the attack.&lt;/p&gt;

&lt;p&gt;I hope you found the article interesting. Thank you for reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  References &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://css.csail.mit.edu/6.858/2014/readings/buffer-overflows.pdf"&gt;https://css.csail.mit.edu/6.858/2014/readings/buffer-overflows.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://phrack.org/issues/49/14.html#article"&gt;http://phrack.org/issues/49/14.html#article&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amazon.com/Programming-Ground-Up-Jonathan-Bartlett/dp/1616100648"&gt;https://www.amazon.com/Programming-Ground-Up-Jonathan-Bartlett/dp/1616100648&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ocw.mit.edu/courses/6-858-computer-systems-security-fall-2014/resources/lecture-3-buffer-overflow-exploits-and-defenses/"&gt;https://ocw.mit.edu/courses/6-858-computer-systems-security-fall-2014/resources/lecture-3-buffer-overflow-exploits-and-defenses/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>bufferoverflow</category>
    </item>
  </channel>
</rss>
