Sitemap
Bobby Yang

Bobby Yang

Software Engineer @ Wonder Workshop

/proc

4 min readJun 18, 2018

Preface:

The /proc filesystem is an in-memory file system that is created by the kernel of most unix-like operating systems. This means the “files” are just virtual memory that’s mapped to a folder, usually the /proc directory. When you open and try to read one of these files, because the files aren’t really there, they are created as you open them by the kernel.

After doing a quick ls /proc and ps aux, you can see that the processes listed in the process list have directories in the /proc folder. You can also see files likecpuinfo and meminfo along with plenty of other important looking files.

Press enter or click to view image in full size
ps aus (left) and ls /proc (right)

Each of the process directories contains “files” and other “folders” that contain info about the process. In a nut shell, the /proc folder contains basically all the details about everything that’s happening on a unix machine.

In this blog post, I’m going to be focusing mostly on the /proc/{pid}/maps file and the /proc/{pid}/mem file. With these two files, you can easily find, access, and sometimes even change the heap of currently running programs.

For this post, I’ll be using a C program that dynamically allocates a string and then prints the string on a loop. And with the maps file and the mem file, be able to modify the dynamically allocated string (in the heap) as it is still running.

Quick refresher. The stack memory is allocated on compilation so when program runs, all of the variables stored in the stack are already there and just needs to be accessed. The heap, on the other hand, is allocated on runtime and is where all of the dynamically allocated variables are.

Simple example

Press enter or click to view image in full size
stack variables vs heap variables

/proc/{pid}/maps

The maps file shows the contents of pid’s memory mapped out the same way it is in the actual process. However, this file can’t just be “opened”, even if looks like you have permissions.

Press enter or click to view image in full size
maps file and 0 size

annnndddddddd it’s a 0 size file? Well, once again, due to the nature of virtual files, they don’t have a predefined size and content. It’s generated as you read it, so there can’t be a size.

Get Bobby Yang’s stories in your inbox

Join Medium for free to get updates from this writer.

But if you try to vim or cat the file, you’ll probably see something like this.

Press enter or click to view image in full size
maps file

The screenshot was pretty big so I cut it down to the important part. Like mentioned before, the maps file maps out the memory of the process. So we can tell that the heap is located from memory address 0x00c37000 to 0x00c5800 .

With this knowledge, we can then go on to actually read the heap of the process and modify it.

/proc/{pid}/mem

This file contains the process memory, and with the memory location of the process’s heap, you can read the memory of the process by getting the offset, and read from the start address to the end address (0x00c37000 to 0x00c5800) .

Here’s a super simple python script example that shows how to do this.

Press enter or click to view image in full size

Basically, what the program does is take a pid that is passed in as a command line argument and opens the corresponding maps file and mem file. Then goes to read the maps file until it finds the line where the heap memory where tells us the start and end of the heap. Then you seek to that memory address in the mem file and read from the offset the number of end — start bytes.

You can also modify the heap as well if you want by using write . Just like writing to any other file.

Quick recap.

  1. The /proc filesystem isn’t a real, physical file system. It’s just a part of the virtual memory that’s mapped and mounted to psuedo-files in (normally) /proc directory.
  2. The /proc directory contains many files about the kernel itself along with information about processes that are running.
  3. The maps file is a file that maps the memory locations that a process is using. (you can use that file to directly read parts of memory the process is using by starting from the offset and reading to the end of that part of memory)
  4. The mem file is a file that contains the process memory. You can use the memory address locations in the maps file to read what’s stored there in this file.

--

--

Bobby Yang
Bobby Yang

Written by Bobby Yang

Software Engineer @ Wonder Workshop