1999-01-01 17:59:27 +01:00
|
|
|
/*
|
|
|
|
* Server-side file mapping management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-01-01 17:59:27 +01:00
|
|
|
*/
|
|
|
|
|
2001-10-14 18:18:52 +02:00
|
|
|
#include "config.h"
|
2002-04-26 21:05:15 +02:00
|
|
|
#include "wine/port.h"
|
2001-10-14 18:18:52 +02:00
|
|
|
|
1999-01-01 17:59:27 +01:00
|
|
|
#include <assert.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1999-01-01 17:59:27 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-02-14 21:27:09 +01:00
|
|
|
#include <sys/stat.h>
|
2011-01-28 17:23:45 +01:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
1999-01-01 17:59:27 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2005-10-27 20:30:37 +02:00
|
|
|
#include "winternl.h"
|
2020-05-11 13:12:48 +02:00
|
|
|
#include "ddk/wdm.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
2013-11-21 17:29:51 +01:00
|
|
|
#include "process.h"
|
1999-06-22 19:26:53 +02:00
|
|
|
#include "request.h"
|
2007-10-25 20:09:54 +02:00
|
|
|
#include "security.h"
|
1999-01-01 17:59:27 +01:00
|
|
|
|
2008-11-05 12:24:05 +01:00
|
|
|
/* list of memory ranges, used to store committed info */
|
|
|
|
struct ranges
|
|
|
|
{
|
2017-09-26 14:30:18 +02:00
|
|
|
struct object obj; /* object header */
|
|
|
|
unsigned int count; /* number of used ranges */
|
|
|
|
unsigned int max; /* number of allocated ranges */
|
2008-11-05 12:24:05 +01:00
|
|
|
struct range
|
|
|
|
{
|
|
|
|
file_pos_t start;
|
|
|
|
file_pos_t end;
|
2017-09-26 14:30:18 +02:00
|
|
|
} *ranges;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ranges_dump( struct object *obj, int verbose );
|
|
|
|
static void ranges_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops ranges_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct ranges), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&no_type, /* type */
|
2017-09-26 14:30:18 +02:00
|
|
|
ranges_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
|
|
|
no_signal, /* signal */
|
|
|
|
no_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2017-09-26 14:30:18 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
no_get_full_name, /* get_full_name */
|
2017-09-26 14:30:18 +02:00
|
|
|
no_lookup_name, /* lookup_name */
|
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
|
|
|
no_open_file, /* open_file */
|
2019-03-25 14:37:09 +01:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2017-09-26 14:30:18 +02:00
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
ranges_destroy /* destroy */
|
2008-11-05 12:24:05 +01:00
|
|
|
};
|
|
|
|
|
2017-10-04 10:06:14 +02:00
|
|
|
/* file backing the shared sections of a PE image mapping */
|
|
|
|
struct shared_map
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct fd *fd; /* file descriptor of the mapped PE file */
|
|
|
|
struct file *file; /* temp file holding the shared data */
|
|
|
|
struct list entry; /* entry in global shared maps list */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void shared_map_dump( struct object *obj, int verbose );
|
|
|
|
static void shared_map_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops shared_map_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct shared_map), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&no_type, /* type */
|
2017-10-04 10:06:14 +02:00
|
|
|
shared_map_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
|
|
|
no_signal, /* signal */
|
|
|
|
no_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2017-10-04 10:06:14 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
no_get_full_name, /* get_full_name */
|
2017-10-04 10:06:14 +02:00
|
|
|
no_lookup_name, /* lookup_name */
|
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
|
|
|
no_open_file, /* open_file */
|
2019-03-25 14:37:09 +01:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2017-10-04 10:06:14 +02:00
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
shared_map_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct list shared_map_list = LIST_INIT( shared_map_list );
|
|
|
|
|
2017-09-26 14:11:49 +02:00
|
|
|
/* memory view mapped in client address space */
|
|
|
|
struct memory_view
|
|
|
|
{
|
|
|
|
struct list entry; /* entry in per-process view list */
|
2017-09-26 14:50:43 +02:00
|
|
|
struct fd *fd; /* fd for mapped file */
|
2017-09-26 14:38:46 +02:00
|
|
|
struct ranges *committed; /* list of committed ranges in this mapping */
|
2017-10-04 10:08:31 +02:00
|
|
|
struct shared_map *shared; /* temp file for shared PE mapping */
|
2020-05-26 13:23:21 +02:00
|
|
|
pe_image_info_t image; /* image info (for PE image mapping) */
|
2017-09-26 14:11:49 +02:00
|
|
|
unsigned int flags; /* SEC_* flags */
|
|
|
|
client_ptr_t base; /* view base address (in process addr space) */
|
|
|
|
mem_size_t size; /* view size */
|
|
|
|
file_pos_t start; /* start offset in mapping */
|
2021-02-15 12:24:15 +01:00
|
|
|
data_size_t namelen;
|
|
|
|
WCHAR name[1]; /* filename for .so dll image views */
|
2017-09-26 14:11:49 +02:00
|
|
|
};
|
|
|
|
|
2021-02-04 13:44:18 +01:00
|
|
|
|
|
|
|
static const WCHAR mapping_name[] = {'S','e','c','t','i','o','n'};
|
|
|
|
|
|
|
|
struct type_descr mapping_type =
|
|
|
|
{
|
|
|
|
{ mapping_name, sizeof(mapping_name) }, /* name */
|
2021-02-05 12:08:42 +01:00
|
|
|
SECTION_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
|
|
|
|
{ /* mapping */
|
|
|
|
STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ,
|
|
|
|
STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
|
|
|
|
STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
|
|
|
|
SECTION_ALL_ACCESS
|
|
|
|
},
|
2021-02-04 13:44:18 +01:00
|
|
|
};
|
|
|
|
|
1999-01-01 17:59:27 +01:00
|
|
|
struct mapping
|
|
|
|
{
|
2001-04-14 00:38:39 +02:00
|
|
|
struct object obj; /* object header */
|
2008-12-17 19:25:49 +01:00
|
|
|
mem_size_t size; /* mapping size */
|
2016-07-26 07:20:02 +02:00
|
|
|
unsigned int flags; /* SEC_* flags */
|
2009-11-23 15:59:37 +01:00
|
|
|
struct fd *fd; /* fd for mapped file */
|
2016-07-26 12:38:50 +02:00
|
|
|
pe_image_info_t image; /* image info (for PE image mapping) */
|
2008-11-05 12:24:05 +01:00
|
|
|
struct ranges *committed; /* list of committed ranges in this mapping */
|
2017-10-04 10:06:14 +02:00
|
|
|
struct shared_map *shared; /* temp file for shared PE mapping */
|
1999-01-01 17:59:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void mapping_dump( struct object *obj, int verbose );
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *mapping_get_fd( struct object *obj );
|
1999-01-01 17:59:27 +01:00
|
|
|
static void mapping_destroy( struct object *obj );
|
2009-11-23 16:16:46 +01:00
|
|
|
static enum server_fd_type mapping_get_fd_type( struct fd *fd );
|
1999-01-01 17:59:27 +01:00
|
|
|
|
|
|
|
static const struct object_ops mapping_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct mapping), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&mapping_type, /* type */
|
2000-01-01 01:56:27 +01:00
|
|
|
mapping_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-02-19 01:33:32 +01:00
|
|
|
mapping_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2007-10-03 14:10:37 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
default_get_full_name, /* get_full_name */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2016-02-04 13:08:02 +01:00
|
|
|
directory_link_name, /* link_name */
|
|
|
|
default_unlink_name, /* unlink_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2019-03-25 14:37:09 +01:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2006-11-02 20:52:22 +01:00
|
|
|
fd_close_handle, /* close_handle */
|
2000-01-01 01:56:27 +01:00
|
|
|
mapping_destroy /* destroy */
|
1999-01-01 17:59:27 +01:00
|
|
|
};
|
|
|
|
|
2009-11-23 16:16:46 +01:00
|
|
|
static const struct fd_ops mapping_fd_ops =
|
|
|
|
{
|
|
|
|
default_fd_get_poll_events, /* get_poll_events */
|
|
|
|
default_poll_event, /* poll_event */
|
|
|
|
mapping_get_fd_type, /* get_fd_type */
|
2015-05-05 05:17:45 +02:00
|
|
|
no_fd_read, /* read */
|
|
|
|
no_fd_write, /* write */
|
|
|
|
no_fd_flush, /* flush */
|
2017-12-21 16:12:01 +01:00
|
|
|
no_fd_get_file_info, /* get_file_info */
|
2017-10-02 16:42:16 +02:00
|
|
|
no_fd_get_volume_info, /* get_volume_info */
|
2009-11-23 16:16:46 +01:00
|
|
|
no_fd_ioctl, /* ioctl */
|
|
|
|
no_fd_queue_async, /* queue_async */
|
2016-12-01 12:10:34 +01:00
|
|
|
default_fd_reselect_async /* reselect_async */
|
2009-11-23 16:16:46 +01:00
|
|
|
};
|
|
|
|
|
2013-01-08 22:02:28 +01:00
|
|
|
static size_t page_mask;
|
1999-02-28 11:13:59 +01:00
|
|
|
|
2005-04-19 13:59:13 +02:00
|
|
|
#define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
|
1999-02-28 11:13:59 +01:00
|
|
|
|
|
|
|
|
2017-09-26 14:30:18 +02:00
|
|
|
static void ranges_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct ranges *ranges = (struct ranges *)obj;
|
|
|
|
fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ranges_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct ranges *ranges = (struct ranges *)obj;
|
|
|
|
free( ranges->ranges );
|
|
|
|
}
|
|
|
|
|
2017-10-04 10:06:14 +02:00
|
|
|
static void shared_map_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct shared_map *shared = (struct shared_map *)obj;
|
|
|
|
fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shared_map_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct shared_map *shared = (struct shared_map *)obj;
|
|
|
|
|
|
|
|
release_object( shared->fd );
|
|
|
|
release_object( shared->file );
|
|
|
|
list_remove( &shared->entry );
|
|
|
|
}
|
|
|
|
|
2009-11-23 15:58:25 +01:00
|
|
|
/* extend a file beyond the current end of file */
|
2009-11-23 16:03:37 +01:00
|
|
|
static int grow_file( int unix_fd, file_pos_t new_size )
|
2009-11-23 15:58:25 +01:00
|
|
|
{
|
|
|
|
static const char zero;
|
|
|
|
off_t size = new_size;
|
|
|
|
|
|
|
|
if (sizeof(new_size) > sizeof(size) && size != new_size)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* extend the file one byte beyond the requested size and then truncate it */
|
|
|
|
/* this should work around ftruncate implementations that can't extend files */
|
|
|
|
if (pwrite( unix_fd, &zero, 1, size ) != -1)
|
|
|
|
{
|
|
|
|
ftruncate( unix_fd, size );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-28 17:23:45 +01:00
|
|
|
/* check if the current directory allows exec mappings */
|
|
|
|
static int check_current_dir_for_exec(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char tmpfn[] = "anonmap.XXXXXX";
|
|
|
|
void *ret = MAP_FAILED;
|
|
|
|
|
|
|
|
fd = mkstemps( tmpfn, 0 );
|
|
|
|
if (fd == -1) return 0;
|
|
|
|
if (grow_file( fd, 1 ))
|
|
|
|
{
|
|
|
|
ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
|
|
|
|
if (ret != MAP_FAILED) munmap( ret, get_page_size() );
|
|
|
|
}
|
|
|
|
close( fd );
|
|
|
|
unlink( tmpfn );
|
|
|
|
return (ret != MAP_FAILED);
|
|
|
|
}
|
|
|
|
|
2009-11-23 16:20:15 +01:00
|
|
|
/* create a temp file for anonymous mappings */
|
2009-11-23 16:28:45 +01:00
|
|
|
static int create_temp_file( file_pos_t size )
|
2009-11-23 16:20:15 +01:00
|
|
|
{
|
2011-01-28 17:23:45 +01:00
|
|
|
static int temp_dir_fd = -1;
|
|
|
|
char tmpfn[] = "anonmap.XXXXXX";
|
2009-11-23 16:20:15 +01:00
|
|
|
int fd;
|
|
|
|
|
2011-01-28 17:23:45 +01:00
|
|
|
if (temp_dir_fd == -1)
|
|
|
|
{
|
|
|
|
temp_dir_fd = server_dir_fd;
|
|
|
|
if (!check_current_dir_for_exec())
|
|
|
|
{
|
|
|
|
/* the server dir is noexec, try the config dir instead */
|
|
|
|
fchdir( config_dir_fd );
|
|
|
|
if (check_current_dir_for_exec())
|
|
|
|
temp_dir_fd = config_dir_fd;
|
|
|
|
else /* neither works, fall back to server dir */
|
|
|
|
fchdir( server_dir_fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
|
|
|
|
|
2009-11-23 16:20:15 +01:00
|
|
|
fd = mkstemps( tmpfn, 0 );
|
2009-11-23 16:28:45 +01:00
|
|
|
if (fd != -1)
|
2009-11-23 16:20:15 +01:00
|
|
|
{
|
2009-11-23 16:28:45 +01:00
|
|
|
if (!grow_file( fd, size ))
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
unlink( tmpfn );
|
2009-11-23 16:20:15 +01:00
|
|
|
}
|
2009-11-23 16:28:45 +01:00
|
|
|
else file_set_error();
|
2011-01-28 17:23:45 +01:00
|
|
|
|
|
|
|
if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
|
2009-11-23 16:28:45 +01:00
|
|
|
return fd;
|
2009-11-23 16:20:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:11:49 +02:00
|
|
|
/* find a memory view from its base address */
|
2021-02-02 10:12:56 +01:00
|
|
|
struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
|
2017-09-26 14:11:49 +02:00
|
|
|
{
|
|
|
|
struct memory_view *view;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
|
|
|
|
if (view->base == base) return view;
|
|
|
|
|
|
|
|
set_error( STATUS_NOT_MAPPED_VIEW );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:09:49 +01:00
|
|
|
/* find a memory view from any address inside it */
|
|
|
|
static struct memory_view *find_mapped_addr( struct process *process, client_ptr_t addr )
|
|
|
|
{
|
|
|
|
struct memory_view *view;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
|
|
|
|
if (addr >= view->base && addr < view->base + view->size) return view;
|
|
|
|
|
|
|
|
set_error( STATUS_NOT_MAPPED_VIEW );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-02 10:12:56 +01:00
|
|
|
/* get the main exe memory view */
|
|
|
|
struct memory_view *get_exe_view( struct process *process )
|
|
|
|
{
|
|
|
|
return LIST_ENTRY( list_head( &process->views ), struct memory_view, entry );
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:55:37 +01:00
|
|
|
/* add a view to the process list */
|
2021-02-11 11:49:06 +01:00
|
|
|
static void add_process_view( struct thread *thread, struct memory_view *view )
|
2021-01-26 11:55:37 +01:00
|
|
|
{
|
2021-02-11 11:49:06 +01:00
|
|
|
struct process *process = thread->process;
|
|
|
|
|
2021-01-26 11:55:37 +01:00
|
|
|
if (view->flags & SEC_IMAGE)
|
|
|
|
{
|
2021-02-11 11:49:06 +01:00
|
|
|
if (is_process_init_done( process ))
|
|
|
|
generate_debug_event( thread, DbgLoadDllStateChange, view );
|
|
|
|
else if (!(view->image.image_charact & IMAGE_FILE_DLL))
|
2021-01-26 11:55:37 +01:00
|
|
|
{
|
|
|
|
/* main exe */
|
|
|
|
list_add_head( &process->views, &view->entry );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_add_tail( &process->views, &view->entry );
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:11:49 +02:00
|
|
|
static void free_memory_view( struct memory_view *view )
|
|
|
|
{
|
2017-09-26 14:50:43 +02:00
|
|
|
if (view->fd) release_object( view->fd );
|
2017-09-26 14:38:46 +02:00
|
|
|
if (view->committed) release_object( view->committed );
|
2017-10-04 10:08:31 +02:00
|
|
|
if (view->shared) release_object( view->shared );
|
2017-09-26 14:11:49 +02:00
|
|
|
list_remove( &view->entry );
|
|
|
|
free( view );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free all mapped views at process exit */
|
|
|
|
void free_mapped_views( struct process *process )
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
while ((ptr = list_head( &process->views )))
|
|
|
|
free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
|
|
|
|
}
|
|
|
|
|
2001-04-14 00:38:39 +02:00
|
|
|
/* find the shared PE mapping for a given mapping */
|
2017-10-04 10:06:14 +02:00
|
|
|
static struct shared_map *get_shared_file( struct fd *fd )
|
2001-04-14 00:38:39 +02:00
|
|
|
{
|
2017-10-04 10:06:14 +02:00
|
|
|
struct shared_map *ptr;
|
2001-04-14 00:38:39 +02:00
|
|
|
|
2017-10-04 10:06:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
|
|
|
|
if (is_same_file_fd( ptr->fd, fd ))
|
|
|
|
return (struct shared_map *)grab_object( ptr );
|
2001-04-14 00:38:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-21 12:34:44 +01:00
|
|
|
/* return the size of the memory mapping and file range of a given section */
|
|
|
|
static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
|
|
|
|
off_t *file_start, size_t *file_size )
|
2005-08-15 16:50:06 +02:00
|
|
|
{
|
2006-12-21 12:34:44 +01:00
|
|
|
static const unsigned int sector_align = 0x1ff;
|
2005-08-15 16:50:06 +02:00
|
|
|
|
2006-12-21 12:34:44 +01:00
|
|
|
if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
|
|
|
|
else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
|
|
|
|
|
|
|
|
*file_start = sec->PointerToRawData & ~sector_align;
|
|
|
|
*file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
|
|
|
|
if (*file_size > *map_size) *file_size = *map_size;
|
2005-08-15 16:50:06 +02:00
|
|
|
}
|
|
|
|
|
2008-11-05 12:24:05 +01:00
|
|
|
/* add a range to the committed list */
|
2017-09-26 14:38:46 +02:00
|
|
|
static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
|
|
|
unsigned int i, j;
|
2017-09-26 14:38:46 +02:00
|
|
|
struct ranges *committed = view->committed;
|
2008-11-05 12:24:05 +01:00
|
|
|
struct range *ranges;
|
|
|
|
|
2017-09-26 14:38:46 +02:00
|
|
|
if ((start & page_mask) || (end & page_mask) ||
|
|
|
|
start >= view->size || end >= view->size ||
|
|
|
|
start >= end)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:30:18 +02:00
|
|
|
if (!committed) return; /* everything committed already */
|
2008-11-05 12:24:05 +01:00
|
|
|
|
2017-09-26 14:38:46 +02:00
|
|
|
start += view->start;
|
|
|
|
end += view->start;
|
|
|
|
|
2017-09-26 14:30:18 +02:00
|
|
|
for (i = 0, ranges = committed->ranges; i < committed->count; i++)
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
|
|
|
if (ranges[i].start > end) break;
|
|
|
|
if (ranges[i].end < start) continue;
|
|
|
|
if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
|
|
|
|
if (ranges[i].end < end) /* extend upwards and maybe merge with next */
|
|
|
|
{
|
2017-09-26 14:30:18 +02:00
|
|
|
for (j = i + 1; j < committed->count; j++)
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
|
|
|
if (ranges[j].start > end) break;
|
|
|
|
if (ranges[j].end > end) end = ranges[j].end;
|
|
|
|
}
|
|
|
|
if (j > i + 1)
|
|
|
|
{
|
2017-09-26 14:30:18 +02:00
|
|
|
memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
|
|
|
|
committed->count -= j - (i + 1);
|
2008-11-05 12:24:05 +01:00
|
|
|
}
|
|
|
|
ranges[i].end = end;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now add a new range */
|
|
|
|
|
2017-09-26 14:30:18 +02:00
|
|
|
if (committed->count == committed->max)
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
2017-09-26 14:30:18 +02:00
|
|
|
unsigned int new_size = committed->max * 2;
|
|
|
|
struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
|
2008-11-05 12:24:05 +01:00
|
|
|
if (!new_ptr) return;
|
2017-09-26 14:30:18 +02:00
|
|
|
committed->max = new_size;
|
2019-09-12 05:05:06 +02:00
|
|
|
ranges = committed->ranges = new_ptr;
|
2008-11-05 12:24:05 +01:00
|
|
|
}
|
2017-09-26 14:30:18 +02:00
|
|
|
memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
|
2008-11-05 12:24:05 +01:00
|
|
|
ranges[i].start = start;
|
|
|
|
ranges[i].end = end;
|
2017-09-26 14:30:18 +02:00
|
|
|
committed->count++;
|
2008-11-05 12:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find the range containing start and return whether it's committed */
|
2017-09-26 14:38:46 +02:00
|
|
|
static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2017-09-26 14:38:46 +02:00
|
|
|
struct ranges *committed = view->committed;
|
2008-11-05 12:24:05 +01:00
|
|
|
struct range *ranges;
|
|
|
|
|
2017-09-26 14:38:46 +02:00
|
|
|
if ((start & page_mask) || start >= view->size)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-26 14:30:18 +02:00
|
|
|
if (!committed) /* everything is committed */
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
2017-09-26 14:38:46 +02:00
|
|
|
*size = view->size - start;
|
2008-11-05 12:24:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2017-09-26 14:30:18 +02:00
|
|
|
for (i = 0, ranges = committed->ranges; i < committed->count; i++)
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
2017-09-26 14:38:46 +02:00
|
|
|
if (ranges[i].start > view->start + start)
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
2017-09-26 14:38:46 +02:00
|
|
|
*size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
|
2008-11-05 12:24:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-09-26 14:38:46 +02:00
|
|
|
if (ranges[i].end > view->start + start)
|
2008-11-05 12:24:05 +01:00
|
|
|
{
|
2017-09-26 14:38:46 +02:00
|
|
|
*size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
|
2008-11-05 12:24:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 14:38:46 +02:00
|
|
|
*size = view->size - start;
|
2008-11-05 12:24:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-14 22:20:01 +02:00
|
|
|
/* allocate and fill the temp file for a shared PE image mapping */
|
|
|
|
static int build_shared_mapping( struct mapping *mapping, int fd,
|
2005-04-19 13:59:13 +02:00
|
|
|
IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
|
2000-08-14 22:20:01 +02:00
|
|
|
{
|
2017-10-04 10:06:14 +02:00
|
|
|
struct shared_map *shared;
|
|
|
|
struct file *file;
|
2006-12-21 12:34:44 +01:00
|
|
|
unsigned int i;
|
2008-12-17 19:25:49 +01:00
|
|
|
mem_size_t total_size;
|
2007-10-10 14:06:25 +02:00
|
|
|
size_t file_size, map_size, max_size;
|
2004-05-01 04:50:06 +02:00
|
|
|
off_t shared_pos, read_pos, write_pos;
|
2000-12-22 22:12:36 +01:00
|
|
|
char *buffer = NULL;
|
|
|
|
int shared_fd;
|
2000-12-03 05:02:09 +01:00
|
|
|
long toread;
|
2000-08-14 22:20:01 +02:00
|
|
|
|
|
|
|
/* compute the total size of the shared mapping */
|
|
|
|
|
|
|
|
total_size = max_size = 0;
|
|
|
|
for (i = 0; i < nb_sec; i++)
|
|
|
|
{
|
|
|
|
if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
|
|
|
|
(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
|
|
|
|
{
|
2006-12-21 12:34:44 +01:00
|
|
|
get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
|
|
|
|
if (file_size > max_size) max_size = file_size;
|
|
|
|
total_size += map_size;
|
2000-08-14 22:20:01 +02:00
|
|
|
}
|
|
|
|
}
|
2007-10-10 14:06:25 +02:00
|
|
|
if (!total_size) return 1; /* nothing to do */
|
2000-08-14 22:20:01 +02:00
|
|
|
|
2017-10-04 10:06:14 +02:00
|
|
|
if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
|
2001-04-14 00:38:39 +02:00
|
|
|
|
2000-08-14 22:20:01 +02:00
|
|
|
/* create a temp file for the mapping */
|
|
|
|
|
2009-11-23 16:28:45 +01:00
|
|
|
if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
|
2017-10-04 10:06:14 +02:00
|
|
|
if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
|
2000-08-14 22:20:01 +02:00
|
|
|
|
|
|
|
if (!(buffer = malloc( max_size ))) goto error;
|
|
|
|
|
|
|
|
/* copy the shared sections data into the temp file */
|
|
|
|
|
2004-05-01 04:50:06 +02:00
|
|
|
shared_pos = 0;
|
|
|
|
for (i = 0; i < nb_sec; i++)
|
2000-08-14 22:20:01 +02:00
|
|
|
{
|
|
|
|
if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
|
|
|
|
if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
|
2006-12-21 12:34:44 +01:00
|
|
|
get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
|
2004-05-01 04:50:06 +02:00
|
|
|
write_pos = shared_pos;
|
2006-12-21 12:34:44 +01:00
|
|
|
shared_pos += map_size;
|
|
|
|
if (!sec[i].PointerToRawData || !file_size) continue;
|
|
|
|
toread = file_size;
|
2000-12-03 05:02:09 +01:00
|
|
|
while (toread)
|
|
|
|
{
|
2006-12-21 12:34:44 +01:00
|
|
|
long res = pread( fd, buffer + file_size - toread, toread, read_pos );
|
2007-01-08 21:34:15 +01:00
|
|
|
if (!res && toread < 0x200) /* partial sector at EOF is not an error */
|
|
|
|
{
|
|
|
|
file_size -= toread;
|
|
|
|
break;
|
|
|
|
}
|
2000-12-03 05:02:09 +01:00
|
|
|
if (res <= 0) goto error;
|
|
|
|
toread -= res;
|
2004-05-01 04:50:06 +02:00
|
|
|
read_pos += res;
|
2000-12-03 05:02:09 +01:00
|
|
|
}
|
2006-12-21 12:34:44 +01:00
|
|
|
if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
|
2000-08-14 22:20:01 +02:00
|
|
|
}
|
2017-10-04 10:06:14 +02:00
|
|
|
|
|
|
|
if (!(shared = alloc_object( &shared_map_ops ))) goto error;
|
|
|
|
shared->fd = (struct fd *)grab_object( mapping->fd );
|
|
|
|
shared->file = file;
|
|
|
|
list_add_head( &shared_map_list, &shared->entry );
|
|
|
|
mapping->shared = shared;
|
2017-10-16 15:28:38 +02:00
|
|
|
free( buffer );
|
2000-08-14 22:20:01 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
error:
|
2017-10-04 10:06:14 +02:00
|
|
|
release_object( file );
|
2006-10-09 23:34:36 +02:00
|
|
|
free( buffer );
|
2000-08-14 22:20:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:22:43 +01:00
|
|
|
/* load the CLR header from its section */
|
|
|
|
static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
|
|
|
|
IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
size_t map_size, file_size;
|
|
|
|
off_t file_start;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!va || !size) return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nb_sec; i++)
|
|
|
|
{
|
|
|
|
if (va < sec[i].VirtualAddress) continue;
|
|
|
|
if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
|
|
|
|
get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
|
|
|
|
if (size >= map_size) continue;
|
|
|
|
if (va - sec[i].VirtualAddress >= map_size - size) continue;
|
|
|
|
file_size = min( file_size, map_size );
|
|
|
|
size = min( size, sizeof(*hdr) );
|
|
|
|
ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
|
|
|
|
if (ret <= 0) break;
|
|
|
|
if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
|
|
|
|
return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
|
|
|
|
(hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
|
|
|
|
hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-14 22:20:01 +02:00
|
|
|
/* retrieve the mapping parameters for an executable (PE) image */
|
2016-07-26 12:38:50 +02:00
|
|
|
static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
|
2000-08-14 22:20:01 +02:00
|
|
|
{
|
2019-04-22 11:34:25 +02:00
|
|
|
static const char builtin_signature[] = "Wine builtin DLL";
|
2019-02-09 15:23:47 +01:00
|
|
|
static const char fakedll_signature[] = "Wine placeholder DLL";
|
|
|
|
|
2018-02-15 12:22:43 +01:00
|
|
|
IMAGE_COR20_HEADER clr;
|
|
|
|
IMAGE_SECTION_HEADER sec[96];
|
2009-08-12 12:53:16 +02:00
|
|
|
struct
|
2019-02-09 15:23:47 +01:00
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER dos;
|
2019-04-22 11:34:25 +02:00
|
|
|
char buffer[32];
|
2019-02-09 15:23:47 +01:00
|
|
|
} mz;
|
|
|
|
struct
|
2009-08-12 12:53:16 +02:00
|
|
|
{
|
|
|
|
DWORD Signature;
|
|
|
|
IMAGE_FILE_HEADER FileHeader;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
IMAGE_OPTIONAL_HEADER32 hdr32;
|
|
|
|
IMAGE_OPTIONAL_HEADER64 hdr64;
|
|
|
|
} opt;
|
|
|
|
} nt;
|
2004-05-01 04:50:06 +02:00
|
|
|
off_t pos;
|
2020-05-06 09:15:54 +02:00
|
|
|
int size, opt_size;
|
2019-02-09 15:23:47 +01:00
|
|
|
size_t mz_size, clr_va, clr_size;
|
2018-01-30 11:03:09 +01:00
|
|
|
unsigned int i, cpu_mask = get_supported_cpu_mask();
|
2000-08-14 22:20:01 +02:00
|
|
|
|
|
|
|
/* load the headers */
|
|
|
|
|
2016-07-27 09:49:37 +02:00
|
|
|
if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
|
2019-02-09 15:23:47 +01:00
|
|
|
size = pread( unix_fd, &mz, sizeof(mz), 0 );
|
|
|
|
if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
|
|
|
|
if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
|
|
|
|
mz_size = size;
|
|
|
|
pos = mz.dos.e_lfanew;
|
2002-08-27 02:34:41 +02:00
|
|
|
|
2009-08-12 12:53:16 +02:00
|
|
|
size = pread( unix_fd, &nt, sizeof(nt), pos );
|
2019-06-13 20:30:35 +02:00
|
|
|
if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
|
2009-08-12 12:53:16 +02:00
|
|
|
/* zero out Optional header in the case it's not present or partial */
|
2020-05-06 09:15:54 +02:00
|
|
|
opt_size = max( nt.FileHeader.SizeOfOptionalHeader, offsetof( IMAGE_OPTIONAL_HEADER32, CheckSum ));
|
|
|
|
size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + opt_size );
|
2009-08-12 12:53:16 +02:00
|
|
|
if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
|
2013-11-21 17:29:51 +01:00
|
|
|
if (nt.Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
{
|
2018-09-25 16:52:00 +02:00
|
|
|
IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
|
|
|
|
if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
|
|
|
|
if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
|
|
|
|
if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
|
|
|
|
return STATUS_INVALID_IMAGE_NE_FORMAT;
|
2013-11-21 17:29:51 +01:00
|
|
|
}
|
|
|
|
|
2009-08-12 12:53:16 +02:00
|
|
|
switch (nt.opt.hdr32.Magic)
|
|
|
|
{
|
|
|
|
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
|
2018-01-30 10:47:32 +01:00
|
|
|
switch (nt.FileHeader.Machine)
|
|
|
|
{
|
|
|
|
case IMAGE_FILE_MACHINE_I386:
|
2018-10-11 15:12:29 +02:00
|
|
|
mapping->image.cpu = CPU_x86;
|
2018-01-30 10:47:32 +01:00
|
|
|
if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
case IMAGE_FILE_MACHINE_ARM:
|
|
|
|
case IMAGE_FILE_MACHINE_THUMB:
|
|
|
|
case IMAGE_FILE_MACHINE_ARMNT:
|
2018-10-11 15:12:29 +02:00
|
|
|
mapping->image.cpu = CPU_ARM;
|
2018-01-30 10:47:32 +01:00
|
|
|
if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
case IMAGE_FILE_MACHINE_POWERPC:
|
2018-10-11 15:12:29 +02:00
|
|
|
mapping->image.cpu = CPU_POWERPC;
|
2018-01-30 10:47:32 +01:00
|
|
|
if (cpu_mask & CPU_FLAG(CPU_POWERPC)) break;
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
default:
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
2018-02-15 12:22:43 +01:00
|
|
|
clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
|
|
|
|
clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
|
|
|
|
|
2021-01-22 17:21:13 +01:00
|
|
|
mapping->image.base = nt.opt.hdr32.ImageBase;
|
|
|
|
mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
|
|
|
|
mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
|
|
|
|
mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
|
|
|
|
mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
|
|
|
|
mapping->image.subsystem = nt.opt.hdr32.Subsystem;
|
|
|
|
mapping->image.subsystem_minor = nt.opt.hdr32.MinorSubsystemVersion;
|
|
|
|
mapping->image.subsystem_major = nt.opt.hdr32.MajorSubsystemVersion;
|
|
|
|
mapping->image.osversion_minor = nt.opt.hdr32.MinorOperatingSystemVersion;
|
|
|
|
mapping->image.osversion_major = nt.opt.hdr32.MajorOperatingSystemVersion;
|
|
|
|
mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
|
|
|
|
mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
|
|
|
|
nt.opt.hdr32.AddressOfEntryPoint ||
|
|
|
|
nt.opt.hdr32.SectionAlignment & page_mask);
|
|
|
|
mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
|
|
|
|
mapping->image.checksum = nt.opt.hdr32.CheckSum;
|
|
|
|
mapping->image.image_flags = 0;
|
2018-02-15 12:22:43 +01:00
|
|
|
if (nt.opt.hdr32.SectionAlignment & page_mask)
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
|
|
|
|
if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
|
|
|
|
mapping->image.contains_code && !(clr_va && clr_size))
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
|
2009-08-12 12:53:16 +02:00
|
|
|
break;
|
2018-01-30 10:47:32 +01:00
|
|
|
|
2009-08-12 12:53:16 +02:00
|
|
|
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
|
2018-01-30 10:47:32 +01:00
|
|
|
if (!(cpu_mask & CPU_64BIT_MASK)) return STATUS_INVALID_IMAGE_WIN_64;
|
|
|
|
switch (nt.FileHeader.Machine)
|
|
|
|
{
|
|
|
|
case IMAGE_FILE_MACHINE_AMD64:
|
2018-10-11 15:12:29 +02:00
|
|
|
mapping->image.cpu = CPU_x86_64;
|
2018-01-30 10:47:32 +01:00
|
|
|
if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
case IMAGE_FILE_MACHINE_ARM64:
|
2018-10-11 15:12:29 +02:00
|
|
|
mapping->image.cpu = CPU_ARM64;
|
2018-01-30 10:47:32 +01:00
|
|
|
if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
default:
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
2018-02-15 12:22:43 +01:00
|
|
|
clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
|
|
|
|
clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
|
|
|
|
|
2021-01-22 17:21:13 +01:00
|
|
|
mapping->image.base = nt.opt.hdr64.ImageBase;
|
|
|
|
mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
|
|
|
|
mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
|
|
|
|
mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
|
|
|
|
mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
|
|
|
|
mapping->image.subsystem = nt.opt.hdr64.Subsystem;
|
|
|
|
mapping->image.subsystem_minor = nt.opt.hdr64.MinorSubsystemVersion;
|
|
|
|
mapping->image.subsystem_major = nt.opt.hdr64.MajorSubsystemVersion;
|
|
|
|
mapping->image.osversion_minor = nt.opt.hdr64.MinorOperatingSystemVersion;
|
|
|
|
mapping->image.osversion_major = nt.opt.hdr64.MajorOperatingSystemVersion;
|
|
|
|
mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
|
|
|
|
mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
|
|
|
|
nt.opt.hdr64.AddressOfEntryPoint ||
|
|
|
|
nt.opt.hdr64.SectionAlignment & page_mask);
|
|
|
|
mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
|
|
|
|
mapping->image.checksum = nt.opt.hdr64.CheckSum;
|
|
|
|
mapping->image.image_flags = 0;
|
2018-02-15 12:22:43 +01:00
|
|
|
if (nt.opt.hdr64.SectionAlignment & page_mask)
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
|
|
|
|
if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
|
|
|
|
mapping->image.contains_code && !(clr_va && clr_size))
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
|
2009-08-12 12:53:16 +02:00
|
|
|
break;
|
2018-01-30 10:47:32 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
2009-08-12 12:53:16 +02:00
|
|
|
}
|
2018-01-30 10:47:32 +01:00
|
|
|
|
2016-07-26 12:38:50 +02:00
|
|
|
mapping->image.image_charact = nt.FileHeader.Characteristics;
|
|
|
|
mapping->image.machine = nt.FileHeader.Machine;
|
2021-02-02 10:12:56 +01:00
|
|
|
mapping->image.dbg_offset = nt.FileHeader.PointerToSymbolTable;
|
|
|
|
mapping->image.dbg_size = nt.FileHeader.NumberOfSymbols;
|
2016-07-26 12:38:50 +02:00
|
|
|
mapping->image.zerobits = 0; /* FIXME */
|
|
|
|
mapping->image.file_size = file_size;
|
2018-02-15 12:22:43 +01:00
|
|
|
mapping->image.loader_flags = clr_va && clr_size;
|
2019-04-30 06:10:55 +02:00
|
|
|
mapping->image.__pad = 0;
|
2019-04-22 11:34:25 +02:00
|
|
|
if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ))
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin;
|
|
|
|
else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ))
|
2019-02-09 15:23:47 +01:00
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
|
2000-08-14 22:20:01 +02:00
|
|
|
|
|
|
|
/* load the section headers */
|
|
|
|
|
2009-08-12 12:53:16 +02:00
|
|
|
pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
|
2018-10-22 22:39:01 +02:00
|
|
|
if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
|
2000-08-14 22:20:01 +02:00
|
|
|
size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
|
2016-07-27 09:49:37 +02:00
|
|
|
if (!mapping->size) mapping->size = mapping->image.map_size;
|
|
|
|
else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
|
|
|
|
if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
|
2016-07-26 12:38:50 +02:00
|
|
|
if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
|
2018-02-15 12:22:43 +01:00
|
|
|
if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
|
2000-08-14 22:20:01 +02:00
|
|
|
|
2018-01-30 11:03:09 +01:00
|
|
|
for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
|
|
|
|
if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
|
|
|
|
|
2018-02-15 12:22:43 +01:00
|
|
|
if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
|
|
|
|
(clr.Flags & COMIMAGE_FLAGS_ILONLY))
|
|
|
|
{
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
|
2021-01-22 17:21:13 +01:00
|
|
|
if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
|
2019-02-15 15:52:50 +01:00
|
|
|
{
|
2021-01-22 17:21:13 +01:00
|
|
|
if (!(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
|
|
|
|
{
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
|
|
|
|
if (cpu_mask & CPU_FLAG(CPU_x86_64)) mapping->image.cpu = CPU_x86_64;
|
|
|
|
else if (cpu_mask & CPU_FLAG(CPU_ARM64)) mapping->image.cpu = CPU_ARM64;
|
|
|
|
}
|
|
|
|
if (clr.Flags & COMIMAGE_FLAGS_32BITPREFERRED)
|
|
|
|
mapping->image.image_flags |= IMAGE_FLAGS_ComPlusPrefer32bit;
|
2019-02-15 15:52:50 +01:00
|
|
|
}
|
2018-02-15 12:22:43 +01:00
|
|
|
}
|
2000-08-14 22:20:01 +02:00
|
|
|
|
2018-02-15 12:22:43 +01:00
|
|
|
if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
|
|
|
|
return STATUS_INVALID_FILE_FOR_SECTION;
|
2000-08-14 22:20:01 +02:00
|
|
|
|
2018-02-15 12:22:43 +01:00
|
|
|
return STATUS_SUCCESS;
|
2000-08-14 22:20:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:30:18 +02:00
|
|
|
static struct ranges *create_ranges(void)
|
|
|
|
{
|
|
|
|
struct ranges *ranges = alloc_object( &ranges_ops );
|
|
|
|
|
|
|
|
if (!ranges) return NULL;
|
|
|
|
ranges->count = 0;
|
|
|
|
ranges->max = 8;
|
2017-10-03 10:28:50 +02:00
|
|
|
if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
|
2017-09-26 14:30:18 +02:00
|
|
|
{
|
|
|
|
release_object( ranges );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:15:03 +02:00
|
|
|
static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
|
|
|
|
{
|
|
|
|
switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
|
|
|
|
{
|
|
|
|
case SEC_IMAGE:
|
|
|
|
if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
|
|
|
|
if (handle) return SEC_FILE | SEC_IMAGE;
|
|
|
|
set_error( STATUS_INVALID_FILE_FOR_SECTION );
|
|
|
|
return 0;
|
|
|
|
case SEC_COMMIT:
|
|
|
|
if (!handle) return flags;
|
|
|
|
/* fall through */
|
|
|
|
case SEC_RESERVE:
|
|
|
|
if (flags & SEC_LARGE_PAGES) break;
|
|
|
|
if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-29 12:26:57 +02:00
|
|
|
static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
|
|
|
|
unsigned int attr, mem_size_t size, unsigned int flags,
|
|
|
|
obj_handle_t handle, unsigned int file_access,
|
|
|
|
const struct security_descriptor *sd )
|
1999-01-01 17:59:27 +01:00
|
|
|
{
|
|
|
|
struct mapping *mapping;
|
2009-11-23 16:16:46 +01:00
|
|
|
struct file *file;
|
|
|
|
struct fd *fd;
|
2009-11-23 16:03:37 +01:00
|
|
|
int unix_fd;
|
|
|
|
struct stat st;
|
1999-02-28 11:13:59 +01:00
|
|
|
|
2013-01-08 22:02:28 +01:00
|
|
|
if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
|
1999-01-01 17:59:27 +01:00
|
|
|
|
2016-02-12 14:57:33 +01:00
|
|
|
if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
|
1999-01-01 17:59:27 +01:00
|
|
|
return NULL;
|
2005-11-21 17:27:03 +01:00
|
|
|
if (get_error() == STATUS_OBJECT_NAME_EXISTS)
|
2020-05-29 12:26:57 +02:00
|
|
|
return mapping; /* Nothing else to do */
|
1999-01-01 17:59:27 +01:00
|
|
|
|
2016-07-27 09:49:37 +02:00
|
|
|
mapping->size = size;
|
2009-11-23 15:59:37 +01:00
|
|
|
mapping->fd = NULL;
|
2017-10-04 10:06:14 +02:00
|
|
|
mapping->shared = NULL;
|
2008-11-05 12:24:05 +01:00
|
|
|
mapping->committed = NULL;
|
2000-08-14 22:20:01 +02:00
|
|
|
|
2017-09-08 12:15:03 +02:00
|
|
|
if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
|
|
|
|
|
2001-01-05 05:08:07 +01:00
|
|
|
if (handle)
|
1999-01-01 17:59:27 +01:00
|
|
|
{
|
2011-04-19 17:19:31 +02:00
|
|
|
const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
|
2009-11-23 17:24:09 +01:00
|
|
|
unsigned int mapping_access = FILE_MAPPING_ACCESS;
|
2009-11-23 17:09:08 +01:00
|
|
|
|
2017-09-12 11:42:04 +02:00
|
|
|
if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
|
2009-11-23 16:16:46 +01:00
|
|
|
fd = get_obj_fd( (struct object *)file );
|
2009-11-23 17:09:08 +01:00
|
|
|
|
|
|
|
/* file sharing rules for mappings are different so we use magic the access rights */
|
2016-07-26 07:20:02 +02:00
|
|
|
if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
|
2017-09-12 11:42:04 +02:00
|
|
|
else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
|
2011-04-19 17:19:31 +02:00
|
|
|
|
|
|
|
if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
|
|
|
|
{
|
|
|
|
mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
|
|
|
|
if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
|
|
|
|
}
|
2009-11-23 16:16:46 +01:00
|
|
|
release_object( file );
|
|
|
|
release_object( fd );
|
|
|
|
if (!mapping->fd) goto error;
|
|
|
|
|
2009-11-23 16:03:37 +01:00
|
|
|
if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
|
2016-07-26 12:38:50 +02:00
|
|
|
if (fstat( unix_fd, &st ) == -1)
|
2000-08-14 22:20:01 +02:00
|
|
|
{
|
2016-07-26 12:38:50 +02:00
|
|
|
file_set_error();
|
2013-11-21 17:29:51 +01:00
|
|
|
goto error;
|
2000-08-14 22:20:01 +02:00
|
|
|
}
|
2016-07-26 12:38:50 +02:00
|
|
|
if (flags & SEC_IMAGE)
|
2009-11-23 16:03:37 +01:00
|
|
|
{
|
2016-07-26 12:38:50 +02:00
|
|
|
unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
|
2020-05-29 12:26:57 +02:00
|
|
|
if (!err) return mapping;
|
2016-07-26 12:38:50 +02:00
|
|
|
set_error( err );
|
2009-11-23 16:03:37 +01:00
|
|
|
goto error;
|
|
|
|
}
|
2016-07-27 09:49:37 +02:00
|
|
|
if (!mapping->size)
|
1999-01-01 17:59:27 +01:00
|
|
|
{
|
2016-07-27 09:49:37 +02:00
|
|
|
if (!(mapping->size = st.st_size))
|
2004-01-09 01:34:14 +01:00
|
|
|
{
|
2005-12-12 13:38:43 +01:00
|
|
|
set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
|
2004-01-09 01:34:14 +01:00
|
|
|
goto error;
|
|
|
|
}
|
2003-02-14 21:27:09 +01:00
|
|
|
}
|
2016-07-27 15:20:31 +02:00
|
|
|
else if (st.st_size < mapping->size)
|
|
|
|
{
|
2017-09-12 11:42:04 +02:00
|
|
|
if (!(file_access & FILE_WRITE_DATA))
|
2016-07-27 15:20:31 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_SECTION_TOO_BIG );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!grow_file( unix_fd, mapping->size )) goto error;
|
|
|
|
}
|
1999-01-01 17:59:27 +01:00
|
|
|
}
|
1999-02-28 11:13:59 +01:00
|
|
|
else /* Anonymous mapping (no associated file) */
|
|
|
|
{
|
2017-09-08 12:15:03 +02:00
|
|
|
if (!mapping->size)
|
1999-02-28 11:13:59 +01:00
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
1999-02-28 11:13:59 +01:00
|
|
|
goto error;
|
|
|
|
}
|
2017-09-26 14:30:18 +02:00
|
|
|
if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
|
2016-07-27 09:49:37 +02:00
|
|
|
mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
|
|
|
|
if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
|
2009-11-23 16:28:45 +01:00
|
|
|
if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
|
2010-08-23 17:09:45 +02:00
|
|
|
allow_fd_caching( mapping->fd );
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
2020-05-29 12:26:57 +02:00
|
|
|
return mapping;
|
1999-02-28 11:13:59 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
release_object( mapping );
|
|
|
|
return NULL;
|
1999-01-01 17:59:27 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 16:41:07 +01:00
|
|
|
/* create a read-only file mapping for the specified fd */
|
|
|
|
struct mapping *create_fd_mapping( struct object *root, const struct unicode_str *name,
|
|
|
|
struct fd *fd, unsigned int attr, const struct security_descriptor *sd )
|
|
|
|
{
|
|
|
|
struct mapping *mapping;
|
|
|
|
int unix_fd;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd ))) return NULL;
|
|
|
|
if (get_error() == STATUS_OBJECT_NAME_EXISTS) return mapping; /* Nothing else to do */
|
|
|
|
|
|
|
|
mapping->shared = NULL;
|
|
|
|
mapping->committed = NULL;
|
|
|
|
mapping->flags = SEC_FILE;
|
|
|
|
mapping->fd = (struct fd *)grab_object( fd );
|
|
|
|
set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
|
|
|
|
|
|
|
|
if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
|
|
|
|
if (fstat( unix_fd, &st ) == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!(mapping->size = st.st_size))
|
|
|
|
{
|
|
|
|
set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return mapping;
|
|
|
|
|
|
|
|
error:
|
|
|
|
release_object( mapping );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-29 12:26:57 +02:00
|
|
|
static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
2011-04-18 13:41:54 +02:00
|
|
|
{
|
|
|
|
return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
|
|
|
|
}
|
|
|
|
|
2021-02-02 10:12:56 +01:00
|
|
|
/* open a new file for the file descriptor backing the view */
|
|
|
|
struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing )
|
2011-04-18 13:41:54 +02:00
|
|
|
{
|
2021-02-02 10:12:56 +01:00
|
|
|
if (!view->fd) return NULL;
|
2017-10-03 11:59:20 +02:00
|
|
|
return create_file_for_fd_obj( view->fd, access, sharing );
|
2011-04-18 13:41:54 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 10:12:56 +01:00
|
|
|
/* get the image info for a SEC_IMAGE mapped view */
|
|
|
|
const pe_image_info_t *get_view_image_info( const struct memory_view *view, client_ptr_t *base )
|
2020-05-26 13:23:21 +02:00
|
|
|
{
|
2021-02-02 10:12:56 +01:00
|
|
|
if (!(view->flags & SEC_IMAGE)) return NULL;
|
|
|
|
*base = view->base;
|
2020-05-26 13:23:21 +02:00
|
|
|
return &view->image;
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:14:27 +01:00
|
|
|
/* get the file name for a mapped view */
|
|
|
|
int get_view_nt_name( const struct memory_view *view, struct unicode_str *name )
|
|
|
|
{
|
2021-02-15 12:24:15 +01:00
|
|
|
if (view->namelen) /* .so builtin */
|
|
|
|
{
|
|
|
|
name->str = view->name;
|
|
|
|
name->len = view->namelen;
|
|
|
|
return 1;
|
|
|
|
}
|
2021-02-09 16:14:27 +01:00
|
|
|
if (!view->fd) return 0;
|
|
|
|
get_nt_name( view->fd, name );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-11 11:49:06 +01:00
|
|
|
/* generate all startup events of a given process */
|
|
|
|
void generate_startup_debug_events( struct process *process )
|
|
|
|
{
|
|
|
|
struct memory_view *view;
|
|
|
|
struct list *ptr = list_head( &process->views );
|
|
|
|
struct thread *thread, *first_thread = get_process_first_thread( process );
|
|
|
|
|
|
|
|
if (!ptr) return;
|
|
|
|
view = LIST_ENTRY( ptr, struct memory_view, entry );
|
|
|
|
generate_debug_event( first_thread, DbgCreateProcessStateChange, view );
|
|
|
|
|
|
|
|
/* generate ntdll.dll load event */
|
|
|
|
while (ptr && (ptr = list_next( &process->views, ptr )))
|
|
|
|
{
|
|
|
|
view = LIST_ENTRY( ptr, struct memory_view, entry );
|
|
|
|
if (!(view->flags & SEC_IMAGE)) continue;
|
|
|
|
generate_debug_event( first_thread, DbgLoadDllStateChange, view );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate creation events */
|
|
|
|
LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
|
|
|
|
{
|
|
|
|
if (thread != first_thread)
|
|
|
|
generate_debug_event( thread, DbgCreateThreadStateChange, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate dll events (in loading order) */
|
|
|
|
while (ptr && (ptr = list_next( &process->views, ptr )))
|
|
|
|
{
|
|
|
|
view = LIST_ENTRY( ptr, struct memory_view, entry );
|
|
|
|
if (!(view->flags & SEC_IMAGE)) continue;
|
|
|
|
generate_debug_event( first_thread, DbgLoadDllStateChange, view );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-01 17:59:27 +01:00
|
|
|
static void mapping_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct mapping *mapping = (struct mapping *)obj;
|
|
|
|
assert( obj->ops == &mapping_ops );
|
2017-10-04 10:06:14 +02:00
|
|
|
fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
|
2005-04-19 13:59:13 +02:00
|
|
|
(unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
|
2017-10-04 10:06:14 +02:00
|
|
|
mapping->flags, mapping->fd, mapping->shared );
|
1999-01-01 17:59:27 +01:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *mapping_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct mapping *mapping = (struct mapping *)obj;
|
2009-11-23 15:59:37 +01:00
|
|
|
return (struct fd *)grab_object( mapping->fd );
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
|
|
|
|
1999-01-01 17:59:27 +01:00
|
|
|
static void mapping_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct mapping *mapping = (struct mapping *)obj;
|
|
|
|
assert( obj->ops == &mapping_ops );
|
2009-11-23 15:59:37 +01:00
|
|
|
if (mapping->fd) release_object( mapping->fd );
|
2017-09-26 14:30:18 +02:00
|
|
|
if (mapping->committed) release_object( mapping->committed );
|
2017-10-04 10:06:14 +02:00
|
|
|
if (mapping->shared) release_object( mapping->shared );
|
1999-01-01 17:59:27 +01:00
|
|
|
}
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2009-11-23 16:16:46 +01:00
|
|
|
static enum server_fd_type mapping_get_fd_type( struct fd *fd )
|
|
|
|
{
|
|
|
|
return FD_TYPE_FILE;
|
|
|
|
}
|
|
|
|
|
1999-11-13 20:49:05 +01:00
|
|
|
int get_page_size(void)
|
|
|
|
{
|
2013-01-08 22:02:28 +01:00
|
|
|
if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
|
1999-11-13 20:49:05 +01:00
|
|
|
return page_mask + 1;
|
|
|
|
}
|
|
|
|
|
2020-05-29 12:26:57 +02:00
|
|
|
struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
|
|
|
|
unsigned int attr, const struct security_descriptor *sd )
|
2020-05-11 13:12:48 +02:00
|
|
|
{
|
2020-05-25 20:25:50 +02:00
|
|
|
void *ptr;
|
2020-05-29 12:26:57 +02:00
|
|
|
struct mapping *mapping;
|
2020-05-11 13:12:48 +02:00
|
|
|
|
2020-09-23 11:32:47 +02:00
|
|
|
if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA),
|
|
|
|
SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL;
|
2020-05-25 20:25:50 +02:00
|
|
|
ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
|
2020-07-08 09:20:57 +02:00
|
|
|
if (ptr != MAP_FAILED)
|
|
|
|
{
|
|
|
|
user_shared_data = ptr;
|
2020-07-09 17:15:40 +02:00
|
|
|
user_shared_data->SystemCall = 1;
|
2020-07-08 09:20:57 +02:00
|
|
|
}
|
2020-05-29 12:26:57 +02:00
|
|
|
return &mapping->obj;
|
2020-05-11 13:12:48 +02:00
|
|
|
}
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
/* create a file mapping */
|
|
|
|
DECL_HANDLER(create_mapping)
|
|
|
|
{
|
2020-05-29 12:26:57 +02:00
|
|
|
struct object *root;
|
|
|
|
struct mapping *mapping;
|
2005-11-18 17:31:18 +01:00
|
|
|
struct unicode_str name;
|
2007-10-25 20:09:54 +02:00
|
|
|
const struct security_descriptor *sd;
|
2016-02-01 06:57:37 +01:00
|
|
|
const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2016-01-15 10:15:31 +01:00
|
|
|
if (!objattr) return;
|
2007-10-25 20:09:54 +02:00
|
|
|
|
2020-05-29 12:26:57 +02:00
|
|
|
if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
|
|
|
|
req->file_handle, req->file_access, sd )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
2007-11-05 15:23:36 +01:00
|
|
|
if (get_error() == STATUS_OBJECT_NAME_EXISTS)
|
2020-05-29 12:26:57 +02:00
|
|
|
reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
|
2007-11-05 15:23:36 +01:00
|
|
|
else
|
2020-05-29 12:26:57 +02:00
|
|
|
reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
|
2016-01-15 09:53:34 +01:00
|
|
|
req->access, objattr->attributes );
|
2020-05-29 12:26:57 +02:00
|
|
|
release_object( mapping );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
2005-12-02 16:13:13 +01:00
|
|
|
|
|
|
|
if (root) release_object( root );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open a handle to a mapping */
|
|
|
|
DECL_HANDLER(open_mapping)
|
|
|
|
{
|
2016-01-29 08:36:45 +01:00
|
|
|
struct unicode_str name = get_req_unicode_str();
|
2005-11-18 17:31:18 +01:00
|
|
|
|
2016-01-29 08:23:29 +01:00
|
|
|
reply->handle = open_object( current->process, req->rootdir, req->access,
|
|
|
|
&mapping_ops, &name, req->attributes );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get a mapping information */
|
|
|
|
DECL_HANDLER(get_mapping_info)
|
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
struct mapping *mapping;
|
|
|
|
|
2013-11-21 17:29:51 +01:00
|
|
|
if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
|
|
|
|
|
2016-07-26 12:38:50 +02:00
|
|
|
reply->size = mapping->size;
|
|
|
|
reply->flags = mapping->flags;
|
|
|
|
|
|
|
|
if (mapping->flags & SEC_IMAGE)
|
|
|
|
set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
|
2016-07-26 07:20:02 +02:00
|
|
|
|
2016-07-26 07:43:02 +02:00
|
|
|
if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
|
|
|
|
{
|
|
|
|
release_object( mapping );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-04 10:06:14 +02:00
|
|
|
if (mapping->shared)
|
|
|
|
reply->shared_file = alloc_handle( current->process, mapping->shared->file,
|
2017-09-26 15:03:38 +02:00
|
|
|
GENERIC_READ|GENERIC_WRITE, 0 );
|
2013-11-21 17:29:51 +01:00
|
|
|
release_object( mapping );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
2008-11-05 12:24:05 +01:00
|
|
|
|
2017-09-26 14:11:49 +02:00
|
|
|
/* add a memory view in the current process */
|
|
|
|
DECL_HANDLER(map_view)
|
|
|
|
{
|
|
|
|
struct mapping *mapping = NULL;
|
|
|
|
struct memory_view *view;
|
2021-02-15 12:24:15 +01:00
|
|
|
data_size_t namelen = 0;
|
2017-09-26 14:11:49 +02:00
|
|
|
|
|
|
|
if (!req->size || (req->base & page_mask) || req->base + req->size < req->base) /* overflow */
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure we don't already have an overlapping view */
|
|
|
|
LIST_FOR_EACH_ENTRY( view, ¤t->process->views, struct memory_view, entry )
|
|
|
|
{
|
|
|
|
if (view->base + view->size <= req->base) continue;
|
|
|
|
if (view->base >= req->base + req->size) continue;
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:55:37 +01:00
|
|
|
if (!req->mapping) /* image mapping for a .so dll */
|
|
|
|
{
|
2021-02-15 12:24:15 +01:00
|
|
|
if (get_req_data_size() > sizeof(view->image)) namelen = get_req_data_size() - sizeof(view->image);
|
|
|
|
if (!(view = mem_alloc( offsetof( struct memory_view, name[namelen] )))) return;
|
2021-01-26 11:55:37 +01:00
|
|
|
memset( view, 0, sizeof(*view) );
|
2021-02-15 12:24:15 +01:00
|
|
|
view->base = req->base;
|
|
|
|
view->size = req->size;
|
|
|
|
view->start = req->start;
|
|
|
|
view->flags = SEC_IMAGE;
|
|
|
|
view->namelen = namelen;
|
2021-01-26 11:55:37 +01:00
|
|
|
memcpy( &view->image, get_req_data(), min( sizeof(view->image), get_req_data_size() ));
|
2021-02-15 12:24:15 +01:00
|
|
|
memcpy( view->name, (pe_image_info_t *)get_req_data() + 1, namelen );
|
2021-02-11 11:49:06 +01:00
|
|
|
add_process_view( current, view );
|
2021-01-26 11:55:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:11:49 +02:00
|
|
|
if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
|
|
|
|
|
|
|
|
if (mapping->flags & SEC_IMAGE)
|
|
|
|
{
|
|
|
|
if (req->start || req->size > mapping->image.map_size)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (req->start >= mapping->size ||
|
|
|
|
req->start + req->size < req->start ||
|
|
|
|
req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-02-15 12:24:15 +01:00
|
|
|
if ((view = mem_alloc( offsetof( struct memory_view, name[namelen] ))))
|
2017-09-26 14:11:49 +02:00
|
|
|
{
|
|
|
|
view->base = req->base;
|
|
|
|
view->size = req->size;
|
|
|
|
view->start = req->start;
|
|
|
|
view->flags = mapping->flags;
|
2021-02-15 12:24:15 +01:00
|
|
|
view->namelen = namelen;
|
2017-09-26 14:50:43 +02:00
|
|
|
view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
|
2017-09-26 14:38:46 +02:00
|
|
|
view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
|
2017-10-04 10:08:31 +02:00
|
|
|
view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
|
2021-02-11 11:49:06 +01:00
|
|
|
if (view->flags & SEC_IMAGE) view->image = mapping->image;
|
|
|
|
add_process_view( current, view );
|
|
|
|
if (view->flags & SEC_IMAGE && view->base != mapping->image.base)
|
|
|
|
set_error( STATUS_IMAGE_NOT_AT_BASE );
|
2017-09-26 14:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
release_object( mapping );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmap a memory view from the current process */
|
|
|
|
DECL_HANDLER(unmap_view)
|
|
|
|
{
|
|
|
|
struct memory_view *view = find_mapped_view( current->process, req->base );
|
|
|
|
|
2021-02-11 11:49:06 +01:00
|
|
|
if (!view) return;
|
|
|
|
if (view->flags & SEC_IMAGE) generate_debug_event( current, DbgUnloadDllStateChange, view );
|
|
|
|
free_memory_view( view );
|
2017-09-26 14:11:49 +02:00
|
|
|
}
|
|
|
|
|
2008-11-05 12:24:05 +01:00
|
|
|
/* get a range of committed pages in a file mapping */
|
|
|
|
DECL_HANDLER(get_mapping_committed_range)
|
|
|
|
{
|
2017-09-26 14:38:46 +02:00
|
|
|
struct memory_view *view = find_mapped_view( current->process, req->base );
|
2008-11-05 12:24:05 +01:00
|
|
|
|
2017-09-26 14:38:46 +02:00
|
|
|
if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
|
2008-11-05 12:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add a range to the committed pages in a file mapping */
|
|
|
|
DECL_HANDLER(add_mapping_committed_range)
|
|
|
|
{
|
2017-09-26 14:38:46 +02:00
|
|
|
struct memory_view *view = find_mapped_view( current->process, req->base );
|
2008-11-05 12:24:05 +01:00
|
|
|
|
2017-09-26 14:38:46 +02:00
|
|
|
if (view) add_committed_range( view, req->offset, req->offset + req->size );
|
2008-11-05 12:24:05 +01:00
|
|
|
}
|
2017-09-26 14:55:13 +02:00
|
|
|
|
|
|
|
/* check if two memory maps are for the same file */
|
|
|
|
DECL_HANDLER(is_same_mapping)
|
|
|
|
{
|
|
|
|
struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
|
|
|
|
struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
|
|
|
|
|
|
|
|
if (!view1 || !view2) return;
|
|
|
|
if (!view1->fd || !view2->fd ||
|
|
|
|
!(view1->flags & SEC_IMAGE) || !(view2->flags & SEC_IMAGE) ||
|
|
|
|
!is_same_file_fd( view1->fd, view2->fd ))
|
|
|
|
set_error( STATUS_NOT_SAME_DEVICE );
|
|
|
|
}
|
2021-02-10 16:09:49 +01:00
|
|
|
|
|
|
|
/* get the filename of a mapping */
|
|
|
|
DECL_HANDLER(get_mapping_filename)
|
|
|
|
{
|
|
|
|
struct process *process;
|
|
|
|
struct memory_view *view;
|
|
|
|
struct unicode_str name;
|
|
|
|
|
|
|
|
if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
|
|
|
|
|
|
|
|
if ((view = find_mapped_addr( process, req->addr )) && get_view_nt_name( view, &name ))
|
|
|
|
{
|
|
|
|
reply->len = name.len;
|
2021-02-11 11:01:28 +01:00
|
|
|
if (name.len > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
|
|
|
|
else if (!name.len) set_error( STATUS_FILE_INVALID );
|
|
|
|
else set_reply_data( name.str, name.len );
|
2021-02-10 16:09:49 +01:00
|
|
|
}
|
|
|
|
else set_error( STATUS_INVALID_ADDRESS );
|
|
|
|
|
|
|
|
release_object( process );
|
|
|
|
}
|