The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Hello, I need some help for my assigment
The attachment is used to start running 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:
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]
Â
#include <sys/mman.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>void print_file(char *filename){int fd = open(filename,O_RDONLY);if(fd == -1){printf("Cannot open file %s...\n",filename);exit(1);}char buffer[4096];int bytes;while((bytes = read(fd,buffer,4096)) > 0){write(1,buffer,bytes);}}int main(){printf("Before we create a memory map, here's a view of this process's currentmemory map:\n");print_file("/proc/self/maps");int fd = open("/bin/bash",O_RDONLY);char *ptr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);if(ptr == NULL){printf("Cannot mmap!\n");exit(1);}printf("Okay, we have mapped '/bin/bash' in, let's print its first fewbytes:\n");*(ptr+4) = 0;printf("\t Here's the elf: '%s'!\n",ptr+1);printf("\n=-=-=-=-=-=\n\n");printf("Okay, we have created a memory map, here's the new view of thisprocess's current memory map:\n");print_file("/proc/self/maps");}
-----------