CNT5605 - Fall 2015
Assignment 2
Your program submission is due by Thursday, September 10.

Simple C program to demonstrate system call understanding

There are three objectives of the assignment:

  1. Demonstrate that you can write a simple C program.
  2. Demonstrate that you understand simple formatted i/o using libc.
  3. Demonstrate that you can make a simple system call and show that the action desired is what happened.

Your goal is to write a C program that looks at its own memory mappings in /proc/self/maps, and unmaps any of the mappings that have permission settings that do not allow any read, write, or execute permission.

For instance, here are the memory mappings for a bash process; there are several mappings that are inaccessible to that process since they don't allow read, write, or execute on the mapping:

00400000-004ef000 r-xp 00000000 08:11 3670022                            /bin/bash
006ef000-006f0000 r--p 000ef000 08:11 3670022                            /bin/bash
00d9e000-00fb2000 rw-p 00000000 00:00 0                                  [heap]
7f86073fa000-7f8607405000 r-xp 00000000 08:11 2363469                    /lib/x86_64-linux-gnu/libnss_files-2.19.so
7f8607405000-7f8607604000 ---p 0000b000 08:11 2363469                    /lib/x86_64-linux-gnu/libnss_files-2.19.so     # Here is one
7f8607604000-7f8607605000 r--p 0000a000 08:11 2363469                    /lib/x86_64-linux-gnu/libnss_files-2.19.so
7f8607605000-7f8607606000 rw-p 0000b000 08:11 2363469                    /lib/x86_64-linux-gnu/libnss_files-2.19.so
7f8607606000-7f8607611000 r-xp 00000000 08:11 2363596                    /lib/x86_64-linux-gnu/libnss_nis-2.19.so
7f8607611000-7f8607810000 ---p 0000b000 08:11 2363596                    /lib/x86_64-linux-gnu/libnss_nis-2.19.so       # Here is another one
7f8607810000-7f8607811000 r--p 0000a000 08:11 2363596                    /lib/x86_64-linux-gnu/libnss_nis-2.19.so
7f8607829000-7f8607a28000 ---p 00017000 08:11 2363567                    /lib/x86_64-linux-gnu/libnsl-2.19.so           # And another
7f8607a28000-7f8607a29000 r--p 00016000 08:11 2363567                    /lib/x86_64-linux-gnu/libnsl-2.19.so
7f8607a2c000-7f8607a35000 r-xp 00000000 08:11 2363566                    /lib/x86_64-linux-gnu/libnss_compat-2.19.so
7f8607a35000-7f8607c34000 ---p 00009000 08:11 2363566                    /lib/x86_64-linux-gnu/libnss_compat-2.19.so    # And one more
7f8607c34000-7f8607c35000 r--p 00008000 08:11 2363566                    /lib/x86_64-l

To do this, you will need to use the system call munmap(2), which allows a process to remove one of its current mappings.

Please name your program delete-mmaps.c. The program should do the following:

  1. First, print out all of its current memory mappings that are found in /proc/self/maps.
  2. Second, walk through each line of /proc/self/maps using fopen(3) and fscanf(3). For each line:
    • Check to see if the permissions of the mapping are "---p".
    • If so, then attempt to apply munmap(2) to that mapping.
    • Then print out the line, the arguments that you supplied to munmap, and print the result code. If it shows an error, print out the error also.
  3. Finally, again print out the entire contents of /proc/self/maps, which should no longer contain any mappings with "---p" permissions.

For instance, here is a run of my reference implementation on linprog3:

  % ./delete-mmaps


  ====== FILE /proc/self/maps ======
  00400000-00401000 r-xp 00000000 00:1d 156663809                          /home/faculty/langley/test-read-maps
  00600000-00601000 rw-p 00000000 00:1d 156663809                          /home/faculty/langley/test-read-maps
  7f0b7e4c8000-7f0b7e66e000 r-xp 00000000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e66e000-7f0b7e86e000 ---p 001a6000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e86e000-7f0b7e872000 r--p 001a6000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e872000-7f0b7e874000 rw-p 001aa000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e874000-7f0b7e878000 rw-p 00000000 00:00 0 
  7f0b7e878000-7f0b7e89a000 r-xp 00000000 08:02 2240731                    /lib64/ld-2.20.so
  7f0b7ea6e000-7f0b7ea71000 rw-p 00000000 00:00 0 
  7f0b7ea98000-7f0b7ea9a000 rw-p 00000000 00:00 0 
  7f0b7ea9a000-7f0b7ea9b000 r--p 00022000 08:02 2240731                    /lib64/ld-2.20.so
  7f0b7ea9b000-7f0b7ea9c000 rw-p 00023000 08:02 2240731                    /lib64/ld-2.20.so
  7f0b7ea9c000-7f0b7ea9d000 rw-p 00000000 00:00 0 
  7ffc16b06000-7ffc16b27000 rw-p 00000000 00:00 0                          [stack]
  7ffc16b8f000-7ffc16b91000 r--p 00000000 00:00 0                          [vvar]
  7ffc16b91000-7ffc16b93000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]


  Scanning /proc/self/maps to find mmaps with ---p permissions...

  Found one at address start = 0x7f0b7e66e000, address end = 0x7f0b7e86e000, length = 2097152, permissions = ---p
  Attempting to unmap(0x7f0b7e66e000,2097152)...
  And the return value was 0


  ====== FILE /proc/self/maps ======
  00400000-00401000 r-xp 00000000 00:1d 156663809                          /home/faculty/langley/test-read-maps
  00600000-00601000 rw-p 00000000 00:1d 156663809                          /home/faculty/langley/test-read-maps
  00ff3000-01014000 rw-p 00000000 00:00 0                                  [heap]
  7f0b7e4c8000-7f0b7e66e000 r-xp 00000000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e86e000-7f0b7e872000 r--p 001a6000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e872000-7f0b7e874000 rw-p 001aa000 08:02 2240721                    /lib64/libc-2.20.so
  7f0b7e874000-7f0b7e878000 rw-p 00000000 00:00 0 
  7f0b7e878000-7f0b7e89a000 r-xp 00000000 08:02 2240731                    /lib64/ld-2.20.so
  7f0b7ea6e000-7f0b7ea71000 rw-p 00000000 00:00 0 
  7f0b7ea97000-7f0b7ea9a000 rw-p 00000000 00:00 0 
  7f0b7ea9a000-7f0b7ea9b000 r--p 00022000 08:02 2240731                    /lib64/ld-2.20.so
  7f0b7ea9b000-7f0b7ea9c000 rw-p 00023000 08:02 2240731                    /lib64/ld-2.20.so
  7f0b7ea9c000-7f0b7ea9d000 rw-p 00000000 00:00 0 
  7ffc16b06000-7ffc16b27000 rw-p 00000000 00:00 0                          [stack]
  7ffc16b8f000-7ffc16b91000 r--p 00000000 00:00 0                          [vvar]
  7ffc16b91000-7ffc16b93000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

I would suggest that you start with the program test-mmap.c, which should give you a running start on the assignment, but please feel free to start ab initio if you prefer. Please do not use C++ (which wouldn't help you much anyway for this kind of systems programming.)

Also, here is a copy of my reference version: delete-mmaps.

No journal is due, but you must submit your C code (not your program binary) on Blackboard by 11:59pm on Thursday, September 10th. I will test your program on linprog3 and other Linux machines.