| 5154 | } |
| 5155 | |
| 5156 | static bool win_runtime_directory_secure(const wchar_t *runtime_dir) { |
| 5157 | win_security_t security; |
| 5158 | if (!win_security_init(&security)) { |
| 5159 | return false; |
| 5160 | } |
| 5161 | bool created = CreateDirectoryW(runtime_dir, &security.directory_attributes) != 0; |
| 5162 | DWORD create_error = created ? ERROR_SUCCESS : GetLastError(); |
| 5163 | if (!created && create_error != ERROR_ALREADY_EXISTS) { |
| 5164 | char message[96]; |
| 5165 | (void)snprintf(message, sizeof(message), |
| 5166 | "could not create the directory (Windows error %lu)", |
| 5167 | (unsigned long)create_error); |
| 5168 | ipc_validation_detail_set_for_path(runtime_dir, message); |
| 5169 | win_security_destroy(&security); |
| 5170 | return false; |
| 5171 | } |
| 5172 | DWORD attributes = GetFileAttributesW(runtime_dir); |
| 5173 | if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0 || |
| 5174 | (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { |
| 5175 | /* What already exists at this path is adopted only if it is a plain |
| 5176 | * directory. Say which rule refused it: the walk in front of this |
| 5177 | * function tolerates losing a creation race, so this check is what |
| 5178 | * stands between that tolerance and a planted file or junction. */ |
| 5179 | const char *rule = attributes == INVALID_FILE_ATTRIBUTES ? "cannot be inspected" |
| 5180 | : (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0 |
| 5181 | ? "exists but is not a directory" |
| 5182 | : "is a reparse point (junction or symlink)"; |
| 5183 | ipc_validation_detail_set_for_path(runtime_dir, rule); |
| 5184 | win_security_destroy(&security); |
| 5185 | return false; |
| 5186 | } |
| 5187 | HANDLE directory = |
| 5188 | CreateFileW(runtime_dir, READ_CONTROL | WRITE_DAC | WRITE_OWNER, |
| 5189 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, |
| 5190 | FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 5191 | bool can_write_owner = directory != INVALID_HANDLE_VALUE; |
| 5192 | if (!can_write_owner) { |
| 5193 | directory = |
| 5194 | CreateFileW(runtime_dir, READ_CONTROL | WRITE_DAC, |
| 5195 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, |
| 5196 | FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 5197 | } |
| 5198 | if (directory == INVALID_HANDLE_VALUE) { |
| 5199 | win_security_destroy(&security); |
| 5200 | return false; |
| 5201 | } |
| 5202 | BY_HANDLE_FILE_INFORMATION file_info; |
| 5203 | bool valid_handle = GetFileInformationByHandle(directory, &file_info) != 0 && |
| 5204 | (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && |
| 5205 | (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0; |
| 5206 | bool owner_exact = valid_handle && win_file_owner_secure(&security, directory, true); |
| 5207 | /* One-time normalization of the admin-group default-owner artifact: a |
| 5208 | * directory created by plain mkdir under an Administrators-default-owner |
| 5209 | * token (standard policy on Windows Server) is born owned by BUILTIN\ |
| 5210 | * Administrators even though it is this account's own private dir. A |
| 5211 | * TRUSTED owner (the launcher's directory policy: SYSTEM, Administrators, |
| 5212 | * TrustedInstaller) is re-stamped to the exact token user inside the same |
| 5213 | * repair that already re-protects the DACL; any other owner remains |
no test coverage detected