From 3fe9866ffeb04273bc1bfed27bb39e16c1d5ebbf Mon Sep 17 00:00:00 2001 From: "Emilio G. Cota" Date: Wed, 14 Mar 2018 10:37:35 -0400 Subject: [PATCH] osdep: introduce qemu_mprotect_rwx/none Backports commit 5fa64b3130af9a45e7e2a904bde1f8cfb72be5c9 from qemu --- msvc/unicorn/unicorn/unicorn.vcxproj | 1 + msvc/unicorn/unicorn/unicorn.vcxproj.filters | 3 + .../unicorn_static/unicorn_static.vcxproj | 1 + .../unicorn_static.vcxproj.filters | 3 + qemu/include/qemu/osdep.h | 9 +++ qemu/include/sysemu/os-posix.h | 37 ++++++++++ qemu/util/Makefile.objs | 1 + qemu/util/osdep.c | 69 +++++++++++++++++++ 8 files changed, 124 insertions(+) create mode 100644 qemu/include/sysemu/os-posix.h create mode 100644 qemu/util/osdep.c diff --git a/msvc/unicorn/unicorn/unicorn.vcxproj b/msvc/unicorn/unicorn/unicorn.vcxproj index 0293c4a3..6f47956d 100644 --- a/msvc/unicorn/unicorn/unicorn.vcxproj +++ b/msvc/unicorn/unicorn/unicorn.vcxproj @@ -230,6 +230,7 @@ copy $(SolutionDir)..\include\unicorn\*.h $(SolutionDir)distro\include\unicorn\ + diff --git a/msvc/unicorn/unicorn/unicorn.vcxproj.filters b/msvc/unicorn/unicorn/unicorn.vcxproj.filters index 1ad8d713..6b0d2468 100644 --- a/msvc/unicorn/unicorn/unicorn.vcxproj.filters +++ b/msvc/unicorn/unicorn/unicorn.vcxproj.filters @@ -47,6 +47,9 @@ qemu\util + + qemu\util + qemu\util diff --git a/msvc/unicorn/unicorn_static/unicorn_static.vcxproj b/msvc/unicorn/unicorn_static/unicorn_static.vcxproj index 09c691ef..5c42360f 100644 --- a/msvc/unicorn/unicorn_static/unicorn_static.vcxproj +++ b/msvc/unicorn/unicorn_static/unicorn_static.vcxproj @@ -58,6 +58,7 @@ + diff --git a/msvc/unicorn/unicorn_static/unicorn_static.vcxproj.filters b/msvc/unicorn/unicorn_static/unicorn_static.vcxproj.filters index 18e02a11..6cfde79c 100644 --- a/msvc/unicorn/unicorn_static/unicorn_static.vcxproj.filters +++ b/msvc/unicorn/unicorn_static/unicorn_static.vcxproj.filters @@ -183,6 +183,9 @@ qemu\util + + qemu\util + qemu\util diff --git a/qemu/include/qemu/osdep.h b/qemu/include/qemu/osdep.h index f6069225..10783a76 100644 --- a/qemu/include/qemu/osdep.h +++ b/qemu/include/qemu/osdep.h @@ -86,10 +86,16 @@ #include "sysemu/os-win32.h" #endif +#ifdef CONFIG_POSIX +#include "sysemu/os-posix.h" +#endif + #include "glib_compat.h" #include "qemu/typedefs.h" +struct uc_struct; + /* * We have a lot of unaudited code that may fail in strange ways, or * even be a security risk during migration, if you disable assertions @@ -242,6 +248,9 @@ QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x))) #endif +int qemu_mprotect_rwx(struct uc_struct *uc, void *addr, size_t size); +int qemu_mprotect_none(struct uc_struct *uc, void *addr, size_t size); + void *qemu_try_memalign(size_t alignment, size_t size); void *qemu_memalign(size_t alignment, size_t size); void *qemu_anon_ram_alloc(size_t size, uint64_t *align); diff --git a/qemu/include/sysemu/os-posix.h b/qemu/include/sysemu/os-posix.h new file mode 100644 index 00000000..8b26036c --- /dev/null +++ b/qemu/include/sysemu/os-posix.h @@ -0,0 +1,37 @@ +/* + * win32 specific declarations + * + * Copyright (c) 2003-2008 Fabrice Bellard + * Copyright (c) 2010 Jes Sorensen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef QEMU_OS_POSIX_H +#define QEMU_OS_POSIX_H + +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/qemu/util/Makefile.objs b/qemu/util/Makefile.objs index 616d87a2..196c5862 100644 --- a/qemu/util/Makefile.objs +++ b/qemu/util/Makefile.objs @@ -10,5 +10,6 @@ util-obj-y += crc32c.o util-obj-y += host-utils.o util-obj-y += getauxval.o util-obj-y += log.o +util-obj-y += osdep.o util-obj-y += qht.o util-obj-y += range.o diff --git a/qemu/util/osdep.c b/qemu/util/osdep.c new file mode 100644 index 00000000..f1a0249a --- /dev/null +++ b/qemu/util/osdep.c @@ -0,0 +1,69 @@ +/* + * QEMU low level functions + * + * Copyright (c) 2003 Fabrice Bellard + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "qemu/osdep.h" + +#include "qemu-common.h" +#include "qemu/cutils.h" +#include "uc_priv.h" + +static int qemu_mprotect__osdep(struct uc_struct *uc, void *addr, size_t size, int prot) +{ + g_assert(!((uintptr_t)addr & ~uc->qemu_real_host_page_mask)); + g_assert(!(size & ~uc->qemu_real_host_page_mask)); + +#ifdef _WIN32 + DWORD old_protect; + + if (!VirtualProtect(addr, size, prot, &old_protect)) { + //error_report("%s: VirtualProtect failed with error code %ld", + // __func__, GetLastError()); + return -1; + } + return 0; +#else + if (mprotect(addr, size, prot)) { + //error_report("%s: mprotect failed: %s", __func__, strerror(errno)); + return -1; + } + return 0; +#endif +} + +int qemu_mprotect_rwx(struct uc_struct *uc, void *addr, size_t size) +{ +#ifdef _WIN32 + return qemu_mprotect__osdep(uc, addr, size, PAGE_EXECUTE_READWRITE); +#else + return qemu_mprotect__osdep(uc, addr, size, PROT_READ | PROT_WRITE | PROT_EXEC); +#endif +} + +int qemu_mprotect_none(struct uc_struct *uc, void *addr, size_t size) +{ +#ifdef _WIN32 + return qemu_mprotect__osdep(uc, addr, size, PAGE_NOACCESS); +#else + return qemu_mprotect__osdep(uc, addr, size, PROT_NONE); +#endif +}