<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Scott</title>
    <description>The latest articles on Forem by Scott (@olivestem).</description>
    <link>https://forem.com/olivestem</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1143223%2F35dc562f-e492-4f65-8428-c8a14092ef76.png</url>
      <title>Forem: Scott</title>
      <link>https://forem.com/olivestem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/olivestem"/>
    <language>en</language>
    <item>
      <title>How to Install Drozer using Docker</title>
      <dc:creator>Scott</dc:creator>
      <pubDate>Mon, 06 May 2024 00:35:11 +0000</pubDate>
      <link>https://forem.com/olivestem/how-to-install-drozer-using-docker-nmm</link>
      <guid>https://forem.com/olivestem/how-to-install-drozer-using-docker-nmm</guid>
      <description>&lt;p&gt;Drozer is a security testing framework for Android, which allows you to easily scan for vulnerabilities in Android components. Typically, this involves scanning activities, content providers, broadcast receivers, and services. In this article, you will learn how to install Drozer and connect it to a local Android emulator.&lt;/p&gt;

&lt;p&gt;There are two components available for Drozer. The first component is the Drozer agent. This component is installed on your Android device. The Drozer agent acts as a server, running on port 31415. The Drozer client runs on any computer and it is designed to send requests to the Drozer agent. With these two components together, you can scan you Android applications for vulnerabilities. Let's get started with installing the Drozer agent!&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing the Drozer Agent
&lt;/h2&gt;

&lt;p&gt;To connect to a device, Drozer uses an agent application. This application is available on the &lt;a href="https://github.com/WithSecureLabs/drozer-agent/releases/latest"&gt;Drozer repository&lt;/a&gt;. To install the agent on your device, first, download the agent APK. Once you have downloaded the apk, use adb to install the app using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb install drozer-agent.apk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this command completes, locate the drozer agent application on your device. Tap the app to open it. The main app screen will provide you with the ability to start the Drozer embedded server. Tap the button labelled Off to activate the server.&lt;/p&gt;

&lt;p&gt;The Drozer server runs on port 31415 of your device. To allow this port to be reachable, you need to forward the port using the following adb command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb forward tcp:31415 tcp:31415
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your drozer agent is now ready to accept connections from the drozer client!&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing the Drozer client
&lt;/h2&gt;

&lt;p&gt;Drozer provides you with a variety of methods for installing the Drozer client. For this tutorial, we will use the Docker version of Drozer. To start, ensure that your have Docker installed on your computer.&lt;/p&gt;

&lt;p&gt;You can install and run Drozer through Docker using a single command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --add-host host.docker.internal:host-gateway withsecurelabs/drozer console connect --server host.docker.internal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates a connection between the Docker image and your local computer. With the connection created, it downloads the withsecurelabs/drozer image and runs the console connect command to connect to your local emulator. Once this is complete you will receive a shell that is connected to the Drozer agent! You are now up and running with Drozer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn More About Android Penetration Testing with OliveStem
&lt;/h2&gt;

&lt;p&gt;Want to learn more about Drozer and Android penetration testing? Check out our &lt;a href="https://olivestem.net/courses/android-penetration-testing"&gt;Android penetration testing course&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>android</category>
      <category>security</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding Heap Memory Allocation in C - Malloc and Free</title>
      <dc:creator>Scott</dc:creator>
      <pubDate>Sun, 27 Aug 2023 22:24:28 +0000</pubDate>
      <link>https://forem.com/olivestem/understanding-heap-memory-allocation-in-c-malloc-and-free-32hd</link>
      <guid>https://forem.com/olivestem/understanding-heap-memory-allocation-in-c-malloc-and-free-32hd</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/UPaIAbUklSM"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Malloc and Free are the most common functions used for heap memory management. These functions provide the following advantages over sbrk and brk:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They are standardized as part of the C language.&lt;/li&gt;
&lt;li&gt;They are easier to use with threaded programs.&lt;/li&gt;
&lt;li&gt;They provide a simple interface for memory to be allocated, especially in smaller units.&lt;/li&gt;
&lt;li&gt;Allow the arbitrary deallocation of blocks of memory.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Malloc takes in a size as an argument and returns a pointer to the newly allocated block of memory that is of the size specified in the argument. The allocated memory is not initialized. &lt;/p&gt;

&lt;p&gt;The free function takes in a pointer to a block of allocated memory. The function will then add the block of memory to a list of free blocks, which are recycled by future malloc calls.&lt;/p&gt;

&lt;p&gt;When malloc is called, it will generally complete the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if there is a block of memory in the list of free blocks large enough to allocate for the memory request.&lt;/li&gt;
&lt;li&gt;If no free blocks are available, allocate a new block of memory to use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Based on this process, it is important to note that when malloc is called, it does not always increase the program break. This is because in some cases, the memory may be available in the list of free blocks, meaning no new memory needs to be allocated to the heap. &lt;/p&gt;

&lt;p&gt;With this basic understanding of malloc, let's look at a simple example of a malloc in C.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[]){&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;newS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;newS&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newS&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a struct named s which contains a single property, value. To be able to use this struct, we need to allocate some memory for it. To do this, we use a malloc as shown in the first line of the main function. The argument for our malloc call is &lt;em&gt;sizeof(struct s)&lt;/em&gt;. This operation determines how much memory is required to contain our struct s, allocates a memory block of this size, and returns a pointer of the block to the variable &lt;em&gt;newS&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;With &lt;em&gt;newS&lt;/em&gt; initialized, we can start to work with its property &lt;em&gt;value&lt;/em&gt;. To access the property, we use the pointer notation &lt;em&gt;newS-&amp;gt;value&lt;/em&gt;. The line after the allocation shows how to set the value, and the print shows how to access the value.&lt;/p&gt;

&lt;p&gt;At the end of the program, we no longer need the memory assocaited with &lt;em&gt;newS&lt;/em&gt;. We make a call to the &lt;em&gt;free&lt;/em&gt; function, providing &lt;em&gt;newS&lt;/em&gt; as an argument. This will add the memory associated with &lt;em&gt;newS&lt;/em&gt; to the free list, allowing it be used for future malloc calls. &lt;/p&gt;

&lt;p&gt;It’s important to note that when the process terminates, all of its memory is returned to the system, so free isn’t actually required for this simple program. Free is generally used in more complex programs to make the program more readable and to help with efficient memory management.&lt;/p&gt;

&lt;p&gt;You now know the basics of working with malloc and free! &lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>linux</category>
    </item>
    <item>
      <title>Printing in the BIOS with x86 NASM</title>
      <dc:creator>Scott</dc:creator>
      <pubDate>Sun, 27 Aug 2023 20:11:39 +0000</pubDate>
      <link>https://forem.com/olivestem/printing-in-the-bios-with-x86-nasm-67n</link>
      <guid>https://forem.com/olivestem/printing-in-the-bios-with-x86-nasm-67n</guid>
      <description>&lt;p&gt;In my last article, I showed how you could create a simple bootloader. In this article, we will extend the bootloader to allow it to print a message in the BIOS. To check out the previous article, &lt;a href="https://dev.to/olivestem/building-a-simple-bootloader-in-nasm-x86-6nj"&gt;Click here!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zT4WyIgZGow"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;BIOS allows for simple inputs and outputs, as the name suggests. Using BIOS interrupts, we can output a simple message to the screen to confirm that our code is working. To start, we are going to initialize our registers and stack memory to use in our program. We do this by adding some code to the main label.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We start by moving 0 into ax. We do this because we can’t move immediate data into registers like ds, so the only way to get 0 into them is to put 0 in another register first. Next, we set to the DS, ES, and SS registers to 0. The DS register sets the start address of the data segment, the ES register sets the start point of the extra segment, and the SS register sets the start address of the stack. After initializing these registers, we move 0x7C00 into the stack pointer so it is able to grow below the operating system.&lt;/p&gt;

&lt;p&gt;With these registers initialized, we can now start the process of printing data to the screen. To start, here is the full code to print a message from the bootloader:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let’s discuss each of the added lines of code in detail. At the bottom of our code, we defined the text we want to print to the screen. The variable that contains the text is called os_boot_message. It contains the message Our OS has booted! After the message, we add 0x0D and 0x0A, which are the characters for a new line. Finally, we add a 0, which is used to find the end of the string when we print it. In the main label, we load the os_boot_message variable into the si register. The si register is known as the source index register, and it is generally used for string operations. With the string loaded, we then call the print function.&lt;/p&gt;

&lt;p&gt;The print function starts by pushing si, ax, and bx onto the stack to preserve their values. Next, we enter the print_loop label. This loop will first load a byte from the si register using the LODSB instruction. The next OR instruction checks if the value loaded was a 0, indicating the end of the string. The JZ instruction will jump to the done_print label if the value loaded was a 0. If the value was not zero, the value 0x0E is loaded into the ah register. This value tells the BIOS we are trying to print to the screen. Next, 0 is moved into BH. The BH register tells the BIOS the page number. This is generally set to 0 except for cases where you need to do double-buffering to draw to an off-screen page. Finally, we send an interrupt to the BIOS using INT 0x10, which tells the BIOS we want to write a character to the screen.&lt;/p&gt;

&lt;p&gt;This print loop continues until a 0 character is reached, at which point ax, si, and bx are popped from the stack and the function returns to the caller. &lt;/p&gt;

&lt;p&gt;With this complete, you can now build and run your operating system once again. Use make to build the image, then use &lt;code&gt;qemu-system-i386 -fda build/main.img&lt;/code&gt; to load it. The result will be something similar to below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_w2QCIdT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8h6q6gdq5ybuikw67zdi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_w2QCIdT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8h6q6gdq5ybuikw67zdi.png" alt="Image of the BIOS output" width="624" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have create a basic bootloader than can print data onto the screen. With this you are well on your way to creating a functional operating system with x86!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>assembly</category>
      <category>learning</category>
    </item>
    <item>
      <title>Building a Simple Bootloader in NASM x86</title>
      <dc:creator>Scott</dc:creator>
      <pubDate>Sun, 27 Aug 2023 00:20:47 +0000</pubDate>
      <link>https://forem.com/olivestem/building-a-simple-bootloader-in-nasm-x86-6nj</link>
      <guid>https://forem.com/olivestem/building-a-simple-bootloader-in-nasm-x86-6nj</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/7BqWNT-5hN8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;A bootloader is a program that is responsible for booting a computer. In this tutorial, we will start the process of creating a simple bootloader that halts as soon as the file boots. This tutorial will use x86 assembly with legacy booting Let’s start by creating a file named main.asm, which will contain all our bootloader code. To start creating the bootloader, you will first need to understand a bit about how a computer boots from a drive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics of Legacy Booting
&lt;/h2&gt;

&lt;p&gt;When a computer first boots, it enters the BIOS (basic input output system). The BIOS provides many different utilities and tools that allow operating systems to perform hardware initialization during startup. In legacy booting, the BIOS will search for the operating system on the disk to start the booting process. The BIOS will load the first sector of each bootable device into memory at location 0x7C00. When the BIOS loads the sector, it checks for a special signature, 0xAA55. If this signature if found, the BIOS will start executing code.&lt;/p&gt;

&lt;p&gt;To get our program to start running in BIOS, we need to place the signature 0xAA55 at the location 0x7C00. To do this, we use the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The first line, ORG 0x7C00 tells the assembler to start calculating all memory offsets at 0x7C00. We do this because the memory location BIOS uses is 0x7C00. The next line, BITS 16, tells the assembler to output 16-bit instructions from the code. We use 16 bits because the processor will always start thinking it is in 16-bit mode. We can later move our code to 32-bit or 64-bit if required.&lt;/p&gt;

&lt;p&gt;The main label of our program has a single instruction inside, hlt. This instruction will cause the CPU to halt, preventing it from executing any further instructions. The halt instruction lasts until the next external interrupt occurs, so we need to ensure that the system remains halted if this does occur. To keep the system halted, we create a label named .halt, which contains a single instruction that jumps back to the label. This will create an infinite loop, preventing the program from progressing further or ending. &lt;/p&gt;

&lt;p&gt;The last two lines are where we add the 0AA55 signature for the BIOS. The disk we are using for our boot image has a block size of 512. We want to place the signature in the last two bytes of the disk. The code $-$$ gives us the size of our program so far in bytes. If we take 510-($-$$), we get the number of bytes left until we reach byte 510. We then use the times instruction to place a value of 0 into each byte before 510. After doing this, we will be at byte 510, ready to write the signature. To write the signature, we use DW (which defines data that is 2 bytes of size), providing a value of 00AA55h, which is the required signature. &lt;/p&gt;

&lt;p&gt;With this, we are now ready to create a disk image with our simple operating system bootloader!&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the Disk Image
&lt;/h2&gt;

&lt;p&gt;To build the disk image, create a makefile in your directory. I have placed my main.asm file in a folder called src. I have also created another folder named build to contain all our build data. Inside the makefile, put the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This makefile starts by defining a few constants. The constant ASM contains our assembler, NASM. The SRC_DIR constant contains the folder of our source code, src. The BUILD_DIR constant contains the folder to place our build data, build. The first rule in the makefile moves the main.bin file resulting from assembling main.asm into an img file. It then pads the file to make it 1.44MB, which is the minimum size required for this to be considered a valid disk. The second rule runs NASM against the main.asm file to create the main.bin file for the disk.&lt;/p&gt;

&lt;p&gt;With the makefile defined, run make to test a build. You should now have a file named main.img in the build directory. To try booting off the disk, simply provide it to any emulator. For example, qemu can boot this disk using the command: qemu-system-i386 -fda build/main.img. When you do this, you will see a result like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmi64729ea16rlipt8l8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmi64729ea16rlipt8l8p.png" alt="An image of the operating system loaded in qemu"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have created a simple bootloader for an operating system. With this solid foundations, you can start to develop more functionality for the system. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>assembly</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Understanding Heap Memory Allocation in C - sbrk and brk</title>
      <dc:creator>Scott</dc:creator>
      <pubDate>Mon, 21 Aug 2023 22:01:35 +0000</pubDate>
      <link>https://forem.com/olivestem/understanding-heap-memory-allocation-in-c-sbrk-and-brk-3coj</link>
      <guid>https://forem.com/olivestem/understanding-heap-memory-allocation-in-c-sbrk-and-brk-3coj</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/NDKArv9WAlQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this tutorial, we will explore process memory to better understand how memory is allocated on the heap in C. To start, let's take a look at the general memory structure of a process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stack Memory
&lt;/h2&gt;

&lt;p&gt;When a function is called, the stack memory grows to accommodate the stack frame of the function. When the function returns, the stack memory shrinks in size as it pops off the stack frame of the function. The stack memory starts at the high end of memory and grows downwards, towards the heap.&lt;/p&gt;

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

&lt;p&gt;A special purpose register in your processor called a stack pointer tracks the current top of the stack. Each stack frame on the stack contains function arguments, local variables, and call linkage information such as return addresses for the function. Since functions can be called inside functions, we can often see multiple frames on a stack.&lt;/p&gt;

&lt;p&gt;In C, we typically don’t need to worry about stack memory in too much detail. Knowing about how function frames work provides important context, but we don’t usually directly interact with the stack. When we allocate memory in C, we are interacting with the heap, which we will look at next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Heap Memory
&lt;/h2&gt;

&lt;p&gt;The heap is a dynamic area of memory that grows towards the stack. When you allocate memory in a program, that memory is allocated on the heap. To allocate memory, we usually use &lt;strong&gt;malloc&lt;/strong&gt;, which is based on another set of functions, &lt;strong&gt;brk()&lt;/strong&gt; and &lt;strong&gt;sbrk()&lt;/strong&gt;. These functions work by resizing the program break, which is the memory location that represents the end of heap memory and the start of unallocated memory. &lt;/p&gt;

&lt;h3&gt;
  
  
  brk and sbrk
&lt;/h3&gt;

&lt;p&gt;Resizing the heap of a process is a simple task. We just need to tell the kernel to adjust the location of the program break (which is defined as the current top of the heap). Since the memory that lies passed the program break is unallocated memory, we can move the break into the unallocated memory to allocate some new memory for our process. There are two functions associated with this process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;brk(void *end_data_segment)&lt;/strong&gt;: Sets the program break to the location specified by &lt;em&gt;end_data_segment&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;sbrk(intptr_t increment)&lt;/strong&gt;: Increments the size of the program break by &lt;em&gt;increment&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To see how these functions work, let’s look at a simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[]){&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbrk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, provide an increment of 0 to sbrk. When an increment of 0 is provided to sbrk, the current break address is returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7p9mqzczik7dhz8cgr6r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7p9mqzczik7dhz8cgr6r.png" alt="The output of the first code snippet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you provide a value other than 0 to &lt;em&gt;sbrk&lt;/em&gt;, you get the previous break address, meaning the address before the break was changed. We can see this effect using a few increments in a row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[]){&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbrk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First increment of 0x5: %p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;currentBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbrk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Second increment of 0x5: %p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;currentBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbrk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Third increment of 0x5: %p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;currentBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbrk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fourth increment of 0x5: %p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;currentBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbrk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fifth increment of 0x5: %p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;currentBreak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this set of increments, you will get a result similar to the following figure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5qpnmr4docii5ho1tk24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5qpnmr4docii5ho1tk24.png" alt="The output of the second code snippet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that the break between the first and second increment is larger than the 0x5 we specified. This is because printf allocates memory to use as a buffer for stdout. When this happens initially, we see a large change in the heap break. The third, fourth, and fifth increment move consistently by the increment, 0x5, because no additional data is allocated on the heap between them. &lt;/p&gt;

&lt;p&gt;In most cases, we don’t use &lt;em&gt;sbrk&lt;/em&gt; and &lt;em&gt;brk&lt;/em&gt; directly to adjust the heap in C. Instead, we use a more user-friendly version of this process known as &lt;em&gt;malloc&lt;/em&gt; and &lt;em&gt;free&lt;/em&gt;. In the next article, we will explore these functions in more detail!&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
