There are three objectives of the assignment:
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:
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 Friday, May 20. I will test your program on linprog3 and other Linux machines.