2024-08-22 17:07:19 -07:00
|
|
|
From 10f273da9caa0b7c814f46b76279065a956393af Mon Sep 17 00:00:00 2001
|
2020-04-28 04:07:19 -07:00
|
|
|
From: Andrew Wesie <awesie@gmail.com>
|
|
|
|
Date: Fri, 24 Apr 2020 14:55:14 -0500
|
|
|
|
Subject: [PATCH] ntdll: Track if a WRITECOPY page has been modified.
|
|
|
|
|
|
|
|
Once a WRITECOPY page is modified, it should be mapped as if it is a normal
|
|
|
|
read-write page.
|
|
|
|
|
|
|
|
Signed-off-by: Andrew Wesie <awesie@gmail.com>
|
|
|
|
---
|
2024-08-22 17:07:19 -07:00
|
|
|
dlls/ntdll/unix/virtual.c | 30 ++++++++++++++++++++++++------
|
|
|
|
1 file changed, 24 insertions(+), 6 deletions(-)
|
2020-04-28 04:07:19 -07:00
|
|
|
|
2020-06-02 16:20:16 -07:00
|
|
|
diff --git a/dlls/ntdll/unix/virtual.c b/dlls/ntdll/unix/virtual.c
|
2024-08-22 17:07:19 -07:00
|
|
|
index 5eadabf7dca..58fd4d0edfc 100644
|
2020-06-02 16:20:16 -07:00
|
|
|
--- a/dlls/ntdll/unix/virtual.c
|
|
|
|
+++ b/dlls/ntdll/unix/virtual.c
|
2023-05-25 16:22:33 -07:00
|
|
|
@@ -122,6 +122,7 @@ struct file_view
|
2020-04-28 04:07:19 -07:00
|
|
|
#define VPROT_GUARD 0x10
|
|
|
|
#define VPROT_COMMITTED 0x20
|
|
|
|
#define VPROT_WRITEWATCH 0x40
|
|
|
|
+#define VPROT_WRITTEN 0x80
|
|
|
|
/* per-mapping protection flags */
|
2023-06-13 17:59:42 -07:00
|
|
|
#define VPROT_ARM64EC 0x0100 /* view may contain ARM64EC code */
|
|
|
|
#define VPROT_SYSTEM 0x0200 /* system view (underlying mmap not under our control) */
|
2024-08-22 17:07:19 -07:00
|
|
|
@@ -1155,7 +1156,7 @@ static int get_unix_prot( BYTE vprot )
|
2020-04-28 04:07:19 -07:00
|
|
|
#if defined(__i386__)
|
|
|
|
if (vprot & VPROT_WRITECOPY)
|
|
|
|
{
|
|
|
|
- if (experimental_WRITECOPY())
|
|
|
|
+ if (experimental_WRITECOPY() && !(vprot & VPROT_WRITTEN))
|
|
|
|
prot = (prot & ~PROT_WRITE) | PROT_READ;
|
|
|
|
else
|
|
|
|
prot |= PROT_WRITE | PROT_READ;
|
2024-08-22 17:07:19 -07:00
|
|
|
@@ -1672,7 +1673,11 @@ static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t siz
|
2020-04-28 04:07:19 -07:00
|
|
|
*/
|
2020-06-02 16:20:16 -07:00
|
|
|
static DWORD get_win32_prot( BYTE vprot, unsigned int map_prot )
|
2020-04-28 04:07:19 -07:00
|
|
|
{
|
|
|
|
- DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
|
|
|
|
+ DWORD ret;
|
|
|
|
+
|
|
|
|
+ if ((vprot & VPROT_WRITECOPY) && (vprot & VPROT_WRITTEN))
|
|
|
|
+ vprot = (vprot & ~VPROT_WRITECOPY) | VPROT_WRITE;
|
|
|
|
+ ret = VIRTUAL_Win32Flags[vprot & 0x0f];
|
|
|
|
if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
|
|
|
|
if (map_prot & SEC_NOCACHE) ret |= PAGE_NOCACHE;
|
|
|
|
return ret;
|
2024-08-22 17:07:19 -07:00
|
|
|
@@ -1778,16 +1783,29 @@ static void mprotect_range( void *base, size_t size, BYTE set, BYTE clear )
|
|
|
|
*/
|
|
|
|
static BOOL set_vprot( struct file_view *view, void *base, size_t size, BYTE vprot )
|
|
|
|
{
|
|
|
|
+ int unix_prot;
|
|
|
|
+
|
2020-04-28 04:07:19 -07:00
|
|
|
if (view->protect & VPROT_WRITEWATCH)
|
|
|
|
{
|
|
|
|
/* each page may need different protections depending on write watch flag */
|
|
|
|
- set_page_vprot_bits( base, size, vprot & ~VPROT_WRITEWATCH, ~vprot & ~VPROT_WRITEWATCH );
|
|
|
|
+ set_page_vprot_bits( base, size, vprot & ~VPROT_WRITEWATCH, ~vprot & ~(VPROT_WRITEWATCH|VPROT_WRITTEN) );
|
|
|
|
mprotect_range( base, size, 0, 0 );
|
|
|
|
return TRUE;
|
|
|
|
}
|
2024-08-22 17:07:19 -07:00
|
|
|
+
|
|
|
|
if (enable_write_exceptions && is_vprot_exec_write( vprot )) vprot |= VPROT_WRITEWATCH;
|
|
|
|
- if (mprotect_exec( base, size, get_unix_prot(vprot) )) return FALSE;
|
|
|
|
- set_page_vprot( base, size, vprot );
|
|
|
|
+ unix_prot = get_unix_prot(vprot);
|
2021-06-17 18:13:24 -07:00
|
|
|
+
|
2020-04-28 04:07:19 -07:00
|
|
|
+ /* check that we can map this memory with PROT_WRITE since we cannot fail later */
|
|
|
|
+ if (vprot & VPROT_WRITECOPY)
|
|
|
|
+ unix_prot |= PROT_WRITE;
|
|
|
|
+
|
2024-08-22 17:07:19 -07:00
|
|
|
+ if (mprotect_exec( base, size, unix_prot )) return FALSE;
|
2020-04-28 04:07:19 -07:00
|
|
|
+ /* each page may need different protections depending on writecopy */
|
|
|
|
+ set_page_vprot_bits( base, size, vprot, ~vprot & ~VPROT_WRITTEN );
|
|
|
|
+ if (vprot & VPROT_WRITECOPY)
|
|
|
|
+ mprotect_range( base, size, 0, 0 );
|
|
|
|
+
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2024-08-22 17:07:19 -07:00
|
|
|
@@ -4093,7 +4111,7 @@ NTSTATUS virtual_handle_fault( EXCEPTION_RECORD *rec, void *stack )
|
2020-04-28 04:07:19 -07:00
|
|
|
}
|
|
|
|
if (vprot & VPROT_WRITECOPY)
|
|
|
|
{
|
|
|
|
- set_page_vprot_bits( page, page_size, VPROT_WRITE, VPROT_WRITECOPY );
|
|
|
|
+ set_page_vprot_bits( page, page_size, VPROT_WRITE | VPROT_WRITTEN, VPROT_WRITECOPY );
|
|
|
|
mprotect_range( page, page_size, 0, 0 );
|
|
|
|
}
|
|
|
|
/* ignore fault if page is writable now */
|
|
|
|
--
|
2024-08-22 17:07:19 -07:00
|
|
|
2.45.2
|
2020-04-28 04:07:19 -07:00
|
|
|
|