forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create hardlinks to the main executable and each loaded dll in subdirectories of /var/run/cygfork/, if that one exists on the NTFS file system. The directory names consist of the user sid, the main executable's NTFS IndexNumber, and the most recent LastWriteTime of all involved binaries (dlls and main executable). Next to the main.exe hardlink we create the empty file main.exe.local to enable dll redirection. The name of the mutex to synchronize hardlink creation/cleanup also is assembled from these directory names, to allow for synchronized cleanup of even orphaned hardlink directories. The hardlink to each dynamically loaded dll goes into another directory, named using the NTFS IndexNumber of the dll's original directory. * Makefile.in (DLL_OFILES): Add forkable.o. * dll_init.h (struct dll): Declare member variables fbi, fii, forkable_ntname. Declare methods nominate_forkable, create_forkable. (struct dll_list): Declare enum forkables_needs. Declare member variables forkables_dirx_size, forkables_dirx_ntname, forkables_mutex_name, forkables_mutex. Declare private methods forkable_ntnamesize, prepare_forkables_nomination, update_forkables_needs, update_forkables, create_forkables, denominate_forkables, close_mutex, try_remove_forkables, set_forkables_inheritance, request_forkables. Declare public static methods ntopenfile, read_fii, read_fbi. Declare public methods release_forkables, cleanup_forkables. Define public inline method setup_forkables. * dll_init.cc (dll_list::alloc): Allocate memory to hold the name of the hardlink in struct dll member forkable_ntname. Initialize struct dll members fbi, fii. (dll_list::load_after_fork): Call release_forkables method. * fork.cc: Rename public fork function to static dofork, add with_forkables as bool pointer parameter. Add new fork function calling dofork. (struct frok): Add bool pointer member with_forkables, add as constructor parameter. (frok::parent): Call dlls.setup_forkables before CreateProcessW, dlls.release_forkables afterwards. * pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables. * syscalls.cc (_unlink_nt): Rename public unlink_nt function to static _unlink_nt, with 'shareable' as additional argument. (unlink_nt): New, wrap _unlink_nt for original behaviour. (unlink_nt_shareable): New, wrap _unlink_nt to keep a binary file still loadable while removing one of its hardlinks. * forkable.cc: New file. Implement static functions mkdirs, rmdirs, rmdirs_synchronized, stat_real_file_once, format_IndexNumber, rootname, sidname, exename, lwtimename. Define static array forkable_nameparts. (struct dll): Implement nominate_forkable, create_forkable. (struct dll_list): Implement static methods ntopenfile, read_fii, read_fbi. Implement forkable_ntnamesize,
This commit is contained in:
committed by
Corinna Vinschen
parent
dac0b6826b
commit
8ddb1f60c8
@@ -300,6 +300,7 @@ dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
|
||||
PWCHAR ntname = nt_max_path_buf ();
|
||||
GetModuleFileNameW (h, ntname, NT_MAX_PATH);
|
||||
PWCHAR modname = form_ntname (ntname, NT_MAX_PATH, ntname);
|
||||
DWORD ntnamelen = modname - ntname;
|
||||
while (modname > ntname && *(modname - 1) != L'\\')
|
||||
--modname;
|
||||
|
||||
@@ -334,6 +335,12 @@ dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t forkntsize = forkable_ntnamesize (type, ntname, modname);
|
||||
|
||||
/* FIXME: Change this to new at some point. */
|
||||
d = (dll *) cmalloc (HEAP_2_DLL, sizeof (*d)
|
||||
+ ((ntnamelen + forkntsize) * sizeof (*ntname)));
|
||||
|
||||
/* Now we've allocated a block of information. Fill it in with the
|
||||
supplied info about this DLL. */
|
||||
wcscpy (d->ntname, ntname);
|
||||
@@ -348,6 +355,15 @@ dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
|
||||
d->image_size = ((pefile*)h)->optional_hdr ()->SizeOfImage;
|
||||
d->preferred_base = (void*) ((pefile*)h)->optional_hdr()->ImageBase;
|
||||
d->type = type;
|
||||
d->fbi.FileAttributes = INVALID_FILE_ATTRIBUTES;
|
||||
d->fii.IndexNumber.QuadPart = -1LL;
|
||||
if (!forkntsize)
|
||||
d->forkable_ntname = NULL;
|
||||
else
|
||||
{
|
||||
d->forkable_ntname = d->ntname + ntnamelen + 1;
|
||||
*d->forkable_ntname = L'\0';
|
||||
}
|
||||
append (d);
|
||||
if (type == DLL_LOAD)
|
||||
loaded_dlls++;
|
||||
@@ -654,6 +670,8 @@ dll_list::reserve_space ()
|
||||
void
|
||||
dll_list::load_after_fork (HANDLE parent)
|
||||
{
|
||||
release_forkables ();
|
||||
|
||||
// moved to frok::child for performance reasons:
|
||||
// dll_list::reserve_space();
|
||||
|
||||
|
Reference in New Issue
Block a user