History

tccrun: 'selinux' mmap: use only one mapping

In the previous implementation, the rx mapping was never used. Therefor it is assumed that it is not needed. With only one mapping there is no reason to use a real /tmp/.xxxx file either as we can use an anonymous mapping.

grischka committed 9 years ago

Commit ea2c36c1 parent 5420bb81 file · +19 −38Browse files

Unified · Split

tccrun.c+19−38
@@ -60,7 +60,7 @@ static void win64_del_function_table(void *); · 59 unmodified lines
6060
6161 LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
6262 {
63− int size; void *mem;
63+ int size;
6464
6565 if (TCC_RELOCATE_AUTO != ptr)
6666 return tcc_relocate_ex(s1, ptr);
@@ -70,35 +70,17 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) · 3 unmodified lines
7070 return -1;
7171
7272 #ifdef HAVE_SELINUX
73− { /* Use mmap instead of malloc for Selinux. Ref:
74− http://www.gnu.org/s/libc/manual/html_node/File-Size.html */
75−
76− char tmpfname[] = "/tmp/.tccrunXXXXXX";
77− int fd = mkstemp (tmpfname);
78− void *wr_mem;
79−
80− unlink (tmpfname);
81− ftruncate (fd, size);
82−
83− wr_mem = mmap (NULL, size, PROT_READ|PROT_WRITE,
84− MAP_SHARED, fd, 0);
85− if (wr_mem == MAP_FAILED)
86− tcc_error("/tmp not writeable");
87− mem = mmap (NULL, size, PROT_READ|PROT_EXEC,
88− MAP_SHARED, fd, 0);
89− if (mem == MAP_FAILED)
90− tcc_error("/tmp not executable");
91−
92− tcc_relocate_ex(s1, wr_mem);
93− dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
94− dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, wr_mem);
95− dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
96− }
73+ /* Use mmap instead of malloc for Selinux. */
74+ ptr = mmap (NULL, size, PROT_READ|PROT_WRITE,
75+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
76+ if (ptr == MAP_FAILED)
77+ tcc_error("tccrun: could not map memory");
78+ dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
9779 #else
98− mem = tcc_malloc(size);
99− tcc_relocate_ex(s1, mem); /* no more errors expected */
100− dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
80+ ptr = tcc_malloc(size);
10181 #endif
82+ tcc_relocate_ex(s1, ptr); /* no more errors expected */
83+ dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr);
10284 return 0;
10385 }
10486
@@ -108,13 +90,12 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 3 unmodified lines
10890
10991 for (i = 0; i < s1->nb_runtime_mem; ++i) {
11092 #ifdef HAVE_SELINUX
111− int size = (int)(addr_t)s1->runtime_mem[i];
112− munmap(s1->runtime_mem[++i], size);
113− munmap(s1->runtime_mem[++i], size);
93+ unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++];
94+ munmap(s1->runtime_mem[i], size);
11495 #else
115−# ifdef _WIN64
96+#ifdef _WIN64
11697 win64_del_function_table(*(void**)s1->runtime_mem[i]);
117−# endif
98+#endif
11899 tcc_free(s1->runtime_mem[i]);
119100 #endif
120101 }
@@ -264,19 +245,19 @@ static void set_pages_executable(void *ptr, unsigned long length) · 143 unmodified lines
264245 unsigned long old_protect;
265246 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
266247 #else
248+ void __clear_cache(void *beginning, void *end);
249+ addr_t start, end;
267250 #ifndef PAGESIZE
268251 # define PAGESIZE 4096
269252 #endif
270− addr_t start, end;
271253 start = (addr_t)ptr & ~(PAGESIZE - 1);
272254 end = (addr_t)ptr + length;
273255 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
274256 if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
275257 tcc_error("mprotect failed: did you mean to configure --with-selinux?");
276−#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
277− { extern void __clear_cache(void *beginning, void *end);
278− __clear_cache(ptr, (char *)ptr + length); }
279−#endif
258+# if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
259+ __clear_cache(ptr, (char *)ptr + length);
260+# endif
280261 #endif
281262 }
282263

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t