Commit Graph

12 Commits

Author SHA1 Message Date
Michael Haubenwallner b6ebca61e3 Cygwin: dll_list: drop unused read_fbi method 2019-05-05 10:04:43 -04:00
Michael Haubenwallner e33db96a14 Cygwin: dll_list: query dll file id at load time
NtQueryVirtualMemory for MemorySectionName does not reliable return the
changed dll file name when another process does move the file around,
and we may end up creating forkable hardlinks to wrong dll files.
So query the file id when loading the dll rather than before fork.
2019-04-30 18:10:08 +02:00
Michael Haubenwallner 0f5776c47c Cygwin: dll_list: stat_real_file_once as dll method
Make stat_real_file_once a method of struct dll, to be more flexible on
where to use.  Also, debug print memory section name queried for a dll.
This is a preparation to query the file id when loading the dll.
2019-04-30 18:10:08 +02:00
Michael Haubenwallner 4c79da8017 Cygwin: dll_list: drop FILE_BASIC_INFORMATION
Querying FILE_BASIC_INFORMATION is needless since using win pid+threadid
for forkables dirname rather than newest last write time.
2019-04-30 18:10:08 +02:00
Michael Haubenwallner 204efa6bba Cygwin: use win pid+threadid for forkables dirname
Rather than newest last write time of all dlls loaded, use the forking
process' windows pid and windows thread id as directory name to create
the forkable hardlinks into.  While this may create hardlinks more
often, it does avoid conflicts between dlls not having the newest last
write time.
2019-04-12 20:01:04 +02:00
Corinna Vinschen 3b3ba558e9 Cygwin: use NULL security descriptor in InitializeObjectAttributes
Using sec_none{_nih} is just a roundabout way to specify a
NULL SD.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-25 21:06:15 +01:00
Michael Haubenwallner 3956ddd9bf forkables: hardlink without WRITE_ATTRIBUTES first
When the current process has renamed (to bin) a readonly dll, we get
STATUS_TRANSACTION_NOT_ACTIVE for unknown reason when subsequently
creating the forkable hardlink. A workaround is to open the original
file with FILE_WRITE_ATTRIBUTES access, but that fails with permission
denied for users not owning the original file.

* forkable.cc (dll::create_forkable): Retry hardlink creation using the
original file's handle opened with FILE_WRITE_ATTRIBUTES access when the
first attempt fails with STATUS_TRANSACTION_NOT_ACTIVE.
2019-02-07 15:58:02 +01:00
Michael Haubenwallner 22d68bada3 forkables: inline dll_list::forkables_supported
And LONG fits better for shared_info member forkable_hardlink_support.
2019-02-07 15:58:02 +01:00
Michael Haubenwallner 135577f708 forkables: simplify disabling via shm
* Rename cygwin_shared->prefer_forkable_hardlinks to
  forkable_hardlink_support, with values
  0 for Unknown, 1 for Supported, -1 for Unsupported.
  Upon first dll loaded ever, dll_list::forkable_ntnamesize checks the
  /var/run/cygfork directory to both exist and reside on NTFS, setting
  cygwin_shared->forkable_hardlink_support accordingly.
* Replace enum forkables_needs by bool forkables_created: Set
  to True by request_forkables after creating forkable hardlinks.
2019-02-07 15:58:02 +01:00
Michael Haubenwallner 8bbb3d3a23 forkables: use dynloaded dll's IndexNumber as dirname 2019-02-07 15:58:02 +01:00
Michael Haubenwallner 5a41aa6f4d forkables: Keep hardlinks disabled via shared mem.
To avoid the need for each process to check the filesystem to detect
that hardlink creation is impossible or disabled, cache this fact in
shared memory.  Removing cygfork directory while in use does disable
hardlinks creation.  To (re-)enable hardlinks creation, the cygfork
directory has to exist before the first cygwin process does fork.

	* forkable.cc (dll_list::forkable_ntnamesize): Short cut
	forkables needs to impossible when disabled via shared memory.
	(dll_list::update_forkables_needs): When detecting hardlink
	creation as impossible (not on NTFS) while still (we are the
	first one checking) enabled via shared memory, disable the
	shared memory value.
	(dll_list::request_forkables): Disable the shared memory value
	when hardlinks creation became disabled, that is when the
	cygfork directory was removed.
	* include/cygwin/version.h: Bump CYGWIN_VERSION_SHARED_DATA 6.
	* shared_info.h (struct shared_info): Add member
	prefer_forkable_hardlinks.  Update CURR_SHARED_MAGIC.
	* shared.cc (shared_info::initialize): Initialize
	prefer_forkable_hardlinks to 1 (Yes).
2019-02-07 15:58:02 +01:00
Michael Haubenwallner 8ddb1f60c8 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,
2019-02-07 15:58:02 +01:00