Skip to the content ForoHackers
Log inJoin

Reading /proc/PID/maps: a process's address space in ten lines

Binary analysisLinuxMemory management
#975 · hexdump_hermit · · Report

One of the most useful files for understanding what a process really looks like:

$ head -8 /proc/self/maps
5593a1c2e000-5593a1c30000 r--p 00000000 fd:01 1310722   /usr/bin/cat
5593a1c30000-5593a1c35000 r-xp 00002000 fd:01 1310722   /usr/bin/cat
5593a1c35000-5593a1c38000 r--p 00007000 fd:01 1310722   /usr/bin/cat
5593a1c38000-5593a1c3a000 rw-p 00009000 fd:01 1310722   /usr/bin/cat
5593a2d16000-5593a2d37000 rw-p 00000000 00:00 0         [heap]
7f2b5c000000-7f2b5c1a0000 r-xp 00000000 fd:01 1315008   /usr/lib/x86_64-linux-gnu/libc.so.6
7ffd1d5a2000-7ffd1d5c3000 rw-p 00000000 00:00 0         [stack]
7ffd1d5f7000-7ffd1d5fb000 r--p 00000000 00:00 0         [vvar]

Columns: address range, permissions (r, w, x, and p private or s shared), file offset, device, inode, path.

What to look for:

  • The binary appears several times, one mapping per segment, each with different permissions. Code is r-x, constants r--, data rw-.
  • No mapping is both w and x. That is W^X, and when you do see rwx it deserves a question.
  • No path and inode 0 means anonymous memory: heap, stack, or whatever mmap handed out.
  • Run it twice. The addresses change every time. That is ASLR.

/proc/PID/smaps adds resident size, dirty pages and more per mapping. Great for "where did my RAM go?".

#1301 · rubber_duck_supervisor · · Report

Beginner question, then: if the binary is mapped four times, is the file loaded into memory four times?

#1302 · hexdump_hermit · · Report

No. Four views onto the same file pages in the page cache. Read-only ones are shared between every process running that binary. Only the rw-p ones get private copies, and only for pages actually written (copy-on-write).

#1303 · heap_spray_and_pray · · Report

The defensive reading: the whole point of the permission columns is that data and code are separate things, so a bug that lets someone write bytes should not let them run those bytes. Whenever you see an rwx mapping in a process you did not expect, find out who asked for it. JITs have a good reason. Most things do not.

#1304 · null_terminated_hopes · · Report

Run it twice and compare, they said. I did. My [heap] moved by a few megabytes. I took it personally.

Log in or join to reply.