|
| 1 | +From da83e4d22ab055d33c2985889c3487e23359d3ba Mon Sep 17 00:00:00 2001 |
| 2 | +From: Corinna Vinschen < [email protected]> |
| 3 | +Date: Fri, 8 Mar 2024 20:57:06 +0100 |
| 4 | +Subject: [PATCH 80/N] Cygwin: try to avoid recalling offline files |
| 5 | + |
| 6 | +Chances are high that Cygwin recalls offline files from remote |
| 7 | +storage, even if the file is only accessed during stat(2) or |
| 8 | +readdir(3). |
| 9 | + |
| 10 | +To avoid this |
| 11 | +- make sure Cygwin is placeholder-aware, |
| 12 | +- open files in path_conv handling, as well as in stat(2)/readdir(3) |
| 13 | + scenarios with FILE_OPEN_NO_RECALL, and |
| 14 | +- during symlink checking or testing for executablility, don't even |
| 15 | + try to open the file if one of the OFFLINE attributes is set. |
| 16 | + |
| 17 | +Reported-by: Marcin Wisnicki < [email protected]> |
| 18 | +Signed-off-by: Corinna Vinschen < [email protected]> |
| 19 | +Signed-off-by: Johannes Schindelin < [email protected]> |
| 20 | +--- |
| 21 | + winsup/cygwin/autoload.cc | 1 + |
| 22 | + winsup/cygwin/dcrt0.cc | 3 +++ |
| 23 | + winsup/cygwin/fhandler/disk_file.cc | 20 ++++++++++++++------ |
| 24 | + winsup/cygwin/local_includes/ntdll.h | 8 ++++++++ |
| 25 | + winsup/cygwin/local_includes/path.h | 16 +++++++++++++++- |
| 26 | + winsup/cygwin/local_includes/winlean.h | 8 ++++++++ |
| 27 | + winsup/cygwin/path.cc | 17 +++++++++++++---- |
| 28 | + 7 files changed, 62 insertions(+), 11 deletions(-) |
| 29 | + |
| 30 | +diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc |
| 31 | +index cdf6e75..c579bef 100644 |
| 32 | +--- a/winsup/cygwin/autoload.cc |
| 33 | ++++ b/winsup/cygwin/autoload.cc |
| 34 | +@@ -477,6 +477,7 @@ LoadDLLfuncEx (SetThreadDescription, 8, KernelBase, 1) |
| 35 | + LoadDLLfunc (VirtualAlloc2, 28, KernelBase) |
| 36 | + |
| 37 | + LoadDLLfunc (NtMapViewOfSectionEx, 36, ntdll) |
| 38 | ++LoadDLLfuncEx (RtlSetProcessPlaceholderCompatibilityMode, 4, ntdll, 1) |
| 39 | + |
| 40 | + LoadDLLfunc (ldap_bind_s, 0, wldap32) |
| 41 | + LoadDLLfunc (ldap_count_entries, 0, wldap32) |
| 42 | +diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc |
| 43 | +index 84a1cda..f4d79b9 100644 |
| 44 | +--- a/winsup/cygwin/dcrt0.cc |
| 45 | ++++ b/winsup/cygwin/dcrt0.cc |
| 46 | +@@ -821,6 +821,9 @@ dll_crt0_1 (void *) |
| 47 | + if (dynamically_loaded) |
| 48 | + sigproc_init (); |
| 49 | + |
| 50 | ++ /* Call this before accessing any files. */ |
| 51 | ++ RtlSetProcessPlaceholderCompatibilityMode (PHCM_EXPOSE_PLACEHOLDERS); |
| 52 | ++ |
| 53 | + check_sanity_and_sync (user_data); |
| 54 | + |
| 55 | + /* Initialize malloc and then call user_shared_initialize since it relies |
| 56 | +diff --git a/winsup/cygwin/fhandler/disk_file.cc b/winsup/cygwin/fhandler/disk_file.cc |
| 57 | +index 470e9d4..2d39066 100644 |
| 58 | +--- a/winsup/cygwin/fhandler/disk_file.cc |
| 59 | ++++ b/winsup/cygwin/fhandler/disk_file.cc |
| 60 | +@@ -175,7 +175,9 @@ readdir_check_reparse_point (POBJECT_ATTRIBUTES attr, bool remote) |
| 61 | + bool ret = false; |
| 62 | + |
| 63 | + status = NtOpenFile (&reph, READ_CONTROL, attr, &io, FILE_SHARE_VALID_FLAGS, |
| 64 | +- FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REPARSE_POINT); |
| 65 | ++ FILE_OPEN_NO_RECALL |
| 66 | ++ | FILE_OPEN_FOR_BACKUP_INTENT |
| 67 | ++ | FILE_OPEN_REPARSE_POINT); |
| 68 | + if (NT_SUCCESS (status)) |
| 69 | + { |
| 70 | + PREPARSE_DATA_BUFFER rp = (PREPARSE_DATA_BUFFER) tp.c_get (); |
| 71 | +@@ -326,6 +328,7 @@ fhandler_base::fstat_by_name (struct stat *buf) |
| 72 | + status = NtOpenFile (&dir, SYNCHRONIZE | FILE_LIST_DIRECTORY, |
| 73 | + &attr, &io, FILE_SHARE_VALID_FLAGS, |
| 74 | + FILE_SYNCHRONOUS_IO_NONALERT |
| 75 | ++ | FILE_OPEN_NO_RECALL |
| 76 | + | FILE_OPEN_FOR_BACKUP_INTENT |
| 77 | + | FILE_DIRECTORY_FILE); |
| 78 | + if (!NT_SUCCESS (status)) |
| 79 | +@@ -609,7 +612,8 @@ fhandler_disk_file::fstatvfs (struct statvfs *sfs) |
| 80 | + opened = NT_SUCCESS (NtOpenFile (&fh, READ_CONTROL, |
| 81 | + pc.get_object_attr (attr, sec_none_nih), |
| 82 | + &io, FILE_SHARE_VALID_FLAGS, |
| 83 | +- FILE_OPEN_FOR_BACKUP_INTENT)); |
| 84 | ++ FILE_OPEN_NO_RECALL |
| 85 | ++ | FILE_OPEN_FOR_BACKUP_INTENT)); |
| 86 | + if (!opened) |
| 87 | + { |
| 88 | + /* Can't open file. Try again with parent dir. */ |
| 89 | +@@ -618,7 +622,8 @@ fhandler_disk_file::fstatvfs (struct statvfs *sfs) |
| 90 | + attr.ObjectName = &dirname; |
| 91 | + opened = NT_SUCCESS (NtOpenFile (&fh, READ_CONTROL, &attr, &io, |
| 92 | + FILE_SHARE_VALID_FLAGS, |
| 93 | +- FILE_OPEN_FOR_BACKUP_INTENT)); |
| 94 | ++ FILE_OPEN_NO_RECALL |
| 95 | ++ | FILE_OPEN_FOR_BACKUP_INTENT)); |
| 96 | + if (!opened) |
| 97 | + goto out; |
| 98 | + } |
| 99 | +@@ -2054,7 +2059,8 @@ readdir_get_ino (const char *path, bool dot_dot) |
| 100 | + || NT_SUCCESS (NtOpenFile (&hdl, READ_CONTROL, |
| 101 | + pc.get_object_attr (attr, sec_none_nih), |
| 102 | + &io, FILE_SHARE_VALID_FLAGS, |
| 103 | +- FILE_OPEN_FOR_BACKUP_INTENT |
| 104 | ++ FILE_OPEN_NO_RECALL |
| 105 | ++ | FILE_OPEN_FOR_BACKUP_INTENT |
| 106 | + | (pc.is_known_reparse_point () |
| 107 | + ? FILE_OPEN_REPARSE_POINT : 0))) |
| 108 | + ) |
| 109 | +@@ -2103,8 +2109,9 @@ fhandler_disk_file::readdir_helper (DIR *dir, dirent *de, DWORD w32_err, |
| 110 | + Mountpoints and unknown or unhandled reparse points will be treated |
| 111 | + as normal file/directory/unknown. In all cases, returning the INO of |
| 112 | + the reparse point (not of the target) matches behavior of posix systems. |
| 113 | ++ Unless the file is OFFLINE. *. |
| 114 | + */ |
| 115 | +- if (attr & FILE_ATTRIBUTE_REPARSE_POINT) |
| 116 | ++ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && !isoffline (attr)) |
| 117 | + { |
| 118 | + OBJECT_ATTRIBUTES oattr; |
| 119 | + |
| 120 | +@@ -2345,7 +2352,8 @@ go_ahead: |
| 121 | + &nfs_aol_ffei, sizeof nfs_aol_ffei) |
| 122 | + : NtOpenFile (&hdl, READ_CONTROL, &attr, &io, |
| 123 | + FILE_SHARE_VALID_FLAGS, |
| 124 | +- FILE_OPEN_FOR_BACKUP_INTENT |
| 125 | ++ FILE_OPEN_NO_RECALL |
| 126 | ++ | FILE_OPEN_FOR_BACKUP_INTENT |
| 127 | + | FILE_OPEN_REPARSE_POINT); |
| 128 | + if (NT_SUCCESS (f_status)) |
| 129 | + { |
| 130 | +diff --git a/winsup/cygwin/local_includes/ntdll.h b/winsup/cygwin/local_includes/ntdll.h |
| 131 | +index a4e8b88..aa1f3b9 100644 |
| 132 | +--- a/winsup/cygwin/local_includes/ntdll.h |
| 133 | ++++ b/winsup/cygwin/local_includes/ntdll.h |
| 134 | +@@ -159,6 +159,13 @@ extern GUID __cygwin_socket_guid; |
| 135 | + #define FILE_VC_QUOTAS_REBUILDING 0x00000200 |
| 136 | + #define FILE_VC_VALID_MASK 0x000003ff |
| 137 | + |
| 138 | ++#define PHCM_APPLICATION_DEFAULT 0 |
| 139 | ++#define PHCM_DISGUISE_PLACEHOLDER 1 |
| 140 | ++#define PHCM_EXPOSE_PLACEHOLDERS 2 |
| 141 | ++#define PHCM_MAX 2 |
| 142 | ++#define PHCM_ERROR_INVALID_PARAMETER -1 |
| 143 | ++#define PHCM_ERROR_NO_TEB -2 |
| 144 | ++ |
| 145 | + /* IOCTL code to impersonate client of named pipe. */ |
| 146 | + |
| 147 | + #define FSCTL_PIPE_DISCONNECT CTL_CODE(FILE_DEVICE_NAMED_PIPE, 1, \ |
| 148 | +@@ -1605,6 +1612,7 @@ extern "C" |
| 149 | + BOOLEAN); |
| 150 | + NTSTATUS RtlSetGroupSecurityDescriptor (PSECURITY_DESCRIPTOR, PSID, BOOLEAN); |
| 151 | + NTSTATUS RtlSetOwnerSecurityDescriptor (PSECURITY_DESCRIPTOR, PSID, BOOLEAN); |
| 152 | ++ CHAR RtlSetProcessPlaceholderCompatibilityMode (CHAR); |
| 153 | + PUCHAR RtlSubAuthorityCountSid (PSID); |
| 154 | + PULONG RtlSubAuthoritySid (PSID, ULONG); |
| 155 | + ULONG RtlUnicodeStringToAnsiSize (PUNICODE_STRING); |
| 156 | +diff --git a/winsup/cygwin/local_includes/path.h b/winsup/cygwin/local_includes/path.h |
| 157 | +index 74f831e..8c97c42 100644 |
| 158 | +--- a/winsup/cygwin/local_includes/path.h |
| 159 | ++++ b/winsup/cygwin/local_includes/path.h |
| 160 | +@@ -23,6 +23,14 @@ has_attribute (DWORD attributes, DWORD attribs_to_test) |
| 161 | + && (attributes & attribs_to_test); |
| 162 | + } |
| 163 | + |
| 164 | ++extern inline bool |
| 165 | ++isoffline (DWORD attributes) |
| 166 | ++{ |
| 167 | ++ return has_attribute (attributes, FILE_ATTRIBUTE_OFFLINE |
| 168 | ++ | FILE_ATTRIBUTE_RECALL_ON_OPEN |
| 169 | ++ | FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS); |
| 170 | ++} |
| 171 | ++ |
| 172 | + enum executable_states |
| 173 | + { |
| 174 | + is_executable, |
| 175 | +@@ -230,6 +238,12 @@ class path_conv |
| 176 | + bool exists () const {return fileattr != INVALID_FILE_ATTRIBUTES;} |
| 177 | + bool has_attribute (DWORD x) const {return exists () && (fileattr & x);} |
| 178 | + int isdir () const {return has_attribute (FILE_ATTRIBUTE_DIRECTORY);} |
| 179 | ++ bool isoffline () const |
| 180 | ++ { |
| 181 | ++ return has_attribute (FILE_ATTRIBUTE_OFFLINE |
| 182 | ++ | FILE_ATTRIBUTE_RECALL_ON_OPEN |
| 183 | ++ | FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS); |
| 184 | ++ } |
| 185 | + executable_states exec_state () |
| 186 | + { |
| 187 | + extern int _check_for_executable; |
| 188 | +@@ -237,7 +251,7 @@ class path_conv |
| 189 | + return is_executable; |
| 190 | + if (mount_flags & MOUNT_NOTEXEC) |
| 191 | + return not_executable; |
| 192 | +- if (!_check_for_executable) |
| 193 | ++ if (isoffline () || !_check_for_executable) |
| 194 | + return dont_care_if_executable; |
| 195 | + return dont_know_if_executable; |
| 196 | + } |
| 197 | +diff --git a/winsup/cygwin/local_includes/winlean.h b/winsup/cygwin/local_includes/winlean.h |
| 198 | +index 9b30b65..d4b4038 100644 |
| 199 | +--- a/winsup/cygwin/local_includes/winlean.h |
| 200 | ++++ b/winsup/cygwin/local_includes/winlean.h |
| 201 | +@@ -74,6 +74,14 @@ details. */ |
| 202 | + #undef CRITICAL |
| 203 | + #endif |
| 204 | + |
| 205 | ++/* Filesystem flags not yet supported by Mingw-w64 headers. */ |
| 206 | ++#ifndef FILE_ATTRIBUTE_RECALL_ON_OPEN |
| 207 | ++#define FILE_ATTRIBUTE_RECALL_ON_OPEN 0x00040000 |
| 208 | ++#endif |
| 209 | ++#ifndef FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS |
| 210 | ++#define FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS 0x00400000 |
| 211 | ++#endif |
| 212 | ++ |
| 213 | + /* So-called "Microsoft Account" SIDs (S-1-11-...) have a netbios domain name |
| 214 | + "MicrosoftAccounts". The new "Application Container SIDs" (S-1-15-...) |
| 215 | + have a netbios domain name "APPLICATION PACKAGE AUTHORITY" |
| 216 | +diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc |
| 217 | +index 30d444c..80f4c19 100644 |
| 218 | +--- a/winsup/cygwin/path.cc |
| 219 | ++++ b/winsup/cygwin/path.cc |
| 220 | +@@ -577,6 +577,7 @@ getfileattr (const char *path, bool caseinsensitive) /* path has to be always ab |
| 221 | + status = NtOpenFile (&dir, SYNCHRONIZE | FILE_LIST_DIRECTORY, |
| 222 | + &attr, &io, FILE_SHARE_VALID_FLAGS, |
| 223 | + FILE_SYNCHRONOUS_IO_NONALERT |
| 224 | ++ | FILE_OPEN_NO_RECALL |
| 225 | + | FILE_OPEN_FOR_BACKUP_INTENT |
| 226 | + | FILE_DIRECTORY_FILE); |
| 227 | + if (NT_SUCCESS (status)) |
| 228 | +@@ -3378,7 +3379,8 @@ restart: |
| 229 | + } |
| 230 | + status = NtOpenFile (&h, READ_CONTROL | FILE_READ_ATTRIBUTES, |
| 231 | + &attr, &io, FILE_SHARE_VALID_FLAGS, |
| 232 | +- FILE_OPEN_REPARSE_POINT |
| 233 | ++ FILE_OPEN_NO_RECALL |
| 234 | ++ | FILE_OPEN_REPARSE_POINT |
| 235 | + | FILE_OPEN_FOR_BACKUP_INTENT); |
| 236 | + debug_printf ("%y = NtOpenFile (no-EAs %S)", status, &upath); |
| 237 | + } |
| 238 | +@@ -3506,6 +3508,7 @@ restart: |
| 239 | + status = NtOpenFile (&dir, SYNCHRONIZE | FILE_LIST_DIRECTORY, |
| 240 | + &dattr, &io, FILE_SHARE_VALID_FLAGS, |
| 241 | + FILE_SYNCHRONOUS_IO_NONALERT |
| 242 | ++ | FILE_OPEN_NO_RECALL |
| 243 | + | FILE_OPEN_FOR_BACKUP_INTENT |
| 244 | + | FILE_DIRECTORY_FILE); |
| 245 | + if (!NT_SUCCESS (status)) |
| 246 | +@@ -3595,7 +3598,11 @@ restart: |
| 247 | + directory using a relative path, symlink evaluation goes totally |
| 248 | + awry. We never want a virtual drive evaluated as symlink. */ |
| 249 | + if (upath.Length <= 14) |
| 250 | +- goto file_not_symlink; |
| 251 | ++ goto file_not_symlink; |
| 252 | ++ |
| 253 | ++ /* Offline files, even if reparse points, are not symlinks. */ |
| 254 | ++ if (isoffline (fileattr)) |
| 255 | ++ goto file_not_symlink; |
| 256 | + |
| 257 | + /* Reparse points are potentially symlinks. This check must be |
| 258 | + performed before checking the SYSTEM attribute for sysfile |
| 259 | +@@ -3641,7 +3648,8 @@ restart: |
| 260 | + |
| 261 | + status = NtOpenFile (&sym_h, SYNCHRONIZE | GENERIC_READ, &attr, &io, |
| 262 | + FILE_SHARE_VALID_FLAGS, |
| 263 | +- FILE_OPEN_FOR_BACKUP_INTENT |
| 264 | ++ FILE_OPEN_NO_RECALL |
| 265 | ++ | FILE_OPEN_FOR_BACKUP_INTENT |
| 266 | + | FILE_SYNCHRONOUS_IO_NONALERT); |
| 267 | + if (!NT_SUCCESS (status)) |
| 268 | + res = 0; |
| 269 | +@@ -3685,7 +3693,8 @@ restart: |
| 270 | + |
| 271 | + status = NtOpenFile (&sym_h, SYNCHRONIZE | GENERIC_READ, &attr, &io, |
| 272 | + FILE_SHARE_VALID_FLAGS, |
| 273 | +- FILE_OPEN_FOR_BACKUP_INTENT |
| 274 | ++ FILE_OPEN_NO_RECALL |
| 275 | ++ | FILE_OPEN_FOR_BACKUP_INTENT |
| 276 | + | FILE_SYNCHRONOUS_IO_NONALERT); |
| 277 | + |
| 278 | + if (!NT_SUCCESS (status)) |
0 commit comments