Sunday 22 October 2017

Memory protection

mmap( ) Dynamically map pages of memory into the process’ memory context
munmap( ) Unmap pages of memory from the process memory context.
mprotect( ) Change protection of memory pages.

#include
#include
/****************************************************************************
* This application illustrates the usage of the mmap(),mprotect(),and
munmap()
/**********************************
int main ()
 {
 int mib[3];     /*MIB array for sysctl() */
 size_t pgSize;      /*variable to store page size */
 size_t sz = sizeof (pgSize);     /* size of pgSize variable */
 size_t bufSize;     /* size of buffer */
 char * pBuf;      /* buffer */


 /* get "hw.mmu.pageSize" info */
 mib[0] = CTL_HW;
 mib[1] = HW_MMU;
 mib[2] = HW_MMU_PAGESIZE;
 if (sysctl (mib, 3, &pgSize, &sz, NULL, 0) != 0)
exit (1);


 /* buffer size is 4 pages */
 bufSize = 4 * pgSize;


 /* request mapped memory for a buffer */
 pBuf = mmap (NULL, bufSize, (PROT_READ | PROT_WRITE),
 (MAP_PRIVATE | MAP_ANON), MAP_ANON_FD, 0);

 /* check for error */
 if (pBuf == MAP_FAILED)
 exit (1);


 /* write protect the first page */
 if (mprotect (pBuf, pgSize, PROT_READ) !
= 0)
 {
/*
 * no need to call unmap before exiting as all memory mapped for
 * a process is automatically released.
 */
exit (1);
}

 /*
 * Unmap the buffer; the unmap() has to be called for the entire buffer.
 * Note that unmapping before exit() is not necesary; it is shown here
 * only for illustration purpose.
 */
 if (munmap (pBuf, bufSize) != 0)
exit (1);
 printf ("execution succeded\n");
 exit (0);
 }

===========
VxWorks all applications are run in kernel mode, as there is no difference between user and kernel modes. That gives all application access to the whole address space. This can lead to problems considering memory protection.
To solve this issue in VxWorks 5 and earlier the solution is to include virtual memory library called VxVMI, allowing applications to mark data areas private and make it only accessible by the tasks it belongs to. The problem with this approach is that you will need a memory management unit to use this library.
VxWorks 6 has a different approach but is still supporting the approach above for backward compatibility. VxWorks 6 are now supporting user-mode and kernels-modes. The kernel runs in kernel mode and VxWorks real-time processes (RTPs) are running in user-mode. The kernel is protected from the RTPs and the RTPs are protected from each other.
==================

No comments:

Post a Comment