summaryrefslogtreecommitdiff
path: root/src/lib/evil
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2019-08-05 08:47:19 -0400
committerMike Blumenkrantz <zmike@samsung.com>2019-08-05 08:47:19 -0400
commitfb023ee6d2c7859446960512433bd633c77a420a (patch)
treea68795ad061630d3e526183c6dffff6092242c26 /src/lib/evil
parent94b05f15e6e0548517289092ebb16c4cabd3c9fa (diff)
evil: add mprotect() and fix a bit mmap(). Elm_test is working
Test Plan: compilation aand elm_test working Reviewers: raster, cedric, zmike Reviewed By: zmike Subscribers: #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D9493
Diffstat (limited to 'src/lib/evil')
-rw-r--r--src/lib/evil/evil_mman.c110
-rw-r--r--src/lib/evil/sys/mman.h22
2 files changed, 86 insertions, 46 deletions
diff --git a/src/lib/evil/evil_mman.c b/src/lib/evil/evil_mman.c
index 83b9ee7..b1bcf35 100644
--- a/src/lib/evil/evil_mman.c
+++ b/src/lib/evil/evil_mman.c
@@ -6,12 +6,42 @@
6#include <stdio.h> 6#include <stdio.h>
7#include <sys/types.h> 7#include <sys/types.h>
8#include <unistd.h> 8#include <unistd.h>
9#include <errno.h>
9 10
10#include <io.h> 11#include <io.h>
11 12
12#include "sys/mman.h" 13#include "sys/mman.h"
13#include "evil_private.h" 14#include "evil_private.h"
14 15
16/*
17 * Possible values
18 * PAGE_EXECUTE_READ (equivalent to PAGE_EXECUTE_WRITECOPY)
19 * PAGE_EXECUTE_READWRITE
20 * PAGE_READONLY (equivalent to PAGE_WRITECOPY)
21 * PAGE_READWRITE
22 */
23static DWORD
24_evil_mmap_protection_get(int prot)
25{
26 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
27 return 0xffffffff;
28
29 if (prot & PROT_WRITE)
30 {
31 if (prot & PROT_EXEC)
32 return PAGE_EXECUTE_READWRITE;
33 else
34 return PAGE_READWRITE;
35 }
36 else
37 {
38 if (prot & PROT_EXEC)
39 return PAGE_EXECUTE_READ;
40 else
41 return PAGE_READONLY;
42 }
43}
44
15 45
16/***** API *****/ 46/***** API *****/
17 47
@@ -25,51 +55,39 @@ mmap(void *addr EVIL_UNUSED,
25 off_t offset) 55 off_t offset)
26{ 56{
27 HANDLE fm; 57 HANDLE fm;
28 DWORD protect = PAGE_NOACCESS; 58 DWORD protect;
29 DWORD acs = 0; 59 DWORD acs = 0;
30 HANDLE handle; 60 HANDLE handle;
31 void *data; 61 void *data;
32 62
33 /* support only MAP_SHARED */ 63 /* get protection */
34 if (!(flags & MAP_SHARED)) 64 protect = _evil_mmap_protection_get(prot);
65 if (protect == 0xffffffff)
35 return MAP_FAILED; 66 return MAP_FAILED;
36 67
37 if (prot & PROT_EXEC) 68 /* check if the mapping is backed by a file or not */
69 if (fd == -1)
38 { 70 {
39 if (prot & PROT_READ) 71 /* shared memory */
40 { 72 if (!(flags & MAP_ANON) || offset)
41 if (prot & PROT_WRITE) 73 return MAP_FAILED;
42 protect = PAGE_EXECUTE_READWRITE;
43 else
44 protect = PAGE_EXECUTE_READ;
45 }
46 else
47 {
48 if (prot & PROT_WRITE)
49 protect = PAGE_EXECUTE_WRITECOPY;
50 else
51 protect = PAGE_EXECUTE;
52 }
53 } 74 }
54 else 75 else
55 { 76 {
56 if (prot & PROT_READ) 77 if (flags & MAP_ANON)
57 { 78 return MAP_FAILED;
58 if (prot & PROT_WRITE)
59 protect = PAGE_READWRITE;
60 else
61 protect = PAGE_READONLY;
62 }
63 else if (prot & PROT_WRITE)
64 protect = PAGE_WRITECOPY;
65 } 79 }
66 80
67 handle = (HANDLE)_get_osfhandle(fd); 81 if (fd == -1)
68 if (handle == INVALID_HANDLE_VALUE) 82 handle = INVALID_HANDLE_VALUE;
83 else
69 { 84 {
70 fprintf(stderr, "[Evil] [mmap] _get_osfhandle failed\n"); 85 handle = (HANDLE)_get_osfhandle(fd);
71 86 if ((errno == EBADF) && (handle == INVALID_HANDLE_VALUE))
72 return MAP_FAILED; 87 {
88 fprintf(stderr, "[Evil] [mmap] _get_osfhandle failed\n");
89 return MAP_FAILED;
90 }
73 } 91 }
74 92
75 fm = CreateFileMapping(handle, NULL, protect, 0, 0, NULL); 93 fm = CreateFileMapping(handle, NULL, protect, 0, 0, NULL);
@@ -80,21 +98,14 @@ mmap(void *addr EVIL_UNUSED,
80 return MAP_FAILED; 98 return MAP_FAILED;
81 } 99 }
82 100
83 if ((protect & PAGE_READWRITE) == PAGE_READWRITE) 101 if (prot & PROT_WRITE)
84 acs = FILE_MAP_ALL_ACCESS; 102 acs = FILE_MAP_WRITE;
85 else if ((protect & PAGE_WRITECOPY) == PAGE_WRITECOPY)
86 acs = FILE_MAP_COPY;
87#if 0
88 if (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ))
89 acs = FILE_MAP_EXECUTE;
90#endif
91 else if (protect & (PAGE_READWRITE | PAGE_READONLY))
92 acs = FILE_MAP_READ;
93 else 103 else
94 { 104 acs = FILE_MAP_READ;
95 if ((protect & PAGE_WRITECOPY) == PAGE_WRITECOPY) 105 if (prot & PROT_EXEC)
96 acs = FILE_MAP_WRITE; 106 acs |= FILE_MAP_EXECUTE;
97 } 107 if (prot & MAP_PRIVATE)
108 acs |= FILE_MAP_COPY;
98 109
99 data = MapViewOfFile(fm, 110 data = MapViewOfFile(fm,
100 acs, 111 acs,
@@ -126,3 +137,10 @@ munmap(void *addr,
126 137
127 return (res == 0) ? -1 : 0; 138 return (res == 0) ? -1 : 0;
128} 139}
140
141int
142mprotect(void *addr, size_t len, int prot)
143{
144 DWORD old;
145 return VirtualProtect(addr, len, _evil_mmap_protection_get(prot), &old) ? 0 : -1;
146}
diff --git a/src/lib/evil/sys/mman.h b/src/lib/evil/sys/mman.h
index 15f176a..39ebd20 100644
--- a/src/lib/evil/sys/mman.h
+++ b/src/lib/evil/sys/mman.h
@@ -71,6 +71,8 @@ extern "C" {
71#define MAP_SHARED 0x0001 71#define MAP_SHARED 0x0001
72#define MAP_PRIVATE 0x0002 72#define MAP_PRIVATE 0x0002
73#define MAP_FIXED 0x0010 73#define MAP_FIXED 0x0010
74#define MAP_ANON 0x0020
75#define MAP_ANONYMOUS MAP_ANON
74 76
75#define MAP_FAILED ((void *)-1) 77#define MAP_FAILED ((void *)-1)
76 78
@@ -154,6 +156,26 @@ EAPI void *mmap(void *addr,
154EAPI int munmap(void *addr, 156EAPI int munmap(void *addr,
155 size_t len); 157 size_t len);
156 158
159/**
160 * Changes protection for the calling process' address.
161 *
162 * @param addr Pointer to the base address.
163 * @param len Length of the memory.
164 * @param New protection.
165 * @return 0 on success, -1 otherwise.
166 *
167 * Changes protection for the calling process' memory page.
168 * @p addr must be a valid adress in the user space. @p prot
169 * must be compatible with the old protection.
170 *
171 * Conformity: None.
172 *
173 * Supported OS: Windows Vista
174 *
175 * @ingroup Evil_Mman
176 */
177EAPI int mprotect(void *addr, size_t len, int prot);
178
157 179
158#ifdef __cplusplus 180#ifdef __cplusplus
159} 181}